source: trunk/dns.cgi@ 376

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

/trunk

Factor out another little code fragment: result, warning, and
error message output to templates.

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