source: trunk/dns.cgi@ 211

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

/trunk

The lets-call-it-a-release commit, for sure this time!
Fill in some lingering details in the UI - company name in the
header, version on the login and footer, identify *what* you're
logging in to on the login page.

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