source: trunk/dns.cgi@ 13

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

/trunk

Checkpoint
Merging record add/edit pieces into one template/code block
Added debugging array @debugbits to push strings into rather than printing,

since we haven't spit out headers before the processing (this is so we can
redirect)

Added DNSDB::grpName
Removed obsolete defrecs.tmpl

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