source: trunk/dns.cgi@ 513

Last change on this file since 513 was 513, checked in by Kris Deugau, 12 years ago

/trunk

Get rid of another pair of minor messes in the error log. See #25.

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