source: trunk/dns.cgi@ 355

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

/trunk

Fix buglet in add record (possibly update record as well);
hostname entered was not retained on error case

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