source: trunk/dns.cgi@ 778

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

/trunk

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

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

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