source: trunk/dns.cgi@ 17

Last change on this file since 17 was 17, checked in by Kris Deugau, 15 years ago

/trunk

checkpoint
dns.cgi:

  • misc group-handling tweaks/fixme-fixes
  • misc code and SQL formatting normalizations
  • add group management list-the-groups page
  • extend common-template-parameters section

DNSDB.pm:

  • fix start point for returned records in record list
  • fix several return-on-error conditions for record add/update

templates/

  • add group management/comment to menu
  • use fpnla.tmpl in domlist.tmpl
  • misc minor fixups
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 19.0 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-30 20:54:10 +0000 (Wed, 30 Sep 2009) $
6# SVN revision $Rev: 17 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
18use DBI;
19
20use lib '.';
21# custom modules
22use DNSDB qw(:ALL);
23
24my @debugbits; # temp, to be spit out near the end of processing
25
26# Let's do these templates right...
27my $templatedir = "templates";
28my $sessiondir = "session";
29
30# Set up the CGI object...
31my $q = new CGI::Simple;
32# ... and get query-string params as well as POST params if necessary
33$q->parse_query_string;
34
35# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
36my %webvar = $q->Vars;
37
38# persistent stuff needed on most/all pages
39my $sid = ($webvar{sid} ? $webvar{sid} : undef);
40my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
41#$sid = $session->id() if !$sid;
42if (!$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
51my $group = ($webvar{group} ? $webvar{group} : 1);
52# handle login redirect
53if ($webvar{action} && $webvar{action} eq 'login') {
54 ##fixme: need to actually do a user/pass check
55 changepage(page => "domlist");
56}
57
58my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
59my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
60
61# default
62#my $perpage = 15;
63my $perpage = 3;
64my $offset = ($webvar{offset} ? $webvar{offset} : 0);
65
66# NB: these must match the field name and SQL ascend/descend syntax respectively
67my $sortfield = "domains";
68my $sortorder = "asc";
69
70my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
71#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
72# { AutoCommit => 0 }) or die $DBI::errstr;
73
74##fixme. PLEASE! <G>
75print $msg if !$dbh;
76
77# fiddle hardcoded "defaults" as per system/user (?) prefs
78initGlobals($dbh);
79
80## Default page is a login page
81#my $page; # to be initialized as an HTML::Template entity sooner or later
82
83
84
85# decide which page to spit out...
86$webvar{page} = 'login' if !$webvar{page};
87#if (!$webvar{page}) {
88# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
89#} else {
90#}
91
92my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
93
94$page->param(sid => $sid);
95
96if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
97
98# hmm. seeing problems in some possibly-not-so-corner cases.
99# this currently only handles "domain on", "domain off"
100 if (defined($webvar{action})) {
101 domStatus($dbh,$webvar{id},$webvar{action});
102 }
103
104 listdomains();
105
106} elsif ($webvar{page} eq 'reclist') {
107
108 # Handle record list for both default records (per-group) and live domain records
109
110 $page->param(defrec => $webvar{defrec});
111 $page->param(id => $webvar{id});
112 $page->param(curpage => 'reclist');
113
114# select count(*) from (default_)?records where (group|domain)_id=?
115 my $sth = $dbh->prepare("SELECT count(*) FROM ".
116 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
117 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
118 "AND NOT type=$reverse_typemap{SOA}");
119 $sth->execute($webvar{id});
120 my ($count) = ($sth->fetchrow_array);
121
122# fill the page-count and first-previous-next-last-all details
123 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
124 fill_fpnla($count); # should put some params on this sub...
125
126 $page->param(defrec => $webvar{defrec});
127 if ($webvar{defrec} eq 'y') {
128##fixme: hardcoded group
129 showdomain('y',$group);
130 } else {
131 showdomain('n',$webvar{id});
132 }
133
134} elsif ($webvar{page} eq 'newdomain') {
135
136 # weesa gonna discard parent_group_id for now
137 my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id");
138 $sth->execute;
139 my @grplist;
140 while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
141 my %row;
142 $row{grpname} = $grpname;
143 $row{grpval} = $grpid;
144##fixme: need magic
145# $row{defgrp} = '';
146 push @grplist, \%row;
147 }
148
149 $page->param(grplist => \@grplist);
150
151} elsif ($webvar{page} eq 'deldom') {
152
153 $page->param(id => $webvar{id});
154 # first pass = confirm y/n (sorta)
155 if (!defined($webvar{del})) {
156 $page->param(del_getconf => 1);
157 $page->param(domain => domainName($dbh,$webvar{id}));
158# print some neato things?
159
160# } else {
161# #whether actually deleting or cancelling we redirect to the domain list, default format
162
163 } elsif ($webvar{del} eq 'ok') {
164 my ($code,$msg) = delDomain($dbh, $webvar{id});
165 if ($code ne 'OK') {
166# need to find failure mode
167 $page->param(del_failed => 1);
168 $page->param(errmsg => $msg);
169 } else {
170 # success. go back to the domain list, do not pass "GO"
171 changepage(page => "domlist");
172 }
173 } else {
174 # cancelled. whee!
175 changepage(page => "domlist");
176 }
177
178} elsif ($webvar{page} eq 'record') {
179
180 if ($webvar{recact} eq 'new') {
181
182 $page->param(todo => "Add record to");
183 $page->param(recact => "add");
184 fill_rectypes();
185
186 } elsif ($webvar{recact} eq 'add') {
187
188 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
189 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
190 push @recargs, $webvar{distance};
191 if ($webvar{type} == $reverse_typemap{SRV}) {
192 push @recargs, $webvar{weight};
193 push @recargs, $webvar{port};
194 }
195 }
196 my ($code,$msg) = addRec(@recargs);
197
198 if ($code eq 'OK') {
199 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
200 } else {
201 $page->param(failed => 1);
202 $page->param(errmsg => $msg);
203 fill_recdata(); # populate the form... er, mostly.
204 }
205
206 } elsif ($webvar{recact} eq 'edit') {
207
208 $page->param(todo => "Update record");
209 $page->param(recact => "update");
210 $page->param(parentid => $webvar{parentid});
211 $page->param(id => $webvar{id});
212 $page->param(defrec => $webvar{defrec});
213 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
214 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
215 $sth->execute($webvar{id});
216 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
217 $page->param(name => $host);
218 $page->param(address => $val);
219 $page->param(distance => $distance);
220 $page->param(weight => $weight);
221 $page->param(port => $port);
222 $page->param(ttl => $ttl);
223 fill_rectypes($type);
224
225 } elsif ($webvar{recact} eq 'update') {
226
227 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
228 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
229 $webvar{distance},$webvar{weight},$webvar{port});
230
231 if ($code eq 'OK') {
232 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
233 } else {
234 $page->param(failed => 1);
235 $page->param(errmsg => $msg);
236 $page->param(wastrying => "updating");
237 $page->param(todo => "Update record");
238 $page->param(recact => "update");
239 $page->param(parentid => $webvar{parentid});
240 $page->param(defrec => $webvar{defrec});
241 $page->param(id => $webvar{id});
242 fill_recdata();
243 }
244 }
245
246 if ($webvar{defrec} eq 'y') {
247 $page->param(dohere => "default records in group ".grpName($dbh,$webvar{parentid}));
248 } else {
249 $page->param(dohere => domainName($dbh,$webvar{parentid}));
250 }
251
252} elsif ($webvar{page} eq 'newrec') {
253 push @debugbits, "whee!\n";
254
255 # populate most fields as needed. (eg, type list.)
256 stdrecs();
257
258} elsif ($webvar{page} eq 'addrec') {
259
260 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
261 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
262 push @recargs, $webvar{distance};
263 if ($webvar{type} == $reverse_typemap{SRV}) {
264 push @recargs, $webvar{weight};
265 push @recargs, $webvar{port};
266 }
267 }
268# wtf?
269# push @recargs,
270 my ($code,$msg) = addRec(@recargs);
271
272 if ($code eq 'OK') {
273 showdomain($webvar{defrec},$webvar{parentid});
274# NB: should **really** redirect here, in case of reload. >_< eyowch.
275 } else {
276 $page->param(add_failed => 1);
277 $page->param(errmsg => $msg);
278 stdrecs($webvar{type}); # populate the form... er, mostly.
279 $page->param(name => $webvar{name});
280 $page->param(address => $webvar{address});
281 $page->param(distance => $webvar{distance})
282 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
283 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
284 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
285 }
286
287 $page->param(defrec => $webvar{defrec});
288
289} elsif ($webvar{page} eq 'conf_del') {
290
291 $page->param(id => $webvar{id});
292 $page->param(defrec => $webvar{defrec});
293
294 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
295
296} elsif ($webvar{page} eq 'delrec') {
297
298 $page->param(id => $webvar{id});
299 $page->param(defrec => $webvar{defrec});
300 # first pass = confirm y/n (sorta)
301 if (!defined($webvar{del})) {
302 $page->param(del_getconf => 1);
303 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
304 $page->param(host => $rec{host});
305 $page->param(ftype => $typemap{$rec{type}});
306 $page->param(recval => $rec{val});
307 } else {
308 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
309 if ($code ne 'OK') {
310## need to find failure mode
311 $page->param(del_failed => 1);
312 $page->param(errmsg => $msg);
313 }
314##fixme: group/parent instead of hardcoded 1
315 showdomain('y',1);
316 }
317
318} elsif ($webvar{page} eq 'editsoa') {
319
320 fillsoa($webvar{defrec},$webvar{recid});
321
322} elsif ($webvar{page} eq 'updatesoa') {
323 print "ooooo!\n";
324
325 my $sth;
326 my $sql = '';
327 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
328 # plus a bit of magic to update the appropriate table
329 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
330 " set host='$webvar{prins}:$webvar{contact}',".
331 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
332 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
333 $sth = $dbh->prepare($sql);
334 $sth->execute;
335
336 if ($sth->err) {
337 $page->param(update_failed => 1);
338 $page->param(msg => $DBI::errstr);
339 fillsoa($webvar{defrec},1);
340 } else {
341 $page->param(update_failed => 0);
342##fixme! need to set group ID properly here
343 showdomain('y',1);
344 }
345
346} elsif ($webvar{page} eq 'adddomain') {
347 # Need some magic here.
348
349##fixme: Group should be variable
350 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
351
352# hokay, a bit of magic to decide which page we hit.
353 if ($code eq 'OK') {
354# redirect to dns.cgi?etc&page=reclist
355 changepage(page => "reclist", id => $msg);
356 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
357 showdomain(0,$msg);
358##work
359 } else {
360# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
361 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
362 $page->param(add_failed => 1);
363 $page->param(domain => $webvar{domain});
364 $page->param(errmsg => $msg);
365 }
366
367} elsif ($webvar{page} eq 'grpman') {
368
369 my $sth = $dbh->prepare("select count(*) from groups");
370 $sth->execute;
371 my ($count) = ($sth->fetchrow_array);
372
373# fill page count and first-previous-next-last-all bits
374##fixme - hardcoded group bit
375 fill_pgcount($count,"groups",'');
376 fill_fpnla($count);
377
378 my @grplist;
379 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
380 "count(distinct(u.email)), count(distinct(d.domain)) ".
381 "FROM groups g ".
382 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
383 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
384 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
385 "GROUP BY g.group_id, g.group_name, g2.group_name ".
386 "ORDER BY g.group_name".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
387
388 $sth->execute;
389 my $rownum = 0;
390 while (my @data = $sth->fetchrow_array) {
391 my %row;
392 $row{grpid} = $data[0];
393 $row{grpname} = $data[1];
394 $row{pgrp} = $data[2];
395 $row{nusers} = $data[3];
396 $row{ndomains} = $data[4];
397 $row{bg} = ($rownum++)%2;
398 $row{sid} = $sid;
399 push @grplist, \%row;
400 }
401 $page->param(grptable => \@grplist);
402
403} elsif ($webvar{page} eq 'newgrp') {
404
405}
406
407
408# start output here so we can redirect pages.
409print "Content-type: text/html\n\n", $header->output;
410
411foreach (@debugbits) { print; }
412
413# common bits
414if ($webvar{page} ne 'login') {
415 $page->param(grp => $group);
416 $page->param(grpname => grpName($dbh,$group));
417 $page->param(curpage => $webvar{page});
418}
419
420# spit it out
421print $page->output;
422
423print "<div id=debug>webvar keys: <pre>\n";
424foreach my $key (keys %webvar) {
425 print "key: $key\tval: $webvar{$key}\n";
426}
427print "</pre>\nENV:\n<pre>\n";
428foreach my $key (keys %ENV) {
429 print "key: $key\tval: $ENV{$key}\n";
430}
431print "</pre></div>\n";
432
433print $footer->output;
434
435
436exit 0;
437
438
439sub changepage {
440 my %params = @_; # think this works the way I want...
441
442 # handle user check
443 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
444 foreach (keys %params) {
445 $newurl .= "&$_=$params{$_}";
446 }
447
448 print "Status: 302\nLocation: $newurl\n\n";
449 exit;
450} # end changepage
451
452
453sub fillsoa {
454 my $def = shift;
455 my $id = shift;
456 my $domname;
457
458 if ($webvar{domain} == 0) {
459 $domname = "DOMAIN";
460 } else {
461 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
462 $sth->execute($webvar{domain});
463 ($domname) = $sth->fetchrow_array();
464 }
465
466 $page->param(domain => $domname);
467 $page->param(defrec => !$webvar{domain});
468 $page->param(group => $DNSDB::group);
469
470# defaults
471 $page->param(defcontact => $DNSDB::def{contact});
472 $page->param(defns => $DNSDB::def{prins});
473 $page->param(defsoattl => $DNSDB::def{soattl});
474 $page->param(defrefresh => $DNSDB::def{refresh});
475 $page->param(defretry => $DNSDB::def{retry});
476 $page->param(defexpire => $DNSDB::def{expire});
477 $page->param(defminttl => $DNSDB::def{minttl});
478
479 # there are probably better ways to do this. TMTOWTDI.
480 my %soa = getSOA($dbh,$def,$id);
481
482 $page->param(domainid => $webvar{domain});
483 $page->param(recid => $soa{recid});
484 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
485 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
486 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
487 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
488 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
489 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
490 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
491}
492
493sub showdomain {
494 my $def = shift;
495 my $id = shift;
496
497 # get the SOA first
498 my %soa = getSOA($dbh,$def,$id);
499
500 $page->param(recid => $soa{recid});
501 $page->param(contact => $soa{contact});
502 $page->param(prins => $soa{prins});
503 $page->param(refresh => $soa{refresh});
504 $page->param(retry => $soa{retry});
505 $page->param(expire => $soa{expire});
506 $page->param(minttl => $soa{minttl});
507 $page->param(ttl => $soa{ttl});
508
509# my @foo2 = getDomRecs($dbh,'def',1);
510 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
511
512 my $row = 0;
513 foreach my $rec (@$foo2) {
514 $rec->{type} = $typemap{$rec->{type}};
515 $rec->{row} = $row % 2;
516 $rec->{defrec} = $webvar{defrec};
517 $rec->{sid} = $webvar{sid};
518 $rec->{id} = $id;
519 $row++;
520 }
521 $page->param(reclist => $foo2);
522}
523
524# fill in record type list on add/update/edit record template
525sub fill_rectypes {
526 my $type = shift || $reverse_typemap{A};
527
528 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
529 $sth->execute;
530 my @typelist;
531 while (my ($rval,$rname) = $sth->fetchrow_array()) {
532 my %row = ( recval => $rval, recname => $rname );
533 $row{tselect} = 1 if $rval == $type;
534 push @typelist, \%row;
535 }
536 $page->param(typelist => \@typelist);
537}
538
539sub fill_recdata {
540 fill_rectypes($webvar{type});
541
542 $page->param(name => $webvar{name});
543 $page->param(address => $webvar{address});
544 $page->param(distance => $webvar{distance})
545 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
546 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
547 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
548 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
549}
550
551sub fill_fpnla {
552 my $count = shift;
553##fixme
554 if ($offset eq 'all') {
555 push @debugbits, "foo! wanna see'em all\n";
556 } else {
557 # all these bits only have sensible behaviour if offset is numeric. err, probably.
558 if ($count > $perpage) {
559 # if there are more results than the default, always show the "all" link
560 $page->param(navall => 1);
561
562 if ($offset > 0) {
563 $page->param(navfirst => 1);
564 $page->param(navprev => 1);
565 $page->param(prevoffs => $offset-1);
566 }
567
568 # show "next" and "last" links if we're not on the last page of results
569 if ( (($offset+1) * $perpage - $count) < 0 ) {
570 $page->param(navnext => 1);
571 $page->param(nextoffs => $offset+1);
572 $page->param(navlast => 1);
573 $page->param(lastoffs => int (($count-1)/$perpage));
574 }
575 }
576 }
577} # end fill_fpnla()
578
579
580sub fill_pgcount {
581 my $pgcount = shift;
582 my $pgtype = shift;
583 my $parent = shift;
584
585 $page->param(ntot => $pgcount);
586 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
587 $page->param(npglast => ($offset eq 'all' ? $pgcount :
588 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
589 ));
590 $page->param(pgtype => $pgtype);
591 $page->param(parent => $parent);
592} # end fill_pgcount()
593
594
595sub listdomains {
596
597 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
598 $sth->execute($group);
599 my ($count) = $sth->fetchrow_array;
600
601# fill page count and first-previous-next-last-all bits
602##fixme - hardcoded group bit
603 fill_pgcount($count,"domains","default group");
604 fill_fpnla($count);
605
606##fixme - group
607 $page->param(grp => 1);
608 my @domlist;
609 $sth = $dbh->prepare("select domain_id,domain,status,groups.group_name from domains".
610 " inner join groups on domains.group_id=groups.group_id".
611 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
612 $sth->execute;
613 my $rownum = 0;
614 while (my @data = $sth->fetchrow_array) {
615 my %row;
616 $row{domainid} = $data[0];
617 $row{domain} = $data[1];
618 $row{status} = ($data[2] ? 'Active' : 'Inactive');
619 $row{group} = $data[3];
620 $row{bg} = ($rownum++)%2;
621# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
622 $row{mkactive} = !$data[2];
623 $row{sid} = $sid;
624 $row{offset} = $offset;
625##fixme: need to clean up status indicator/usage/inversion
626 push @domlist, \%row;
627 }
628 $page->param(domtable => \@domlist);
629} # end listdomains()
Note: See TracBrowser for help on using the repository browser.