source: trunk/dns.cgi@ 287

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

/trunk

Move action logging for addRec() into DNSDB.pm. See #35

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