source: trunk/dns.cgi@ 15

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

/trunk

checkpoint

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 17.3 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-15 12:51:42 +0000 (Tue, 15 Sep 2009) $
6# SVN revision $Rev: 15 $
7# Last update by $Author: kdeugau $
8###
[3]9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
[2]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
[13]24my @debugbits; # temp, to be spit out near the end of processing
25
[2]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...
[7]36my %webvar = $q->Vars;
[2]37
[13]38# persistent stuff needed on most/all pages
[2]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
[13]51my $group = ($webvar{grp} ? $webvar{grp} : 1);
[2]52# handle login redirect
53if ($webvar{action} && $webvar{action} eq 'login') {
[11]54 ##fixme: need to actually do a user/pass check
55 changepage(page => "domlist");
[2]56}
57
58my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
59my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
60
61# default
[3]62#my $perpage = 15;
[15]63my $perpage = 3;
[2]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
[15]80## Default page is a login page
81#my $page; # to be initialized as an HTML::Template entity sooner or later
[2]82
[3]83
84
[2]85# decide which page to spit out...
[15]86$webvar{page} = 'login' if !$webvar{page};
87#if (!$webvar{page}) {
88# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
89#} else {
90#}
[2]91
[15]92my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
93
[2]94$page->param(sid => $sid);
95
96if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
[3]97
98# hmm. seeing problems in some possibly-not-so-corner cases.
[10]99# this currently only handles "domain on", "domain off"
[3]100 if (defined($webvar{action})) {
101 domStatus($dbh,$webvar{id},$webvar{action});
102 }
103
[11]104 listdomains();
[2]105
106} elsif ($webvar{page} eq 'reclist') {
107
[4]108 # Handle record list for both default records (per-group) and live domain records
[2]109
110 $page->param(defrec => $webvar{defrec});
[4]111 $page->param(id => $webvar{id});
112 $page->param(curpage => 'reclist');
[2]113
[4]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
[12]122# fill the page-count and first-previous-next-last-all details
123 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
[7]124 fill_fpnla($count); # should put some params on this sub...
[4]125
126 $page->param(defrec => $webvar{defrec});
[2]127 if ($webvar{defrec} eq 'y') {
[12]128##fixme: hardcoded group
[2]129 showdomain('y',1);
130 } else {
131 showdomain('n',$webvar{id});
132 }
133
[4]134} elsif ($webvar{page} eq 'newdomain') {
[2]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
[11]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
[13]178} elsif ($webvar{page} eq 'record') {
179 if ($webvar{recact} eq 'new') {
[15]180 $page->param(todo => "Add record to");
181 $page->param(recact => "add");
[13]182 stdrecs();
[15]183 } elsif ($webvar{recact} eq 'add') {
184
185 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
186 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
187 push @recargs, $webvar{distance};
188 if ($webvar{type} == $reverse_typemap{SRV}) {
189 push @recargs, $webvar{weight};
190 push @recargs, $webvar{port};
191 }
192 }
193 # wtf?
194 # push @recargs,
195 my ($code,$msg) = addRec(@recargs);
196
197 if ($code eq 'OK') {
198 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
199# showdomain($webvar{defrec},$webvar{parentid});
200 # NB: should **really** redirect here, in case of reload. >_< eyowch.
201 } else {
202 $page->param(add_failed => 1);
203 $page->param(errmsg => $msg);
204 stdrecs($webvar{type}); # populate the form... er, mostly.
205 $page->param(name => $webvar{name});
206 $page->param(address => $webvar{address});
207 $page->param(distance => $webvar{distance})
208 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
209 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
210 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
211 }
212
213 $page->param(defrec => $webvar{defrec});
214
[13]215 } elsif ($webvar{recact} eq 'edit') {
[15]216
217 $page->param(todo => "Edit record in");
[13]218 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
219 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
220 $sth->execute($webvar{id});
221 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
222 $page->param(name => $host);
223 $page->param(address => $val);
224 $page->param(distance => $distance);
225 $page->param(weight => $weight);
226 $page->param(port => $port);
227 $page->param(ttl => $ttl);
228 stdrecs($type);
229 }
230 if ($webvar{defrec} eq 'y') {
231 $page->param(domain => "group ".grpName($dbh,$webvar{parentid}));
232 } else {
233 $page->param(domain => domainName($dbh,$webvar{parentid}));
234 }
235
[2]236} elsif ($webvar{page} eq 'newrec') {
[13]237 push @debugbits, "whee!\n";
[2]238
[3]239 # populate most fields as needed. (eg, type list.)
[13]240 stdrecs();
[2]241
242} elsif ($webvar{page} eq 'addrec') {
243
244 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
245 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
246 push @recargs, $webvar{distance};
247 if ($webvar{type} == $reverse_typemap{SRV}) {
248 push @recargs, $webvar{weight};
249 push @recargs, $webvar{port};
250 }
251 }
[13]252# wtf?
253# push @recargs,
[2]254 my ($code,$msg) = addRec(@recargs);
255
256 if ($code eq 'OK') {
257 showdomain($webvar{defrec},$webvar{parentid});
258# NB: should **really** redirect here, in case of reload. >_< eyowch.
259 } else {
260 $page->param(add_failed => 1);
261 $page->param(errmsg => $msg);
[13]262 stdrecs($webvar{type}); # populate the form... er, mostly.
[2]263 $page->param(name => $webvar{name});
264 $page->param(address => $webvar{address});
265 $page->param(distance => $webvar{distance})
266 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
267 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
268 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
269 }
270
[3]271 $page->param(defrec => $webvar{defrec});
272
[2]273} elsif ($webvar{page} eq 'conf_del') {
274
275 $page->param(id => $webvar{id});
276 $page->param(defrec => $webvar{defrec});
277
278 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
279
280} elsif ($webvar{page} eq 'delrec') {
281
282 $page->param(id => $webvar{id});
283 $page->param(defrec => $webvar{defrec});
284 # first pass = confirm y/n (sorta)
285 if (!defined($webvar{del})) {
286 $page->param(del_getconf => 1);
[3]287 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
288 $page->param(host => $rec{host});
289 $page->param(ftype => $typemap{$rec{type}});
290 $page->param(recval => $rec{val});
[2]291 } else {
[3]292 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
293 if ($code ne 'OK') {
294## need to find failure mode
295 $page->param(del_failed => 1);
296 $page->param(errmsg => $msg);
297 }
298##fixme: group/parent instead of hardcoded 1
299 showdomain('y',1);
[2]300 }
301
302} elsif ($webvar{page} eq 'editsoa') {
303
304 fillsoa($webvar{defrec},$webvar{recid});
305
306} elsif ($webvar{page} eq 'updatesoa') {
307 print "ooooo!\n";
308
309 my $sth;
310 my $sql = '';
311 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
312 # plus a bit of magic to update the appropriate table
313 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
314 " set host='$webvar{prins}:$webvar{contact}',".
315 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
316 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
317 $sth = $dbh->prepare($sql);
318 $sth->execute;
319
320 if ($sth->err) {
321 $page->param(update_failed => 1);
322 $page->param(msg => $DBI::errstr);
323 fillsoa($webvar{defrec},1);
324 } else {
325 $page->param(update_failed => 0);
326##fixme! need to set group ID properly here
327 showdomain('y',1);
328 }
329
330} elsif ($webvar{page} eq 'adddomain') {
331 # Need some magic here.
332
333##fixme: Group should be variable
334 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
335
336# hokay, a bit of magic to decide which page we hit.
337 if ($code eq 'OK') {
[11]338# redirect to dns.cgi?etc&page=reclist
[12]339 changepage(page => "reclist", id => $msg);
[3]340 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
341 showdomain(0,$msg);
342##work
[2]343 } else {
344# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
345 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
346 $page->param(add_failed => 1);
347 $page->param(domain => $webvar{domain});
348 $page->param(errmsg => $msg);
349 }
350
351
352}
353
354
[7]355## hmm. may want to move this so we can redirect after adding/deleting/etc
356print "Content-type: text/html\n\n", $header->output;
357
[13]358foreach (@debugbits) { print; }
359
[15]360$page->param(grp => $group) if $webvar{page} ne 'login';
[13]361
[2]362# spit it out
363print $page->output;
364
365print "<div id=debug>webvar keys: <pre>\n";
366foreach my $key (keys %webvar) {
367 print "key: $key\tval: $webvar{$key}\n";
368}
369print "</pre>\nENV:\n<pre>\n";
370foreach my $key (keys %ENV) {
371 print "key: $key\tval: $ENV{$key}\n";
372}
373print "</pre></div>\n";
374
375print $footer->output;
376
377
378exit 0;
379
380
[11]381sub changepage {
382 my %params = @_; # think this works the way I want...
383
384 # handle user check
385 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
386 foreach (keys %params) {
387 $newurl .= "&$_=$params{$_}";
388 }
389
390 print "Status: 302\nLocation: $newurl\n\n";
391 exit;
392} # end changepage
393
394
[2]395sub fillsoa {
396 my $def = shift;
397 my $id = shift;
398 my $domname;
399
400 if ($webvar{domain} == 0) {
401 $domname = "DOMAIN";
402 } else {
403 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
404 $sth->execute();
405 ($domname) = $sth->fetchrow_array();
406 }
407
408 $page->param(domain => $domname);
409 $page->param(defrec => !$webvar{domain});
410 $page->param(group => $DNSDB::group);
411
412# defaults
413 $page->param(defcontact => $DNSDB::def{contact});
414 $page->param(defns => $DNSDB::def{prins});
415 $page->param(defsoattl => $DNSDB::def{soattl});
416 $page->param(defrefresh => $DNSDB::def{refresh});
417 $page->param(defretry => $DNSDB::def{retry});
418 $page->param(defexpire => $DNSDB::def{expire});
419 $page->param(defminttl => $DNSDB::def{minttl});
420
421 # there are probably better ways to do this. TMTOWTDI.
422 my %soa = getSOA($dbh,$def,$id);
423
424 $page->param(domainid => $webvar{domain});
425 $page->param(recid => $soa{recid});
426 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
427 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
428 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
429 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
430 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
431 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
432 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
433}
434
435sub showdomain {
436 my $def = shift;
437 my $id = shift;
438
439 # get the SOA first
440 my %soa = getSOA($dbh,$def,$id);
441
442 $page->param(recid => $soa{recid});
443 $page->param(contact => $soa{contact});
444 $page->param(prins => $soa{prins});
445 $page->param(refresh => $soa{refresh});
446 $page->param(retry => $soa{retry});
447 $page->param(expire => $soa{expire});
448 $page->param(minttl => $soa{minttl});
449 $page->param(ttl => $soa{ttl});
450
451# my @foo2 = getDomRecs($dbh,'def',1);
[4]452 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]453
454 my $row = 0;
455 foreach my $rec (@$foo2) {
456 $rec->{type} = $typemap{$rec->{type}};
457 $rec->{row} = $row % 2;
458 $rec->{defrec} = $webvar{defrec};
459 $rec->{sid} = $webvar{sid};
[13]460 $rec->{id} = $id;
[2]461 $row++;
462 }
463 $page->param(reclist => $foo2);
464}
465
[13]466sub stdrecs {
467 my $type = shift || $reverse_typemap{A};
468
[2]469 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
470 $sth->execute;
471
[13]472push @debugbits, "DEBUG: type $type\n";
[2]473 my @typelist;
474 while (my ($rval,$rname) = $sth->fetchrow_array()) {
475 my %row = ( recval => $rval, recname => $rname );
[13]476 $row{tselect} = 1 if $rval == $type;
[2]477 push @typelist, \%row;
478 }
479 $page->param(typelist => \@typelist);
480 $page->param(domain => domainName($dbh,$webvar{domainid}));
481 $page->param(defrec => $webvar{defrec});
482 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
[3]483 if ($webvar{defrec} eq 'y') {
484 ##fixme - should be groupid
485 $page->param(parentid => 1);
486 } else {
[4]487print "DEBUG: foobar: parentid $webvar{parentid}<br>\n";
[3]488 $page->param(parentid => $webvar{parentid});
489 }
[2]490}
[7]491
492sub fill_fpnla {
493 my $count = shift;
494##fixme
495 if ($offset eq 'all') {
496 #print "foo! wanna see'em all\n";
497 } else {
498 # all these bits only have sensible behaviour if offset is numeric. err, probably.
499 if ($count > $perpage) {
500 # if there are more results than the default, always show the "all" link
501 $page->param(navall => 1);
502
503 if ($offset > 0) {
504 $page->param(navfirst => 1);
505 $page->param(navprev => 1);
506 $page->param(prevoffs => $offset-1);
507 }
508
509 # show "next" and "last" links if we're not on the last page of results
510 if ( (($offset+1) * $perpage - $count) < 0 ) {
511 $page->param(navnext => 1);
512 $page->param(nextoffs => $offset+1);
513 $page->param(navlast => 1);
[8]514 $page->param(lastoffs => int (($count-1)/$perpage));
[7]515 }
516 }
517 }
[10]518} # end fill_fpnla()
519
520
[12]521sub fill_pgcount {
522 my $pgcount = shift;
523 my $pgtype = shift;
524 my $parent = shift;
525
526 $page->param(ntot => $pgcount);
527 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
528 $page->param(npglast => ($offset eq 'all' ? $pgcount :
529 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
530 ));
531 $page->param(pgtype => $pgtype);
532 $page->param(parent => $parent);
533} # end fill_pgcount()
534
535
[11]536sub listdomains {
[10]537 my $sth = $dbh->prepare("select count(*) from domains");
538 $sth->execute;
539 my ($count) = ($sth->fetchrow_array);
540
[12]541# fill page count and first-previous-next-last-all bits
542##fixme - hardcoded group bit
543 fill_pgcount($count,"domains","default group");
[10]544 fill_fpnla($count);
545
546##fixme - group
[13]547 $page->param(grp => 1);
[10]548 my @domlist;
[13]549 $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
[10]550 " inner join groups on domains.group_id=groups.group_id".
551 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
552 $sth->execute;
553 my $rownum = 0;
554 while (my @data = $sth->fetchrow_array) {
555 my %row;
556 $row{domainid} = $data[0];
557 $row{domain} = $data[1];
558 $row{status} = ($data[2] ? 'Active' : 'Inactive');
559 $row{group} = $data[3];
560 $row{bg} = ($rownum++)%2;
561# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
562 $row{mkactive} = !$data[2];
563 $row{sid} = $sid;
564 $row{offset} = $offset;
565##fixme: need to clean up status indicator/usage/inversion
566 push @domlist, \%row;
567 }
568 $page->param(domtable => \@domlist);
[11]569} # end listdomains()
Note: See TracBrowser for help on using the repository browser.