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