source: trunk/dns.cgi@ 37

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

/trunk

AXFR import is now basically functional

  • could maybe use a few more recognized types
  • need to find someplace to stuff the serial from the imported SOA

Fix missing template substitution on regular new domain creation

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 31.6 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-11-18 18:42:22 +0000 (Wed, 18 Nov 2009) $
6# SVN revision $Rev: 37 $
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
[37]70my $perpage = 15;
71#my $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};
[31]532 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
533 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]534 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
535
536 if ($webvar{qfor}) {
537 my $resolv = Net::DNS::Resolver->new;
[31]538 $resolv->tcp_timeout(5); # make me adjustable!
539 $resolv->udp_timeout(5); # make me adjustable!
540 $resolv->recurse(0) if $webvar{nrecurse};
541 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
[30]542 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
543 if ($query) {
544
545 $page->param(showresults => 1);
546
547 my @answer;
548 foreach my $rr ($query->answer) {
549# next unless $rr->type eq "A" or $rr->type eq 'NS';
550 my %row;
551 my ($host,$ttl,$class,$type,$data) =
[31]552 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]553 $row{host} = $host;
554 $row{ftype} = $type;
[31]555 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]556 push @answer, \%row;
557 }
558 $page->param(answer => \@answer);
559
560 my @additional;
561 foreach my $rr ($query->additional) {
562# next unless $rr->type eq "A" or $rr->type eq 'NS';
563 my %row;
564 my ($host,$ttl,$class,$type,$data) =
565 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
566 $row{host} = $host;
567 $row{ftype} = $type;
568 $row{rdata} = $data;
569 push @additional, \%row;
570 }
571 $page->param(additional => \@additional);
572
573 my @authority;
574 foreach my $rr ($query->authority) {
575# next unless $rr->type eq "A" or $rr->type eq 'NS';
576 my %row;
577 my ($host,$ttl,$class,$type,$data) =
578 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
579 $row{host} = $host;
580 $row{ftype} = $type;
581 $row{rdata} = $data;
582 push @authority, \%row;
583 }
584 $page->param(authority => \@authority);
585
586 $page->param(usedresolver => $resolv->answerfrom);
587 $page->param(frtype => $typemap{$webvar{type}});
588
589 } else {
590 $page->param(errmsg => $resolv->errorstring);
591 }
592 }
593 ## done DNS query
594
[31]595} elsif ($webvar{page} eq 'axfr') {
596
597 # don't need this while we've got the dropdown in the menu. hmm.
598 #fill_grouplist;
599
600 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
601 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
602 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
[37]603 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]604 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
605##work
[33]606
607##fixme: check group too?
608 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
609 $page->param(errmsg => "Need to set host to import from");
610 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
611 $page->param(errmsg => "Need domains to import");
612 } else {
613 my @domlist = split /\s+/, $webvar{importdoms};
614 my @results;
615my $rnum = 0;
616 foreach my $domain (@domlist) {
[34]617 my %row;
618 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
619 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
[35]620 $row{domok} = $msg if $code eq 'OK';
621 if ($code eq 'WARN') {
622 $msg =~ s|\n|<br />|g;
623 $row{domwarn} = $msg;
624 }
[37]625 if ($code eq 'FAIL') {
626 $msg =~ s|\n|<br />|g;
627 $row{domerr} = $msg;
628 }
[33]629 # do stuff! DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
630 $row{domain} = $domain;
631# $row{row} = $rnum++;
632 push @results, \%row;
633 }
634 $page->param(axfrresults => \@results);
635 }
636
[2]637}
638
639
[17]640# start output here so we can redirect pages.
[7]641print "Content-type: text/html\n\n", $header->output;
642
[20]643##common bits
[17]644if ($webvar{page} ne 'login') {
[30]645 $page->param(username => $session->param("username"));
646
[20]647 $page->param(group => $curgroup);
648 $page->param(groupname => groupName($dbh,$curgroup));
649
[24]650 # group tree. should go elsewhere, probably
651 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
652 $page->param(grptree => $tmpgrplist);
653
[20]654 # stuff for menu group change. nb: this is icky.
655 fill_grouplist("grouplist");
656 $page->param(whereami => $ENV{REQUEST_URI});
[17]657}
[13]658
[24]659foreach (@debugbits) { print; }
660
[2]661# spit it out
662print $page->output;
663
664print "<div id=debug>webvar keys: <pre>\n";
665foreach my $key (keys %webvar) {
666 print "key: $key\tval: $webvar{$key}\n";
667}
[20]668print "</pre>\nsession:\n<pre>\n";
669my $sesdata = $session->dataref();
670foreach my $key (keys %$sesdata) {
671 print "key: $key\tval: ".$sesdata->{$key}."\n";
672}
[2]673print "</pre>\nENV:\n<pre>\n";
674foreach my $key (keys %ENV) {
675 print "key: $key\tval: $ENV{$key}\n";
676}
677print "</pre></div>\n";
678
679print $footer->output;
680
[18]681# as per the docs, Just In Case
682$session->flush();
[2]683
684exit 0;
685
686
[24]687sub fill_grptree {
688 my $root = shift;
689 my $cur = shift;
690
691 my @childlist;
692
693 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
694 getChildren($dbh,$root,\@childlist,'immediate');
695 return if $#childlist == -1;
696 my @grouplist;
697 foreach (@childlist) {
698 my %row;
699 $row{grpname} = groupName($dbh,$_);
700 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
701 $row{subs} = fill_grptree($_,$cur);
702 push @grouplist, \%row;
703 }
704 $grptree->param(treelvl => \@grouplist);
705 return $grptree->output;
706}
707
708
[11]709sub changepage {
710 my %params = @_; # think this works the way I want...
711
712 # handle user check
713 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
714 foreach (keys %params) {
715 $newurl .= "&$_=$params{$_}";
716 }
717
[30]718 # Just In Case
719 $session->flush();
720
[11]721 print "Status: 302\nLocation: $newurl\n\n";
722 exit;
723} # end changepage
724
725
[2]726sub fillsoa {
727 my $def = shift;
728 my $id = shift;
729 my $domname;
730
731 if ($webvar{domain} == 0) {
732 $domname = "DOMAIN";
733 } else {
[17]734 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
735 $sth->execute($webvar{domain});
[2]736 ($domname) = $sth->fetchrow_array();
737 }
738
[17]739 $page->param(domain => $domname);
740 $page->param(defrec => !$webvar{domain});
741 $page->param(group => $DNSDB::group);
[2]742
743# defaults
[17]744 $page->param(defcontact => $DNSDB::def{contact});
745 $page->param(defns => $DNSDB::def{prins});
746 $page->param(defsoattl => $DNSDB::def{soattl});
747 $page->param(defrefresh => $DNSDB::def{refresh});
748 $page->param(defretry => $DNSDB::def{retry});
749 $page->param(defexpire => $DNSDB::def{expire});
750 $page->param(defminttl => $DNSDB::def{minttl});
[2]751
752 # there are probably better ways to do this. TMTOWTDI.
753 my %soa = getSOA($dbh,$def,$id);
754
755 $page->param(domainid => $webvar{domain});
756 $page->param(recid => $soa{recid});
757 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
758 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
759 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
760 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
761 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
762 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
763 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
764}
765
766sub showdomain {
767 my $def = shift;
768 my $id = shift;
769
770 # get the SOA first
771 my %soa = getSOA($dbh,$def,$id);
772
773 $page->param(recid => $soa{recid});
774 $page->param(contact => $soa{contact});
775 $page->param(prins => $soa{prins});
776 $page->param(refresh => $soa{refresh});
777 $page->param(retry => $soa{retry});
778 $page->param(expire => $soa{expire});
779 $page->param(minttl => $soa{minttl});
780 $page->param(ttl => $soa{ttl});
781
782# my @foo2 = getDomRecs($dbh,'def',1);
[4]783 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]784
785 my $row = 0;
786 foreach my $rec (@$foo2) {
787 $rec->{type} = $typemap{$rec->{type}};
788 $rec->{row} = $row % 2;
789 $rec->{defrec} = $webvar{defrec};
790 $rec->{sid} = $webvar{sid};
[13]791 $rec->{id} = $id;
[23]792 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
793 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
794 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]795 $row++;
796 }
797 $page->param(reclist => $foo2);
798}
799
[23]800
[16]801# fill in record type list on add/update/edit record template
802sub fill_rectypes {
[13]803 my $type = shift || $reverse_typemap{A};
[31]804 my $soaflag = shift || 0;
[13]805
[17]806 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]807 $sth->execute;
808 my @typelist;
809 while (my ($rval,$rname) = $sth->fetchrow_array()) {
810 my %row = ( recval => $rval, recname => $rname );
[13]811 $row{tselect} = 1 if $rval == $type;
[2]812 push @typelist, \%row;
813 }
[31]814 if ($soaflag) {
815 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
816 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
817 push @typelist, \%row;
818 }
[2]819 $page->param(typelist => \@typelist);
[31]820} # fill_rectypes
[16]821
822sub fill_recdata {
823 fill_rectypes($webvar{type});
824
825 $page->param(name => $webvar{name});
826 $page->param(address => $webvar{address});
827 $page->param(distance => $webvar{distance})
828 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
829 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
830 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[2]831 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
832}
[7]833
[24]834
835sub fill_actypelist {
836 my @actypes;
837
838 my %row1 = (actypeval => 'u', actypename => 'user');
839 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
840 push @actypes, \%row1;
841
842 my %row2 = (actypeval => 'S', actypename => 'superuser');
843 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
844 push @actypes, \%row2;
845
846 $page->param(actypelist => \@actypes);
847}
848
849
[7]850sub fill_fpnla {
851 my $count = shift;
852##fixme
853 if ($offset eq 'all') {
[17]854 push @debugbits, "foo! wanna see'em all\n";
[7]855 } else {
856 # all these bits only have sensible behaviour if offset is numeric. err, probably.
857 if ($count > $perpage) {
858 # if there are more results than the default, always show the "all" link
859 $page->param(navall => 1);
860
861 if ($offset > 0) {
862 $page->param(navfirst => 1);
863 $page->param(navprev => 1);
864 $page->param(prevoffs => $offset-1);
865 }
866
867 # show "next" and "last" links if we're not on the last page of results
868 if ( (($offset+1) * $perpage - $count) < 0 ) {
869 $page->param(navnext => 1);
870 $page->param(nextoffs => $offset+1);
871 $page->param(navlast => 1);
[8]872 $page->param(lastoffs => int (($count-1)/$perpage));
[7]873 }
874 }
875 }
[10]876} # end fill_fpnla()
877
878
[12]879sub fill_pgcount {
880 my $pgcount = shift;
881 my $pgtype = shift;
882 my $parent = shift;
883
884 $page->param(ntot => $pgcount);
885 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
886 $page->param(npglast => ($offset eq 'all' ? $pgcount :
887 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
888 ));
889 $page->param(pgtype => $pgtype);
890 $page->param(parent => $parent);
891} # end fill_pgcount()
892
893
[11]894sub listdomains {
[17]895 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
[20]896 $sth->execute($curgroup);
[17]897 my ($count) = $sth->fetchrow_array;
898
[12]899# fill page count and first-previous-next-last-all bits
900##fixme - hardcoded group bit
[20]901 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]902 fill_fpnla($count);
903
904##fixme - group
[20]905 $page->param(group => $curgroup);
[10]906 my @domlist;
[20]907 $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
908 " INNER JOIN groups ON domains.group_id=groups.group_id".
909 " WHERE domains.group_id=?".
910 " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
911 $sth->execute($curgroup);
[10]912 my $rownum = 0;
913 while (my @data = $sth->fetchrow_array) {
914 my %row;
915 $row{domainid} = $data[0];
916 $row{domain} = $data[1];
917 $row{status} = ($data[2] ? 'Active' : 'Inactive');
918 $row{group} = $data[3];
919 $row{bg} = ($rownum++)%2;
920# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
921 $row{mkactive} = !$data[2];
922 $row{sid} = $sid;
923 $row{offset} = $offset;
924##fixme: need to clean up status indicator/usage/inversion
925 push @domlist, \%row;
926 }
927 $page->param(domtable => \@domlist);
[11]928} # end listdomains()
[18]929
930
[22]931sub listgroups {
[26]932 my @childgroups;
933 getChildren($dbh, $logingroup, \@childgroups, 'all');
934 my $childlist = join(',',@childgroups);
935
936 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")";
937 my $sth = $dbh->prepare($sql);
938
[22]939 $sth->execute;
940 my ($count) = ($sth->fetchrow_array);
941# fill page count and first-previous-next-last-all bits
942##fixme - hardcoded group bit
943 fill_pgcount($count,"groups",'');
944 fill_fpnla($count);
945
946 my @grouplist;
947 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[26]948 "count(distinct(u.username)), count(distinct(d.domain)) ".
[22]949 "FROM groups g ".
950 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
951 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
952 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[26]953 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[22]954 "GROUP BY g.group_id, g.group_name, g2.group_name ".
955 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
956 $sth->execute;
957
958 my $rownum = 0;
959 while (my @data = $sth->fetchrow_array) {
960 my %row;
961 $row{groupid} = $data[0];
962 $row{groupname} = $data[1];
963 $row{pgroup} = $data[2];
964 $row{nusers} = $data[3];
965 $row{ndomains} = $data[4];
966 $row{bg} = ($rownum++)%2;
967 $row{sid} = $sid;
968 push @grouplist, \%row;
969 }
970 $page->param(grouptable => \@grouplist);
971} # end listgroups()
972
973
[20]974sub fill_grouplist {
[19]975 my $template_var = shift;
976 my $cur = shift || $curgroup;
[26]977
978 my @childgroups;
979 getChildren($dbh, $logingroup, \@childgroups, 'all');
980 my $childlist = join(',',@childgroups);
981
[18]982 # weesa gonna discard parent_group_id for now
[26]983 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
984 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
985 "ORDER BY group_id");
[18]986 $sth->execute;
[20]987 my @grouplist;
988 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]989 my %row;
[20]990 $row{groupname} = $groupname;
991 $row{groupval} = $groupid;
[18]992##fixme: need magic
[20]993# $row{defgroup} = '';
994 $row{groupactive} = 1 if $groupid == $cur;
995 push @grouplist, \%row;
[18]996 }
997
[20]998 $page->param("$template_var" => \@grouplist);
[18]999
[24]1000} # end fill_grouplist()
1001
[26]1002
[24]1003sub list_users {
1004 my $sth = $dbh->prepare("select count(*) from users where group_id=?");
1005 $sth->execute($curgroup);
1006 my ($count) = ($sth->fetchrow_array);
1007
1008# fill page count and first-previous-next-last-all bits
1009##fixme - hardcoded group bit
1010 fill_pgcount($count,"users",'');
1011 fill_fpnla($count);
1012
1013 my @userlist;
[25]1014 $sth = $dbh->prepare("SELECT u.user_id, u.username, u.firstname, u.lastname, u.type, g.group_name, u.status ".
[24]1015 "FROM users u ".
1016 "INNER JOIN groups g ON u.group_id=g.group_id ".
1017 "WHERE u.group_id=?".
1018 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1019 $sth->execute($curgroup);
1020
1021 my $rownum = 0;
1022 while (my @data = $sth->fetchrow_array) {
1023 my %row;
1024 $row{userid} = $data[0];
1025 $row{username} = $data[1];
1026 $row{userfull} = "$data[2] $data[3]";
1027 $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
1028 $row{usergroup} = $data[5];
1029 $row{mkactive} = $data[6];
1030 $row{bg} = ($rownum++)%2;
1031 $row{sid} = $sid;
1032 push @userlist, \%row;
1033 }
1034 $page->param(usertable => \@userlist);
[18]1035}
Note: See TracBrowser for help on using the repository browser.