source: trunk/dns.cgi@ 251

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

/trunk

Fix navigation glitch on deleting a record; revrec wasn't
propagated causing you to land on the wrong record list.
See #26

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