| [2] | 1 | #!/usr/bin/perl -w -T | 
|---|
|  | 2 | # dns/cgi-bin/dns.cgi | 
|---|
|  | 3 | ### | 
|---|
|  | 4 | # SVN revision info | 
|---|
|  | 5 | # $Date: 2009-10-15 17:23:51 +0000 (Thu, 15 Oct 2009) $ | 
|---|
|  | 6 | # SVN revision $Rev: 20 $ | 
|---|
|  | 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; | 
|---|
|  | 18 | use DBI; | 
|---|
|  | 19 |  | 
|---|
|  | 20 | use lib '.'; | 
|---|
|  | 21 | # custom modules | 
|---|
|  | 22 | use DNSDB qw(:ALL); | 
|---|
|  | 23 |  | 
|---|
| [13] | 24 | my @debugbits;  # temp, to be spit out near the end of processing | 
|---|
|  | 25 |  | 
|---|
| [2] | 26 | # Let's do these templates right... | 
|---|
|  | 27 | my $templatedir = "templates"; | 
|---|
|  | 28 | my $sessiondir = "session"; | 
|---|
|  | 29 |  | 
|---|
|  | 30 | # Set up the CGI object... | 
|---|
|  | 31 | my $q = new CGI::Simple; | 
|---|
|  | 32 | # ... and get query-string params as well as POST params if necessary | 
|---|
|  | 33 | $q->parse_query_string; | 
|---|
|  | 34 |  | 
|---|
|  | 35 | # This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about... | 
|---|
| [7] | 36 | my %webvar = $q->Vars; | 
|---|
| [2] | 37 |  | 
|---|
| [13] | 38 | # persistent stuff needed on most/all pages | 
|---|
| [2] | 39 | my $sid = ($webvar{sid} ? $webvar{sid} : undef); | 
|---|
|  | 40 | my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir}); | 
|---|
|  | 41 | #$sid = $session->id() if !$sid; | 
|---|
|  | 42 | if (!$sid) { | 
|---|
|  | 43 | # init stuff.  can probably axe this down to just above if'n'when user manipulation happens | 
|---|
|  | 44 | $sid = $session->id(); | 
|---|
|  | 45 | # need to know the "upper" group the user can deal with;  may as well | 
|---|
|  | 46 | # stick this in the session rather than calling out to the DB every time. | 
|---|
| [18] | 47 | $session->param('logingroup',1); | 
|---|
|  | 48 | $session->param('curgroup',1);        # yes, we *do* need to track this too.  er, probably. | 
|---|
| [2] | 49 | } | 
|---|
|  | 50 |  | 
|---|
| [19] | 51 | my $logingroup = ($session->param('logingroup') ? $session->param('logingroup') : 1); | 
|---|
|  | 52 | my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $logingroup); | 
|---|
| [17] | 53 | my $group = ($webvar{group} ? $webvar{group} : 1); | 
|---|
| [18] | 54 |  | 
|---|
| [2] | 55 | # handle login redirect | 
|---|
|  | 56 | if ($webvar{action} && $webvar{action} eq 'login') { | 
|---|
| [11] | 57 | ##fixme:  need to actually do a user/pass check | 
|---|
|  | 58 | changepage(page => "domlist"); | 
|---|
| [2] | 59 | } | 
|---|
|  | 60 |  | 
|---|
| [20] | 61 | if ($webvar{action} && $webvar{action} eq 'chgroup') { | 
|---|
|  | 62 | # fiddle session-stored group data | 
|---|
|  | 63 | # magic incantation to... uhhh... | 
|---|
|  | 64 | $session->param('curgroup', $webvar{group}); | 
|---|
|  | 65 | $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup')); | 
|---|
|  | 66 | } | 
|---|
|  | 67 |  | 
|---|
| [2] | 68 | my $header = HTML::Template->new(filename => "$templatedir/header.tmpl"); | 
|---|
|  | 69 | my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl"); | 
|---|
|  | 70 |  | 
|---|
|  | 71 | # default | 
|---|
| [3] | 72 | #my $perpage = 15; | 
|---|
| [15] | 73 | my $perpage = 3; | 
|---|
| [2] | 74 | my $offset = ($webvar{offset} ? $webvar{offset} : 0); | 
|---|
|  | 75 |  | 
|---|
|  | 76 | # NB:  these must match the field name and SQL ascend/descend syntax respectively | 
|---|
|  | 77 | my $sortfield = "domains"; | 
|---|
|  | 78 | my $sortorder = "asc"; | 
|---|
|  | 79 |  | 
|---|
|  | 80 | my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret"); | 
|---|
|  | 81 | #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret", | 
|---|
|  | 82 | #       { AutoCommit => 0 }) or die $DBI::errstr; | 
|---|
|  | 83 |  | 
|---|
|  | 84 | ##fixme.  PLEASE!  <G> | 
|---|
|  | 85 | print $msg if !$dbh; | 
|---|
|  | 86 |  | 
|---|
|  | 87 | # fiddle hardcoded "defaults" as per system/user (?) prefs | 
|---|
|  | 88 | initGlobals($dbh); | 
|---|
|  | 89 |  | 
|---|
| [15] | 90 | ## Default page is a login page | 
|---|
|  | 91 | #my $page;      # to be initialized as an HTML::Template entity sooner or later | 
|---|
| [2] | 92 |  | 
|---|
| [3] | 93 |  | 
|---|
|  | 94 |  | 
|---|
| [2] | 95 | # decide which page to spit out... | 
|---|
| [15] | 96 | $webvar{page} = 'login' if !$webvar{page}; | 
|---|
|  | 97 | #if (!$webvar{page}) { | 
|---|
|  | 98 | #  $page = HTML::Template->new(filename => "$templatedir/login.tmpl"); | 
|---|
|  | 99 | #} else { | 
|---|
|  | 100 | #} | 
|---|
| [2] | 101 |  | 
|---|
| [15] | 102 | my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl"); | 
|---|
|  | 103 |  | 
|---|
| [2] | 104 | $page->param(sid => $sid); | 
|---|
|  | 105 |  | 
|---|
|  | 106 | if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') { | 
|---|
| [3] | 107 |  | 
|---|
|  | 108 | # hmm.  seeing problems in some possibly-not-so-corner cases. | 
|---|
| [10] | 109 | # this currently only handles "domain on", "domain off" | 
|---|
| [3] | 110 | if (defined($webvar{action})) { | 
|---|
|  | 111 | domStatus($dbh,$webvar{id},$webvar{action}); | 
|---|
|  | 112 | } | 
|---|
|  | 113 |  | 
|---|
| [18] | 114 | $page->param(curpage => $webvar{page}); | 
|---|
|  | 115 |  | 
|---|
| [11] | 116 | listdomains(); | 
|---|
| [2] | 117 |  | 
|---|
|  | 118 | } elsif ($webvar{page} eq 'reclist') { | 
|---|
|  | 119 |  | 
|---|
| [4] | 120 | # Handle record list for both default records (per-group) and live domain records | 
|---|
| [2] | 121 |  | 
|---|
|  | 122 | $page->param(defrec => $webvar{defrec}); | 
|---|
| [4] | 123 | $page->param(id => $webvar{id}); | 
|---|
| [18] | 124 | $page->param(curpage => $webvar{page}); | 
|---|
| [2] | 125 |  | 
|---|
| [4] | 126 | # select count(*) from (default_)?records where (group|domain)_id=? | 
|---|
|  | 127 | my $sth = $dbh->prepare("SELECT count(*) FROM ". | 
|---|
|  | 128 | ($webvar{defrec} eq 'y' ? 'default_' : '')."records ". | 
|---|
|  | 129 | "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ". | 
|---|
|  | 130 | "AND NOT type=$reverse_typemap{SOA}"); | 
|---|
|  | 131 | $sth->execute($webvar{id}); | 
|---|
|  | 132 | my ($count) = ($sth->fetchrow_array); | 
|---|
|  | 133 |  | 
|---|
| [12] | 134 | # fill the page-count and first-previous-next-last-all details | 
|---|
|  | 135 | fill_pgcount($count,"records",domainName($dbh,$webvar{id})); | 
|---|
| [7] | 136 | fill_fpnla($count);  # should put some params on this sub... | 
|---|
| [4] | 137 |  | 
|---|
|  | 138 | $page->param(defrec => $webvar{defrec}); | 
|---|
| [2] | 139 | if ($webvar{defrec} eq 'y') { | 
|---|
| [12] | 140 | ##fixme: hardcoded group | 
|---|
| [20] | 141 | showdomain('y',$curgroup); | 
|---|
| [2] | 142 | } else { | 
|---|
|  | 143 | showdomain('n',$webvar{id}); | 
|---|
|  | 144 | } | 
|---|
|  | 145 |  | 
|---|
| [4] | 146 | } elsif ($webvar{page} eq 'newdomain') { | 
|---|
| [2] | 147 |  | 
|---|
|  | 148 |  | 
|---|
| [11] | 149 | } elsif ($webvar{page} eq 'deldom') { | 
|---|
|  | 150 |  | 
|---|
|  | 151 | $page->param(id => $webvar{id}); | 
|---|
|  | 152 | # first pass = confirm y/n (sorta) | 
|---|
|  | 153 | if (!defined($webvar{del})) { | 
|---|
|  | 154 | $page->param(del_getconf => 1); | 
|---|
|  | 155 | $page->param(domain => domainName($dbh,$webvar{id})); | 
|---|
|  | 156 | # print some neato things? | 
|---|
|  | 157 |  | 
|---|
|  | 158 | #  } else { | 
|---|
|  | 159 | #    #whether actually deleting or cancelling we redirect to the domain list, default format | 
|---|
|  | 160 |  | 
|---|
|  | 161 | } elsif ($webvar{del} eq 'ok') { | 
|---|
|  | 162 | my ($code,$msg) = delDomain($dbh, $webvar{id}); | 
|---|
|  | 163 | if ($code ne 'OK') { | 
|---|
|  | 164 | # need to find failure mode | 
|---|
|  | 165 | $page->param(del_failed => 1); | 
|---|
|  | 166 | $page->param(errmsg => $msg); | 
|---|
|  | 167 | } else { | 
|---|
|  | 168 | # success.  go back to the domain list, do not pass "GO" | 
|---|
|  | 169 | changepage(page => "domlist"); | 
|---|
|  | 170 | } | 
|---|
|  | 171 | } else { | 
|---|
|  | 172 | # cancelled.  whee! | 
|---|
|  | 173 | changepage(page => "domlist"); | 
|---|
|  | 174 | } | 
|---|
|  | 175 |  | 
|---|
| [13] | 176 | } elsif ($webvar{page} eq 'record') { | 
|---|
| [16] | 177 |  | 
|---|
| [13] | 178 | if ($webvar{recact} eq 'new') { | 
|---|
| [16] | 179 |  | 
|---|
| [15] | 180 | $page->param(todo => "Add record to"); | 
|---|
|  | 181 | $page->param(recact => "add"); | 
|---|
| [16] | 182 | fill_rectypes(); | 
|---|
|  | 183 |  | 
|---|
| [15] | 184 | } elsif ($webvar{recact} eq 'add') { | 
|---|
|  | 185 |  | 
|---|
|  | 186 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl}); | 
|---|
|  | 187 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) { | 
|---|
|  | 188 | push @recargs, $webvar{distance}; | 
|---|
|  | 189 | if ($webvar{type} == $reverse_typemap{SRV}) { | 
|---|
|  | 190 | push @recargs, $webvar{weight}; | 
|---|
|  | 191 | push @recargs, $webvar{port}; | 
|---|
|  | 192 | } | 
|---|
|  | 193 | } | 
|---|
|  | 194 | my ($code,$msg) = addRec(@recargs); | 
|---|
|  | 195 |  | 
|---|
|  | 196 | if ($code eq 'OK') { | 
|---|
|  | 197 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}); | 
|---|
|  | 198 | } else { | 
|---|
| [16] | 199 | $page->param(failed => 1); | 
|---|
| [15] | 200 | $page->param(errmsg => $msg); | 
|---|
| [16] | 201 | fill_recdata();   # populate the form... er, mostly. | 
|---|
| [15] | 202 | } | 
|---|
|  | 203 |  | 
|---|
| [13] | 204 | } elsif ($webvar{recact} eq 'edit') { | 
|---|
| [15] | 205 |  | 
|---|
| [16] | 206 | $page->param(todo           => "Update record"); | 
|---|
|  | 207 | $page->param(recact         => "update"); | 
|---|
|  | 208 | $page->param(parentid       => $webvar{parentid}); | 
|---|
| [17] | 209 | $page->param(id             => $webvar{id}); | 
|---|
| [16] | 210 | $page->param(defrec         => $webvar{defrec}); | 
|---|
| [13] | 211 | my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ". | 
|---|
|  | 212 | ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?"); | 
|---|
|  | 213 | $sth->execute($webvar{id}); | 
|---|
|  | 214 | my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array; | 
|---|
|  | 215 | $page->param(name           => $host); | 
|---|
|  | 216 | $page->param(address        => $val); | 
|---|
|  | 217 | $page->param(distance       => $distance); | 
|---|
|  | 218 | $page->param(weight         => $weight); | 
|---|
|  | 219 | $page->param(port           => $port); | 
|---|
|  | 220 | $page->param(ttl            => $ttl); | 
|---|
| [16] | 221 | fill_rectypes($type); | 
|---|
|  | 222 |  | 
|---|
|  | 223 | } elsif ($webvar{recact} eq 'update') { | 
|---|
|  | 224 |  | 
|---|
|  | 225 | my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id}, | 
|---|
|  | 226 | $webvar{name},$webvar{type},$webvar{address},$webvar{ttl}, | 
|---|
|  | 227 | $webvar{distance},$webvar{weight},$webvar{port}); | 
|---|
|  | 228 |  | 
|---|
|  | 229 | if ($code eq 'OK') { | 
|---|
| [17] | 230 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec}); | 
|---|
| [16] | 231 | } else { | 
|---|
|  | 232 | $page->param(failed       => 1); | 
|---|
|  | 233 | $page->param(errmsg       => $msg); | 
|---|
|  | 234 | $page->param(wastrying    => "updating"); | 
|---|
|  | 235 | $page->param(todo         => "Update record"); | 
|---|
|  | 236 | $page->param(recact       => "update"); | 
|---|
|  | 237 | $page->param(parentid     => $webvar{parentid}); | 
|---|
|  | 238 | $page->param(defrec       => $webvar{defrec}); | 
|---|
| [17] | 239 | $page->param(id           => $webvar{id}); | 
|---|
| [16] | 240 | fill_recdata(); | 
|---|
|  | 241 | } | 
|---|
| [13] | 242 | } | 
|---|
| [16] | 243 |  | 
|---|
| [13] | 244 | if ($webvar{defrec} eq 'y') { | 
|---|
| [20] | 245 | $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid})); | 
|---|
| [13] | 246 | } else { | 
|---|
| [16] | 247 | $page->param(dohere => domainName($dbh,$webvar{parentid})); | 
|---|
| [13] | 248 | } | 
|---|
|  | 249 |  | 
|---|
| [2] | 250 | } elsif ($webvar{page} eq 'newrec') { | 
|---|
| [13] | 251 | push @debugbits, "whee!\n"; | 
|---|
| [2] | 252 |  | 
|---|
| [3] | 253 | # populate most fields as needed.  (eg, type list.) | 
|---|
| [13] | 254 | stdrecs(); | 
|---|
| [2] | 255 |  | 
|---|
|  | 256 | } elsif ($webvar{page} eq 'addrec') { | 
|---|
|  | 257 |  | 
|---|
|  | 258 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl}); | 
|---|
|  | 259 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) { | 
|---|
|  | 260 | push @recargs, $webvar{distance}; | 
|---|
|  | 261 | if ($webvar{type} == $reverse_typemap{SRV}) { | 
|---|
|  | 262 | push @recargs, $webvar{weight}; | 
|---|
|  | 263 | push @recargs, $webvar{port}; | 
|---|
|  | 264 | } | 
|---|
|  | 265 | } | 
|---|
| [13] | 266 | # wtf? | 
|---|
|  | 267 | #  push @recargs, | 
|---|
| [2] | 268 | my ($code,$msg) = addRec(@recargs); | 
|---|
|  | 269 |  | 
|---|
|  | 270 | if ($code eq 'OK') { | 
|---|
|  | 271 | showdomain($webvar{defrec},$webvar{parentid}); | 
|---|
|  | 272 | # NB: should **really** redirect here, in case of reload.  >_<  eyowch. | 
|---|
|  | 273 | } else { | 
|---|
|  | 274 | $page->param(add_failed => 1); | 
|---|
|  | 275 | $page->param(errmsg => $msg); | 
|---|
| [13] | 276 | stdrecs($webvar{type});     # populate the form... er, mostly. | 
|---|
| [2] | 277 | $page->param(name => $webvar{name}); | 
|---|
|  | 278 | $page->param(address => $webvar{address}); | 
|---|
|  | 279 | $page->param(distance => $webvar{distance}) | 
|---|
|  | 280 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}); | 
|---|
|  | 281 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV}; | 
|---|
|  | 282 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV}; | 
|---|
|  | 283 | } | 
|---|
|  | 284 |  | 
|---|
| [3] | 285 | $page->param(defrec => $webvar{defrec}); | 
|---|
|  | 286 |  | 
|---|
| [2] | 287 | } elsif ($webvar{page} eq 'conf_del') { | 
|---|
|  | 288 |  | 
|---|
|  | 289 | $page->param(id => $webvar{id}); | 
|---|
|  | 290 | $page->param(defrec => $webvar{defrec}); | 
|---|
|  | 291 |  | 
|---|
|  | 292 | my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec}); | 
|---|
|  | 293 |  | 
|---|
|  | 294 | } elsif ($webvar{page} eq 'delrec') { | 
|---|
|  | 295 |  | 
|---|
|  | 296 | $page->param(id => $webvar{id}); | 
|---|
|  | 297 | $page->param(defrec => $webvar{defrec}); | 
|---|
|  | 298 | # first pass = confirm y/n (sorta) | 
|---|
|  | 299 | if (!defined($webvar{del})) { | 
|---|
|  | 300 | $page->param(del_getconf => 1); | 
|---|
| [3] | 301 | my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id}); | 
|---|
|  | 302 | $page->param(host => $rec{host}); | 
|---|
|  | 303 | $page->param(ftype => $typemap{$rec{type}}); | 
|---|
|  | 304 | $page->param(recval => $rec{val}); | 
|---|
| [2] | 305 | } else { | 
|---|
| [3] | 306 | my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id}); | 
|---|
|  | 307 | if ($code ne 'OK') { | 
|---|
|  | 308 | ## need to find failure mode | 
|---|
|  | 309 | $page->param(del_failed => 1); | 
|---|
|  | 310 | $page->param(errmsg => $msg); | 
|---|
|  | 311 | } | 
|---|
|  | 312 | ##fixme:  group/parent instead of hardcoded 1 | 
|---|
|  | 313 | showdomain('y',1); | 
|---|
| [2] | 314 | } | 
|---|
|  | 315 |  | 
|---|
|  | 316 | } elsif ($webvar{page} eq 'editsoa') { | 
|---|
|  | 317 |  | 
|---|
|  | 318 | fillsoa($webvar{defrec},$webvar{recid}); | 
|---|
|  | 319 |  | 
|---|
|  | 320 | } elsif ($webvar{page} eq 'updatesoa') { | 
|---|
|  | 321 | print "ooooo!\n"; | 
|---|
|  | 322 |  | 
|---|
|  | 323 | my $sth; | 
|---|
|  | 324 | my $sql = ''; | 
|---|
|  | 325 | # no domain ID, so we're editing the default SOA for a group (we don't care which one here) | 
|---|
|  | 326 | # plus a bit of magic to update the appropriate table | 
|---|
|  | 327 | $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records"). | 
|---|
|  | 328 | " set host='$webvar{prins}:$webvar{contact}',". | 
|---|
|  | 329 | " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',". | 
|---|
|  | 330 | " ttl=$webvar{ttl} where record_id=$webvar{recid}"; | 
|---|
|  | 331 | $sth = $dbh->prepare($sql); | 
|---|
|  | 332 | $sth->execute; | 
|---|
|  | 333 |  | 
|---|
|  | 334 | if ($sth->err) { | 
|---|
|  | 335 | $page->param(update_failed => 1); | 
|---|
|  | 336 | $page->param(msg => $DBI::errstr); | 
|---|
|  | 337 | fillsoa($webvar{defrec},1); | 
|---|
|  | 338 | } else { | 
|---|
|  | 339 | $page->param(update_failed => 0); | 
|---|
|  | 340 | ##fixme!  need to set group ID properly here | 
|---|
|  | 341 | showdomain('y',1); | 
|---|
|  | 342 | } | 
|---|
|  | 343 |  | 
|---|
|  | 344 | } elsif ($webvar{page} eq 'adddomain') { | 
|---|
|  | 345 | # Need some magic here. | 
|---|
|  | 346 |  | 
|---|
|  | 347 | ##fixme:  Group should be variable | 
|---|
| [20] | 348 | my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0)); | 
|---|
| [2] | 349 |  | 
|---|
|  | 350 | # hokay, a bit of magic to decide which page we hit. | 
|---|
|  | 351 | if ($code eq 'OK') { | 
|---|
| [11] | 352 | # redirect to dns.cgi?etc&page=reclist | 
|---|
| [12] | 353 | changepage(page => "reclist", id => $msg); | 
|---|
| [3] | 354 | $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl"); | 
|---|
|  | 355 | showdomain(0,$msg); | 
|---|
|  | 356 | ##work | 
|---|
| [2] | 357 | } else { | 
|---|
|  | 358 | # oooh, yeah, this is supposed to be a redirect.  er, maybe.  whee. | 
|---|
|  | 359 | $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl"); | 
|---|
|  | 360 | $page->param(add_failed => 1); | 
|---|
|  | 361 | $page->param(domain => $webvar{domain}); | 
|---|
|  | 362 | $page->param(errmsg => $msg); | 
|---|
|  | 363 | } | 
|---|
|  | 364 |  | 
|---|
| [17] | 365 | } elsif ($webvar{page} eq 'grpman') { | 
|---|
| [2] | 366 |  | 
|---|
| [17] | 367 | my $sth = $dbh->prepare("select count(*) from groups"); | 
|---|
|  | 368 | $sth->execute; | 
|---|
|  | 369 | my ($count) = ($sth->fetchrow_array); | 
|---|
|  | 370 |  | 
|---|
|  | 371 | # fill page count and first-previous-next-last-all bits | 
|---|
|  | 372 | ##fixme - hardcoded group bit | 
|---|
|  | 373 | fill_pgcount($count,"groups",''); | 
|---|
|  | 374 | fill_fpnla($count); | 
|---|
|  | 375 |  | 
|---|
| [20] | 376 | my @grouplist; | 
|---|
| [17] | 377 | $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ". | 
|---|
|  | 378 | "count(distinct(u.email)), count(distinct(d.domain)) ". | 
|---|
|  | 379 | "FROM groups g ". | 
|---|
|  | 380 | "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ". | 
|---|
|  | 381 | "LEFT OUTER JOIN users u ON u.group_id=g.group_id ". | 
|---|
|  | 382 | "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ". | 
|---|
|  | 383 | "GROUP BY g.group_id, g.group_name, g2.group_name ". | 
|---|
| [18] | 384 | "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage)); | 
|---|
|  | 385 | $sth->execute; | 
|---|
| [17] | 386 |  | 
|---|
|  | 387 | my $rownum = 0; | 
|---|
|  | 388 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 389 | my %row; | 
|---|
| [20] | 390 | $row{groupid} = $data[0]; | 
|---|
|  | 391 | $row{groupname} = $data[1]; | 
|---|
|  | 392 | $row{pgroup} = $data[2]; | 
|---|
| [17] | 393 | $row{nusers} = $data[3]; | 
|---|
|  | 394 | $row{ndomains} = $data[4]; | 
|---|
|  | 395 | $row{bg} = ($rownum++)%2; | 
|---|
|  | 396 | $row{sid} = $sid; | 
|---|
| [20] | 397 | push @grouplist, \%row; | 
|---|
| [17] | 398 | } | 
|---|
| [20] | 399 | $page->param(grouptable => \@grouplist); | 
|---|
| [17] | 400 |  | 
|---|
| [18] | 401 | $page->param(curpage => $webvar{page}); | 
|---|
|  | 402 |  | 
|---|
| [17] | 403 | } elsif ($webvar{page} eq 'newgrp') { | 
|---|
| [20] | 404 |  | 
|---|
| [18] | 405 | # do.. uhh.. stuff.. if we have no webvar{action} | 
|---|
|  | 406 | if ($webvar{action} && $webvar{action} eq 'add') { | 
|---|
|  | 407 | # not gonna provide the 4th param: template-or-clone flag, just yet | 
|---|
|  | 408 | my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup}); | 
|---|
|  | 409 | changepage(page => "grpman") if $code eq 'OK'; | 
|---|
|  | 410 | $page->param(add_failed => 1); | 
|---|
|  | 411 | $page->param(errmsg => $msg); | 
|---|
|  | 412 | $page->param(newgroup => $webvar{newgroup}); | 
|---|
| [20] | 413 | fill_grouplist('pargroup',$webvar{pargroup}); | 
|---|
| [19] | 414 | } else { | 
|---|
|  | 415 | #    $page->param | 
|---|
| [20] | 416 | fill_grouplist('pargroup',$curgroup); | 
|---|
| [19] | 417 |  | 
|---|
| [18] | 418 | } | 
|---|
| [20] | 419 |  | 
|---|
|  | 420 | } elsif ($webvar{page} eq 'delgroup') { | 
|---|
|  | 421 |  | 
|---|
|  | 422 | $page->param(id => $webvar{id}); | 
|---|
|  | 423 | # first pass = confirm y/n (sorta) | 
|---|
|  | 424 | if (!defined($webvar{del})) { | 
|---|
|  | 425 | $page->param(del_getconf => 1); | 
|---|
|  | 426 | $page->param(groupname => groupName($dbh,$webvar{id})); | 
|---|
|  | 427 | # print some neato things? | 
|---|
|  | 428 |  | 
|---|
|  | 429 | #  } else { | 
|---|
|  | 430 | #    #whether actually deleting or cancelling we redirect to the group list, default format | 
|---|
|  | 431 |  | 
|---|
|  | 432 | } elsif ($webvar{del} eq 'ok') { | 
|---|
|  | 433 | my ($code,$msg) = delGroup($dbh, $webvar{id}); | 
|---|
|  | 434 | if ($code ne 'OK') { | 
|---|
|  | 435 | # need to find failure mode | 
|---|
|  | 436 | $page->param(del_failed => 1); | 
|---|
|  | 437 | $page->param(errmsg => $msg); | 
|---|
|  | 438 | } else { | 
|---|
|  | 439 | # success.  go back to the domain list, do not pass "GO" | 
|---|
|  | 440 | changepage(page => "grpman"); | 
|---|
|  | 441 | } | 
|---|
|  | 442 | } else { | 
|---|
|  | 443 | # cancelled.  whee! | 
|---|
|  | 444 | changepage(page => "grpman"); | 
|---|
|  | 445 | } | 
|---|
|  | 446 |  | 
|---|
| [2] | 447 | } | 
|---|
|  | 448 |  | 
|---|
|  | 449 |  | 
|---|
| [17] | 450 | # start output here so we can redirect pages. | 
|---|
| [7] | 451 | print "Content-type: text/html\n\n", $header->output; | 
|---|
|  | 452 |  | 
|---|
| [13] | 453 | foreach (@debugbits) { print; } | 
|---|
|  | 454 |  | 
|---|
| [20] | 455 | ##common bits | 
|---|
| [17] | 456 | if ($webvar{page} ne 'login') { | 
|---|
| [20] | 457 | $page->param(group => $curgroup); | 
|---|
|  | 458 | $page->param(groupname => groupName($dbh,$curgroup)); | 
|---|
|  | 459 |  | 
|---|
|  | 460 | # stuff for menu group change.  nb: this is icky. | 
|---|
|  | 461 | fill_grouplist("grouplist"); | 
|---|
|  | 462 | $page->param(whereami => $ENV{REQUEST_URI}); | 
|---|
| [17] | 463 | } | 
|---|
| [13] | 464 |  | 
|---|
| [2] | 465 | # spit it out | 
|---|
|  | 466 | print $page->output; | 
|---|
|  | 467 |  | 
|---|
|  | 468 | print "<div id=debug>webvar keys: <pre>\n"; | 
|---|
|  | 469 | foreach my $key (keys %webvar) { | 
|---|
|  | 470 | print "key: $key\tval: $webvar{$key}\n"; | 
|---|
|  | 471 | } | 
|---|
| [20] | 472 | print "</pre>\nsession:\n<pre>\n"; | 
|---|
|  | 473 | my $sesdata = $session->dataref(); | 
|---|
|  | 474 | foreach my $key (keys %$sesdata) { | 
|---|
|  | 475 | print "key: $key\tval: ".$sesdata->{$key}."\n"; | 
|---|
|  | 476 | } | 
|---|
| [2] | 477 | print "</pre>\nENV:\n<pre>\n"; | 
|---|
|  | 478 | foreach my $key (keys %ENV) { | 
|---|
|  | 479 | print "key: $key\tval: $ENV{$key}\n"; | 
|---|
|  | 480 | } | 
|---|
|  | 481 | print "</pre></div>\n"; | 
|---|
|  | 482 |  | 
|---|
|  | 483 | print $footer->output; | 
|---|
|  | 484 |  | 
|---|
| [18] | 485 | # as per the docs, Just In Case | 
|---|
|  | 486 | $session->flush(); | 
|---|
| [2] | 487 |  | 
|---|
|  | 488 | exit 0; | 
|---|
|  | 489 |  | 
|---|
|  | 490 |  | 
|---|
| [11] | 491 | sub changepage { | 
|---|
|  | 492 | my %params = @_;      # think this works the way I want... | 
|---|
|  | 493 |  | 
|---|
|  | 494 | # handle user check | 
|---|
|  | 495 | my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid"; | 
|---|
|  | 496 | foreach (keys %params) { | 
|---|
|  | 497 | $newurl .= "&$_=$params{$_}"; | 
|---|
|  | 498 | } | 
|---|
|  | 499 |  | 
|---|
|  | 500 | print "Status: 302\nLocation: $newurl\n\n"; | 
|---|
|  | 501 | exit; | 
|---|
|  | 502 | } # end changepage | 
|---|
|  | 503 |  | 
|---|
|  | 504 |  | 
|---|
| [2] | 505 | sub fillsoa { | 
|---|
|  | 506 | my $def = shift; | 
|---|
|  | 507 | my $id = shift; | 
|---|
|  | 508 | my $domname; | 
|---|
|  | 509 |  | 
|---|
|  | 510 | if ($webvar{domain} == 0) { | 
|---|
|  | 511 | $domname = "DOMAIN"; | 
|---|
|  | 512 | } else { | 
|---|
| [17] | 513 | my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?"); | 
|---|
|  | 514 | $sth->execute($webvar{domain}); | 
|---|
| [2] | 515 | ($domname) = $sth->fetchrow_array(); | 
|---|
|  | 516 | } | 
|---|
|  | 517 |  | 
|---|
| [17] | 518 | $page->param(domain   => $domname); | 
|---|
|  | 519 | $page->param(defrec   => !$webvar{domain}); | 
|---|
|  | 520 | $page->param(group    => $DNSDB::group); | 
|---|
| [2] | 521 |  | 
|---|
|  | 522 | # defaults | 
|---|
| [17] | 523 | $page->param(defcontact       => $DNSDB::def{contact}); | 
|---|
|  | 524 | $page->param(defns            => $DNSDB::def{prins}); | 
|---|
|  | 525 | $page->param(defsoattl        => $DNSDB::def{soattl}); | 
|---|
|  | 526 | $page->param(defrefresh       => $DNSDB::def{refresh}); | 
|---|
|  | 527 | $page->param(defretry         => $DNSDB::def{retry}); | 
|---|
|  | 528 | $page->param(defexpire        => $DNSDB::def{expire}); | 
|---|
|  | 529 | $page->param(defminttl        => $DNSDB::def{minttl}); | 
|---|
| [2] | 530 |  | 
|---|
|  | 531 | # there are probably better ways to do this.  TMTOWTDI. | 
|---|
|  | 532 | my %soa = getSOA($dbh,$def,$id); | 
|---|
|  | 533 |  | 
|---|
|  | 534 | $page->param(domainid => $webvar{domain}); | 
|---|
|  | 535 | $page->param(recid    => $soa{recid}); | 
|---|
|  | 536 | $page->param(prins    => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins})); | 
|---|
|  | 537 | $page->param(contact  => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact})); | 
|---|
|  | 538 | $page->param(refresh  => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh})); | 
|---|
|  | 539 | $page->param(retry    => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry})); | 
|---|
|  | 540 | $page->param(expire   => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire})); | 
|---|
|  | 541 | $page->param(minttl   => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl})); | 
|---|
|  | 542 | $page->param(ttl      => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl})); | 
|---|
|  | 543 | } | 
|---|
|  | 544 |  | 
|---|
|  | 545 | sub showdomain { | 
|---|
|  | 546 | my $def = shift; | 
|---|
|  | 547 | my $id = shift; | 
|---|
|  | 548 |  | 
|---|
|  | 549 | # get the SOA first | 
|---|
|  | 550 | my %soa = getSOA($dbh,$def,$id); | 
|---|
|  | 551 |  | 
|---|
|  | 552 | $page->param(recid    => $soa{recid}); | 
|---|
|  | 553 | $page->param(contact  => $soa{contact}); | 
|---|
|  | 554 | $page->param(prins    => $soa{prins}); | 
|---|
|  | 555 | $page->param(refresh  => $soa{refresh}); | 
|---|
|  | 556 | $page->param(retry    => $soa{retry}); | 
|---|
|  | 557 | $page->param(expire   => $soa{expire}); | 
|---|
|  | 558 | $page->param(minttl   => $soa{minttl}); | 
|---|
|  | 559 | $page->param(ttl      => $soa{ttl}); | 
|---|
|  | 560 |  | 
|---|
|  | 561 | #  my @foo2 = getDomRecs($dbh,'def',1); | 
|---|
| [4] | 562 | my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset}); | 
|---|
| [2] | 563 |  | 
|---|
|  | 564 | my $row = 0; | 
|---|
|  | 565 | foreach my $rec (@$foo2) { | 
|---|
|  | 566 | $rec->{type} = $typemap{$rec->{type}}; | 
|---|
|  | 567 | $rec->{row} = $row % 2; | 
|---|
|  | 568 | $rec->{defrec} = $webvar{defrec}; | 
|---|
|  | 569 | $rec->{sid} = $webvar{sid}; | 
|---|
| [13] | 570 | $rec->{id} = $id; | 
|---|
| [2] | 571 | $row++; | 
|---|
|  | 572 | } | 
|---|
|  | 573 | $page->param(reclist => $foo2); | 
|---|
|  | 574 | } | 
|---|
|  | 575 |  | 
|---|
| [16] | 576 | # fill in record type list on add/update/edit record template | 
|---|
|  | 577 | sub fill_rectypes { | 
|---|
| [13] | 578 | my $type = shift || $reverse_typemap{A}; | 
|---|
|  | 579 |  | 
|---|
| [17] | 580 | my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder"); | 
|---|
| [2] | 581 | $sth->execute; | 
|---|
|  | 582 | my @typelist; | 
|---|
|  | 583 | while (my ($rval,$rname) = $sth->fetchrow_array()) { | 
|---|
|  | 584 | my %row = ( recval => $rval, recname => $rname ); | 
|---|
| [13] | 585 | $row{tselect} = 1 if $rval == $type; | 
|---|
| [2] | 586 | push @typelist, \%row; | 
|---|
|  | 587 | } | 
|---|
|  | 588 | $page->param(typelist => \@typelist); | 
|---|
| [16] | 589 | } | 
|---|
|  | 590 |  | 
|---|
|  | 591 | sub fill_recdata { | 
|---|
|  | 592 | fill_rectypes($webvar{type}); | 
|---|
|  | 593 |  | 
|---|
|  | 594 | $page->param(name     => $webvar{name}); | 
|---|
|  | 595 | $page->param(address  => $webvar{address}); | 
|---|
|  | 596 | $page->param(distance => $webvar{distance}) | 
|---|
|  | 597 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}); | 
|---|
|  | 598 | $page->param(weight   => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV}; | 
|---|
|  | 599 | $page->param(port     => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV}; | 
|---|
| [2] | 600 | $page->param(ttl      => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl})); | 
|---|
|  | 601 | } | 
|---|
| [7] | 602 |  | 
|---|
|  | 603 | sub fill_fpnla { | 
|---|
|  | 604 | my $count = shift; | 
|---|
|  | 605 | ##fixme | 
|---|
|  | 606 | if ($offset eq 'all') { | 
|---|
| [17] | 607 | push @debugbits, "foo!  wanna see'em all\n"; | 
|---|
| [7] | 608 | } else { | 
|---|
|  | 609 | # all these bits only have sensible behaviour if offset is numeric. err, probably. | 
|---|
|  | 610 | if ($count > $perpage) { | 
|---|
|  | 611 | # if there are more results than the default, always show the "all" link | 
|---|
|  | 612 | $page->param(navall => 1); | 
|---|
|  | 613 |  | 
|---|
|  | 614 | if ($offset > 0) { | 
|---|
|  | 615 | $page->param(navfirst => 1); | 
|---|
|  | 616 | $page->param(navprev => 1); | 
|---|
|  | 617 | $page->param(prevoffs => $offset-1); | 
|---|
|  | 618 | } | 
|---|
|  | 619 |  | 
|---|
|  | 620 | # show "next" and "last" links if we're not on the last page of results | 
|---|
|  | 621 | if ( (($offset+1) * $perpage - $count) < 0 ) { | 
|---|
|  | 622 | $page->param(navnext => 1); | 
|---|
|  | 623 | $page->param(nextoffs => $offset+1); | 
|---|
|  | 624 | $page->param(navlast => 1); | 
|---|
| [8] | 625 | $page->param(lastoffs => int (($count-1)/$perpage)); | 
|---|
| [7] | 626 | } | 
|---|
|  | 627 | } | 
|---|
|  | 628 | } | 
|---|
| [10] | 629 | } # end fill_fpnla() | 
|---|
|  | 630 |  | 
|---|
|  | 631 |  | 
|---|
| [12] | 632 | sub fill_pgcount { | 
|---|
|  | 633 | my $pgcount = shift; | 
|---|
|  | 634 | my $pgtype = shift; | 
|---|
|  | 635 | my $parent = shift; | 
|---|
|  | 636 |  | 
|---|
|  | 637 | $page->param(ntot => $pgcount); | 
|---|
|  | 638 | $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1)); | 
|---|
|  | 639 | $page->param(npglast => ($offset eq 'all' ? $pgcount : | 
|---|
|  | 640 | ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) ) | 
|---|
|  | 641 | )); | 
|---|
|  | 642 | $page->param(pgtype => $pgtype); | 
|---|
|  | 643 | $page->param(parent => $parent); | 
|---|
|  | 644 | } # end fill_pgcount() | 
|---|
|  | 645 |  | 
|---|
|  | 646 |  | 
|---|
| [11] | 647 | sub listdomains { | 
|---|
| [10] | 648 |  | 
|---|
| [17] | 649 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?"); | 
|---|
| [20] | 650 | $sth->execute($curgroup); | 
|---|
| [17] | 651 | my ($count) = $sth->fetchrow_array; | 
|---|
|  | 652 |  | 
|---|
| [12] | 653 | # fill page count and first-previous-next-last-all bits | 
|---|
|  | 654 | ##fixme - hardcoded group bit | 
|---|
| [20] | 655 | fill_pgcount($count,"domains",groupName($dbh,$curgroup)); | 
|---|
| [10] | 656 | fill_fpnla($count); | 
|---|
|  | 657 |  | 
|---|
|  | 658 | ##fixme - group | 
|---|
| [20] | 659 | $page->param(group => $curgroup); | 
|---|
| [10] | 660 | my @domlist; | 
|---|
| [20] | 661 | $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains". | 
|---|
|  | 662 | " INNER JOIN groups ON domains.group_id=groups.group_id". | 
|---|
|  | 663 | " WHERE domains.group_id=?". | 
|---|
|  | 664 | " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage)); | 
|---|
|  | 665 | $sth->execute($curgroup); | 
|---|
| [10] | 666 | my $rownum = 0; | 
|---|
|  | 667 | while (my @data = $sth->fetchrow_array) { | 
|---|
|  | 668 | my %row; | 
|---|
|  | 669 | $row{domainid} = $data[0]; | 
|---|
|  | 670 | $row{domain} = $data[1]; | 
|---|
|  | 671 | $row{status} = ($data[2] ? 'Active' : 'Inactive'); | 
|---|
|  | 672 | $row{group} = $data[3]; | 
|---|
|  | 673 | $row{bg} = ($rownum++)%2; | 
|---|
|  | 674 | #    $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0); | 
|---|
|  | 675 | $row{mkactive} = !$data[2]; | 
|---|
|  | 676 | $row{sid} = $sid; | 
|---|
|  | 677 | $row{offset} = $offset; | 
|---|
|  | 678 | ##fixme:  need to clean up status indicator/usage/inversion | 
|---|
|  | 679 | push @domlist, \%row; | 
|---|
|  | 680 | } | 
|---|
|  | 681 | $page->param(domtable => \@domlist); | 
|---|
| [11] | 682 | } # end listdomains() | 
|---|
| [18] | 683 |  | 
|---|
|  | 684 |  | 
|---|
| [20] | 685 | sub fill_grouplist { | 
|---|
| [19] | 686 | my $template_var = shift; | 
|---|
|  | 687 | my $cur = shift || $curgroup; | 
|---|
| [18] | 688 | # weesa gonna discard parent_group_id for now | 
|---|
|  | 689 | my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id"); | 
|---|
|  | 690 | $sth->execute; | 
|---|
| [20] | 691 | my @grouplist; | 
|---|
|  | 692 | while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) { | 
|---|
| [18] | 693 | my %row; | 
|---|
| [20] | 694 | $row{groupname} = $groupname; | 
|---|
|  | 695 | $row{groupval} = $groupid; | 
|---|
| [18] | 696 | ##fixme: need magic | 
|---|
| [20] | 697 | #    $row{defgroup} = ''; | 
|---|
|  | 698 | $row{groupactive} = 1 if $groupid == $cur; | 
|---|
|  | 699 | push @grouplist, \%row; | 
|---|
| [18] | 700 | } | 
|---|
|  | 701 |  | 
|---|
| [20] | 702 | $page->param("$template_var" => \@grouplist); | 
|---|
| [18] | 703 |  | 
|---|
|  | 704 | } | 
|---|