source: trunk/dns.cgi@ 283

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

/trunk

Convert zone status change logging to DNSDB-internal. See #35
Add a global for holding general result messages for subs that
return booleans. This allows us to easily display the same text
as was added to the log.
Fix docucomment on mailNotify(); it (still!) referred to its
copy-paste parent in the IP Database.

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