source: trunk/dns.cgi@ 193

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

/trunk

Hack _log() to dig up user data when (not if) it's missing. Better solution needed.
Clean up debugging statements in addDomain()
Add some ##fixme's to dns.cgi
Log distance, port, weight as needed for new MX, SRV records

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