source: trunk/dns.cgi@ 762

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

/trunk

Fix a taint issue using Net::DNS for AXFR with a hostname. Inexplicably,
looking up the IP ourselves and passing that (still tainted!) string in
as the connect host works just fine.

Probably needs further review with newer Net::DNS versions to make sure
this still works, or find a different workaround.

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