source: trunk/dns.cgi@ 837

Last change on this file since 837 was 797, checked in by Kris Deugau, 4 years ago

/trunk

Clean up a lingering nuisance with Perl's default include path; all scripts
should now run correctly no matter what the caller's current directory.

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