source: trunk/dns.cgi@ 811

Last change on this file since 811 was 797, checked in by Kris Deugau, 4 years ago

/trunk

Clean up a lingering nuisance with Perl's default include path; all scripts
should now run correctly no matter what the caller's current directory.

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 100.4 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
[262]2# Main web UI script for DeepNet DNS Administrator
3##
4# $Id: dns.cgi 797 2020-11-03 20:38:37Z kdeugau $
[797]5# Copyright 2008-2020 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;
[706]27use CGI::Session '-ip_match';
[30]28use Net::DNS;
[2]29use DBI;
[543]30
[83]31use Data::Dumper;
[2]32
[95]33#sub is_tainted {
34# # from perldoc perlsec
35# return ! eval { eval("#" . substr(join("", @_), 0, 0)); 1 };
36#}
37#use Cwd 'abs_path';
38#use File::Basename;
39#use lib dirname( abs_path $0 );
40#die "argh! tainted!" if is_tainted($0);
41#die "argh! \@INC got tainted!" if is_tainted(@INC);
42
[797]43# push "the directory the script is in" into @INC
44use FindBin;
45use lib "$FindBin::RealBin/";
[216]46
[468]47use DNSDB;
[2]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
[493]67# create a DNSDB object. this loads some local system defaults and connects to the DB
68# with the credentials configured
69##fixme: pass params for loadConfig, and use them there, to allow one codebase to support multiple sites
[468]70my $dnsdb = new DNSDB;
71
72my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
73my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
74$footer->param(version => $DNSDB::VERSION);
75
[493]76##fixme: slim chance this could be triggered on errors other than DB failure?
[468]77if (!$dnsdb) {
78 print "Content-type: text/html\n\n";
79 print $header->output;
80 my $errpage = HTML::Template->new(filename => "$templatedir/dberr.tmpl");
81 $errpage->param(errmsg => $DNSDB::errstr);
82 print $errpage->output;
83 print $footer->output;
84 exit;
[163]85}
86
[468]87$header->param(orgname => $dnsdb->{orgname}) if $dnsdb->{orgname} ne 'Example Corp';
88
[493]89my $logingroup;
90my $curgroup;
91my @viewablegroups;
92
93# retrieve the session ID from our cookie, if possible
94my $sid = $q->cookie('dnsadmin_session');
95
96# see if the session loads
97my $session = CGI::Session->load("driver:File", $sid, {Directory => $dnsdb->{sessiondir}})
[68]98 or die CGI::Session->errstr();
[493]99
[641]100if (!$sid || $session->is_expired || !$session->param('uid') || !$dnsdb->userStatus($session->param('uid')) ) {
[493]101 $webvar{page} = 'login';
102} else {
103 # we have a session to load from, maybe
104 $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1);
105 $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup);
106 # security check - does the user have permission to view this entity?
107 # this is a prep step used "many" places
108 $dnsdb->getChildren($logingroup, \@viewablegroups, 'all');
109 push @viewablegroups, $logingroup;
110##fixme: make sessions persist through closing the site?
111# this even bridges browser close too. hmm...
112 $webvar{page} = 'domlist' if !$webvar{page};
[2]113}
114
[493]115# set $webvar{page} before we try to use it.
[176]116$webvar{page} = 'login' if !$webvar{page};
117
[493]118## per-page startwith, filter, searchsubs
[160]119
120##fixme: complain-munge-and-continue with non-"[a-z0-9-.]" filter and startwith
121$webvar{startwith} =~ s/^(0-9|[a-z]).*/$1/ if $webvar{startwith};
122# not much call for chars not allowed in domain names
[728]123$webvar{filter} =~ s/[^a-zA-Z0-9_.:\@%-]//g if $webvar{filter};
[176]124## only set 'y' if box is checked, no other values legal
125## however, see https://secure.deepnet.cx/trac/dnsadmin/ticket/31
126# first, drop obvious fakes
127delete $webvar{searchsubs} if $webvar{searchsubs} && $webvar{searchsubs} !~ /^[ny]/;
128# strip the known "turn me off!" bit.
129$webvar{searchsubs} =~ s/^n\s?// if $webvar{searchsubs};
130# strip non-y/n - note this legitimately allows {searchsubs} to go empty
131$webvar{searchsubs} =~ s/[^yn]//g if $webvar{searchsubs};
[160]132
[533]133# pagination
134my $perpage = 15; # Just In Case
135$perpage = $dnsdb->{perpage} if $dnsdb->{perpage};
136my $offset = ($webvar{offset} ? $webvar{offset} : 0);
137
138## set up "URL to self" (whereami edition)
139# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
140my $uri_self = $ENV{REQUEST_URI};
[738]141$uri_self = "/dns.cgi" if !$uri_self || $uri_self eq '/';
[533]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# much magic happens. if startwith or a search string change (to, from, or
148# across, in the request vs whatever's in the session) then the offset should
149# be reset to 0 so that the first/prev/next/last widget populates correctly,
150# and so that the list of whatever we're looking at actually shows things
151# (since we may have started on page 42 of 300 with a LOOOOONG list, but we
152# now only need 3 pages for the filtered list).
153# while we're at it, plonk these into the session for safekeeping.
154if (defined($webvar{startwith})) {
155 if ($webvar{startwith} ne $session->param($webvar{page}.'startwith')) {
156 $uri_self =~ s/\&amp;offset=[^&]//;
157 $offset = 0;
158 }
159 $session->param($webvar{page}.'startwith', $webvar{startwith});
160}
161if (defined($webvar{filter})) {
[705]162 $session->param($webvar{page}.'filter', '') if !$session->param($webvar{page}.'filter');
[533]163 if ($webvar{filter} ne $session->param($webvar{page}.'filter')) {
164 $uri_self =~ s/\&amp;offset=[^&]//;
165 $offset = 0;
166 }
167 $session->param($webvar{page}.'filter', $webvar{filter})
168}
[57]169$session->param($webvar{page}.'searchsubs', $webvar{searchsubs}) if defined($webvar{searchsubs});
[54]170
[533]171# and now that the search/filter criteria for this page are set, put them in some globals for actual use.
[54]172my $startwith = $session->param($webvar{page}.'startwith');
173my $filter = $session->param($webvar{page}.'filter');
174my $searchsubs = $session->param($webvar{page}.'searchsubs');
175
[160]176# ... and assemble the args
177my @filterargs;
178push @filterargs, "^[$startwith]" if $startwith;
179push @filterargs, $filter if $filter;
180
[117]181# and search filter options. these get stored in the session, but discarded
182# as soon as you switch to a different page.
183##fixme: think about retaining these on a per-page basis, as well as offset; same as the sort-order bits
184no warnings qw(uninitialized);
185$uri_self =~ s/\&amp;startwith=[a-z09-]*(\&)?/$1/g;
186$uri_self =~ s/\&amp;searchsubs=[a-z09-]*(\&)?/$1/g;
187$uri_self =~ s/\&amp;filter=[a-z09-]*(\&)?/$1/g;
188use warnings qw(uninitialized);
189
[213]190# Fix up $uri_self so we don't lose the session/page
[493]191$uri_self .= "?page=$webvar{page}" if $uri_self =~ m{/dns.cgi$};
192$uri_self = "$ENV{SCRIPT_NAME}?page=$webvar{page}$1" if $uri_self =~ m{/dns.cgi\&(.+)$};
[213]193
[493]194## end uri_self monkeying
195
[2]196# NB: these must match the field name and SQL ascend/descend syntax respectively
[493]197# sortby is reset to a suitable "default", then re-reset to whatever the user has
198# clicked on last in the record=listing subs, but best to put a default here.
[41]199my $sortby = "domain";
200my $sortorder = "ASC";
[2]201
[493]202# Create the page template object. Display a reasonable error page and whine if the template doesn't exist.
[173]203my $page;
204eval {
[238]205 # sigh. can't set loop_context_vars or global_vars once instantiated.
206 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl",
207 loop_context_vars => 1, global_vars => 1);
[173]208};
209if ($@) {
[238]210 my $msg = $@;
[173]211 $page = HTML::Template->new(filename => "$templatedir/badpage.tmpl");
[238]212 if (-e "$templatedir/$webvar{page}.tmpl") {
213 $page->param(badtemplate => $q->escapeHTML($msg));
214 } else {
215 warn "Bad page $webvar{page} requested";
216 $page->param(badpage => $q->escapeHTML($webvar{page}));
217 }
[173]218 $webvar{page} = 'badpage';
219}
[154]220
[553]221$session->expire($dnsdb->{timeout});
[551]222my $sesscookie = $q->cookie( -name => 'dnsadmin_session',
223 -value => $sid,
[591]224 -expires => "+".$dnsdb->{timeout},
[551]225 -secure => 0,
226## fixme: need to extract root path for cookie, so as to limit cookie to dnsadmin instance
227# -path => $url
228 );
[493]229
230# handle can-happen-on-(almost)-any-page actions
[30]231if ($webvar{action}) {
[493]232
[30]233 if ($webvar{action} eq 'login') {
[65]234 # Snag ACL/permissions here too
[26]235
[468]236 my $userdata = $dnsdb->login($webvar{username}, $webvar{password});
[183]237
[279]238 if ($userdata) {
239
[493]240 # (re)create the session
241 $session = new CGI::Session("driver:File", $sid, {Directory => $dnsdb->{sessiondir}})
242 or die CGI::Session->errstr();
243 $sid = $session->id();
244
245 $sesscookie = $q->cookie( -name => 'dnsadmin_session',
246 -value => $sid,
[591]247 -expires => "+".$dnsdb->{timeout},
[493]248 -secure => 0,
249## fixme: need to extract root path for cookie, so as to limit cookie to dnsadmin instance
250# -path => $url
251 );
252
[183]253 # set session bits
[493]254 $session->expire($dnsdb->{timeout});
[279]255 $session->param('logingroup',$userdata->{group_id});
256 $session->param('curgroup',$userdata->{group_id});
257 $session->param('uid',$userdata->{user_id});
[280]258 $session->param('username',$webvar{username});
[591]259 $curgroup = $userdata->{group_id};
[183]260
[493]261# for reference. seems we don't need to set these on login any more.
262# $session->param('domlistsortby','domain');
263# $session->param('domlistorder','ASC');
264# $session->param('revzonessortby','revnet');
265# $session->param('revzonesorder','ASC');
266# $session->param('useradminsortby','user');
267# $session->param('useradminorder','ASC');
268# $session->param('grpmansortby','group');
269# $session->param('grpmanorder','ASC');
270# $session->param('reclistsortby','host');
271# $session->param('reclistorder','ASC');
272# $session->param('loclistsortby','description');
273# $session->param('loclistorder','ASC');
274# $session->param('logsortby','stamp');
275# $session->param('logorder','DESC');
[183]276
[493]277 ## "recover my link" - tack on request bits and use requested page instead of hardcoding domlist
278 # this could possibly be compacted by munging changepage a little so we don't have to deconstruct
279 # and reconstruct the URI argument list.
280 my %target = (page => "domlist");
[570]281 if ($webvar{target} && $webvar{target} =~ /\?/ && $webvar{target} !~ /page=login/) {
[493]282 my $tmp = (split /\?/, $webvar{target})[1];
283 $tmp =~ s/^\&//;
284 my @targs = split /\&/, $tmp;
285 foreach (@targs) {
286 my ($k,$v) = split /=/;
287 $target{$k} = $v if $k;
288 # if we're going through a "session expired" login, we may have a different
289 # "current group" than the login group.
290 $session->param('curgroup', $v) if $k eq 'curgroup';
291##fixme: page=record goes "FOOM", sometimes - cause/fix?
292 }
293 }
294 changepage(%target);
295
[30]296 } else {
[183]297 $webvar{loginfailed} = 1;
298 } # user data fetch check
[29]299
[30]300 } elsif ($webvar{action} eq 'logout') {
301 # delete the session
302 $session->delete();
303 $session->flush();
304
[493]305 my $sesscookie = $q->cookie( -name => 'dnsadmin_session',
306 -value => $sid,
307 -expires => "-1",
308 -secure => 0,
309## fixme: need to extract root path for cookie, so as to limit cookie to dnsadmin instance
310# -path => $url
311 );
312
[30]313 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}";
314 $newurl =~ s|/[^/]+$|/|;
[493]315 print $q->redirect( -uri => $newurl, -cookie => $sesscookie);
[30]316 exit;
317
[493]318 } elsif ($webvar{action} eq 'chgroup' && $webvar{page} ne 'login') {
[57]319 # fiddle session-stored group data
320 # magic incantation to... uhhh...
[117]321
322 # ... and the "change group" bits...
323 $uri_self =~ s/\&amp;group=[^&]*//g;
324
[154]325 # security check - does the user have permission to view this entity?
[155]326 my $errmsg;
[154]327 if (!(grep /^$webvar{group}$/, @viewablegroups)) {
328 # hmm. Reset the current group to the login group? Yes. Prevents confusing behaviour elsewhere.
329 $session->param('curgroup',$logingroup);
330 $webvar{group} = $logingroup;
331 $curgroup = $logingroup;
[155]332 $errmsg = "You are not permitted to view or make changes in the requested group";
333 $page->param(errmsg => $errmsg);
[154]334 }
[153]335
[57]336 $session->param('curgroup', $webvar{group});
337 $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
[155]338
339 # I hate special cases.
[224]340##fixme: probably need to handle webvar{revrec}=='y' too
[155]341 if ($webvar{page} eq 'reclist' && $webvar{defrec} eq 'y') {
[245]342 my %args = (page => $webvar{page}, id => $curgroup, defrec => $webvar{defrec}, revrec => $webvar{revrec});
[155]343 $args{errmsg} = $errmsg if $errmsg;
344 changepage(%args);
345 }
[533]346 # add offset back *into* $uri_self if we're also currently looking at a live record list.
347 if ($webvar{page} eq 'reclist' && $webvar{defrec} eq 'n') {
348 $uri_self .= "\&amp;offset=$offset";
349 }
350 } # done action=chgroup
[57]351} # handle global webvar{action}s
[26]352
[493]353
[319]354# finally check if the user was disabled. we could just leave this for logout/session expiry,
355# but if they keep the session active they'll continue to have access long after being disabled. :/
356# Treat it as a session expiry.
[468]357if ($session->param('uid') && !$dnsdb->userStatus($session->param('uid')) ) {
[319]358 $sid = '';
359 $session->delete; # force expiry of the session Right Away
360 $session->flush; # make sure it hits storage
361 changepage(page=> "login", sessexpired => 1);
362}
363
[279]364# Misc Things To Do on most pages
[489]365my %permissions;
366$dnsdb->getPermissions('user', $session->param('uid'), \%permissions);
[468]367$dnsdb->initActionLog($session->param('uid'));
[57]368
[493]369##
370## Per-page processing
371##
[2]372
[26]373if ($webvar{page} eq 'login') {
[3]374
[493]375 my $target = $ENV{REQUEST_URI};
376 $target =~ s/\&/\&amp;/g;
377 $page->param(target => $target); # needs to be trimmed a little, maybe?
378
379 $page->param(sessexpired => 1) if (!$sid && $target !~ m|/$|);
380
381 if ($webvar{loginfailed}) {
382 $page->param(loginfailed => 1);
383 $webvar{target} =~ s/\&/\&amp;/g; # XHTML we do (not) love you so
384 $page->param(target => $webvar{target}) if $webvar{target};
385 }
386# if $webvar{sessexpired}; # or this with below?
387 if ($session->is_expired) {
388 $page->param(sessexpired => 1);
389 $session->delete(); # Just to make sure
390 $session->flush();
391 }
[210]392 $page->param(version => $DNSDB::VERSION);
[493]393 $page->param(script_self => ($ENV{SCRIPT_NAME} =~ m|/([^/]+)$|)[0]);
[26]394
395} elsif ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
396
[239]397 $page->param(domlist => 1);
398
[3]399# hmm. seeing problems in some possibly-not-so-corner cases.
[10]400# this currently only handles "domain on", "domain off"
[275]401 if (defined($webvar{zonestatus})) {
[154]402 # security check - does the user have permission to access this entity?
403 my $flag = 0;
404 foreach (@viewablegroups) {
[470]405 $flag = 1 if $dnsdb->isParent($_, 'group', $webvar{id}, 'domain');
[154]406 }
[188]407 if ($flag && ($permissions{admin} || $permissions{domain_edit})) {
[477]408 my $stat = $dnsdb->zoneStatus($webvar{id}, 'n', $webvar{zonestatus});
[283]409 $page->param(resultmsg => $DNSDB::resultstr);
[154]410 } else {
411 $page->param(errmsg => "You are not permitted to view or change the requested domain");
412 }
[275]413 $uri_self =~ s/\&amp;zonestatus=[^&]*//g; # clean up URL for stuffing into templates
[3]414 }
415
[376]416 show_msgs();
[147]417
[18]418 $page->param(curpage => $webvar{page});
419
[11]420 listdomains();
[2]421
[4]422} elsif ($webvar{page} eq 'newdomain') {
[2]423
[95]424 changepage(page => "domlist", errmsg => "You are not permitted to add domains")
425 unless ($permissions{admin} || $permissions{domain_create});
426
[310]427 $webvar{group} = $curgroup if !$webvar{group};
428 fill_grouplist("grouplist", $webvar{group});
[516]429 fill_loclist($curgroup, $webvar{defloc} ? $webvar{defloc} : '');
[126]430
[174]431 if ($session->param('add_failed')) {
432 $session->clear('add_failed');
[62]433 $page->param(add_failed => 1);
[174]434 $page->param(errmsg => $session->param('errmsg'));
435 $session->clear('errmsg');
[62]436 $page->param(domain => $webvar{domain});
[310]437 $page->param(addinactive => $webvar{makeactive} eq 'n');
[62]438 }
[2]439
[57]440} elsif ($webvar{page} eq 'adddomain') {
441
[95]442 changepage(page => "domlist", errmsg => "You are not permitted to add domains")
443 unless ($permissions{admin} || $permissions{domain_create});
444
[162]445 # security check - does the user have permission to access this entity?
[169]446 if (!check_scope(id => $webvar{group}, type => 'group')) {
[174]447 $session->param('add_failed', 1);
448##fixme: domain a security risk for XSS?
449 changepage(page => "newdomain", domain => $webvar{domain},
[162]450 errmsg => "You do not have permission to add a domain to the requested group");
451 }
452
[205]453 $webvar{makeactive} = 0 if !defined($webvar{makeactive});
454
[516]455 my ($code,$msg) = $dnsdb->addDomain($webvar{domain}, $webvar{group}, ($webvar{makeactive} eq 'on' ? 1 : 0),
456 $webvar{defloc});
[57]457
458 if ($code eq 'OK') {
[736]459 $webvar{domain} = lc($webvar{domain}) if $dnsdb->{lowercase};
[483]460 $dnsdb->mailNotify("New ".($webvar{makeactive} eq 'on' ? 'Active' : 'Inactive')." Domain Created",
[198]461 ($webvar{makeactive} eq 'on' ? 'Active' : 'Inactive').qq( domain "$webvar{domain}" added by ).
462 $session->param("username"));
[57]463 changepage(page => "reclist", id => $msg);
464 } else {
[174]465 $session->param('add_failed', 1);
466##fixme: domain a security risk for XSS?
[516]467 changepage(page => "newdomain", errmsg => $msg, domain => $webvar{domain},
468 group => $webvar{group}, makeactive => ($webvar{makeactive} ? 'y' : 'n'), defloc => $webvar{defloc});
[57]469 }
470
[11]471} elsif ($webvar{page} eq 'deldom') {
472
[95]473 changepage(page => "domlist", errmsg => "You are not permitted to delete domains")
474 unless ($permissions{admin} || $permissions{domain_delete});
475
[162]476 # security check - does the user have permission to access this entity?
[169]477 if (!check_scope(id => $webvar{id}, type => 'domain')) {
[162]478 changepage(page => "domlist", errmsg => "You do not have permission to delete the requested domain");
479 }
480
[11]481 $page->param(id => $webvar{id});
[88]482
[11]483 # first pass = confirm y/n (sorta)
484 if (!defined($webvar{del})) {
[88]485
[11]486 $page->param(del_getconf => 1);
[473]487 $page->param(domain => $dnsdb->domainName($webvar{id}));
[11]488
[88]489 } elsif ($webvar{del} eq 'ok') {
[473]490 my $pargroup = $dnsdb->parentID(id => $webvar{id}, type => 'domain', revrec => $webvar{revrec});
[477]491 my ($code,$msg) = $dnsdb->delZone($webvar{id}, $webvar{revrec});
[187]492 if ($code eq 'OK') {
[285]493 changepage(page => "domlist", resultmsg => $msg);
[187]494 } else {
[285]495 changepage(page => "domlist", errmsg => $msg);
[11]496 }
[88]497
[11]498 } else {
499 # cancelled. whee!
500 changepage(page => "domlist");
501 }
502
[237]503} elsif ($webvar{page} eq 'revzones') {
504
505 $webvar{revrec} = 'y';
[274]506
[275]507 if (defined($webvar{zonestatus})) {
508 # security check - does the user have permission to access this entity?
509 my $flag = 0;
510 foreach (@viewablegroups) {
[470]511 $flag = 1 if $dnsdb->isParent($_, 'group', $webvar{id}, 'revzone');
[275]512 }
513 if ($flag && ($permissions{admin} || $permissions{domain_edit})) {
[477]514 my $stat = $dnsdb->zoneStatus($webvar{id}, 'y', $webvar{zonestatus});
[283]515 $page->param(resultmsg => $DNSDB::resultstr);
[275]516 } else {
517 $page->param(errmsg => "You are not permitted to view or change the requested reverse zone");
518 }
519 $uri_self =~ s/\&amp;zonestatus=[^&]*//g; # clean up URL for stuffing into templates
520 }
521
[376]522 show_msgs();
[274]523
[237]524 $page->param(curpage => $webvar{page});
525 listzones();
526
[260]527} elsif ($webvar{page} eq 'newrevzone') {
528
529## scope/access check - use domain settings? invent new (bleh)
530 changepage(page => "revzones", errmsg => "You are not permitted to add reverse zones")
531 unless ($permissions{admin} || $permissions{domain_create});
532
533 fill_grouplist("grouplist");
[704]534 fill_loclist($curgroup, $webvar{defloc} ? $webvar{defloc} : '');
[260]535
[269]536 # prepopulate revpatt with the matching default record
[481]537# $dnsdb->getRecByName(revrec => $webvar{revrec}, defrec => $webvar{defrec}, host => 'string');
[269]538
[270]539 if ($session->param('add_failed')) {
540 $session->clear('add_failed');
541 $page->param(errmsg => $session->param('errmsg'));
542 $session->clear('errmsg');
[260]543 $page->param(revzone => $webvar{revzone});
544 $page->param(revpatt => $webvar{revpatt});
545 }
546
547} elsif ($webvar{page} eq 'addrevzone') {
548
549 changepage(page => "revzones", errmsg => "You are not permitted to add reverse zones")
550 unless ($permissions{admin} || $permissions{domain_create});
551
552 # security check - does the user have permission to access this entity?
553 if (!check_scope(id => $webvar{group}, type => 'group')) {
554 changepage(page => "newrevzone", add_failed => 1, revzone => $webvar{revzone}, revpatt => $webvar{revpatt},
555 errmsg => "You do not have permission to add a reverse zone to the requested group");
556 }
557
[477]558 my ($code,$msg) = $dnsdb->addRDNS($webvar{revzone}, $webvar{revpatt}, $webvar{group},
[446]559 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{location});
[260]560
561 if ($code eq 'OK') {
562 changepage(page => "reclist", id => $msg, revrec => 'y');
[286]563 } elsif ($code eq 'WARN') {
564 changepage(page => "reclist", id => $msg, revrec => 'y', warnmsg => $DNSDB::resultstr);
[260]565 } else {
[270]566 $session->param('add_failed', 1);
567 changepage(page => "newrevzone", revzone => $webvar{revzone}, revpatt => $webvar{revpatt}, errmsg => $msg);
[260]568 }
569
[274]570} elsif ($webvar{page} eq 'delrevzone') {
[260]571
[274]572 changepage(page => "revzones", errmsg => "You are not permitted to delete reverse zones")
573 unless ($permissions{admin} || $permissions{domain_delete});
574
575 # security check - does the user have permission to access this entity?
576 if (!check_scope(id => $webvar{id}, type => 'revzone')) {
577 changepage(page => "revzones", errmsg => "You do not have permission to delete the requested reverse zone");
578 }
579
580 $page->param(id => $webvar{id});
581
582 # first pass = confirm y/n (sorta)
583 if (!defined($webvar{del})) {
584
585 $page->param(del_getconf => 1);
[473]586 $page->param(revzone => $dnsdb->revName($webvar{id}));
[274]587
588 } elsif ($webvar{del} eq 'ok') {
[473]589 my $pargroup = $dnsdb->parentID(id => $webvar{id}, type => 'revzone', revrec => $webvar{revrec});
590 my $zone = $dnsdb->revName($webvar{id});
[477]591 my ($code,$msg) = $dnsdb->delZone($webvar{id}, 'y');
[274]592 if ($code eq 'OK') {
[285]593 changepage(page => "revzones", resultmsg => $msg);
[274]594 } else {
[285]595 changepage(page => "revzones", errmsg => $msg);
[274]596 }
597
598 } else {
599 # cancelled. whee!
600 changepage(page => "revzones");
601 }
602
[47]603} elsif ($webvar{page} eq 'reclist') {
604
[162]605 # security check - does the user have permission to view this entity?
[244]606 if (!check_scope(id => $webvar{id}, type =>
607 ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'revzone' : 'domain')))) {
[154]608 $page->param(errmsg => "You are not permitted to view or change the requested ".
[244]609 ($webvar{defrec} eq 'y' ? "group's default records" :
610 ($webvar{revrec} eq 'y' ? "reverse zone's records" : "domain's records")));
[160]611 $page->param(perm_err => 1); # this causes the template to skip the record listing output.
[162]612 goto DONERECLIST; # and now we skip filling in the content which is not printed due to perm_err above
[154]613 }
[162]614
[140]615# hmm. where do we send them?
616 if ($webvar{defrec} eq 'y' && !$permissions{admin}) {
617 $page->param(errmsg => "You are not permitted to edit default records");
618 $page->param(perm_err => 1);
[162]619 } else {
[140]620
621 $page->param(mayeditsoa => $permissions{admin} || $permissions{domain_edit});
[95]622##fixme: ACL needs pondering. Does "edit domain" interact with record add/remove/etc?
623# Note this seems to be answered "no" in Vega.
624# ACLs
[140]625 $page->param(record_create => ($permissions{admin} || $permissions{record_create}) );
[160]626# we don't have any general edit links on the page; they're all embedded in the TMPL_LOOP
627# $page->param(record_edit => ($permissions{admin} || $permissions{record_edit}) );
[140]628 $page->param(record_delete => ($permissions{admin} || $permissions{record_delete}) );
[95]629
[47]630 # Handle record list for both default records (per-group) and live domain records
631
[140]632 $page->param(defrec => $webvar{defrec});
[227]633 $page->param(revrec => $webvar{revrec});
[140]634 $page->param(id => $webvar{id});
635 $page->param(curpage => $webvar{page});
[47]636
[666]637 my $count = $dnsdb->getRecCount(defrec => $webvar{defrec}, revrec => $webvar{revrec},
638 id => $webvar{id}, filter => $filter);
[47]639
[140]640 $sortby = 'host';
[76]641# sort/order
[140]642 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
643 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[76]644
[140]645 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
646 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[76]647
[72]648# set up the headers
[224]649 my @cols;
650 my %colheads;
651 if ($webvar{revrec} eq 'n') {
652 @cols = ('host', 'type', 'val', 'distance', 'weight', 'port', 'ttl');
653 %colheads = (host => 'Name', type => 'Type', val => 'Address',
[72]654 distance => 'Distance', weight => 'Weight', port => 'Port', ttl => 'TTL');
[224]655 } else {
[267]656 @cols = ('val', 'type', 'host', 'ttl');
[268]657 %colheads = (val => 'IP Address', type => 'Type', host => 'Hostname', ttl => 'TTL');
[224]658 }
659 my %custom = (id => $webvar{id}, defrec => $webvar{defrec}, revrec => $webvar{revrec});
[140]660 fill_colheads($sortby, $sortorder, \@cols, \%colheads, \%custom);
[72]661
[47]662# fill the page-count and first-previous-next-last-all details
[140]663 fill_pgcount($count,"records",
[473]664 ($webvar{defrec} eq 'y' ? "group ".$dnsdb->groupName($webvar{id}) :
665 ($webvar{revrec} eq 'y' ? $dnsdb->revName($webvar{id}) : $dnsdb->domainName($webvar{id}))
[224]666 ));
[140]667 fill_fpnla($count); # should put some params on this sub...
[47]668
[140]669 $page->param(defrec => $webvar{defrec});
[224]670 showzone($webvar{defrec}, $webvar{revrec}, $webvar{id});
[248]671 if ($webvar{defrec} eq 'n') {
672 if ($webvar{revrec} eq 'n') {
673 $page->param(logdom => 1);
674 } else {
675 $page->param(logrdns => 1);
676 }
[140]677 }
[47]678
[376]679 show_msgs();
[63]680
[140]681 } # close "you can't edit default records" check
682
[162]683 # Yes, this is a GOTO target. PTBHTTT.
684 DONERECLIST: ;
685
[13]686} elsif ($webvar{page} eq 'record') {
[16]687
[155]688 # security check - does the user have permission to access this entity?
[244]689 if (!check_scope(id => $webvar{id}, type =>
[248]690 ($webvar{defrec} eq 'y' ? ($webvar{revrec} eq 'y' ? 'defrevrec' : 'defrec') : 'record'))) {
[158]691 $page->param(perm_err => "You are not permitted to edit the requested record");
[155]692 goto DONEREC;
693 }
694 # round 2, check the parent.
[244]695 if (!check_scope(id => $webvar{parentid}, type =>
696 ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'revzone' : 'domain')))) {
[155]697 my $msg = ($webvar{defrec} eq 'y' ?
698 "You are not permitted to add or edit default records in the requested group" :
[244]699 "You are not permitted to add or edit records in the requested domain/zone");
[155]700 $page->param(perm_err => $msg);
701 goto DONEREC;
702 }
703
[253]704 $page->param(defrec => $webvar{defrec});
705 $page->param(revrec => $webvar{revrec});
706 $page->param(fwdzone => $webvar{revrec} eq 'n');
707
[13]708 if ($webvar{recact} eq 'new') {
[16]709
[95]710 changepage(page => "reclist", errmsg => "You are not permitted to add records", id => $webvar{parentid})
711 unless ($permissions{admin} || $permissions{record_create});
712
[87]713 $page->param(todo => "Add record");
[15]714 $page->param(recact => "add");
[59]715 $page->param(parentid => $webvar{parentid});
[16]716
[59]717 fill_recdata();
718
[383]719 if ($webvar{defrec} eq 'n') {
[477]720 my $defloc = $dnsdb->getZoneLocation($webvar{revrec}, $webvar{parentid});
[383]721 fill_loclist($curgroup, $defloc);
722 }
723
[15]724 } elsif ($webvar{recact} eq 'add') {
725
[95]726 changepage(page => "reclist", errmsg => "You are not permitted to add records", id => $webvar{parentid})
727 unless ($permissions{admin} || $permissions{record_create});
728
[389]729 # location check - if user does not have record_locchg, set $webvar{location} to default location for zone
[477]730 my $parloc = $dnsdb->getZoneLocation($webvar{revrec}, $webvar{parentid});
[389]731 $webvar{location} = $parloc unless ($permissions{admin} || $permissions{record_locchg});
732
[481]733 my @recargs = ($webvar{defrec}, $webvar{revrec}, $webvar{parentid},
[543]734 \$webvar{name}, \$webvar{type}, \$webvar{address}, $webvar{ttl}, $webvar{location},
735 $webvar{expires}, $webvar{stamp});
[15]736 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
737 push @recargs, $webvar{distance};
738 if ($webvar{type} == $reverse_typemap{SRV}) {
739 push @recargs, $webvar{weight};
740 push @recargs, $webvar{port};
741 }
742 }
[59]743
[481]744 my ($code,$msg) = $dnsdb->addRec(@recargs);
[15]745
[234]746 if ($code eq 'OK' || $code eq 'WARN') {
747 my %pageparams = (page => "reclist", id => $webvar{parentid},
748 defrec => $webvar{defrec}, revrec => $webvar{revrec});
[628]749 $pageparams{warnmsg} = $msg."<br />\n".$DNSDB::resultstr if $code eq 'WARN';
[287]750 $pageparams{resultmsg} = $DNSDB::resultstr if $code eq 'OK';
[234]751 changepage(%pageparams);
[15]752 } else {
[24]753 $page->param(failed => 1);
754 $page->param(errmsg => $msg);
755 $page->param(wastrying => "adding");
[87]756 $page->param(todo => "Add record");
[24]757 $page->param(recact => "add");
758 $page->param(parentid => $webvar{parentid});
759 $page->param(id => $webvar{id});
[16]760 fill_recdata(); # populate the form... er, mostly.
[383]761 if ($webvar{defrec} eq 'n') {
762 fill_loclist($curgroup, $webvar{location});
763 }
[15]764 }
765
[13]766 } elsif ($webvar{recact} eq 'edit') {
[15]767
[95]768 changepage(page => "reclist", errmsg => "You are not permitted to edit records", id => $webvar{parentid})
769 unless ($permissions{admin} || $permissions{record_edit});
770
[16]771 $page->param(todo => "Update record");
772 $page->param(recact => "update");
773 $page->param(parentid => $webvar{parentid});
[17]774 $page->param(id => $webvar{id});
[481]775 my $recdata = $dnsdb->getRecLine($webvar{defrec}, $webvar{revrec}, $webvar{id});
[90]776 $page->param(name => $recdata->{host});
777 $page->param(address => $recdata->{val});
778 $page->param(distance => $recdata->{distance});
779 $page->param(weight => $recdata->{weight});
780 $page->param(port => $recdata->{port});
781 $page->param(ttl => $recdata->{ttl});
[483]782 $page->param(typelist => $dnsdb->getTypelist($webvar{revrec}, $recdata->{type}));
[543]783 if ($recdata->{stampactive}) {
784 $page->param(stamp => $recdata->{stamp});
785 $page->param(stamp_until => $recdata->{expires});
786 }
[383]787 if ($webvar{defrec} eq 'n') {
788 fill_loclist($curgroup, $recdata->{location});
789 }
790
[16]791 } elsif ($webvar{recact} eq 'update') {
792
[95]793 changepage(page => "reclist", errmsg => "You are not permitted to edit records", id => $webvar{parentid})
794 unless ($permissions{admin} || $permissions{record_edit});
795
[389]796 # retain old location if user doesn't have permission to fiddle locations
[481]797 my $oldrec = $dnsdb->getRecLine($webvar{defrec}, $webvar{revrec}, $webvar{id});
[389]798 $webvar{location} = $oldrec->{location} unless ($permissions{admin} || $permissions{record_locchg});
799
[481]800 my ($code,$msg) = $dnsdb->updateRec($webvar{defrec}, $webvar{revrec}, $webvar{id}, $webvar{parentid},
801 \$webvar{name}, \$webvar{type}, \$webvar{address}, $webvar{ttl}, $webvar{location},
[543]802 $webvar{expires}, $webvar{stamp},
[481]803 $webvar{distance}, $webvar{weight}, $webvar{port});
[16]804
[272]805 if ($code eq 'OK' || $code eq 'WARN') {
[288]806 my %pageparams = (page => "reclist", id => $webvar{parentid},
807 defrec => $webvar{defrec}, revrec => $webvar{revrec});
[628]808 $pageparams{warnmsg} = $msg."<br />\n".$DNSDB::resultstr if $code eq 'WARN';
[288]809 $pageparams{resultmsg} = $DNSDB::resultstr if $code eq 'OK';
810 changepage(%pageparams);
[16]811 } else {
812 $page->param(failed => 1);
813 $page->param(errmsg => $msg);
814 $page->param(wastrying => "updating");
815 $page->param(todo => "Update record");
816 $page->param(recact => "update");
817 $page->param(parentid => $webvar{parentid});
[17]818 $page->param(id => $webvar{id});
[16]819 fill_recdata();
820 }
[13]821 }
[16]822
[13]823 if ($webvar{defrec} eq 'y') {
[473]824 $page->param(dohere => "default records in group ".$dnsdb->groupName($webvar{parentid}));
[13]825 } else {
[473]826 $page->param(dohere => $dnsdb->domainName($webvar{parentid})) if $webvar{revrec} eq 'n';
827 $page->param(dohere => $dnsdb->revName($webvar{parentid})) if $webvar{revrec} eq 'y';
[13]828 }
829
[155]830 # Yes, this is a GOTO target. PTBHTTT.
831 DONEREC: ;
832
[2]833} elsif ($webvar{page} eq 'delrec') {
834
[111]835 # This is a complete separate segment since it uses a different template from add/edit records above
836
[243]837 changepage(page => "reclist", errmsg => "You are not permitted to delete records", id => $webvar{parentid},
[244]838 defrec => $webvar{defrec}, revrec => $webvar{revrec})
[95]839 unless ($permissions{admin} || $permissions{record_delete});
840
[244]841 if (!check_scope(id => $webvar{id}, type =>
842 ($webvar{defrec} eq 'y' ? ($webvar{revrec} eq 'y' ? 'defrevrec' : 'defrec') : 'record'))) {
843 # redirect to domlist because we don't have permission for the entity requested
[250]844 changepage(page => 'domlist', revrec => $webvar{revrec},
845 errmsg => "You do not have permission to delete records in the requested ".
[182]846 ($webvar{defrec} eq 'y' ? 'group' : 'domain'));
847 }
848
[2]849 $page->param(id => $webvar{id});
850 $page->param(defrec => $webvar{defrec});
[243]851 $page->param(revrec => $webvar{revrec});
[39]852 $page->param(parentid => $webvar{parentid});
[2]853 # first pass = confirm y/n (sorta)
854 if (!defined($webvar{del})) {
855 $page->param(del_getconf => 1);
[481]856 my $rec = $dnsdb->getRecLine($webvar{defrec}, $webvar{revrec}, $webvar{id});
[107]857 $page->param(host => $rec->{host});
858 $page->param(ftype => $typemap{$rec->{type}});
859 $page->param(recval => $rec->{val});
[39]860 } elsif ($webvar{del} eq 'ok') {
[481]861 my ($code,$msg) = $dnsdb->delRec($webvar{defrec}, $webvar{revrec}, $webvar{id});
[187]862 if ($code eq 'OK') {
[290]863 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec},
864 revrec => $webvar{revrec}, resultmsg => $msg);
[187]865 } else {
[3]866## need to find failure mode
[88]867 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec},
[290]868 revrec => $webvar{revrec}, errmsg => $msg);
[3]869 }
[39]870 } else {
[250]871 changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}, revrec => $webvar{revrec});
[2]872 }
873
874} elsif ($webvar{page} eq 'editsoa') {
875
[162]876 # security check - does the user have permission to view this entity?
[244]877 # id is domain/revzone/group id
878 if (!check_scope(id => $webvar{id}, type =>
[248]879 ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'revzone' : 'domain')))) {
[162]880 changepage(page => 'domlist', errmsg => "You do not have permission to edit the ".
881 ($webvar{defrec} eq 'y' ? 'default ' : '')."SOA record for the requested ".
882 ($webvar{defrec} eq 'y' ? 'group' : 'domain'));
883 }
884
885 if ($webvar{defrec} eq 'y') {
886 changepage(page => "domlist", errmsg => "You are not permitted to edit default records")
887 unless $permissions{admin};
888 } else {
889 changepage(page => "reclist", errmsg => "You are not permitted to edit domain SOA records", id => $webvar{id})
[111]890 unless ($permissions{admin} || $permissions{domain_edit});
[162]891 }
[111]892
[277]893 fillsoa($webvar{defrec},$webvar{revrec},$webvar{id});
[2]894
895} elsif ($webvar{page} eq 'updatesoa') {
896
[162]897 # security check - does the user have permission to view this entity?
898 # pass 1, record ID
[244]899 if (!check_scope(id => $webvar{recid}, type =>
900 ($webvar{defrec} eq 'y' ? ($webvar{revrec} eq 'y' ? 'defrevrec' : 'defrec') : 'record'))) {
[311]901##fixme: should we redirect to the requested record list page instead of the domain list?
[162]902 changepage(page => 'domlist', errmsg => "You do not have permission to edit the requested SOA record");
903 }
904 # pass 2, parent (group or domain) ID
[244]905 if (!check_scope(id => $webvar{id}, type =>
906 ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'revzone' : 'domain')))) {
[311]907 changepage(page => ($webvar{revrec} eq 'y' ? 'revzones' : 'domlist'),
908 errmsg => "You do not have permission to edit the ".
[162]909 ($webvar{defrec} eq 'y' ? 'default ' : '')."SOA record for the requested ".
[311]910 ($webvar{defrec} eq 'y' ? 'group' : ($webvar{revrec} eq 'y' ? 'reverse zone' : 'domain')) );
[162]911 }
912
[111]913 changepage(page => "reclist", errmsg => "You are not permitted to edit domain SOA records", id => $webvar{id})
914 unless ($permissions{admin} || $permissions{domain_edit});
915
[481]916 my ($code, $msg) = $dnsdb->updateSOA($webvar{defrec}, $webvar{revrec},
[277]917 (contact => $webvar{contact}, prins => $webvar{prins}, refresh => $webvar{refresh},
[311]918 retry => $webvar{retry}, expire => $webvar{expire}, minttl => $webvar{minttl},
919 ttl => $webvar{ttl}, id => $webvar{id}) );
[277]920 if ($code eq 'OK') {
921 changepage(page => "reclist", id => $webvar{id}, defrec => $webvar{defrec}, revrec => $webvar{revrec},
922 resultmsg => "SOA record updated");
923 } else {
[2]924 $page->param(update_failed => 1);
[311]925 $page->param(msg => $msg);
926 fillsoa($webvar{defrec}, $webvar{revrec}, $webvar{id}, 'w');
[277]927 }
928
[17]929} elsif ($webvar{page} eq 'grpman') {
[2]930
[22]931 listgroups();
[140]932
933# Permissions!
934 $page->param(addgrp => $permissions{admin} || $permissions{group_create});
935 $page->param(edgrp => $permissions{admin} || $permissions{group_edit});
936 $page->param(delgrp => $permissions{admin} || $permissions{group_delete});
937
[376]938 show_msgs();
[18]939 $page->param(curpage => $webvar{page});
940
[17]941} elsif ($webvar{page} eq 'newgrp') {
[20]942
[179]943 changepage(page => "grpman", errmsg => "You are not permitted to add groups")
944 unless ($permissions{admin} || $permissions{group_create});
[111]945
[207]946 # do.. uhh.. stuff.. if we have no webvar{grpaction}
947 if ($webvar{grpaction} && $webvar{grpaction} eq 'add') {
[179]948
949 # security check - does the user have permission to access this entity?
950 if (!check_scope(id => $webvar{pargroup}, type => 'group')) {
951 changepage(page => "grpman", errmsg => "You are not permitted to add a group to the requested parent group");
952 }
953
[66]954 my %newperms;
[179]955 my $alterperms = 0;
[66]956 foreach (@permtypes) {
957 $newperms{$_} = 0;
[179]958 if ($permissions{admin} || $permissions{$_}) {
959 $newperms{$_} = (defined($webvar{$_}) && $webvar{$_} eq 'on' ? 1 : 0);
960 } else {
961 $alterperms = 1;
962 }
[66]963 }
[392]964 # "Chained" permissions. Some permissions imply others; make sure they get set.
965 foreach (keys %permchains) {
966 if ($newperms{$_} && !$newperms{$permchains{$_}}) {
967 $newperms{$permchains{$_}} = 1;
968 }
969 }
[431]970 # force inheritance of parent group's default records with inherit flag,
971 # otherwise we end up with the hardcoded defaults from DNSDB.pm. See
972 # https://secure.deepnet.cx/trac/dnsadmin/ticket/8 for the UI enhancement
973 # that will make this variable.
[476]974 my ($code,$msg) = $dnsdb->addGroup($webvar{newgroup}, $webvar{pargroup}, \%newperms, 1);
[57]975 if ($code eq 'OK') {
[179]976 if ($alterperms) {
977 changepage(page => "grpman", warnmsg =>
978 "You can only grant permissions you hold. New group $webvar{newgroup} added with reduced access.");
979 } else {
980 changepage(page => "grpman", resultmsg => "Added group $webvar{newgroup}");
981 }
[187]982 } # fallthrough else
[66]983 # no point in doing extra work
984 fill_permissions($page, \%newperms);
[18]985 $page->param(add_failed => 1);
986 $page->param(errmsg => $msg);
987 $page->param(newgroup => $webvar{newgroup});
[66]988 fill_grouplist('pargroup',$webvar{pargroup});
[19]989 } else {
[66]990 fill_grouplist('pargroup',$curgroup);
[88]991 # fill default permissions with immediate parent's current ones
[66]992 my %parperms;
[474]993 $dnsdb->getPermissions('group', $curgroup, \%parperms);
[66]994 fill_permissions($page, \%parperms);
[18]995 }
[20]996
[22]997} elsif ($webvar{page} eq 'delgrp') {
[20]998
[111]999 changepage(page => "grpman", errmsg => "You are not permitted to delete groups", id => $webvar{parentid})
1000 unless ($permissions{admin} || $permissions{group_delete});
1001
[179]1002 # security check - does the user have permission to access this entity?
1003 if (!check_scope(id => $webvar{id}, type => 'group')) {
1004 changepage(page => "grpman", errmsg => "You are not permitted to delete the requested group");
1005 }
1006
[20]1007 $page->param(id => $webvar{id});
1008 # first pass = confirm y/n (sorta)
1009 if (!defined($webvar{del})) {
1010 $page->param(del_getconf => 1);
[140]1011
1012##fixme
1013# do a check for "group has stuff in it", and splatter a big warning
1014# up along with an unchecked-by-default check box to YES DAMMIT DELETE THE WHOLE THING
1015
[20]1016 } elsif ($webvar{del} eq 'ok') {
[476]1017 my ($code,$msg) = $dnsdb->delGroup($webvar{id});
[187]1018 if ($code eq 'OK') {
[57]1019##fixme: need to clean up log when deleting a major container
[293]1020 changepage(page => "grpman", resultmsg => $msg);
[187]1021 } else {
1022# need to find failure mode
[293]1023 changepage(page => "grpman", errmsg => $msg);
[20]1024 }
1025 } else {
1026 # cancelled. whee!
1027 changepage(page => "grpman");
1028 }
[473]1029 $page->param(delgroupname => $dnsdb->groupName($webvar{id}));
[24]1030
[65]1031} elsif ($webvar{page} eq 'edgroup') {
1032
[140]1033 changepage(page => "grpman", errmsg => "You are not permitted to edit groups")
[111]1034 unless ($permissions{admin} || $permissions{group_edit});
1035
[179]1036 # security check - does the user have permission to access this entity?
1037 if (!check_scope(id => $webvar{gid}, type => 'group')) {
1038 changepage(page => "grpman", errmsg => "You are not permitted to edit the requested group");
1039 }
1040
[291]1041 if ($webvar{grpaction} && $webvar{grpaction} eq 'updperms') {
[65]1042 # extra safety check; make sure user can't construct a URL to bypass ACLs
1043 my %curperms;
[474]1044 $dnsdb->getPermissions('group', $webvar{gid}, \%curperms);
[66]1045 my %chperms;
[178]1046 my $alterperms = 0;
[66]1047 foreach (@permtypes) {
[65]1048 $webvar{$_} = 0 if !defined($webvar{$_});
1049 $webvar{$_} = 1 if $webvar{$_} eq 'on';
[178]1050 if ($permissions{admin} || $permissions{$_}) {
1051 $chperms{$_} = $webvar{$_} if $curperms{$_} ne $webvar{$_};
1052 } else {
1053 $alterperms = 1;
1054 $chperms{$_} = 0;
1055 }
[65]1056 }
[390]1057 # "Chained" permissions. Some permissions imply others; make sure they get set.
1058 foreach (keys %permchains) {
1059 if ($chperms{$_} && !$chperms{$permchains{$_}}) {
1060 $chperms{$permchains{$_}} = 1;
1061 }
1062 }
[474]1063 my ($code,$msg) = $dnsdb->changePermissions('group', $webvar{gid}, \%chperms);
[66]1064 if ($code eq 'OK') {
[178]1065 if ($alterperms) {
1066 changepage(page => "grpman", warnmsg =>
1067 "You can only grant permissions you hold. Default permissions in group ".
[473]1068 $dnsdb->groupName($webvar{gid})." updated with reduced access");
[178]1069 } else {
[294]1070 changepage(page => "grpman", resultmsg => $msg);
[178]1071 }
[187]1072 } # fallthrough else
[66]1073 # no point in doing extra work
1074 fill_permissions($page, \%chperms);
1075 $page->param(errmsg => $msg);
[65]1076 }
1077 $page->param(gid => $webvar{gid});
[473]1078 $page->param(grpmeddle => $dnsdb->groupName($webvar{gid}));
[65]1079 my %grpperms;
[474]1080 $dnsdb->getPermissions('group', $webvar{gid}, \%grpperms);
[66]1081 fill_permissions($page, \%grpperms);
[65]1082
[638]1083} elsif ($webvar{page} eq 'bulkdomain' || $webvar{page} eq 'bulkrev') {
[110]1084 # Bulk operations on domains. Note all but group move are available on the domain list.
1085
[638]1086 changepage(page => "domlist", errmsg => "You are not permitted to make bulk zone changes")
[111]1087 unless ($permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
[110]1088
[126]1089 fill_grouplist("grouplist");
1090
[638]1091 $page->param(fwdzone => $webvar{page} eq 'bulkdomain');
[110]1092
[638]1093 my $count = $dnsdb->getZoneCount(revrec => ($webvar{page} eq 'bulkdomain' ? 'n' : 'y'),
1094 curgroup => $curgroup);
1095
[110]1096 $page->param(curpage => $webvar{page});
[473]1097 fill_pgcount($count, 'domains', $dnsdb->groupName($curgroup));
[110]1098 fill_fpnla($count);
[112]1099 $page->param(perpage => $perpage);
[110]1100
[638]1101 my $domlist = $dnsdb->getZoneList(revrec => ($webvar{page} eq 'bulkdomain' ? 'n' : 'y'),
1102 curgroup => $curgroup, offset => $offset);
[110]1103 my $rownum = 0;
[313]1104 foreach my $dom (@{$domlist}) {
1105 delete $dom->{status};
1106 delete $dom->{group};
[638]1107 $dom->{newrow} = (++$rownum) % 5 == 0 && $rownum != $perpage;
[110]1108 }
[313]1109
1110 $page->param(domtable => $domlist);
[112]1111 # ACLs
[110]1112 $page->param(maymove => ($permissions{admin} || ($permissions{domain_edit} && $permissions{domain_create} && $permissions{domain_delete})));
1113 $page->param(maystatus => $permissions{admin} || $permissions{domain_edit});
1114 $page->param(maydelete => $permissions{admin} || $permissions{domain_delete});
1115
[640]1116#} elsif ($webvar{page} eq 'confirmbulkdom' || $webvar{page} eq 'confirmbulkrev') {
1117} elsif ($webvar{page} eq 'confirmbulk') {
1118
1119 changepage(page => "domlist", errmsg => "You are not permitted to make bulk zone changes")
1120 unless ($permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
1121
1122 $page->param(bulkaction => $webvar{bulkaction});
1123 $page->param(destgroup => $webvar{destgroup});
1124 my @zlist;
1125 my $rownum = 0;
1126
1127##fixme: this could probably be made more efficient, since this looks up 2 zone names for
1128# each comparison during sort rather than slurping them in bulk once before doing the sort
1129 # sort zones by zone name, not ID
1130 sub zsort {
1131 my $tmpa = ($a =~ /^dom/ ? $dnsdb->domainName($webvar{$a}) : $dnsdb->revName($webvar{$a}) );
1132 my $tmpb = ($b =~ /^dom/ ? $dnsdb->domainName($webvar{$b}) : $dnsdb->revName($webvar{$b}) );
1133 return $tmpa cmp $tmpb;
1134 }
1135 # eugh. can't see a handy way to sort this mess by zone name the way it is on the submitting page. :(
1136 foreach my $input (sort zsort grep(/^(?:dom|rev)_/, keys %webvar) ) {
1137 next unless $input =~ /^(dom|rev)_\d+$/;
1138 my $fr = $1;
1139 my %row = (zoneid => $webvar{$input},
1140 zone => ($fr eq 'dom' ? $dnsdb->domainName($webvar{$input}) : $dnsdb->revName($webvar{$input}) ),
1141 zvarname => $input,
1142 newrow => ( (++$rownum) % 5 == 0 && $rownum != $perpage),
1143 );
1144 push @zlist, \%row;
1145 }
1146 $page->param(domtable => \@zlist);
1147
[112]1148} elsif ($webvar{page} eq 'bulkchange') {
[110]1149
[155]1150 # security check - does the user have permission to access this entity?
[169]1151 if (!check_scope(id => $webvar{destgroup}, type => 'group')) {
[155]1152 $page->param(errmsg => "You are not permitted to make bulk changes in the requested group");
1153 goto DONEBULK;
1154 }
1155
[640]1156 # skip the changes if user did not confirm
1157 my $wasrev = grep /^rev_/, keys %webvar;
1158 changepage(page => ($wasrev ? "bulkrev" : "bulkdomain")) unless $webvar{okdel} eq 'y';
1159
1160 changepage(page => "domlist", errmsg => "You are not permitted to make bulk zone changes")
1161 unless ($permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
1162
[295]1163 # per-action scope checks
[207]1164 if ($webvar{bulkaction} eq 'move') {
[640]1165 changepage(page => "domlist", errmsg => "You are not permitted to bulk-move zones")
[112]1166 unless ($permissions{admin} || ($permissions{domain_edit} && $permissions{domain_create} && $permissions{domain_delete}));
[473]1167 my $newgname = $dnsdb->groupName($webvar{destgroup});
[114]1168 $page->param(action => "Move to group $newgname");
[207]1169 } elsif ($webvar{bulkaction} eq 'deactivate' || $webvar{bulkaction} eq 'activate') {
[640]1170 changepage(page => "domlist", errmsg => "You are not permitted to bulk-$webvar{bulkaction} zones")
[112]1171 unless ($permissions{admin} || $permissions{domain_edit});
[640]1172 $page->param(action => "$webvar{bulkaction} zones");
[207]1173 } elsif ($webvar{bulkaction} eq 'delete') {
[640]1174 changepage(page => "domlist", errmsg => "You are not permitted to bulk-delete zones")
[112]1175 unless ($permissions{admin} || $permissions{domain_delete});
[640]1176 $page->param(action => "$webvar{bulkaction} zones");
[295]1177 } else {
1178 # unknown action, bypass actually doing anything. it should not be possible in
1179 # normal operations, and anyone who meddles with the URL gets what they deserve.
1180 goto DONEBULK;
1181 } # move/(de)activate/delete if()
1182
1183 my @bulkresults;
1184 # nngh. due to alpha-sorting on the previous page, we can't use domid-numeric
1185 # order here, and since we don't have the domain names until we go around this
1186 # loop, we can't alpha-sort them here. :(
[638]1187 foreach my $input (keys %webvar) {
[295]1188 my %row;
[638]1189 next unless $input =~ /^(dom|rev)_\d+$/;
1190 my $fr = $1;
[295]1191 # second security check - does the user have permission to meddle with this domain?
[638]1192 if (!check_scope(id => $webvar{$input}, type => ($fr eq 'dom' ? 'domain' : 'revzone'))) {
1193 $row{domerr} = "You are not permitted to make changes to the requested zone";
1194 $row{domain} = $webvar{$input};
[114]1195 push @bulkresults, \%row;
[295]1196 next;
[114]1197 }
[638]1198 $row{domain} = ($fr eq 'dom' ? $dnsdb->domainName($webvar{$input}) : $dnsdb->revName($webvar{$input}));
[114]1199
[295]1200 # Do the $webvar{bulkaction}
1201 my ($code, $msg);
[638]1202 ($code, $msg) = $dnsdb->changeGroup(($fr eq 'dom' ? 'domain' : 'revzone'), $webvar{$input}, $webvar{destgroup})
[295]1203 if $webvar{bulkaction} eq 'move';
1204 if ($webvar{bulkaction} eq 'deactivate' || $webvar{bulkaction} eq 'activate') {
[638]1205 my $stat = $dnsdb->zoneStatus($webvar{$input}, ($fr eq 'dom' ? 'n' : 'y'),
1206 ($webvar{bulkaction} eq 'activate' ? 'domon' : 'domoff'));
[295]1207 $code = (defined($stat) ? 'OK' : 'FAIL');
1208 $msg = (defined($stat) ? $DNSDB::resultstr : $DNSDB::errstr);
1209 }
[638]1210 ($code, $msg) = $dnsdb->delZone($webvar{$input}, ($fr eq 'dom' ? 'n' : 'y'))
[295]1211 if $webvar{bulkaction} eq 'delete';
[114]1212
[295]1213 # Set the result output from the action
1214 if ($code eq 'OK') {
1215 $row{domok} = $msg;
1216 } elsif ($code eq 'WARN') {
1217 $row{domwarn} = $msg;
1218 } else {
1219 $row{domerr} = $msg;
1220 }
1221 push @bulkresults, \%row;
[112]1222
[295]1223 } # foreach (keys %webvar)
1224 $page->param(bulkresults => \@bulkresults);
1225
[155]1226 # Yes, this is a GOTO target. PTHBTTT.
1227 DONEBULK: ;
1228
[24]1229} elsif ($webvar{page} eq 'useradmin') {
1230
[139]1231 if (defined($webvar{userstatus})) {
[153]1232 # security check - does the user have permission to access this entity?
1233 my $flag = 0;
1234 foreach (@viewablegroups) {
[470]1235 $flag = 1 if $dnsdb->isParent($_, 'group', $webvar{id}, 'user');
[153]1236 }
[319]1237 if ($flag && ($permissions{admin} || $permissions{user_edit} ||
1238 ($permissions{self_edit} && $webvar{id} == $session->param('uid')) )) {
[479]1239 my $stat = $dnsdb->userStatus($webvar{id}, $webvar{userstatus});
[641]1240 # kick user out if user disabled self
1241 # arguably there should be a more specific error message for this case
1242 changepage(page=> 'login', sessexpired => 1) if $webvar{id} == $session->param('uid');
[296]1243 $page->param(resultmsg => $DNSDB::resultstr);
[153]1244 } else {
1245 $page->param(errmsg => "You are not permitted to view or change the requested user");
1246 }
[188]1247 $uri_self =~ s/\&amp;userstatus=[^&]*//g; # clean up URL for stuffing into templates
[51]1248 }
1249
[142]1250 list_users();
1251
1252# Permissions!
1253 $page->param(adduser => $permissions{admin} || $permissions{user_create});
1254# should we block viewing other users? Vega blocks "editing"...
1255# NB: no "edit self" link as with groups here. maybe there should be?
1256# $page->param(eduser => $permissions{admin} || $permissions{user_edit});
1257 $page->param(deluser => $permissions{admin} || $permissions{user_delete});
1258
[376]1259 show_msgs();
[24]1260 $page->param(curpage => $webvar{page});
1261
[67]1262} elsif ($webvar{page} eq 'user') {
1263
[111]1264 # All user add/edit actions fall through the same page, since there aren't
1265 # really any hard differences between the templates
1266
[83]1267 #fill_actypelist($webvar{accttype});
[67]1268 fill_clonemelist();
1269 my %grpperms;
[474]1270 $dnsdb->getPermissions('group', $curgroup, \%grpperms);
[83]1271
[67]1272 my $grppermlist = new HTML::Template(filename => "$templatedir/permlist.tmpl");
1273 my %noaccess;
1274 fill_permissions($grppermlist, \%grpperms, \%noaccess);
1275 $grppermlist->param(info => 1);
1276 $page->param(grpperms => $grppermlist->output);
[83]1277
[67]1278 $page->param(is_admin => $permissions{admin});
1279
[207]1280 $webvar{useraction} = '' if !$webvar{useraction};
[88]1281
[207]1282 if ($webvar{useraction} eq 'add' or $webvar{useraction} eq 'update') {
[67]1283
[207]1284 $page->param(add => 1) if $webvar{useraction} eq 'add';
[83]1285
[294]1286 # can't re-use $code and $msg for update if we want to be able to identify separate failure states
1287 my ($code,$code2,$msg,$msg2) = ('OK','OK','OK','OK');
[67]1288
1289 my $alterperms = 0; # flag iff we need to force custom permissions due to user's current access limits
1290
[87]1291 my %newperms; # we're going to prefill the existing permissions, so we can change them.
[474]1292 $dnsdb->getPermissions('user', $webvar{uid}, \%newperms);
[87]1293
[67]1294 if ($webvar{pass1} ne $webvar{pass2}) {
1295 $code = 'FAIL';
1296 $msg = "Passwords don't match";
1297 } else {
1298
[592]1299 my $permstring = 'i'; # start with "inherit"
[67]1300
[592]1301 # Remap passed checkbox states from webvar to integer/boolean values in %newperms
1302 foreach (@permtypes) {
1303 $newperms{$_} = (defined($webvar{$_}) && $webvar{$_} eq 'on' ? 1 : 0);
1304 }
[83]1305
[592]1306 # Check for chained permissions. Some permissions imply others; make sure they get set.
1307 foreach (keys %permchains) {
1308 if ($newperms{$_} && !$newperms{$permchains{$_}}) {
1309 $newperms{$permchains{$_}} = 1;
1310 }
1311 }
1312
1313 # check for possible priviledge escalations
[83]1314 if (!$permissions{admin}) {
[592]1315 if ($webvar{perms_type} eq 'inherit') {
1316 # Group permissions are only relevant if inheriting
1317 my %grpperms;
1318 $dnsdb->getPermissions('group', $curgroup, \%grpperms);
1319 my $ret = $dnsdb->comparePermissions(\%permissions, \%grpperms);
1320 if ($ret eq '<' || $ret eq '!') {
1321 # User's permissions are not a superset or equivalent to group. Can't inherit
1322 # (and include access user doesn't currently have), so we force custom.
1323 $webvar{perms_type} = 'custom';
1324 $alterperms = 1;
1325 }
1326 }
1327 my $ret = $dnsdb->comparePermissions(\%newperms, \%permissions);
1328 if ($ret eq '>' || $ret eq '!') {
1329 # User's new permissions are not a subset or equivalent to previous. Can't add
1330 # permissions user doesn't currently have, so we force custom.
[83]1331 $webvar{perms_type} = 'custom';
1332 $alterperms = 1;
1333 }
1334 }
1335
[592]1336##fixme:
1337# could possibly factor building the meat of the permstring out of this if/elsif set, so
1338# as to avoid running around @permtypes quite so many times
[67]1339 if ($webvar{perms_type} eq 'custom') {
1340 $permstring = 'C:';
1341 foreach (@permtypes) {
[87]1342 if ($permissions{admin} || $permissions{$_}) {
[67]1343 $permstring .= ",$_" if defined($webvar{$_}) && $webvar{$_} eq 'on';
[592]1344 } else {
1345 $newperms{$_} = 0; # remove permissions user doesn't currently have
[67]1346 }
1347 }
1348 $page->param(perm_custom => 1);
1349 } elsif ($permissions{admin} && $webvar{perms_type} eq 'clone') {
1350 $permstring = "c:$webvar{clonesrc}";
[474]1351 $dnsdb->getPermissions('user', $webvar{clonesrc}, \%newperms);
[67]1352 $page->param(perm_clone => 1);
1353 }
[592]1354 # Recheck chained permissions, in the supposedly impossible case that the removals
1355 # above mangled one of them. This *should* be impossible via normal web UI operations.
[390]1356 foreach (keys %permchains) {
1357 if ($newperms{$_} && !$newperms{$permchains{$_}}) {
1358 $newperms{$permchains{$_}} = 1;
1359 $permstring .= ",$permchains{$_}";
1360 }
1361 }
[207]1362 if ($webvar{useraction} eq 'add') {
[144]1363 changepage(page => "useradmin", errmsg => "You do not have permission to add new users")
1364 unless $permissions{admin} || $permissions{user_create};
[181]1365 # no scope check; user is created in the current group
[479]1366 ($code,$msg) = $dnsdb->addUser($webvar{uname}, $curgroup, $webvar{pass1},
[83]1367 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype}, $permstring,
1368 $webvar{fname}, $webvar{lname}, $webvar{phone});
1369 } else {
[144]1370 changepage(page => "useradmin", errmsg => "You do not have permission to edit users")
[319]1371 unless $permissions{admin} || $permissions{user_edit} ||
1372 ($permissions{self_edit} && $session->param('uid') == $webvar{uid});
[181]1373 # security check - does the user have permission to access this entity?
1374 if (!check_scope(id => $webvar{user}, type => 'user')) {
1375 changepage(page => "useradmin", errmsg => "You do not have permission to edit the requested user");
1376 }
[294]1377# User update is icky. I'd really like to do this in one atomic operation,
1378# but that gets hairy by either duplicating a **lot** of code in DNSDB.pm
1379# or self-torture trying to not commit the transaction until we're really done.
[83]1380 # Allowing for changing group, but not coding web support just yet.
[479]1381 ($code,$msg) = $dnsdb->updateUser($webvar{uid}, $webvar{uname}, $webvar{gid}, $webvar{pass1},
[83]1382 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
1383 $webvar{fname}, $webvar{lname}, $webvar{phone});
1384 if ($code eq 'OK') {
[592]1385 $newperms{admin} = 1 if $permissions{admin} && $webvar{accttype} eq 'S';
[474]1386 ($code2,$msg2) = $dnsdb->changePermissions('user', $webvar{uid}, \%newperms, ($permstring eq 'i'));
[83]1387 }
1388 }
[67]1389 }
1390
[294]1391 if ($code eq 'OK' && $code2 eq 'OK') {
1392 my %pageparams = (page => "useradmin");
[67]1393 if ($alterperms) {
[294]1394 $pageparams{warnmsg} = "You can only grant permissions you hold.\nUser ".
1395 ($webvar{useraction} eq 'add' ? "$webvar{uname} added" : "info updated for $webvar{uname}").
1396 ".\nPermissions ".($webvar{useraction} eq 'add' ? 'added' : 'updated')." with reduced access.";
[67]1397 } else {
[294]1398 $pageparams{resultmsg} = "$msg".($webvar{useraction} eq 'add' ? '' : "\n$msg2");
[67]1399 }
[294]1400 changepage(%pageparams);
[83]1401
1402 # add/update failed:
[67]1403 } else {
1404 $page->param(add_failed => 1);
[207]1405 $page->param(action => $webvar{useraction});
[83]1406 $page->param(set_permgroup => 1);
[87]1407 if ($webvar{perms_type} eq 'inherit') { # set permission class radio
1408 $page->param(perm_inherit => 1);
1409 } elsif ($webvar{perms_type} eq 'clone') {
1410 $page->param(perm_clone => 1);
1411 } else {
1412 $page->param(perm_custom => 1);
1413 }
[67]1414 $page->param(uname => $webvar{uname});
1415 $page->param(fname => $webvar{fname});
1416 $page->param(lname => $webvar{lname});
1417 $page->param(pass1 => $webvar{pass1});
1418 $page->param(pass2 => $webvar{pass2});
[294]1419 $page->param(errmsg => "User info updated but permissions update failed: $msg2") if $code eq 'OK';
1420 $page->param(errmsg => $msg) if $code ne 'OK';
[83]1421 fill_permissions($page, \%newperms);
1422 fill_actypelist($webvar{accttype});
[67]1423 fill_clonemelist();
1424 }
1425
[207]1426 } elsif ($webvar{useraction} eq 'edit') {
[83]1427
[144]1428 changepage(page => "useradmin", errmsg => "You do not have permission to edit users")
[319]1429 unless $permissions{admin} || $permissions{user_edit} ||
1430 ($permissions{self_edit} && $session->param('uid') == $webvar{user});
[144]1431
[181]1432 # security check - does the user have permission to access this entity?
1433 if (!check_scope(id => $webvar{user}, type => 'user')) {
1434 changepage(page => "useradmin", errmsg => "You do not have permission to edit the requested user");
1435 }
1436
[83]1437 $page->param(set_permgroup => 1);
1438 $page->param(action => 'update');
1439 $page->param(uid => $webvar{user});
1440 fill_clonemelist();
1441
[479]1442 my $userinfo = $dnsdb->getUserData($webvar{user});
[83]1443 fill_actypelist($userinfo->{type});
1444 # not using this yet, but adding it now means we can *much* more easily do so later.
[294]1445 $page->param(gid => $userinfo->{group_id});
[83]1446
1447 my %curperms;
[474]1448 $dnsdb->getPermissions('user', $webvar{user}, \%curperms);
[83]1449 fill_permissions($page, \%curperms);
1450
1451 $page->param(uname => $userinfo->{username});
1452 $page->param(fname => $userinfo->{firstname});
1453 $page->param(lname => $userinfo->{lastname});
[87]1454 $page->param(set_permgroup => 1);
[83]1455 if ($userinfo->{inherit_perm}) {
1456 $page->param(perm_inherit => 1);
1457 } else {
1458 $page->param(perm_custom => 1);
1459 }
[67]1460 } else {
[144]1461 changepage(page => "useradmin", errmsg => "You are not allowed to add new users")
1462 unless $permissions{admin} || $permissions{user_create};
[67]1463 # default is "new"
[83]1464 $page->param(add => 1);
1465 $page->param(action => 'add');
1466 fill_permissions($page, \%grpperms);
1467 fill_actypelist();
[67]1468 }
1469
[90]1470} elsif ($webvar{page} eq 'deluser') {
1471
[145]1472 changepage(page=> "useradmin", errmsg => "You are not allowed to delete users")
1473 unless $permissions{admin} || $permissions{user_delete};
1474
[181]1475 # security check - does the user have permission to access this entity?
1476 if (!check_scope(id => $webvar{id}, type => 'user')) {
1477 changepage(page => "useradmin", errmsg => "You are not permitted to delete the requested user");
1478 }
1479
[90]1480 $page->param(id => $webvar{id});
1481 # first pass = confirm y/n (sorta)
1482 if (!defined($webvar{del})) {
1483 $page->param(del_getconf => 1);
[473]1484 $page->param(user => $dnsdb->userFullName($webvar{id}));
[90]1485 } elsif ($webvar{del} eq 'ok') {
[479]1486 my ($code,$msg) = $dnsdb->delUser($webvar{id});
[187]1487 if ($code eq 'OK') {
[90]1488 # success. go back to the user list, do not pass "GO"
[297]1489 changepage(page => "useradmin", resultmsg => $msg);
[187]1490 } else {
[297]1491 changepage(page => "useradmin", errmsg => $msg);
[90]1492 }
1493 } else {
1494 # cancelled. whee!
1495 changepage(page => "useradmin");
1496 }
1497
[370]1498} elsif ($webvar{page} eq 'loclist') {
1499
[374]1500 changepage(page => "domlist", errmsg => "You are not allowed access to this function")
1501 unless $permissions{admin} || $permissions{location_view};
[370]1502
1503 # security check - does the user have permission to access this entity?
1504# if (!check_scope(id => $webvar{id}, type => 'loc')) {
1505# changepage(page => "loclist", errmsg => "You are not permitted to <foo> the requested location/view");
1506# }
1507 list_locations();
[376]1508 show_msgs();
[370]1509
1510# Permissions!
[374]1511 $page->param(addloc => $permissions{admin} || $permissions{location_create});
1512 $page->param(delloc => $permissions{admin} || $permissions{location_delete});
[370]1513
1514} elsif ($webvar{page} eq 'location') {
1515
[374]1516 changepage(page => "domlist", errmsg => "You are not allowed access to this function")
1517 unless $permissions{admin} || $permissions{location_view};
[370]1518
[374]1519 # security check - does the user have permission to access this entity?
1520# if (!check_scope(id => $webvar{id}, type => 'loc')) {
1521# changepage(page => "loclist", errmsg => "You are not permitted to <foo> the requested location/view");
1522# }
1523
[428]1524 $webvar{locact} = '' if !$webvar{locact};
1525
1526 if ($webvar{locact} eq 'add') {
[374]1527 changepage(page => "loclist", errmsg => "You are not permitted to add locations/views", id => $webvar{parentid})
1528 unless ($permissions{admin} || $permissions{location_create});
1529
[709]1530 my ($code,$msg) = $dnsdb->addLoc(group => $curgroup, desc => $webvar{locname},
1531 comments => $webvar{comments}, iplist => $webvar{iplist});
[374]1532
1533 if ($code eq 'OK' || $code eq 'WARN') {
1534 my %pageparams = (page => "loclist", id => $webvar{parentid},
1535 defrec => $webvar{defrec}, revrec => $webvar{revrec});
[628]1536 $pageparams{warnmsg} = $msg."<br />\n".$DNSDB::resultstr if $code eq 'WARN';
[374]1537 $pageparams{resultmsg} = $DNSDB::resultstr if $code eq 'OK';
1538 changepage(%pageparams);
1539 } else {
1540 $page->param(failed => 1);
1541 $page->param(errmsg => $msg);
1542 $page->param(wastrying => "adding");
1543 $page->param(todo => "Add location/view");
1544 $page->param(locact => "add");
1545 $page->param(id => $webvar{id});
[375]1546 $page->param(locname => $webvar{locname});
1547 $page->param(comments => $webvar{comments});
1548 $page->param(iplist => $webvar{iplist});
[374]1549 }
1550
1551 } elsif ($webvar{locact} eq 'edit') {
1552 changepage(page => "loclist", errmsg => "You are not permitted to edit locations/views", id => $webvar{parentid})
1553 unless ($permissions{admin} || $permissions{location_edit});
[375]1554
[480]1555 my $loc = $dnsdb->getLoc($webvar{loc});
[377]1556 $page->param(wastrying => "editing");
[375]1557 $page->param(todo => "Edit location/view");
1558 $page->param(locact => "update");
1559 $page->param(id => $webvar{loc});
1560 $page->param(locname => $loc->{description});
1561 $page->param(comments => $loc->{comments});
1562 $page->param(iplist => $loc->{iplist});
1563
[374]1564 } elsif ($webvar{locact} eq 'update') {
1565 changepage(page => "loclist", errmsg => "You are not permitted to edit locations/views", id => $webvar{parentid})
1566 unless ($permissions{admin} || $permissions{location_edit});
[377]1567
[480]1568 my ($code,$msg) = $dnsdb->updateLoc($webvar{id}, $curgroup, $webvar{locname}, $webvar{comments}, $webvar{iplist});
[377]1569
1570 if ($code eq 'OK') {
1571 changepage(page => "loclist", resultmsg => $msg);
1572 } else {
1573 $page->param(failed => 1);
1574 $page->param(errmsg => $msg);
1575 $page->param(wastrying => "editing");
1576 $page->param(todo => "Edit location/view");
1577 $page->param(locact => "update");
1578 $page->param(id => $webvar{loc});
1579 $page->param(locname => $webvar{locname});
1580 $page->param(comments => $webvar{comments});
1581 $page->param(iplist => $webvar{iplist});
1582 }
[374]1583 } else {
1584 changepage(page => "loclist", errmsg => "You are not permitted to add locations/views", id => $webvar{parentid})
1585 unless ($permissions{admin} || $permissions{location_create});
1586
1587 $page->param(todo => "Add location/view");
1588 $page->param(locact => "add");
1589 $page->param(locname => ($webvar{locname} ? $webvar{locname} : ''));
1590 $page->param(iplist => ($webvar{iplist} ? $webvar{iplist} : ''));
[376]1591
1592 show_msgs();
[374]1593 }
1594
[428]1595} elsif ($webvar{page} eq 'delloc') {
1596
1597 changepage(page=> "loclist", errmsg => "You are not allowed to delete locations")
1598 unless $permissions{admin} || $permissions{location_delete};
1599
1600 # security check - does the user have permission to access this entity?
1601# if (!check_scope(id => $webvar{id}, type => 'loc')) {
1602# changepage(page => "loclist", errmsg => "You are not permitted to <foo> the requested location/view");
1603# }
1604
1605 $page->param(locid => $webvar{locid});
[480]1606 my $locdata = $dnsdb->getLoc($webvar{locid});
[428]1607 $locdata->{description} = $webvar{locid} if !$locdata->{description};
1608 # first pass = confirm y/n (sorta)
1609 if (!defined($webvar{del})) {
1610 $page->param(del_getconf => 1);
1611 $page->param(location => $locdata->{description});
1612 } elsif ($webvar{del} eq 'ok') {
[480]1613 my ($code,$msg) = $dnsdb->delLoc($webvar{locid});
[428]1614 if ($code eq 'OK') {
1615 # success. go back to the user list, do not pass "GO"
1616 changepage(page => "loclist", resultmsg => $msg);
1617 } else {
1618 changepage(page => "loclist", errmsg => $msg);
1619 }
1620 } else {
1621 # cancelled. whee!
1622 changepage(page => "loclist");
1623 }
1624
[30]1625} elsif ($webvar{page} eq 'dnsq') {
1626
[702]1627 if ($webvar{qfor}) {
1628 $webvar{qfor} =~ s/^\s*//;
1629 $webvar{qfor} =~ s/\s*$//;
1630 $page->param(qfor => $webvar{qfor});
1631 }
1632 if ($webvar{resolver}) {
1633 $webvar{resolver} =~ s/^\s*//;
1634 $webvar{resolver} =~ s/\s*$//;
1635 $page->param(resolver => $webvar{resolver});
1636 }
[483]1637 $page->param(typelist => $dnsdb->getTypelist('l', ($webvar{type} ? $webvar{type} : undef)));
[31]1638 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]1639
1640 if ($webvar{qfor}) {
1641 my $resolv = Net::DNS::Resolver->new;
[31]1642 $resolv->tcp_timeout(5); # make me adjustable!
1643 $resolv->udp_timeout(5); # make me adjustable!
1644 $resolv->recurse(0) if $webvar{nrecurse};
1645 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
[30]1646 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
1647 if ($query) {
1648
1649 $page->param(showresults => 1);
1650
1651 my @answer;
1652 foreach my $rr ($query->answer) {
1653# next unless $rr->type eq "A" or $rr->type eq 'NS';
1654 my %row;
1655 my ($host,$ttl,$class,$type,$data) =
[31]1656 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]1657 $row{host} = $host;
1658 $row{ftype} = $type;
[31]1659 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]1660 push @answer, \%row;
1661 }
1662 $page->param(answer => \@answer);
1663
1664 my @additional;
1665 foreach my $rr ($query->additional) {
1666# next unless $rr->type eq "A" or $rr->type eq 'NS';
1667 my %row;
1668 my ($host,$ttl,$class,$type,$data) =
1669 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
1670 $row{host} = $host;
1671 $row{ftype} = $type;
1672 $row{rdata} = $data;
1673 push @additional, \%row;
1674 }
1675 $page->param(additional => \@additional);
1676
1677 my @authority;
1678 foreach my $rr ($query->authority) {
1679# next unless $rr->type eq "A" or $rr->type eq 'NS';
1680 my %row;
1681 my ($host,$ttl,$class,$type,$data) =
1682 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
1683 $row{host} = $host;
1684 $row{ftype} = $type;
1685 $row{rdata} = $data;
1686 push @authority, \%row;
1687 }
1688 $page->param(authority => \@authority);
1689
1690 $page->param(usedresolver => $resolv->answerfrom);
1691 $page->param(frtype => $typemap{$webvar{type}});
1692
1693 } else {
1694 $page->param(errmsg => $resolv->errorstring);
1695 }
1696 }
1697 ## done DNS query
1698
[31]1699} elsif ($webvar{page} eq 'axfr') {
1700
[111]1701 changepage(page => "domlist", errmsg => "You are not permitted to import domains")
1702 unless ($permissions{admin} || $permissions{domain_create});
1703
[31]1704 # don't need this while we've got the dropdown in the menu. hmm.
[126]1705 fill_grouplist("grouplist");
[31]1706
1707 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
1708 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
1709 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
[436]1710 $page->param(forcettl => $webvar{forcettl}) if $webvar{forcettl};
1711 $page->param(newttl => $webvar{newttl}) if $webvar{newttl};
[308]1712 # This next one is arguably better on by default, but Breaking Things Is Bad, Mmmkay?
1713 $page->param(mergematching => $webvar{mergematching}) if $webvar{mergematching};
[37]1714 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]1715 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
[33]1716
[91]1717 # shut up warning about uninitialized variable
1718 $webvar{doit} = '' if !defined($webvar{doit});
1719
[33]1720 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
1721 $page->param(errmsg => "Need to set host to import from");
1722 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
1723 $page->param(errmsg => "Need domains to import");
[91]1724 } elsif ($webvar{doit} eq 'y') {
[162]1725
1726 # security check - does the user have permission to access this entity?
[169]1727 if (!check_scope(id => $webvar{group}, type => 'group')) {
[162]1728 $page->param(errmsg => "You are not permitted to import domains into the requested group");
1729 goto DONEAXFR;
1730 }
1731
[308]1732 # Bizarre Things Happen when you AXFR a null-named zone.
1733 $webvar{importdoms} =~ s/^\s+//;
[33]1734 my @domlist = split /\s+/, $webvar{importdoms};
1735 my @results;
1736 foreach my $domain (@domlist) {
[761]1737##fixme: Net::DNS has made changes somewhere between 0.66something (~~ Debian wheezy) and
1738# 0.81 (~~ Debian jessie) that cause taint failures when providing a hostname as a nameserver
1739# for AXFR. A proper fix may boil down to "split AXFR into its own script". Feh.
1740# For now, we'll just convert the requested AXFR host to an IP, and pass that down the chain instead.
1741 my $nsip = gethostbyname($webvar{ifrom});
1742 use Socket;
1743 $nsip = inet_ntoa($nsip);
1744 # strangely enough we don't seem to need to detaint:
1745 #($nsip) = ($nsip =~ /^([a-fA-F0-9:.]+)$/);
[34]1746 my %row;
[761]1747 my ($code,$msg) = $dnsdb->importAXFR($nsip, $domain, $webvar{group},
[484]1748 status => $webvar{domactive}, rwsoa => $webvar{rwsoa}, rwns => $webvar{rwns},
1749 newttl => ($webvar{forcettl} ? $webvar{newttl} : 0),
1750 merge => $webvar{mergematching});
[35]1751 $row{domok} = $msg if $code eq 'OK';
1752 if ($code eq 'WARN') {
1753 $msg =~ s|\n|<br />|g;
1754 $row{domwarn} = $msg;
1755 }
[37]1756 if ($code eq 'FAIL') {
[91]1757 $msg =~ s|\n|<br />\n|g;
[37]1758 $row{domerr} = $msg;
1759 }
[91]1760 $msg = "<br />\n".$msg if $msg =~ m|<br />|;
[33]1761 $row{domain} = $domain;
1762 push @results, \%row;
1763 }
1764 $page->param(axfrresults => \@results);
1765 }
1766
[155]1767 # Yes, this is a GOTO target. PTBHTTT.
1768 DONEAXFR: ;
1769
[48]1770} elsif ($webvar{page} eq 'whoisq') {
[47]1771
[48]1772 if ($webvar{qfor}) {
1773 use Net::Whois::Raw;
1774 use Text::Wrap;
1775
1776# caching useful?
1777#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
1778#$Net::Whois::Raw::CACHE_TIME = 60;
1779
1780 my ($dominfo, $whois_server) = whois($webvar{qfor});
1781##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
1782
1783 # le sigh. idjits spit out data without linefeeds...
1784 $Text::Wrap::columns = 88;
1785
[93]1786# &%$@%@# high-bit crap. We should probably find a way to properly recode these
1787# instead of one-by-one. Note CGI::Simple's escapeHTML() doesn't do more than
1788# the bare minimum. :/
[48]1789# Mainly an XHTML validation thing.
[93]1790 $dominfo = $q->escapeHTML($dominfo);
[48]1791 $dominfo =~ s/\xa9/\&copy;/g;
1792 $dominfo =~ s/\xae/\&reg;/g;
1793
1794 $page->param(qfor => $webvar{qfor});
1795 $page->param(dominfo => wrap('','',$dominfo));
1796 $page->param(whois_server => $whois_server);
1797 } else {
1798 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
1799 }
1800
[47]1801} elsif ($webvar{page} eq 'log') {
1802
[60]1803 my $id = $curgroup; # we do this because the group log may be called from (almost) any page,
1804 # but the others are much more limited. this is probably non-optimal.
[180]1805
[61]1806 if ($webvar{ltype} && $webvar{ltype} eq 'user') {
[323]1807##fixme: where should we call this from?
[60]1808 $id = $webvar{id};
[733]1809##fixme: don't include username on out-of-scope users
1810 $page->param(logfor => 'user '.($id ? $dnsdb->userFullName($id) : $webvar{fname}));
[60]1811 } elsif ($webvar{ltype} && $webvar{ltype} eq 'dom') {
[59]1812 $id = $webvar{id};
[180]1813 if (!check_scope(id => $id, type => 'domain')) {
1814 $page->param(errmsg => "You are not permitted to view log entries for the requested domain");
1815 goto DONELOG;
1816 }
[473]1817 $page->param(logfor => 'domain '.$dnsdb->domainName($id));
[248]1818 } elsif ($webvar{ltype} && $webvar{ltype} eq 'rdns') {
1819 $id = $webvar{id};
1820 if (!check_scope(id => $id, type => 'revzone')) {
1821 $page->param(errmsg => "You are not permitted to view log entries for the requested reverse zone");
1822 goto DONELOG;
1823 }
[473]1824 $page->param(logfor => 'reverse zone '.$dnsdb->revName($id));
[59]1825 } else {
1826 # Default to listing curgroup log
[473]1827 $page->param(logfor => 'group '.$dnsdb->groupName($id));
[180]1828 # note that scope limitations are applied via the change-group check;
1829 # group log is always for the "current" group
[59]1830 }
[323]1831 $webvar{ltype} = 'group' if !$webvar{ltype};
1832
[733]1833 # Done here since we want to allow more arbitrary blobs in the log filter
1834 if (defined($webvar{logfilter})) {
1835 $session->param('logfilter', '') if !$session->param('logfilter');
1836 if ($webvar{logfilter} ne $session->param('logfilter')) {
1837 $uri_self =~ s/\&amp;offset=[^&]//;
1838 $offset = 0;
1839 }
1840 $session->param('logfilter', $webvar{logfilter})
1841 }
1842 my $logfilter = $session->param('logfilter');
[746]1843 $filter = $logfilter;
[737]1844 $page->param(logfilter => $logfilter);
[733]1845
1846 my $lcount = $dnsdb->getLogCount(id => $id, group => $logingroup, fname => $webvar{fname},
1847 logtype => $webvar{ltype}, filter => $logfilter);
1848 if (!$lcount) {
1849 $page->param(errmsg => $dnsdb->errstr);
1850 $lcount = 0;
1851 }
1852
[323]1853 $page->param(id => $id);
1854 $page->param(ltype => $webvar{ltype});
1855
1856 fill_fpnla($lcount);
1857 fill_pgcount($lcount, "log entries", '');
1858 $page->param(curpage => $webvar{page}.($webvar{ltype} ? "&amp;ltype=$webvar{ltype}" : ''));
1859
1860 $sortby = 'stamp';
1861 $sortorder = 'DESC'; # newest-first; although filtering is probably going to be more useful than sorting
1862# sort/order
1863 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1864 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1865
1866 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1867 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
1868
1869 # Set up the column headings with the sort info
[733]1870 my @cols = ('fname','domain','revzone','entry','stamp');
1871 my %colnames = (fname => 'Name', domain => 'Forward zone', revzone => 'Reverse zone',
1872 entry => 'Log Entry', stamp => 'Date/Time');
[323]1873 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
1874
1875##fixme: increase per-page limit or use separate limit for log? some ops give *lots* of entries...
[733]1876 my $logentries = $dnsdb->getLogEntries(id => $id, group => $logingroup, fname => $webvar{fname},
1877 logtype => $webvar{ltype},
1878 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder, filter => $logfilter);
1879 if (!$logentries) {
1880 $page->param(errmsg => $dnsdb->errstr);
1881 } else {
1882 # undef $logentries is inexplicably puking instead of showing "no entries found",
1883 # like all the rest of the methods that return a list the same way. idunno...
1884 $page->param(logentries => $logentries);
1885 }
[323]1886
[248]1887##fixme:
[323]1888# - on log record creation, bundle "parented" log actions (eg, "AXFR record blah for domain foo",
1889# or "Add record bar for new domain baz") into one entry (eg, "AXFR domain foo", "Add domain baz")?
1890# need a way to expand this into the complete list, and to exclude "child" entries
[47]1891
[180]1892 # scope check fail target
1893 DONELOG: ;
1894
[722]1895} elsif ($webvar{page} eq 'recsearch') {
1896
[723]1897 # we do this for the domain and record list filter/search - it should be extremely rare to
1898 # need to search on characters outside this set until we get into IDNs
[724]1899 # note this is a little larger due to template records
1900 $webvar{searchfor} =~ s/[^a-zA-Z0-9_.:\@%-]//g if $webvar{searchfor};
[722]1901
[723]1902 # save the search in the session, same as the "filter" in various other lists...
1903 if (defined($webvar{searchfor})) {
1904 if ($session->param('recsearch') && $webvar{searchfor} ne $session->param('recsearch')) {
1905 $uri_self =~ s/\&amp;offset=[^&]//;
1906 $offset = 0;
1907 }
1908 $session->param(recsearch => $webvar{searchfor});
1909 }
1910 my $searchfor = $session->param('recsearch');
[722]1911
[723]1912 $sortby = 'host';
1913 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1914 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1915 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1916 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
1917
1918 # some magic to label and linkify the column headers for sorting
[735]1919 my @cols = ('domain','revzone','host','type','val');
[723]1920 my %colheads = (domain => "Domain (Group)", revzone => "Reverse zone (Group)", host => "Host",
[735]1921 type => "Type", val => "IP/value");
1922 # only users allowed to see location/view data get this column
1923 if ($permissions{admin} || $permissions{location_view}) {
1924 $colheads{location} = "Location";
1925 push @cols, 'location';
1926 }
[723]1927 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
1928
1929 # pgcount.tmpl
1930 my $count = $dnsdb->recSearchCount(searchfor => $searchfor, group => $logingroup);
1931 fill_pgcount($count, "records");
1932 fill_fpnla($count);
1933
1934 # and a bit for fpnla.tmpl
1935 $page->param(curpage => $webvar{page});
1936
1937 $page->param(searchfor => $searchfor);
1938 my $recset = $dnsdb->recSearch(searchfor => $searchfor, group => $logingroup, offset => $webvar{offset},
1939 sortby => $sortby, sortorder => $sortorder);
[722]1940 $page->param(searchresults => $recset);
1941
[60]1942} # end $webvar{page} dance
[2]1943
1944
[17]1945# start output here so we can redirect pages.
[493]1946print $q->header( -cookie => $sesscookie);
1947print $header->output;
[7]1948
[20]1949##common bits
[374]1950# mostly things in the menu
[173]1951if ($webvar{page} ne 'login' && $webvar{page} ne 'badpage') {
[30]1952 $page->param(username => $session->param("username"));
1953
[20]1954 $page->param(group => $curgroup);
[473]1955 $page->param(groupname => $dnsdb->groupName($curgroup));
1956 $page->param(logingrp => $dnsdb->groupName($logingroup));
[117]1957 $page->param(logingrp_num => $logingroup);
[20]1958
[224]1959##fixme
1960 $page->param(mayrdns => 1);
1961
[383]1962 $page->param(mayloc => ($permissions{admin} || $permissions{location_view}));
[374]1963
[140]1964 $page->param(maydefrec => $permissions{admin});
[111]1965 $page->param(mayimport => $permissions{admin} || $permissions{domain_create});
1966 $page->param(maybulk => $permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
1967
[140]1968 $page->param(chggrps => ($permissions{admin} || $permissions{group_create} || $permissions{group_edit} || $permissions{group_delete}));
1969
[24]1970 # group tree. should go elsewhere, probably
1971 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
1972 $page->param(grptree => $tmpgrplist);
[65]1973 $page->param(subs => ($tmpgrplist ? 1 : 0)); # probably not useful to pass gobs of data in for a boolean
[42]1974 $page->param(inlogingrp => $curgroup == $logingroup);
1975
[493]1976# fill in the URL-to-self for the group tree and search-by-letter
[117]1977 $page->param(whereami => $uri_self);
[493]1978# fill in general URL-to-self
[591]1979 $page->param(script_self => "$ENV{SCRIPT_NAME}?");
[758]1980# fill in the generalized path-to-instance
1981 $page->param(webpath => ($ENV{SCRIPT_NAME} =~ m|(/.+)/[^/]+$|)[0]);
[17]1982}
[13]1983
[166]1984if (@debugbits) {
1985 print "<pre>\n";
1986 foreach (@debugbits) { print; }
1987 print "</pre>\n";
1988}
[24]1989
[2]1990# spit it out
1991print $page->output;
1992
[38]1993if ($debugenv) {
1994 print "<div id=\"debug\">webvar keys: <pre>\n";
1995 foreach my $key (keys %webvar) {
1996 print "key: $key\tval: $webvar{$key}\n";
1997 }
1998 print "</pre>\nsession:\n<pre>\n";
1999 my $sesdata = $session->dataref();
2000 foreach my $key (keys %$sesdata) {
2001 print "key: $key\tval: ".$sesdata->{$key}."\n";
2002 }
2003 print "</pre>\nENV:\n<pre>\n";
2004 foreach my $key (keys %ENV) {
2005 print "key: $key\tval: $ENV{$key}\n";
2006 }
2007 print "</pre></div>\n";
[2]2008}
2009
2010print $footer->output;
2011
[18]2012# as per the docs, Just In Case
2013$session->flush();
[2]2014
2015exit 0;
2016
2017
[24]2018sub fill_grptree {
2019 my $root = shift;
2020 my $cur = shift;
[69]2021 my $indent = shift || ' ';
[24]2022
2023 my @childlist;
2024
[533]2025 # some magic to control bad offsets on group change
2026 my $grp_uri_self = $uri_self;
2027 $grp_uri_self =~ s/\&amp;offset=[^&]+// unless ($webvar{page} eq 'reclist' && $webvar{defrec} eq 'n');
2028
[24]2029 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
[476]2030 $dnsdb->getChildren($root, \@childlist, 'immediate');
[24]2031 return if $#childlist == -1;
2032 my @grouplist;
2033 foreach (@childlist) {
2034 my %row;
[473]2035 $row{grpname} = $dnsdb->groupName($_);
[117]2036 $row{grpnum} = $_;
[533]2037 $row{whereami} = $grp_uri_self;
[185]2038 $row{curgrp} = ($_ == $cur);
[470]2039 $row{expanded} = $dnsdb->isParent($_, 'group', $cur, 'group');
[185]2040 $row{expanded} = 1 if $_ == $cur;
[69]2041 $row{subs} = fill_grptree($_,$cur,$indent.' ');
2042 $row{indent} = $indent;
[24]2043 push @grouplist, \%row;
2044 }
[69]2045 $grptree->param(indent => $indent);
[24]2046 $grptree->param(treelvl => \@grouplist);
2047 return $grptree->output;
2048}
2049
[11]2050sub changepage {
2051 my %params = @_; # think this works the way I want...
2052
[174]2053 # cross-site scripting fixup. instead of passing error messages by URL/form
2054 # variable, put them in the session where the nasty user can't meddle.
[177]2055 # these are done here since it's far simpler to pass them in from wherever
2056 # than set them locally everywhere.
2057 foreach my $sessme ('resultmsg','warnmsg','errmsg') {
[272]2058 if (my $tmp = $params{$sessme}) {
[286]2059 $tmp =~ s/^\n//;
[272]2060 $tmp =~ s|\n|<br />\n|g;
[286]2061 $session->param($sessme, $tmp);
[177]2062 delete $params{$sessme};
2063 }
[174]2064 }
2065
[11]2066 # handle user check
[493]2067 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?";
[245]2068 foreach (sort keys %params) {
[451]2069## fixme: something is undefined here on add location
[92]2070 $newurl .= "&$_=".$q->url_encode($params{$_});
[11]2071 }
2072
[30]2073 # Just In Case
2074 $session->flush();
2075
[493]2076 print $q->redirect ( -url => $newurl, -cookie => $sesscookie);
[11]2077 exit;
2078} # end changepage
2079
[376]2080# wrap up the usual suspects for result, warning, or error messages to be displayed
2081sub show_msgs {
2082 if ($session->param('resultmsg')) {
2083 $page->param(resultmsg => $session->param('resultmsg'));
2084 $session->clear('resultmsg');
2085 }
2086 if ($session->param('warnmsg')) {
2087 $page->param(warnmsg => $session->param('warnmsg'));
2088 $session->clear('warnmsg');
2089 }
2090 if ($session->param('errmsg')) {
2091 $page->param(errmsg => $session->param('errmsg'));
2092 $session->clear('errmsg');
2093 }
2094} # end show_msgs
2095
[2]2096sub fillsoa {
[277]2097 my $defrec = shift;
2098 my $revrec = shift;
[2]2099 my $id = shift;
[311]2100 my $preserve = shift || 'd'; # Flag to use webvar fields or retrieve from database
2101
[277]2102 my $domname = ($defrec eq 'y' ? '' : "DOMAIN");
[2]2103
[277]2104 $page->param(defrec => $defrec);
2105 $page->param(revrec => $revrec);
[2]2106
[39]2107# i had a good reason to do this when I wrote it...
2108# $page->param(domain => $domname);
2109# $page->param(group => $DNSDB::group);
[277]2110 $page->param(isgrp => 1) if $defrec eq 'y';
[473]2111 $page->param(parent => ($defrec eq 'y' ? $dnsdb->groupName($id) :
2112 ($revrec eq 'n' ? $dnsdb->domainName($id) : $dnsdb->revName($id)) ) );
[2]2113
2114# defaults
[17]2115 $page->param(defcontact => $DNSDB::def{contact});
2116 $page->param(defns => $DNSDB::def{prins});
2117 $page->param(defsoattl => $DNSDB::def{soattl});
2118 $page->param(defrefresh => $DNSDB::def{refresh});
2119 $page->param(defretry => $DNSDB::def{retry});
2120 $page->param(defexpire => $DNSDB::def{expire});
2121 $page->param(defminttl => $DNSDB::def{minttl});
[2]2122
[311]2123 $page->param(id => $id);
[2]2124
[311]2125 if ($preserve eq 'd') {
2126 # there are probably better ways to do this. TMTOWTDI.
[481]2127 my $soa = $dnsdb->getSOA($defrec, $revrec, $id);
[311]2128
2129 $page->param(prins => ($soa->{prins} ? $soa->{prins} : $DNSDB::def{prins}));
2130 $page->param(contact => ($soa->{contact} ? $soa->{contact} : $DNSDB::def{contact}));
2131 $page->param(refresh => ($soa->{refresh} ? $soa->{refresh} : $DNSDB::def{refresh}));
2132 $page->param(retry => ($soa->{retry} ? $soa->{retry} : $DNSDB::def{retry}));
2133 $page->param(expire => ($soa->{expire} ? $soa->{expire} : $DNSDB::def{expire}));
2134 $page->param(minttl => ($soa->{minttl} ? $soa->{minttl} : $DNSDB::def{minttl}));
2135 $page->param(ttl => ($soa->{ttl} ? $soa->{ttl} : $DNSDB::def{soattl}));
2136 } else {
2137 $page->param(prins => ($webvar{prins} ? $webvar{prins} : $DNSDB::def{prins}));
2138 $page->param(contact => ($webvar{contact} ? $webvar{contact} : $DNSDB::def{contact}));
2139 $page->param(refresh => ($webvar{refresh} ? $webvar{refresh} : $DNSDB::def{refresh}));
2140 $page->param(retry => ($webvar{retry} ? $webvar{retry} : $DNSDB::def{retry}));
2141 $page->param(expire => ($webvar{expire} ? $webvar{expire} : $DNSDB::def{expire}));
2142 $page->param(minttl => ($webvar{minttl} ? $webvar{minttl} : $DNSDB::def{minttl}));
2143 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{soattl}));
2144 }
[2]2145}
2146
[224]2147sub showzone {
[2]2148 my $def = shift;
[224]2149 my $rev = shift;
[2]2150 my $id = shift;
2151
2152 # get the SOA first
[481]2153 my $soa = $dnsdb->getSOA($def, $rev, $id);
[2]2154
[311]2155 $page->param(contact => $soa->{contact});
2156 $page->param(prins => $soa->{prins});
[788]2157 $page->param(serial => $soa->{serial});
[311]2158 $page->param(refresh => $soa->{refresh});
2159 $page->param(retry => $soa->{retry});
2160 $page->param(expire => $soa->{expire});
2161 $page->param(minttl => $soa->{minttl});
2162 $page->param(ttl => $soa->{ttl});
[2]2163
[495]2164 my $foo2 = $dnsdb->getRecList(defrec => $def, revrec => $rev, id => $id, offset => $webvar{offset},
[481]2165 sortby => $sortby, sortorder => $sortorder, filter => $filter);
[2]2166
2167 foreach my $rec (@$foo2) {
[760]2168 if ($typemap{$rec->{type}}) {
2169 $rec->{type} = $typemap{$rec->{type}};
2170 } else {
2171 $rec->{type} = "TYPE$rec->{type}";
2172 }
[224]2173 $rec->{fwdzone} = $rev eq 'n';
[559]2174 $rec->{ttl} = '(auto)' if $rec->{ttl} == -1;
[23]2175 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
2176 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
2177 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[95]2178# ACLs
2179 $rec->{record_edit} = ($permissions{admin} || $permissions{record_edit});
2180 $rec->{record_delete} = ($permissions{admin} || $permissions{record_delete});
[383]2181 $rec->{locname} = '' unless ($permissions{admin} || $permissions{location_view});
[543]2182# Timestamps
2183 if ($rec->{expires}) {
2184 $rec->{stamptype} = $rec->{ispast} ? 'expired at' : 'expires at';
2185 } else {
2186 $rec->{stamptype} = 'valid after';
2187 }
2188 # strip seconds and timezone? no, not yet. could probably offer a config knob on this display at some point.
2189# $rec->{stamp} =~ s/:\d\d-\d+$//;
2190 delete $rec->{expires};
2191 delete $rec->{ispast};
[2]2192 }
2193 $page->param(reclist => $foo2);
2194}
2195
[16]2196sub fill_recdata {
[583]2197 # le sigh. we may get called with many empty %webvar keys
[91]2198 no warnings qw( uninitialized );
2199
[101]2200##todo: allow BIND-style bare names, ASS-U-ME that the name is within the domain?
2201# prefill <domain> or DOMAIN in "Host" space for new records
[242]2202 if ($webvar{revrec} eq 'n') {
[583]2203 $page->param(typelist => $dnsdb->getTypelist($webvar{revrec}, $webvar{type}));
[473]2204 my $domroot = ($webvar{defrec} eq 'y' ? 'DOMAIN' : $dnsdb->domainName($webvar{parentid}));
[338]2205 $page->param(name => ($webvar{name} ? $webvar{name} : $domroot));
[242]2206 $page->param(address => $webvar{address});
2207 $page->param(distance => $webvar{distance})
[16]2208 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
[242]2209 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
2210 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
2211 } else {
[486]2212 my $domroot = ($webvar{defrec} eq 'y' ? 'ADMINDOMAIN' : ".$dnsdb->{domain}");
[242]2213 $page->param(name => ($webvar{name} ? $webvar{name} : $domroot));
[583]2214 my $zname = ($webvar{defrec} eq 'y' ? 'ZONE' : $dnsdb->revName($webvar{parentid}, 'y'));
[242]2215 $zname =~ s|\d*/\d+$||;
2216 $page->param(address => ($webvar{address} ? $webvar{address} : $zname));
[583]2217 $page->param(typelist => $dnsdb->getTypelist($webvar{revrec},
[608]2218 $webvar{type} || ($zname =~ /:/ ? $reverse_typemap{'AAAA+PTR'} : $reverse_typemap{'A+PTR'})));
[242]2219 }
[101]2220# retrieve the right ttl instead of falling (way) back to the hardcoded system default
[481]2221 my $soa = $dnsdb->getSOA($webvar{defrec}, $webvar{revrec}, $webvar{parentid});
[311]2222 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $soa->{minttl}));
[543]2223 $page->param(stamp_until => ($webvar{expires} eq 'until'));
2224 $page->param(stamp => $webvar{stamp});
[2]2225}
[7]2226
[24]2227sub fill_actypelist {
[83]2228 my $curtype = shift || 'u';
2229
[24]2230 my @actypes;
2231
2232 my %row1 = (actypeval => 'u', actypename => 'user');
[83]2233 $row1{typesel} = 1 if $curtype eq 'u';
[24]2234 push @actypes, \%row1;
2235
2236 my %row2 = (actypeval => 'S', actypename => 'superuser');
[83]2237 $row2{typesel} = 1 if $curtype eq 'S';
[24]2238 push @actypes, \%row2;
2239
[83]2240 $page->param(actypelist => \@actypes);
[24]2241}
2242
[65]2243sub fill_clonemelist {
[87]2244 # shut up some warnings, but don't stomp on caller's state
2245 local $webvar{clonesrc} = 0 if !defined($webvar{clonesrc});
2246
[479]2247 my $clones = $dnsdb->getUserDropdown($curgroup, $webvar{clonesrc});
[326]2248 $page->param(clonesrc => $clones);
[65]2249}
2250
[7]2251sub fill_fpnla {
2252 my $count = shift;
2253 if ($offset eq 'all') {
[70]2254 $page->param(perpage => $perpage);
[41]2255# uhm....
[7]2256 } else {
2257 # all these bits only have sensible behaviour if offset is numeric. err, probably.
2258 if ($count > $perpage) {
2259 # if there are more results than the default, always show the "all" link
2260 $page->param(navall => 1);
2261
2262 if ($offset > 0) {
2263 $page->param(navfirst => 1);
2264 $page->param(navprev => 1);
2265 $page->param(prevoffs => $offset-1);
2266 }
2267
2268 # show "next" and "last" links if we're not on the last page of results
2269 if ( (($offset+1) * $perpage - $count) < 0 ) {
2270 $page->param(navnext => 1);
2271 $page->param(nextoffs => $offset+1);
2272 $page->param(navlast => 1);
[8]2273 $page->param(lastoffs => int (($count-1)/$perpage));
[7]2274 }
[87]2275 } else {
2276 $page->param(onepage => 1);
[7]2277 }
2278 }
[10]2279} # end fill_fpnla()
2280
[12]2281sub fill_pgcount {
2282 my $pgcount = shift;
2283 my $pgtype = shift;
2284 my $parent = shift;
2285
2286 $page->param(ntot => $pgcount);
2287 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
2288 $page->param(npglast => ($offset eq 'all' ? $pgcount :
2289 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
2290 ));
2291 $page->param(pgtype => $pgtype);
2292 $page->param(parent => $parent);
[137]2293 $page->param(filter => $filter);
[12]2294} # end fill_pgcount()
2295
[41]2296
[237]2297sub listdomains { listzones(); } # temp
2298
2299sub listzones {
[95]2300# ACLs
2301 $page->param(domain_create => ($permissions{admin} || $permissions{domain_create}) );
2302 $page->param(domain_edit => ($permissions{admin} || $permissions{domain_edit}) );
2303 $page->param(domain_delete => ($permissions{admin} || $permissions{domain_delete}) );
2304
[52]2305 my @childgroups;
[476]2306 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
[52]2307 my $childlist = join(',',@childgroups);
2308
[477]2309 my $count = $dnsdb->getZoneCount(childlist => $childlist, curgroup => $curgroup, revrec => $webvar{revrec},
2310 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
[17]2311
[12]2312# fill page count and first-previous-next-last-all bits
[473]2313 fill_pgcount($count,($webvar{revrec} eq 'n' ? 'domains' : 'revzones'),$dnsdb->groupName($curgroup));
[10]2314 fill_fpnla($count);
2315
[493]2316 $sortby = ($webvar{revrec} eq 'n' ? 'domain' : 'revnet');
[41]2317# sort/order
[51]2318 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2319 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]2320
[120]2321 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2322 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]2323
[44]2324# set up the headers
[237]2325 my @cols = (($webvar{revrec} eq 'n' ? 'domain' : 'revnet'), 'status', 'group');
2326 my %colheads = (domain => 'Domain', revnet => 'Reverse Zone', status => 'Status', group => 'Group');
[54]2327 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
[41]2328
2329 # hack! hack! pthbttt. have to rethink the status column storage,
2330 # or inactive comes "before" active. *sigh*
2331 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
2332
[51]2333# waffle, waffle - keep state on these as well as sortby, sortorder?
[237]2334##fixme: put this higher so the count doesn't get munched?
[53]2335 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[41]2336
[53]2337 $page->param(filter => $filter) if $filter;
2338 $page->param(searchsubs => $searchsubs) if $searchsubs;
[41]2339
[237]2340 $page->param(group => $curgroup);
[41]2341
[477]2342 my $zonelist = $dnsdb->getZoneList(childlist => $childlist, curgroup => $curgroup, revrec => $webvar{revrec},
[237]2343 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
[533]2344 offset => $offset, sortby => $sortby, sortorder => $sortorder
[477]2345 );
[239]2346# probably don't need this, keeping for reference for now
[583]2347# foreach my $rec (@$zonelist) {
[239]2348# }
[237]2349 $page->param(domtable => $zonelist);
[11]2350} # end listdomains()
[18]2351
[87]2352
[22]2353sub listgroups {
[53]2354
[153]2355# security check - does the user have permission to view this entity?
2356 if (!(grep /^$curgroup$/, @viewablegroups)) {
2357 # hmm. Reset the current group to the login group? Yes. Prevents confusing behaviour elsewhere.
2358 $session->param('curgroup',$logingroup);
2359 $page->param(errmsg => "You are not permitted to view the requested group");
2360 $curgroup = $logingroup;
2361 }
2362
[26]2363 my @childgroups;
[476]2364 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
[26]2365 my $childlist = join(',',@childgroups);
2366
[476]2367 my ($count) = $dnsdb->getGroupCount(childlist => $childlist, curgroup => $curgroup,
2368 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
[26]2369
[22]2370# fill page count and first-previous-next-last-all bits
2371 fill_pgcount($count,"groups",'');
2372 fill_fpnla($count);
2373
[80]2374 $page->param(gid => $curgroup);
2375
[124]2376 $sortby = 'group';
[42]2377# sort/order
[51]2378 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2379 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]2380
[120]2381 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2382 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]2383
[44]2384# set up the headers
[314]2385 my @cols = ('group','parent','nusers','ndomains','nrevzones');
2386 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains', nrevzones => 'Reverse Zones');
[54]2387 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[42]2388
[51]2389# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]2390 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]2391
[53]2392 $page->param(filter => $filter) if $filter;
2393 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]2394
2395# munge sortby for columns in database
2396 $sortby = 'g.group_name' if $sortby eq 'group';
2397 $sortby = 'g2.group_name' if $sortby eq 'parent';
2398
[476]2399 my $glist = $dnsdb->getGroupList(childlist => $childlist, curgroup => $curgroup,
[314]2400 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
[476]2401 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
[22]2402
[314]2403 $page->param(grouptable => $glist);
[22]2404} # end listgroups()
2405
[92]2406
[20]2407sub fill_grouplist {
[19]2408 my $template_var = shift;
2409 my $cur = shift || $curgroup;
[26]2410
[327]2411 # little recursive utility sub-sub
2412 sub getgroupdrop {
2413 my $root = shift;
2414 my $cur = shift; # to tag the selected group
2415 my $grplist = shift;
2416 my $indent = shift || '&nbsp;&nbsp;&nbsp;&nbsp;';
[26]2417
[327]2418 my @childlist;
[476]2419 $dnsdb->getChildren($root, \@childlist, 'immediate');
[327]2420 return if $#childlist == -1;
2421 foreach (@childlist) {
2422 my %row;
2423 $row{groupval} = $_;
2424 $row{groupactive} = ($_ == $cur);
[473]2425 $row{groupname} = $indent.$dnsdb->groupName($_);
[327]2426 push @{$grplist}, \%row;
2427 getgroupdrop($_, $cur, $grplist, $indent.'&nbsp;&nbsp;&nbsp;&nbsp;');
2428 }
2429 }
[117]2430
[20]2431 my @grouplist;
[327]2432 push @grouplist, { groupval => $logingroup, groupactive => $logingroup == $curgroup,
[473]2433 groupname => $dnsdb->groupName($logingroup) };
[327]2434 getgroupdrop($logingroup, $curgroup, \@grouplist);
[18]2435
[20]2436 $page->param("$template_var" => \@grouplist);
[24]2437} # end fill_grouplist()
2438
[92]2439
[383]2440sub fill_loclist {
2441 my $cur = shift || $curgroup;
2442 my $defloc = shift || '';
2443
2444 return unless ($permissions{admin} || $permissions{location_view});
2445
[388]2446 $page->param(location_view => ($permissions{admin} || $permissions{location_view}));
[383]2447
[388]2448 if ($permissions{admin} || $permissions{record_locchg}) {
[480]2449 my $loclist = $dnsdb->getLocDropdown($cur, $defloc);
[388]2450 $page->param(record_locchg => 1);
2451 $page->param(loclist => $loclist);
2452 } else {
[480]2453 my $loc = $dnsdb->getLoc($defloc);
[388]2454 $page->param(loc_name => $loc->{description});
2455 }
[383]2456} # end fill_loclist()
2457
2458
[24]2459sub list_users {
[52]2460
2461 my @childgroups;
[476]2462 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
[52]2463 my $childlist = join(',',@childgroups);
2464
[479]2465 my $count = $dnsdb->getUserCount(childlist => $childlist, curgroup => $curgroup,
2466 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
[24]2467
2468# fill page count and first-previous-next-last-all bits
2469 fill_pgcount($count,"users",'');
2470 fill_fpnla($count);
2471
[124]2472 $sortby = 'user';
[44]2473# sort/order
[51]2474 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2475 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]2476
[120]2477 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2478 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
[51]2479
[44]2480# set up the headers
2481 my @cols = ('user','fname','type','group','status');
2482 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
[54]2483 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[44]2484
[51]2485# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]2486 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]2487
[53]2488 $page->param(filter => $filter) if $filter;
2489 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]2490
[479]2491 my $ulist = $dnsdb->getUserList(childlist => $childlist, curgroup => $curgroup,
[325]2492 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
[479]2493 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
[325]2494 # Some UI things need to be done to the list (unlike other lists)
2495 foreach my $u (@{$ulist}) {
2496 $u->{eduser} = ($permissions{admin} ||
2497 ($permissions{user_edit} && $u->{type} ne 'S') ||
2498 ($permissions{self_edit} && $u->{user_id} == $session->param('uid')) );
2499 $u->{deluser} = ($permissions{admin} || ($permissions{user_delete} && $u->{type} ne 'S'));
2500 $u->{type} = ($u->{type} eq 'S' ? 'superuser' : 'user');
[24]2501 }
[325]2502 $page->param(usertable => $ulist);
[55]2503} # end list_users()
[43]2504
[92]2505
[370]2506sub list_locations {
2507
2508 my @childgroups;
[476]2509 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
[370]2510 my $childlist = join(',',@childgroups);
2511
[480]2512 my $count = $dnsdb->getLocCount(childlist => $childlist, curgroup => $curgroup,
2513 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
[370]2514
2515# fill page count and first-previous-next-last-all bits
2516 fill_pgcount($count,"locations/views",'');
2517 fill_fpnla($count);
2518
2519 $sortby = 'user';
2520# sort/order
2521 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2522 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
2523
2524 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2525 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
2526
2527# set up the headers
2528 my @cols = ('description', 'iplist', 'group');
2529 my %colnames = (description => 'Location/View Name', iplist => 'Permitted IPs/Ranges', group => 'Group');
2530 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
2531
2532# waffle, waffle - keep state on these as well as sortby, sortorder?
2533 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
2534
2535 $page->param(filter => $filter) if $filter;
2536 $page->param(searchsubs => $searchsubs) if $searchsubs;
2537
[480]2538 my $loclist = $dnsdb->getLocList(childlist => $childlist, curgroup => $curgroup,
[370]2539 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
[480]2540 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
[370]2541 # Some UI things need to be done to the list
2542 foreach my $l (@{$loclist}) {
[377]2543 $l->{iplist} = "(All IPs)" if !$l->{iplist};
[370]2544 $l->{edloc} = ($permissions{admin} || $permissions{loc_edit});
2545 $l->{delloc} = ($permissions{admin} || $permissions{loc_delete});
2546 }
2547 $page->param(loctable => $loclist);
2548} # end list_locations()
2549
2550
[43]2551# Generate all of the glop necessary to add or not the appropriate marker/flag for
2552# the sort order and column in domain, user, group, and record lists
2553# Takes an array ref and hash ref
2554sub fill_colheads {
[54]2555 my $sortby = shift;
2556 my $sortorder = shift;
[43]2557 my $cols = shift;
2558 my $colnames = shift;
[72]2559 my $custom = shift;
[43]2560
2561 my @headings;
2562
2563 foreach my $col (@$cols) {
2564 my %coldata;
2565 $coldata{page} = $webvar{page};
2566 $coldata{offset} = $webvar{offset} if $webvar{offset};
2567 $coldata{sortby} = $col;
2568 $coldata{colname} = $colnames->{$col};
2569 if ($col eq $sortby) {
2570 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
2571 $coldata{sortorder} = $sortorder;
2572 } else {
2573 $coldata{order} = 'ASC';
2574 }
[72]2575 if ($custom) {
2576 foreach my $ckey (keys %$custom) {
2577 $coldata{$ckey} = $custom->{$ckey};
2578 }
2579 }
[43]2580 push @headings, \%coldata;
2581 }
2582
2583 $page->param(colheads => \@headings);
2584
[54]2585} # end fill_colheads()
[55]2586
[92]2587
[66]2588# we have to do this in a variety of places; let's make it consistent
2589sub fill_permissions {
2590 my $template = shift; # may need to do several sets on a single page
2591 my $permset = shift; # hashref to permissions on object
[67]2592 my $usercan = shift || \%permissions; # allow alternate user-is-allowed permission block
[66]2593
2594 foreach (@permtypes) {
[67]2595 $template->param("may_$_" => ($usercan->{admin} || $usercan->{$_}));
[66]2596 $template->param($_ => $permset->{$_});
2597 }
2598}
[155]2599
2600# so simple when defined as a sub instead of inline. O_o
2601sub check_scope {
[169]2602 my %args = @_;
2603 my $entity = $args{id} || 0; # prevent the shooting of feet with SQL "... intcolumn = '' ..."
2604 my $entype = $args{type} || '';
[155]2605
2606 if ($entype eq 'group') {
2607 return 1 if grep /^$entity$/, @viewablegroups;
2608 } else {
2609 foreach (@viewablegroups) {
[470]2610 return 1 if $dnsdb->isParent($_, 'group', $entity, $entype);
[155]2611 }
2612 }
2613}
Note: See TracBrowser for help on using the repository browser.