source: trunk/dns.cgi@ 680

Last change on this file since 680 was 666, checked in by Kris Deugau, 10 years ago

/trunk

Convert calling convention for getRecCount() to a hash so we can pass the
same arguments to getRecCount() and getRecList()

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