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