source: trunk/dns.cgi@ 256

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

/trunk

Update parentID() (and all calls to it) for reverse records,
move to DNSDB.pm. See #26.

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