source: trunk/dns.cgi@ 737

Last change on this file since 737 was 737, checked in by Kris Deugau, 9 years ago

/trunk

Properly track the filter argument for the log separately from other
filter fields, so that (among other things) we can accept more characters
for filtering the log entries. See #17.

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