source: trunk/dns.cgi@ 111

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

/trunk

Add ACL checks to prevent contructed-URL ACL bypasses on SOA records, group
add/edit/delete, axfr import; correct logic in bulk domain ACL check

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