source: trunk/dns.cgi@ 65

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

/trunk

checkpoint, adding permissions/ACL support

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 49.2 KB
RevLine 
[2]1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2010-11-25 21:26:08 +0000 (Thu, 25 Nov 2010) $
6# SVN revision $Rev: 65 $
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
478 my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
[57]479 if ($code eq 'OK') {
[55]480 logaction(0, $session->param("username"), $webvar{pargroup}, "Added group $webvar{newgroup}");
481 changepage(page => "grpman");
482 }
[18]483 $page->param(add_failed => 1);
484 $page->param(errmsg => $msg);
485 $page->param(newgroup => $webvar{newgroup});
[20]486 fill_grouplist('pargroup',$webvar{pargroup});
[19]487 } else {
488# $page->param
[20]489 fill_grouplist('pargroup',$curgroup);
[19]490
[18]491 }
[20]492
[22]493} elsif ($webvar{page} eq 'delgrp') {
[20]494
495 $page->param(id => $webvar{id});
496 # first pass = confirm y/n (sorta)
497 if (!defined($webvar{del})) {
498 $page->param(del_getconf => 1);
[23]499# $page->param(groupname => groupName($dbh,$webvar{id}));
[20]500# print some neato things?
501
502# } else {
503# #whether actually deleting or cancelling we redirect to the group list, default format
504
505 } elsif ($webvar{del} eq 'ok') {
[57]506 my $deleteme = groupName($dbh,$webvar{id}); # get this before we delete it...
[20]507 my ($code,$msg) = delGroup($dbh, $webvar{id});
508 if ($code ne 'OK') {
509# need to find failure mode
510 $page->param(del_failed => 1);
511 $page->param(errmsg => $msg);
[22]512 $page->param(curpage => $webvar{page});
513 listgroups();
[20]514 } else {
[57]515##fixme: need to clean up log when deleting a major container
516 logaction(0, $session->param("username"), $webvar{curgroup}, "Deleted group $deleteme");
[20]517 # success. go back to the domain list, do not pass "GO"
518 changepage(page => "grpman");
519 }
520 } else {
521 # cancelled. whee!
522 changepage(page => "grpman");
523 }
[23]524 $page->param(delgroupname => groupName($dbh, $webvar{id}));
[24]525
[65]526} elsif ($webvar{page} eq 'edgroup') {
527
528 if ($webvar{action} eq 'updperms') {
529 # extra safety check; make sure user can't construct a URL to bypass ACLs
530 my %curperms;
531 getPermissions($dbh, 'group', $webvar{gid}, \%curperms);
532 foreach (('group_edit','group_create','group_delete',
533 'user_edit','user_create','user_delete',
534 'domain_edit','domain_create','domain_delete',
535 'record_edit','record_create','record_delete',
536 'self_edit')
537 ) {
538 $webvar{$_} = 0 if !defined($webvar{$_});
539 $webvar{$_} = 1 if $webvar{$_} eq 'on';
540push @debugbits, "$_ has changed: '$curperms{$_}' => '$webvar{$_}'<br>\n" if $curperms{$_} ne $webvar{$_};
541 if ($permissions{admin} || $permissions{$_}) {
542 if (($webvar{$_} eq 'on' && !$curperms{$_}) or
543 (!$webvar{$_} && $curperms{$_})) {
544 push @debugbits, '&nbsp;&nbsp;'."may update $_<br>\n";
545 }
546 }
547 }
548 }
549 $page->param(gid => $webvar{gid});
550 $page->param(grpmeddle => groupName($dbh, $webvar{gid}));
551 my %grpperms;
552 getPermissions($dbh, 'group', $webvar{gid}, \%grpperms);
553# unless (0) {
554 foreach (('group_edit','group_create','group_delete',
555 'user_edit','user_create','user_delete',
556 'domain_edit','domain_create','domain_delete',
557 'record_edit','record_create','record_delete',
558 'self_edit')
559 ) {
560#push @debugbits, "$_ => admin? '$permissions{admin}' may_$_? '$permissions{$_}' group? '$grpperms{$_}'<br>\n";
561 $page->param("may_$_" => ($permissions{admin} || $permissions{$_}));
562 $page->param($_ => $grpperms{$_});
563 }
564# }
565# my %grpperms = getPermissions('group',$webvar{group});
566
[24]567} elsif ($webvar{page} eq 'useradmin') {
568
[51]569 if (defined($webvar{action})) {
570 userStatus($dbh,$webvar{id},$webvar{action});
571 }
572
[24]573 $page->param(curpage => $webvar{page});
574
[51]575 list_users();
576
[24]577} elsif ($webvar{page} eq 'newuser') {
578
579 # foo?
580 fill_actypelist();
[65]581 fill_clonemelist();
[24]582
583} elsif ($webvar{page} eq 'adduser') {
584
585 my ($code,$msg);
586
587 if ($webvar{pass1} ne $webvar{pass2}) {
588 $code = 'FAIL';
589 $msg = "Passwords don't match";
590 } else {
[65]591# assemble a permission string - far simpler than trying to pass an
592# indeterminate set of permission flags individually
593my $permstring;
594if ($webvar{perms_type} eq 'custom') {
595 $permstring = 'C:,g:,u:,d:,r:';
596 $page->param(perm_custom => 1);
597} elsif ($webvar{perms_type} eq 'clone') {
598 $permstring = 'c:';
599 $page->param(perm_clone => 1);
600} else {
601 $permstring = 'i';
602# $page->param(perm_inherit => 1);
603}
[38]604 ($code,$msg) = addUser($dbh,$webvar{uname}, $webvar{group}, $webvar{pass1},
[25]605 ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{accttype},
606 $webvar{fname}, $webvar{lname}, $webvar{phone});
[24]607 }
608
609# hokay, a bit of magic to decide which page we hit.
610 if ($code eq 'OK') {
[57]611##log
[55]612 logaction(0, $session->param("username"), $webvar{group},
613 "Added user $webvar{uname} ($webvar{fname} $webvar{lname})");
[24]614 changepage(page => "useradmin");
615 } else {
[38]616# oddity - apparently, xhtml 1.0 strict swallows username as an HTML::Template var. O_o
[24]617 $page->param(add_failed => 1);
[38]618 $page->param(uname => $webvar{uname});
[24]619 $page->param(fname => $webvar{fname});
620 $page->param(lname => $webvar{lname});
621 $page->param(pass1 => $webvar{pass1});
622 $page->param(pass2 => $webvar{pass2});
623 $page->param(errmsg => $msg);
624 fill_actypelist();
[65]625 fill_clonemelist();
[24]626 }
627
[38]628# $page->param(add_failed => 1);
[25]629
630} elsif ($webvar{page} eq 'deluser') {
631
632 $page->param(id => $webvar{id});
633 # first pass = confirm y/n (sorta)
634 if (!defined($webvar{del})) {
635 $page->param(del_getconf => 1);
636 $page->param(user => userFullName($dbh,$webvar{id}));
637 } elsif ($webvar{del} eq 'ok') {
[57]638##fixme: find group id user is in (for logging) *before* we delete the user
[25]639 my ($code,$msg) = delUser($dbh, $webvar{id});
640 if ($code ne 'OK') {
641# need to find failure mode
642 $page->param(del_failed => 1);
643 $page->param(errmsg => $msg);
644 list_users($curgroup);
645 } else {
[65]646 # success. go back to the user list, do not pass "GO"
[57]647##log
648 logaction(0, $session->param("username"), $webvar{group}, "Added domain $webvar{domain}");
[25]649 changepage(page => "useradmin");
650 }
651 } else {
652 # cancelled. whee!
653 changepage(page => "useradmin");
654 }
655
[65]656} elsif ($webvar{page} eq 'edituser') {
657
[30]658} elsif ($webvar{page} eq 'dnsq') {
659
660 $page->param(qfor => $webvar{qfor}) if $webvar{qfor};
[31]661 fill_rectypes($webvar{type} ? $webvar{type} : '', 1);
662 $page->param(nrecurse => $webvar{nrecurse}) if $webvar{nrecurse};
[30]663 $page->param(resolver => $webvar{resolver}) if $webvar{resolver};
664
665 if ($webvar{qfor}) {
666 my $resolv = Net::DNS::Resolver->new;
[31]667 $resolv->tcp_timeout(5); # make me adjustable!
668 $resolv->udp_timeout(5); # make me adjustable!
669 $resolv->recurse(0) if $webvar{nrecurse};
670 $resolv->nameservers($webvar{resolver}) if $webvar{resolver};
[30]671 my $query = $resolv->query($webvar{qfor}, $typemap{$webvar{type}});
672 if ($query) {
673
674 $page->param(showresults => 1);
675
676 my @answer;
677 foreach my $rr ($query->answer) {
678# next unless $rr->type eq "A" or $rr->type eq 'NS';
679 my %row;
680 my ($host,$ttl,$class,$type,$data) =
[31]681 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/s);
[30]682 $row{host} = $host;
683 $row{ftype} = $type;
[31]684 $row{rdata} = ($type eq 'SOA' ? "<pre>$data</pre>" : $data);
[30]685 push @answer, \%row;
686 }
687 $page->param(answer => \@answer);
688
689 my @additional;
690 foreach my $rr ($query->additional) {
691# next unless $rr->type eq "A" or $rr->type eq 'NS';
692 my %row;
693 my ($host,$ttl,$class,$type,$data) =
694 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
695 $row{host} = $host;
696 $row{ftype} = $type;
697 $row{rdata} = $data;
698 push @additional, \%row;
699 }
700 $page->param(additional => \@additional);
701
702 my @authority;
703 foreach my $rr ($query->authority) {
704# next unless $rr->type eq "A" or $rr->type eq 'NS';
705 my %row;
706 my ($host,$ttl,$class,$type,$data) =
707 ($rr->string =~ /^([0-9a-zA-Z_.-]+)\s+(\d+)\s+([A-Za-z]+)\s+([A-Za-z]+)\s+(.+)$/);
708 $row{host} = $host;
709 $row{ftype} = $type;
710 $row{rdata} = $data;
711 push @authority, \%row;
712 }
713 $page->param(authority => \@authority);
714
715 $page->param(usedresolver => $resolv->answerfrom);
716 $page->param(frtype => $typemap{$webvar{type}});
717
718 } else {
719 $page->param(errmsg => $resolv->errorstring);
720 }
721 }
722 ## done DNS query
723
[31]724} elsif ($webvar{page} eq 'axfr') {
725
726 # don't need this while we've got the dropdown in the menu. hmm.
727 #fill_grouplist;
728
729 $page->param(ifrom => $webvar{ifrom}) if $webvar{ifrom};
730 $page->param(rwsoa => $webvar{rwsoa}) if $webvar{rwsoa};
731 $page->param(rwns => $webvar{rwns}) if $webvar{rwns};
[37]732 $page->param(dominactive => 1) if (!$webvar{domactive} && $webvar{doit}); # eww.
[31]733 $page->param(importdoms => $webvar{importdoms}) if $webvar{importdoms};
[33]734
735##fixme: check group too?
736 if ($webvar{doit} eq 'y' && !$webvar{ifrom}) {
737 $page->param(errmsg => "Need to set host to import from");
738 } elsif ($webvar{doit} eq 'y' && !$webvar{importdoms}) {
739 $page->param(errmsg => "Need domains to import");
740 } else {
741 my @domlist = split /\s+/, $webvar{importdoms};
742 my @results;
743my $rnum = 0;
744 foreach my $domain (@domlist) {
[34]745 my %row;
746 my ($code,$msg) = importAXFR($dbh, $webvar{ifrom}, $domain, $webvar{group},
747 $webvar{domstatus}, $webvar{rwsoa}, $webvar{rwns});
[35]748 $row{domok} = $msg if $code eq 'OK';
749 if ($code eq 'WARN') {
750 $msg =~ s|\n|<br />|g;
751 $row{domwarn} = $msg;
752 }
[37]753 if ($code eq 'FAIL') {
754 $msg =~ s|\n|<br />|g;
755 $row{domerr} = $msg;
756 }
[33]757 # do stuff! DNSDB::importAXFR($webvar{ifrom}, $webvar{rwsoa}, $webvar{rwns}, $domain, <flags>)
758 $row{domain} = $domain;
759# $row{row} = $rnum++;
760 push @results, \%row;
761 }
762 $page->param(axfrresults => \@results);
763 }
764
[48]765} elsif ($webvar{page} eq 'whoisq') {
[47]766
[48]767 if ($webvar{qfor}) {
768 use Net::Whois::Raw;
769 use Text::Wrap;
770
771# caching useful?
772#$Net::Whois::Raw::CACHE_DIR = "/var/spool/pwhois/";
773#$Net::Whois::Raw::CACHE_TIME = 60;
774
775 my ($dominfo, $whois_server) = whois($webvar{qfor});
776##fixme: if we're given an IP, try rwhois as well as whois so we get the real final data
777
778 # le sigh. idjits spit out data without linefeeds...
779 $Text::Wrap::columns = 88;
780
781# &%$@%@# high-bit crap. We should probably find a way to properly recode these instead of one-by-one.
782# Mainly an XHTML validation thing.
783 $dominfo =~ s/\xa9/\&copy;/g;
784 $dominfo =~ s/\xae/\&reg;/g;
785
786 $page->param(qfor => $webvar{qfor});
787 $page->param(dominfo => wrap('','',$dominfo));
788 $page->param(whois_server => $whois_server);
789 } else {
790 $page->param(errmsg => "Missing host or domain to query in WHOIS") if $webvar{askaway};
791 }
792
[47]793} elsif ($webvar{page} eq 'log') {
794
795##fixme put in some real log-munching stuff
796##fixme need to add bits to *create* log entries...
[59]797 my $sql = "SELECT user_id, email, name, entry, date_trunc('second',stamp) FROM log WHERE ";
[60]798 my $id = $curgroup; # we do this because the group log may be called from (almost) any page,
799 # but the others are much more limited. this is probably non-optimal.
[61]800 if ($webvar{ltype} && $webvar{ltype} eq 'user') {
[60]801 $sql .= "user_id=?";
802 $id = $webvar{id};
803 $page->param(logfor => 'user '.userFullName($dbh,$id));
804 } elsif ($webvar{ltype} && $webvar{ltype} eq 'dom') {
[59]805 $sql .= "domain_id=?";
806 $id = $webvar{id};
[60]807 $page->param(logfor => 'domain '.domainName($dbh,$id));
[59]808 } else {
809 # Default to listing curgroup log
810 $sql .= "group_id=?";
[60]811 $page->param(logfor => 'group '.groupName($dbh,$id));
[59]812 }
813 my $sth = $dbh->prepare($sql);
814 $sth->execute($id);
[47]815 my @logbits;
[59]816 while (my ($uid, $email, $name, $entry, $stamp) = $sth->fetchrow_array) {
[47]817 my %row;
[59]818 $row{userfname} = $name;
819 $row{userid} = $uid;
820 $row{useremail} = $email;
821 $row{logentry} = $entry;
822 ($row{logtime}) = ($stamp =~ /^(.+)-\d\d$/);
[47]823 push @logbits, \%row;
824 }
825 $page->param(logentries => \@logbits);
826
[60]827} # end $webvar{page} dance
[2]828
829
[17]830# start output here so we can redirect pages.
[7]831print "Content-type: text/html\n\n", $header->output;
832
[20]833##common bits
[17]834if ($webvar{page} ne 'login') {
[30]835 $page->param(username => $session->param("username"));
836
[20]837 $page->param(group => $curgroup);
838 $page->param(groupname => groupName($dbh,$curgroup));
[43]839 $page->param(logingrp => groupName($dbh,$logingroup));
[20]840
[24]841 # group tree. should go elsewhere, probably
842 my $tmpgrplist = fill_grptree($logingroup,$curgroup);
843 $page->param(grptree => $tmpgrplist);
[65]844 $page->param(subs => ($tmpgrplist ? 1 : 0)); # probably not useful to pass gobs of data in for a boolean
[42]845 $page->param(inlogingrp => $curgroup == $logingroup);
846
[20]847 # stuff for menu group change. nb: this is icky.
848 fill_grouplist("grouplist");
[53]849
850## set up "URL to self"
[38]851# @#$%@%@#% XHTML - & in a URL must be escaped. >:(
852 my $tmp_ruri = $ENV{REQUEST_URI};
853 $tmp_ruri =~ s/\&([a-z])/\&amp\;$1/g;
[53]854
[39]855# le sigh. and we need to strip any previous action
856 $tmp_ruri =~ s/\&amp;action=[^&]+//g;
[52]857
[53]858# and search filter options. these get stored in the session, but discarded
859# as soon as you switch to a different page.
860##fixme: think about retaining these on a per-page basis, as well as offset; same as the sort-order bits
861 no warnings qw(uninitialized);
862 $tmp_ruri =~ s/\&amp;startwith=[a-z09-]*(\&)?/$1/g;
863 $tmp_ruri =~ s/\&amp;searchsubs=[a-z09-]*(\&)?/$1/g;
864 $tmp_ruri =~ s/\&amp;filter=[a-z09-]*(\&)?/$1/g;
865 use warnings qw(uninitialized);
[52]866
[53]867# fill in the URL-to-self
[38]868 $page->param(whereami => $tmp_ruri);
[17]869}
[13]870
[24]871foreach (@debugbits) { print; }
872
[2]873# spit it out
874print $page->output;
875
[38]876if ($debugenv) {
877 print "<div id=\"debug\">webvar keys: <pre>\n";
878 foreach my $key (keys %webvar) {
879 print "key: $key\tval: $webvar{$key}\n";
880 }
881 print "</pre>\nsession:\n<pre>\n";
882 my $sesdata = $session->dataref();
883 foreach my $key (keys %$sesdata) {
884 print "key: $key\tval: ".$sesdata->{$key}."\n";
885 }
886 print "</pre>\nENV:\n<pre>\n";
887 foreach my $key (keys %ENV) {
888 print "key: $key\tval: $ENV{$key}\n";
889 }
890 print "</pre></div>\n";
[2]891}
892
893print $footer->output;
894
[18]895# as per the docs, Just In Case
896$session->flush();
[2]897
898exit 0;
899
900
[24]901sub fill_grptree {
902 my $root = shift;
903 my $cur = shift;
904
905 my @childlist;
906
907 my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
908 getChildren($dbh,$root,\@childlist,'immediate');
909 return if $#childlist == -1;
910 my @grouplist;
[65]911 my $foome = 0;
[24]912 foreach (@childlist) {
913 my %row;
914 $row{grpname} = groupName($dbh,$_);
915 $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
916 $row{subs} = fill_grptree($_,$cur);
[65]917 $row{last} = 1 if ++$foome > $#childlist;
[24]918 push @grouplist, \%row;
919 }
920 $grptree->param(treelvl => \@grouplist);
921 return $grptree->output;
922}
923
[11]924sub changepage {
925 my %params = @_; # think this works the way I want...
926
927 # handle user check
928 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
929 foreach (keys %params) {
930 $newurl .= "&$_=$params{$_}";
931 }
932
[30]933 # Just In Case
934 $session->flush();
935
[11]936 print "Status: 302\nLocation: $newurl\n\n";
937 exit;
938} # end changepage
939
[2]940sub fillsoa {
941 my $def = shift;
942 my $id = shift;
[39]943 my $domname = ($def eq 'y' ? '' : "DOMAIN");
[2]944
[39]945 $page->param(defrec => $def);
[2]946
[39]947# i had a good reason to do this when I wrote it...
948# $page->param(domain => $domname);
949# $page->param(group => $DNSDB::group);
950 $page->param(isgrp => 1) if $def eq 'y';
951 $page->param(parent => ($def eq 'y' ? groupName($dbh, $DNSDB::group) : domainName($dbh, $id)) );
[2]952
953# defaults
[17]954 $page->param(defcontact => $DNSDB::def{contact});
955 $page->param(defns => $DNSDB::def{prins});
956 $page->param(defsoattl => $DNSDB::def{soattl});
957 $page->param(defrefresh => $DNSDB::def{refresh});
958 $page->param(defretry => $DNSDB::def{retry});
959 $page->param(defexpire => $DNSDB::def{expire});
960 $page->param(defminttl => $DNSDB::def{minttl});
[2]961
962 # there are probably better ways to do this. TMTOWTDI.
963 my %soa = getSOA($dbh,$def,$id);
964
[39]965 $page->param(id => $id);
[2]966 $page->param(recid => $soa{recid});
967 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
968 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
969 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
970 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
971 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
972 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
973 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
974}
975
976sub showdomain {
977 my $def = shift;
978 my $id = shift;
979
980 # get the SOA first
981 my %soa = getSOA($dbh,$def,$id);
982
983 $page->param(recid => $soa{recid});
984 $page->param(contact => $soa{contact});
985 $page->param(prins => $soa{prins});
986 $page->param(refresh => $soa{refresh});
987 $page->param(retry => $soa{retry});
988 $page->param(expire => $soa{expire});
989 $page->param(minttl => $soa{minttl});
990 $page->param(ttl => $soa{ttl});
991
992# my @foo2 = getDomRecs($dbh,'def',1);
[4]993 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
[2]994
995 my $row = 0;
996 foreach my $rec (@$foo2) {
997 $rec->{type} = $typemap{$rec->{type}};
998 $rec->{row} = $row % 2;
[62]999 $rec->{defrec} = $def;
[2]1000 $rec->{sid} = $webvar{sid};
[13]1001 $rec->{id} = $id;
[23]1002 $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
1003 $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
1004 $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
[2]1005 $row++;
1006 }
1007 $page->param(reclist => $foo2);
1008}
1009
[16]1010# fill in record type list on add/update/edit record template
1011sub fill_rectypes {
[13]1012 my $type = shift || $reverse_typemap{A};
[31]1013 my $soaflag = shift || 0;
[13]1014
[17]1015 my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
[2]1016 $sth->execute;
1017 my @typelist;
1018 while (my ($rval,$rname) = $sth->fetchrow_array()) {
1019 my %row = ( recval => $rval, recname => $rname );
[13]1020 $row{tselect} = 1 if $rval == $type;
[2]1021 push @typelist, \%row;
1022 }
[31]1023 if ($soaflag) {
1024 my %row = ( recval => $reverse_typemap{SOA}, recname => 'SOA' );
1025 $row{tselect} = 1 if $reverse_typemap{SOA} == $type;
1026 push @typelist, \%row;
1027 }
[2]1028 $page->param(typelist => \@typelist);
[31]1029} # fill_rectypes
[16]1030
1031sub fill_recdata {
1032 fill_rectypes($webvar{type});
1033
1034 $page->param(name => $webvar{name});
1035 $page->param(address => $webvar{address});
1036 $page->param(distance => $webvar{distance})
1037 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
1038 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
1039 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
[2]1040 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
1041}
[7]1042
[24]1043sub fill_actypelist {
1044 my @actypes;
1045
1046 my %row1 = (actypeval => 'u', actypename => 'user');
1047 $row1{typesel} = 1 if $webvar{accttype} eq 'u';
1048 push @actypes, \%row1;
1049
1050 my %row2 = (actypeval => 'S', actypename => 'superuser');
1051 $row2{typesel} = 1 if $webvar{accttype} eq 'S';
1052 push @actypes, \%row2;
1053
1054 $page->param(actypelist => \@actypes);
1055}
1056
[65]1057sub fill_clonemelist {
1058 my $sth = $dbh->prepare("SELECT username,user_id FROM users WHERE group_id=$curgroup");
1059 $sth->execute;
1060
1061 my @clonesrc;
1062 while (my ($username,$uid) = $sth->fetchrow_array) {
1063 my %row = (
1064 username => $username,
1065 uid => $uid,
1066 selected => ($webvar{clonesrc} == $uid ? 1 : 0)
1067 );
1068 push @clonesrc, \%row;
1069 }
1070 $page->param(clonesrc => \@clonesrc);
1071}
1072
[7]1073sub fill_fpnla {
1074 my $count = shift;
1075##fixme
1076 if ($offset eq 'all') {
[41]1077# uhm....
[7]1078 } else {
1079 # all these bits only have sensible behaviour if offset is numeric. err, probably.
1080 if ($count > $perpage) {
1081 # if there are more results than the default, always show the "all" link
1082 $page->param(navall => 1);
1083
1084 if ($offset > 0) {
1085 $page->param(navfirst => 1);
1086 $page->param(navprev => 1);
1087 $page->param(prevoffs => $offset-1);
1088 }
1089
1090 # show "next" and "last" links if we're not on the last page of results
1091 if ( (($offset+1) * $perpage - $count) < 0 ) {
1092 $page->param(navnext => 1);
1093 $page->param(nextoffs => $offset+1);
1094 $page->param(navlast => 1);
[8]1095 $page->param(lastoffs => int (($count-1)/$perpage));
[7]1096 }
1097 }
1098 }
[10]1099} # end fill_fpnla()
1100
[12]1101sub fill_pgcount {
1102 my $pgcount = shift;
1103 my $pgtype = shift;
1104 my $parent = shift;
1105
1106 $page->param(ntot => $pgcount);
1107 $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
1108 $page->param(npglast => ($offset eq 'all' ? $pgcount :
1109 ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
1110 ));
1111 $page->param(pgtype => $pgtype);
1112 $page->param(parent => $parent);
1113} # end fill_pgcount()
1114
[11]1115sub listdomains {
[41]1116
[62]1117 $startwith = $session->param($webvar{page}.'startwith');
1118 $filter = $session->param($webvar{page}.'filter');
1119 $searchsubs = $session->param($webvar{page}.'searchsubs');
1120
[53]1121##fixme: $logingroup or $curgroup?
[52]1122 my @childgroups;
[61]1123 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1124 my $childlist = join(',',@childgroups);
1125
[57]1126 my $sql = "SELECT count(*) FROM domains WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[53]1127 ($startwith ? " AND domain ~* '^[$startwith]'" : '').
1128 ($filter ? " AND domain ~* '$filter'" : '');
[52]1129 my $sth = $dbh->prepare($sql);
1130 $sth->execute;
[17]1131 my ($count) = $sth->fetchrow_array;
1132
[12]1133# fill page count and first-previous-next-last-all bits
1134##fixme - hardcoded group bit
[20]1135 fill_pgcount($count,"domains",groupName($dbh,$curgroup));
[10]1136 fill_fpnla($count);
1137
[41]1138# sort/order
[51]1139 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1140 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[41]1141
[51]1142 $sortby = $session->param($webvar{page}.'sortby');
1143 $sortorder = $session->param($webvar{page}.'order');
1144
[44]1145# set up the headers
1146 my @cols = ('domain', 'status', 'group');
1147 my %colheads = (domain => 'Domain', status => 'Status', group => 'Group');
[54]1148 fill_colheads($sortby, $sortorder, \@cols, \%colheads);
[41]1149
[47]1150# $page->param(sortorder => $sortorder);
[41]1151 # hack! hack! pthbttt. have to rethink the status column storage,
1152 # or inactive comes "before" active. *sigh*
1153 $sortorder = ($sortorder eq 'ASC' ? 'DESC' : 'ASC') if $sortby eq 'status';
1154
[51]1155# waffle, waffle - keep state on these as well as sortby, sortorder?
[53]1156 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[41]1157
[53]1158 $page->param(filter => $filter) if $filter;
1159 $page->param(searchsubs => $searchsubs) if $searchsubs;
[41]1160
1161##fixme
1162##fixme push the SQL and direct database fiddling off into a sub in DNSDB.pm
1163##fixme
1164
[20]1165 $page->param(group => $curgroup);
[10]1166 my @domlist;
[52]1167 $sql = "SELECT domain_id,domain,status,groups.group_name AS group FROM domains".
[20]1168 " INNER JOIN groups ON domains.group_id=groups.group_id".
[57]1169 " WHERE domains.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[41]1170##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1171 ($startwith ? " AND domain ~* '^[$startwith]'" : '').
1172 ($filter ? " AND domain ~* '$filter'" : '').
[41]1173 " ORDER BY ".($sortby eq 'group' ? 'groups.group_name' : $sortby).
1174 " $sortorder ".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
1175 $sth = $dbh->prepare($sql);
[52]1176 $sth->execute;
[10]1177 my $rownum = 0;
1178 while (my @data = $sth->fetchrow_array) {
1179 my %row;
1180 $row{domainid} = $data[0];
1181 $row{domain} = $data[1];
1182 $row{status} = ($data[2] ? 'Active' : 'Inactive');
1183 $row{group} = $data[3];
1184 $row{bg} = ($rownum++)%2;
1185# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
1186 $row{mkactive} = !$data[2];
1187 $row{sid} = $sid;
1188 $row{offset} = $offset;
1189##fixme: need to clean up status indicator/usage/inversion
1190 push @domlist, \%row;
1191 }
1192 $page->param(domtable => \@domlist);
[11]1193} # end listdomains()
[18]1194
[22]1195sub listgroups {
[53]1196
[26]1197 my @childgroups;
[53]1198 getChildren($dbh, $logingroup, \@childgroups, 'all') if $searchsubs;
[26]1199 my $childlist = join(',',@childgroups);
1200
[54]1201 my $sql = "SELECT count(*) FROM groups WHERE parent_group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
[53]1202 ($startwith ? " AND group_name ~* '^[$startwith]'" : '').
1203 ($filter ? " AND group_name ~* '$filter'" : '');
[26]1204 my $sth = $dbh->prepare($sql);
1205
[22]1206 $sth->execute;
1207 my ($count) = ($sth->fetchrow_array);
1208# fill page count and first-previous-next-last-all bits
1209##fixme - hardcoded group bit
1210 fill_pgcount($count,"groups",'');
1211 fill_fpnla($count);
1212
[51]1213# $sortby = 'group';
[42]1214# sort/order
[51]1215 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1216 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[42]1217
[51]1218 $sortby = $session->param($webvar{page}.'sortby');
1219 $sortorder = $session->param($webvar{page}.'order');
1220
[44]1221# set up the headers
1222 my @cols = ('group','parent','nusers','ndomains');
1223 my %colnames = (group => 'Group', parent => 'Parent Group', nusers => 'Users', ndomains => 'Domains');
[54]1224 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[42]1225
[51]1226# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1227 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1228
[53]1229 $page->param(filter => $filter) if $filter;
1230 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1231
1232# munge sortby for columns in database
1233 $sortby = 'g.group_name' if $sortby eq 'group';
1234 $sortby = 'g2.group_name' if $sortby eq 'parent';
1235
[22]1236 my @grouplist;
1237 $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
[51]1238 "count(distinct(u.username)) AS nusers, count(distinct(d.domain)) AS ndomains ".
[22]1239 "FROM groups g ".
1240 "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
1241 "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
1242 "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
[54]1243 "WHERE g.parent_group_id IN ($logingroup".($childlist ? ",$childlist" : '').") ".
[51]1244##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1245 ($startwith ? " AND g.group_name ~* '^[$startwith]'" : '').
1246 ($filter ? " AND g.group_name ~* '$filter'" : '').
[51]1247 " GROUP BY g.group_id, g.group_name, g2.group_name ".
1248 " ORDER BY $sortby $sortorder ".
1249 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
[22]1250 $sth->execute;
1251
1252 my $rownum = 0;
1253 while (my @data = $sth->fetchrow_array) {
1254 my %row;
1255 $row{groupid} = $data[0];
1256 $row{groupname} = $data[1];
1257 $row{pgroup} = $data[2];
1258 $row{nusers} = $data[3];
1259 $row{ndomains} = $data[4];
1260 $row{bg} = ($rownum++)%2;
1261 $row{sid} = $sid;
1262 push @grouplist, \%row;
1263 }
1264 $page->param(grouptable => \@grouplist);
1265} # end listgroups()
1266
[20]1267sub fill_grouplist {
[19]1268 my $template_var = shift;
1269 my $cur = shift || $curgroup;
[26]1270
1271 my @childgroups;
1272 getChildren($dbh, $logingroup, \@childgroups, 'all');
1273 my $childlist = join(',',@childgroups);
1274
[18]1275 # weesa gonna discard parent_group_id for now
[26]1276 my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ".
1277 "WHERE group_id IN ($logingroup".($childlist ? ",$childlist" : '').")".
1278 "ORDER BY group_id");
[18]1279 $sth->execute;
[20]1280 my @grouplist;
1281 while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
[18]1282 my %row;
[20]1283 $row{groupname} = $groupname;
1284 $row{groupval} = $groupid;
[18]1285##fixme: need magic
[20]1286# $row{defgroup} = '';
1287 $row{groupactive} = 1 if $groupid == $cur;
1288 push @grouplist, \%row;
[18]1289 }
1290
[20]1291 $page->param("$template_var" => \@grouplist);
[18]1292
[24]1293} # end fill_grouplist()
1294
1295sub list_users {
[52]1296
1297 my @childgroups;
[53]1298 getChildren($dbh, $curgroup, \@childgroups, 'all') if $searchsubs;
[52]1299 my $childlist = join(',',@childgroups);
1300
1301 my $sql = "SELECT count(*) FROM users WHERE group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[53]1302 ($startwith ? " AND username ~* '^[$startwith]'" : '').
1303 ($filter ? " AND username ~* '$filter'" : '');
[52]1304 my $sth = $dbh->prepare($sql);
1305 $sth->execute;
[24]1306 my ($count) = ($sth->fetchrow_array);
1307
1308# fill page count and first-previous-next-last-all bits
1309##fixme - hardcoded group bit
1310 fill_pgcount($count,"users",'');
1311 fill_fpnla($count);
1312
[51]1313# $sortby = 'user';
[44]1314# sort/order
[51]1315 $session->param($webvar{page}.'sortby', $webvar{sortby}) if $webvar{sortby};
1316 $session->param($webvar{page}.'order', $webvar{order}) if $webvar{order};
[44]1317
[51]1318 $sortby = $session->param($webvar{page}.'sortby');
1319 $sortorder = $session->param($webvar{page}.'order');
1320
[44]1321# set up the headers
1322 my @cols = ('user','fname','type','group','status');
1323 my %colnames = (user => 'Username', fname => 'Full Name', type => 'Type', group => 'Group', status => 'Status');
[54]1324 fill_colheads($sortby, $sortorder, \@cols, \%colnames);
[44]1325
[51]1326# waffle, waffle - keep state on these as well as sortby, sortorder?
[64]1327 $page->param("start$startwith" => 1) if $startwith && $startwith =~ /^(?:[a-z]|0-9)$/;
[51]1328
[53]1329 $page->param(filter => $filter) if $filter;
1330 $page->param(searchsubs => $searchsubs) if $searchsubs;
[51]1331
1332# munge sortby for columns in database
1333 $sortby = 'u.username' if $sortby eq 'user';
1334 $sortby = 'u.type' if $sortby eq 'type';
1335 $sortby = 'g.group_name' if $sortby eq 'group';
1336 $sortby = 'u.status' if $sortby eq 'status';
1337
[24]1338 my @userlist;
[52]1339 $sql = "SELECT u.user_id, u.username, u.firstname || ' ' || u.lastname AS fname, u.type, g.group_name, u.status ".
[24]1340 "FROM users u ".
1341 "INNER JOIN groups g ON u.group_id=g.group_id ".
[52]1342 "WHERE u.group_id IN ($curgroup".($childlist ? ",$childlist" : '').")".
[51]1343##fixme: don't do variable subs in SQL, use placeholders and params in ->execute()
[53]1344 ($startwith ? " AND u.username ~* '^[$startwith]'" : '').
1345 ($filter ? " AND u.username ~* '$filter'" : '').
[51]1346 " ORDER BY $sortby $sortorder ".
[52]1347 ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage);
[51]1348
[52]1349 $sth = $dbh->prepare($sql);
1350 $sth->execute;
[24]1351
1352 my $rownum = 0;
1353 while (my @data = $sth->fetchrow_array) {
[41]1354 no warnings "uninitialized"; # Just In Case something stupid happens and a user gets no first or last name
[24]1355 my %row;
1356 $row{userid} = $data[0];
1357 $row{username} = $data[1];
[51]1358 $row{userfull} = $data[2];
1359 $row{usertype} = ($data[3] eq 'S' ? 'superuser' : "user");
1360 $row{usergroup} = $data[4];
1361 $row{active} = $data[5];
[24]1362 $row{bg} = ($rownum++)%2;
1363 $row{sid} = $sid;
1364 push @userlist, \%row;
1365 }
1366 $page->param(usertable => \@userlist);
[55]1367} # end list_users()
[43]1368
1369# Generate all of the glop necessary to add or not the appropriate marker/flag for
1370# the sort order and column in domain, user, group, and record lists
1371# Takes an array ref and hash ref
1372sub fill_colheads {
[54]1373 my $sortby = shift;
1374 my $sortorder = shift;
[43]1375 my $cols = shift;
1376 my $colnames = shift;
1377
1378 my @headings;
1379
1380 foreach my $col (@$cols) {
1381 my %coldata;
1382 $coldata{firstcol} = 1 if $col eq $cols->[0];
1383 $coldata{sid} = $sid;
1384 $coldata{page} = $webvar{page};
1385 $coldata{offset} = $webvar{offset} if $webvar{offset};
1386 $coldata{sortby} = $col;
1387 $coldata{colname} = $colnames->{$col};
1388 if ($col eq $sortby) {
1389 $coldata{order} = ($sortorder eq 'ASC' ? 'DESC' : 'ASC');
1390 $coldata{sortorder} = $sortorder;
1391 } else {
1392 $coldata{order} = 'ASC';
1393 }
1394 push @headings, \%coldata;
1395 }
1396
1397 $page->param(colheads => \@headings);
1398
[54]1399} # end fill_colheads()
[55]1400
1401sub logaction {
[59]1402 my $domid = shift;
1403 my $username = shift;
1404 my $groupid = shift;
1405 my $entry = shift;
[55]1406
[59]1407##fixme: ooohhh, nasty. what if we get a failure *here*? PTHBTT!
1408 my $sth = $dbh->prepare("SELECT user_id, firstname || ' ' || lastname FROM users WHERE username=?");
[55]1409 $sth->execute($username);
1410 my ($user_id, $fullname) = $sth->fetchrow_array;
1411
1412 $sth = $dbh->prepare("INSERT INTO log (domain_id,user_id,group_id,email,name,entry) ".
1413 "VALUES (?,?,?,?,?,?)");
[57]1414 $sth->execute($domid,$user_id,$groupid,$username,$fullname,$entry);
[55]1415} # end logaction()
[57]1416
[59]1417##fixme: generalize to return appropriate id on all cases (ie, use $partype)
[57]1418sub parentID {
1419 my $id = shift;
1420 my $idtype = shift;
1421 my $partype = shift;
1422 my $defrec = shift || '';
1423
1424 my $sql = '';
1425
1426 if ($idtype eq 'dom') {
[59]1427 return $id if $defrec eq 'y'; # "domain" + default records, we're really looking at a group.
[57]1428 $sql = "SELECT group_id FROM domains WHERE domain_id=?";
1429 } elsif ($idtype eq 'rec') {
[59]1430 if ($defrec eq 'y') {
1431 $sql = "SELECT group_id FROM default_records WHERE record_id=?";
[57]1432 } else {
1433 return
1434 $sql = "SELECT d.group_id FROM domains d".
1435 " INNER JOIN records r ON d.domain_id=r.domain_id".
1436 " WHERE r.record_id=?";
1437 }
1438 } elsif ($idtype eq 'group') {
1439 $sql = "SELECT parent_group_id FROM groups WHERE group_id=?";
1440 } elsif ($idtype eq 'user') {
1441 $sql = "SELECT group_id FROM users WHERE user_id=?";
1442 } else {
1443 return "FOO", "BAR"; # can't get here.... we think.
1444 }
[59]1445 my $sth = $dbh->prepare($sql);
1446 $sth->execute($id);
1447 my ($retid) = $sth->fetchrow_array;
1448 return $retid if $retid;
1449 # ahh! fall of the edge of the world if things went sideways
1450 ##fixme: really need to do a little more error handling, I think
[64]1451} # end parentID()
Note: See TracBrowser for help on using the repository browser.