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