source: trunk/dns.cgi@ 30

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

/trunk

checkpoint

  • filled out "normal" DNS lookup, still need to fiddle with manual recursion
  • tweak fine details of login handling so we don't get an

"... uninitialized value in string eq ..." in the log

  • logout mostly working - needs additional crosscheck to kick logged-out sessions back to the login screen
  • add username blurb on menu top and as a session param (use this for above)
  • housekeeping on images/
  • misc finetuning and nitpicking of HTML
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 29.7 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-11-06 23:02:00 +0000 (Fri, 06 Nov 2009) $
6# SVN revision $Rev: 30 $
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;
[29]18use Crypt::PasswdMD5;
[30]19use Net::DNS;
[2]20use DBI;
21
22use lib '.';
23# custom modules
24use DNSDB qw(:ALL);
25
[13]26my @debugbits; # temp, to be spit out near the end of processing
27
[2]28# Let's do these templates right...
29my $templatedir = "templates";
30my $sessiondir = "session";
31
32# Set up the CGI object...
33my $q = new CGI::Simple;
34# ... and get query-string params as well as POST params if necessary
35$q->parse_query_string;
36
37# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
[7]38my %webvar = $q->Vars;
[2]39
[13]40# persistent stuff needed on most/all pages
[2]41my $sid = ($webvar{sid} ? $webvar{sid} : undef);
42my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
43#$sid = $session->id() if !$sid;
44if (!$sid) {
45 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
46 $sid = $session->id();
47# need to know the "upper" group the user can deal with; may as well
48# stick this in the session rather than calling out to the DB every time.
[18]49 $session->param('logingroup',1);
50 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
[2]51}
52
[19]53my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
54my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
[17]55my $group = ($webvar{group} ? $webvar{group} : 1);
[18]56
[26]57# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
[2]58
[20]59if ($webvar{action} && $webvar{action} eq 'chgroup') {
60 # fiddle session-stored group data
61 # magic incantation to... uhhh...
62 $session->param('curgroup', $webvar{group});
63 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
64}
65
[2]66my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
67my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
68
69# default
[3]70#my $perpage = 15;
[15]71my $perpage = 3;
[2]72my $offset = ($webvar{offset} ? $webvar{offset} : 0);
73
74# NB: these must match the field name and SQL ascend/descend syntax respectively
75my $sortfield = "domains";
76my $sortorder = "asc";
77
[29]78my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
[2]79#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
80# { AutoCommit => 0 }) or die $DBI::errstr;
81
82##fixme. PLEASE! <G>
83print $msg if !$dbh;
84
85# fiddle hardcoded "defaults" as per system/user (?) prefs
86initGlobals($dbh);
87
[26]88# handle login redirect
[30]89if ($webvar{action}) {
90 if ($webvar{action} eq 'login') {
91 my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
92 $sth->execute($webvar{username});
93 my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
94 $webvar{loginfailed} = 1 if !defined($uid);
[26]95
[30]96 if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
97 $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
98 } else {
99 $webvar{loginfailed} = 1 if $pass ne $webvar{password};
100 }
[29]101
[30]102 # set session bits
103 $session->param('logingroup',$gid);
104 $session->param('curgroup',$gid);
105 $session->param('username',$webvar{username});
[26]106
[30]107 changepage(page => "domlist") if !defined($webvar{loginfailed});
108 } elsif ($webvar{action} eq 'logout') {
109 # delete the session
110 $session->delete();
111 $session->flush();
112
113 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}";
114 $newurl =~ s|/[^/]+$|/|;
115 print "Status: 302\nLocation: $newurl\n\n";
116 exit;
117
118 }
[26]119}
120
[15]121## Default page is a login page
122#my $page; # to be initialized as an HTML::Template entity sooner or later
[2]123
[3]124
125
[2]126# decide which page to spit out...
[15]127$webvar{page} = 'login' if !$webvar{page};
128#if (!$webvar{page}) {
129# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
130#} else {
131#}
[2]132
[15]133my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
134
[2]135$page->param(sid => $sid);
136
[26]137if ($webvar{page} eq 'login') {
[3]138
[26]139 $page->param(loginfailed => 1) if $webvar{loginfailed};
140##fixme: set up session init to actually *check* for session timeout
141 $page->param(timeout => 1) if $webvar{sesstimeout};
142
143} elsif ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
144
[3]145# hmm. seeing problems in some possibly-not-so-corner cases.
[10]146# this currently only handles "domain on", "domain off"
[3]147 if (defined($webvar{action})) {
148 domStatus($dbh,$webvar{id},$webvar{action});
149 }
150
[18]151 $page->param(curpage => $webvar{page});
152
[11]153 listdomains();
[2]154
155} elsif ($webvar{page} eq 'reclist') {
156
[4]157 # Handle record list for both default records (per-group) and live domain records
[2]158
159 $page->param(defrec => $webvar{defrec});
[4]160 $page->param(id => $webvar{id});
[18]161 $page->param(curpage => $webvar{page});
[2]162
[4]163# select count(*) from (default_)?records where (group|domain)_id=?
164 my $sth = $dbh->prepare("SELECT count(*) FROM ".
165 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
166 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
167 "AND NOT type=$reverse_typemap{SOA}");
168 $sth->execute($webvar{id});
169 my ($count) = ($sth->fetchrow_array);
170
[12]171# fill the page-count and first-previous-next-last-all details
172 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
[7]173 fill_fpnla($count); # should put some params on this sub...
[4]174
175 $page->param(defrec => $webvar{defrec});
[2]176 if ($webvar{defrec} eq 'y') {
[12]177##fixme: hardcoded group
[20]178 showdomain('y',$curgroup);
[2]179 } else {
180 showdomain('n',$webvar{id});
181 }
182
[4]183} elsif ($webvar{page} eq 'newdomain') {
[2]184
185
[11]186} elsif ($webvar{page} eq 'deldom') {
187
188 $page->param(id => $webvar{id});
189 # first pass = confirm y/n (sorta)
190 if (!defined($webvar{del})) {
191 $page->param(del_getconf => 1);
192 $page->param(domain => domainName($dbh,$webvar{id}));
193# print some neato things?
194
195# } else {
196# #whether actually deleting or cancelling we redirect to the domain list, default format
197
198 } elsif ($webvar{del} eq 'ok') {
199 my ($code,$msg) = delDomain($dbh, $webvar{id});
200 if ($code ne 'OK') {
201# need to find failure mode
202 $page->param(del_failed => 1);
203 $page->param(errmsg => $msg);
[22]204 listdomains($curgroup);
[11]205 } else {
206 # success. go back to the domain list, do not pass "GO"
207 changepage(page => "domlist");
208 }
209 } else {
210 # cancelled. whee!
211 changepage(page => "domlist");
212 }
213
[13]214} elsif ($webvar{page} eq 'record') {
[16]215
[13]216 if ($webvar{recact} eq 'new') {
[16]217
[15]218 $page->param(todo => "Add record to");
219 $page->param(recact => "add");
[16]220 fill_rectypes();
221
[15]222 } elsif ($webvar{recact} eq 'add') {
223
224 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
225 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
226 push @recargs, $webvar{distance};
227 if ($webvar{type} == $reverse_typemap{SRV}) {
228 push @recargs, $webvar{weight};
229 push @recargs, $webvar{port};
230 }
231 }
232 my ($code,$msg) = addRec(@recargs);
233
234 if ($code eq 'OK') {
235 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
236 } else {
[24]237
238 $page->param(failed => 1);
239 $page->param(errmsg => $msg);
240 $page->param(wastrying => "adding");
241 $page->param(todo => "Add record to");
242 $page->param(recact => "add");
243 $page->param(parentid => $webvar{parentid});
244 $page->param(defrec => $webvar{defrec});
245 $page->param(id => $webvar{id});
[16]246 fill_recdata(); # populate the form... er, mostly.
[15]247 }
248
[13]249 } elsif ($webvar{recact} eq 'edit') {
[15]250
[16]251 $page->param(todo => "Update record");
252 $page->param(recact => "update");
253 $page->param(parentid => $webvar{parentid});
[17]254 $page->param(id => $webvar{id});
[16]255 $page->param(defrec => $webvar{defrec});
[13]256 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
257 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
258 $sth->execute($webvar{id});
259 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
260 $page->param(name => $host);
261 $page->param(address => $val);
262 $page->param(distance => $distance);
263 $page->param(weight => $weight);
264 $page->param(port => $port);
265 $page->param(ttl => $ttl);
[16]266 fill_rectypes($type);
267
268 } elsif ($webvar{recact} eq 'update') {
269
270 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
271 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
272 $webvar{distance},$webvar{weight},$webvar{port});
273
274 if ($code eq 'OK') {
[17]275 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[16]276 } else {
277 $page->param(failed => 1);
278 $page->param(errmsg => $msg);
279 $page->param(wastrying => "updating");
280 $page->param(todo => "Update record");
281 $page->param(recact => "update");
282 $page->param(parentid => $webvar{parentid});
283 $page->param(defrec => $webvar{defrec});
[17]284 $page->param(id => $webvar{id});
[16]285 fill_recdata();
286 }
[13]287 }
[16]288
[13]289 if ($webvar{defrec} eq 'y') {
[20]290 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
[13]291 } else {
[24]292 $page->param(parentid => $webvar{parentid});
293# $page->param(id => $webvar{id});
[16]294 $page->param(dohere => domainName($dbh,$webvar{parentid}));
[13]295 }
296
[2]297} elsif ($webvar{page} eq 'newrec') {
[13]298 push @debugbits, "whee!\n";
[2]299
[3]300 # populate most fields as needed. (eg, type list.)
[13]301 stdrecs();
[2]302
303} elsif ($webvar{page} eq 'addrec') {
304
305 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
306 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
307 push @recargs, $webvar{distance};
308 if ($webvar{type} == $reverse_typemap{SRV}) {
309 push @recargs, $webvar{weight};
310 push @recargs, $webvar{port};
311 }
312 }
[13]313# wtf?
314# push @recargs,
[2]315 my ($code,$msg) = addRec(@recargs);
316
317 if ($code eq 'OK') {
318 showdomain($webvar{defrec},$webvar{parentid});
319# NB: should **really** redirect here, in case of reload. >_< eyowch.
320 } else {
321 $page->param(add_failed => 1);
322 $page->param(errmsg => $msg);
[13]323 stdrecs($webvar{type}); # populate the form... er, mostly.
[2]324 $page->param(name => $webvar{name});
325 $page->param(address => $webvar{address});
326 $page->param(distance => $webvar{distance})
327 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
328 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
329 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
330 }
331
[3]332 $page->param(defrec => $webvar{defrec});
333
[2]334} elsif ($webvar{page} eq 'conf_del') {
335
336 $page->param(id => $webvar{id});
337 $page->param(defrec => $webvar{defrec});
338
339 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
340
341} elsif ($webvar{page} eq 'delrec') {
342
343 $page->param(id => $webvar{id});
344 $page->param(defrec => $webvar{defrec});
345 # first pass = confirm y/n (sorta)
346 if (!defined($webvar{del})) {
347 $page->param(del_getconf => 1);
[3]348 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
349 $page->param(host => $rec{host});
350 $page->param(ftype => $typemap{$rec{type}});
351 $page->param(recval => $rec{val});
[2]352 } else {
[3]353 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
354 if ($code ne 'OK') {
355## need to find failure mode
356 $page->param(del_failed => 1);
357 $page->param(errmsg => $msg);
358 }
359##fixme: group/parent instead of hardcoded 1
360 showdomain('y',1);
[2]361 }
362
363} elsif ($webvar{page} eq 'editsoa') {
364
365 fillsoa($webvar{defrec},$webvar{recid});
366
367} elsif ($webvar{page} eq 'updatesoa') {
368 print "ooooo!\n";
369
370 my $sth;
371 my $sql = '';
372 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
373 # plus a bit of magic to update the appropriate table
374 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
375 " set host='$webvar{prins}:$webvar{contact}',".
376 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
377 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
378 $sth = $dbh->prepare($sql);
379 $sth->execute;
380
381 if ($sth->err) {
382 $page->param(update_failed => 1);
383 $page->param(msg => $DBI::errstr);
384 fillsoa($webvar{defrec},1);
385 } else {
386 $page->param(update_failed => 0);
387##fixme! need to set group ID properly here
388 showdomain('y',1);
389 }
390
391} elsif ($webvar{page} eq 'adddomain') {
392 # Need some magic here.
393
394##fixme: Group should be variable
[20]395 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
[2]396
397# hokay, a bit of magic to decide which page we hit.
398 if ($code eq 'OK') {
[11]399# redirect to dns.cgi?etc&page=reclist
[12]400 changepage(page => "reclist", id => $msg);
[24]401# $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
402# showdomain(0,$msg);
[2]403 } else {
404# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
[24]405##fixme: session ID
[2]406 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
407 $page->param(add_failed => 1);
408 $page->param(domain => $webvar{domain});
409 $page->param(errmsg => $msg);
410 }
411
[17]412} elsif ($webvar{page} eq 'grpman') {
[2]413
[22]414 listgroups();
[18]415 $page->param(curpage => $webvar{page});
416
[17]417} elsif ($webvar{page} eq 'newgrp') {
[20]418
[18]419 # do.. uhh.. stuff.. if we have no webvar{action}
420 if ($webvar{action} && $webvar{action} eq 'add') {
421 # not gonna provide the 4th param: template-or-clone flag, just yet
422 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
423 changepage(page => "grpman") if $code eq 'OK';
424 $page->param(add_failed => 1);
425 $page->param(errmsg => $msg);
426 $page->param(newgroup => $webvar{newgroup});
[20]427 fill_grouplist('pargroup',$webvar{pargroup});
[19]428 } else {
429# $page->param
[20]430 fill_grouplist('pargroup',$curgroup);
[19]431
[18]432 }
[20]433
[22]434} elsif ($webvar{page} eq 'delgrp') {
[20]435
436 $page->param(id => $webvar{id});
437 # first pass = confirm y/n (sorta)
438 if (!defined($webvar{del})) {
439 $page->param(del_getconf => 1);
[23]440# $page->param(groupname => groupName($dbh,$webvar{id}));
[20]441# print some neato things?
442
443# } else {
444# #whether actually deleting or cancelling we redirect to the group list, default format
445
446 } elsif ($webvar{del} eq 'ok') {
447 my ($code,$msg) = delGroup($dbh, $webvar{id});
[23]448push @debugbits, groupName($dbh, $webvar{id});
[20]449 if ($code ne 'OK') {
450# need to find failure mode
451 $page->param(del_failed => 1);
452 $page->param(errmsg => $msg);
[22]453 $page->param(curpage => $webvar{page});
454 listgroups();
[20]455 } else {
456 # success. go back to the domain list, do not pass "GO"
457 changepage(page => "grpman");
458 }
459 } else {
460 # cancelled. whee!
461 changepage(page => "grpman");
462 }
[23]463 $page->param(delgroupname => groupName($dbh, $webvar{id}));
[24]464
465} elsif ($webvar{page} eq 'useradmin') {
466
467 list_users();
468 $page->param(curpage => $webvar{page});
469
470} elsif ($webvar{page} eq 'newuser') {
471
472 # foo?
473 fill_actypelist();
474
475} elsif ($webvar{page} eq 'adduser') {
476
477 my ($code,$msg);
478
479 if ($webvar{pass1} ne $webvar{pass2}) {
480 $code = 'FAIL';
481 $msg = "Passwords don't match";
482 } else {
483 ($code,$msg) = addUser($dbh,$webvar{username}, $webvar{group}, $webvar{pass1},
[25]484 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
485 $webvar{fname}, $webvar{lname}, $webvar{phone});
[24]486 }
487
488# hokay, a bit of magic to decide which page we hit.
489 if ($code eq 'OK') {
490 changepage(page => "useradmin");
491 } else {
492# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
493# $page = HTML::Template->new(filename => "$templatedir/newuser.tmpl");
494 $page->param(add_failed => 1);
495 $page->param(username => $webvar{username});
496 $page->param(fname => $webvar{fname});
497 $page->param(lname => $webvar{lname});
498 $page->param(pass1 => $webvar{pass1});
499 $page->param(pass2 => $webvar{pass2});
500 $page->param(errmsg => $msg);
501 fill_actypelist();
502 }
503
504 $page->param(add_failed => 1);
[25]505
506} elsif ($webvar{page} eq 'deluser') {
507
508 $page->param(id => $webvar{id});
509 # first pass = confirm y/n (sorta)
510 if (!defined($webvar{del})) {
511 $page->param(del_getconf => 1);
512 $page->param(user => userFullName($dbh,$webvar{id}));
513 } elsif ($webvar{del} eq 'ok') {
514 my ($code,$msg) = delUser($dbh, $webvar{id});
515 if ($code ne 'OK') {
516# need to find failure mode
517 $page->param(del_failed => 1);
518 $page->param(errmsg => $msg);
519 list_users($curgroup);
520 } else {
521 # success. go back to the domain list, do not pass "GO"
522 changepage(page => "useradmin");
523 }
524 } else {
525 # cancelled. whee!
526 changepage(page => "useradmin");
527 }
528
[30]529} elsif ($webvar{page} eq 'dnsq') {
530
531 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
532 fill_rectypes($webvar{type} ? $webvar{type} : '');
533 $page->param(trace => $webvar{trace}) if $webvar{trace};
534 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
535
536 if ($webvar{qfor}) {
537 my $resolv = Net::DNS::Resolver->new;
538 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
539 if ($query) {
540
541 $page->param(showresults => 1);
542
543 my @answer;
544 foreach my $rr ($query->answer) {
545# next unless $rr->type eq "A" or $rr->type eq 'NS';
546 my %row;
547 my ($host,$ttl,$class,$type,$data) =
548 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
549 $row{host} = $host;
550 $row{ftype} = $type;
551 $row{rdata} = $data;
552 push @answer, \%row;
553 }
554 $page->param(answer => \@answer);
555
556 my @additional;
557 foreach my $rr ($query->additional) {
558# next unless $rr->type eq "A" or $rr->type eq 'NS';
559 my %row;
560 my ($host,$ttl,$class,$type,$data) =
561 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
562 $row{host} = $host;
563 $row{ftype} = $type;
564 $row{rdata} = $data;
565 push @additional, \%row;
566 }
567 $page->param(additional => \@additional);
568
569 my @authority;
570 foreach my $rr ($query->authority) {
571# next unless $rr->type eq "A" or $rr->type eq 'NS';
572 my %row;
573 my ($host,$ttl,$class,$type,$data) =
574 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
575 $row{host} = $host;
576 $row{ftype} = $type;
577 $row{rdata} = $data;
578 push @authority, \%row;
579 }
580 $page->param(authority => \@authority);
581
582 $page->param(usedresolver => $resolv->answerfrom);
583 $page->param(frtype => $typemap{$webvar{type}});
584
585 } else {
586 $page->param(errmsg => $resolv->errorstring);
587 }
588 }
589 ## done DNS query
590
[2]591}
592
593
[17]594# start output here so we can redirect pages.
[7]595print "Content-type: text/html\n\n", $header->output;
596
[20]597##common bits
[17]598if ($webvar{page} ne 'login') {
[30]599 $page->param(username => $session->param("username"));
600
[20]601 $page->param(group => $curgroup);
602 $page->param(groupname => groupName($dbh,$curgroup));
603
[24]604 # group tree. should go elsewhere, probably
605 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
606 $page->param(grptree => $tmpgrplist);
607
[20]608 # stuff for menu group change. nb: this is icky.
609 fill_grouplist("grouplist");
610 $page->param(whereami => $ENV{REQUEST_URI});
[17]611}
[13]612
[24]613foreach (@debugbits) { print; }
614
[2]615# spit it out
616print $page->output;
617
618print "<div id=debug>webvar keys: <pre>\n";
619foreach my $key (keys %webvar) {
620 print "key: $key\tval: $webvar{$key}\n";
621}
[20]622print "</pre>\nsession:\n<pre>\n";
623my $sesdata = $session->dataref();
624foreach my $key (keys %$sesdata) {
625 print "key: $key\tval: ".$sesdata->{$key}."\n";
626}
[2]627print "</pre>\nENV:\n<pre>\n";
628foreach my $key (keys %ENV) {
629 print "key: $key\tval: $ENV{$key}\n";
630}
631print "</pre></div>\n";
632
633print $footer->output;
634
[18]635# as per the docs, Just In Case
636$session->flush();
[2]637
638exit 0;
639
640
[24]641sub fill_grptree {
642 my $root = shift;
643 my $cur = shift;
644
645 my @childlist;
646
647 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
648 getChildren($dbh,$root,\@childlist,'immediate');
649 return if $#childlist == -1;
650 my @grouplist;
651 foreach (@childlist) {
652 my %row;
653 $row{grpname} = groupName($dbh,$_);
654 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
655 $row{subs} = fill_grptree($_,$cur);
656 push @grouplist, \%row;
657 }
658 $grptree->param(treelvl => \@grouplist);
659 return $grptree->output;
660}
661
662
[11]663sub changepage {
664 my %params = @_; # think this works the way I want...
665
666 # handle user check
667 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
668 foreach (keys %params) {
669 $newurl .= "&$_=$params{$_}";
670 }
671
[30]672 # Just In Case
673 $session->flush();
674
[11]675 print "Status: 302\nLocation: $newurl\n\n";
676 exit;
677} # end changepage
678
679
[2]680sub fillsoa {
681 my $def = shift;
682 my $id = shift;
683 my $domname;
684
685 if ($webvar{domain} == 0) {
686 $domname = "DOMAIN";
687 } else {
[17]688 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
689 $sth->execute($webvar{domain});
[2]690 ($domname) = $sth->fetchrow_array();
691 }
692
[17]693 $page->param(domain => $domname);
694 $page->param(defrec => !$webvar{domain});
695 $page->param(group => $DNSDB::group);
[2]696
697# defaults
[17]698 $page->param(defcontact => $DNSDB::def{contact});
699 $page->param(defns => $DNSDB::def{prins});
700 $page->param(defsoattl => $DNSDB::def{soattl});
701 $page->param(defrefresh => $DNSDB::def{refresh});
702 $page->param(defretry => $DNSDB::def{retry});
703 $page->param(defexpire => $DNSDB::def{expire});
704 $page->param(defminttl => $DNSDB::def{minttl});
[2]705
706 # there are probably better ways to do this. TMTOWTDI.
707 my %soa = getSOA($dbh,$def,$id);
708
709 $page->param(domainid => $webvar{domain});
710 $page->param(recid => $soa{recid});
711 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
712 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
713 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
714 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
715 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
716 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
717 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
718}
719
720sub showdomain {
721 my $def = shift;
722 my $id = shift;
723
724 # get the SOA first
725 my %soa = getSOA($dbh,$def,$id);
726
727 $page->param(recid => $soa{recid});
728 $page->param(contact => $soa{contact});
729 $page->param(prins => $soa{prins});
730 $page->param(refresh => $soa{refresh});
731 $page->param(retry => $soa{retry});
732 $page->param(expire => $soa{expire});
733 $page->param(minttl => $soa{minttl});
734 $page->param(ttl => $soa{ttl});
735
736# my @foo2 = getDomRecs($dbh,'def',1);
[4]737 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]738
739 my $row = 0;
740 foreach my $rec (@$foo2) {
741 $rec->{type} = $typemap{$rec->{type}};
742 $rec->{row} = $row % 2;
743 $rec->{defrec} = $webvar{defrec};
744 $rec->{sid} = $webvar{sid};
[13]745 $rec->{id} = $id;
[23]746 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
747 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
748 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]749 $row++;
750 }
751 $page->param(reclist => $foo2);
752}
753
[23]754
[16]755# fill in record type list on add/update/edit record template
756sub fill_rectypes {
[13]757 my $type = shift || $reverse_typemap{A};
758
[17]759 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]760 $sth->execute;
761 my @typelist;
762 while (my ($rval,$rname) = $sth->fetchrow_array()) {
763 my %row = ( recval => $rval, recname => $rname );
[13]764 $row{tselect} = 1 if $rval == $type;
[2]765 push @typelist, \%row;
766 }
767 $page->param(typelist => \@typelist);
[16]768}
769
770sub fill_recdata {
771 fill_rectypes($webvar{type});
772
773 $page->param(name => $webvar{name});
774 $page->param(address => $webvar{address});
775 $page->param(distance => $webvar{distance})
776 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
777 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
778 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[2]779 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
780}
[7]781
[24]782
783sub fill_actypelist {
784 my @actypes;
785
786 my %row1 = (actypeval => 'u', actypename => 'user');
787 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
788 push @actypes, \%row1;
789
790 my %row2 = (actypeval => 'S', actypename => 'superuser');
791 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
792 push @actypes, \%row2;
793
794 $page->param(actypelist => \@actypes);
795}
796
797
[7]798sub fill_fpnla {
799 my $count = shift;
800##fixme
801 if ($offset eq 'all') {
[17]802 push @debugbits, "foo! wanna see'em all\n";
[7]803 } else {
804 # all these bits only have sensible behaviour if offset is numeric. err, probably.
805 if ($count > $perpage) {
806 # if there are more results than the default, always show the "all" link
807 $page->param(navall => 1);
808
809 if ($offset > 0) {
810 $page->param(navfirst => 1);
811 $page->param(navprev => 1);
812 $page->param(prevoffs => $offset-1);
813 }
814
815 # show "next" and "last" links if we're not on the last page of results
816 if ( (($offset+1) * $perpage - $count) < 0 ) {
817 $page->param(navnext => 1);
818 $page->param(nextoffs => $offset+1);
819 $page->param(navlast => 1);
[8]820 $page->param(lastoffs => int (($count-1)/$perpage));
[7]821 }
822 }
823 }
[10]824} # end fill_fpnla()
825
826
[12]827sub fill_pgcount {
828 my $pgcount = shift;
829 my $pgtype = shift;
830 my $parent = shift;
831
832 $page->param(ntot => $pgcount);
833 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
834 $page->param(npglast => ($offset eq 'all' ? $pgcount :
835 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
836 ));
837 $page->param(pgtype => $pgtype);
838 $page->param(parent => $parent);
839} # end fill_pgcount()
840
841
[11]842sub listdomains {
[17]843 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
[20]844 $sth->execute($curgroup);
[17]845 my ($count) = $sth->fetchrow_array;
846
[12]847# fill page count and first-previous-next-last-all bits
848##fixme - hardcoded group bit
[20]849 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]850 fill_fpnla($count);
851
852##fixme - group
[20]853 $page->param(group => $curgroup);
[10]854 my @domlist;
[20]855 $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
856 " INNER JOIN groups ON domains.group_id=groups.group_id".
857 " WHERE domains.group_id=?".
858 " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
859 $sth->execute($curgroup);
[10]860 my $rownum = 0;
861 while (my @data = $sth->fetchrow_array) {
862 my %row;
863 $row{domainid} = $data[0];
864 $row{domain} = $data[1];
865 $row{status} = ($data[2] ? 'Active' : 'Inactive');
866 $row{group} = $data[3];
867 $row{bg} = ($rownum++)%2;
868# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
869 $row{mkactive} = !$data[2];
870 $row{sid} = $sid;
871 $row{offset} = $offset;
872##fixme: need to clean up status indicator/usage/inversion
873 push @domlist, \%row;
874 }
875 $page->param(domtable => \@domlist);
[11]876} # end listdomains()
[18]877
878
[22]879sub listgroups {
[26]880 my @childgroups;
881 getChildren($dbh, $logingroup, \@childgroups, 'all');
882 my $childlist = join(',',@childgroups);
883
884 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")";
885 my $sth = $dbh->prepare($sql);
886
[22]887 $sth->execute;
888 my ($count) = ($sth->fetchrow_array);
889# fill page count and first-previous-next-last-all bits
890##fixme - hardcoded group bit
891 fill_pgcount($count,"groups",'');
892 fill_fpnla($count);
893
894 my @grouplist;
895 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[26]896 "count(distinct(u.username)), count(distinct(d.domain)) ".
[22]897 "FROM groups g ".
898 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
899 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
900 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[26]901 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[22]902 "GROUP BY g.group_id, g.group_name, g2.group_name ".
903 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
904 $sth->execute;
905
906 my $rownum = 0;
907 while (my @data = $sth->fetchrow_array) {
908 my %row;
909 $row{groupid} = $data[0];
910 $row{groupname} = $data[1];
911 $row{pgroup} = $data[2];
912 $row{nusers} = $data[3];
913 $row{ndomains} = $data[4];
914 $row{bg} = ($rownum++)%2;
915 $row{sid} = $sid;
916 push @grouplist, \%row;
917 }
918 $page->param(grouptable => \@grouplist);
919} # end listgroups()
920
921
[20]922sub fill_grouplist {
[19]923 my $template_var = shift;
924 my $cur = shift || $curgroup;
[26]925
926 my @childgroups;
927 getChildren($dbh, $logingroup, \@childgroups, 'all');
928 my $childlist = join(',',@childgroups);
929
[18]930 # weesa gonna discard parent_group_id for now
[26]931 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
932 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
933 "ORDER BY group_id");
[18]934 $sth->execute;
[20]935 my @grouplist;
936 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]937 my %row;
[20]938 $row{groupname} = $groupname;
939 $row{groupval} = $groupid;
[18]940##fixme: need magic
[20]941# $row{defgroup} = '';
942 $row{groupactive} = 1 if $groupid == $cur;
943 push @grouplist, \%row;
[18]944 }
945
[20]946 $page->param("$template_var" => \@grouplist);
[18]947
[24]948} # end fill_grouplist()
949
[26]950
[24]951sub list_users {
952 my $sth = $dbh->prepare("select count(*) from users where group_id=?");
953 $sth->execute($curgroup);
954 my ($count) = ($sth->fetchrow_array);
955
956# fill page count and first-previous-next-last-all bits
957##fixme - hardcoded group bit
958 fill_pgcount($count,"users",'');
959 fill_fpnla($count);
960
961 my @userlist;
[25]962 $sth = $dbh->prepare("SELECT u.user_id, u.username, u.firstname, u.lastname, u.type, g.group_name, u.status ".
[24]963 "FROM users u ".
964 "INNER JOIN groups g ON u.group_id=g.group_id ".
965 "WHERE u.group_id=?".
966 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
967 $sth->execute($curgroup);
968
969 my $rownum = 0;
970 while (my @data = $sth->fetchrow_array) {
971 my %row;
972 $row{userid} = $data[0];
973 $row{username} = $data[1];
974 $row{userfull} = "$data[2] $data[3]";
975 $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
976 $row{usergroup} = $data[5];
977 $row{mkactive} = $data[6];
978 $row{bg} = ($rownum++)%2;
979 $row{sid} = $sid;
980 push @userlist, \%row;
981 }
982 $page->param(usertable => \@userlist);
[18]983}
Note: See TracBrowser for help on using the repository browser.