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