source: trunk/dns.cgi@ 10

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

/trunk

Move domain list generation into a sub; it's needed from several places
Confirm domain deletion works as expected; reenable actual deletion.
Misc comment-docs

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 14.0 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-10 17:34:34 +0000 (Thu, 10 Sep 2009) $
6# SVN revision $Rev: 10 $
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
24# Let's do these templates right...
25my $templatedir = "templates";
26my $sessiondir = "session";
27
28# Set up the CGI object...
29my $q = new CGI::Simple;
30# ... and get query-string params as well as POST params if necessary
31$q->parse_query_string;
32
33# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
[7]34my %webvar = $q->Vars;
[2]35
36my $sid = ($webvar{sid} ? $webvar{sid} : undef);
37my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
38#$sid = $session->id() if !$sid;
39if (!$sid) {
40 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
41 $sid = $session->id();
42# need to know the "upper" group the user can deal with; may as well
43# stick this in the session rather than calling out to the DB every time.
44 $session->param('logingroupid',1);
45 $session->param('workinggroupid',1); # yes, we *do* need to track this too. er, probably.
46}
47
48# handle login redirect
49if ($webvar{action} && $webvar{action} eq 'login') {
50 # handle user check
51 my $newurl = "http://$ENV{HTTP_HOST}$ENV{REQUEST_URI}?sid=$sid&page=domlist";
52 print "Status: 302\nLocation: $newurl\n\n";
53 exit;
54}
55
56my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
57my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
58
59# default
[3]60#my $perpage = 15;
[7]61my $perpage = 4;
[2]62my $offset = ($webvar{offset} ? $webvar{offset} : 0);
63
64# NB: these must match the field name and SQL ascend/descend syntax respectively
65my $sortfield = "domains";
66my $sortorder = "asc";
67
68my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
69#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
70# { AutoCommit => 0 }) or die $DBI::errstr;
71
72##fixme. PLEASE! <G>
73print $msg if !$dbh;
74
75# fiddle hardcoded "defaults" as per system/user (?) prefs
76initGlobals($dbh);
77
78# Default page is a login page
79my $page; # to be initialized as an HTML::Template entity sooner or later
80
[3]81
82
[2]83# decide which page to spit out...
84if (!$webvar{page}) {
85 $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
86} else {
87 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
88}
89
90$page->param(sid => $sid);
91
92if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
[3]93
94# hmm. seeing problems in some possibly-not-so-corner cases.
[10]95# this currently only handles "domain on", "domain off"
[3]96 if (defined($webvar{action})) {
97 domStatus($dbh,$webvar{id},$webvar{action});
98 }
99
[10]100 domlist();
[2]101
102} elsif ($webvar{page} eq 'reclist') {
103
[4]104 # Handle record list for both default records (per-group) and live domain records
[2]105
106 $page->param(defrec => $webvar{defrec});
[4]107 $page->param(id => $webvar{id});
108 $page->param(curpage => 'reclist');
[2]109
[4]110# select count(*) from (default_)?records where (group|domain)_id=?
111 my $sth = $dbh->prepare("SELECT count(*) FROM ".
112 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
113 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
114 "AND NOT type=$reverse_typemap{SOA}");
115 $sth->execute($webvar{id});
116 my ($count) = ($sth->fetchrow_array);
117
118 $page->param(ntot => $count);
119 $page->param(nfirst => 1);
120 $page->param(nlast => 10);
121
[7]122 fill_fpnla($count); # should put some params on this sub...
[4]123
124##fixme
[2]125 $page->param(domain => domainName($dbh,$webvar{id}));
[4]126 $page->param(id => $webvar{id});
[2]127##fixme
128
[4]129 $page->param(defrec => $webvar{defrec});
[2]130 if ($webvar{defrec} eq 'y') {
131 showdomain('y',1);
132 } else {
133 showdomain('n',$webvar{id});
134 }
135
[4]136} elsif ($webvar{page} eq 'newdomain') {
[2]137
138 # weesa gonna discard parent_group_id for now
139 my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id");
140 $sth->execute;
141 my @grplist;
142 while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
143 my %row;
144 $row{grpname} = $grpname;
145 $row{grpval} = $grpid;
146##fixme: need magic
147# $row{defgrp} = '';
148 push @grplist, \%row;
149 }
150
151 $page->param(grplist => \@grplist);
152
153} elsif ($webvar{page} eq 'newrec') {
154 print "whee!\n";
155
[3]156 # populate most fields as needed. (eg, type list.)
157 newrec();
[2]158
159} elsif ($webvar{page} eq 'addrec') {
160
161 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
162 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
163 push @recargs, $webvar{distance};
164 if ($webvar{type} == $reverse_typemap{SRV}) {
165 push @recargs, $webvar{weight};
166 push @recargs, $webvar{port};
167 }
168 }
169 push @recargs,
170 my ($code,$msg) = addRec(@recargs);
171
172 if ($code eq 'OK') {
173 showdomain($webvar{defrec},$webvar{parentid});
174# NB: should **really** redirect here, in case of reload. >_< eyowch.
175 } else {
176 $page->param(add_failed => 1);
177 $page->param(errmsg => $msg);
178 newrec(); # populate the form... er, mostly.
179 $page->param(name => $webvar{name});
180 $page->param(address => $webvar{address});
181 $page->param(distance => $webvar{distance})
182 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
183 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
184 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
185 }
186
[3]187 $page->param(defrec => $webvar{defrec});
188
[2]189} elsif ($webvar{page} eq 'conf_del') {
190
191 $page->param(id => $webvar{id});
192 $page->param(defrec => $webvar{defrec});
193
194 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
195
196} elsif ($webvar{page} eq 'delrec') {
197
198 $page->param(id => $webvar{id});
199 $page->param(defrec => $webvar{defrec});
200 # first pass = confirm y/n (sorta)
201 if (!defined($webvar{del})) {
202 $page->param(del_getconf => 1);
[3]203 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
204 $page->param(host => $rec{host});
205 $page->param(ftype => $typemap{$rec{type}});
206 $page->param(recval => $rec{val});
[2]207 } else {
[3]208 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
209 if ($code ne 'OK') {
210## need to find failure mode
211 $page->param(del_failed => 1);
212 $page->param(errmsg => $msg);
213 }
214##fixme: group/parent instead of hardcoded 1
215 showdomain('y',1);
[2]216 }
217
[5]218} elsif ($webvar{page} eq 'deldom') {
219
220 $page->param(id => $webvar{id});
221 # first pass = confirm y/n (sorta)
222 if (!defined($webvar{del})) {
223 $page->param(del_getconf => 1);
224 $page->param(domain => domainName($dbh,$webvar{id}));
225# print some neato things?
[7]226
227# } else {
228# #whether actually deleting or cancelling we redirect to the domain list, default format
229
[5]230 } elsif ($webvar{del} eq 'ok') {
231 my ($code,$msg) = delDomain($dbh, $webvar{id});
232 if ($code ne 'OK') {
233# need to find failure mode
234 $page->param(del_failed => 1);
235 $page->param(errmsg => $msg);
[10]236 } else {
237##work
238 domlist();
[5]239 }
240 } else {
241 # cancelled. whee!
242 $page->param(del_failed => 0);
[10]243 domlist();
[5]244 }
245
[2]246} elsif ($webvar{page} eq 'editsoa') {
247
248 fillsoa($webvar{defrec},$webvar{recid});
249
250} elsif ($webvar{page} eq 'updatesoa') {
251 print "ooooo!\n";
252
253 my $sth;
254 my $sql = '';
255 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
256 # plus a bit of magic to update the appropriate table
257 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
258 " set host='$webvar{prins}:$webvar{contact}',".
259 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
260 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
261 $sth = $dbh->prepare($sql);
262 $sth->execute;
263
264 if ($sth->err) {
265 $page->param(update_failed => 1);
266 $page->param(msg => $DBI::errstr);
267 fillsoa($webvar{defrec},1);
268 } else {
269 $page->param(update_failed => 0);
270##fixme! need to set group ID properly here
271 showdomain('y',1);
272 }
273
274} elsif ($webvar{page} eq 'adddomain') {
275 # Need some magic here.
276
277##fixme: Group should be variable
278 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
279
280# hokay, a bit of magic to decide which page we hit.
281 if ($code eq 'OK') {
[3]282 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
283 showdomain(0,$msg);
284##work
[2]285 } else {
286# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
287 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
288 $page->param(add_failed => 1);
289 $page->param(domain => $webvar{domain});
290 $page->param(errmsg => $msg);
291 }
292
293
294}
295
296
[7]297## hmm. may want to move this so we can redirect after adding/deleting/etc
298print "Content-type: text/html\n\n", $header->output;
299
[2]300# spit it out
301print $page->output;
302
303print "<div id=debug>webvar keys: <pre>\n";
304foreach my $key (keys %webvar) {
305 print "key: $key\tval: $webvar{$key}\n";
306}
307print "</pre>\nENV:\n<pre>\n";
308foreach my $key (keys %ENV) {
309 print "key: $key\tval: $ENV{$key}\n";
310}
311print "</pre></div>\n";
312
313print $footer->output;
314
315
316exit 0;
317
318
319sub fillsoa {
320 my $def = shift;
321 my $id = shift;
322 my $domname;
323
324 if ($webvar{domain} == 0) {
325 $domname = "DOMAIN";
326 } else {
327 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
328 $sth->execute();
329 ($domname) = $sth->fetchrow_array();
330 }
331
332 $page->param(domain => $domname);
333 $page->param(defrec => !$webvar{domain});
334 $page->param(group => $DNSDB::group);
335
336# defaults
337 $page->param(defcontact => $DNSDB::def{contact});
338 $page->param(defns => $DNSDB::def{prins});
339 $page->param(defsoattl => $DNSDB::def{soattl});
340 $page->param(defrefresh => $DNSDB::def{refresh});
341 $page->param(defretry => $DNSDB::def{retry});
342 $page->param(defexpire => $DNSDB::def{expire});
343 $page->param(defminttl => $DNSDB::def{minttl});
344
345 # there are probably better ways to do this. TMTOWTDI.
346 my %soa = getSOA($dbh,$def,$id);
347
348 $page->param(domainid => $webvar{domain});
349 $page->param(recid => $soa{recid});
350 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
351 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
352 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
353 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
354 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
355 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
356 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
357}
358
359sub showdomain {
360 my $def = shift;
361 my $id = shift;
362
363 # get the SOA first
364 my %soa = getSOA($dbh,$def,$id);
365
366 $page->param(recid => $soa{recid});
367 $page->param(contact => $soa{contact});
368 $page->param(prins => $soa{prins});
369 $page->param(refresh => $soa{refresh});
370 $page->param(retry => $soa{retry});
371 $page->param(expire => $soa{expire});
372 $page->param(minttl => $soa{minttl});
373 $page->param(ttl => $soa{ttl});
374
375# my @foo2 = getDomRecs($dbh,'def',1);
[4]376 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]377
378 my $row = 0;
379 foreach my $rec (@$foo2) {
380 $rec->{type} = $typemap{$rec->{type}};
381 $rec->{row} = $row % 2;
382 # Feh.
383 $rec->{defrec} = $webvar{defrec};
384 # And **FEH!!**
385 $rec->{sid} = $webvar{sid};
386 $row++;
387 }
388 $page->param(reclist => $foo2);
389}
390
391sub newrec {
392 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
393 $sth->execute;
394
395 my @typelist;
396 while (my ($rval,$rname) = $sth->fetchrow_array()) {
397 my %row = ( recval => $rval, recname => $rname );
398 $row{tselect} = 1 if $rval == $webvar{type};
399 push @typelist, \%row;
400 }
401 $page->param(typelist => \@typelist);
402 $page->param(domain => domainName($dbh,$webvar{domainid}));
403 $page->param(defrec => $webvar{defrec});
404 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
[3]405 if ($webvar{defrec} eq 'y') {
406 ##fixme - should be groupid
407 $page->param(parentid => 1);
408 } else {
[4]409print "DEBUG: foobar: parentid $webvar{parentid}<br>\n";
[3]410 $page->param(parentid => $webvar{parentid});
411 }
[2]412}
[7]413
414sub fill_fpnla {
415 my $count = shift;
416##fixme
417 if ($offset eq 'all') {
418 #print "foo! wanna see'em all\n";
419 } else {
420 # all these bits only have sensible behaviour if offset is numeric. err, probably.
421 if ($count > $perpage) {
422 # if there are more results than the default, always show the "all" link
423 $page->param(navall => 1);
424
425 if ($offset > 0) {
426 $page->param(navfirst => 1);
427 $page->param(navprev => 1);
428 $page->param(prevoffs => $offset-1);
429 }
430
431 # show "next" and "last" links if we're not on the last page of results
432 if ( (($offset+1) * $perpage - $count) < 0 ) {
433 $page->param(navnext => 1);
434 $page->param(nextoffs => $offset+1);
435 $page->param(navlast => 1);
[8]436 $page->param(lastoffs => int (($count-1)/$perpage));
[7]437 }
438 }
439 }
[10]440} # end fill_fpnla()
441
442
443sub domlist {
444 my $sth = $dbh->prepare("select count(*) from domains");
445 $sth->execute;
446 my ($count) = ($sth->fetchrow_array);
447
448 fill_fpnla($count);
449
450 $page->param(ndomains => $count);
451 $page->param(nstart => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
452 $page->param(npglast => ($offset eq 'all' ? $count :
453 ( (($offset+1)*$perpage) > $count ? $count : (($offset+1)*$perpage) )
454 ));
455
456##fixme - group
457 $page->param(id => 1);
458 my @domlist;
459 my $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
460 " inner join groups on domains.group_id=groups.group_id".
461 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
462 $sth->execute;
463 my $rownum = 0;
464 while (my @data = $sth->fetchrow_array) {
465 my %row;
466 $row{domainid} = $data[0];
467 $row{domain} = $data[1];
468 $row{status} = ($data[2] ? 'Active' : 'Inactive');
469 $row{group} = $data[3];
470 $row{bg} = ($rownum++)%2;
471# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
472 $row{mkactive} = !$data[2];
473 $row{sid} = $sid;
474 $row{offset} = $offset;
475##fixme: need to clean up status indicator/usage/inversion
476 push @domlist, \%row;
477 }
478 $page->param(domtable => \@domlist);
479} # end domlist()
Note: See TracBrowser for help on using the repository browser.