source: trunk/dns.cgi@ 460

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

/trunk

Annoyance tweak in log page; we don't use the username as an
email address anywhere, so don't indicate so in the table heading.

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