source: trunk/dns.cgi@ 321

Last change on this file since 321 was 320, checked in by Kris Deugau, 13 years ago

/trunk

Administrivia cleanup - overdue to update the copyright dates

Not sure how I managed this, but the copy of the GPL in COPYING
got pasted in the file twice. O_o Recopied from the FSF website,
diff confirms just removal of the duplicate

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