source: trunk/dns.cgi@ 110

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

/trunk

Add "Bulk domain operations" area - move-to-group, activate/deactivate, delete

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