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