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