source: trunk/dns.cgi@ 99

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

/trunk

Fiddle results-per-page default
Bugfix for offset on filtering a result set close enough to

the end of the original full set that the filtered set
doesn't have enough results to reach that offset.

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