source: trunk/dns.cgi@ 48

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

/trunk

WHOIS query is functional - requires Net::Whois::Raw and Text::Wrap (idjit

WHOIS servers return (very-)long-line respones sometimes)

Added proper template for WHOIS, removed copy-of-VegaDNS-output version

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 36.6 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-12-10 21:24:55 +0000 (Thu, 10 Dec 2009) $
6# SVN revision $Rev: 48 $
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
27my $debugenv = 0;
28
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...
39my %webvar = $q->Vars;
40
41# persistent stuff needed on most/all pages
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.
50 $session->param('logingroup',1);
51 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
52}
53
54my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
55my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
56my $group = ($webvar{group} ? $webvar{group} : 1);
57
58# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
59
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
67my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
68my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
69
70# default
71#my $perpage = 15;
72my $perpage = 3;
73my $offset = ($webvar{offset} ? $webvar{offset} : 0);
74
75# NB: these must match the field name and SQL ascend/descend syntax respectively
76my $sortby = "domain";
77my $sortorder = "ASC";
78
79my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","dbhost");
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
89# handle login redirect
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);
96
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 }
102
103 # set session bits
104 $session->param('logingroup',$gid);
105 $session->param('curgroup',$gid);
106 $session->param('username',$webvar{username});
107
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 }
120}
121
122## Default page is a login page
123#my $page; # to be initialized as an HTML::Template entity sooner or later
124
125
126
127# decide which page to spit out...
128$webvar{page} = 'login' if !$webvar{page};
129#if (!$webvar{page}) {
130# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
131#} else {
132#}
133
134my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
135
136$page->param(sid => $sid);
137
138if ($webvar{page} eq 'login') {
139
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
146# hmm. seeing problems in some possibly-not-so-corner cases.
147# this currently only handles "domain on", "domain off"
148 if (defined($webvar{action})) {
149 domStatus($dbh,$webvar{id},$webvar{action});
150 }
151
152 $page->param(curpage => $webvar{page});
153
154 listdomains();
155
156} elsif ($webvar{page} eq 'newdomain') {
157
158
159} elsif ($webvar{page} eq 'deldom') {
160
161 $page->param(id => $webvar{id});
162 # first pass = confirm y/n (sorta)
163 if (!defined($webvar{del})) {
164 $page->param(del_getconf => 1);
165 $page->param(domain => domainName($dbh,$webvar{id}));
166# print some neato things?
167
168# } else {
169# #whether actually deleting or cancelling we redirect to the domain list, default format
170
171 } elsif ($webvar{del} eq 'ok') {
172 my ($code,$msg) = delDomain($dbh, $webvar{id});
173 if ($code ne 'OK') {
174# need to find failure mode
175 $page->param(del_failed => 1);
176 $page->param(errmsg => $msg);
177 listdomains($curgroup);
178 } else {
179 # success. go back to the domain list, do not pass "GO"
180 changepage(page => "domlist");
181 }
182 } else {
183 # cancelled. whee!
184 changepage(page => "domlist");
185 }
186
187} elsif ($webvar{page} eq 'reclist') {
188
189 # Handle record list for both default records (per-group) and live domain records
190
191 $page->param(defrec => $webvar{defrec});
192 $page->param(id => $webvar{id});
193 $page->param(curpage => $webvar{page});
194
195# select count(*) from (default_)?records where (group|domain)_id=?
196 my $sth = $dbh->prepare("SELECT count(*) FROM ".
197 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
198 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
199 "AND NOT type=$reverse_typemap{SOA}");
200 $sth->execute($webvar{id});
201 my ($count) = ($sth->fetchrow_array);
202
203# fill the page-count and first-previous-next-last-all details
204 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
205 fill_fpnla($count); # should put some params on this sub...
206
207 $page->param(defrec => $webvar{defrec});
208 if ($webvar{defrec} eq 'y') {
209##fixme: hardcoded group
210 showdomain('y',$curgroup);
211 } else {
212 showdomain('n',$webvar{id});
213 }
214
215} elsif ($webvar{page} eq 'record') {
216
217 if ($webvar{recact} eq 'new') {
218
219 $page->param(todo => "Add record to");
220 $page->param(recact => "add");
221 fill_rectypes();
222
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 {
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});
247 fill_recdata(); # populate the form... er, mostly.
248 }
249
250 } elsif ($webvar{recact} eq 'edit') {
251
252 $page->param(todo => "Update record");
253 $page->param(recact => "update");
254 $page->param(parentid => $webvar{parentid});
255 $page->param(id => $webvar{id});
256 $page->param(defrec => $webvar{defrec});
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);
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') {
276 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
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});
285 $page->param(id => $webvar{id});
286 fill_recdata();
287 }
288 }
289
290 if ($webvar{defrec} eq 'y') {
291 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
292 } else {
293 $page->param(parentid => $webvar{parentid});
294# $page->param(id => $webvar{id});
295 $page->param(dohere => domainName($dbh,$webvar{parentid}));
296 }
297
298} elsif ($webvar{page} eq 'newrec') {
299 push @debugbits, "whee!\n";
300
301 # populate most fields as needed. (eg, type list.)
302 stdrecs();
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 }
314# wtf?
315# push @recargs,
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);
324 stdrecs($webvar{type}); # populate the form... er, mostly.
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
333 $page->param(defrec => $webvar{defrec});
334
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});
346 $page->param(parentid => $webvar{parentid});
347 # first pass = confirm y/n (sorta)
348 if (!defined($webvar{del})) {
349 $page->param(del_getconf => 1);
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});
354 } elsif ($webvar{del} eq 'ok') {
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);
360 showdomain($webvar{defrec}, $webvar{parentid});
361 } else {
362 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
363 }
364 } else {
365 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
366 }
367
368} elsif ($webvar{page} eq 'editsoa') {
369
370 fillsoa($webvar{defrec},$webvar{id});
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
378 $sql = "update ".($webvar{defrec} eq 'y' ? "default_records" : "records").
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);
388 fillsoa($webvar{defrec},$webvar{id});
389 } else {
390 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
391 $page->param(update_failed => 0);
392##fixme! need to set group ID properly here
393# showdomain('y',1);
394 }
395
396} elsif ($webvar{page} eq 'adddomain') {
397 # Need some magic here.
398
399##fixme: Group should be variable
400 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
401
402# hokay, a bit of magic to decide which page we hit.
403 if ($code eq 'OK') {
404# redirect to dns.cgi?etc&page=reclist
405 changepage(page => "reclist", id => $msg);
406# $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
407# showdomain(0,$msg);
408 } else {
409# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
410##fixme: session ID
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
417} elsif ($webvar{page} eq 'grpman') {
418
419 listgroups();
420 $page->param(curpage => $webvar{page});
421
422} elsif ($webvar{page} eq 'newgrp') {
423
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});
432 fill_grouplist('pargroup',$webvar{pargroup});
433 } else {
434# $page->param
435 fill_grouplist('pargroup',$curgroup);
436
437 }
438
439} elsif ($webvar{page} eq 'delgrp') {
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);
445# $page->param(groupname => groupName($dbh,$webvar{id}));
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});
453push @debugbits, groupName($dbh, $webvar{id});
454 if ($code ne 'OK') {
455# need to find failure mode
456 $page->param(del_failed => 1);
457 $page->param(errmsg => $msg);
458 $page->param(curpage => $webvar{page});
459 listgroups();
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 }
468 $page->param(delgroupname => groupName($dbh, $webvar{id}));
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 {
488 ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
489 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
490 $webvar{fname}, $webvar{lname}, $webvar{phone});
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 {
497# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
498 $page->param(add_failed => 1);
499 $page->param(uname => $webvar{uname});
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
508# $page->param(add_failed => 1);
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
533} elsif ($webvar{page} eq 'dnsq') {
534
535 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
536 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
537 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
538 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
539
540 if ($webvar{qfor}) {
541 my $resolv = Net::DNS::Resolver->new;
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};
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) =
556 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
557 $row{host} = $host;
558 $row{ftype} = $type;
559 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
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
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};
607 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
608 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
609##work
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) {
621 my %row;
622 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
623 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
624 $row{domok} = $msg if $code eq 'OK';
625 if ($code eq 'WARN') {
626 $msg =~ s|\n|<br />|g;
627 $row{domwarn} = $msg;
628 }
629 if ($code eq 'FAIL') {
630 $msg =~ s|\n|<br />|g;
631 $row{domerr} = $msg;
632 }
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
641} elsif ($webvar{page} eq 'whoisq') {
642
643 if ($webvar{qfor}) {
644 use Net::Whois::Raw;
645 use Text::Wrap;
646
647# caching useful?
648#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
649#$Net::Whois::Raw::CACHE_TIME = 60;
650
651 my ($dominfo, $whois_server) = whois($webvar{qfor});
652##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
653
654 # le sigh. idjits spit out data without linefeeds...
655 $Text::Wrap::columns = 88;
656
657# &%$@%@# high-bit crap. We should probably find a way to properly recode these instead of one-by-one.
658# Mainly an XHTML validation thing.
659 $dominfo =~ s/\xa9/\&copy;/g;
660 $dominfo =~ s/\xae/\&reg;/g;
661
662 $page->param(qfor => $webvar{qfor});
663 $page->param(dominfo => wrap('','',$dominfo));
664 $page->param(whois_server => $whois_server);
665 } else {
666 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
667 }
668
669} elsif ($webvar{page} eq 'log') {
670
671##fixme put in some real log-munching stuff
672##fixme need to add bits to *create* log entries...
673 my $sth = $dbh->prepare("SELECT * FROM log");
674 $sth->execute;
675 my @logbits;
676 while (my @data = $sth->fetchrow_array) {
677 my %row;
678# (1,1,1,'test@test','Test','frobbed the whatsit',now());
679 $row{userfname} = $data[4];
680 $row{userid} = $data[1];
681 $row{useremail} = $data[3];
682 $row{logentry} = $data[5];
683 $row{logtime} = $data[6];
684 push @logbits, \%row;
685 }
686 $page->param(logentries => \@logbits);
687
688}
689
690
691# start output here so we can redirect pages.
692print "Content-type: text/html\n\n", $header->output;
693
694##common bits
695if ($webvar{page} ne 'login') {
696 $page->param(username => $session->param("username"));
697
698 $page->param(group => $curgroup);
699 $page->param(groupname => groupName($dbh,$curgroup));
700 $page->param(logingrp => groupName($dbh,$logingroup));
701
702 # group tree. should go elsewhere, probably
703 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
704 $page->param(grptree => $tmpgrplist);
705
706 $page->param(inlogingrp => $curgroup == $logingroup);
707
708 # stuff for menu group change. nb: this is icky.
709 fill_grouplist("grouplist");
710# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
711 my $tmp_ruri = $ENV{REQUEST_URI};
712 $tmp_ruri =~ s/\&([a-z])/\&amp\;$1/g;
713# le sigh. and we need to strip any previous action
714 $tmp_ruri =~ s/\&amp;action=[^&]+//g;
715# and any bits from the "starts with" letter block - (ab)using this widget
716# to fill the bulk of the URI so various templates don't grow to insanity
717 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]+//g;
718# $page->param(whereami => $ENV{REQUEST_URI});
719 $page->param(whereami => $tmp_ruri);
720}
721
722foreach (@debugbits) { print; }
723
724# spit it out
725print $page->output;
726
727if ($debugenv) {
728 print "<div id=\"debug\">webvar keys: <pre>\n";
729 foreach my $key (keys %webvar) {
730 print "key: $key\tval: $webvar{$key}\n";
731 }
732 print "</pre>\nsession:\n<pre>\n";
733 my $sesdata = $session->dataref();
734 foreach my $key (keys %$sesdata) {
735 print "key: $key\tval: ".$sesdata->{$key}."\n";
736 }
737 print "</pre>\nENV:\n<pre>\n";
738 foreach my $key (keys %ENV) {
739 print "key: $key\tval: $ENV{$key}\n";
740 }
741 print "</pre></div>\n";
742}
743
744print $footer->output;
745
746# as per the docs, Just In Case
747$session->flush();
748
749exit 0;
750
751
752sub fill_grptree {
753 my $root = shift;
754 my $cur = shift;
755
756 my @childlist;
757
758 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
759 getChildren($dbh,$root,\@childlist,'immediate');
760 return if $#childlist == -1;
761 my @grouplist;
762 foreach (@childlist) {
763 my %row;
764 $row{grpname} = groupName($dbh,$_);
765 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
766 $row{subs} = fill_grptree($_,$cur);
767 push @grouplist, \%row;
768 }
769 $grptree->param(treelvl => \@grouplist);
770 return $grptree->output;
771}
772
773
774sub changepage {
775 my %params = @_; # think this works the way I want...
776
777 # handle user check
778 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
779 foreach (keys %params) {
780 $newurl .= "&$_=$params{$_}";
781 }
782
783 # Just In Case
784 $session->flush();
785
786 print "Status: 302\nLocation: $newurl\n\n";
787 exit;
788} # end changepage
789
790
791sub fillsoa {
792 my $def = shift;
793 my $id = shift;
794 my $domname = ($def eq 'y' ? '' : "DOMAIN");
795
796 $page->param(defrec => $def);
797
798# i had a good reason to do this when I wrote it...
799# $page->param(domain => $domname);
800# $page->param(group => $DNSDB::group);
801 $page->param(isgrp => 1) if $def eq 'y';
802 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
803
804# defaults
805 $page->param(defcontact => $DNSDB::def{contact});
806 $page->param(defns => $DNSDB::def{prins});
807 $page->param(defsoattl => $DNSDB::def{soattl});
808 $page->param(defrefresh => $DNSDB::def{refresh});
809 $page->param(defretry => $DNSDB::def{retry});
810 $page->param(defexpire => $DNSDB::def{expire});
811 $page->param(defminttl => $DNSDB::def{minttl});
812
813 # there are probably better ways to do this. TMTOWTDI.
814 my %soa = getSOA($dbh,$def,$id);
815
816 $page->param(id => $id);
817 $page->param(recid => $soa{recid});
818 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
819 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
820 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
821 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
822 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
823 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
824 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
825}
826
827sub showdomain {
828 my $def = shift;
829 my $id = shift;
830
831 # get the SOA first
832 my %soa = getSOA($dbh,$def,$id);
833
834 $page->param(recid => $soa{recid});
835 $page->param(contact => $soa{contact});
836 $page->param(prins => $soa{prins});
837 $page->param(refresh => $soa{refresh});
838 $page->param(retry => $soa{retry});
839 $page->param(expire => $soa{expire});
840 $page->param(minttl => $soa{minttl});
841 $page->param(ttl => $soa{ttl});
842
843# my @foo2 = getDomRecs($dbh,'def',1);
844 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
845
846 my $row = 0;
847 foreach my $rec (@$foo2) {
848 $rec->{type} = $typemap{$rec->{type}};
849 $rec->{row} = $row % 2;
850 $rec->{defrec} = $webvar{defrec};
851 $rec->{sid} = $webvar{sid};
852 $rec->{id} = $id;
853 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
854 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
855 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
856 $row++;
857 }
858 $page->param(reclist => $foo2);
859}
860
861
862# fill in record type list on add/update/edit record template
863sub fill_rectypes {
864 my $type = shift || $reverse_typemap{A};
865 my $soaflag = shift || 0;
866
867 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
868 $sth->execute;
869 my @typelist;
870 while (my ($rval,$rname) = $sth->fetchrow_array()) {
871 my %row = ( recval => $rval, recname => $rname );
872 $row{tselect} = 1 if $rval == $type;
873 push @typelist, \%row;
874 }
875 if ($soaflag) {
876 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
877 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
878 push @typelist, \%row;
879 }
880 $page->param(typelist => \@typelist);
881} # fill_rectypes
882
883sub fill_recdata {
884 fill_rectypes($webvar{type});
885
886 $page->param(name => $webvar{name});
887 $page->param(address => $webvar{address});
888 $page->param(distance => $webvar{distance})
889 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
890 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
891 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
892 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
893}
894
895
896sub fill_actypelist {
897 my @actypes;
898
899 my %row1 = (actypeval => 'u', actypename => 'user');
900 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
901 push @actypes, \%row1;
902
903 my %row2 = (actypeval => 'S', actypename => 'superuser');
904 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
905 push @actypes, \%row2;
906
907 $page->param(actypelist => \@actypes);
908}
909
910
911sub fill_fpnla {
912 my $count = shift;
913##fixme
914 if ($offset eq 'all') {
915# uhm....
916 } else {
917 # all these bits only have sensible behaviour if offset is numeric. err, probably.
918 if ($count > $perpage) {
919 # if there are more results than the default, always show the "all" link
920 $page->param(navall => 1);
921
922 if ($offset > 0) {
923 $page->param(navfirst => 1);
924 $page->param(navprev => 1);
925 $page->param(prevoffs => $offset-1);
926 }
927
928 # show "next" and "last" links if we're not on the last page of results
929 if ( (($offset+1) * $perpage - $count) < 0 ) {
930 $page->param(navnext => 1);
931 $page->param(nextoffs => $offset+1);
932 $page->param(navlast => 1);
933 $page->param(lastoffs => int (($count-1)/$perpage));
934 }
935 }
936 }
937} # end fill_fpnla()
938
939
940sub fill_pgcount {
941 my $pgcount = shift;
942 my $pgtype = shift;
943 my $parent = shift;
944
945 $page->param(ntot => $pgcount);
946 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
947 $page->param(npglast => ($offset eq 'all' ? $pgcount :
948 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
949 ));
950 $page->param(pgtype => $pgtype);
951 $page->param(parent => $parent);
952} # end fill_pgcount()
953
954
955sub listdomains {
956
957##fixme: account for searches and starts-with filters
958 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
959 $sth->execute($curgroup);
960 my ($count) = $sth->fetchrow_array;
961
962# fill page count and first-previous-next-last-all bits
963##fixme - hardcoded group bit
964 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
965 fill_fpnla($count);
966
967# sort/order
968 $sortby= $webvar{sortby} if $webvar{sortby};
969 $sortorder = $webvar{order} if $webvar{order};
970
971# set up the headers
972 my @cols = ('domain', 'status', 'group');
973 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
974 fill_colheads(\@cols, \%colheads);
975
976# $page->param(sortorder => $sortorder);
977 # hack! hack! pthbttt. have to rethink the status column storage,
978 # or inactive comes "before" active. *sigh*
979 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
980
981 $page->param("start$webvar{startwith}" => 1) if $webvar{startwith} && $webvar{startwith} =~ /^[a-z]$/;
982
983 $page->param(filter => $webvar{filter}) if $webvar{filter};
984
985##fixme
986##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
987##fixme
988
989 $page->param(group => $curgroup);
990 my @domlist;
991 my $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
992 " INNER JOIN groups ON domains.group_id=groups.group_id".
993 " WHERE domains.group_id=?".
994##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
995 (defined($webvar{startwith}) ? " AND domain ~* '^[$webvar{startwith}]'" : '').
996 (defined($webvar{filter}) ? " AND domain ~* '$webvar{filter}'" : '').
997 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
998 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
999 $sth = $dbh->prepare($sql);
1000 $sth->execute($curgroup);
1001 my $rownum = 0;
1002 while (my @data = $sth->fetchrow_array) {
1003 my %row;
1004 $row{domainid} = $data[0];
1005 $row{domain} = $data[1];
1006 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1007 $row{group} = $data[3];
1008 $row{bg} = ($rownum++)%2;
1009# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
1010 $row{mkactive} = !$data[2];
1011 $row{sid} = $sid;
1012 $row{offset} = $offset;
1013##fixme: need to clean up status indicator/usage/inversion
1014 push @domlist, \%row;
1015 }
1016 $page->param(domtable => \@domlist);
1017} # end listdomains()
1018
1019
1020sub listgroups {
1021 my @childgroups;
1022 getChildren($dbh, $logingroup, \@childgroups, 'all');
1023 my $childlist = join(',',@childgroups);
1024
1025 my $sql = "SELECT count(*) FROM groups WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")";
1026 my $sth = $dbh->prepare($sql);
1027
1028 $sth->execute;
1029 my ($count) = ($sth->fetchrow_array);
1030# fill page count and first-previous-next-last-all bits
1031##fixme - hardcoded group bit
1032 fill_pgcount($count,"groups",'');
1033 fill_fpnla($count);
1034
1035 $sortby = 'group';
1036# sort/order
1037 $sortby = $webvar{sortby} if $webvar{sortby};
1038 $sortorder = $webvar{order} if $webvar{order};
1039
1040# set up the headers
1041 my @cols = ('group','parent','nusers','ndomains');
1042 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
1043 fill_colheads(\@cols, \%colnames);
1044
1045 my @grouplist;
1046 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
1047 "count(distinct(u.username)), count(distinct(d.domain)) ".
1048 "FROM groups g ".
1049 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1050 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1051 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
1052 "WHERE g.group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
1053 "GROUP BY g.group_id, g.group_name, g2.group_name ".
1054 "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1055 $sth->execute;
1056
1057 my $rownum = 0;
1058 while (my @data = $sth->fetchrow_array) {
1059 my %row;
1060 $row{groupid} = $data[0];
1061 $row{groupname} = $data[1];
1062 $row{pgroup} = $data[2];
1063 $row{nusers} = $data[3];
1064 $row{ndomains} = $data[4];
1065 $row{bg} = ($rownum++)%2;
1066 $row{sid} = $sid;
1067 push @grouplist, \%row;
1068 }
1069 $page->param(grouptable => \@grouplist);
1070} # end listgroups()
1071
1072
1073sub fill_grouplist {
1074 my $template_var = shift;
1075 my $cur = shift || $curgroup;
1076
1077 my @childgroups;
1078 getChildren($dbh, $logingroup, \@childgroups, 'all');
1079 my $childlist = join(',',@childgroups);
1080
1081 # weesa gonna discard parent_group_id for now
1082 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1083 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1084 "ORDER BY group_id");
1085 $sth->execute;
1086 my @grouplist;
1087 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
1088 my %row;
1089 $row{groupname} = $groupname;
1090 $row{groupval} = $groupid;
1091##fixme: need magic
1092# $row{defgroup} = '';
1093 $row{groupactive} = 1 if $groupid == $cur;
1094 push @grouplist, \%row;
1095 }
1096
1097 $page->param("$template_var" => \@grouplist);
1098
1099} # end fill_grouplist()
1100
1101
1102sub list_users {
1103 my $sth = $dbh->prepare("select count(*) from users where group_id=?");
1104 $sth->execute($curgroup);
1105 my ($count) = ($sth->fetchrow_array);
1106
1107# fill page count and first-previous-next-last-all bits
1108##fixme - hardcoded group bit
1109 fill_pgcount($count,"users",'');
1110 fill_fpnla($count);
1111
1112 $sortby = 'user';
1113# sort/order
1114 $sortby = $webvar{sortby} if $webvar{sortby};
1115 $sortorder = $webvar{order} if $webvar{order};
1116
1117# set up the headers
1118 my @cols = ('user','fname','type','group','status');
1119 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
1120 fill_colheads(\@cols, \%colnames);
1121
1122 my @userlist;
1123 $sth = $dbh->prepare("SELECT u.user_id, u.username, u.firstname, u.lastname, u.type, g.group_name, u.status ".
1124 "FROM users u ".
1125 "INNER JOIN groups g ON u.group_id=g.group_id ".
1126 "WHERE u.group_id=?".
1127 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
1128 $sth->execute($curgroup);
1129
1130 my $rownum = 0;
1131 while (my @data = $sth->fetchrow_array) {
1132 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
1133 my %row;
1134 $row{userid} = $data[0];
1135 $row{username} = $data[1];
1136 $row{userfull} = "$data[2] $data[3]";
1137 $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
1138 $row{usergroup} = $data[5];
1139 $row{mkactive} = $data[6];
1140 $row{bg} = ($rownum++)%2;
1141 $row{sid} = $sid;
1142 push @userlist, \%row;
1143 }
1144 $page->param(usertable => \@userlist);
1145}
1146
1147# Generate all of the glop necessary to add or not the appropriate marker/flag for
1148# the sort order and column in domain, user, group, and record lists
1149# Takes an array ref and hash ref
1150sub fill_colheads {
1151 my $cols = shift;
1152 my $colnames = shift;
1153
1154 my @headings;
1155
1156 foreach my $col (@$cols) {
1157 my %coldata;
1158 $coldata{firstcol} = 1 if $col eq $cols->[0];
1159 $coldata{sid} = $sid;
1160 $coldata{page} = $webvar{page};
1161 $coldata{offset} = $webvar{offset} if $webvar{offset};
1162 $coldata{sortby} = $col;
1163 $coldata{colname} = $colnames->{$col};
1164 if ($col eq $sortby) {
1165 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
1166 $coldata{sortorder} = $sortorder;
1167 } else {
1168 $coldata{order} = 'ASC';
1169 }
1170 push @headings, \%coldata;
1171 }
1172
1173 $page->param(colheads => \@headings);
1174
1175} # end gentableheadings
Note: See TracBrowser for help on using the repository browser.