source: trunk/dns.cgi@ 554

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

/trunk

Catch a fine edge case missed in r551

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