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