source: trunk/dns.cgi@ 536

Last change on this file since 536 was 533, checked in by Kris Deugau, 11 years ago

/trunk

Fix up poor edge-case handling of pagination offsets when changing groups or
filter options. Fix pagination on bulk domain page.
Required:

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