source: trunk/dns.cgi@ 34

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

/trunk

checkpoint - Net::DNS + DBD::Pg crosswiring bug?

(AXFR NOTAUTH causes Pg transaction commit)

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 31.6 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-11-11 22:55:32 +0000 (Wed, 11 Nov 2009) $
6# SVN revision $Rev: 34 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
18use Crypt::PasswdMD5;
19use Net::DNS;
20use DBI;
21
22use lib '.';
23# custom modules
24use DNSDB qw(:ALL);
25
26my @debugbits; # temp, to be spit out near the end of processing
27
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...
38my %webvar = $q->Vars;
39
40# persistent stuff needed on most/all pages
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.
49 $session->param('logingroup',1);
50 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
51}
52
53my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
54my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
55my $group = ($webvar{group} ? $webvar{group} : 1);
56
57# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
58
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
66my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
67my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
68
69# default
70#my $perpage = 15;
71my $perpage = 3;
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
78my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
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
88# handle login redirect
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);
95
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 }
101
102 # set session bits
103 $session->param('logingroup',$gid);
104 $session->param('curgroup',$gid);
105 $session->param('username',$webvar{username});
106
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 }
119}
120
121## Default page is a login page
122#my $page; # to be initialized as an HTML::Template entity sooner or later
123
124
125
126# decide which page to spit out...
127$webvar{page} = 'login' if !$webvar{page};
128#if (!$webvar{page}) {
129# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
130#} else {
131#}
132
133my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
134
135$page->param(sid => $sid);
136
137if ($webvar{page} eq 'login') {
138
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
145# hmm. seeing problems in some possibly-not-so-corner cases.
146# this currently only handles "domain on", "domain off"
147 if (defined($webvar{action})) {
148 domStatus($dbh,$webvar{id},$webvar{action});
149 }
150
151 $page->param(curpage => $webvar{page});
152
153 listdomains();
154
155} elsif ($webvar{page} eq 'reclist') {
156
157 # Handle record list for both default records (per-group) and live domain records
158
159 $page->param(defrec => $webvar{defrec});
160 $page->param(id => $webvar{id});
161 $page->param(curpage => $webvar{page});
162
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
171# fill the page-count and first-previous-next-last-all details
172 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
173 fill_fpnla($count); # should put some params on this sub...
174
175 $page->param(defrec => $webvar{defrec});
176 if ($webvar{defrec} eq 'y') {
177##fixme: hardcoded group
178 showdomain('y',$curgroup);
179 } else {
180 showdomain('n',$webvar{id});
181 }
182
183} elsif ($webvar{page} eq 'newdomain') {
184
185
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);
204 listdomains($curgroup);
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
214} elsif ($webvar{page} eq 'record') {
215
216 if ($webvar{recact} eq 'new') {
217
218 $page->param(todo => "Add record to");
219 $page->param(recact => "add");
220 fill_rectypes();
221
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 {
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});
246 fill_recdata(); # populate the form... er, mostly.
247 }
248
249 } elsif ($webvar{recact} eq 'edit') {
250
251 $page->param(todo => "Update record");
252 $page->param(recact => "update");
253 $page->param(parentid => $webvar{parentid});
254 $page->param(id => $webvar{id});
255 $page->param(defrec => $webvar{defrec});
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);
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') {
275 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
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});
284 $page->param(id => $webvar{id});
285 fill_recdata();
286 }
287 }
288
289 if ($webvar{defrec} eq 'y') {
290 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
291 } else {
292 $page->param(parentid => $webvar{parentid});
293# $page->param(id => $webvar{id});
294 $page->param(dohere => domainName($dbh,$webvar{parentid}));
295 }
296
297} elsif ($webvar{page} eq 'newrec') {
298 push @debugbits, "whee!\n";
299
300 # populate most fields as needed. (eg, type list.)
301 stdrecs();
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 }
313# wtf?
314# push @recargs,
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);
323 stdrecs($webvar{type}); # populate the form... er, mostly.
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
332 $page->param(defrec => $webvar{defrec});
333
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);
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});
352 } else {
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);
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
395 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
396
397# hokay, a bit of magic to decide which page we hit.
398 if ($code eq 'OK') {
399# redirect to dns.cgi?etc&page=reclist
400 changepage(page => "reclist", id => $msg);
401# $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
402# showdomain(0,$msg);
403 } else {
404# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
405##fixme: session ID
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
412} elsif ($webvar{page} eq 'grpman') {
413
414 listgroups();
415 $page->param(curpage => $webvar{page});
416
417} elsif ($webvar{page} eq 'newgrp') {
418
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});
427 fill_grouplist('pargroup',$webvar{pargroup});
428 } else {
429# $page->param
430 fill_grouplist('pargroup',$curgroup);
431
432 }
433
434} elsif ($webvar{page} eq 'delgrp') {
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);
440# $page->param(groupname => groupName($dbh,$webvar{id}));
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});
448push @debugbits, groupName($dbh, $webvar{id});
449 if ($code ne 'OK') {
450# need to find failure mode
451 $page->param(del_failed => 1);
452 $page->param(errmsg => $msg);
453 $page->param(curpage => $webvar{page});
454 listgroups();
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 }
463 $page->param(delgroupname => groupName($dbh, $webvar{id}));
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},
484 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
485 $webvar{fname}, $webvar{lname}, $webvar{phone});
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);
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
529} elsif ($webvar{page} eq 'dnsq') {
530
531 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
532 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
533 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
534 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
535
536 if ($webvar{qfor}) {
537 my $resolv = Net::DNS::Resolver->new;
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};
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) =
552 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
553 $row{host} = $host;
554 $row{ftype} = $type;
555 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
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
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};
603 $page->param(dominactive => 1) unless $webvar{domactive};
604 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
605##work
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) {
617 my %row;
618 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
619 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
620 $row{domok} = 1 if $code eq 'OK';
621 $row{domwarn} = $msg if $code eq 'WARN';
622 $row{domerr} = $msg if $code eq 'FAIL';
623push @debugbits, "$domain: $code<br>\n";
624 # do stuff! DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
625 $row{domain} = $domain;
626# $row{row} = $rnum++;
627 push @results, \%row;
628 }
629 $page->param(axfrresults => \@results);
630 }
631
632 push @debugbits, "<pre>$webvar{importdoms}</pre>";
633}
634
635
636# start output here so we can redirect pages.
637print "Content-type: text/html\n\n", $header->output;
638
639##common bits
640if ($webvar{page} ne 'login') {
641 $page->param(username => $session->param("username"));
642
643 $page->param(group => $curgroup);
644 $page->param(groupname => groupName($dbh,$curgroup));
645
646 # group tree. should go elsewhere, probably
647 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
648 $page->param(grptree => $tmpgrplist);
649
650 # stuff for menu group change. nb: this is icky.
651 fill_grouplist("grouplist");
652 $page->param(whereami => $ENV{REQUEST_URI});
653}
654
655foreach (@debugbits) { print; }
656
657# spit it out
658print $page->output;
659
660print "<div id=debug>webvar keys: <pre>\n";
661foreach my $key (keys %webvar) {
662 print "key: $key\tval: $webvar{$key}\n";
663}
664print "</pre>\nsession:\n<pre>\n";
665my $sesdata = $session->dataref();
666foreach my $key (keys %$sesdata) {
667 print "key: $key\tval: ".$sesdata->{$key}."\n";
668}
669print "</pre>\nENV:\n<pre>\n";
670foreach my $key (keys %ENV) {
671 print "key: $key\tval: $ENV{$key}\n";
672}
673print "</pre></div>\n";
674
675print $footer->output;
676
677# as per the docs, Just In Case
678$session->flush();
679
680exit 0;
681
682
683sub fill_grptree {
684 my $root = shift;
685 my $cur = shift;
686
687 my @childlist;
688
689 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
690 getChildren($dbh,$root,\@childlist,'immediate');
691 return if $#childlist == -1;
692 my @grouplist;
693 foreach (@childlist) {
694 my %row;
695 $row{grpname} = groupName($dbh,$_);
696 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
697 $row{subs} = fill_grptree($_,$cur);
698 push @grouplist, \%row;
699 }
700 $grptree->param(treelvl => \@grouplist);
701 return $grptree->output;
702}
703
704
705sub changepage {
706 my %params = @_; # think this works the way I want...
707
708 # handle user check
709 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
710 foreach (keys %params) {
711 $newurl .= "&$_=$params{$_}";
712 }
713
714 # Just In Case
715 $session->flush();
716
717 print "Status: 302\nLocation: $newurl\n\n";
718 exit;
719} # end changepage
720
721
722sub fillsoa {
723 my $def = shift;
724 my $id = shift;
725 my $domname;
726
727 if ($webvar{domain} == 0) {
728 $domname = "DOMAIN";
729 } else {
730 my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
731 $sth->execute($webvar{domain});
732 ($domname) = $sth->fetchrow_array();
733 }
734
735 $page->param(domain => $domname);
736 $page->param(defrec => !$webvar{domain});
737 $page->param(group => $DNSDB::group);
738
739# defaults
740 $page->param(defcontact => $DNSDB::def{contact});
741 $page->param(defns => $DNSDB::def{prins});
742 $page->param(defsoattl => $DNSDB::def{soattl});
743 $page->param(defrefresh => $DNSDB::def{refresh});
744 $page->param(defretry => $DNSDB::def{retry});
745 $page->param(defexpire => $DNSDB::def{expire});
746 $page->param(defminttl => $DNSDB::def{minttl});
747
748 # there are probably better ways to do this. TMTOWTDI.
749 my %soa = getSOA($dbh,$def,$id);
750
751 $page->param(domainid => $webvar{domain});
752 $page->param(recid => $soa{recid});
753 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
754 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
755 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
756 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
757 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
758 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
759 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
760}
761
762sub showdomain {
763 my $def = shift;
764 my $id = shift;
765
766 # get the SOA first
767 my %soa = getSOA($dbh,$def,$id);
768
769 $page->param(recid => $soa{recid});
770 $page->param(contact => $soa{contact});
771 $page->param(prins => $soa{prins});
772 $page->param(refresh => $soa{refresh});
773 $page->param(retry => $soa{retry});
774 $page->param(expire => $soa{expire});
775 $page->param(minttl => $soa{minttl});
776 $page->param(ttl => $soa{ttl});
777
778# my @foo2 = getDomRecs($dbh,'def',1);
779 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
780
781 my $row = 0;
782 foreach my $rec (@$foo2) {
783 $rec->{type} = $typemap{$rec->{type}};
784 $rec->{row} = $row % 2;
785 $rec->{defrec} = $webvar{defrec};
786 $rec->{sid} = $webvar{sid};
787 $rec->{id} = $id;
788 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
789 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
790 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
791 $row++;
792 }
793 $page->param(reclist => $foo2);
794}
795
796
797# fill in record type list on add/update/edit record template
798sub fill_rectypes {
799 my $type = shift || $reverse_typemap{A};
800 my $soaflag = shift || 0;
801
802 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
803 $sth->execute;
804 my @typelist;
805 while (my ($rval,$rname) = $sth->fetchrow_array()) {
806 my %row = ( recval => $rval, recname => $rname );
807 $row{tselect} = 1 if $rval == $type;
808 push @typelist, \%row;
809 }
810 if ($soaflag) {
811 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
812 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
813 push @typelist, \%row;
814 }
815 $page->param(typelist => \@typelist);
816} # fill_rectypes
817
818sub fill_recdata {
819 fill_rectypes($webvar{type});
820
821 $page->param(name => $webvar{name});
822 $page->param(address => $webvar{address});
823 $page->param(distance => $webvar{distance})
824 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
825 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
826 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
827 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
828}
829
830
831sub fill_actypelist {
832 my @actypes;
833
834 my %row1 = (actypeval => 'u', actypename => 'user');
835 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
836 push @actypes, \%row1;
837
838 my %row2 = (actypeval => 'S', actypename => 'superuser');
839 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
840 push @actypes, \%row2;
841
842 $page->param(actypelist => \@actypes);
843}
844
845
846sub fill_fpnla {
847 my $count = shift;
848##fixme
849 if ($offset eq 'all') {
850 push @debugbits, "foo! wanna see'em all\n";
851 } else {
852 # all these bits only have sensible behaviour if offset is numeric. err, probably.
853 if ($count > $perpage) {
854 # if there are more results than the default, always show the "all" link
855 $page->param(navall => 1);
856
857 if ($offset > 0) {
858 $page->param(navfirst => 1);
859 $page->param(navprev => 1);
860 $page->param(prevoffs => $offset-1);
861 }
862
863 # show "next" and "last" links if we're not on the last page of results
864 if ( (($offset+1) * $perpage - $count) < 0 ) {
865 $page->param(navnext => 1);
866 $page->param(nextoffs => $offset+1);
867 $page->param(navlast => 1);
868 $page->param(lastoffs => int (($count-1)/$perpage));
869 }
870 }
871 }
872} # end fill_fpnla()
873
874
875sub fill_pgcount {
876 my $pgcount = shift;
877 my $pgtype = shift;
878 my $parent = shift;
879
880 $page->param(ntot => $pgcount);
881 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
882 $page->param(npglast => ($offset eq 'all' ? $pgcount :
883 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
884 ));
885 $page->param(pgtype => $pgtype);
886 $page->param(parent => $parent);
887} # end fill_pgcount()
888
889
890sub listdomains {
891 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
892 $sth->execute($curgroup);
893 my ($count) = $sth->fetchrow_array;
894
895# fill page count and first-previous-next-last-all bits
896##fixme - hardcoded group bit
897 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
898 fill_fpnla($count);
899
900##fixme - group
901 $page->param(group => $curgroup);
902 my @domlist;
903 $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
904 " INNER JOIN groups ON domains.group_id=groups.group_id".
905 " WHERE domains.group_id=?".
906 " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
907 $sth->execute($curgroup);
908 my $rownum = 0;
909 while (my @data = $sth->fetchrow_array) {
910 my %row;
911 $row{domainid} = $data[0];
912 $row{domain} = $data[1];
913 $row{status} = ($data[2] ? 'Active' : 'Inactive');
914 $row{group} = $data[3];
915 $row{bg} = ($rownum++)%2;
916# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
917 $row{mkactive} = !$data[2];
918 $row{sid} = $sid;
919 $row{offset} = $offset;
920##fixme: need to clean up status indicator/usage/inversion
921 push @domlist, \%row;
922 }
923 $page->param(domtable => \@domlist);
924} # end listdomains()
925
926
927sub listgroups {
928 my @childgroups;
929 getChildren($dbh, $logingroup, \@childgroups, 'all');
930 my $childlist = join(',',@childgroups);
931
932 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")";
933 my $sth = $dbh->prepare($sql);
934
935 $sth->execute;
936 my ($count) = ($sth->fetchrow_array);
937# fill page count and first-previous-next-last-all bits
938##fixme - hardcoded group bit
939 fill_pgcount($count,"groups",'');
940 fill_fpnla($count);
941
942 my @grouplist;
943 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
944 "count(distinct(u.username)), count(distinct(d.domain)) ".
945 "FROM groups g ".
946 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
947 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
948 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
949 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
950 "GROUP BY g.group_id, g.group_name, g2.group_name ".
951 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
952 $sth->execute;
953
954 my $rownum = 0;
955 while (my @data = $sth->fetchrow_array) {
956 my %row;
957 $row{groupid} = $data[0];
958 $row{groupname} = $data[1];
959 $row{pgroup} = $data[2];
960 $row{nusers} = $data[3];
961 $row{ndomains} = $data[4];
962 $row{bg} = ($rownum++)%2;
963 $row{sid} = $sid;
964 push @grouplist, \%row;
965 }
966 $page->param(grouptable => \@grouplist);
967} # end listgroups()
968
969
970sub fill_grouplist {
971 my $template_var = shift;
972 my $cur = shift || $curgroup;
973
974 my @childgroups;
975 getChildren($dbh, $logingroup, \@childgroups, 'all');
976 my $childlist = join(',',@childgroups);
977
978 # weesa gonna discard parent_group_id for now
979 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
980 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
981 "ORDER BY group_id");
982 $sth->execute;
983 my @grouplist;
984 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
985 my %row;
986 $row{groupname} = $groupname;
987 $row{groupval} = $groupid;
988##fixme: need magic
989# $row{defgroup} = '';
990 $row{groupactive} = 1 if $groupid == $cur;
991 push @grouplist, \%row;
992 }
993
994 $page->param("$template_var" => \@grouplist);
995
996} # end fill_grouplist()
997
998
999sub list_users {
1000 my $sth = $dbh->prepare("select count(*) from users where group_id=?");
1001 $sth->execute($curgroup);
1002 my ($count) = ($sth->fetchrow_array);
1003
1004# fill page count and first-previous-next-last-all bits
1005##fixme - hardcoded group bit
1006 fill_pgcount($count,"users",'');
1007 fill_fpnla($count);
1008
1009 my @userlist;
1010 $sth = $dbh->prepare("SELECT u.user_id, u.username, u.firstname, u.lastname, u.type, g.group_name, u.status ".
1011 "FROM users u ".
1012 "INNER JOIN groups g ON u.group_id=g.group_id ".
1013 "WHERE u.group_id=?".
1014 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1015 $sth->execute($curgroup);
1016
1017 my $rownum = 0;
1018 while (my @data = $sth->fetchrow_array) {
1019 my %row;
1020 $row{userid} = $data[0];
1021 $row{username} = $data[1];
1022 $row{userfull} = "$data[2] $data[3]";
1023 $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
1024 $row{usergroup} = $data[5];
1025 $row{mkactive} = $data[6];
1026 $row{bg} = ($rownum++)%2;
1027 $row{sid} = $sid;
1028 push @userlist, \%row;
1029 }
1030 $page->param(usertable => \@userlist);
1031}
Note: See TracBrowser for help on using the repository browser.