source: trunk/dns.cgi@ 491

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

/trunk

Object conversion of DNSDB.pm, part 21. See #11.

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