source: trunk/dns.cgi@ 16

Last change on this file since 16 was 16, 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: 18.0 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-15 21:52:13 +0000 (Tue, 15 Sep 2009) $
6# SVN revision $Rev: 16 $
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{grp} ? $webvar{grp} : 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',1);
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(defrec => $webvar{defrec});
212 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
213 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
214 $sth->execute($webvar{id});
215 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
216 $page->param(name => $host);
217 $page->param(address => $val);
218 $page->param(distance => $distance);
219 $page->param(weight => $weight);
220 $page->param(port => $port);
221 $page->param(ttl => $ttl);
222 fill_rectypes($type);
223
224 } elsif ($webvar{recact} eq 'update') {
225
226 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
227 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
228 $webvar{distance},$webvar{weight},$webvar{port});
229
230 if ($code eq 'OK') {
231 $page->param(failed => 1);
232 $page->param(errmsg => "testing");
233 $page->param(wastrying => "updating");
234 $page->param(todo => "Update record");
235 $page->param(recact => "update");
236 $page->param(parentid => $webvar{parentid});
237 $page->param(defrec => $webvar{defrec});
238 fill_recdata();
239# changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
240 } else {
241 $page->param(failed => 1);
242 $page->param(errmsg => $msg);
243 $page->param(wastrying => "updating");
244 $page->param(todo => "Update record");
245 $page->param(recact => "update");
246 $page->param(parentid => $webvar{parentid});
247 $page->param(defrec => $webvar{defrec});
248 fill_recdata();
249 }
250 }
251
252 if ($webvar{defrec} eq 'y') {
253 $page->param(dohere => "group ".grpName($dbh,$webvar{parentid}));
254 } else {
255 $page->param(dohere => domainName($dbh,$webvar{parentid}));
256 }
257
258} elsif ($webvar{page} eq 'newrec') {
259 push @debugbits, "whee!\n";
260
261 # populate most fields as needed. (eg, type list.)
262 stdrecs();
263
264} elsif ($webvar{page} eq 'addrec') {
265
266 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
267 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
268 push @recargs, $webvar{distance};
269 if ($webvar{type} == $reverse_typemap{SRV}) {
270 push @recargs, $webvar{weight};
271 push @recargs, $webvar{port};
272 }
273 }
274# wtf?
275# push @recargs,
276 my ($code,$msg) = addRec(@recargs);
277
278 if ($code eq 'OK') {
279 showdomain($webvar{defrec},$webvar{parentid});
280# NB: should **really** redirect here, in case of reload. >_< eyowch.
281 } else {
282 $page->param(add_failed => 1);
283 $page->param(errmsg => $msg);
284 stdrecs($webvar{type}); # populate the form... er, mostly.
285 $page->param(name => $webvar{name});
286 $page->param(address => $webvar{address});
287 $page->param(distance => $webvar{distance})
288 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
289 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
290 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
291 }
292
293 $page->param(defrec => $webvar{defrec});
294
295} elsif ($webvar{page} eq 'conf_del') {
296
297 $page->param(id => $webvar{id});
298 $page->param(defrec => $webvar{defrec});
299
300 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
301
302} elsif ($webvar{page} eq 'delrec') {
303
304 $page->param(id => $webvar{id});
305 $page->param(defrec => $webvar{defrec});
306 # first pass = confirm y/n (sorta)
307 if (!defined($webvar{del})) {
308 $page->param(del_getconf => 1);
309 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
310 $page->param(host => $rec{host});
311 $page->param(ftype => $typemap{$rec{type}});
312 $page->param(recval => $rec{val});
313 } else {
314 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
315 if ($code ne 'OK') {
316## need to find failure mode
317 $page->param(del_failed => 1);
318 $page->param(errmsg => $msg);
319 }
320##fixme: group/parent instead of hardcoded 1
321 showdomain('y',1);
322 }
323
324} elsif ($webvar{page} eq 'editsoa') {
325
326 fillsoa($webvar{defrec},$webvar{recid});
327
328} elsif ($webvar{page} eq 'updatesoa') {
329 print "ooooo!\n";
330
331 my $sth;
332 my $sql = '';
333 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
334 # plus a bit of magic to update the appropriate table
335 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
336 " set host='$webvar{prins}:$webvar{contact}',".
337 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
338 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
339 $sth = $dbh->prepare($sql);
340 $sth->execute;
341
342 if ($sth->err) {
343 $page->param(update_failed => 1);
344 $page->param(msg => $DBI::errstr);
345 fillsoa($webvar{defrec},1);
346 } else {
347 $page->param(update_failed => 0);
348##fixme! need to set group ID properly here
349 showdomain('y',1);
350 }
351
352} elsif ($webvar{page} eq 'adddomain') {
353 # Need some magic here.
354
355##fixme: Group should be variable
356 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
357
358# hokay, a bit of magic to decide which page we hit.
359 if ($code eq 'OK') {
360# redirect to dns.cgi?etc&page=reclist
361 changepage(page => "reclist", id => $msg);
362 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
363 showdomain(0,$msg);
364##work
365 } else {
366# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
367 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
368 $page->param(add_failed => 1);
369 $page->param(domain => $webvar{domain});
370 $page->param(errmsg => $msg);
371 }
372
373
374}
375
376
377## hmm. may want to move this so we can redirect after adding/deleting/etc
378print "Content-type: text/html\n\n", $header->output;
379
380foreach (@debugbits) { print; }
381
382$page->param(grp => $group) if $webvar{page} ne 'login';
383
384# spit it out
385print $page->output;
386
387print "<div id=debug>webvar keys: <pre>\n";
388foreach my $key (keys %webvar) {
389 print "key: $key\tval: $webvar{$key}\n";
390}
391print "</pre>\nENV:\n<pre>\n";
392foreach my $key (keys %ENV) {
393 print "key: $key\tval: $ENV{$key}\n";
394}
395print "</pre></div>\n";
396
397print $footer->output;
398
399
400exit 0;
401
402
403sub changepage {
404 my %params = @_; # think this works the way I want...
405
406 # handle user check
407 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
408 foreach (keys %params) {
409 $newurl .= "&$_=$params{$_}";
410 }
411
412 print "Status: 302\nLocation: $newurl\n\n";
413 exit;
414} # end changepage
415
416
417sub fillsoa {
418 my $def = shift;
419 my $id = shift;
420 my $domname;
421
422 if ($webvar{domain} == 0) {
423 $domname = "DOMAIN";
424 } else {
425 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
426 $sth->execute();
427 ($domname) = $sth->fetchrow_array();
428 }
429
430 $page->param(domain => $domname);
431 $page->param(defrec => !$webvar{domain});
432 $page->param(group => $DNSDB::group);
433
434# defaults
435 $page->param(defcontact => $DNSDB::def{contact});
436 $page->param(defns => $DNSDB::def{prins});
437 $page->param(defsoattl => $DNSDB::def{soattl});
438 $page->param(defrefresh => $DNSDB::def{refresh});
439 $page->param(defretry => $DNSDB::def{retry});
440 $page->param(defexpire => $DNSDB::def{expire});
441 $page->param(defminttl => $DNSDB::def{minttl});
442
443 # there are probably better ways to do this. TMTOWTDI.
444 my %soa = getSOA($dbh,$def,$id);
445
446 $page->param(domainid => $webvar{domain});
447 $page->param(recid => $soa{recid});
448 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
449 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
450 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
451 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
452 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
453 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
454 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
455}
456
457sub showdomain {
458 my $def = shift;
459 my $id = shift;
460
461 # get the SOA first
462 my %soa = getSOA($dbh,$def,$id);
463
464 $page->param(recid => $soa{recid});
465 $page->param(contact => $soa{contact});
466 $page->param(prins => $soa{prins});
467 $page->param(refresh => $soa{refresh});
468 $page->param(retry => $soa{retry});
469 $page->param(expire => $soa{expire});
470 $page->param(minttl => $soa{minttl});
471 $page->param(ttl => $soa{ttl});
472
473# my @foo2 = getDomRecs($dbh,'def',1);
474 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
475
476 my $row = 0;
477 foreach my $rec (@$foo2) {
478 $rec->{type} = $typemap{$rec->{type}};
479 $rec->{row} = $row % 2;
480 $rec->{defrec} = $webvar{defrec};
481 $rec->{sid} = $webvar{sid};
482 $rec->{id} = $id;
483 $row++;
484 }
485 $page->param(reclist => $foo2);
486}
487
488# fill in record type list on add/update/edit record template
489sub fill_rectypes {
490 my $type = shift || $reverse_typemap{A};
491
492 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
493 $sth->execute;
494push @debugbits, "type $type";
495 my @typelist;
496 while (my ($rval,$rname) = $sth->fetchrow_array()) {
497 my %row = ( recval => $rval, recname => $rname );
498 $row{tselect} = 1 if $rval == $type;
499 push @typelist, \%row;
500 }
501 $page->param(typelist => \@typelist);
502}
503
504sub fill_recdata {
505 fill_rectypes($webvar{type});
506
507 $page->param(name => $webvar{name});
508 $page->param(address => $webvar{address});
509 $page->param(distance => $webvar{distance})
510 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
511 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
512 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
513 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
514}
515
516sub fill_fpnla {
517 my $count = shift;
518##fixme
519 if ($offset eq 'all') {
520 #print "foo! wanna see'em all\n";
521 } else {
522 # all these bits only have sensible behaviour if offset is numeric. err, probably.
523 if ($count > $perpage) {
524 # if there are more results than the default, always show the "all" link
525 $page->param(navall => 1);
526
527 if ($offset > 0) {
528 $page->param(navfirst => 1);
529 $page->param(navprev => 1);
530 $page->param(prevoffs => $offset-1);
531 }
532
533 # show "next" and "last" links if we're not on the last page of results
534 if ( (($offset+1) * $perpage - $count) < 0 ) {
535 $page->param(navnext => 1);
536 $page->param(nextoffs => $offset+1);
537 $page->param(navlast => 1);
538 $page->param(lastoffs => int (($count-1)/$perpage));
539 }
540 }
541 }
542} # end fill_fpnla()
543
544
545sub fill_pgcount {
546 my $pgcount = shift;
547 my $pgtype = shift;
548 my $parent = shift;
549
550 $page->param(ntot => $pgcount);
551 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
552 $page->param(npglast => ($offset eq 'all' ? $pgcount :
553 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
554 ));
555 $page->param(pgtype => $pgtype);
556 $page->param(parent => $parent);
557} # end fill_pgcount()
558
559
560sub listdomains {
561 my $sth = $dbh->prepare("select count(*) from domains");
562 $sth->execute;
563 my ($count) = ($sth->fetchrow_array);
564
565# fill page count and first-previous-next-last-all bits
566##fixme - hardcoded group bit
567 fill_pgcount($count,"domains","default group");
568 fill_fpnla($count);
569
570##fixme - group
571 $page->param(grp => 1);
572 my @domlist;
573 $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
574 " inner join groups on domains.group_id=groups.group_id".
575 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
576 $sth->execute;
577 my $rownum = 0;
578 while (my @data = $sth->fetchrow_array) {
579 my %row;
580 $row{domainid} = $data[0];
581 $row{domain} = $data[1];
582 $row{status} = ($data[2] ? 'Active' : 'Inactive');
583 $row{group} = $data[3];
584 $row{bg} = ($rownum++)%2;
585# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
586 $row{mkactive} = !$data[2];
587 $row{sid} = $sid;
588 $row{offset} = $offset;
589##fixme: need to clean up status indicator/usage/inversion
590 push @domlist, \%row;
591 }
592 $page->param(domtable => \@domlist);
593} # end listdomains()
Note: See TracBrowser for help on using the repository browser.