[2] | 1 | #!/usr/bin/perl -w -T
|
---|
| 2 | # dns/cgi-bin/dns.cgi
|
---|
| 3 | ###
|
---|
| 4 | # SVN revision info
|
---|
| 5 | # $Date: 2009-10-20 18:47:36 +0000 (Tue, 20 Oct 2009) $
|
---|
| 6 | # SVN revision $Rev: 23 $
|
---|
| 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);
|
---|
[22] | 167 | listdomains($curgroup);
|
---|
[11] | 168 | } else {
|
---|
| 169 | # success. go back to the domain list, do not pass "GO"
|
---|
| 170 | changepage(page => "domlist");
|
---|
| 171 | }
|
---|
| 172 | } else {
|
---|
| 173 | # cancelled. whee!
|
---|
| 174 | changepage(page => "domlist");
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[13] | 177 | } elsif ($webvar{page} eq 'record') {
|
---|
[16] | 178 |
|
---|
[13] | 179 | if ($webvar{recact} eq 'new') {
|
---|
[16] | 180 |
|
---|
[15] | 181 | $page->param(todo => "Add record to");
|
---|
| 182 | $page->param(recact => "add");
|
---|
[16] | 183 | fill_rectypes();
|
---|
| 184 |
|
---|
[15] | 185 | } elsif ($webvar{recact} eq 'add') {
|
---|
| 186 |
|
---|
| 187 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
|
---|
| 188 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
|
---|
| 189 | push @recargs, $webvar{distance};
|
---|
| 190 | if ($webvar{type} == $reverse_typemap{SRV}) {
|
---|
| 191 | push @recargs, $webvar{weight};
|
---|
| 192 | push @recargs, $webvar{port};
|
---|
| 193 | }
|
---|
| 194 | }
|
---|
| 195 | my ($code,$msg) = addRec(@recargs);
|
---|
| 196 |
|
---|
| 197 | if ($code eq 'OK') {
|
---|
| 198 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
| 199 | } else {
|
---|
[16] | 200 | $page->param(failed => 1);
|
---|
[15] | 201 | $page->param(errmsg => $msg);
|
---|
[16] | 202 | fill_recdata(); # populate the form... er, mostly.
|
---|
[15] | 203 | }
|
---|
| 204 |
|
---|
[13] | 205 | } elsif ($webvar{recact} eq 'edit') {
|
---|
[15] | 206 |
|
---|
[16] | 207 | $page->param(todo => "Update record");
|
---|
| 208 | $page->param(recact => "update");
|
---|
| 209 | $page->param(parentid => $webvar{parentid});
|
---|
[17] | 210 | $page->param(id => $webvar{id});
|
---|
[16] | 211 | $page->param(defrec => $webvar{defrec});
|
---|
[13] | 212 | my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
|
---|
| 213 | ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
|
---|
| 214 | $sth->execute($webvar{id});
|
---|
| 215 | my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
|
---|
| 216 | $page->param(name => $host);
|
---|
| 217 | $page->param(address => $val);
|
---|
| 218 | $page->param(distance => $distance);
|
---|
| 219 | $page->param(weight => $weight);
|
---|
| 220 | $page->param(port => $port);
|
---|
| 221 | $page->param(ttl => $ttl);
|
---|
[16] | 222 | fill_rectypes($type);
|
---|
| 223 |
|
---|
| 224 | } elsif ($webvar{recact} eq 'update') {
|
---|
| 225 |
|
---|
| 226 | my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
|
---|
| 227 | $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
|
---|
| 228 | $webvar{distance},$webvar{weight},$webvar{port});
|
---|
| 229 |
|
---|
| 230 | if ($code eq 'OK') {
|
---|
[17] | 231 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
[16] | 232 | } else {
|
---|
| 233 | $page->param(failed => 1);
|
---|
| 234 | $page->param(errmsg => $msg);
|
---|
| 235 | $page->param(wastrying => "updating");
|
---|
| 236 | $page->param(todo => "Update record");
|
---|
| 237 | $page->param(recact => "update");
|
---|
| 238 | $page->param(parentid => $webvar{parentid});
|
---|
| 239 | $page->param(defrec => $webvar{defrec});
|
---|
[17] | 240 | $page->param(id => $webvar{id});
|
---|
[16] | 241 | fill_recdata();
|
---|
| 242 | }
|
---|
[13] | 243 | }
|
---|
[16] | 244 |
|
---|
[13] | 245 | if ($webvar{defrec} eq 'y') {
|
---|
[20] | 246 | $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
|
---|
[13] | 247 | } else {
|
---|
[16] | 248 | $page->param(dohere => domainName($dbh,$webvar{parentid}));
|
---|
[13] | 249 | }
|
---|
| 250 |
|
---|
[2] | 251 | } elsif ($webvar{page} eq 'newrec') {
|
---|
[13] | 252 | push @debugbits, "whee!\n";
|
---|
[2] | 253 |
|
---|
[3] | 254 | # populate most fields as needed. (eg, type list.)
|
---|
[13] | 255 | stdrecs();
|
---|
[2] | 256 |
|
---|
| 257 | } elsif ($webvar{page} eq 'addrec') {
|
---|
| 258 |
|
---|
| 259 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
|
---|
| 260 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
|
---|
| 261 | push @recargs, $webvar{distance};
|
---|
| 262 | if ($webvar{type} == $reverse_typemap{SRV}) {
|
---|
| 263 | push @recargs, $webvar{weight};
|
---|
| 264 | push @recargs, $webvar{port};
|
---|
| 265 | }
|
---|
| 266 | }
|
---|
[13] | 267 | # wtf?
|
---|
| 268 | # push @recargs,
|
---|
[2] | 269 | my ($code,$msg) = addRec(@recargs);
|
---|
| 270 |
|
---|
| 271 | if ($code eq 'OK') {
|
---|
| 272 | showdomain($webvar{defrec},$webvar{parentid});
|
---|
| 273 | # NB: should **really** redirect here, in case of reload. >_< eyowch.
|
---|
| 274 | } else {
|
---|
| 275 | $page->param(add_failed => 1);
|
---|
| 276 | $page->param(errmsg => $msg);
|
---|
[13] | 277 | stdrecs($webvar{type}); # populate the form... er, mostly.
|
---|
[2] | 278 | $page->param(name => $webvar{name});
|
---|
| 279 | $page->param(address => $webvar{address});
|
---|
| 280 | $page->param(distance => $webvar{distance})
|
---|
| 281 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
---|
| 282 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
| 283 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
| 284 | }
|
---|
| 285 |
|
---|
[3] | 286 | $page->param(defrec => $webvar{defrec});
|
---|
| 287 |
|
---|
[2] | 288 | } elsif ($webvar{page} eq 'conf_del') {
|
---|
| 289 |
|
---|
| 290 | $page->param(id => $webvar{id});
|
---|
| 291 | $page->param(defrec => $webvar{defrec});
|
---|
| 292 |
|
---|
| 293 | my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
|
---|
| 294 |
|
---|
| 295 | } elsif ($webvar{page} eq 'delrec') {
|
---|
| 296 |
|
---|
| 297 | $page->param(id => $webvar{id});
|
---|
| 298 | $page->param(defrec => $webvar{defrec});
|
---|
| 299 | # first pass = confirm y/n (sorta)
|
---|
| 300 | if (!defined($webvar{del})) {
|
---|
| 301 | $page->param(del_getconf => 1);
|
---|
[3] | 302 | my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
|
---|
| 303 | $page->param(host => $rec{host});
|
---|
| 304 | $page->param(ftype => $typemap{$rec{type}});
|
---|
| 305 | $page->param(recval => $rec{val});
|
---|
[2] | 306 | } else {
|
---|
[3] | 307 | my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
|
---|
| 308 | if ($code ne 'OK') {
|
---|
| 309 | ## need to find failure mode
|
---|
| 310 | $page->param(del_failed => 1);
|
---|
| 311 | $page->param(errmsg => $msg);
|
---|
| 312 | }
|
---|
| 313 | ##fixme: group/parent instead of hardcoded 1
|
---|
| 314 | showdomain('y',1);
|
---|
[2] | 315 | }
|
---|
| 316 |
|
---|
| 317 | } elsif ($webvar{page} eq 'editsoa') {
|
---|
| 318 |
|
---|
| 319 | fillsoa($webvar{defrec},$webvar{recid});
|
---|
| 320 |
|
---|
| 321 | } elsif ($webvar{page} eq 'updatesoa') {
|
---|
| 322 | print "ooooo!\n";
|
---|
| 323 |
|
---|
| 324 | my $sth;
|
---|
| 325 | my $sql = '';
|
---|
| 326 | # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
|
---|
| 327 | # plus a bit of magic to update the appropriate table
|
---|
| 328 | $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
|
---|
| 329 | " set host='$webvar{prins}:$webvar{contact}',".
|
---|
| 330 | " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
|
---|
| 331 | " ttl=$webvar{ttl} where record_id=$webvar{recid}";
|
---|
| 332 | $sth = $dbh->prepare($sql);
|
---|
| 333 | $sth->execute;
|
---|
| 334 |
|
---|
| 335 | if ($sth->err) {
|
---|
| 336 | $page->param(update_failed => 1);
|
---|
| 337 | $page->param(msg => $DBI::errstr);
|
---|
| 338 | fillsoa($webvar{defrec},1);
|
---|
| 339 | } else {
|
---|
| 340 | $page->param(update_failed => 0);
|
---|
| 341 | ##fixme! need to set group ID properly here
|
---|
| 342 | showdomain('y',1);
|
---|
| 343 | }
|
---|
| 344 |
|
---|
| 345 | } elsif ($webvar{page} eq 'adddomain') {
|
---|
| 346 | # Need some magic here.
|
---|
| 347 |
|
---|
| 348 | ##fixme: Group should be variable
|
---|
[20] | 349 | my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
|
---|
[2] | 350 |
|
---|
| 351 | # hokay, a bit of magic to decide which page we hit.
|
---|
| 352 | if ($code eq 'OK') {
|
---|
[11] | 353 | # redirect to dns.cgi?etc&page=reclist
|
---|
[12] | 354 | changepage(page => "reclist", id => $msg);
|
---|
[3] | 355 | $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
|
---|
| 356 | showdomain(0,$msg);
|
---|
| 357 | ##work
|
---|
[2] | 358 | } else {
|
---|
| 359 | # oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
|
---|
| 360 | $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
|
---|
| 361 | $page->param(add_failed => 1);
|
---|
| 362 | $page->param(domain => $webvar{domain});
|
---|
| 363 | $page->param(errmsg => $msg);
|
---|
| 364 | }
|
---|
| 365 |
|
---|
[17] | 366 | } elsif ($webvar{page} eq 'grpman') {
|
---|
[2] | 367 |
|
---|
[22] | 368 | listgroups();
|
---|
[18] | 369 | $page->param(curpage => $webvar{page});
|
---|
| 370 |
|
---|
[17] | 371 | } elsif ($webvar{page} eq 'newgrp') {
|
---|
[20] | 372 |
|
---|
[18] | 373 | # do.. uhh.. stuff.. if we have no webvar{action}
|
---|
| 374 | if ($webvar{action} && $webvar{action} eq 'add') {
|
---|
| 375 | # not gonna provide the 4th param: template-or-clone flag, just yet
|
---|
| 376 | my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
|
---|
| 377 | changepage(page => "grpman") if $code eq 'OK';
|
---|
| 378 | $page->param(add_failed => 1);
|
---|
| 379 | $page->param(errmsg => $msg);
|
---|
| 380 | $page->param(newgroup => $webvar{newgroup});
|
---|
[20] | 381 | fill_grouplist('pargroup',$webvar{pargroup});
|
---|
[19] | 382 | } else {
|
---|
| 383 | # $page->param
|
---|
[20] | 384 | fill_grouplist('pargroup',$curgroup);
|
---|
[19] | 385 |
|
---|
[18] | 386 | }
|
---|
[20] | 387 |
|
---|
[22] | 388 | } elsif ($webvar{page} eq 'delgrp') {
|
---|
[20] | 389 |
|
---|
| 390 | $page->param(id => $webvar{id});
|
---|
| 391 | # first pass = confirm y/n (sorta)
|
---|
| 392 | if (!defined($webvar{del})) {
|
---|
| 393 | $page->param(del_getconf => 1);
|
---|
[23] | 394 | # $page->param(groupname => groupName($dbh,$webvar{id}));
|
---|
[20] | 395 | # print some neato things?
|
---|
| 396 |
|
---|
| 397 | # } else {
|
---|
| 398 | # #whether actually deleting or cancelling we redirect to the group list, default format
|
---|
| 399 |
|
---|
| 400 | } elsif ($webvar{del} eq 'ok') {
|
---|
| 401 | my ($code,$msg) = delGroup($dbh, $webvar{id});
|
---|
[23] | 402 | push @debugbits, groupName($dbh, $webvar{id});
|
---|
[20] | 403 | if ($code ne 'OK') {
|
---|
| 404 | # need to find failure mode
|
---|
| 405 | $page->param(del_failed => 1);
|
---|
| 406 | $page->param(errmsg => $msg);
|
---|
[22] | 407 | $page->param(curpage => $webvar{page});
|
---|
| 408 | listgroups();
|
---|
[20] | 409 | } else {
|
---|
| 410 | # success. go back to the domain list, do not pass "GO"
|
---|
| 411 | changepage(page => "grpman");
|
---|
| 412 | }
|
---|
| 413 | } else {
|
---|
| 414 | # cancelled. whee!
|
---|
| 415 | changepage(page => "grpman");
|
---|
| 416 | }
|
---|
[23] | 417 | $page->param(delgroupname => groupName($dbh, $webvar{id}));
|
---|
[2] | 418 | }
|
---|
| 419 |
|
---|
| 420 |
|
---|
[17] | 421 | # start output here so we can redirect pages.
|
---|
[7] | 422 | print "Content-type: text/html\n\n", $header->output;
|
---|
| 423 |
|
---|
[13] | 424 | foreach (@debugbits) { print; }
|
---|
| 425 |
|
---|
[20] | 426 | ##common bits
|
---|
[17] | 427 | if ($webvar{page} ne 'login') {
|
---|
[20] | 428 | $page->param(group => $curgroup);
|
---|
| 429 | $page->param(groupname => groupName($dbh,$curgroup));
|
---|
| 430 |
|
---|
| 431 | # stuff for menu group change. nb: this is icky.
|
---|
| 432 | fill_grouplist("grouplist");
|
---|
| 433 | $page->param(whereami => $ENV{REQUEST_URI});
|
---|
[17] | 434 | }
|
---|
[13] | 435 |
|
---|
[2] | 436 | # spit it out
|
---|
| 437 | print $page->output;
|
---|
| 438 |
|
---|
| 439 | print "<div id=debug>webvar keys: <pre>\n";
|
---|
| 440 | foreach my $key (keys %webvar) {
|
---|
| 441 | print "key: $key\tval: $webvar{$key}\n";
|
---|
| 442 | }
|
---|
[20] | 443 | print "</pre>\nsession:\n<pre>\n";
|
---|
| 444 | my $sesdata = $session->dataref();
|
---|
| 445 | foreach my $key (keys %$sesdata) {
|
---|
| 446 | print "key: $key\tval: ".$sesdata->{$key}."\n";
|
---|
| 447 | }
|
---|
[2] | 448 | print "</pre>\nENV:\n<pre>\n";
|
---|
| 449 | foreach my $key (keys %ENV) {
|
---|
| 450 | print "key: $key\tval: $ENV{$key}\n";
|
---|
| 451 | }
|
---|
| 452 | print "</pre></div>\n";
|
---|
| 453 |
|
---|
| 454 | print $footer->output;
|
---|
| 455 |
|
---|
[18] | 456 | # as per the docs, Just In Case
|
---|
| 457 | $session->flush();
|
---|
[2] | 458 |
|
---|
| 459 | exit 0;
|
---|
| 460 |
|
---|
| 461 |
|
---|
[11] | 462 | sub changepage {
|
---|
| 463 | my %params = @_; # think this works the way I want...
|
---|
| 464 |
|
---|
| 465 | # handle user check
|
---|
| 466 | my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
|
---|
| 467 | foreach (keys %params) {
|
---|
| 468 | $newurl .= "&$_=$params{$_}";
|
---|
| 469 | }
|
---|
| 470 |
|
---|
| 471 | print "Status: 302\nLocation: $newurl\n\n";
|
---|
| 472 | exit;
|
---|
| 473 | } # end changepage
|
---|
| 474 |
|
---|
| 475 |
|
---|
[2] | 476 | sub fillsoa {
|
---|
| 477 | my $def = shift;
|
---|
| 478 | my $id = shift;
|
---|
| 479 | my $domname;
|
---|
| 480 |
|
---|
| 481 | if ($webvar{domain} == 0) {
|
---|
| 482 | $domname = "DOMAIN";
|
---|
| 483 | } else {
|
---|
[17] | 484 | my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
|
---|
| 485 | $sth->execute($webvar{domain});
|
---|
[2] | 486 | ($domname) = $sth->fetchrow_array();
|
---|
| 487 | }
|
---|
| 488 |
|
---|
[17] | 489 | $page->param(domain => $domname);
|
---|
| 490 | $page->param(defrec => !$webvar{domain});
|
---|
| 491 | $page->param(group => $DNSDB::group);
|
---|
[2] | 492 |
|
---|
| 493 | # defaults
|
---|
[17] | 494 | $page->param(defcontact => $DNSDB::def{contact});
|
---|
| 495 | $page->param(defns => $DNSDB::def{prins});
|
---|
| 496 | $page->param(defsoattl => $DNSDB::def{soattl});
|
---|
| 497 | $page->param(defrefresh => $DNSDB::def{refresh});
|
---|
| 498 | $page->param(defretry => $DNSDB::def{retry});
|
---|
| 499 | $page->param(defexpire => $DNSDB::def{expire});
|
---|
| 500 | $page->param(defminttl => $DNSDB::def{minttl});
|
---|
[2] | 501 |
|
---|
| 502 | # there are probably better ways to do this. TMTOWTDI.
|
---|
| 503 | my %soa = getSOA($dbh,$def,$id);
|
---|
| 504 |
|
---|
| 505 | $page->param(domainid => $webvar{domain});
|
---|
| 506 | $page->param(recid => $soa{recid});
|
---|
| 507 | $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
|
---|
| 508 | $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
|
---|
| 509 | $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
|
---|
| 510 | $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
|
---|
| 511 | $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
|
---|
| 512 | $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
|
---|
| 513 | $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
|
---|
| 514 | }
|
---|
| 515 |
|
---|
| 516 | sub showdomain {
|
---|
| 517 | my $def = shift;
|
---|
| 518 | my $id = shift;
|
---|
| 519 |
|
---|
| 520 | # get the SOA first
|
---|
| 521 | my %soa = getSOA($dbh,$def,$id);
|
---|
| 522 |
|
---|
| 523 | $page->param(recid => $soa{recid});
|
---|
| 524 | $page->param(contact => $soa{contact});
|
---|
| 525 | $page->param(prins => $soa{prins});
|
---|
| 526 | $page->param(refresh => $soa{refresh});
|
---|
| 527 | $page->param(retry => $soa{retry});
|
---|
| 528 | $page->param(expire => $soa{expire});
|
---|
| 529 | $page->param(minttl => $soa{minttl});
|
---|
| 530 | $page->param(ttl => $soa{ttl});
|
---|
| 531 |
|
---|
| 532 | # my @foo2 = getDomRecs($dbh,'def',1);
|
---|
[4] | 533 | my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
|
---|
[2] | 534 |
|
---|
| 535 | my $row = 0;
|
---|
| 536 | foreach my $rec (@$foo2) {
|
---|
| 537 | $rec->{type} = $typemap{$rec->{type}};
|
---|
| 538 | $rec->{row} = $row % 2;
|
---|
| 539 | $rec->{defrec} = $webvar{defrec};
|
---|
| 540 | $rec->{sid} = $webvar{sid};
|
---|
[13] | 541 | $rec->{id} = $id;
|
---|
[23] | 542 | $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
|
---|
| 543 | $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
|
---|
| 544 | $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
|
---|
[2] | 545 | $row++;
|
---|
| 546 | }
|
---|
| 547 | $page->param(reclist => $foo2);
|
---|
| 548 | }
|
---|
| 549 |
|
---|
[23] | 550 |
|
---|
[16] | 551 | # fill in record type list on add/update/edit record template
|
---|
| 552 | sub fill_rectypes {
|
---|
[13] | 553 | my $type = shift || $reverse_typemap{A};
|
---|
| 554 |
|
---|
[17] | 555 | my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
|
---|
[2] | 556 | $sth->execute;
|
---|
| 557 | my @typelist;
|
---|
| 558 | while (my ($rval,$rname) = $sth->fetchrow_array()) {
|
---|
| 559 | my %row = ( recval => $rval, recname => $rname );
|
---|
[13] | 560 | $row{tselect} = 1 if $rval == $type;
|
---|
[2] | 561 | push @typelist, \%row;
|
---|
| 562 | }
|
---|
| 563 | $page->param(typelist => \@typelist);
|
---|
[16] | 564 | }
|
---|
| 565 |
|
---|
| 566 | sub fill_recdata {
|
---|
| 567 | fill_rectypes($webvar{type});
|
---|
| 568 |
|
---|
| 569 | $page->param(name => $webvar{name});
|
---|
| 570 | $page->param(address => $webvar{address});
|
---|
| 571 | $page->param(distance => $webvar{distance})
|
---|
| 572 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
---|
| 573 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
| 574 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
[2] | 575 | $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
|
---|
| 576 | }
|
---|
[7] | 577 |
|
---|
| 578 | sub fill_fpnla {
|
---|
| 579 | my $count = shift;
|
---|
| 580 | ##fixme
|
---|
| 581 | if ($offset eq 'all') {
|
---|
[17] | 582 | push @debugbits, "foo! wanna see'em all\n";
|
---|
[7] | 583 | } else {
|
---|
| 584 | # all these bits only have sensible behaviour if offset is numeric. err, probably.
|
---|
| 585 | if ($count > $perpage) {
|
---|
| 586 | # if there are more results than the default, always show the "all" link
|
---|
| 587 | $page->param(navall => 1);
|
---|
| 588 |
|
---|
| 589 | if ($offset > 0) {
|
---|
| 590 | $page->param(navfirst => 1);
|
---|
| 591 | $page->param(navprev => 1);
|
---|
| 592 | $page->param(prevoffs => $offset-1);
|
---|
| 593 | }
|
---|
| 594 |
|
---|
| 595 | # show "next" and "last" links if we're not on the last page of results
|
---|
| 596 | if ( (($offset+1) * $perpage - $count) < 0 ) {
|
---|
| 597 | $page->param(navnext => 1);
|
---|
| 598 | $page->param(nextoffs => $offset+1);
|
---|
| 599 | $page->param(navlast => 1);
|
---|
[8] | 600 | $page->param(lastoffs => int (($count-1)/$perpage));
|
---|
[7] | 601 | }
|
---|
| 602 | }
|
---|
| 603 | }
|
---|
[10] | 604 | } # end fill_fpnla()
|
---|
| 605 |
|
---|
| 606 |
|
---|
[12] | 607 | sub fill_pgcount {
|
---|
| 608 | my $pgcount = shift;
|
---|
| 609 | my $pgtype = shift;
|
---|
| 610 | my $parent = shift;
|
---|
| 611 |
|
---|
| 612 | $page->param(ntot => $pgcount);
|
---|
| 613 | $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
|
---|
| 614 | $page->param(npglast => ($offset eq 'all' ? $pgcount :
|
---|
| 615 | ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
|
---|
| 616 | ));
|
---|
| 617 | $page->param(pgtype => $pgtype);
|
---|
| 618 | $page->param(parent => $parent);
|
---|
| 619 | } # end fill_pgcount()
|
---|
| 620 |
|
---|
| 621 |
|
---|
[11] | 622 | sub listdomains {
|
---|
[17] | 623 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
|
---|
[20] | 624 | $sth->execute($curgroup);
|
---|
[17] | 625 | my ($count) = $sth->fetchrow_array;
|
---|
| 626 |
|
---|
[12] | 627 | # fill page count and first-previous-next-last-all bits
|
---|
| 628 | ##fixme - hardcoded group bit
|
---|
[20] | 629 | fill_pgcount($count,"domains",groupName($dbh,$curgroup));
|
---|
[10] | 630 | fill_fpnla($count);
|
---|
| 631 |
|
---|
| 632 | ##fixme - group
|
---|
[20] | 633 | $page->param(group => $curgroup);
|
---|
[10] | 634 | my @domlist;
|
---|
[20] | 635 | $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
|
---|
| 636 | " INNER JOIN groups ON domains.group_id=groups.group_id".
|
---|
| 637 | " WHERE domains.group_id=?".
|
---|
| 638 | " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
---|
| 639 | $sth->execute($curgroup);
|
---|
[10] | 640 | my $rownum = 0;
|
---|
| 641 | while (my @data = $sth->fetchrow_array) {
|
---|
| 642 | my %row;
|
---|
| 643 | $row{domainid} = $data[0];
|
---|
| 644 | $row{domain} = $data[1];
|
---|
| 645 | $row{status} = ($data[2] ? 'Active' : 'Inactive');
|
---|
| 646 | $row{group} = $data[3];
|
---|
| 647 | $row{bg} = ($rownum++)%2;
|
---|
| 648 | # $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
|
---|
| 649 | $row{mkactive} = !$data[2];
|
---|
| 650 | $row{sid} = $sid;
|
---|
| 651 | $row{offset} = $offset;
|
---|
| 652 | ##fixme: need to clean up status indicator/usage/inversion
|
---|
| 653 | push @domlist, \%row;
|
---|
| 654 | }
|
---|
| 655 | $page->param(domtable => \@domlist);
|
---|
[11] | 656 | } # end listdomains()
|
---|
[18] | 657 |
|
---|
| 658 |
|
---|
[22] | 659 | sub listgroups {
|
---|
| 660 | my $sth = $dbh->prepare("select count(*) from groups");
|
---|
| 661 | $sth->execute;
|
---|
| 662 | my ($count) = ($sth->fetchrow_array);
|
---|
| 663 |
|
---|
| 664 | # fill page count and first-previous-next-last-all bits
|
---|
| 665 | ##fixme - hardcoded group bit
|
---|
| 666 | fill_pgcount($count,"groups",'');
|
---|
| 667 | fill_fpnla($count);
|
---|
| 668 |
|
---|
| 669 | my @grouplist;
|
---|
| 670 | $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
|
---|
| 671 | "count(distinct(u.email)), count(distinct(d.domain)) ".
|
---|
| 672 | "FROM groups g ".
|
---|
| 673 | "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
|
---|
| 674 | "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
|
---|
| 675 | "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
|
---|
| 676 | "GROUP BY g.group_id, g.group_name, g2.group_name ".
|
---|
| 677 | "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
---|
| 678 | $sth->execute;
|
---|
| 679 |
|
---|
| 680 | my $rownum = 0;
|
---|
| 681 | while (my @data = $sth->fetchrow_array) {
|
---|
| 682 | my %row;
|
---|
| 683 | $row{groupid} = $data[0];
|
---|
| 684 | $row{groupname} = $data[1];
|
---|
| 685 | $row{pgroup} = $data[2];
|
---|
| 686 | $row{nusers} = $data[3];
|
---|
| 687 | $row{ndomains} = $data[4];
|
---|
| 688 | $row{bg} = ($rownum++)%2;
|
---|
| 689 | $row{sid} = $sid;
|
---|
| 690 | push @grouplist, \%row;
|
---|
| 691 | }
|
---|
| 692 | $page->param(grouptable => \@grouplist);
|
---|
| 693 | } # end listgroups()
|
---|
| 694 |
|
---|
| 695 |
|
---|
[20] | 696 | sub fill_grouplist {
|
---|
[19] | 697 | my $template_var = shift;
|
---|
| 698 | my $cur = shift || $curgroup;
|
---|
[18] | 699 | # weesa gonna discard parent_group_id for now
|
---|
| 700 | my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id");
|
---|
| 701 | $sth->execute;
|
---|
[20] | 702 | my @grouplist;
|
---|
| 703 | while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
|
---|
[18] | 704 | my %row;
|
---|
[20] | 705 | $row{groupname} = $groupname;
|
---|
| 706 | $row{groupval} = $groupid;
|
---|
[18] | 707 | ##fixme: need magic
|
---|
[20] | 708 | # $row{defgroup} = '';
|
---|
| 709 | $row{groupactive} = 1 if $groupid == $cur;
|
---|
| 710 | push @grouplist, \%row;
|
---|
[18] | 711 | }
|
---|
| 712 |
|
---|
[20] | 713 | $page->param("$template_var" => \@grouplist);
|
---|
[18] | 714 |
|
---|
| 715 | }
|
---|