source: trunk/dns.cgi@ 9

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

/trunk

Found likely solution to off-by-one-on-integer-multiples last-offset calculation

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