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