source: branches/stable/dns.cgi@ 430

Last change on this file since 430 was 430, checked in by Kris Deugau, 12 years ago

/branches/stable

Backport scope check fix on default record delete from r244.
Unfortunately this is a fragment of a much larger change so
it was copied by hand.

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