source: trunk/dns.cgi@ 540

Last change on this file since 540 was 533, checked in by Kris Deugau, 11 years ago

/trunk

Fix up poor edge-case handling of pagination offsets when changing groups or
filter options. Fix pagination on bulk domain page.
Required:

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