source: trunk/dns.cgi@ 27

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

/trunk

checkpoint

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