source: branches/stable/dns.cgi@ 794

Last change on this file since 794 was 756, checked in by Kris Deugau, 8 years ago

/branches/stable

Merge /trunk through r749 for 1.4.0

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