source: trunk/dns.cgi@ 189

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

/trunk

UI tweaks and fixes for user and domain status changes:

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