source: branches/stable/dns.cgi@ 546

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

/branches/stable

Merge reverse DNS work from /trunk, 3 of mumble.

Includes changes through r440. One minor conflict
due to old partial merge

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