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