source: trunk/dns.cgi@ 66

Last change on this file since 66 was 66, checked in by Kris Deugau, 14 years ago

/trunk

Basic group permissions editing functional - enforcing is trivial

  • add group now adds the permissions entry. TBD: permission inheritance
  • edit group Does The Right Thing(TM) - either editing the existing entry, or converting an inherited permission group to a separate one. still needs to rewrite subgroup and contained user inherited permissions
  • the HTML permissions table rows have been moved. edit-user should pick this up, and will require calling the template explicitly so as to show both the default and custom permissions.
  • the list of individual permissions have been moved to a list in DNSDB.pm code that refers to this should not assume any given length - this makes adding new permission types (somewhat) easier

Tweak menu group-tree CSS (again) add some (broken) images

  • this should probalby revert to an earlier setup that uses an image as the <li> bullet point rather than pushing the text to the right, since many (most?) nodes will usually be leaf nodes

HTML changes not validated

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