source: trunk/dns.cgi@ 75

Last change on this file since 75 was 72, checked in by Kris Deugau, 14 years ago

/trunk

Checkpoint - adding sort-on-title-click to record list columns

  • HTML seems complete
  • Still need to store sort type, direction in session
  • Still need to pass sort info into record retrieval sub in DNSDB.pm
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 53.9 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2011-02-16 23:04:21 +0000 (Wed, 16 Feb 2011) $
6# SVN revision $Rev: 72 $
7# Last update by $Author: kdeugau $
8###
[3]9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
[2]10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
[29]18use Crypt::PasswdMD5;
[30]19use Net::DNS;
[2]20use DBI;
21
22use lib '.';
23# custom modules
24use DNSDB qw(:ALL);
25
[13]26my @debugbits; # temp, to be spit out near the end of processing
[61]27my $debugenv = 1;
[13]28
[2]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...
[7]39my %webvar = $q->Vars;
[2]40
[13]41# persistent stuff needed on most/all pages
[2]42my $sid = ($webvar{sid} ? $webvar{sid} : undef);
[68]43my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir})
44 or die CGI::Session->errstr();
[2]45#$sid = $session->id() if !$sid;
46if (!$sid) {
47 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
48 $sid = $session->id();
49# need to know the "upper" group the user can deal with; may as well
50# stick this in the session rather than calling out to the DB every time.
[18]51 $session->param('logingroup',1);
52 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
[51]53 $session->param('domlistsortby','domain');
54 $session->param('domlistorder','ASC');
[54]55 $session->param('useradminsortby','user');
[51]56 $session->param('useradminorder','ASC');
57 $session->param('grpmansortby','group');
58 $session->param('grpmanorder','ASC');
59 $session->param('reclistsortby','name');
60 $session->param('reclistorder','ASC');
[53]61# $session->param('filter','login');
62# $session->param('startwith','login');
63# $session->param('searchsubs','login');
[2]64}
65
[19]66my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
67my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
[17]68my $group = ($webvar{group} ? $webvar{group} : 1);
[18]69
[54]70# per-page startwith, filter, searchsubs
[64]71$session->param($webvar{page}.'startwith', $webvar{startwith}) if defined($webvar{startwith});
[62]72$session->param($webvar{page}.'filter', $webvar{filter}) if defined($webvar{filter});
[54]73$webvar{searchsubs} =~ s/^n ?// if $webvar{searchsubs};
[57]74$session->param($webvar{page}.'searchsubs', $webvar{searchsubs}) if defined($webvar{searchsubs});
[54]75
[68]76# decide which page to spit out...
77# also set $webvar{page} before we try to use it.
78$webvar{page} = 'login' if !$webvar{page};
79
[54]80my $startwith = $session->param($webvar{page}.'startwith');
81my $filter = $session->param($webvar{page}.'filter');
82my $searchsubs = $session->param($webvar{page}.'searchsubs');
83
[26]84# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
[2]85
86my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
87my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
88
89# default
[38]90#my $perpage = 15;
91my $perpage = 3;
[2]92my $offset = ($webvar{offset} ? $webvar{offset} : 0);
93
94# NB: these must match the field name and SQL ascend/descend syntax respectively
[41]95my $sortby = "domain";
96my $sortorder = "ASC";
[2]97
[68]98my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret","newdbhost");
[2]99#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
100# { AutoCommit => 0 }) or die $DBI::errstr;
101
102##fixme. PLEASE! <G>
103print $msg if !$dbh;
104
105# fiddle hardcoded "defaults" as per system/user (?) prefs
106initGlobals($dbh);
107
[26]108# handle login redirect
[30]109if ($webvar{action}) {
110 if ($webvar{action} eq 'login') {
[65]111 # Snag ACL/permissions here too
[30]112 my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
113 $sth->execute($webvar{username});
114 my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
115 $webvar{loginfailed} = 1 if !defined($uid);
[26]116
[30]117 if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
118 $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
119 } else {
120 $webvar{loginfailed} = 1 if $pass ne $webvar{password};
121 }
[29]122
[30]123 # set session bits
124 $session->param('logingroup',$gid);
125 $session->param('curgroup',$gid);
[65]126 $session->param('uid',$uid);
[30]127 $session->param('username',$webvar{username});
[26]128
[30]129 changepage(page => "domlist") if !defined($webvar{loginfailed});
130 } elsif ($webvar{action} eq 'logout') {
131 # delete the session
132 $session->delete();
133 $session->flush();
134
135 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}";
136 $newurl =~ s|/[^/]+$|/|;
137 print "Status: 302\nLocation: $newurl\n\n";
138 exit;
139
[57]140 } elsif ($webvar{action} eq 'chgroup') {
141 # fiddle session-stored group data
142 # magic incantation to... uhhh...
143 $session->param('curgroup', $webvar{group});
144 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
[30]145 }
[57]146} # handle global webvar{action}s
[26]147
[65]148initPermissions($dbh,$session->param('uid'));
[57]149
[15]150## Default page is a login page
151#my $page; # to be initialized as an HTML::Template entity sooner or later
[2]152
[3]153
[15]154#if (!$webvar{page}) {
155# $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
156#} else {
157#}
[2]158
[15]159my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
160
[2]161$page->param(sid => $sid);
162
[26]163if ($webvar{page} eq 'login') {
[3]164
[26]165 $page->param(loginfailed => 1) if $webvar{loginfailed};
166##fixme: set up session init to actually *check* for session timeout
167 $page->param(timeout => 1) if $webvar{sesstimeout};
168
169} elsif ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
170
[3]171# hmm. seeing problems in some possibly-not-so-corner cases.
[10]172# this currently only handles "domain on", "domain off"
[3]173 if (defined($webvar{action})) {
[62]174 my $stat = domStatus($dbh,$webvar{id},$webvar{action});
175 logaction($webvar{id}, $session->param("username"), parentID($webvar{id}, 'dom', 'group'),
176 "Changed ".domainName($dbh, $webvar{id})." state to ".($stat ? 'active' : 'inactive'));
[3]177 }
178
[18]179 $page->param(curpage => $webvar{page});
[62]180 if ($webvar{del_failed}) {
181 $page->param(del_failed => 1);
182 $page->param(errmsg => $webvar{errmsg});
183 }
[18]184
[11]185 listdomains();
[2]186
[4]187} elsif ($webvar{page} eq 'newdomain') {
[2]188
[57]189 # hmm. nothing to do here?
190 # - group list is filled by the same bit that fills the group list in the menu
[62]191 if ($webvar{add_failed}) {
192 $page->param(add_failed => 1);
193 $page->param(errmsg => $webvar{errmsg});
194 $page->param(domain => $webvar{domain});
195 }
[2]196
[57]197} elsif ($webvar{page} eq 'adddomain') {
198
199 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
200
201 if ($code eq 'OK') {
202 logaction($msg, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
203 changepage(page => "reclist", id => $msg);
204 } else {
[62]205 logaction(0, $session->param("username"), $webvar{group}, "Failed adding domain $webvar{domain} ($msg)");
[57]206 changepage(page => "newdomain", add_failed => 1, domain => $webvar{domain}, errmsg => $msg);
207 }
208
[11]209} elsif ($webvar{page} eq 'deldom') {
210
211 $page->param(id => $webvar{id});
212 # first pass = confirm y/n (sorta)
213 if (!defined($webvar{del})) {
214 $page->param(del_getconf => 1);
215 $page->param(domain => domainName($dbh,$webvar{id}));
216# print some neato things?
217
218# } else {
219# #whether actually deleting or cancelling we redirect to the domain list, default format
220
221 } elsif ($webvar{del} eq 'ok') {
[57]222 my $pargroup = parentID($webvar{id}, 'dom', 'group');
[61]223 my $dom = domainName($dbh, $webvar{id});
[11]224 my ($code,$msg) = delDomain($dbh, $webvar{id});
225 if ($code ne 'OK') {
226# need to find failure mode
[62]227 logaction($webvar{id}, $session->param("username"), $pargroup, "Failed to delete domain $dom ($msg)");
228 changepage(page => "domlist", del_failed => 1, errmsg => $msg);
[11]229 } else {
[61]230 logaction($webvar{id}, $session->param("username"), $pargroup, "Deleted domain $dom");
231 changepage(page => "domlist");
[11]232 }
233 } else {
234 # cancelled. whee!
235 changepage(page => "domlist");
236 }
237
[47]238} elsif ($webvar{page} eq 'reclist') {
239
240 # Handle record list for both default records (per-group) and live domain records
241
242 $page->param(defrec => $webvar{defrec});
243 $page->param(id => $webvar{id});
244 $page->param(curpage => $webvar{page});
245
246# select count(*) from (default_)?records where (group|domain)_id=?
247 my $sth = $dbh->prepare("SELECT count(*) FROM ".
248 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
249 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
250 "AND NOT type=$reverse_typemap{SOA}");
251 $sth->execute($webvar{id});
252 my ($count) = ($sth->fetchrow_array);
253
[72]254##work
255# set up the headers
256 my @cols = ('host', 'type', 'val', 'distance', 'weight', 'port', 'ttl');
257 my %colheads = (host => 'Name', type => 'Type', val => 'Address',
258 distance => 'Distance', weight => 'Weight', port => 'Port', ttl => 'TTL');
259 my %custom = (id => $webvar{id}, defrec => $webvar{defrec});
260 fill_colheads($sortby, $sortorder, \@cols, \%colheads, \%custom);
261
[47]262# fill the page-count and first-previous-next-last-all details
263 fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
264 fill_fpnla($count); # should put some params on this sub...
265
266 $page->param(defrec => $webvar{defrec});
267 if ($webvar{defrec} eq 'y') {
268##fixme: hardcoded group
269 showdomain('y',$curgroup);
270 } else {
271 showdomain('n',$webvar{id});
[60]272 $page->param(logdom => 1);
[47]273 }
274
[63]275 if ($webvar{del_failed}) {
276 $page->param(del_failed => 1);
277 $page->param(errmsg => $webvar{errmsg});
278 }
279
[13]280} elsif ($webvar{page} eq 'record') {
[16]281
[13]282 if ($webvar{recact} eq 'new') {
[16]283
[15]284 $page->param(todo => "Add record to");
285 $page->param(recact => "add");
[59]286 $page->param(parentid => $webvar{parentid});
287 $page->param(defrec => $webvar{defrec});
[16]288
[59]289 fill_recdata();
290
[15]291 } elsif ($webvar{recact} eq 'add') {
292
293 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
294 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
295 push @recargs, $webvar{distance};
296 if ($webvar{type} == $reverse_typemap{SRV}) {
297 push @recargs, $webvar{weight};
298 push @recargs, $webvar{port};
299 }
300 }
[59]301
[15]302 my ($code,$msg) = addRec(@recargs);
303
304 if ($code eq 'OK') {
[57]305 if ($webvar{defrec} eq 'y') {
306 logaction(0, $session->param("username"), $webvar{parentid},
[63]307 "Added default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
[57]308 } else {
309 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
[63]310 "Added record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
[57]311 }
[15]312 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
313 } else {
[24]314 $page->param(failed => 1);
315 $page->param(errmsg => $msg);
316 $page->param(wastrying => "adding");
317 $page->param(todo => "Add record to");
318 $page->param(recact => "add");
319 $page->param(parentid => $webvar{parentid});
320 $page->param(defrec => $webvar{defrec});
321 $page->param(id => $webvar{id});
[16]322 fill_recdata(); # populate the form... er, mostly.
[59]323 if ($webvar{defrec} eq 'y') {
324 logaction(0, $session->param("username"), $webvar{parentid},
[63]325 "Failed adding default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]326 } else {
327 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
[63]328 "Failed adding record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]329 }
[15]330 }
331
[13]332 } elsif ($webvar{recact} eq 'edit') {
[15]333
[16]334 $page->param(todo => "Update record");
335 $page->param(recact => "update");
336 $page->param(parentid => $webvar{parentid});
[17]337 $page->param(id => $webvar{id});
[16]338 $page->param(defrec => $webvar{defrec});
[57]339##fixme: SQL does not belong!
[13]340 my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
341 ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
342 $sth->execute($webvar{id});
343 my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
344 $page->param(name => $host);
345 $page->param(address => $val);
346 $page->param(distance => $distance);
347 $page->param(weight => $weight);
348 $page->param(port => $port);
349 $page->param(ttl => $ttl);
[16]350 fill_rectypes($type);
351
352 } elsif ($webvar{recact} eq 'update') {
353
354 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
355 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
356 $webvar{distance},$webvar{weight},$webvar{port});
357
358 if ($code eq 'OK') {
[55]359##fixme: need more magic to get proper group - if domain was fiddled
360# from search-subgroups listing, may not be "current" group
[57]361
362# SELECT d.group_id FROM domains d INNER JOIN records r ON d.domain_id=r.domain_id WHERE r.record_id=?
363# $sth->execute($webvar{id});
364##log
365 if ($webvar{defrec} eq 'y') {
366 logaction(0, $session->param("username"), $webvar{parentid},
367 "Updated default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
368 } else {
369 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{id}, 'rec', 'group'),
370 "Updated record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}");
371 }
[17]372 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[16]373 } else {
374 $page->param(failed => 1);
375 $page->param(errmsg => $msg);
376 $page->param(wastrying => "updating");
377 $page->param(todo => "Update record");
378 $page->param(recact => "update");
379 $page->param(parentid => $webvar{parentid});
380 $page->param(defrec => $webvar{defrec});
[17]381 $page->param(id => $webvar{id});
[16]382 fill_recdata();
[59]383 if ($webvar{defrec} eq 'y') {
384 logaction(0, $session->param("username"), $webvar{parentid},
[63]385 "Failed updating default record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]386 } else {
387 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
[63]388 "Failed updating record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]389 }
[16]390 }
[13]391 }
[16]392
[13]393 if ($webvar{defrec} eq 'y') {
[20]394 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
[13]395 } else {
[24]396 $page->param(parentid => $webvar{parentid});
397# $page->param(id => $webvar{id});
[16]398 $page->param(dohere => domainName($dbh,$webvar{parentid}));
[13]399 }
400
[2]401} elsif ($webvar{page} eq 'delrec') {
402
403 $page->param(id => $webvar{id});
404 $page->param(defrec => $webvar{defrec});
[39]405 $page->param(parentid => $webvar{parentid});
[2]406 # first pass = confirm y/n (sorta)
407 if (!defined($webvar{del})) {
408 $page->param(del_getconf => 1);
[3]409 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
410 $page->param(host => $rec{host});
411 $page->param(ftype => $typemap{$rec{type}});
412 $page->param(recval => $rec{val});
[39]413 } elsif ($webvar{del} eq 'ok') {
[62]414# get rec data before we try to delete it
415 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
[3]416 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
417 if ($code ne 'OK') {
418## need to find failure mode
[62]419 if ($webvar{defrec} eq 'y') {
420 logaction(0, $session->param("username"), $rec{parid},
[63]421 "Failed deleting default record '$rec{host} $typemap{$rec{type}} $rec{val}', TTL $rec{ttl} ($msg)");
[62]422 } else {
423 logaction($rec{parid}, $session->param("username"), parentID($rec{parid}, 'dom', 'group'),
[63]424 "Failed deleting record '$rec{host} $typemap{$rec{type}} $rec{val}', TTL $rec{ttl} ($msg)");
[62]425 }
[63]426changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, del_failed => 1, errmsg => $msg);
[3]427 $page->param(del_failed => 1);
428 $page->param(errmsg => $msg);
[39]429 showdomain($webvar{defrec}, $webvar{parentid});
430 } else {
[62]431 if ($webvar{defrec} eq 'y') {
432 logaction(0, $session->param("username"), $rec{parid},
433 "Deleted default record '$rec{host} $typemap{$rec{type}} $rec{val}', TTL $rec{ttl}");
434 } else {
435 logaction($rec{parid}, $session->param("username"), parentID($rec{parid}, 'dom', 'group'),
436 "Deleted record '$rec{host} $typemap{$rec{type}} $rec{val}', TTL $rec{ttl}");
437 }
[39]438 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[3]439 }
[39]440 } else {
441 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[2]442 }
443
444} elsif ($webvar{page} eq 'editsoa') {
445
[39]446 fillsoa($webvar{defrec},$webvar{id});
[2]447
448} elsif ($webvar{page} eq 'updatesoa') {
449
450 my $sth;
451 my $sql = '';
452 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
453 # plus a bit of magic to update the appropriate table
[39]454 $sql = "update ".($webvar{defrec} eq 'y' ? "default_records" : "records").
[2]455 " set host='$webvar{prins}:$webvar{contact}',".
456 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
457 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
458 $sth = $dbh->prepare($sql);
459 $sth->execute;
460
461 if ($sth->err) {
462 $page->param(update_failed => 1);
463 $page->param(msg => $DBI::errstr);
[39]464 fillsoa($webvar{defrec},$webvar{id});
[2]465 } else {
[57]466
467##fixme! need to set group ID properly here
468# SELECT group_id FROM domains WHERE domain_id=?
469# $sth->execute($webvar{id});
470##log
[55]471 logaction(0, $session->param("username"), $webvar{group},
472 "Updated SOA (ns $webvar{prins}, contact $webvar{contact}, refresh $webvar{refresh},".
473 " retry $webvar{retry}, expire $webvar{expire}, minTTL $webvar{minttl}, TTL $webvar{ttl}");
[39]474 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec});
[57]475# $page->param(update_failed => 0);
[39]476# showdomain('y',1);
[2]477 }
478
[17]479} elsif ($webvar{page} eq 'grpman') {
[2]480
[22]481 listgroups();
[18]482 $page->param(curpage => $webvar{page});
483
[17]484} elsif ($webvar{page} eq 'newgrp') {
[20]485
[18]486 # do.. uhh.. stuff.. if we have no webvar{action}
487 if ($webvar{action} && $webvar{action} eq 'add') {
488 # not gonna provide the 4th param: template-or-clone flag, just yet
[66]489 my %newperms;
490 foreach (@permtypes) {
491 $newperms{$_} = 0;
492 $newperms{$_} = 1 if $webvar{$_} eq 'on';
493 }
494 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup}, \%newperms);
[57]495 if ($code eq 'OK') {
[55]496 logaction(0, $session->param("username"), $webvar{pargroup}, "Added group $webvar{newgroup}");
497 changepage(page => "grpman");
498 }
[66]499 # no point in doing extra work
500 fill_permissions($page, \%newperms);
[18]501 $page->param(add_failed => 1);
502 $page->param(errmsg => $msg);
503 $page->param(newgroup => $webvar{newgroup});
[66]504 fill_grouplist('pargroup',$webvar{pargroup});
[19]505 } else {
506# $page->param
[66]507 fill_grouplist('pargroup',$curgroup);
508 # fill default permissions with immediate parent's current ones
509 my %parperms;
510 getPermissions($dbh, 'group', $curgroup, \%parperms);
511 fill_permissions($page, \%parperms);
[18]512 }
[20]513
[22]514} elsif ($webvar{page} eq 'delgrp') {
[20]515
516 $page->param(id => $webvar{id});
517 # first pass = confirm y/n (sorta)
518 if (!defined($webvar{del})) {
519 $page->param(del_getconf => 1);
[23]520# $page->param(groupname => groupName($dbh,$webvar{id}));
[20]521# print some neato things?
522
523# } else {
524# #whether actually deleting or cancelling we redirect to the group list, default format
525
526 } elsif ($webvar{del} eq 'ok') {
[57]527 my $deleteme = groupName($dbh,$webvar{id}); # get this before we delete it...
[20]528 my ($code,$msg) = delGroup($dbh, $webvar{id});
529 if ($code ne 'OK') {
530# need to find failure mode
531 $page->param(del_failed => 1);
532 $page->param(errmsg => $msg);
[22]533 $page->param(curpage => $webvar{page});
534 listgroups();
[20]535 } else {
[57]536##fixme: need to clean up log when deleting a major container
537 logaction(0, $session->param("username"), $webvar{curgroup}, "Deleted group $deleteme");
[20]538 # success. go back to the domain list, do not pass "GO"
539 changepage(page => "grpman");
540 }
541 } else {
542 # cancelled. whee!
543 changepage(page => "grpman");
544 }
[23]545 $page->param(delgroupname => groupName($dbh, $webvar{id}));
[24]546
[65]547} elsif ($webvar{page} eq 'edgroup') {
548
549 if ($webvar{action} eq 'updperms') {
550 # extra safety check; make sure user can't construct a URL to bypass ACLs
551 my %curperms;
552 getPermissions($dbh, 'group', $webvar{gid}, \%curperms);
[66]553 my %chperms;
554 foreach (@permtypes) {
[65]555 $webvar{$_} = 0 if !defined($webvar{$_});
556 $webvar{$_} = 1 if $webvar{$_} eq 'on';
[66]557 $chperms{$_} = $webvar{$_} if $curperms{$_} ne $webvar{$_};
[65]558 }
[66]559 my ($code,$msg) = changePermissions($dbh, 'group', $webvar{gid}, \%chperms);
560 if ($code eq 'OK') {
561 logaction(0, $session->param("username"), $webvar{gid}, "Changed default permissions in group $webvar{gid}");
562 changepage(page => "grpman");
563 }
564 # no point in doing extra work
565 fill_permissions($page, \%chperms);
566 $page->param(errmsg => $msg);
[65]567 }
568 $page->param(gid => $webvar{gid});
569 $page->param(grpmeddle => groupName($dbh, $webvar{gid}));
570 my %grpperms;
571 getPermissions($dbh, 'group', $webvar{gid}, \%grpperms);
[66]572 fill_permissions($page, \%grpperms);
[65]573
[24]574} elsif ($webvar{page} eq 'useradmin') {
575
[51]576 if (defined($webvar{action})) {
577 userStatus($dbh,$webvar{id},$webvar{action});
578 }
579
[24]580 $page->param(curpage => $webvar{page});
581
[51]582 list_users();
583
[67]584} elsif ($webvar{page} eq 'user') {
585
586 fill_actypelist();
587 fill_clonemelist();
588 my %grpperms;
589 getPermissions($dbh, 'group', $curgroup, \%grpperms);
590 fill_permissions($page, \%grpperms);
591 my $grppermlist = new HTML::Template(filename => "$templatedir/permlist.tmpl");
592 my %noaccess;
593 fill_permissions($grppermlist, \%grpperms, \%noaccess);
594 $grppermlist->param(info => 1);
595 $page->param(grpperms => $grppermlist->output);
596 $page->param(is_admin => $permissions{admin});
597
598# if ($webvar{action} eq 'new') {
599# } els
600 if ($webvar{action} eq 'add') {
601
602 my ($code,$msg);
603
604 my $alterperms = 0; # flag iff we need to force custom permissions due to user's current access limits
605
606 if ($webvar{pass1} ne $webvar{pass2}) {
607 $code = 'FAIL';
608 $msg = "Passwords don't match";
609 } else {
610# assemble a permission string - far simpler than trying to pass an
611# indeterminate set of permission flags individually
612
613# ooooh.
614# OOOOH.
615# We have to see if the user can add any particular permissions; otherwise we have a priviledge escalation. Whee.
616
617if (!$permissions{admin}) {
618 my %grpperms;
619 getPermissions($dbh, 'group', $curgroup, \%grpperms);
620 my $ret = comparePermissions(\%permissions, \%grpperms);
621 if ($ret ne '<' && $ret ne '!') {
622 # User's permissions are not a superset or equivalent to group. Can't inherit
623 # (and include access user doesn't currently have), so we force custom.
624 $webvar{perms_type} = 'custom';
625 $alterperms = 1;
626 }
627}
628##work
629 my $permstring;
630 if ($webvar{perms_type} eq 'custom') {
631 $permstring = 'C:';
632 foreach (@permtypes) {
633 if ($permissions{admin}) {
634 $permstring .= ",$_" if defined($webvar{$_}) && $webvar{$_} eq 'on';
635 } else {
636 $permstring .= ",$_" if $permissions{$_} && defined($webvar{$_}) && $webvar{$_} eq 'on';
637 }
638 }
639 $page->param(perm_custom => 1);
640 } elsif ($permissions{admin} && $webvar{perms_type} eq 'clone') {
641 $permstring = "c:$webvar{clonesrc}";
642 $page->param(perm_clone => 1);
643 } else {
644 $permstring = 'i';
645 }
646 ($code,$msg) = addUser($dbh,$webvar{uname}, $curgroup, $webvar{pass1},
647 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype}, $permstring,
648 $webvar{fname}, $webvar{lname}, $webvar{phone});
649 }
650
651# hokay, a bit of magic to decide which page we hit.
652 if ($code eq 'OK') {
653##log
654 logaction(0, $session->param("username"), $webvar{group},
655 "Added user $webvar{uname} ($webvar{fname} $webvar{lname})");
656 if ($alterperms) {
657 changepage(page => "useradmin", warnmsg =>
658 "You can only grant permissions you hold. $webvar{uname} added with reduced access.");
659 } else {
660 changepage(page => "useradmin");
661 }
662 } else {
663# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
664 $page->param(add_failed => 1);
665 $page->param(uname => $webvar{uname});
666 $page->param(fname => $webvar{fname});
667 $page->param(lname => $webvar{lname});
668 $page->param(pass1 => $webvar{pass1});
669 $page->param(pass2 => $webvar{pass2});
670 $page->param(errmsg => $msg);
671 fill_actypelist();
672 fill_clonemelist();
673 }
674
675 } elsif ($webvar{action} eq 'edit') {
676 } elsif ($webvar{action} eq 'update') {
677 } else {
678 # default is "new"
679 }
680
[24]681} elsif ($webvar{page} eq 'newuser') {
682
683 # foo?
684 fill_actypelist();
[65]685 fill_clonemelist();
[24]686
[67]687 my %grpperms;
688 getPermissions($dbh, 'group', $curgroup, \%grpperms);
689 fill_permissions($page, \%grpperms);
690
691 my $grppermlist = new HTML::Template(filename => "$templatedir/permlist.tmpl");
692 my %noaccess;
693 fill_permissions($grppermlist, \%grpperms, \%noaccess);
694 $grppermlist->param(info => 1);
695 $page->param(grpperms => $grppermlist->output);
696
[24]697} elsif ($webvar{page} eq 'adduser') {
698
699 my ($code,$msg);
700
701 if ($webvar{pass1} ne $webvar{pass2}) {
702 $code = 'FAIL';
703 $msg = "Passwords don't match";
704 } else {
[65]705# assemble a permission string - far simpler than trying to pass an
706# indeterminate set of permission flags individually
707my $permstring;
708if ($webvar{perms_type} eq 'custom') {
709 $permstring = 'C:,g:,u:,d:,r:';
710 $page->param(perm_custom => 1);
711} elsif ($webvar{perms_type} eq 'clone') {
712 $permstring = 'c:';
713 $page->param(perm_clone => 1);
714} else {
715 $permstring = 'i';
716# $page->param(perm_inherit => 1);
717}
[38]718 ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
[25]719 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
720 $webvar{fname}, $webvar{lname}, $webvar{phone});
[24]721 }
722
723# hokay, a bit of magic to decide which page we hit.
724 if ($code eq 'OK') {
[57]725##log
[55]726 logaction(0, $session->param("username"), $webvar{group},
727 "Added user $webvar{uname} ($webvar{fname} $webvar{lname})");
[24]728 changepage(page => "useradmin");
729 } else {
[38]730# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
[24]731 $page->param(add_failed => 1);
[38]732 $page->param(uname => $webvar{uname});
[24]733 $page->param(fname => $webvar{fname});
734 $page->param(lname => $webvar{lname});
735 $page->param(pass1 => $webvar{pass1});
736 $page->param(pass2 => $webvar{pass2});
737 $page->param(errmsg => $msg);
738 fill_actypelist();
[65]739 fill_clonemelist();
[24]740 }
741
[38]742# $page->param(add_failed => 1);
[25]743
744} elsif ($webvar{page} eq 'deluser') {
745
746 $page->param(id => $webvar{id});
747 # first pass = confirm y/n (sorta)
748 if (!defined($webvar{del})) {
749 $page->param(del_getconf => 1);
750 $page->param(user => userFullName($dbh,$webvar{id}));
751 } elsif ($webvar{del} eq 'ok') {
[57]752##fixme: find group id user is in (for logging) *before* we delete the user
[25]753 my ($code,$msg) = delUser($dbh, $webvar{id});
754 if ($code ne 'OK') {
755# need to find failure mode
756 $page->param(del_failed => 1);
757 $page->param(errmsg => $msg);
758 list_users($curgroup);
759 } else {
[65]760 # success. go back to the user list, do not pass "GO"
[57]761##log
762 logaction(0, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
[25]763 changepage(page => "useradmin");
764 }
765 } else {
766 # cancelled. whee!
767 changepage(page => "useradmin");
768 }
769
[65]770} elsif ($webvar{page} eq 'edituser') {
771
[30]772} elsif ($webvar{page} eq 'dnsq') {
773
774 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
[31]775 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
776 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]777 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
778
779 if ($webvar{qfor}) {
780 my $resolv = Net::DNS::Resolver->new;
[31]781 $resolv->tcp_timeout(5); # make me adjustable!
782 $resolv->udp_timeout(5); # make me adjustable!
783 $resolv->recurse(0) if $webvar{nrecurse};
784 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
[30]785 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
786 if ($query) {
787
788 $page->param(showresults => 1);
789
790 my @answer;
791 foreach my $rr ($query->answer) {
792# next unless $rr->type eq "A" or $rr->type eq 'NS';
793 my %row;
794 my ($host,$ttl,$class,$type,$data) =
[31]795 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]796 $row{host} = $host;
797 $row{ftype} = $type;
[31]798 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]799 push @answer, \%row;
800 }
801 $page->param(answer => \@answer);
802
803 my @additional;
804 foreach my $rr ($query->additional) {
805# next unless $rr->type eq "A" or $rr->type eq 'NS';
806 my %row;
807 my ($host,$ttl,$class,$type,$data) =
808 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
809 $row{host} = $host;
810 $row{ftype} = $type;
811 $row{rdata} = $data;
812 push @additional, \%row;
813 }
814 $page->param(additional => \@additional);
815
816 my @authority;
817 foreach my $rr ($query->authority) {
818# next unless $rr->type eq "A" or $rr->type eq 'NS';
819 my %row;
820 my ($host,$ttl,$class,$type,$data) =
821 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
822 $row{host} = $host;
823 $row{ftype} = $type;
824 $row{rdata} = $data;
825 push @authority, \%row;
826 }
827 $page->param(authority => \@authority);
828
829 $page->param(usedresolver => $resolv->answerfrom);
830 $page->param(frtype => $typemap{$webvar{type}});
831
832 } else {
833 $page->param(errmsg => $resolv->errorstring);
834 }
835 }
836 ## done DNS query
837
[31]838} elsif ($webvar{page} eq 'axfr') {
839
840 # don't need this while we've got the dropdown in the menu. hmm.
841 #fill_grouplist;
842
843 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
844 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
845 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
[37]846 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]847 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
[33]848
849##fixme: check group too?
850 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
851 $page->param(errmsg => "Need to set host to import from");
852 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
853 $page->param(errmsg => "Need domains to import");
854 } else {
855 my @domlist = split /\s+/, $webvar{importdoms};
856 my @results;
857my $rnum = 0;
858 foreach my $domain (@domlist) {
[34]859 my %row;
860 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
861 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
[35]862 $row{domok} = $msg if $code eq 'OK';
863 if ($code eq 'WARN') {
864 $msg =~ s|\n|<br />|g;
865 $row{domwarn} = $msg;
866 }
[37]867 if ($code eq 'FAIL') {
868 $msg =~ s|\n|<br />|g;
869 $row{domerr} = $msg;
870 }
[33]871 # do stuff! DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
872 $row{domain} = $domain;
873# $row{row} = $rnum++;
874 push @results, \%row;
875 }
876 $page->param(axfrresults => \@results);
877 }
878
[48]879} elsif ($webvar{page} eq 'whoisq') {
[47]880
[48]881 if ($webvar{qfor}) {
882 use Net::Whois::Raw;
883 use Text::Wrap;
884
885# caching useful?
886#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
887#$Net::Whois::Raw::CACHE_TIME = 60;
888
889 my ($dominfo, $whois_server) = whois($webvar{qfor});
890##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
891
892 # le sigh. idjits spit out data without linefeeds...
893 $Text::Wrap::columns = 88;
894
895# &%$@%@# high-bit crap. We should probably find a way to properly recode these instead of one-by-one.
896# Mainly an XHTML validation thing.
897 $dominfo =~ s/\xa9/\&copy;/g;
898 $dominfo =~ s/\xae/\&reg;/g;
899
900 $page->param(qfor => $webvar{qfor});
901 $page->param(dominfo => wrap('','',$dominfo));
902 $page->param(whois_server => $whois_server);
903 } else {
904 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
905 }
906
[47]907} elsif ($webvar{page} eq 'log') {
908
909##fixme put in some real log-munching stuff
910##fixme need to add bits to *create* log entries...
[59]911 my $sql = "SELECT user_id, email, name, entry, date_trunc('second',stamp) FROM log WHERE ";
[60]912 my $id = $curgroup; # we do this because the group log may be called from (almost) any page,
913 # but the others are much more limited. this is probably non-optimal.
[61]914 if ($webvar{ltype} && $webvar{ltype} eq 'user') {
[60]915 $sql .= "user_id=?";
916 $id = $webvar{id};
917 $page->param(logfor => 'user '.userFullName($dbh,$id));
918 } elsif ($webvar{ltype} && $webvar{ltype} eq 'dom') {
[59]919 $sql .= "domain_id=?";
920 $id = $webvar{id};
[60]921 $page->param(logfor => 'domain '.domainName($dbh,$id));
[59]922 } else {
923 # Default to listing curgroup log
924 $sql .= "group_id=?";
[60]925 $page->param(logfor => 'group '.groupName($dbh,$id));
[59]926 }
927 my $sth = $dbh->prepare($sql);
928 $sth->execute($id);
[47]929 my @logbits;
[59]930 while (my ($uid, $email, $name, $entry, $stamp) = $sth->fetchrow_array) {
[47]931 my %row;
[59]932 $row{userfname} = $name;
933 $row{userid} = $uid;
934 $row{useremail} = $email;
935 $row{logentry} = $entry;
936 ($row{logtime}) = ($stamp =~ /^(.+)-\d\d$/);
[47]937 push @logbits, \%row;
938 }
939 $page->param(logentries => \@logbits);
940
[60]941} # end $webvar{page} dance
[2]942
943
[17]944# start output here so we can redirect pages.
[7]945print "Content-type: text/html\n\n", $header->output;
946
[20]947##common bits
[17]948if ($webvar{page} ne 'login') {
[30]949 $page->param(username => $session->param("username"));
950
[20]951 $page->param(group => $curgroup);
952 $page->param(groupname => groupName($dbh,$curgroup));
[43]953 $page->param(logingrp => groupName($dbh,$logingroup));
[20]954
[24]955 # group tree. should go elsewhere, probably
956 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
957 $page->param(grptree => $tmpgrplist);
[65]958 $page->param(subs => ($tmpgrplist ? 1 : 0)); # probably not useful to pass gobs of data in for a boolean
[42]959 $page->param(inlogingrp => $curgroup == $logingroup);
960
[20]961 # stuff for menu group change. nb: this is icky.
962 fill_grouplist("grouplist");
[53]963
964## set up "URL to self"
[38]965# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
966 my $tmp_ruri = $ENV{REQUEST_URI};
967 $tmp_ruri =~ s/\&([a-z])/\&amp\;$1/g;
[53]968
[39]969# le sigh. and we need to strip any previous action
970 $tmp_ruri =~ s/\&amp;action=[^&]+//g;
[52]971
[53]972# and search filter options. these get stored in the session, but discarded
973# as soon as you switch to a different page.
974##fixme: think about retaining these on a per-page basis, as well as offset; same as the sort-order bits
975 no warnings qw(uninitialized);
976 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]*(\&)?/$1/g;
977 $tmp_ruri =~ s/\&amp;searchsubs=[a-z09-]*(\&)?/$1/g;
978 $tmp_ruri =~ s/\&amp;filter=[a-z09-]*(\&)?/$1/g;
979 use warnings qw(uninitialized);
[52]980
[53]981# fill in the URL-to-self
[38]982 $page->param(whereami => $tmp_ruri);
[17]983}
[13]984
[24]985foreach (@debugbits) { print; }
986
[2]987# spit it out
988print $page->output;
989
[38]990if ($debugenv) {
991 print "<div id=\"debug\">webvar keys: <pre>\n";
992 foreach my $key (keys %webvar) {
993 print "key: $key\tval: $webvar{$key}\n";
994 }
995 print "</pre>\nsession:\n<pre>\n";
996 my $sesdata = $session->dataref();
997 foreach my $key (keys %$sesdata) {
998 print "key: $key\tval: ".$sesdata->{$key}."\n";
999 }
1000 print "</pre>\nENV:\n<pre>\n";
1001 foreach my $key (keys %ENV) {
1002 print "key: $key\tval: $ENV{$key}\n";
1003 }
1004 print "</pre></div>\n";
[2]1005}
1006
1007print $footer->output;
1008
[18]1009# as per the docs, Just In Case
1010$session->flush();
[2]1011
1012exit 0;
1013
1014
[24]1015sub fill_grptree {
1016 my $root = shift;
1017 my $cur = shift;
[69]1018 my $indent = shift || ' ';
[24]1019
1020 my @childlist;
1021
1022 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
1023 getChildren($dbh,$root,\@childlist,'immediate');
1024 return if $#childlist == -1;
1025 my @grouplist;
1026 foreach (@childlist) {
1027 my %row;
1028 $row{grpname} = groupName($dbh,$_);
[69]1029 # for all that HTML::Template is supposed to keep the HTML out of the Perl, this is so much more compact...
1030 $row{grpdisp} = ($_ == $cur ? "<b>$row{grpname}</b>" : $row{grpname});
1031 $row{subs} = fill_grptree($_,$cur,$indent.' ');
1032 $row{indent} = $indent;
[24]1033 push @grouplist, \%row;
1034 }
[69]1035 $grptree->param(indent => $indent);
[24]1036 $grptree->param(treelvl => \@grouplist);
1037 return $grptree->output;
1038}
1039
[11]1040sub changepage {
1041 my %params = @_; # think this works the way I want...
1042
1043 # handle user check
1044 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
1045 foreach (keys %params) {
1046 $newurl .= "&$_=$params{$_}";
1047 }
1048
[30]1049 # Just In Case
1050 $session->flush();
1051
[11]1052 print "Status: 302\nLocation: $newurl\n\n";
1053 exit;
1054} # end changepage
1055
[2]1056sub fillsoa {
1057 my $def = shift;
1058 my $id = shift;
[39]1059 my $domname = ($def eq 'y' ? '' : "DOMAIN");
[2]1060
[39]1061 $page->param(defrec => $def);
[2]1062
[39]1063# i had a good reason to do this when I wrote it...
1064# $page->param(domain => $domname);
1065# $page->param(group => $DNSDB::group);
1066 $page->param(isgrp => 1) if $def eq 'y';
1067 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
[2]1068
1069# defaults
[17]1070 $page->param(defcontact => $DNSDB::def{contact});
1071 $page->param(defns => $DNSDB::def{prins});
1072 $page->param(defsoattl => $DNSDB::def{soattl});
1073 $page->param(defrefresh => $DNSDB::def{refresh});
1074 $page->param(defretry => $DNSDB::def{retry});
1075 $page->param(defexpire => $DNSDB::def{expire});
1076 $page->param(defminttl => $DNSDB::def{minttl});
[2]1077
1078 # there are probably better ways to do this. TMTOWTDI.
1079 my %soa = getSOA($dbh,$def,$id);
1080
[39]1081 $page->param(id => $id);
[2]1082 $page->param(recid => $soa{recid});
1083 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
1084 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
1085 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
1086 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
1087 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
1088 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
1089 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
1090}
1091
1092sub showdomain {
1093 my $def = shift;
1094 my $id = shift;
1095
1096 # get the SOA first
1097 my %soa = getSOA($dbh,$def,$id);
1098
1099 $page->param(recid => $soa{recid});
1100 $page->param(contact => $soa{contact});
1101 $page->param(prins => $soa{prins});
1102 $page->param(refresh => $soa{refresh});
1103 $page->param(retry => $soa{retry});
1104 $page->param(expire => $soa{expire});
1105 $page->param(minttl => $soa{minttl});
1106 $page->param(ttl => $soa{ttl});
1107
[72]1108 $startwith = $session->param($webvar{page}.'startwith');
1109 $filter = $session->param($webvar{page}.'filter');
1110
[2]1111# my @foo2 = getDomRecs($dbh,'def',1);
[4]1112 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]1113
1114 my $row = 0;
1115 foreach my $rec (@$foo2) {
1116 $rec->{type} = $typemap{$rec->{type}};
1117 $rec->{row} = $row % 2;
[62]1118 $rec->{defrec} = $def;
[2]1119 $rec->{sid} = $webvar{sid};
[13]1120 $rec->{id} = $id;
[23]1121 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
1122 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
1123 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]1124 $row++;
1125 }
1126 $page->param(reclist => $foo2);
1127}
1128
[16]1129# fill in record type list on add/update/edit record template
1130sub fill_rectypes {
[13]1131 my $type = shift || $reverse_typemap{A};
[31]1132 my $soaflag = shift || 0;
[13]1133
[17]1134 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]1135 $sth->execute;
1136 my @typelist;
1137 while (my ($rval,$rname) = $sth->fetchrow_array()) {
1138 my %row = ( recval => $rval, recname => $rname );
[13]1139 $row{tselect} = 1 if $rval == $type;
[2]1140 push @typelist, \%row;
1141 }
[31]1142 if ($soaflag) {
1143 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
1144 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
1145 push @typelist, \%row;
1146 }
[2]1147 $page->param(typelist => \@typelist);
[31]1148} # fill_rectypes
[16]1149
1150sub fill_recdata {
1151 fill_rectypes($webvar{type});
1152
1153 $page->param(name => $webvar{name});
1154 $page->param(address => $webvar{address});
1155 $page->param(distance => $webvar{distance})
1156 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
1157 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
1158 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[2]1159 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
1160}
[7]1161
[24]1162sub fill_actypelist {
1163 my @actypes;
1164
1165 my %row1 = (actypeval => 'u', actypename => 'user');
1166 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
1167 push @actypes, \%row1;
1168
1169 my %row2 = (actypeval => 'S', actypename => 'superuser');
1170 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
1171 push @actypes, \%row2;
1172
1173 $page->param(actypelist => \@actypes);
1174}
1175
[65]1176sub fill_clonemelist {
1177 my $sth = $dbh->prepare("SELECT username,user_id FROM users WHERE group_id=$curgroup");
1178 $sth->execute;
1179
1180 my @clonesrc;
1181 while (my ($username,$uid) = $sth->fetchrow_array) {
1182 my %row = (
1183 username => $username,
1184 uid => $uid,
1185 selected => ($webvar{clonesrc} == $uid ? 1 : 0)
1186 );
1187 push @clonesrc, \%row;
1188 }
1189 $page->param(clonesrc => \@clonesrc);
1190}
1191
[7]1192sub fill_fpnla {
1193 my $count = shift;
1194##fixme
1195 if ($offset eq 'all') {
[70]1196 $page->param(perpage => $perpage);
[41]1197# uhm....
[7]1198 } else {
1199 # all these bits only have sensible behaviour if offset is numeric. err, probably.
1200 if ($count > $perpage) {
1201 # if there are more results than the default, always show the "all" link
1202 $page->param(navall => 1);
1203
1204 if ($offset > 0) {
1205 $page->param(navfirst => 1);
1206 $page->param(navprev => 1);
1207 $page->param(prevoffs => $offset-1);
1208 }
1209
1210 # show "next" and "last" links if we're not on the last page of results
1211 if ( (($offset+1) * $perpage - $count) < 0 ) {
1212 $page->param(navnext => 1);
1213 $page->param(nextoffs => $offset+1);
1214 $page->param(navlast => 1);
[8]1215 $page->param(lastoffs => int (($count-1)/$perpage));
[7]1216 }
1217 }
1218 }
[10]1219} # end fill_fpnla()
1220
[12]1221sub fill_pgcount {
1222 my $pgcount = shift;
1223 my $pgtype = shift;
1224 my $parent = shift;
1225
1226 $page->param(ntot => $pgcount);
1227 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
1228 $page->param(npglast => ($offset eq 'all' ? $pgcount :
1229 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
1230 ));
1231 $page->param(pgtype => $pgtype);
1232 $page->param(parent => $parent);
1233} # end fill_pgcount()
1234
[11]1235sub listdomains {
[41]1236
[62]1237 $startwith = $session->param($webvar{page}.'startwith');
1238 $filter = $session->param($webvar{page}.'filter');
1239 $searchsubs = $session->param($webvar{page}.'searchsubs');
1240
[53]1241##fixme: $logingroup or $curgroup?
[52]1242 my @childgroups;
[61]1243 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1244 my $childlist = join(',',@childgroups);
1245
[57]1246 my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[53]1247 ($startwith ? " AND domain ~* '^[$startwith]'" : '').
1248 ($filter ? " AND domain ~* '$filter'" : '');
[52]1249 my $sth = $dbh->prepare($sql);
1250 $sth->execute;
[17]1251 my ($count) = $sth->fetchrow_array;
1252
[12]1253# fill page count and first-previous-next-last-all bits
1254##fixme - hardcoded group bit
[20]1255 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]1256 fill_fpnla($count);
1257
[41]1258# sort/order
[51]1259 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1260 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]1261
[51]1262 $sortby = $session->param($webvar{page}.'sortby');
1263 $sortorder = $session->param($webvar{page}.'order');
1264
[44]1265# set up the headers
1266 my @cols = ('domain', 'status', 'group');
1267 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
[54]1268 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
[41]1269
[47]1270# $page->param(sortorder => $sortorder);
[41]1271 # hack! hack! pthbttt. have to rethink the status column storage,
1272 # or inactive comes "before" active. *sigh*
1273 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
1274
[51]1275# waffle, waffle - keep state on these as well as sortby, sortorder?
[53]1276 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[41]1277
[53]1278 $page->param(filter => $filter) if $filter;
1279 $page->param(searchsubs => $searchsubs) if $searchsubs;
[41]1280
1281##fixme
1282##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
1283##fixme
1284
[20]1285 $page->param(group => $curgroup);
[10]1286 my @domlist;
[52]1287 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
[20]1288 " INNER JOIN groups ON domains.group_id=groups.group_id".
[57]1289 " WHERE domains.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[41]1290##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1291 ($startwith ? " AND domain ~* '^[$startwith]'" : '').
1292 ($filter ? " AND domain ~* '$filter'" : '').
[41]1293 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
1294 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1295 $sth = $dbh->prepare($sql);
[52]1296 $sth->execute;
[10]1297 my $rownum = 0;
1298 while (my @data = $sth->fetchrow_array) {
1299 my %row;
1300 $row{domainid} = $data[0];
1301 $row{domain} = $data[1];
1302 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1303 $row{group} = $data[3];
1304 $row{bg} = ($rownum++)%2;
1305# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
1306 $row{mkactive} = !$data[2];
1307 $row{sid} = $sid;
1308 $row{offset} = $offset;
1309##fixme: need to clean up status indicator/usage/inversion
1310 push @domlist, \%row;
1311 }
1312 $page->param(domtable => \@domlist);
[11]1313} # end listdomains()
[18]1314
[22]1315sub listgroups {
[53]1316
[26]1317 my @childgroups;
[53]1318 getChildren($dbh, $logingroup, \@childgroups, 'all') if $searchsubs;
[26]1319 my $childlist = join(',',@childgroups);
1320
[54]1321 my $sql = "SELECT count(*) FROM groups WHERE parent_group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
[53]1322 ($startwith ? " AND group_name ~* '^[$startwith]'" : '').
1323 ($filter ? " AND group_name ~* '$filter'" : '');
[26]1324 my $sth = $dbh->prepare($sql);
1325
[22]1326 $sth->execute;
1327 my ($count) = ($sth->fetchrow_array);
1328# fill page count and first-previous-next-last-all bits
1329##fixme - hardcoded group bit
1330 fill_pgcount($count,"groups",'');
1331 fill_fpnla($count);
1332
[51]1333# $sortby = 'group';
[42]1334# sort/order
[51]1335 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1336 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]1337
[51]1338 $sortby = $session->param($webvar{page}.'sortby');
1339 $sortorder = $session->param($webvar{page}.'order');
1340
[44]1341# set up the headers
1342 my @cols = ('group','parent','nusers','ndomains');
1343 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
[54]1344 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[42]1345
[51]1346# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1347 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1348
[53]1349 $page->param(filter => $filter) if $filter;
1350 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1351
1352# munge sortby for columns in database
1353 $sortby = 'g.group_name' if $sortby eq 'group';
1354 $sortby = 'g2.group_name' if $sortby eq 'parent';
1355
[22]1356 my @grouplist;
1357 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[51]1358 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
[22]1359 "FROM groups g ".
1360 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1361 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1362 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[54]1363 "WHERE g.parent_group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[51]1364##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1365 ($startwith ? " AND g.group_name ~* '^[$startwith]'" : '').
1366 ($filter ? " AND g.group_name ~* '$filter'" : '').
[51]1367 " GROUP BY g.group_id, g.group_name, g2.group_name ".
1368 " ORDER BY $sortby $sortorder ".
1369 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
[22]1370 $sth->execute;
1371
1372 my $rownum = 0;
1373 while (my @data = $sth->fetchrow_array) {
1374 my %row;
1375 $row{groupid} = $data[0];
1376 $row{groupname} = $data[1];
1377 $row{pgroup} = $data[2];
1378 $row{nusers} = $data[3];
1379 $row{ndomains} = $data[4];
1380 $row{bg} = ($rownum++)%2;
1381 $row{sid} = $sid;
1382 push @grouplist, \%row;
1383 }
1384 $page->param(grouptable => \@grouplist);
1385} # end listgroups()
1386
[20]1387sub fill_grouplist {
[19]1388 my $template_var = shift;
1389 my $cur = shift || $curgroup;
[26]1390
1391 my @childgroups;
1392 getChildren($dbh, $logingroup, \@childgroups, 'all');
1393 my $childlist = join(',',@childgroups);
1394
[18]1395 # weesa gonna discard parent_group_id for now
[26]1396 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1397 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1398 "ORDER BY group_id");
[18]1399 $sth->execute;
[20]1400 my @grouplist;
1401 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]1402 my %row;
[20]1403 $row{groupname} = $groupname;
1404 $row{groupval} = $groupid;
[18]1405##fixme: need magic
[20]1406# $row{defgroup} = '';
1407 $row{groupactive} = 1 if $groupid == $cur;
1408 push @grouplist, \%row;
[18]1409 }
1410
[20]1411 $page->param("$template_var" => \@grouplist);
[18]1412
[24]1413} # end fill_grouplist()
1414
1415sub list_users {
[52]1416
1417 my @childgroups;
[53]1418 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1419 my $childlist = join(',',@childgroups);
1420
1421 my $sql = "SELECT count(*) FROM users WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[53]1422 ($startwith ? " AND username ~* '^[$startwith]'" : '').
1423 ($filter ? " AND username ~* '$filter'" : '');
[52]1424 my $sth = $dbh->prepare($sql);
1425 $sth->execute;
[24]1426 my ($count) = ($sth->fetchrow_array);
1427
1428# fill page count and first-previous-next-last-all bits
1429##fixme - hardcoded group bit
1430 fill_pgcount($count,"users",'');
1431 fill_fpnla($count);
1432
[51]1433# $sortby = 'user';
[44]1434# sort/order
[51]1435 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1436 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]1437
[51]1438 $sortby = $session->param($webvar{page}.'sortby');
1439 $sortorder = $session->param($webvar{page}.'order');
1440
[44]1441# set up the headers
1442 my @cols = ('user','fname','type','group','status');
1443 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
[54]1444 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[44]1445
[51]1446# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1447 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1448
[53]1449 $page->param(filter => $filter) if $filter;
1450 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1451
1452# munge sortby for columns in database
1453 $sortby = 'u.username' if $sortby eq 'user';
1454 $sortby = 'u.type' if $sortby eq 'type';
1455 $sortby = 'g.group_name' if $sortby eq 'group';
1456 $sortby = 'u.status' if $sortby eq 'status';
1457
[24]1458 my @userlist;
[52]1459 $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
[24]1460 "FROM users u ".
1461 "INNER JOIN groups g ON u.group_id=g.group_id ".
[52]1462 "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[51]1463##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1464 ($startwith ? " AND u.username ~* '^[$startwith]'" : '').
1465 ($filter ? " AND u.username ~* '$filter'" : '').
[51]1466 " ORDER BY $sortby $sortorder ".
[52]1467 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
[51]1468
[52]1469 $sth = $dbh->prepare($sql);
1470 $sth->execute;
[24]1471
1472 my $rownum = 0;
1473 while (my @data = $sth->fetchrow_array) {
[41]1474 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
[24]1475 my %row;
1476 $row{userid} = $data[0];
1477 $row{username} = $data[1];
[51]1478 $row{userfull} = $data[2];
1479 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
1480 $row{usergroup} = $data[4];
1481 $row{active} = $data[5];
[24]1482 $row{bg} = ($rownum++)%2;
1483 $row{sid} = $sid;
1484 push @userlist, \%row;
1485 }
1486 $page->param(usertable => \@userlist);
[55]1487} # end list_users()
[43]1488
1489# Generate all of the glop necessary to add or not the appropriate marker/flag for
1490# the sort order and column in domain, user, group, and record lists
1491# Takes an array ref and hash ref
1492sub fill_colheads {
[54]1493 my $sortby = shift;
1494 my $sortorder = shift;
[43]1495 my $cols = shift;
1496 my $colnames = shift;
[72]1497 my $custom = shift;
[43]1498
1499 my @headings;
1500
1501 foreach my $col (@$cols) {
1502 my %coldata;
1503 $coldata{firstcol} = 1 if $col eq $cols->[0];
1504 $coldata{sid} = $sid;
1505 $coldata{page} = $webvar{page};
1506 $coldata{offset} = $webvar{offset} if $webvar{offset};
1507 $coldata{sortby} = $col;
1508 $coldata{colname} = $colnames->{$col};
1509 if ($col eq $sortby) {
1510 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
1511 $coldata{sortorder} = $sortorder;
1512 } else {
1513 $coldata{order} = 'ASC';
1514 }
[72]1515 if ($custom) {
1516 foreach my $ckey (keys %$custom) {
1517 $coldata{$ckey} = $custom->{$ckey};
1518 }
1519 }
[43]1520 push @headings, \%coldata;
1521 }
1522
1523 $page->param(colheads => \@headings);
1524
[54]1525} # end fill_colheads()
[55]1526
1527sub logaction {
[59]1528 my $domid = shift;
1529 my $username = shift;
1530 my $groupid = shift;
1531 my $entry = shift;
[55]1532
[59]1533##fixme: ooohhh, nasty. what if we get a failure *here*? PTHBTT!
1534 my $sth = $dbh->prepare("SELECT user_id, firstname || ' ' || lastname FROM users WHERE username=?");
[55]1535 $sth->execute($username);
1536 my ($user_id, $fullname) = $sth->fetchrow_array;
1537
1538 $sth = $dbh->prepare("INSERT INTO log (domain_id,user_id,group_id,email,name,entry) ".
1539 "VALUES (?,?,?,?,?,?)");
[57]1540 $sth->execute($domid,$user_id,$groupid,$username,$fullname,$entry);
[55]1541} # end logaction()
[57]1542
[59]1543##fixme: generalize to return appropriate id on all cases (ie, use $partype)
[57]1544sub parentID {
1545 my $id = shift;
1546 my $idtype = shift;
1547 my $partype = shift;
1548 my $defrec = shift || '';
1549
1550 my $sql = '';
1551
1552 if ($idtype eq 'dom') {
[59]1553 return $id if $defrec eq 'y'; # "domain" + default records, we're really looking at a group.
[57]1554 $sql = "SELECT group_id FROM domains WHERE domain_id=?";
1555 } elsif ($idtype eq 'rec') {
[59]1556 if ($defrec eq 'y') {
1557 $sql = "SELECT group_id FROM default_records WHERE record_id=?";
[57]1558 } else {
1559 return
1560 $sql = "SELECT d.group_id FROM domains d".
1561 " INNER JOIN records r ON d.domain_id=r.domain_id".
1562 " WHERE r.record_id=?";
1563 }
1564 } elsif ($idtype eq 'group') {
1565 $sql = "SELECT parent_group_id FROM groups WHERE group_id=?";
1566 } elsif ($idtype eq 'user') {
1567 $sql = "SELECT group_id FROM users WHERE user_id=?";
1568 } else {
1569 return "FOO", "BAR"; # can't get here.... we think.
1570 }
[59]1571 my $sth = $dbh->prepare($sql);
1572 $sth->execute($id);
1573 my ($retid) = $sth->fetchrow_array;
1574 return $retid if $retid;
1575 # ahh! fall of the edge of the world if things went sideways
1576 ##fixme: really need to do a little more error handling, I think
[64]1577} # end parentID()
[66]1578
1579# we have to do this in a variety of places; let's make it consistent
1580sub fill_permissions {
1581 my $template = shift; # may need to do several sets on a single page
1582 my $permset = shift; # hashref to permissions on object
[67]1583 my $usercan = shift || \%permissions; # allow alternate user-is-allowed permission block
[66]1584
1585 foreach (@permtypes) {
[67]1586 $template->param("may_$_" => ($usercan->{admin} || $usercan->{$_}));
[66]1587 $template->param($_ => $permset->{$_});
1588 }
1589}
Note: See TracBrowser for help on using the repository browser.