source: trunk/dns.cgi@ 191

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

/trunk

Minor fix in dns-rpc.cgi to load DB user/pass/etc with loadConfig

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