source: trunk/dns.cgi@ 29

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

/trunk

DNSDB::initGlobals

  • don't roll over and die if the "systemwide defaults" file is missing

dns.cgi

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