source: trunk/dns.cgi@ 876

Last change on this file since 876 was 840, checked in by Kris Deugau, 3 years ago

/trunk

Forgot to update the copyright year

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