source: trunk/dns.cgi@ 39

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

/trunk

XHTML strict, pass 2
fixed up a collection of things found to be broken (mostly relating to hardcoded group bits)
added a collection of notes-to-self to notes

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