source: trunk/dns.cgi@ 544

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

/trunk

Implement most of the UI and back end for handling scheduled changes
to records. See #40.

This turned out to be most of what I had vaguely imagined; only SOA
records can't sanely be set for scheduled changes yet (can't think of
a scenario where this would even be useful) and there's only a small
dusting of UI chrome left for another time.

Bumped up from projected 1.4 to 1.2 per request from Reid Sutherland.

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