source: trunk/dns.cgi@ 358

Last change on this file since 358 was 338, checked in by Kris Deugau, 12 years ago

/trunk

Fix buglet in add record (possibly update record as well);
hostname entered was not retained on error case

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