1 | #!/usr/bin/perl -w -T
|
---|
2 | # dns/cgi-bin/dns.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2009-08-17 17:43:40 +0000 (Mon, 17 Aug 2009) $
|
---|
6 | # SVN revision $Rev: 2 $
|
---|
7 | # Last update by $Author: kdeugau $
|
---|
8 | ###
|
---|
9 | # Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>
|
---|
10 |
|
---|
11 | use strict;
|
---|
12 | use warnings;
|
---|
13 |
|
---|
14 | use CGI::Carp qw (fatalsToBrowser);
|
---|
15 | use CGI::Simple;
|
---|
16 | use HTML::Template;
|
---|
17 | use CGI::Session;
|
---|
18 | use DBI;
|
---|
19 |
|
---|
20 | use lib '.';
|
---|
21 | # custom modules
|
---|
22 | use DNSDB qw(:ALL);
|
---|
23 |
|
---|
24 | # Let's do these templates right...
|
---|
25 | my $templatedir = "templates";
|
---|
26 | my $sessiondir = "session";
|
---|
27 |
|
---|
28 | # Set up the CGI object...
|
---|
29 | my $q = new CGI::Simple;
|
---|
30 | # ... and get query-string params as well as POST params if necessary
|
---|
31 | $q->parse_query_string;
|
---|
32 |
|
---|
33 | my %webvar;
|
---|
34 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
|
---|
35 | foreach ($q->param()) {
|
---|
36 | $webvar{$_} = $q->param($_);
|
---|
37 | }
|
---|
38 |
|
---|
39 | my $sid = ($webvar{sid} ? $webvar{sid} : undef);
|
---|
40 | my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
|
---|
41 | #$sid = $session->id() if !$sid;
|
---|
42 | if (!$sid) {
|
---|
43 | # init stuff. can probably axe this down to just above if'n'when user manipulation happens
|
---|
44 | $sid = $session->id();
|
---|
45 | # need to know the "upper" group the user can deal with; may as well
|
---|
46 | # stick this in the session rather than calling out to the DB every time.
|
---|
47 | $session->param('logingroupid',1);
|
---|
48 | $session->param('workinggroupid',1); # yes, we *do* need to track this too. er, probably.
|
---|
49 | }
|
---|
50 |
|
---|
51 | # handle login redirect
|
---|
52 | if ($webvar{action} && $webvar{action} eq 'login') {
|
---|
53 | # handle user check
|
---|
54 | my $newurl = "http://$ENV{HTTP_HOST}$ENV{REQUEST_URI}?sid=$sid&page=domlist";
|
---|
55 | print "Status: 302\nLocation: $newurl\n\n";
|
---|
56 | exit;
|
---|
57 | }
|
---|
58 |
|
---|
59 | my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
|
---|
60 | my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
|
---|
61 |
|
---|
62 | print "Content-type: text/html\n\n", $header->output;
|
---|
63 |
|
---|
64 | # default
|
---|
65 | my $perpage = 15;
|
---|
66 | my $offset = ($webvar{offset} ? $webvar{offset} : 0);
|
---|
67 |
|
---|
68 | # NB: these must match the field name and SQL ascend/descend syntax respectively
|
---|
69 | my $sortfield = "domains";
|
---|
70 | my $sortorder = "asc";
|
---|
71 |
|
---|
72 | my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
|
---|
73 | #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
|
---|
74 | # { AutoCommit => 0 }) or die $DBI::errstr;
|
---|
75 |
|
---|
76 | ##fixme. PLEASE! <G>
|
---|
77 | print $msg if !$dbh;
|
---|
78 |
|
---|
79 | # fiddle hardcoded "defaults" as per system/user (?) prefs
|
---|
80 | initGlobals($dbh);
|
---|
81 |
|
---|
82 | # Default page is a login page
|
---|
83 | my $page; # to be initialized as an HTML::Template entity sooner or later
|
---|
84 |
|
---|
85 | # decide which page to spit out...
|
---|
86 | if (!$webvar{page}) {
|
---|
87 | $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
|
---|
88 | } else {
|
---|
89 | $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
|
---|
90 | }
|
---|
91 |
|
---|
92 | $page->param(sid => $sid);
|
---|
93 |
|
---|
94 | if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
|
---|
95 | my $sth = $dbh->prepare("select count(*) from domains");
|
---|
96 | $sth->execute;
|
---|
97 | my ($count) = ($sth->fetchrow_array);
|
---|
98 |
|
---|
99 | if ($count > $perpage) {
|
---|
100 | # if there are more results than the default, always show the "all" link
|
---|
101 | $page->param(navall => 1);
|
---|
102 |
|
---|
103 | if ($offset > 0) {
|
---|
104 | $page->param(navfirst => 1);
|
---|
105 | $page->param(navprev => 1);
|
---|
106 | $page->param(prevoffs => $offset-1);
|
---|
107 | }
|
---|
108 |
|
---|
109 | # show "next" and "last" links if we're not on the last page of results
|
---|
110 | if ( (($offset+1) * $perpage - $count) < 0 ) {
|
---|
111 | $page->param(navnext => 1);
|
---|
112 | $page->param(nextoffs => $offset+1);
|
---|
113 | $page->param(navlast => 1);
|
---|
114 | $page->param(lastoffs => int $count/$perpage);
|
---|
115 | }
|
---|
116 | }
|
---|
117 |
|
---|
118 | $page->param(ndomains => $count);
|
---|
119 |
|
---|
120 | my @domlist;
|
---|
121 | $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
|
---|
122 | " inner join groups on domains.group_id=groups.group_id".
|
---|
123 | " order by domain limit $perpage offset ".$offset*$perpage);
|
---|
124 | $sth->execute;
|
---|
125 | my $rownum = 0;
|
---|
126 | while (my @data = $sth->fetchrow_array) {
|
---|
127 | my %row;
|
---|
128 | $row{domainid} = $data[0];
|
---|
129 | $row{domain} = $data[1];
|
---|
130 | $row{status} = $data[2];
|
---|
131 | $row{group} = $data[3];
|
---|
132 | $row{bg} = ($rownum++)%2;
|
---|
133 | $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
|
---|
134 | $row{sid} = $sid;
|
---|
135 | ##fixme: need to clean up status indicator/usage/inversion
|
---|
136 | push @domlist, \%row;
|
---|
137 | }
|
---|
138 | $page->param(domtable => \@domlist);
|
---|
139 |
|
---|
140 | } elsif ($webvar{page} eq 'reclist') {
|
---|
141 |
|
---|
142 | #} elsif ($webvar{page} eq 'domdetail') {
|
---|
143 |
|
---|
144 | $page->param(defrec => $webvar{defrec});
|
---|
145 |
|
---|
146 | ##fixme
|
---|
147 | $page->param(domain => domainName($dbh,$webvar{id}));
|
---|
148 | $page->param(grpid => 1);
|
---|
149 | ##fixme
|
---|
150 |
|
---|
151 | if ($webvar{defrec} eq 'y') {
|
---|
152 | $page->param(id => $webvar{grpid}); ##fixme, hack! hack!
|
---|
153 | showdomain('y',1);
|
---|
154 | } else {
|
---|
155 | $page->param(id => $webvar{id});
|
---|
156 | showdomain('n',$webvar{id});
|
---|
157 | }
|
---|
158 |
|
---|
159 | } elsif ($webvar{page} eq 'defrecs') {
|
---|
160 |
|
---|
161 | showdomain('def',1);
|
---|
162 | $page->param(defrec => 'y');
|
---|
163 |
|
---|
164 | my $sth = $dbh->prepare("select * from default_records");
|
---|
165 | $sth->execute;
|
---|
166 | print "<pre>\n";
|
---|
167 | while (my @data = $sth->fetchrow_array) {
|
---|
168 | foreach (@data) { print; print "\t" };
|
---|
169 | print "\n";
|
---|
170 | }
|
---|
171 | print "</pre>\n";
|
---|
172 |
|
---|
173 | } elsif ($webvar{page} eq 'newdomain') {
|
---|
174 | # weesa gonna discard parent_group_id for now
|
---|
175 | my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id");
|
---|
176 | $sth->execute;
|
---|
177 | my @grplist;
|
---|
178 | while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
|
---|
179 | my %row;
|
---|
180 | $row{grpname} = $grpname;
|
---|
181 | $row{grpval} = $grpid;
|
---|
182 | ##fixme: need magic
|
---|
183 | # $row{defgrp} = '';
|
---|
184 | push @grplist, \%row;
|
---|
185 | }
|
---|
186 |
|
---|
187 | $page->param(grplist => \@grplist);
|
---|
188 |
|
---|
189 | } elsif ($webvar{page} eq 'newrec') {
|
---|
190 | print "whee!\n";
|
---|
191 |
|
---|
192 | newrec();
|
---|
193 |
|
---|
194 | } elsif ($webvar{page} eq 'addrec') {
|
---|
195 | print "wanna add<br>DEBUG: $webvar{parentid}<br>\n";
|
---|
196 |
|
---|
197 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
|
---|
198 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
|
---|
199 | push @recargs, $webvar{distance};
|
---|
200 | if ($webvar{type} == $reverse_typemap{SRV}) {
|
---|
201 | push @recargs, $webvar{weight};
|
---|
202 | push @recargs, $webvar{port};
|
---|
203 | }
|
---|
204 | }
|
---|
205 | push @recargs,
|
---|
206 | my ($code,$msg) = addRec(@recargs);
|
---|
207 |
|
---|
208 | if ($code eq 'OK') {
|
---|
209 | showdomain($webvar{defrec},$webvar{parentid});
|
---|
210 | # NB: should **really** redirect here, in case of reload. >_< eyowch.
|
---|
211 | } else {
|
---|
212 | $page->param(add_failed => 1);
|
---|
213 | $page->param(errmsg => $msg);
|
---|
214 | newrec(); # populate the form... er, mostly.
|
---|
215 | $page->param(name => $webvar{name});
|
---|
216 | $page->param(address => $webvar{address});
|
---|
217 | $page->param(distance => $webvar{distance})
|
---|
218 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
---|
219 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
220 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
221 | }
|
---|
222 |
|
---|
223 | } elsif ($webvar{page} eq 'conf_del') {
|
---|
224 |
|
---|
225 | $page->param(id => $webvar{id});
|
---|
226 | $page->param(defrec => $webvar{defrec});
|
---|
227 |
|
---|
228 | my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
|
---|
229 |
|
---|
230 |
|
---|
231 | } elsif ($webvar{page} eq 'delrec') {
|
---|
232 |
|
---|
233 | $page->param(id => $webvar{id});
|
---|
234 | $page->param(defrec => $webvar{defrec});
|
---|
235 | # first pass = confirm y/n (sorta)
|
---|
236 | if (!defined($webvar{del})) {
|
---|
237 | $page->param(del_getconf => 1);
|
---|
238 | } else {
|
---|
239 | # $page->param(
|
---|
240 | }
|
---|
241 | ##work
|
---|
242 |
|
---|
243 | } elsif ($webvar{page} eq 'editsoa') {
|
---|
244 |
|
---|
245 | fillsoa($webvar{defrec},$webvar{recid});
|
---|
246 |
|
---|
247 | } elsif ($webvar{page} eq 'updatesoa') {
|
---|
248 | print "ooooo!\n";
|
---|
249 |
|
---|
250 | my $sth;
|
---|
251 | my $sql = '';
|
---|
252 | # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
|
---|
253 | # plus a bit of magic to update the appropriate table
|
---|
254 | $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
|
---|
255 | " set host='$webvar{prins}:$webvar{contact}',".
|
---|
256 | " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
|
---|
257 | " ttl=$webvar{ttl} where record_id=$webvar{recid}";
|
---|
258 | $sth = $dbh->prepare($sql);
|
---|
259 | $sth->execute;
|
---|
260 | print "DEBUG: $sql<br>\n";
|
---|
261 | #print "DEBUG: ".$DBI::errstr;
|
---|
262 |
|
---|
263 | if ($sth->err) {
|
---|
264 | $page->param(update_failed => 1);
|
---|
265 | $page->param(msg => $DBI::errstr);
|
---|
266 | fillsoa($webvar{defrec},1);
|
---|
267 | } else {
|
---|
268 | $page->param(update_failed => 0);
|
---|
269 | ##fixme! need to set group ID properly here
|
---|
270 | showdomain('y',1);
|
---|
271 | }
|
---|
272 |
|
---|
273 | } elsif ($webvar{page} eq 'adddomain') {
|
---|
274 | # Need some magic here.
|
---|
275 |
|
---|
276 | ##fixme: Group should be variable
|
---|
277 | my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
|
---|
278 |
|
---|
279 | # hokay, a bit of magic to decide which page we hit.
|
---|
280 | if ($code eq 'OK') {
|
---|
281 | $page = HTML::Template->new(filename => "$templatedir/domlist.tmpl");
|
---|
282 | } else {
|
---|
283 | # oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
|
---|
284 | $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
|
---|
285 | $page->param(add_failed => 1);
|
---|
286 | $page->param(domain => $webvar{domain});
|
---|
287 | $page->param(errmsg => $msg);
|
---|
288 | }
|
---|
289 |
|
---|
290 | # my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)");
|
---|
291 | # $sth->execute($webvar{domain},1,($webvar{makeactive} ? 'active' : 'inactive')) or die $DBI::errstr;
|
---|
292 | # $sth = $dbh->prepare("select domain_id from domains where domain='$webvar{domain}'");
|
---|
293 | # $sth->execute;
|
---|
294 | # my ($dom_id) = $sth->fetchrow_array();
|
---|
295 | # $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=1");
|
---|
296 | # my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)".
|
---|
297 | # " values ($dom_id,?,?,?,?,?)");
|
---|
298 | # $sth->execute;
|
---|
299 | # while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) {
|
---|
300 | # $host =~ s/DOMAIN/$webvar{domain}/g;
|
---|
301 | # $sth_in->execute($host,$type,$val,$dist,$ttl);
|
---|
302 | # }
|
---|
303 | # $dbh->commit or print "failed to add $webvar{domain}: ".$DBI::errstr."\n";
|
---|
304 |
|
---|
305 | # $page->param(add_failed => 1);
|
---|
306 | }
|
---|
307 |
|
---|
308 |
|
---|
309 | # spit it out
|
---|
310 | print $page->output;
|
---|
311 |
|
---|
312 | print "<div id=debug>webvar keys: <pre>\n";
|
---|
313 | foreach my $key (keys %webvar) {
|
---|
314 | print "key: $key\tval: $webvar{$key}\n";
|
---|
315 | }
|
---|
316 | print "</pre>\nENV:\n<pre>\n";
|
---|
317 | foreach my $key (keys %ENV) {
|
---|
318 | print "key: $key\tval: $ENV{$key}\n";
|
---|
319 | }
|
---|
320 | print "</pre></div>\n";
|
---|
321 |
|
---|
322 | print $footer->output;
|
---|
323 |
|
---|
324 |
|
---|
325 | exit 0;
|
---|
326 |
|
---|
327 |
|
---|
328 | sub fillsoa {
|
---|
329 | my $def = shift;
|
---|
330 | my $id = shift;
|
---|
331 | my $domname;
|
---|
332 |
|
---|
333 | if ($webvar{domain} == 0) {
|
---|
334 | $domname = "DOMAIN";
|
---|
335 | } else {
|
---|
336 | my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
|
---|
337 | $sth->execute();
|
---|
338 | ($domname) = $sth->fetchrow_array();
|
---|
339 | }
|
---|
340 |
|
---|
341 | $page->param(domain => $domname);
|
---|
342 | $page->param(defrec => !$webvar{domain});
|
---|
343 | $page->param(group => $DNSDB::group);
|
---|
344 |
|
---|
345 | # defaults
|
---|
346 | $page->param(defcontact => $DNSDB::def{contact});
|
---|
347 | $page->param(defns => $DNSDB::def{prins});
|
---|
348 | $page->param(defsoattl => $DNSDB::def{soattl});
|
---|
349 | $page->param(defrefresh => $DNSDB::def{refresh});
|
---|
350 | $page->param(defretry => $DNSDB::def{retry});
|
---|
351 | $page->param(defexpire => $DNSDB::def{expire});
|
---|
352 | $page->param(defminttl => $DNSDB::def{minttl});
|
---|
353 |
|
---|
354 | # there are probably better ways to do this. TMTOWTDI.
|
---|
355 | my %soa = getSOA($dbh,$def,$id);
|
---|
356 |
|
---|
357 | $page->param(domainid => $webvar{domain});
|
---|
358 | $page->param(recid => $soa{recid});
|
---|
359 | $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
|
---|
360 | $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
|
---|
361 | $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
|
---|
362 | $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
|
---|
363 | $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
|
---|
364 | $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
|
---|
365 | $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
|
---|
366 | }
|
---|
367 |
|
---|
368 | sub showdomain {
|
---|
369 | my $def = shift;
|
---|
370 | my $id = shift;
|
---|
371 |
|
---|
372 | # get the SOA first
|
---|
373 | my %soa = getSOA($dbh,$def,$id);
|
---|
374 |
|
---|
375 | $page->param(recid => $soa{recid});
|
---|
376 | $page->param(contact => $soa{contact});
|
---|
377 | $page->param(prins => $soa{prins});
|
---|
378 | $page->param(refresh => $soa{refresh});
|
---|
379 | $page->param(retry => $soa{retry});
|
---|
380 | $page->param(expire => $soa{expire});
|
---|
381 | $page->param(minttl => $soa{minttl});
|
---|
382 | $page->param(ttl => $soa{ttl});
|
---|
383 |
|
---|
384 | # my @foo2 = getDomRecs($dbh,'def',1);
|
---|
385 | my $foo2 = getDomRecs($dbh,$def,$id);
|
---|
386 |
|
---|
387 | my $row = 0;
|
---|
388 | foreach my $rec (@$foo2) {
|
---|
389 | $rec->{type} = $typemap{$rec->{type}};
|
---|
390 | $rec->{row} = $row % 2;
|
---|
391 | # Feh.
|
---|
392 | $rec->{defrec} = $webvar{defrec};
|
---|
393 | # And **FEH!!**
|
---|
394 | $rec->{sid} = $webvar{sid};
|
---|
395 | $row++;
|
---|
396 | }
|
---|
397 | $page->param(reclist => $foo2);
|
---|
398 | }
|
---|
399 |
|
---|
400 | sub newrec {
|
---|
401 | my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
|
---|
402 | $sth->execute;
|
---|
403 |
|
---|
404 | my @typelist;
|
---|
405 | while (my ($rval,$rname) = $sth->fetchrow_array()) {
|
---|
406 | my %row = ( recval => $rval, recname => $rname );
|
---|
407 | $row{tselect} = 1 if $rval == $webvar{type};
|
---|
408 | push @typelist, \%row;
|
---|
409 | }
|
---|
410 | $page->param(typelist => \@typelist);
|
---|
411 | $page->param(domain => domainName($dbh,$webvar{domainid}));
|
---|
412 | $page->param(parentid => $webvar{parentid});
|
---|
413 | $page->param(defrec => $webvar{defrec});
|
---|
414 | $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
|
---|
415 | }
|
---|