source: trunk/dns.cgi@ 182

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

/trunk

Scope checks (See #30)

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