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