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