source: trunk/dns.cgi@ 736

Last change on this file since 736 was 736, checked in by Kris Deugau, 9 years ago

/trunk

Fold domain name to lowercase for email notice to match how it gets
stored with the lowercase configuration option on.

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