source: trunk/dns.cgi@ 107

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

/trunk

Fix mishandling of record data retrieved before deleting said record.
Tweak logging on record deletion - probably failing due to

mishandling the retrieval.

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