source: trunk/dns.cgi@ 178

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

/trunk

Security review (See #30)

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