source: trunk/dns.cgi@ 244

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

/trunk

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