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