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