source: trunk/dns.cgi@ 382

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

/trunk

Somewhere along the line, addDomain() got updated to ignore the
user data passed in a hash in favour of logging relying on DNSDB.pm
internals, but the call in dns.cgi never got updated to match.

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