source: branches/stable/dns.cgi

Last change on this file was 756, checked in by Kris Deugau, 8 years ago

/branches/stable

Merge /trunk through r749 for 1.4.0

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 99.6 KB
Line 
1#!/usr/bin/perl -w -T
2# Main web UI script for DeepNet DNS Administrator
3##
4# $Id: dns.cgi 756 2017-06-13 17:58:57Z 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 my %row;
1737 my ($code,$msg) = $dnsdb->importAXFR($webvar{ifrom}, $domain, $webvar{group},
1738 status => $webvar{domactive}, rwsoa => $webvar{rwsoa}, rwns => $webvar{rwns},
1739 newttl => ($webvar{forcettl} ? $webvar{newttl} : 0),
1740 merge => $webvar{mergematching});
1741 $row{domok} = $msg if $code eq 'OK';
1742 if ($code eq 'WARN') {
1743 $msg =~ s|\n|<br />|g;
1744 $row{domwarn} = $msg;
1745 }
1746 if ($code eq 'FAIL') {
1747 $msg =~ s|\n|<br />\n|g;
1748 $row{domerr} = $msg;
1749 }
1750 $msg = "<br />\n".$msg if $msg =~ m|<br />|;
1751 $row{domain} = $domain;
1752 push @results, \%row;
1753 }
1754 $page->param(axfrresults => \@results);
1755 }
1756
1757 # Yes, this is a GOTO target. PTBHTTT.
1758 DONEAXFR: ;
1759
1760} elsif ($webvar{page} eq 'whoisq') {
1761
1762 if ($webvar{qfor}) {
1763 use Net::Whois::Raw;
1764 use Text::Wrap;
1765
1766# caching useful?
1767#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
1768#$Net::Whois::Raw::CACHE_TIME = 60;
1769
1770 my ($dominfo, $whois_server) = whois($webvar{qfor});
1771##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
1772
1773 # le sigh. idjits spit out data without linefeeds...
1774 $Text::Wrap::columns = 88;
1775
1776# &%$@%@# high-bit crap. We should probably find a way to properly recode these
1777# instead of one-by-one. Note CGI::Simple's escapeHTML() doesn't do more than
1778# the bare minimum. :/
1779# Mainly an XHTML validation thing.
1780 $dominfo = $q->escapeHTML($dominfo);
1781 $dominfo =~ s/\xa9/\&copy;/g;
1782 $dominfo =~ s/\xae/\&reg;/g;
1783
1784 $page->param(qfor => $webvar{qfor});
1785 $page->param(dominfo => wrap('','',$dominfo));
1786 $page->param(whois_server => $whois_server);
1787 } else {
1788 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
1789 }
1790
1791} elsif ($webvar{page} eq 'log') {
1792
1793 my $id = $curgroup; # we do this because the group log may be called from (almost) any page,
1794 # but the others are much more limited. this is probably non-optimal.
1795
1796 if ($webvar{ltype} && $webvar{ltype} eq 'user') {
1797##fixme: where should we call this from?
1798 $id = $webvar{id};
1799##fixme: don't include username on out-of-scope users
1800 $page->param(logfor => 'user '.($id ? $dnsdb->userFullName($id) : $webvar{fname}));
1801 } elsif ($webvar{ltype} && $webvar{ltype} eq 'dom') {
1802 $id = $webvar{id};
1803 if (!check_scope(id => $id, type => 'domain')) {
1804 $page->param(errmsg => "You are not permitted to view log entries for the requested domain");
1805 goto DONELOG;
1806 }
1807 $page->param(logfor => 'domain '.$dnsdb->domainName($id));
1808 } elsif ($webvar{ltype} && $webvar{ltype} eq 'rdns') {
1809 $id = $webvar{id};
1810 if (!check_scope(id => $id, type => 'revzone')) {
1811 $page->param(errmsg => "You are not permitted to view log entries for the requested reverse zone");
1812 goto DONELOG;
1813 }
1814 $page->param(logfor => 'reverse zone '.$dnsdb->revName($id));
1815 } else {
1816 # Default to listing curgroup log
1817 $page->param(logfor => 'group '.$dnsdb->groupName($id));
1818 # note that scope limitations are applied via the change-group check;
1819 # group log is always for the "current" group
1820 }
1821 $webvar{ltype} = 'group' if !$webvar{ltype};
1822
1823 # Done here since we want to allow more arbitrary blobs in the log filter
1824 if (defined($webvar{logfilter})) {
1825 $session->param('logfilter', '') if !$session->param('logfilter');
1826 if ($webvar{logfilter} ne $session->param('logfilter')) {
1827 $uri_self =~ s/\&amp;offset=[^&]//;
1828 $offset = 0;
1829 }
1830 $session->param('logfilter', $webvar{logfilter})
1831 }
1832 my $logfilter = $session->param('logfilter');
1833 $filter = $logfilter;
1834 $page->param(logfilter => $logfilter);
1835
1836 my $lcount = $dnsdb->getLogCount(id => $id, group => $logingroup, fname => $webvar{fname},
1837 logtype => $webvar{ltype}, filter => $logfilter);
1838 if (!$lcount) {
1839 $page->param(errmsg => $dnsdb->errstr);
1840 $lcount = 0;
1841 }
1842
1843 $page->param(id => $id);
1844 $page->param(ltype => $webvar{ltype});
1845
1846 fill_fpnla($lcount);
1847 fill_pgcount($lcount, "log entries", '');
1848 $page->param(curpage => $webvar{page}.($webvar{ltype} ? "&amp;ltype=$webvar{ltype}" : ''));
1849
1850 $sortby = 'stamp';
1851 $sortorder = 'DESC'; # newest-first; although filtering is probably going to be more useful than sorting
1852# sort/order
1853 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1854 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1855
1856 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1857 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
1858
1859 # Set up the column headings with the sort info
1860 my @cols = ('fname','domain','revzone','entry','stamp');
1861 my %colnames = (fname => 'Name', domain => 'Forward zone', revzone => 'Reverse zone',
1862 entry => 'Log Entry', stamp => 'Date/Time');
1863 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
1864
1865##fixme: increase per-page limit or use separate limit for log? some ops give *lots* of entries...
1866 my $logentries = $dnsdb->getLogEntries(id => $id, group => $logingroup, fname => $webvar{fname},
1867 logtype => $webvar{ltype},
1868 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder, filter => $logfilter);
1869 if (!$logentries) {
1870 $page->param(errmsg => $dnsdb->errstr);
1871 } else {
1872 # undef $logentries is inexplicably puking instead of showing "no entries found",
1873 # like all the rest of the methods that return a list the same way. idunno...
1874 $page->param(logentries => $logentries);
1875 }
1876
1877##fixme:
1878# - on log record creation, bundle "parented" log actions (eg, "AXFR record blah for domain foo",
1879# or "Add record bar for new domain baz") into one entry (eg, "AXFR domain foo", "Add domain baz")?
1880# need a way to expand this into the complete list, and to exclude "child" entries
1881
1882 # scope check fail target
1883 DONELOG: ;
1884
1885} elsif ($webvar{page} eq 'recsearch') {
1886
1887 # we do this for the domain and record list filter/search - it should be extremely rare to
1888 # need to search on characters outside this set until we get into IDNs
1889 # note this is a little larger due to template records
1890 $webvar{searchfor} =~ s/[^a-zA-Z0-9_.:\@%-]//g if $webvar{searchfor};
1891
1892 # save the search in the session, same as the "filter" in various other lists...
1893 if (defined($webvar{searchfor})) {
1894 if ($session->param('recsearch') && $webvar{searchfor} ne $session->param('recsearch')) {
1895 $uri_self =~ s/\&amp;offset=[^&]//;
1896 $offset = 0;
1897 }
1898 $session->param(recsearch => $webvar{searchfor});
1899 }
1900 my $searchfor = $session->param('recsearch');
1901
1902 $sortby = 'host';
1903 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1904 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
1905 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
1906 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
1907
1908 # some magic to label and linkify the column headers for sorting
1909 my @cols = ('domain','revzone','host','type','val');
1910 my %colheads = (domain => "Domain (Group)", revzone => "Reverse zone (Group)", host => "Host",
1911 type => "Type", val => "IP/value");
1912 # only users allowed to see location/view data get this column
1913 if ($permissions{admin} || $permissions{location_view}) {
1914 $colheads{location} = "Location";
1915 push @cols, 'location';
1916 }
1917 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
1918
1919 # pgcount.tmpl
1920 my $count = $dnsdb->recSearchCount(searchfor => $searchfor, group => $logingroup);
1921 fill_pgcount($count, "records");
1922 fill_fpnla($count);
1923
1924 # and a bit for fpnla.tmpl
1925 $page->param(curpage => $webvar{page});
1926
1927 $page->param(searchfor => $searchfor);
1928 my $recset = $dnsdb->recSearch(searchfor => $searchfor, group => $logingroup, offset => $webvar{offset},
1929 sortby => $sortby, sortorder => $sortorder);
1930 $page->param(searchresults => $recset);
1931
1932} # end $webvar{page} dance
1933
1934
1935# start output here so we can redirect pages.
1936print $q->header( -cookie => $sesscookie);
1937print $header->output;
1938
1939##common bits
1940# mostly things in the menu
1941if ($webvar{page} ne 'login' && $webvar{page} ne 'badpage') {
1942 $page->param(username => $session->param("username"));
1943
1944 $page->param(group => $curgroup);
1945 $page->param(groupname => $dnsdb->groupName($curgroup));
1946 $page->param(logingrp => $dnsdb->groupName($logingroup));
1947 $page->param(logingrp_num => $logingroup);
1948
1949##fixme
1950 $page->param(mayrdns => 1);
1951
1952 $page->param(mayloc => ($permissions{admin} || $permissions{location_view}));
1953
1954 $page->param(maydefrec => $permissions{admin});
1955 $page->param(mayimport => $permissions{admin} || $permissions{domain_create});
1956 $page->param(maybulk => $permissions{admin} || $permissions{domain_edit} || $permissions{domain_create} || $permissions{domain_delete});
1957
1958 $page->param(chggrps => ($permissions{admin} || $permissions{group_create} || $permissions{group_edit} || $permissions{group_delete}));
1959
1960 # group tree. should go elsewhere, probably
1961 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
1962 $page->param(grptree => $tmpgrplist);
1963 $page->param(subs => ($tmpgrplist ? 1 : 0)); # probably not useful to pass gobs of data in for a boolean
1964 $page->param(inlogingrp => $curgroup == $logingroup);
1965
1966# fill in the URL-to-self for the group tree and search-by-letter
1967 $page->param(whereami => $uri_self);
1968# fill in general URL-to-self
1969 $page->param(script_self => "$ENV{SCRIPT_NAME}?");
1970}
1971
1972if (@debugbits) {
1973 print "<pre>\n";
1974 foreach (@debugbits) { print; }
1975 print "</pre>\n";
1976}
1977
1978# spit it out
1979print $page->output;
1980
1981if ($debugenv) {
1982 print "<div id=\"debug\">webvar keys: <pre>\n";
1983 foreach my $key (keys %webvar) {
1984 print "key: $key\tval: $webvar{$key}\n";
1985 }
1986 print "</pre>\nsession:\n<pre>\n";
1987 my $sesdata = $session->dataref();
1988 foreach my $key (keys %$sesdata) {
1989 print "key: $key\tval: ".$sesdata->{$key}."\n";
1990 }
1991 print "</pre>\nENV:\n<pre>\n";
1992 foreach my $key (keys %ENV) {
1993 print "key: $key\tval: $ENV{$key}\n";
1994 }
1995 print "</pre></div>\n";
1996}
1997
1998print $footer->output;
1999
2000# as per the docs, Just In Case
2001$session->flush();
2002
2003exit 0;
2004
2005
2006sub fill_grptree {
2007 my $root = shift;
2008 my $cur = shift;
2009 my $indent = shift || ' ';
2010
2011 my @childlist;
2012
2013 # some magic to control bad offsets on group change
2014 my $grp_uri_self = $uri_self;
2015 $grp_uri_self =~ s/\&amp;offset=[^&]+// unless ($webvar{page} eq 'reclist' && $webvar{defrec} eq 'n');
2016
2017 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
2018 $dnsdb->getChildren($root, \@childlist, 'immediate');
2019 return if $#childlist == -1;
2020 my @grouplist;
2021 foreach (@childlist) {
2022 my %row;
2023 $row{grpname} = $dnsdb->groupName($_);
2024 $row{grpnum} = $_;
2025 $row{whereami} = $grp_uri_self;
2026 $row{curgrp} = ($_ == $cur);
2027 $row{expanded} = $dnsdb->isParent($_, 'group', $cur, 'group');
2028 $row{expanded} = 1 if $_ == $cur;
2029 $row{subs} = fill_grptree($_,$cur,$indent.' ');
2030 $row{indent} = $indent;
2031 push @grouplist, \%row;
2032 }
2033 $grptree->param(indent => $indent);
2034 $grptree->param(treelvl => \@grouplist);
2035 return $grptree->output;
2036}
2037
2038sub changepage {
2039 my %params = @_; # think this works the way I want...
2040
2041 # cross-site scripting fixup. instead of passing error messages by URL/form
2042 # variable, put them in the session where the nasty user can't meddle.
2043 # these are done here since it's far simpler to pass them in from wherever
2044 # than set them locally everywhere.
2045 foreach my $sessme ('resultmsg','warnmsg','errmsg') {
2046 if (my $tmp = $params{$sessme}) {
2047 $tmp =~ s/^\n//;
2048 $tmp =~ s|\n|<br />\n|g;
2049 $session->param($sessme, $tmp);
2050 delete $params{$sessme};
2051 }
2052 }
2053
2054 # handle user check
2055 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?";
2056 foreach (sort keys %params) {
2057## fixme: something is undefined here on add location
2058 $newurl .= "&$_=".$q->url_encode($params{$_});
2059 }
2060
2061 # Just In Case
2062 $session->flush();
2063
2064 print $q->redirect ( -url => $newurl, -cookie => $sesscookie);
2065 exit;
2066} # end changepage
2067
2068# wrap up the usual suspects for result, warning, or error messages to be displayed
2069sub show_msgs {
2070 if ($session->param('resultmsg')) {
2071 $page->param(resultmsg => $session->param('resultmsg'));
2072 $session->clear('resultmsg');
2073 }
2074 if ($session->param('warnmsg')) {
2075 $page->param(warnmsg => $session->param('warnmsg'));
2076 $session->clear('warnmsg');
2077 }
2078 if ($session->param('errmsg')) {
2079 $page->param(errmsg => $session->param('errmsg'));
2080 $session->clear('errmsg');
2081 }
2082} # end show_msgs
2083
2084sub fillsoa {
2085 my $defrec = shift;
2086 my $revrec = shift;
2087 my $id = shift;
2088 my $preserve = shift || 'd'; # Flag to use webvar fields or retrieve from database
2089
2090 my $domname = ($defrec eq 'y' ? '' : "DOMAIN");
2091
2092 $page->param(defrec => $defrec);
2093 $page->param(revrec => $revrec);
2094
2095# i had a good reason to do this when I wrote it...
2096# $page->param(domain => $domname);
2097# $page->param(group => $DNSDB::group);
2098 $page->param(isgrp => 1) if $defrec eq 'y';
2099 $page->param(parent => ($defrec eq 'y' ? $dnsdb->groupName($id) :
2100 ($revrec eq 'n' ? $dnsdb->domainName($id) : $dnsdb->revName($id)) ) );
2101
2102# defaults
2103 $page->param(defcontact => $DNSDB::def{contact});
2104 $page->param(defns => $DNSDB::def{prins});
2105 $page->param(defsoattl => $DNSDB::def{soattl});
2106 $page->param(defrefresh => $DNSDB::def{refresh});
2107 $page->param(defretry => $DNSDB::def{retry});
2108 $page->param(defexpire => $DNSDB::def{expire});
2109 $page->param(defminttl => $DNSDB::def{minttl});
2110
2111 $page->param(id => $id);
2112
2113 if ($preserve eq 'd') {
2114 # there are probably better ways to do this. TMTOWTDI.
2115 my $soa = $dnsdb->getSOA($defrec, $revrec, $id);
2116
2117 $page->param(prins => ($soa->{prins} ? $soa->{prins} : $DNSDB::def{prins}));
2118 $page->param(contact => ($soa->{contact} ? $soa->{contact} : $DNSDB::def{contact}));
2119 $page->param(refresh => ($soa->{refresh} ? $soa->{refresh} : $DNSDB::def{refresh}));
2120 $page->param(retry => ($soa->{retry} ? $soa->{retry} : $DNSDB::def{retry}));
2121 $page->param(expire => ($soa->{expire} ? $soa->{expire} : $DNSDB::def{expire}));
2122 $page->param(minttl => ($soa->{minttl} ? $soa->{minttl} : $DNSDB::def{minttl}));
2123 $page->param(ttl => ($soa->{ttl} ? $soa->{ttl} : $DNSDB::def{soattl}));
2124 } else {
2125 $page->param(prins => ($webvar{prins} ? $webvar{prins} : $DNSDB::def{prins}));
2126 $page->param(contact => ($webvar{contact} ? $webvar{contact} : $DNSDB::def{contact}));
2127 $page->param(refresh => ($webvar{refresh} ? $webvar{refresh} : $DNSDB::def{refresh}));
2128 $page->param(retry => ($webvar{retry} ? $webvar{retry} : $DNSDB::def{retry}));
2129 $page->param(expire => ($webvar{expire} ? $webvar{expire} : $DNSDB::def{expire}));
2130 $page->param(minttl => ($webvar{minttl} ? $webvar{minttl} : $DNSDB::def{minttl}));
2131 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{soattl}));
2132 }
2133}
2134
2135sub showzone {
2136 my $def = shift;
2137 my $rev = shift;
2138 my $id = shift;
2139
2140 # get the SOA first
2141 my $soa = $dnsdb->getSOA($def, $rev, $id);
2142
2143 $page->param(contact => $soa->{contact});
2144 $page->param(prins => $soa->{prins});
2145 $page->param(refresh => $soa->{refresh});
2146 $page->param(retry => $soa->{retry});
2147 $page->param(expire => $soa->{expire});
2148 $page->param(minttl => $soa->{minttl});
2149 $page->param(ttl => $soa->{ttl});
2150
2151 my $foo2 = $dnsdb->getRecList(defrec => $def, revrec => $rev, id => $id, offset => $webvar{offset},
2152 sortby => $sortby, sortorder => $sortorder, filter => $filter);
2153
2154 foreach my $rec (@$foo2) {
2155 $rec->{type} = $typemap{$rec->{type}};
2156 $rec->{fwdzone} = $rev eq 'n';
2157 $rec->{ttl} = '(auto)' if $rec->{ttl} == -1;
2158 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
2159 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
2160 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
2161# ACLs
2162 $rec->{record_edit} = ($permissions{admin} || $permissions{record_edit});
2163 $rec->{record_delete} = ($permissions{admin} || $permissions{record_delete});
2164 $rec->{locname} = '' unless ($permissions{admin} || $permissions{location_view});
2165# Timestamps
2166 if ($rec->{expires}) {
2167 $rec->{stamptype} = $rec->{ispast} ? 'expired at' : 'expires at';
2168 } else {
2169 $rec->{stamptype} = 'valid after';
2170 }
2171 # strip seconds and timezone? no, not yet. could probably offer a config knob on this display at some point.
2172# $rec->{stamp} =~ s/:\d\d-\d+$//;
2173 delete $rec->{expires};
2174 delete $rec->{ispast};
2175 }
2176 $page->param(reclist => $foo2);
2177}
2178
2179sub fill_recdata {
2180 # le sigh. we may get called with many empty %webvar keys
2181 no warnings qw( uninitialized );
2182
2183##todo: allow BIND-style bare names, ASS-U-ME that the name is within the domain?
2184# prefill <domain> or DOMAIN in "Host" space for new records
2185 if ($webvar{revrec} eq 'n') {
2186 $page->param(typelist => $dnsdb->getTypelist($webvar{revrec}, $webvar{type}));
2187 my $domroot = ($webvar{defrec} eq 'y' ? 'DOMAIN' : $dnsdb->domainName($webvar{parentid}));
2188 $page->param(name => ($webvar{name} ? $webvar{name} : $domroot));
2189 $page->param(address => $webvar{address});
2190 $page->param(distance => $webvar{distance})
2191 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
2192 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
2193 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
2194 } else {
2195 my $domroot = ($webvar{defrec} eq 'y' ? 'ADMINDOMAIN' : ".$dnsdb->{domain}");
2196 $page->param(name => ($webvar{name} ? $webvar{name} : $domroot));
2197 my $zname = ($webvar{defrec} eq 'y' ? 'ZONE' : $dnsdb->revName($webvar{parentid}, 'y'));
2198 $zname =~ s|\d*/\d+$||;
2199 $page->param(address => ($webvar{address} ? $webvar{address} : $zname));
2200 $page->param(typelist => $dnsdb->getTypelist($webvar{revrec},
2201 $webvar{type} || ($zname =~ /:/ ? $reverse_typemap{'AAAA+PTR'} : $reverse_typemap{'A+PTR'})));
2202 }
2203# retrieve the right ttl instead of falling (way) back to the hardcoded system default
2204 my $soa = $dnsdb->getSOA($webvar{defrec}, $webvar{revrec}, $webvar{parentid});
2205 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $soa->{minttl}));
2206 $page->param(stamp_until => ($webvar{expires} eq 'until'));
2207 $page->param(stamp => $webvar{stamp});
2208}
2209
2210sub fill_actypelist {
2211 my $curtype = shift || 'u';
2212
2213 my @actypes;
2214
2215 my %row1 = (actypeval => 'u', actypename => 'user');
2216 $row1{typesel} = 1 if $curtype eq 'u';
2217 push @actypes, \%row1;
2218
2219 my %row2 = (actypeval => 'S', actypename => 'superuser');
2220 $row2{typesel} = 1 if $curtype eq 'S';
2221 push @actypes, \%row2;
2222
2223 $page->param(actypelist => \@actypes);
2224}
2225
2226sub fill_clonemelist {
2227 # shut up some warnings, but don't stomp on caller's state
2228 local $webvar{clonesrc} = 0 if !defined($webvar{clonesrc});
2229
2230 my $clones = $dnsdb->getUserDropdown($curgroup, $webvar{clonesrc});
2231 $page->param(clonesrc => $clones);
2232}
2233
2234sub fill_fpnla {
2235 my $count = shift;
2236 if ($offset eq 'all') {
2237 $page->param(perpage => $perpage);
2238# uhm....
2239 } else {
2240 # all these bits only have sensible behaviour if offset is numeric. err, probably.
2241 if ($count > $perpage) {
2242 # if there are more results than the default, always show the "all" link
2243 $page->param(navall => 1);
2244
2245 if ($offset > 0) {
2246 $page->param(navfirst => 1);
2247 $page->param(navprev => 1);
2248 $page->param(prevoffs => $offset-1);
2249 }
2250
2251 # show "next" and "last" links if we're not on the last page of results
2252 if ( (($offset+1) * $perpage - $count) < 0 ) {
2253 $page->param(navnext => 1);
2254 $page->param(nextoffs => $offset+1);
2255 $page->param(navlast => 1);
2256 $page->param(lastoffs => int (($count-1)/$perpage));
2257 }
2258 } else {
2259 $page->param(onepage => 1);
2260 }
2261 }
2262} # end fill_fpnla()
2263
2264sub fill_pgcount {
2265 my $pgcount = shift;
2266 my $pgtype = shift;
2267 my $parent = shift;
2268
2269 $page->param(ntot => $pgcount);
2270 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
2271 $page->param(npglast => ($offset eq 'all' ? $pgcount :
2272 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
2273 ));
2274 $page->param(pgtype => $pgtype);
2275 $page->param(parent => $parent);
2276 $page->param(filter => $filter);
2277} # end fill_pgcount()
2278
2279
2280sub listdomains { listzones(); } # temp
2281
2282sub listzones {
2283# ACLs
2284 $page->param(domain_create => ($permissions{admin} || $permissions{domain_create}) );
2285 $page->param(domain_edit => ($permissions{admin} || $permissions{domain_edit}) );
2286 $page->param(domain_delete => ($permissions{admin} || $permissions{domain_delete}) );
2287
2288 my @childgroups;
2289 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
2290 my $childlist = join(',',@childgroups);
2291
2292 my $count = $dnsdb->getZoneCount(childlist => $childlist, curgroup => $curgroup, revrec => $webvar{revrec},
2293 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
2294
2295# fill page count and first-previous-next-last-all bits
2296 fill_pgcount($count,($webvar{revrec} eq 'n' ? 'domains' : 'revzones'),$dnsdb->groupName($curgroup));
2297 fill_fpnla($count);
2298
2299 $sortby = ($webvar{revrec} eq 'n' ? 'domain' : 'revnet');
2300# sort/order
2301 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2302 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
2303
2304 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2305 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
2306
2307# set up the headers
2308 my @cols = (($webvar{revrec} eq 'n' ? 'domain' : 'revnet'), 'status', 'group');
2309 my %colheads = (domain => 'Domain', revnet => 'Reverse Zone', status => 'Status', group => 'Group');
2310 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
2311
2312 # hack! hack! pthbttt. have to rethink the status column storage,
2313 # or inactive comes "before" active. *sigh*
2314 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
2315
2316# waffle, waffle - keep state on these as well as sortby, sortorder?
2317##fixme: put this higher so the count doesn't get munched?
2318 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
2319
2320 $page->param(filter => $filter) if $filter;
2321 $page->param(searchsubs => $searchsubs) if $searchsubs;
2322
2323 $page->param(group => $curgroup);
2324
2325 my $zonelist = $dnsdb->getZoneList(childlist => $childlist, curgroup => $curgroup, revrec => $webvar{revrec},
2326 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
2327 offset => $offset, sortby => $sortby, sortorder => $sortorder
2328 );
2329# probably don't need this, keeping for reference for now
2330# foreach my $rec (@$zonelist) {
2331# }
2332 $page->param(domtable => $zonelist);
2333} # end listdomains()
2334
2335
2336sub listgroups {
2337
2338# security check - does the user have permission to view this entity?
2339 if (!(grep /^$curgroup$/, @viewablegroups)) {
2340 # hmm. Reset the current group to the login group? Yes. Prevents confusing behaviour elsewhere.
2341 $session->param('curgroup',$logingroup);
2342 $page->param(errmsg => "You are not permitted to view the requested group");
2343 $curgroup = $logingroup;
2344 }
2345
2346 my @childgroups;
2347 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
2348 my $childlist = join(',',@childgroups);
2349
2350 my ($count) = $dnsdb->getGroupCount(childlist => $childlist, curgroup => $curgroup,
2351 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
2352
2353# fill page count and first-previous-next-last-all bits
2354 fill_pgcount($count,"groups",'');
2355 fill_fpnla($count);
2356
2357 $page->param(gid => $curgroup);
2358
2359 $sortby = 'group';
2360# sort/order
2361 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2362 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
2363
2364 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2365 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
2366
2367# set up the headers
2368 my @cols = ('group','parent','nusers','ndomains','nrevzones');
2369 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains', nrevzones => 'Reverse Zones');
2370 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
2371
2372# waffle, waffle - keep state on these as well as sortby, sortorder?
2373 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
2374
2375 $page->param(filter => $filter) if $filter;
2376 $page->param(searchsubs => $searchsubs) if $searchsubs;
2377
2378# munge sortby for columns in database
2379 $sortby = 'g.group_name' if $sortby eq 'group';
2380 $sortby = 'g2.group_name' if $sortby eq 'parent';
2381
2382 my $glist = $dnsdb->getGroupList(childlist => $childlist, curgroup => $curgroup,
2383 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
2384 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
2385
2386 $page->param(grouptable => $glist);
2387} # end listgroups()
2388
2389
2390sub fill_grouplist {
2391 my $template_var = shift;
2392 my $cur = shift || $curgroup;
2393
2394 # little recursive utility sub-sub
2395 sub getgroupdrop {
2396 my $root = shift;
2397 my $cur = shift; # to tag the selected group
2398 my $grplist = shift;
2399 my $indent = shift || '&nbsp;&nbsp;&nbsp;&nbsp;';
2400
2401 my @childlist;
2402 $dnsdb->getChildren($root, \@childlist, 'immediate');
2403 return if $#childlist == -1;
2404 foreach (@childlist) {
2405 my %row;
2406 $row{groupval} = $_;
2407 $row{groupactive} = ($_ == $cur);
2408 $row{groupname} = $indent.$dnsdb->groupName($_);
2409 push @{$grplist}, \%row;
2410 getgroupdrop($_, $cur, $grplist, $indent.'&nbsp;&nbsp;&nbsp;&nbsp;');
2411 }
2412 }
2413
2414 my @grouplist;
2415 push @grouplist, { groupval => $logingroup, groupactive => $logingroup == $curgroup,
2416 groupname => $dnsdb->groupName($logingroup) };
2417 getgroupdrop($logingroup, $curgroup, \@grouplist);
2418
2419 $page->param("$template_var" => \@grouplist);
2420} # end fill_grouplist()
2421
2422
2423sub fill_loclist {
2424 my $cur = shift || $curgroup;
2425 my $defloc = shift || '';
2426
2427 return unless ($permissions{admin} || $permissions{location_view});
2428
2429 $page->param(location_view => ($permissions{admin} || $permissions{location_view}));
2430
2431 if ($permissions{admin} || $permissions{record_locchg}) {
2432 my $loclist = $dnsdb->getLocDropdown($cur, $defloc);
2433 $page->param(record_locchg => 1);
2434 $page->param(loclist => $loclist);
2435 } else {
2436 my $loc = $dnsdb->getLoc($defloc);
2437 $page->param(loc_name => $loc->{description});
2438 }
2439} # end fill_loclist()
2440
2441
2442sub list_users {
2443
2444 my @childgroups;
2445 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
2446 my $childlist = join(',',@childgroups);
2447
2448 my $count = $dnsdb->getUserCount(childlist => $childlist, curgroup => $curgroup,
2449 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
2450
2451# fill page count and first-previous-next-last-all bits
2452 fill_pgcount($count,"users",'');
2453 fill_fpnla($count);
2454
2455 $sortby = 'user';
2456# sort/order
2457 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2458 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
2459
2460 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2461 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
2462
2463# set up the headers
2464 my @cols = ('user','fname','type','group','status');
2465 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
2466 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
2467
2468# waffle, waffle - keep state on these as well as sortby, sortorder?
2469 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
2470
2471 $page->param(filter => $filter) if $filter;
2472 $page->param(searchsubs => $searchsubs) if $searchsubs;
2473
2474 my $ulist = $dnsdb->getUserList(childlist => $childlist, curgroup => $curgroup,
2475 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
2476 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
2477 # Some UI things need to be done to the list (unlike other lists)
2478 foreach my $u (@{$ulist}) {
2479 $u->{eduser} = ($permissions{admin} ||
2480 ($permissions{user_edit} && $u->{type} ne 'S') ||
2481 ($permissions{self_edit} && $u->{user_id} == $session->param('uid')) );
2482 $u->{deluser} = ($permissions{admin} || ($permissions{user_delete} && $u->{type} ne 'S'));
2483 $u->{type} = ($u->{type} eq 'S' ? 'superuser' : 'user');
2484 }
2485 $page->param(usertable => $ulist);
2486} # end list_users()
2487
2488
2489sub list_locations {
2490
2491 my @childgroups;
2492 $dnsdb->getChildren($curgroup, \@childgroups, 'all') if $searchsubs;
2493 my $childlist = join(',',@childgroups);
2494
2495 my $count = $dnsdb->getLocCount(childlist => $childlist, curgroup => $curgroup,
2496 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef) );
2497
2498# fill page count and first-previous-next-last-all bits
2499 fill_pgcount($count,"locations/views",'');
2500 fill_fpnla($count);
2501
2502 $sortby = 'user';
2503# sort/order
2504 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
2505 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
2506
2507 $sortby = $session->param($webvar{page}.'sortby') if $session->param($webvar{page}.'sortby');
2508 $sortorder = $session->param($webvar{page}.'order') if $session->param($webvar{page}.'order');
2509
2510# set up the headers
2511 my @cols = ('description', 'iplist', 'group');
2512 my %colnames = (description => 'Location/View Name', iplist => 'Permitted IPs/Ranges', group => 'Group');
2513 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
2514
2515# waffle, waffle - keep state on these as well as sortby, sortorder?
2516 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
2517
2518 $page->param(filter => $filter) if $filter;
2519 $page->param(searchsubs => $searchsubs) if $searchsubs;
2520
2521 my $loclist = $dnsdb->getLocList(childlist => $childlist, curgroup => $curgroup,
2522 filter => ($filter ? $filter : undef), startwith => ($startwith ? $startwith : undef),
2523 offset => $webvar{offset}, sortby => $sortby, sortorder => $sortorder);
2524 # Some UI things need to be done to the list
2525 foreach my $l (@{$loclist}) {
2526 $l->{iplist} = "(All IPs)" if !$l->{iplist};
2527 $l->{edloc} = ($permissions{admin} || $permissions{loc_edit});
2528 $l->{delloc} = ($permissions{admin} || $permissions{loc_delete});
2529 }
2530 $page->param(loctable => $loclist);
2531} # end list_locations()
2532
2533
2534# Generate all of the glop necessary to add or not the appropriate marker/flag for
2535# the sort order and column in domain, user, group, and record lists
2536# Takes an array ref and hash ref
2537sub fill_colheads {
2538 my $sortby = shift;
2539 my $sortorder = shift;
2540 my $cols = shift;
2541 my $colnames = shift;
2542 my $custom = shift;
2543
2544 my @headings;
2545
2546 foreach my $col (@$cols) {
2547 my %coldata;
2548 $coldata{page} = $webvar{page};
2549 $coldata{offset} = $webvar{offset} if $webvar{offset};
2550 $coldata{sortby} = $col;
2551 $coldata{colname} = $colnames->{$col};
2552 if ($col eq $sortby) {
2553 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
2554 $coldata{sortorder} = $sortorder;
2555 } else {
2556 $coldata{order} = 'ASC';
2557 }
2558 if ($custom) {
2559 foreach my $ckey (keys %$custom) {
2560 $coldata{$ckey} = $custom->{$ckey};
2561 }
2562 }
2563 push @headings, \%coldata;
2564 }
2565
2566 $page->param(colheads => \@headings);
2567
2568} # end fill_colheads()
2569
2570
2571# we have to do this in a variety of places; let's make it consistent
2572sub fill_permissions {
2573 my $template = shift; # may need to do several sets on a single page
2574 my $permset = shift; # hashref to permissions on object
2575 my $usercan = shift || \%permissions; # allow alternate user-is-allowed permission block
2576
2577 foreach (@permtypes) {
2578 $template->param("may_$_" => ($usercan->{admin} || $usercan->{$_}));
2579 $template->param($_ => $permset->{$_});
2580 }
2581}
2582
2583# so simple when defined as a sub instead of inline. O_o
2584sub check_scope {
2585 my %args = @_;
2586 my $entity = $args{id} || 0; # prevent the shooting of feet with SQL "... intcolumn = '' ..."
2587 my $entype = $args{type} || '';
2588
2589 if ($entype eq 'group') {
2590 return 1 if grep /^$entity$/, @viewablegroups;
2591 } else {
2592 foreach (@viewablegroups) {
2593 return 1 if $dnsdb->isParent($_, 'group', $entity, $entype);
2594 }
2595 }
2596}
Note: See TracBrowser for help on using the repository browser.