source: trunk/dns.cgi@ 176

Last change on this file since 176 was 176, checked in by Kris Deugau, 13 years ago

/trunk

Remove some more stale commented code
Remove redundant call to initialze $searchsubs
Security review (see #30)

  • set $webvar{page} a little earlier so we don't clutter the session with unusable data
  • tweak initialization of $searchsubs. Improved but will still behave a bit strangely if extra data is deliberately or accidentally added to $webvar{searchsubs} (see #31)
  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 79.3 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2011-12-01 19:58:18 +0000 (Thu, 01 Dec 2011) $
6# SVN revision $Rev: 176 $
7# Last update by $Author: kdeugau $
8###
[87]9# Copyright (C) 2008-2011 - 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;
[92]19use Digest::MD5 qw(md5_hex);
[30]20use Net::DNS;
[2]21use DBI;
[83]22use Data::Dumper;
[2]23
[95]24#sub is_tainted {
25# # from perldoc perlsec
26# return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
27#}
28#use Cwd 'abs_path';
29#use File::Basename;
30#use lib dirname( abs_path $0 );
31#die "argh! tainted!" if is_tainted($0);
32#die "argh! \@INC got tainted!" if is_tainted(@INC);
33
34# custom modules
[2]35use lib '.';
36use DNSDB qw(:ALL);
37
[13]38my @debugbits; # temp, to be spit out near the end of processing
[160]39my $debugenv = 0;
[13]40
[2]41# Let's do these templates right...
42my $templatedir = "templates";
43my $sessiondir = "session";
44
45# Set up the CGI object...
46my $q = new CGI::Simple;
47# ... and get query-string params as well as POST params if necessary
48$q->parse_query_string;
49
50# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
[7]51my %webvar = $q->Vars;
[2]52
[168]53# shut up some warnings, in case we arrive somewhere we forgot to set this
54$webvar{defrec} = 'n' if !$webvar{defrec};
55
[163]56# load some local system defaults (mainly DB connect info)
57# note this is not *absolutely* fatal, since there's a default dbname/user/pass in DNSDB.pm
58# we'll catch a bad DB connect string once we get to trying that
59##fixme: pass params to loadConfig, and use them there, to allow one codebase to support multiple sites
60if (!loadConfig()) {
61 warn "Using default configuration; unable to load custom settings: $DNSDB::errstr";
62}
63
[13]64# persistent stuff needed on most/all pages
[2]65my $sid = ($webvar{sid} ? $webvar{sid} : undef);
[68]66my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir})
67 or die CGI::Session->errstr();
[2]68#$sid = $session->id() if !$sid;
69if (!$sid) {
70 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
71 $sid = $session->id();
[163]72 $session->expire($config{timeout});
[2]73# need to know the "upper" group the user can deal with; may as well
74# stick this in the session rather than calling out to the DB every time.
[18]75 $session->param('logingroup',1);
76 $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
[51]77 $session->param('domlistsortby','domain');
78 $session->param('domlistorder','ASC');
[54]79 $session->param('useradminsortby','user');
[51]80 $session->param('useradminorder','ASC');
81 $session->param('grpmansortby','group');
82 $session->param('grpmanorder','ASC');
[76]83 $session->param('reclistsortby','host');
[51]84 $session->param('reclistorder','ASC');
[2]85}
86
[125]87# Just In Case. Stale sessions should not be resurrectable.
88if ($sid ne $session->id()) {
[163]89 $sid = '';
[125]90 changepage(page=> "login", sessexpired => 1);
91}
92
[163]93# normal expiry, more or less
94if ($session->is_expired) {
95 $sid = '';
96 changepage(page=> "login", sessexpired => 1);
97}
98
[19]99my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
100my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
[18]101
[176]102# decide which page to spit out...
103# also set $webvar{page} before we try to use it.
104$webvar{page} = 'login' if !$webvar{page};
105
[54]106# per-page startwith, filter, searchsubs
[160]107
108##fixme: complain-munge-and-continue with non-"[a-z0-9-.]" filter and startwith
109$webvar{startwith} =~ s/^(0-9|[a-z]).*/$1/ if $webvar{startwith};
110# not much call for chars not allowed in domain names
111$webvar{filter} =~ s/[^a-zA-Z0-9_.:@-]//g if $webvar{filter};
[176]112## only set 'y' if box is checked, no other values legal
113## however, see https://secure.deepnet.cx/trac/dnsadmin/ticket/31
114# first, drop obvious fakes
115delete $webvar{searchsubs} if $webvar{searchsubs} && $webvar{searchsubs} !~ /^[ny]/;
116# strip the known "turn me off!" bit.
117$webvar{searchsubs} =~ s/^n\s?// if $webvar{searchsubs};
118# strip non-y/n - note this legitimately allows {searchsubs} to go empty
119$webvar{searchsubs} =~ s/[^yn]//g if $webvar{searchsubs};
[160]120
[64]121$session->param($webvar{page}.'startwith', $webvar{startwith}) if defined($webvar{startwith});
[62]122$session->param($webvar{page}.'filter', $webvar{filter}) if defined($webvar{filter});
[57]123$session->param($webvar{page}.'searchsubs', $webvar{searchsubs}) if defined($webvar{searchsubs});
[54]124
125my $startwith = $session->param($webvar{page}.'startwith');
126my $filter = $session->param($webvar{page}.'filter');
127my $searchsubs = $session->param($webvar{page}.'searchsubs');
128
[160]129# ... and assemble the args
130my @filterargs;
131push @filterargs, "^[$startwith]" if $startwith;
132push @filterargs, $filter if $filter;
133
[26]134# nrgh, can't handle login here because we don't have a database handle to check the user/pass with yet
[2]135
136my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
137my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
138
[117]139## set up "URL to self"
140# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
141my $uri_self = $ENV{REQUEST_URI};
142$uri_self =~ s/\&([a-z])/\&amp\;$1/g;
143
144# le sigh. and we need to strip any previous action
145$uri_self =~ s/\&amp;action=[^&]+//g;
146
147# and search filter options. these get stored in the session, but discarded
148# as soon as you switch to a different page.
149##fixme: think about retaining these on a per-page basis, as well as offset; same as the sort-order bits
150no warnings qw(uninitialized);
151$uri_self =~ s/\&amp;startwith=[a-z09-]*(\&)?/$1/g;
152$uri_self =~ s/\&amp;searchsubs=[a-z09-]*(\&)?/$1/g;
153$uri_self =~ s/\&amp;filter=[a-z09-]*(\&)?/$1/g;
154use warnings qw(uninitialized);
155
[160]156# pagination
157my $perpage = 15;
[2]158my $offset = ($webvar{offset} ? $webvar{offset} : 0);
159
160# NB: these must match the field name and SQL ascend/descend syntax respectively
[41]161my $sortby = "domain";
162my $sortorder = "ASC";
[2]163
[128]164##fixme: quit throwing the database handle around, and put all the SQL and direct DB fiddling into DNSDB.pm
[112]165# dbname, user, pass, host (optional)
[128]166my ($dbh,$msg) = connectDB($config{dbname}, $config{dbuser}, $config{dbpass}, $config{dbhost});
[2]167
[128]168if (!$dbh) {
169 print "Content-type: text/html\n\n";
170 print $header->output;
171 my $errpage = HTML::Template->new(filename => "$templatedir/dberr.tmpl");
172 $errpage->param(errmsg => $msg);
173 print $errpage->output;
174 print $footer->output;
175 exit;
176}
[2]177
[128]178# Load config pieces from the database. Ideally all but the DB user/pass/etc should be loaded here.
[2]179initGlobals($dbh);
180
[153]181# security check - does the user have permission to view this entity?
182# this is a prep step used "many" places
183my @viewablegroups;
184getChildren($dbh, $logingroup, \@viewablegroups, 'all');
185push @viewablegroups, $logingroup;
186
[173]187my $page;
188eval {
189 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
190};
191if ($@) {
192 warn "Bad page $webvar{page} requested";
193 $page = HTML::Template->new(filename => "$templatedir/badpage.tmpl");
194 $page->param(badpage => $q->escapeHTML($webvar{page}));
195 $webvar{page} = 'badpage';
196}
[154]197
[26]198# handle login redirect
[30]199if ($webvar{action}) {
200 if ($webvar{action} eq 'login') {
[65]201 # Snag ACL/permissions here too
[30]202 my $sth = $dbh->prepare("SELECT user_id,group_id,password,firstname,lastname FROM users WHERE username=?");
203 $sth->execute($webvar{username});
204 my ($uid,$gid,$pass,$fname,$lname) = $sth->fetchrow_array;
205 $webvar{loginfailed} = 1 if !defined($uid);
[26]206
[30]207 if ($pass =~ m|^\$1\$([A-Za-z0-9/.]+)\$|) {
[92]208 # native passwords (crypt-md5)
[30]209 $webvar{loginfailed} = 1 if $pass ne unix_md5_crypt($webvar{password},$1);
[92]210 } elsif ($pass =~ /^[0-9a-f]{32}$/) {
211 # VegaDNS import (hex-coded MD5)
212 $webvar{loginfailed} = 1 if $pass ne md5_hex($webvar{password});
[30]213 } else {
[92]214 # plaintext (convenient now and then)
[30]215 $webvar{loginfailed} = 1 if $pass ne $webvar{password};
216 }
[29]217
[30]218 # set session bits
219 $session->param('logingroup',$gid);
220 $session->param('curgroup',$gid);
[65]221 $session->param('uid',$uid);
[30]222 $session->param('username',$webvar{username});
[26]223
[30]224 changepage(page => "domlist") if !defined($webvar{loginfailed});
225 } elsif ($webvar{action} eq 'logout') {
226 # delete the session
227 $session->delete();
228 $session->flush();
229
230 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}";
231 $newurl =~ s|/[^/]+$|/|;
232 print "Status: 302\nLocation: $newurl\n\n";
233 exit;
234
[57]235 } elsif ($webvar{action} eq 'chgroup') {
236 # fiddle session-stored group data
237 # magic incantation to... uhhh...
[117]238
239 # ... and the "change group" bits...
240 $uri_self =~ s/\&amp;group=[^&]*//g;
241
[154]242 # security check - does the user have permission to view this entity?
[155]243 my $errmsg;
[154]244 if (!(grep /^$webvar{group}$/, @viewablegroups)) {
245 # hmm. Reset the current group to the login group? Yes. Prevents confusing behaviour elsewhere.
246 $session->param('curgroup',$logingroup);
247 $webvar{group} = $logingroup;
248 $curgroup = $logingroup;
[155]249 $errmsg = "You are not permitted to view or make changes in the requested group";
250 $page->param(errmsg => $errmsg);
[154]251 }
[153]252
[57]253 $session->param('curgroup', $webvar{group});
254 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
[155]255
256 # I hate special cases.
257 if ($webvar{page} eq 'reclist' && $webvar{defrec} eq 'y') {
258 my %args = (page => $webvar{page}, id => $curgroup, defrec => $webvar{defrec});
259 $args{errmsg} = $errmsg if $errmsg;
260 changepage(%args);
261 }
262
[30]263 }
[57]264} # handle global webvar{action}s
[26]265
[65]266initPermissions($dbh,$session->param('uid'));
[57]267
[163]268$page->param(sid => $sid) unless $webvar{page} eq 'login'; # no session ID on the login page
[2]269
[26]270if ($webvar{page} eq 'login') {
[3]271
[26]272 $page->param(loginfailed => 1) if $webvar{loginfailed};
[163]273 $page->param(sessexpired => 1) if $webvar{sessexpired};
[26]274
275} elsif ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
276
[3]277# hmm. seeing problems in some possibly-not-so-corner cases.
[10]278# this currently only handles "domain on", "domain off"
[139]279 if (defined($webvar{domstatus})) {
[154]280 # security check - does the user have permission to access this entity?
281 my $flag = 0;
282 foreach (@viewablegroups) {
283 $flag = 1 if isParent($dbh, $_, 'group', $webvar{id}, 'domain');
284 }
285 if ($flag && ($permissions{admin} || $permissions{edit_domain})) {
286 my $stat = domStatus($dbh,$webvar{id},$webvar{domstatus});
287 logaction($webvar{id}, $session->param("username"), parentID($webvar{id}, 'dom', 'group'),
[62]288 "Changed ".domainName($dbh, $webvar{id})." state to ".($stat ? 'active' : 'inactive'));
[154]289 } else {
290 $page->param(errmsg => "You are not permitted to view or change the requested domain");
291 }
[3]292 }
293
[147]294 $page->param(resultmsg => $webvar{resultmsg}) if $webvar{resultmsg};
[174]295 if ($session->param('errmsg')) {
296 $page->param(errmsg => $session->param('errmsg'));
297 $session->clear('errmsg');
298 }
[147]299
[18]300 $page->param(curpage => $webvar{page});
301
[11]302 listdomains();
[2]303
[4]304} elsif ($webvar{page} eq 'newdomain') {
[2]305
[95]306 changepage(page => "domlist", errmsg => "You are not permitted to add domains")
307 unless ($permissions{admin} || $permissions{domain_create});
308
[126]309 fill_grouplist("grouplist");
310
[174]311 if ($session->param('add_failed')) {
312 $session->clear('add_failed');
[62]313 $page->param(add_failed => 1);
[174]314 $page->param(errmsg => $session->param('errmsg'));
315 $session->clear('errmsg');
[62]316 $page->param(domain => $webvar{domain});
317 }
[2]318
[57]319} elsif ($webvar{page} eq 'adddomain') {
320
[95]321 changepage(page => "domlist", errmsg => "You are not permitted to add domains")
322 unless ($permissions{admin} || $permissions{domain_create});
323
[162]324 # security check - does the user have permission to access this entity?
[169]325 if (!check_scope(id => $webvar{group}, type => 'group')) {
[174]326 $session->param('add_failed', 1);
327##fixme: domain a security risk for XSS?
328 changepage(page => "newdomain", domain => $webvar{domain},
[162]329 errmsg => "You do not have permission to add a domain to the requested group");
330 }
331
[57]332 my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
333
334 if ($code eq 'OK') {
335 logaction($msg, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
336 changepage(page => "reclist", id => $msg);
337 } else {
[62]338 logaction(0, $session->param("username"), $webvar{group}, "Failed adding domain $webvar{domain} ($msg)");
[174]339 $session->param('add_failed', 1);
340##fixme: domain a security risk for XSS?
341 changepage(page => "newdomain", domain => $webvar{domain}, errmsg => $msg);
[57]342 }
343
[11]344} elsif ($webvar{page} eq 'deldom') {
345
[95]346 changepage(page => "domlist", errmsg => "You are not permitted to delete domains")
347 unless ($permissions{admin} || $permissions{domain_delete});
348
[162]349 # security check - does the user have permission to access this entity?
[169]350 if (!check_scope(id => $webvar{id}, type => 'domain')) {
[162]351 changepage(page => "domlist", errmsg => "You do not have permission to delete the requested domain");
352 }
353
[11]354 $page->param(id => $webvar{id});
[88]355
[11]356 # first pass = confirm y/n (sorta)
357 if (!defined($webvar{del})) {
[88]358
[11]359 $page->param(del_getconf => 1);
360 $page->param(domain => domainName($dbh,$webvar{id}));
361
[88]362 } elsif ($webvar{del} eq 'ok') {
[57]363 my $pargroup = parentID($webvar{id}, 'dom', 'group');
[61]364 my $dom = domainName($dbh, $webvar{id});
[11]365 my ($code,$msg) = delDomain($dbh, $webvar{id});
366 if ($code ne 'OK') {
[62]367 logaction($webvar{id}, $session->param("username"), $pargroup, "Failed to delete domain $dom ($msg)");
[95]368 changepage(page => "domlist", errmsg => "Error deleting domain $dom: $msg");
[11]369 } else {
[61]370 logaction($webvar{id}, $session->param("username"), $pargroup, "Deleted domain $dom");
[147]371 changepage(page => "domlist", resultmsg => "Deleted domain $dom");
[11]372 }
[88]373
[11]374 } else {
375 # cancelled. whee!
376 changepage(page => "domlist");
377 }
378
[47]379} elsif ($webvar{page} eq 'reclist') {
380
[162]381 # security check - does the user have permission to view this entity?
[169]382 if (!check_scope(id => $webvar{id}, type => ($webvar{defrec} eq 'y' ? 'group' : 'domain'))) {
[154]383 $page->param(errmsg => "You are not permitted to view or change the requested ".
384 ($webvar{defrec} eq 'y' ? "group's default records" : "domain's records"));
[160]385 $page->param(perm_err => 1); # this causes the template to skip the record listing output.
[162]386 goto DONERECLIST; # and now we skip filling in the content which is not printed due to perm_err above
[154]387 }
[162]388
[140]389# hmm. where do we send them?
390 if ($webvar{defrec} eq 'y' && !$permissions{admin}) {
391 $page->param(errmsg => "You are not permitted to edit default records");
392 $page->param(perm_err => 1);
[162]393 } else {
[140]394
395 $page->param(mayeditsoa => $permissions{admin} || $permissions{domain_edit});
[95]396##fixme: ACL needs pondering. Does "edit domain" interact with record add/remove/etc?
397# Note this seems to be answered "no" in Vega.
398# ACLs
[140]399 $page->param(record_create => ($permissions{admin} || $permissions{record_create}) );
[160]400# we don't have any general edit links on the page; they're all embedded in the TMPL_LOOP
401# $page->param(record_edit => ($permissions{admin} || $permissions{record_edit}) );
[140]402 $page->param(record_delete => ($permissions{admin} || $permissions{record_delete}) );
[95]403
[47]404 # Handle record list for both default records (per-group) and live domain records
405
[140]406 $page->param(defrec => $webvar{defrec});
407 $page->param(id => $webvar{id});
408 $page->param(curpage => $webvar{page});
[47]409
[140]410 my $count = getRecCount($dbh, $webvar{defrec}, $webvar{id}, $filter);
[47]411
[140]412 $sortby = 'host';
[76]413# sort/order
[140]414 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
415 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[76]416
[140]417 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
418 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[76]419
[72]420# set up the headers
[140]421 my @cols = ('host', 'type', 'val', 'distance', 'weight', 'port', 'ttl');
422 my %colheads = (host => 'Name', type => 'Type', val => 'Address',
[72]423 distance => 'Distance', weight => 'Weight', port => 'Port', ttl => 'TTL');
[140]424 my %custom = (id => $webvar{id}, defrec => $webvar{defrec});
425 fill_colheads($sortby, $sortorder, \@cols, \%colheads, \%custom);
[72]426
[47]427# fill the page-count and first-previous-next-last-all details
[140]428 fill_pgcount($count,"records",
[97]429 ($webvar{defrec} eq 'y' ? "group ".groupName($dbh,$webvar{id}) : domainName($dbh,$webvar{id})));
[140]430 fill_fpnla($count); # should put some params on this sub...
[47]431
[140]432 $page->param(defrec => $webvar{defrec});
433 if ($webvar{defrec} eq 'y') {
434 showdomain('y',$curgroup);
435 } else {
436 showdomain('n',$webvar{id});
437##fixme: permission for viewing logs?
438 $page->param(logdom => 1);
439 }
[47]440
[151]441 $page->param(resultmsg => $webvar{resultmsg}) if $webvar{resultmsg};
[174]442 if ($session->param('errmsg')) {
443 $page->param(errmsg => $session->param('errmsg'));
444 $session->clear('errmsg');
445 }
[63]446
[140]447 } # close "you can't edit default records" check
448
[162]449 # Yes, this is a GOTO target. PTBHTTT.
450 DONERECLIST: ;
451
[13]452} elsif ($webvar{page} eq 'record') {
[16]453
[155]454 # security check - does the user have permission to access this entity?
[169]455 if (!check_scope(id => $webvar{id}, type => ($webvar{defrec} eq 'y' ? 'defrec' : 'record'))) {
[158]456 $page->param(perm_err => "You are not permitted to edit the requested record");
[155]457 goto DONEREC;
458 }
459 # round 2, check the parent.
[169]460 if (!check_scope(id => $webvar{parentid}, type => ($webvar{defrec} eq 'y' ? 'group' : 'domain'))) {
[155]461 my $msg = ($webvar{defrec} eq 'y' ?
462 "You are not permitted to add or edit default records in the requested group" :
463 "You are not permitted to add or edit records in the requested domain");
464 $page->param(perm_err => $msg);
465 goto DONEREC;
466 }
467
[13]468 if ($webvar{recact} eq 'new') {
[16]469
[95]470 changepage(page => "reclist", errmsg => "You are not permitted to add records", id => $webvar{parentid})
471 unless ($permissions{admin} || $permissions{record_create});
472
[87]473 $page->param(todo => "Add record");
[15]474 $page->param(recact => "add");
[59]475 $page->param(parentid => $webvar{parentid});
476 $page->param(defrec => $webvar{defrec});
[16]477
[59]478 fill_recdata();
479
[15]480 } elsif ($webvar{recact} eq 'add') {
481
[95]482 changepage(page => "reclist", errmsg => "You are not permitted to add records", id => $webvar{parentid})
483 unless ($permissions{admin} || $permissions{record_create});
484
[160]485##fixme: this should probably go in DNSDB::addRec(), need to ponder what to do about PTR and friends
[159]486 # prevent out-of-domain records from getting added by appending the domain, or DOMAIN for default records
487 my $pname = ($webvar{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$webvar{parentid}));
488 $webvar{name} =~ s/\.*$/\.$pname/ if $webvar{name} !~ /$pname$/;
489
[15]490 my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
491 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
492 push @recargs, $webvar{distance};
493 if ($webvar{type} == $reverse_typemap{SRV}) {
494 push @recargs, $webvar{weight};
495 push @recargs, $webvar{port};
496 }
497 }
[59]498
[15]499 my ($code,$msg) = addRec(@recargs);
500
501 if ($code eq 'OK') {
[57]502 if ($webvar{defrec} eq 'y') {
[151]503 my $restr = "Added default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}";
504 logaction(0, $session->param("username"), $webvar{parentid}, $restr);
505 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[57]506 } else {
[151]507 my $restr = "Added record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}";
508 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'), $restr);
509 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[57]510 }
[15]511 } else {
[24]512 $page->param(failed => 1);
513 $page->param(errmsg => $msg);
514 $page->param(wastrying => "adding");
[87]515 $page->param(todo => "Add record");
[24]516 $page->param(recact => "add");
517 $page->param(parentid => $webvar{parentid});
518 $page->param(defrec => $webvar{defrec});
519 $page->param(id => $webvar{id});
[16]520 fill_recdata(); # populate the form... er, mostly.
[59]521 if ($webvar{defrec} eq 'y') {
522 logaction(0, $session->param("username"), $webvar{parentid},
[63]523 "Failed adding default record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]524 } else {
525 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
[63]526 "Failed adding record '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]527 }
[15]528 }
529
[13]530 } elsif ($webvar{recact} eq 'edit') {
[15]531
[95]532 changepage(page => "reclist", errmsg => "You are not permitted to edit records", id => $webvar{parentid})
533 unless ($permissions{admin} || $permissions{record_edit});
534
[16]535 $page->param(todo => "Update record");
536 $page->param(recact => "update");
537 $page->param(parentid => $webvar{parentid});
[17]538 $page->param(id => $webvar{id});
[16]539 $page->param(defrec => $webvar{defrec});
[90]540 my $recdata = getRecLine($dbh, $webvar{defrec}, $webvar{id});
541 $page->param(name => $recdata->{host});
542 $page->param(address => $recdata->{val});
543 $page->param(distance => $recdata->{distance});
544 $page->param(weight => $recdata->{weight});
545 $page->param(port => $recdata->{port});
546 $page->param(ttl => $recdata->{ttl});
547 fill_rectypes($recdata->{type});
[16]548
549 } elsif ($webvar{recact} eq 'update') {
550
[95]551 changepage(page => "reclist", errmsg => "You are not permitted to edit records", id => $webvar{parentid})
552 unless ($permissions{admin} || $permissions{record_edit});
553
[159]554 # prevent out-of-domain records from getting added by appending the domain, or DOMAIN for default records
555 my $pname = ($webvar{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$webvar{parentid}));
556 $webvar{name} =~ s/\.*$/\.$pname/ if $webvar{name} !~ /$pname$/;
557
[160]558 # get current/previous record info so we can log "updated 'foo A 1.2.3.4' to 'foo A 2.3.4.5'"
559 my $oldrec = getRecLine($dbh, $webvar{defrec}, $webvar{id});
[159]560
[16]561 my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
562 $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
563 $webvar{distance},$webvar{weight},$webvar{port});
564
565 if ($code eq 'OK') {
[57]566 if ($webvar{defrec} eq 'y') {
[160]567 my $restr = "Updated default record from '$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}', TTL $oldrec->{ttl}\n".
568 "to '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}";
[151]569 logaction(0, $session->param("username"), $webvar{parentid}, $restr);
570 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[57]571 } else {
[160]572 my $restr = "Updated record from '$oldrec->{host} $typemap{$oldrec->{type}} $oldrec->{val}', TTL $oldrec->{ttl}\n".
573 "to '$webvar{name} $typemap{$webvar{type}} $webvar{address}', TTL $webvar{ttl}";
[151]574 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{id}, 'rec', 'group'), $restr);
575 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[57]576 }
[16]577 } else {
578 $page->param(failed => 1);
579 $page->param(errmsg => $msg);
580 $page->param(wastrying => "updating");
581 $page->param(todo => "Update record");
582 $page->param(recact => "update");
583 $page->param(parentid => $webvar{parentid});
584 $page->param(defrec => $webvar{defrec});
[17]585 $page->param(id => $webvar{id});
[16]586 fill_recdata();
[59]587 if ($webvar{defrec} eq 'y') {
588 logaction(0, $session->param("username"), $webvar{parentid},
[63]589 "Failed updating default record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]590 } else {
591 logaction($webvar{parentid}, $session->param("username"), parentID($webvar{parentid}, 'dom', 'group'),
[63]592 "Failed updating record '$typemap{$webvar{type}} $webvar{name} $webvar{address}', TTL $webvar{ttl} ($msg)");
[59]593 }
[16]594 }
[13]595 }
[16]596
[13]597 if ($webvar{defrec} eq 'y') {
[20]598 $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
[13]599 } else {
[24]600 $page->param(parentid => $webvar{parentid});
[16]601 $page->param(dohere => domainName($dbh,$webvar{parentid}));
[13]602 }
603
[155]604 # Yes, this is a GOTO target. PTBHTTT.
605 DONEREC: ;
606
[2]607} elsif ($webvar{page} eq 'delrec') {
608
[111]609 # This is a complete separate segment since it uses a different template from add/edit records above
610
[95]611 changepage(page => "reclist", errmsg => "You are not permitted to delete records", id => $webvar{parentid})
612 unless ($permissions{admin} || $permissions{record_delete});
613
[2]614 $page->param(id => $webvar{id});
615 $page->param(defrec => $webvar{defrec});
[39]616 $page->param(parentid => $webvar{parentid});
[2]617 # first pass = confirm y/n (sorta)
618 if (!defined($webvar{del})) {
619 $page->param(del_getconf => 1);
[107]620 my $rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
621 $page->param(host => $rec->{host});
622 $page->param(ftype => $typemap{$rec->{type}});
623 $page->param(recval => $rec->{val});
[39]624 } elsif ($webvar{del} eq 'ok') {
[62]625# get rec data before we try to delete it
[107]626 my $rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
[3]627 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
628 if ($code ne 'OK') {
629## need to find failure mode
[62]630 if ($webvar{defrec} eq 'y') {
[107]631 logaction(0, $session->param("username"), $rec->{parid},
632 "Failed deleting default record '$rec->{host} $typemap{$rec->{type}} $rec->{val}',".
633 " TTL $rec->{ttl} ($msg)");
[62]634 } else {
[107]635 logaction($rec->{parid}, $session->param("username"), parentID($rec->{parid}, 'dom', 'group'),
636 "Failed deleting record '$rec->{host} $typemap{$rec->{type}} $rec->{val}', TTL $rec->{ttl} ($msg)");
[62]637 }
[88]638 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec},
[95]639 errmsg => "Error deleting record: $msg");
640# $page->param(del_failed => 1);
641# $page->param(errmsg => $msg);
642# showdomain($webvar{defrec}, $webvar{parentid});
[39]643 } else {
[62]644 if ($webvar{defrec} eq 'y') {
[151]645 my $restr = "Deleted default record '$rec->{host} $typemap{$rec->{type}} $rec->{val}', TTL $rec->{ttl}";
646 logaction(0, $session->param("username"), $rec->{parid}, $restr);
647 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[62]648 } else {
[151]649 my $restr = "Deleted record '$rec->{host} $typemap{$rec->{type}} $rec->{val}', TTL $rec->{ttl}";
650 logaction($rec->{parid}, $session->param("username"), parentID($rec->{parid}, 'dom', 'group'), $restr);
651 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, resultmsg => $restr);
[62]652 }
[3]653 }
[39]654 } else {
655 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
[2]656 }
657
658} elsif ($webvar{page} eq 'editsoa') {
659
[162]660 # security check - does the user have permission to view this entity?
[169]661 if (!check_scope(id => $webvar{id}, type => ($webvar{defrec} eq 'y' ? 'group' : 'domain'))) {
[162]662 changepage(page => 'domlist', errmsg => "You do not have permission to edit the ".
663 ($webvar{defrec} eq 'y' ? 'default ' : '')."SOA record for the requested ".
664 ($webvar{defrec} eq 'y' ? 'group' : 'domain'));
665 }
666
667 if ($webvar{defrec} eq 'y') {
668 changepage(page => "domlist", errmsg => "You are not permitted to edit default records")
669 unless $permissions{admin};
670 } else {
671 changepage(page => "reclist", errmsg => "You are not permitted to edit domain SOA records", id => $webvar{id})
[111]672 unless ($permissions{admin} || $permissions{domain_edit});
[162]673 }
[111]674
[39]675 fillsoa($webvar{defrec},$webvar{id});
[2]676
677} elsif ($webvar{page} eq 'updatesoa') {
678
[162]679 # security check - does the user have permission to view this entity?
680 # pass 1, record ID
[169]681 if (!check_scope(id => $webvar{recid}, type => ($webvar{defrec} eq 'y' ? 'defrec' : 'record'))) {
[162]682 changepage(page => 'domlist', errmsg => "You do not have permission to edit the requested SOA record");
683 }
684 # pass 2, parent (group or domain) ID
[169]685 if (!check_scope(id => $webvar{id}, type => ($webvar{defrec} eq 'y' ? 'group' : 'domain'))) {
[162]686 changepage(page => 'domlist', errmsg => "You do not have permission to edit the ".
687 ($webvar{defrec} eq 'y' ? 'default ' : '')."SOA record for the requested ".
688 ($webvar{defrec} eq 'y' ? 'group' : 'domain'));
689 }
690
[111]691 changepage(page => "reclist", errmsg => "You are not permitted to edit domain SOA records", id => $webvar{id})
692 unless ($permissions{admin} || $permissions{domain_edit});
693
[162]694 # get old SOA for log
695 my %soa = getSOA($dbh,$webvar{defrec},$webvar{id});
696
[2]697 my $sth;
[162]698##fixme: push SQL into DNSDB.pm
699##fixme: data validation: make sure {recid} is really the SOA for {id}
[2]700 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
701 # plus a bit of magic to update the appropriate table
[162]702 my $sql = "UPDATE ".($webvar{defrec} eq 'y' ? "default_records" : "records").
703 " SET host=?, val=?, ttl=? WHERE record_id=?";
[2]704 $sth = $dbh->prepare($sql);
[162]705 $sth->execute("$webvar{prins}:$webvar{contact}",
706 "$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}",
707 $webvar{ttl},
708 $webvar{recid});
[2]709
710 if ($sth->err) {
711 $page->param(update_failed => 1);
712 $page->param(msg => $DBI::errstr);
[39]713 fillsoa($webvar{defrec},$webvar{id});
[2]714 } else {
[57]715
[162]716 # do this in the order of "default to most common case"
717 my $loggroup;
718 my $logdomain = $webvar{id};
719 if ($webvar{defrec} eq 'y') {
720 $loggroup = $webvar{id};
721 $logdomain = 0;
722 } else {
723 $loggroup = parentID($logdomain, 'dom', 'group', $webvar{defrec});
724 }
725
726 logaction($logdomain, $session->param("username"), $loggroup,
727 "Updated ".($webvar{defrec} eq 'y' ? 'default ' : '')."SOA for ".
728 ($webvar{defrec} eq 'y' ? groupName($dbh, $webvar{id}) : domainName($dbh, $webvar{id}) ).
729 ": (ns $soa{prins}, contact $soa{contact}, refresh $soa{refresh},".
730 " retry $soa{retry}, expire $soa{expire}, minTTL $soa{minttl}, TTL $soa{ttl}) to ".
731 "(ns $webvar{prins}, contact $webvar{contact}, refresh $webvar{refresh},".
732 " retry $webvar{retry}, expire $webvar{expire}, minTTL $webvar{minttl}, TTL $webvar{ttl})");
733 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec},
734 resultmsg => "SOA record updated");
[2]735 }
736
[17]737} elsif ($webvar{page} eq 'grpman') {
[2]738
[22]739 listgroups();
[140]740
741# Permissions!
742 $page->param(addgrp => $permissions{admin} || $permissions{group_create});
743 $page->param(edgrp => $permissions{admin} || $permissions{group_edit});
744 $page->param(delgrp => $permissions{admin} || $permissions{group_delete});
745
[147]746 $page->param(resultmsg => $webvar{resultmsg}) if $webvar{resultmsg};
[174]747 if ($session->param('errmsg')) {
748 $page->param(errmsg => $session->param('errmsg'));
749 $session->clear('errmsg');
750 }
[18]751 $page->param(curpage => $webvar{page});
752
[17]753} elsif ($webvar{page} eq 'newgrp') {
[20]754
[111]755 changepage(page => "grpman", errmsg => "You are not permitted to add groups", id => $webvar{parentid})
756 unless ($permissions{admin} || $permissions{group_add});
757
[18]758 # do.. uhh.. stuff.. if we have no webvar{action}
759 if ($webvar{action} && $webvar{action} eq 'add') {
[66]760 my %newperms;
761 foreach (@permtypes) {
762 $newperms{$_} = 0;
[92]763 $newperms{$_} = (defined($webvar{$_}) && $webvar{$_} eq 'on' ? 1 : 0);
[66]764 }
[88]765 # not gonna provide the 4th param: template-or-clone flag, just yet
[66]766 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup}, \%newperms);
[57]767 if ($code eq 'OK') {
[55]768 logaction(0, $session->param("username"), $webvar{pargroup}, "Added group $webvar{newgroup}");
769 changepage(page => "grpman");
770 }
[66]771 # no point in doing extra work
772 fill_permissions($page, \%newperms);
[18]773 $page->param(add_failed => 1);
774 $page->param(errmsg => $msg);
775 $page->param(newgroup => $webvar{newgroup});
[66]776 fill_grouplist('pargroup',$webvar{pargroup});
[19]777 } else {
[66]778 fill_grouplist('pargroup',$curgroup);
[88]779 # fill default permissions with immediate parent's current ones
[66]780 my %parperms;
781 getPermissions($dbh, 'group', $curgroup, \%parperms);
782 fill_permissions($page, \%parperms);
[18]783 }
[20]784
[22]785} elsif ($webvar{page} eq 'delgrp') {
[20]786
[111]787 changepage(page => "grpman", errmsg => "You are not permitted to delete groups", id => $webvar{parentid})
788 unless ($permissions{admin} || $permissions{group_delete});
789
[20]790 $page->param(id => $webvar{id});
791 # first pass = confirm y/n (sorta)
792 if (!defined($webvar{del})) {
793 $page->param(del_getconf => 1);
[140]794
795##fixme
796# do a check for "group has stuff in it", and splatter a big warning
797# up along with an unchecked-by-default check box to YES DAMMIT DELETE THE WHOLE THING
798
[23]799# $page->param(groupname => groupName($dbh,$webvar{id}));
[20]800# print some neato things?
801
802# } else {
803# #whether actually deleting or cancelling we redirect to the group list, default format
804
805 } elsif ($webvar{del} eq 'ok') {
[57]806 my $deleteme = groupName($dbh,$webvar{id}); # get this before we delete it...
[167]807 my $delparent = parentID($webvar{id}, 'group','group');
[20]808 my ($code,$msg) = delGroup($dbh, $webvar{id});
809 if ($code ne 'OK') {
810# need to find failure mode
[167]811 logaction(0, $session->param("username"), $delparent, "Failure deleting group $deleteme: $msg");
[140]812 changepage(page => "grpman", errmsg => "Error deleting group $deleteme: $msg");
[20]813 } else {
[57]814##fixme: need to clean up log when deleting a major container
[167]815 logaction(0, $session->param("username"), $delparent, "Deleted group $deleteme");
[147]816 changepage(page => "grpman", resultmsg => "Deleted group $deleteme");
[20]817 }
818 } else {
819 # cancelled. whee!
820 changepage(page => "grpman");
821 }
[23]822 $page->param(delgroupname => groupName($dbh, $webvar{id}));
[24]823
[65]824} elsif ($webvar{page} eq 'edgroup') {
825
[140]826 changepage(page => "grpman", errmsg => "You are not permitted to edit groups")
[111]827 unless ($permissions{admin} || $permissions{group_edit});
828
[65]829 if ($webvar{action} eq 'updperms') {
830 # extra safety check; make sure user can't construct a URL to bypass ACLs
831 my %curperms;
832 getPermissions($dbh, 'group', $webvar{gid}, \%curperms);
[66]833 my %chperms;
834 foreach (@permtypes) {
[65]835 $webvar{$_} = 0 if !defined($webvar{$_});
836 $webvar{$_} = 1 if $webvar{$_} eq 'on';
[66]837 $chperms{$_} = $webvar{$_} if $curperms{$_} ne $webvar{$_};
[65]838 }
[66]839 my ($code,$msg) = changePermissions($dbh, 'group', $webvar{gid}, \%chperms);
840 if ($code eq 'OK') {
[148]841 logaction(0, $session->param("username"), $webvar{gid},
842 "Updated default permissions in group $webvar{gid} (".groupName($dbh, $webvar{gid}).")");
843 changepage(page => "grpman", resultmsg =>
844 "Updated default permissions in group ".groupName($dbh, $webvar{gid}));
[66]845 }
846 # no point in doing extra work
847 fill_permissions($page, \%chperms);
848 $page->param(errmsg => $msg);
[65]849 }
850 $page->param(gid => $webvar{gid});
851 $page->param(grpmeddle => groupName($dbh, $webvar{gid}));
852 my %grpperms;
853 getPermissions($dbh, 'group', $webvar{gid}, \%grpperms);
[66]854 fill_permissions($page, \%grpperms);
[65]855
[110]856} elsif ($webvar{page} eq 'bulkdomain') {
857 # Bulk operations on domains. Note all but group move are available on the domain list.
858
859 changepage(page => "domlist", errmsg => "You are not permitted to make bulk domain changes")
[111]860 unless ($permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
[110]861
[126]862 fill_grouplist("grouplist");
863
[110]864##fixme
865##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
866##fixme
867
868 my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
869 $sth->execute($curgroup);
870 my ($count) = ($sth->fetchrow_array);
871
872 $page->param(curpage => $webvar{page});
873 fill_pgcount($count,'domains',groupName($dbh,$curgroup));
874 fill_fpnla($count);
[112]875 $page->param(perpage => $perpage);
[110]876
877 my @domlist;
878 my $sql = "SELECT domain_id,domain FROM domains".
879 " WHERE group_id=?".
880 " ORDER BY domain".
881 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
882 $sth = $dbh->prepare($sql);
883 $sth->execute($curgroup);
884 my $rownum = 0;
885 while (my @data = $sth->fetchrow_array) {
886 my %row;
887 $row{domid} = $data[0];
888 $row{domain} = $data[1];
889 $rownum++; # putting this in the expression below causes failures. *eyeroll*
890 $row{newrow} = $rownum % 5 == 0;
891 push @domlist, \%row;
892 }
893 $page->param(domtable => \@domlist);
[112]894 # ACLs
[110]895 $page->param(maymove => ($permissions{admin} || ($permissions{domain_edit} && $permissions{domain_create} && $permissions{domain_delete})));
896 $page->param(maystatus => $permissions{admin} || $permissions{domain_edit});
897 $page->param(maydelete => $permissions{admin} || $permissions{domain_delete});
898
[112]899} elsif ($webvar{page} eq 'bulkchange') {
[110]900
[155]901 # security check - does the user have permission to access this entity?
[169]902 if (!check_scope(id => $webvar{destgroup}, type => 'group')) {
[155]903 $page->param(errmsg => "You are not permitted to make bulk changes in the requested group");
904 goto DONEBULK;
905 }
906
[112]907 if ($webvar{action} eq 'move') {
908 changepage(page => "domlist", errmsg => "You are not permitted to bulk-move domains")
909 unless ($permissions{admin} || ($permissions{domain_edit} && $permissions{domain_create} && $permissions{domain_delete}));
[114]910 my $newgname = groupName($dbh,$webvar{destgroup});
911 $page->param(action => "Move to group $newgname");
912 my @bulkresults;
913 # nngh. due to alpha-sorting on the previous page, we can't use domid-numeric
914 # order here, and since we don't have the domain names until we go around this
915 # loop, we can't alpha-sort them here. :(
916 foreach (keys %webvar) {
917 my %row;
918 next unless $_ =~ /^dom_\d+$/;
[155]919 # second security check - does the user have permission to meddle with this domain?
[169]920 if (!check_scope(id => $webvar{$_}, type => 'domain')) {
[155]921 $row{domerr} = "You are not permitted to make changes to the requested domain";
922 $row{domain} = $webvar{$_};
923 push @bulkresults, \%row;
924 next;
925 }
[114]926 $row{domain} = domainName($dbh,$webvar{$_});
927 my ($code, $msg) = changeGroup($dbh, 'domain', $webvar{$_}, $webvar{destgroup});
928 if ($code eq 'OK') {
929 logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
930 "Moved domain ".domainName($dbh, $webvar{$_})." to group $newgname");
931 $row{domok} = ($code eq 'OK');
932 } else {
933 logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
934 "Failure moving domain ".domainName($dbh, $webvar{$_})." to group $newgname: $msg");
935 }
936 $row{domerr} = $msg;
937 push @bulkresults, \%row;
938 }
939 $page->param(bulkresults => \@bulkresults);
[112]940
[114]941 } elsif ($webvar{action} eq 'deactivate' || $webvar{action} eq 'activate') {
942 changepage(page => "domlist", errmsg => "You are not permitted to bulk-$webvar{action} domains")
[112]943 unless ($permissions{admin} || $permissions{domain_edit});
[114]944 $page->param(action => "$webvar{action} domains");
945 my @bulkresults;
946 foreach (keys %webvar) {
947 my %row;
948 next unless $_ =~ /^dom_\d+$/;
[155]949 # second security check - does the user have permission to meddle with this domain?
[169]950 if (!check_scope(id => $webvar{$_}, type => 'domain')) {
[155]951 $row{domerr} = "You are not permitted to make changes to the requested domain";
952 $row{domain} = $webvar{$_};
953 push @bulkresults, \%row;
954 next;
955 }
[114]956 $row{domain} = domainName($dbh,$webvar{$_});
957##fixme: error handling on status change
958 my $stat = domStatus($dbh,$webvar{$_},($webvar{action} eq 'activate' ? 'domon' : 'domoff'));
959 logaction($webvar{$_}, $session->param("username"), parentID($webvar{$_}, 'dom', 'group'),
960 "Changed domain ".domainName($dbh, $webvar{$_})." state to ".($stat ? 'active' : 'inactive'));
961 $row{domok} = 1;
962# $row{domok} = ($code eq 'OK');
963# $row{domerr} = $msg;
964 push @bulkresults, \%row;
965 }
966 $page->param(bulkresults => \@bulkresults);
967
[112]968 } elsif ($webvar{action} eq 'delete') {
969 changepage(page => "domlist", errmsg => "You are not permitted to bulk-delete domains")
970 unless ($permissions{admin} || $permissions{domain_delete});
[114]971 $page->param(action => "$webvar{action} domains");
972 my @bulkresults;
973 foreach (keys %webvar) {
974 my %row;
975 next unless $_ =~ /^dom_\d+$/;
[155]976 # second security check - does the user have permission to meddle with this domain?
[169]977 if (!check_scope(id => $webvar{$_}, type => 'domain')) {
[155]978 $row{domerr} = "You are not permitted to make changes to the requested domain";
979 $row{domain} = $webvar{$_};
980 push @bulkresults, \%row;
981 next;
982 }
[114]983 $row{domain} = domainName($dbh,$webvar{$_});
984 my $pargroup = parentID($webvar{$_}, 'dom', 'group');
985 my $dom = domainName($dbh, $webvar{$_});
986 my ($code, $msg) = delDomain($dbh, $webvar{$_});
987 if ($code eq 'OK') {
988 logaction($webvar{$_}, $session->param("username"), $pargroup, "Deleted domain $dom");
989 $row{domok} = ($code eq 'OK');
990 } else {
991 logaction($webvar{$_}, $session->param("username"), $pargroup, "Failure deleting domain $dom: $msg");
992 }
993 $row{domerr} = $msg;
994 push @bulkresults, \%row;
995 }
996 $page->param(bulkresults => \@bulkresults);
997
998 } # move/(de)activate/delete if()
999
[112]1000 # not going to handle the unknown $webvar{action} else; it should not be possible in normal
1001 # operations, and anyone who meddles with the URL gets what they deserve.
1002
[155]1003 # Yes, this is a GOTO target. PTHBTTT.
1004 DONEBULK: ;
1005
[24]1006} elsif ($webvar{page} eq 'useradmin') {
1007
[139]1008 if (defined($webvar{userstatus})) {
[153]1009 # security check - does the user have permission to access this entity?
1010 my $flag = 0;
1011 foreach (@viewablegroups) {
1012 $flag = 1 if isParent($dbh, $_, 'group', $webvar{id}, 'user');
1013 }
1014 if ($flag) {
1015 userStatus($dbh,$webvar{id},$webvar{userstatus});
1016 } else {
1017 $page->param(errmsg => "You are not permitted to view or change the requested user");
1018 }
[51]1019 }
1020
[142]1021 list_users();
1022
1023# Permissions!
1024 $page->param(adduser => $permissions{admin} || $permissions{user_create});
1025# should we block viewing other users? Vega blocks "editing"...
1026# NB: no "edit self" link as with groups here. maybe there should be?
1027# $page->param(eduser => $permissions{admin} || $permissions{user_edit});
1028 $page->param(deluser => $permissions{admin} || $permissions{user_delete});
1029
[144]1030 $page->param(resultmsg => $webvar{resultmsg}) if $webvar{resultmsg};
1031 $page->param(warnmsg => $webvar{warnmsg}) if $webvar{warnmsg};
[174]1032 if ($session->param('errmsg')) {
1033 $page->param(errmsg => $session->param('errmsg'));
1034 $session->clear('errmsg');
1035 }
[24]1036 $page->param(curpage => $webvar{page});
1037
[67]1038} elsif ($webvar{page} eq 'user') {
1039
[111]1040 # All user add/edit actions fall through the same page, since there aren't
1041 # really any hard differences between the templates
1042
[83]1043 #fill_actypelist($webvar{accttype});
[67]1044 fill_clonemelist();
1045 my %grpperms;
1046 getPermissions($dbh, 'group', $curgroup, \%grpperms);
[83]1047
[67]1048 my $grppermlist = new HTML::Template(filename => "$templatedir/permlist.tmpl");
1049 my %noaccess;
1050 fill_permissions($grppermlist, \%grpperms, \%noaccess);
1051 $grppermlist->param(info => 1);
1052 $page->param(grpperms => $grppermlist->output);
[83]1053
[67]1054 $page->param(is_admin => $permissions{admin});
1055
[88]1056 $webvar{action} = '' if !$webvar{action};
1057
[83]1058 if ($webvar{action} eq 'add' or $webvar{action} eq 'update') {
[67]1059
[83]1060 $page->param(add => 1) if $webvar{action} eq 'add';
1061
[67]1062 my ($code,$msg);
1063
1064 my $alterperms = 0; # flag iff we need to force custom permissions due to user's current access limits
1065
[87]1066 my %newperms; # we're going to prefill the existing permissions, so we can change them.
1067 getPermissions($dbh, 'user', $webvar{uid}, \%newperms);
1068
[67]1069 if ($webvar{pass1} ne $webvar{pass2}) {
1070 $code = 'FAIL';
1071 $msg = "Passwords don't match";
1072 } else {
1073
[83]1074 # assemble a permission string - far simpler than trying to pass an
1075 # indeterminate set of permission flags individually
[67]1076
[83]1077 # But first, we have to see if the user can add any particular
1078 # permissions; otherwise we have a priviledge escalation. Whee.
1079
1080 if (!$permissions{admin}) {
1081 my %grpperms;
1082 getPermissions($dbh, 'group', $curgroup, \%grpperms);
1083 my $ret = comparePermissions(\%permissions, \%grpperms);
[144]1084 if ($ret eq '<' || $ret eq '!') {
[83]1085 # User's permissions are not a superset or equivalent to group. Can't inherit
1086 # (and include access user doesn't currently have), so we force custom.
1087 $webvar{perms_type} = 'custom';
1088 $alterperms = 1;
1089 }
1090 }
1091
[67]1092 my $permstring;
1093 if ($webvar{perms_type} eq 'custom') {
1094 $permstring = 'C:';
1095 foreach (@permtypes) {
[87]1096 if ($permissions{admin} || $permissions{$_}) {
[67]1097 $permstring .= ",$_" if defined($webvar{$_}) && $webvar{$_} eq 'on';
[87]1098 $newperms{$_} = (defined($webvar{$_}) && $webvar{$_} eq 'on' ? 1 : 0);
[67]1099 }
1100 }
1101 $page->param(perm_custom => 1);
1102 } elsif ($permissions{admin} && $webvar{perms_type} eq 'clone') {
1103 $permstring = "c:$webvar{clonesrc}";
[87]1104 getPermissions($dbh, 'user', $webvar{clonesrc}, \%newperms);
[67]1105 $page->param(perm_clone => 1);
1106 } else {
1107 $permstring = 'i';
1108 }
[83]1109 if ($webvar{action} eq 'add') {
[144]1110 changepage(page => "useradmin", errmsg => "You do not have permission to add new users")
1111 unless $permissions{admin} || $permissions{user_create};
[83]1112 ($code,$msg) = addUser($dbh, $webvar{uname}, $curgroup, $webvar{pass1},
1113 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype}, $permstring,
1114 $webvar{fname}, $webvar{lname}, $webvar{phone});
[90]1115 logaction(0, $session->param("username"), $curgroup, "Added user $webvar{uname} (uid $msg)")
1116 if $code eq 'OK';
[83]1117 } else {
[144]1118 changepage(page => "useradmin", errmsg => "You do not have permission to edit users")
1119 unless $permissions{admin} || $permissions{user_edit};
[83]1120# User update is icky. I'd really like to do this in one atomic
1121# operation, but that would duplicate a **lot** of code in DNSDB.pm
1122 # Allowing for changing group, but not coding web support just yet.
1123 ($code,$msg) = updateUser($dbh, $webvar{uid}, $webvar{uname}, $webvar{gid}, $webvar{pass1},
1124 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
1125 $webvar{fname}, $webvar{lname}, $webvar{phone});
1126 if ($code eq 'OK') {
[90]1127 $newperms{admin} = 1 if $webvar{accttype} eq 'S';
[87]1128 ($code,$msg) = changePermissions($dbh, 'user', $webvar{uid}, \%newperms, ($permstring eq 'i'));
[83]1129 }
[90]1130 logaction(0, $session->param("username"), $curgroup,
1131 "Updated uid $webvar{uid}, user $webvar{uname} ($webvar{fname} $webvar{lname})");
[83]1132 }
[67]1133 }
1134
1135 if ($code eq 'OK') {
[83]1136
[67]1137 if ($alterperms) {
1138 changepage(page => "useradmin", warnmsg =>
[83]1139 "You can only grant permissions you hold. $webvar{uname} ".
1140 ($webvar{action} eq 'add' ? 'added' : 'updated')." with reduced access.");
[67]1141 } else {
[144]1142 changepage(page => "useradmin", resultmsg => "Successfully ".
1143 ($webvar{action} eq 'add' ? 'added' : 'updated')." user $webvar{uname}");
[67]1144 }
[83]1145
1146 # add/update failed:
[67]1147 } else {
1148 $page->param(add_failed => 1);
[83]1149 $page->param(action => $webvar{action});
1150 $page->param(set_permgroup => 1);
[87]1151 if ($webvar{perms_type} eq 'inherit') { # set permission class radio
1152 $page->param(perm_inherit => 1);
1153 } elsif ($webvar{perms_type} eq 'clone') {
1154 $page->param(perm_clone => 1);
1155 } else {
1156 $page->param(perm_custom => 1);
1157 }
[67]1158 $page->param(uname => $webvar{uname});
1159 $page->param(fname => $webvar{fname});
1160 $page->param(lname => $webvar{lname});
1161 $page->param(pass1 => $webvar{pass1});
1162 $page->param(pass2 => $webvar{pass2});
1163 $page->param(errmsg => $msg);
[83]1164 fill_permissions($page, \%newperms);
1165 fill_actypelist($webvar{accttype});
[67]1166 fill_clonemelist();
[88]1167##fixme: log
[67]1168 }
1169
1170 } elsif ($webvar{action} eq 'edit') {
[83]1171
[144]1172 changepage(page => "useradmin", errmsg => "You do not have permission to edit users")
1173 unless $permissions{admin} || $permissions{user_edit};
1174
[83]1175 $page->param(set_permgroup => 1);
1176 $page->param(action => 'update');
1177 $page->param(uid => $webvar{user});
1178 fill_clonemelist();
1179
1180 my $userinfo = getUserData($dbh,$webvar{user});
1181 fill_actypelist($userinfo->{type});
1182 # not using this yet, but adding it now means we can *much* more easily do so later.
1183 $page->param(gid => $webvar{group_id});
1184
1185 my %curperms;
1186 getPermissions($dbh, 'user', $webvar{user}, \%curperms);
1187 fill_permissions($page, \%curperms);
1188
1189 $page->param(uname => $userinfo->{username});
1190 $page->param(fname => $userinfo->{firstname});
1191 $page->param(lname => $userinfo->{lastname});
[87]1192 $page->param(set_permgroup => 1);
[83]1193 if ($userinfo->{inherit_perm}) {
1194 $page->param(perm_inherit => 1);
1195 } else {
1196 $page->param(perm_custom => 1);
1197 }
[67]1198 } else {
[144]1199 changepage(page => "useradmin", errmsg => "You are not allowed to add new users")
1200 unless $permissions{admin} || $permissions{user_create};
[67]1201 # default is "new"
[83]1202 $page->param(add => 1);
1203 $page->param(action => 'add');
1204 fill_permissions($page, \%grpperms);
1205 fill_actypelist();
[67]1206 }
1207
[90]1208} elsif ($webvar{page} eq 'deluser') {
1209
[145]1210 changepage(page=> "useradmin", errmsg => "You are not allowed to delete users")
1211 unless $permissions{admin} || $permissions{user_delete};
1212
[90]1213 $page->param(id => $webvar{id});
1214 # first pass = confirm y/n (sorta)
1215 if (!defined($webvar{del})) {
1216 $page->param(del_getconf => 1);
1217 $page->param(user => userFullName($dbh,$webvar{id}));
1218 } elsif ($webvar{del} eq 'ok') {
1219##fixme: find group id user is in (for logging) *before* we delete the user
1220##fixme: get other user data too for log
[93]1221 my $userref = getUserData($dbh, $webvar{id});
[90]1222 my ($code,$msg) = delUser($dbh, $webvar{id});
1223 if ($code ne 'OK') {
1224# need to find failure mode
1225 $page->param(del_failed => 1);
1226 $page->param(errmsg => $msg);
1227 list_users($curgroup);
[142]1228#Error deleting user <TMPL_VAR NAME=delusername>: <TMPL_VAR NAME=errmsg>
[90]1229 } else {
1230 # success. go back to the user list, do not pass "GO"
[93]1231 # actions on users have a domain id of 0, always
1232 logaction(0, $session->param("username"), $curgroup, "Deleted user $webvar{id}/".$userref->{username}.
1233 " (".$userref->{lastname}.", ".$userref->{firstname}.")");
[145]1234 changepage(page => "useradmin", resultmsg => "Deleted user ".$userref->{username}.
1235 " (".$userref->{lastname}.", ".$userref->{firstname}.")");
[90]1236 }
1237 } else {
1238 # cancelled. whee!
1239 changepage(page => "useradmin");
1240 }
1241
[30]1242} elsif ($webvar{page} eq 'dnsq') {
1243
1244 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
[31]1245 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
1246 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]1247 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
1248
1249 if ($webvar{qfor}) {
1250 my $resolv = Net::DNS::Resolver->new;
[31]1251 $resolv->tcp_timeout(5); # make me adjustable!
1252 $resolv->udp_timeout(5); # make me adjustable!
1253 $resolv->recurse(0) if $webvar{nrecurse};
1254 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
[30]1255 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
1256 if ($query) {
1257
1258 $page->param(showresults => 1);
1259
1260 my @answer;
1261 foreach my $rr ($query->answer) {
1262# next unless $rr->type eq "A" or $rr->type eq 'NS';
1263 my %row;
1264 my ($host,$ttl,$class,$type,$data) =
[31]1265 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]1266 $row{host} = $host;
1267 $row{ftype} = $type;
[31]1268 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]1269 push @answer, \%row;
1270 }
1271 $page->param(answer => \@answer);
1272
1273 my @additional;
1274 foreach my $rr ($query->additional) {
1275# next unless $rr->type eq "A" or $rr->type eq 'NS';
1276 my %row;
1277 my ($host,$ttl,$class,$type,$data) =
1278 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
1279 $row{host} = $host;
1280 $row{ftype} = $type;
1281 $row{rdata} = $data;
1282 push @additional, \%row;
1283 }
1284 $page->param(additional => \@additional);
1285
1286 my @authority;
1287 foreach my $rr ($query->authority) {
1288# next unless $rr->type eq "A" or $rr->type eq 'NS';
1289 my %row;
1290 my ($host,$ttl,$class,$type,$data) =
1291 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
1292 $row{host} = $host;
1293 $row{ftype} = $type;
1294 $row{rdata} = $data;
1295 push @authority, \%row;
1296 }
1297 $page->param(authority => \@authority);
1298
1299 $page->param(usedresolver => $resolv->answerfrom);
1300 $page->param(frtype => $typemap{$webvar{type}});
1301
1302 } else {
1303 $page->param(errmsg => $resolv->errorstring);
1304 }
1305 }
1306 ## done DNS query
1307
[31]1308} elsif ($webvar{page} eq 'axfr') {
1309
[111]1310 changepage(page => "domlist", errmsg => "You are not permitted to import domains")
1311 unless ($permissions{admin} || $permissions{domain_create});
1312
[31]1313 # don't need this while we've got the dropdown in the menu. hmm.
[126]1314 fill_grouplist("grouplist");
[31]1315
1316 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
1317 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
1318 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
[37]1319 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]1320 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
[33]1321
[91]1322 # shut up warning about uninitialized variable
1323 $webvar{doit} = '' if !defined($webvar{doit});
1324
[33]1325 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
1326 $page->param(errmsg => "Need to set host to import from");
1327 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
1328 $page->param(errmsg => "Need domains to import");
[91]1329 } elsif ($webvar{doit} eq 'y') {
[162]1330
1331 # security check - does the user have permission to access this entity?
[169]1332 if (!check_scope(id => $webvar{group}, type => 'group')) {
[162]1333 $page->param(errmsg => "You are not permitted to import domains into the requested group");
1334 goto DONEAXFR;
1335 }
1336
[33]1337 my @domlist = split /\s+/, $webvar{importdoms};
1338 my @results;
1339 foreach my $domain (@domlist) {
[34]1340 my %row;
1341 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
1342 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
[35]1343 $row{domok} = $msg if $code eq 'OK';
1344 if ($code eq 'WARN') {
1345 $msg =~ s|\n|<br />|g;
1346 $row{domwarn} = $msg;
1347 }
[37]1348 if ($code eq 'FAIL') {
[91]1349 $msg =~ s|\n|<br />\n|g;
[37]1350 $row{domerr} = $msg;
1351 }
[91]1352 $msg = "<br />\n".$msg if $msg =~ m|<br />|;
1353 logaction(domainID($dbh, $domain), $session->param("username"), $webvar{group},
1354 "AXFR import $domain from $webvar{ifrom} ($code): $msg");
[33]1355 $row{domain} = $domain;
1356 push @results, \%row;
1357 }
1358 $page->param(axfrresults => \@results);
1359 }
1360
[155]1361 # Yes, this is a GOTO target. PTBHTTT.
1362 DONEAXFR: ;
1363
[48]1364} elsif ($webvar{page} eq 'whoisq') {
[47]1365
[48]1366 if ($webvar{qfor}) {
1367 use Net::Whois::Raw;
1368 use Text::Wrap;
1369
1370# caching useful?
1371#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
1372#$Net::Whois::Raw::CACHE_TIME = 60;
1373
1374 my ($dominfo, $whois_server) = whois($webvar{qfor});
1375##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
1376
1377 # le sigh. idjits spit out data without linefeeds...
1378 $Text::Wrap::columns = 88;
1379
[93]1380# &%$@%@# high-bit crap. We should probably find a way to properly recode these
1381# instead of one-by-one. Note CGI::Simple's escapeHTML() doesn't do more than
1382# the bare minimum. :/
[48]1383# Mainly an XHTML validation thing.
[93]1384 $dominfo = $q->escapeHTML($dominfo);
[48]1385 $dominfo =~ s/\xa9/\&copy;/g;
1386 $dominfo =~ s/\xae/\&reg;/g;
1387
1388 $page->param(qfor => $webvar{qfor});
1389 $page->param(dominfo => wrap('','',$dominfo));
1390 $page->param(whois_server => $whois_server);
1391 } else {
1392 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
1393 }
1394
[47]1395} elsif ($webvar{page} eq 'log') {
1396
1397##fixme put in some real log-munching stuff
[59]1398 my $sql = "SELECT user_id, email, name, entry, date_trunc('second',stamp) FROM log WHERE ";
[60]1399 my $id = $curgroup; # we do this because the group log may be called from (almost) any page,
1400 # but the others are much more limited. this is probably non-optimal.
[61]1401 if ($webvar{ltype} && $webvar{ltype} eq 'user') {
[60]1402 $sql .= "user_id=?";
1403 $id = $webvar{id};
1404 $page->param(logfor => 'user '.userFullName($dbh,$id));
1405 } elsif ($webvar{ltype} && $webvar{ltype} eq 'dom') {
[59]1406 $sql .= "domain_id=?";
1407 $id = $webvar{id};
[60]1408 $page->param(logfor => 'domain '.domainName($dbh,$id));
[59]1409 } else {
1410 # Default to listing curgroup log
1411 $sql .= "group_id=?";
[60]1412 $page->param(logfor => 'group '.groupName($dbh,$id));
[59]1413 }
1414 my $sth = $dbh->prepare($sql);
1415 $sth->execute($id);
[47]1416 my @logbits;
[59]1417 while (my ($uid, $email, $name, $entry, $stamp) = $sth->fetchrow_array) {
[47]1418 my %row;
[59]1419 $row{userfname} = $name;
1420 $row{userid} = $uid;
1421 $row{useremail} = $email;
1422 $row{logentry} = $entry;
1423 ($row{logtime}) = ($stamp =~ /^(.+)-\d\d$/);
[47]1424 push @logbits, \%row;
1425 }
1426 $page->param(logentries => \@logbits);
1427
[60]1428} # end $webvar{page} dance
[2]1429
1430
[17]1431# start output here so we can redirect pages.
[7]1432print "Content-type: text/html\n\n", $header->output;
1433
[20]1434##common bits
[173]1435if ($webvar{page} ne 'login' && $webvar{page} ne 'badpage') {
[30]1436 $page->param(username => $session->param("username"));
1437
[20]1438 $page->param(group => $curgroup);
1439 $page->param(groupname => groupName($dbh,$curgroup));
[43]1440 $page->param(logingrp => groupName($dbh,$logingroup));
[117]1441 $page->param(logingrp_num => $logingroup);
[20]1442
[140]1443 $page->param(maydefrec => $permissions{admin});
[111]1444 $page->param(mayimport => $permissions{admin} || $permissions{domain_create});
1445 $page->param(maybulk => $permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
1446
[140]1447 $page->param(chggrps => ($permissions{admin} || $permissions{group_create} || $permissions{group_edit} || $permissions{group_delete}));
1448
[24]1449 # group tree. should go elsewhere, probably
1450 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
1451 $page->param(grptree => $tmpgrplist);
[65]1452 $page->param(subs => ($tmpgrplist ? 1 : 0)); # probably not useful to pass gobs of data in for a boolean
[42]1453 $page->param(inlogingrp => $curgroup == $logingroup);
1454
[53]1455# fill in the URL-to-self
[117]1456 $page->param(whereami => $uri_self);
[17]1457}
[13]1458
[166]1459if (@debugbits) {
1460 print "<pre>\n";
1461 foreach (@debugbits) { print; }
1462 print "</pre>\n";
1463}
[24]1464
[2]1465# spit it out
1466print $page->output;
1467
[38]1468if ($debugenv) {
1469 print "<div id=\"debug\">webvar keys: <pre>\n";
1470 foreach my $key (keys %webvar) {
1471 print "key: $key\tval: $webvar{$key}\n";
1472 }
1473 print "</pre>\nsession:\n<pre>\n";
1474 my $sesdata = $session->dataref();
1475 foreach my $key (keys %$sesdata) {
1476 print "key: $key\tval: ".$sesdata->{$key}."\n";
1477 }
1478 print "</pre>\nENV:\n<pre>\n";
1479 foreach my $key (keys %ENV) {
1480 print "key: $key\tval: $ENV{$key}\n";
1481 }
1482 print "</pre></div>\n";
[2]1483}
1484
1485print $footer->output;
1486
[18]1487# as per the docs, Just In Case
1488$session->flush();
[2]1489
1490exit 0;
1491
1492
[24]1493sub fill_grptree {
1494 my $root = shift;
1495 my $cur = shift;
[69]1496 my $indent = shift || ' ';
[24]1497
1498 my @childlist;
1499
1500 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
1501 getChildren($dbh,$root,\@childlist,'immediate');
1502 return if $#childlist == -1;
1503 my @grouplist;
1504 foreach (@childlist) {
1505 my %row;
1506 $row{grpname} = groupName($dbh,$_);
[117]1507 $row{grpnum} = $_;
1508 $row{whereami} = $uri_self;
[69]1509 # for all that HTML::Template is supposed to keep the HTML out of the Perl, this is so much more compact...
[117]1510# $row{grpdisp} = ($_ == $cur ? "<b>$row{grpname}</b>" : $row{grpname});
1511$row{curgrp} = ($_ == $cur);
1512$row{expanded} = isParent($dbh, $_, 'group', $cur, 'group');
1513$row{expanded} = 1 if $_ == $cur;
[69]1514 $row{subs} = fill_grptree($_,$cur,$indent.' ');
1515 $row{indent} = $indent;
[24]1516 push @grouplist, \%row;
1517 }
[69]1518 $grptree->param(indent => $indent);
[24]1519 $grptree->param(treelvl => \@grouplist);
1520 return $grptree->output;
1521}
1522
[11]1523sub changepage {
1524 my %params = @_; # think this works the way I want...
1525
[174]1526 # cross-site scripting fixup. instead of passing error messages by URL/form
1527 # variable, put them in the session where the nasty user can't meddle.
1528 if ($params{errmsg}) {
1529 $session->param('errmsg', $params{errmsg});
1530 delete $params{errmsg};
1531 }
1532
[11]1533 # handle user check
1534 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
1535 foreach (keys %params) {
[92]1536 $newurl .= "&$_=".$q->url_encode($params{$_});
[11]1537 }
1538
[30]1539 # Just In Case
1540 $session->flush();
1541
[11]1542 print "Status: 302\nLocation: $newurl\n\n";
1543 exit;
1544} # end changepage
1545
[2]1546sub fillsoa {
1547 my $def = shift;
1548 my $id = shift;
[39]1549 my $domname = ($def eq 'y' ? '' : "DOMAIN");
[2]1550
[39]1551 $page->param(defrec => $def);
[2]1552
[39]1553# i had a good reason to do this when I wrote it...
1554# $page->param(domain => $domname);
1555# $page->param(group => $DNSDB::group);
1556 $page->param(isgrp => 1) if $def eq 'y';
1557 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
[2]1558
1559# defaults
[17]1560 $page->param(defcontact => $DNSDB::def{contact});
1561 $page->param(defns => $DNSDB::def{prins});
1562 $page->param(defsoattl => $DNSDB::def{soattl});
1563 $page->param(defrefresh => $DNSDB::def{refresh});
1564 $page->param(defretry => $DNSDB::def{retry});
1565 $page->param(defexpire => $DNSDB::def{expire});
1566 $page->param(defminttl => $DNSDB::def{minttl});
[2]1567
1568 # there are probably better ways to do this. TMTOWTDI.
1569 my %soa = getSOA($dbh,$def,$id);
1570
[39]1571 $page->param(id => $id);
[2]1572 $page->param(recid => $soa{recid});
1573 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
1574 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
1575 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
1576 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
1577 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
1578 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
1579 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
1580}
1581
1582sub showdomain {
1583 my $def = shift;
1584 my $id = shift;
1585
1586 # get the SOA first
1587 my %soa = getSOA($dbh,$def,$id);
1588
1589 $page->param(contact => $soa{contact});
1590 $page->param(prins => $soa{prins});
1591 $page->param(refresh => $soa{refresh});
1592 $page->param(retry => $soa{retry});
1593 $page->param(expire => $soa{expire});
1594 $page->param(minttl => $soa{minttl});
1595 $page->param(ttl => $soa{ttl});
1596
[137]1597 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset},$sortby,$sortorder,$filter);
[2]1598
1599 my $row = 0;
1600 foreach my $rec (@$foo2) {
1601 $rec->{type} = $typemap{$rec->{type}};
1602 $rec->{row} = $row % 2;
[62]1603 $rec->{defrec} = $def;
[2]1604 $rec->{sid} = $webvar{sid};
[13]1605 $rec->{id} = $id;
[23]1606 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
1607 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
1608 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]1609 $row++;
[95]1610# ACLs
1611 $rec->{record_edit} = ($permissions{admin} || $permissions{record_edit});
1612 $rec->{record_delete} = ($permissions{admin} || $permissions{record_delete});
[2]1613 }
1614 $page->param(reclist => $foo2);
1615}
1616
[16]1617# fill in record type list on add/update/edit record template
1618sub fill_rectypes {
[13]1619 my $type = shift || $reverse_typemap{A};
[31]1620 my $soaflag = shift || 0;
[13]1621
[17]1622 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]1623 $sth->execute;
1624 my @typelist;
1625 while (my ($rval,$rname) = $sth->fetchrow_array()) {
1626 my %row = ( recval => $rval, recname => $rname );
[13]1627 $row{tselect} = 1 if $rval == $type;
[2]1628 push @typelist, \%row;
1629 }
[31]1630 if ($soaflag) {
1631 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
1632 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
1633 push @typelist, \%row;
1634 }
[2]1635 $page->param(typelist => \@typelist);
[31]1636} # fill_rectypes
[16]1637
1638sub fill_recdata {
1639 fill_rectypes($webvar{type});
1640
[91]1641# le sigh. we may get called with many empty %webvar keys
1642 no warnings qw( uninitialized );
1643
[101]1644##todo: allow BIND-style bare names, ASS-U-ME that the name is within the domain?
1645# prefill <domain> or DOMAIN in "Host" space for new records
1646 my $domroot = ($webvar{defrec} eq 'y' ? 'DOMAIN' : domainName($dbh,$webvar{parentid}));
1647 $page->param(name => $domroot);
[16]1648 $page->param(address => $webvar{address});
1649 $page->param(distance => $webvar{distance})
1650 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
1651 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
1652 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[101]1653# retrieve the right ttl instead of falling (way) back to the hardcoded system default
1654 my %soa = getSOA($dbh,$webvar{defrec},$webvar{parentid});
1655 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $soa{minttl}));
[2]1656}
[7]1657
[24]1658sub fill_actypelist {
[83]1659 my $curtype = shift || 'u';
1660
[24]1661 my @actypes;
1662
1663 my %row1 = (actypeval => 'u', actypename => 'user');
[83]1664 $row1{typesel} = 1 if $curtype eq 'u';
[24]1665 push @actypes, \%row1;
1666
1667 my %row2 = (actypeval => 'S', actypename => 'superuser');
[83]1668 $row2{typesel} = 1 if $curtype eq 'S';
[24]1669 push @actypes, \%row2;
1670
[83]1671 $page->param(actypelist => \@actypes);
[24]1672}
1673
[65]1674sub fill_clonemelist {
1675 my $sth = $dbh->prepare("SELECT username,user_id FROM users WHERE group_id=$curgroup");
1676 $sth->execute;
1677
[87]1678 # shut up some warnings, but don't stomp on caller's state
1679 local $webvar{clonesrc} = 0 if !defined($webvar{clonesrc});
1680
[65]1681 my @clonesrc;
1682 while (my ($username,$uid) = $sth->fetchrow_array) {
1683 my %row = (
1684 username => $username,
1685 uid => $uid,
1686 selected => ($webvar{clonesrc} == $uid ? 1 : 0)
1687 );
1688 push @clonesrc, \%row;
1689 }
1690 $page->param(clonesrc => \@clonesrc);
1691}
1692
[7]1693sub fill_fpnla {
1694 my $count = shift;
1695 if ($offset eq 'all') {
[70]1696 $page->param(perpage => $perpage);
[41]1697# uhm....
[7]1698 } else {
1699 # all these bits only have sensible behaviour if offset is numeric. err, probably.
1700 if ($count > $perpage) {
1701 # if there are more results than the default, always show the "all" link
1702 $page->param(navall => 1);
1703
1704 if ($offset > 0) {
1705 $page->param(navfirst => 1);
1706 $page->param(navprev => 1);
1707 $page->param(prevoffs => $offset-1);
1708 }
1709
1710 # show "next" and "last" links if we're not on the last page of results
1711 if ( (($offset+1) * $perpage - $count) < 0 ) {
1712 $page->param(navnext => 1);
1713 $page->param(nextoffs => $offset+1);
1714 $page->param(navlast => 1);
[8]1715 $page->param(lastoffs => int (($count-1)/$perpage));
[7]1716 }
[87]1717 } else {
1718 $page->param(onepage => 1);
[7]1719 }
1720 }
[10]1721} # end fill_fpnla()
1722
[12]1723sub fill_pgcount {
1724 my $pgcount = shift;
1725 my $pgtype = shift;
1726 my $parent = shift;
1727
[98]1728 # Fix display/UI bug where if you are not on the first page of the list, and
1729 # you add a search term or click one of the "starts with" links, you end up
1730 # on a page showing nothing.
1731 # For bonus points, this reverts to the original offset on clicking the "All" link (mostly)
[138]1732 if ($offset ne 'all') {
1733 $offset-- while ($offset * $perpage) >= $pgcount;
1734 }
[98]1735
[12]1736 $page->param(ntot => $pgcount);
1737 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
1738 $page->param(npglast => ($offset eq 'all' ? $pgcount :
1739 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
1740 ));
1741 $page->param(pgtype => $pgtype);
1742 $page->param(parent => $parent);
[137]1743 $page->param(filter => $filter);
[12]1744} # end fill_pgcount()
1745
[11]1746sub listdomains {
[41]1747
[95]1748# ACLs
1749 $page->param(domain_create => ($permissions{admin} || $permissions{domain_create}) );
1750 $page->param(domain_edit => ($permissions{admin} || $permissions{domain_edit}) );
1751 $page->param(domain_delete => ($permissions{admin} || $permissions{domain_delete}) );
1752
[52]1753 my @childgroups;
[61]1754 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1755 my $childlist = join(',',@childgroups);
1756
[57]1757 my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[160]1758 ($startwith ? " AND domain ~* ?" : '').
1759 ($filter ? " AND domain ~* ?" : '');
[52]1760 my $sth = $dbh->prepare($sql);
[160]1761 $sth->execute(@filterargs);
[17]1762 my ($count) = $sth->fetchrow_array;
1763
[12]1764# fill page count and first-previous-next-last-all bits
[20]1765 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]1766 fill_fpnla($count);
1767
[41]1768# sort/order
[51]1769 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1770 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]1771
[120]1772 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1773 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]1774
[44]1775# set up the headers
1776 my @cols = ('domain', 'status', 'group');
1777 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
[54]1778 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
[41]1779
1780 # hack! hack! pthbttt. have to rethink the status column storage,
1781 # or inactive comes "before" active. *sigh*
1782 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
1783
[51]1784# waffle, waffle - keep state on these as well as sortby, sortorder?
[53]1785 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[41]1786
[53]1787 $page->param(filter => $filter) if $filter;
1788 $page->param(searchsubs => $searchsubs) if $searchsubs;
[41]1789
1790##fixme
1791##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
1792##fixme
1793
[20]1794 $page->param(group => $curgroup);
[10]1795 my @domlist;
[52]1796 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
[20]1797 " INNER JOIN groups ON domains.group_id=groups.group_id".
[57]1798 " WHERE domains.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[160]1799 ($startwith ? " AND domain ~* ?" : '').
1800 ($filter ? " AND domain ~* ?" : '').
[41]1801 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
1802 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1803 $sth = $dbh->prepare($sql);
[160]1804 $sth->execute(@filterargs);
[10]1805 my $rownum = 0;
1806 while (my @data = $sth->fetchrow_array) {
1807 my %row;
1808 $row{domainid} = $data[0];
1809 $row{domain} = $data[1];
1810 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1811 $row{group} = $data[3];
1812 $row{bg} = ($rownum++)%2;
1813 $row{mkactive} = !$data[2];
1814 $row{sid} = $sid;
1815 $row{offset} = $offset;
[95]1816# ACLs
1817 $row{domain_edit} = ($permissions{admin} || $permissions{domain_edit});
1818 $row{domain_delete} = ($permissions{admin} || $permissions{domain_delete});
[10]1819 push @domlist, \%row;
1820 }
1821 $page->param(domtable => \@domlist);
[11]1822} # end listdomains()
[18]1823
[87]1824
[22]1825sub listgroups {
[53]1826
[153]1827# security check - does the user have permission to view this entity?
1828 if (!(grep /^$curgroup$/, @viewablegroups)) {
1829 # hmm. Reset the current group to the login group? Yes. Prevents confusing behaviour elsewhere.
1830 $session->param('curgroup',$logingroup);
1831 $page->param(errmsg => "You are not permitted to view the requested group");
1832 $curgroup = $logingroup;
1833 }
1834
[26]1835 my @childgroups;
[140]1836 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[26]1837 my $childlist = join(',',@childgroups);
1838
[140]1839 my $sql = "SELECT count(*) FROM groups WHERE parent_group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[160]1840 ($startwith ? " AND group_name ~* ?" : '').
1841 ($filter ? " AND group_name ~* ?" : '');
[26]1842 my $sth = $dbh->prepare($sql);
[160]1843 $sth->execute(@filterargs);
1844 my ($count) = ($sth->fetchrow_array);
[26]1845
[22]1846# fill page count and first-previous-next-last-all bits
1847 fill_pgcount($count,"groups",'');
1848 fill_fpnla($count);
1849
[80]1850 $page->param(gid => $curgroup);
1851
[124]1852 $sortby = 'group';
[42]1853# sort/order
[51]1854 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1855 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]1856
[120]1857 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1858 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]1859
[44]1860# set up the headers
1861 my @cols = ('group','parent','nusers','ndomains');
1862 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
[54]1863 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[42]1864
[51]1865# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1866 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1867
[53]1868 $page->param(filter => $filter) if $filter;
1869 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1870
1871# munge sortby for columns in database
1872 $sortby = 'g.group_name' if $sortby eq 'group';
1873 $sortby = 'g2.group_name' if $sortby eq 'parent';
1874
[22]1875 my @grouplist;
[160]1876 $sql = "SELECT g.group_id, g.group_name, g2.group_name, ".
[51]1877 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
[22]1878 "FROM groups g ".
1879 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1880 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1881 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[140]1882 "WHERE g.parent_group_id IN ($curgroup".($childlist ? ",$childlist" : '').") ".
[160]1883 ($startwith ? " AND g.group_name ~* ?" : '').
1884 ($filter ? " AND g.group_name ~* ?" : '').
[51]1885 " GROUP BY g.group_id, g.group_name, g2.group_name ".
1886 " ORDER BY $sortby $sortorder ".
[160]1887 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1888 $sth = $dbh->prepare($sql);
1889 $sth->execute(@filterargs);
[22]1890
1891 my $rownum = 0;
1892 while (my @data = $sth->fetchrow_array) {
1893 my %row;
1894 $row{groupid} = $data[0];
1895 $row{groupname} = $data[1];
1896 $row{pgroup} = $data[2];
1897 $row{nusers} = $data[3];
1898 $row{ndomains} = $data[4];
1899 $row{bg} = ($rownum++)%2;
1900 $row{sid} = $sid;
[140]1901 $row{edgrp} = ($permissions{admin} || $permissions{group_edit});
1902 $row{delgrp} = ($permissions{admin} || $permissions{group_delete});
[22]1903 push @grouplist, \%row;
1904 }
1905 $page->param(grouptable => \@grouplist);
1906} # end listgroups()
1907
[92]1908
[20]1909sub fill_grouplist {
[19]1910 my $template_var = shift;
1911 my $cur = shift || $curgroup;
[26]1912
1913 my @childgroups;
1914 getChildren($dbh, $logingroup, \@childgroups, 'all');
1915 my $childlist = join(',',@childgroups);
1916
[117]1917##fixme: need to reorder list so that we can display a pseudotree in group dropdowns
1918
[18]1919 # weesa gonna discard parent_group_id for now
[26]1920 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1921 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1922 "ORDER BY group_id");
[18]1923 $sth->execute;
[20]1924 my @grouplist;
1925 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]1926 my %row;
[20]1927 $row{groupname} = $groupname;
1928 $row{groupval} = $groupid;
[18]1929##fixme: need magic
[93]1930## ... WTF?
[20]1931# $row{defgroup} = '';
1932 $row{groupactive} = 1 if $groupid == $cur;
1933 push @grouplist, \%row;
[18]1934 }
1935
[20]1936 $page->param("$template_var" => \@grouplist);
[18]1937
[24]1938} # end fill_grouplist()
1939
[92]1940
[24]1941sub list_users {
[52]1942
1943 my @childgroups;
[53]1944 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1945 my $childlist = join(',',@childgroups);
1946
1947 my $sql = "SELECT count(*) FROM users WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[160]1948 ($startwith ? " AND username ~* ?" : '').
1949 ($filter ? " AND username ~* ?" : '');
[52]1950 my $sth = $dbh->prepare($sql);
[160]1951 $sth->execute(@filterargs);
[24]1952 my ($count) = ($sth->fetchrow_array);
1953
1954# fill page count and first-previous-next-last-all bits
1955 fill_pgcount($count,"users",'');
1956 fill_fpnla($count);
1957
[124]1958 $sortby = 'user';
[44]1959# sort/order
[51]1960 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1961 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]1962
[120]1963 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1964 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]1965
[44]1966# set up the headers
1967 my @cols = ('user','fname','type','group','status');
1968 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
[54]1969 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[44]1970
[51]1971# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1972 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1973
[53]1974 $page->param(filter => $filter) if $filter;
1975 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1976
1977# munge sortby for columns in database
1978 $sortby = 'u.username' if $sortby eq 'user';
1979 $sortby = 'u.type' if $sortby eq 'type';
1980 $sortby = 'g.group_name' if $sortby eq 'group';
1981 $sortby = 'u.status' if $sortby eq 'status';
1982
[24]1983 my @userlist;
[52]1984 $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
[24]1985 "FROM users u ".
1986 "INNER JOIN groups g ON u.group_id=g.group_id ".
[52]1987 "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[160]1988 ($startwith ? " AND u.username ~* ?" : '').
1989 ($filter ? " AND u.username ~* ?" : '').
[51]1990 " ORDER BY $sortby $sortorder ".
[52]1991 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
[51]1992
[52]1993 $sth = $dbh->prepare($sql);
[160]1994 $sth->execute(@filterargs);
[24]1995
1996 my $rownum = 0;
1997 while (my @data = $sth->fetchrow_array) {
[41]1998 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
[24]1999 my %row;
2000 $row{userid} = $data[0];
2001 $row{username} = $data[1];
[51]2002 $row{userfull} = $data[2];
2003 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
2004 $row{usergroup} = $data[4];
2005 $row{active} = $data[5];
[24]2006 $row{bg} = ($rownum++)%2;
2007 $row{sid} = $sid;
[142]2008 $row{eduser} = ($permissions{admin} || $permissions{user_edit});
2009 $row{deluser} = ($permissions{admin} || $permissions{user_delete});
[24]2010 push @userlist, \%row;
2011 }
2012 $page->param(usertable => \@userlist);
[55]2013} # end list_users()
[43]2014
[92]2015
[43]2016# Generate all of the glop necessary to add or not the appropriate marker/flag for
2017# the sort order and column in domain, user, group, and record lists
2018# Takes an array ref and hash ref
2019sub fill_colheads {
[54]2020 my $sortby = shift;
2021 my $sortorder = shift;
[43]2022 my $cols = shift;
2023 my $colnames = shift;
[72]2024 my $custom = shift;
[43]2025
2026 my @headings;
2027
2028 foreach my $col (@$cols) {
2029 my %coldata;
2030 $coldata{firstcol} = 1 if $col eq $cols->[0];
2031 $coldata{sid} = $sid;
2032 $coldata{page} = $webvar{page};
2033 $coldata{offset} = $webvar{offset} if $webvar{offset};
2034 $coldata{sortby} = $col;
2035 $coldata{colname} = $colnames->{$col};
2036 if ($col eq $sortby) {
2037 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
2038 $coldata{sortorder} = $sortorder;
2039 } else {
2040 $coldata{order} = 'ASC';
2041 }
[72]2042 if ($custom) {
2043 foreach my $ckey (keys %$custom) {
2044 $coldata{$ckey} = $custom->{$ckey};
2045 }
2046 }
[43]2047 push @headings, \%coldata;
2048 }
2049
2050 $page->param(colheads => \@headings);
2051
[54]2052} # end fill_colheads()
[55]2053
[92]2054
[55]2055sub logaction {
[59]2056 my $domid = shift;
2057 my $username = shift;
2058 my $groupid = shift;
2059 my $entry = shift;
[55]2060
[93]2061##fixme: push SQL into DNSDB.pm
[101]2062##fixme: add bits to retrieve group/domain name info to retain after entity is deleted?
[59]2063 my $sth = $dbh->prepare("SELECT user_id, firstname || ' ' || lastname FROM users WHERE username=?");
[55]2064 $sth->execute($username);
2065 my ($user_id, $fullname) = $sth->fetchrow_array;
2066
2067 $sth = $dbh->prepare("INSERT INTO log (domain_id,user_id,group_id,email,name,entry) ".
[107]2068 "VALUES (?,?,?,?,?,?)") or warn $dbh->errstr;
2069 $sth->execute($domid,$user_id,$groupid,$username,$fullname,$entry) or warn $sth->errstr;
[55]2070} # end logaction()
[57]2071
[92]2072
[59]2073##fixme: generalize to return appropriate id on all cases (ie, use $partype)
[57]2074sub parentID {
2075 my $id = shift;
2076 my $idtype = shift;
2077 my $partype = shift;
2078 my $defrec = shift || '';
2079
2080 my $sql = '';
2081
2082 if ($idtype eq 'dom') {
[59]2083 return $id if $defrec eq 'y'; # "domain" + default records, we're really looking at a group.
[57]2084 $sql = "SELECT group_id FROM domains WHERE domain_id=?";
2085 } elsif ($idtype eq 'rec') {
[59]2086 if ($defrec eq 'y') {
2087 $sql = "SELECT group_id FROM default_records WHERE record_id=?";
[57]2088 } else {
2089 $sql = "SELECT d.group_id FROM domains d".
2090 " INNER JOIN records r ON d.domain_id=r.domain_id".
2091 " WHERE r.record_id=?";
2092 }
2093 } elsif ($idtype eq 'group') {
2094 $sql = "SELECT parent_group_id FROM groups WHERE group_id=?";
2095 } elsif ($idtype eq 'user') {
2096 $sql = "SELECT group_id FROM users WHERE user_id=?";
2097 } else {
2098 return "FOO", "BAR"; # can't get here.... we think.
2099 }
[59]2100 my $sth = $dbh->prepare($sql);
2101 $sth->execute($id);
2102 my ($retid) = $sth->fetchrow_array;
2103 return $retid if $retid;
2104 # ahh! fall of the edge of the world if things went sideways
2105 ##fixme: really need to do a little more error handling, I think
[64]2106} # end parentID()
[66]2107
[92]2108
[66]2109# we have to do this in a variety of places; let's make it consistent
2110sub fill_permissions {
2111 my $template = shift; # may need to do several sets on a single page
2112 my $permset = shift; # hashref to permissions on object
[67]2113 my $usercan = shift || \%permissions; # allow alternate user-is-allowed permission block
[66]2114
2115 foreach (@permtypes) {
[67]2116 $template->param("may_$_" => ($usercan->{admin} || $usercan->{$_}));
[66]2117 $template->param($_ => $permset->{$_});
2118 }
2119}
[155]2120
2121# so simple when defined as a sub instead of inline. O_o
2122sub check_scope {
[169]2123 my %args = @_;
2124 my $entity = $args{id} || 0; # prevent the shooting of feet with SQL "... intcolumn = '' ..."
2125 my $entype = $args{type} || '';
[155]2126
2127 if ($entype eq 'group') {
2128 return 1 if grep /^$entity$/, @viewablegroups;
2129 } else {
2130 foreach (@viewablegroups) {
2131 return 1 if isParent($dbh, $_, 'group', $entity, $entype);
2132 }
2133 }
2134}
Note: See TracBrowser for help on using the repository browser.