| 1 | #!/usr/bin/perl -w -T
|
|---|
| 2 | # dns/cgi-bin/dns.cgi
|
|---|
| 3 | ###
|
|---|
| 4 | # SVN revision info
|
|---|
| 5 | # $Date: 2009-10-08 21:29:50 +0000 (Thu, 08 Oct 2009) $
|
|---|
| 6 | # SVN revision $Rev: 18 $
|
|---|
| 7 | # Last update by $Author: kdeugau $
|
|---|
| 8 | ###
|
|---|
| 9 | # Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
|
|---|
| 10 |
|
|---|
| 11 | use strict;
|
|---|
| 12 | use warnings;
|
|---|
| 13 |
|
|---|
| 14 | use CGI::Carp qw (fatalsToBrowser);
|
|---|
| 15 | use CGI::Simple;
|
|---|
| 16 | use HTML::Template;
|
|---|
| 17 | use CGI::Session;
|
|---|
| 18 | use DBI;
|
|---|
| 19 |
|
|---|
| 20 | use lib '.';
|
|---|
| 21 | # custom modules
|
|---|
| 22 | use DNSDB qw(:ALL);
|
|---|
| 23 |
|
|---|
| 24 | my @debugbits; # temp, to be spit out near the end of processing
|
|---|
| 25 |
|
|---|
| 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...
|
|---|
| 36 | my %webvar = $q->Vars;
|
|---|
| 37 |
|
|---|
| 38 | # persistent stuff needed on most/all pages
|
|---|
| 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.
|
|---|
| 47 | $session->param('logingroup',1);
|
|---|
| 48 | $session->param('curgroup',1); # yes, we *do* need to track this too. er, probably.
|
|---|
| 49 | }
|
|---|
| 50 |
|
|---|
| 51 | my $logingroup = $session->param('logingroup');
|
|---|
| 52 | my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $session->param('logingroup'));
|
|---|
| 53 | my $group = ($webvar{group} ? $webvar{group} : 1);
|
|---|
| 54 |
|
|---|
| 55 | # handle login redirect
|
|---|
| 56 | if ($webvar{action} && $webvar{action} eq 'login') {
|
|---|
| 57 | ##fixme: need to actually do a user/pass check
|
|---|
| 58 | changepage(page => "domlist");
|
|---|
| 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
|
|---|
| 65 | #my $perpage = 15;
|
|---|
| 66 | my $perpage = 3;
|
|---|
| 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 |
|
|---|
| 83 | ## Default page is a login page
|
|---|
| 84 | #my $page; # to be initialized as an HTML::Template entity sooner or later
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 |
|
|---|
| 88 | # decide which page to spit out...
|
|---|
| 89 | $webvar{page} = 'login' if !$webvar{page};
|
|---|
| 90 | #if (!$webvar{page}) {
|
|---|
| 91 | # $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
|
|---|
| 92 | #} else {
|
|---|
| 93 | #}
|
|---|
| 94 |
|
|---|
| 95 | my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
|
|---|
| 96 |
|
|---|
| 97 | $page->param(sid => $sid);
|
|---|
| 98 |
|
|---|
| 99 | if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
|
|---|
| 100 |
|
|---|
| 101 | # hmm. seeing problems in some possibly-not-so-corner cases.
|
|---|
| 102 | # this currently only handles "domain on", "domain off"
|
|---|
| 103 | if (defined($webvar{action})) {
|
|---|
| 104 | domStatus($dbh,$webvar{id},$webvar{action});
|
|---|
| 105 | }
|
|---|
| 106 |
|
|---|
| 107 | $page->param(curpage => $webvar{page});
|
|---|
| 108 |
|
|---|
| 109 | listdomains();
|
|---|
| 110 |
|
|---|
| 111 | } elsif ($webvar{page} eq 'reclist') {
|
|---|
| 112 |
|
|---|
| 113 | # Handle record list for both default records (per-group) and live domain records
|
|---|
| 114 |
|
|---|
| 115 | $page->param(defrec => $webvar{defrec});
|
|---|
| 116 | $page->param(id => $webvar{id});
|
|---|
| 117 | $page->param(curpage => $webvar{page});
|
|---|
| 118 |
|
|---|
| 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 |
|
|---|
| 127 | # fill the page-count and first-previous-next-last-all details
|
|---|
| 128 | fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
|
|---|
| 129 | fill_fpnla($count); # should put some params on this sub...
|
|---|
| 130 |
|
|---|
| 131 | $page->param(defrec => $webvar{defrec});
|
|---|
| 132 | if ($webvar{defrec} eq 'y') {
|
|---|
| 133 | ##fixme: hardcoded group
|
|---|
| 134 | showdomain('y',$group);
|
|---|
| 135 | } else {
|
|---|
| 136 | showdomain('n',$webvar{id});
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | } elsif ($webvar{page} eq 'newdomain') {
|
|---|
| 140 |
|
|---|
| 141 |
|
|---|
| 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 |
|
|---|
| 169 | } elsif ($webvar{page} eq 'record') {
|
|---|
| 170 |
|
|---|
| 171 | if ($webvar{recact} eq 'new') {
|
|---|
| 172 |
|
|---|
| 173 | $page->param(todo => "Add record to");
|
|---|
| 174 | $page->param(recact => "add");
|
|---|
| 175 | fill_rectypes();
|
|---|
| 176 |
|
|---|
| 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 {
|
|---|
| 192 | $page->param(failed => 1);
|
|---|
| 193 | $page->param(errmsg => $msg);
|
|---|
| 194 | fill_recdata(); # populate the form... er, mostly.
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | } elsif ($webvar{recact} eq 'edit') {
|
|---|
| 198 |
|
|---|
| 199 | $page->param(todo => "Update record");
|
|---|
| 200 | $page->param(recact => "update");
|
|---|
| 201 | $page->param(parentid => $webvar{parentid});
|
|---|
| 202 | $page->param(id => $webvar{id});
|
|---|
| 203 | $page->param(defrec => $webvar{defrec});
|
|---|
| 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);
|
|---|
| 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') {
|
|---|
| 223 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
|---|
| 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});
|
|---|
| 232 | $page->param(id => $webvar{id});
|
|---|
| 233 | fill_recdata();
|
|---|
| 234 | }
|
|---|
| 235 | }
|
|---|
| 236 |
|
|---|
| 237 | if ($webvar{defrec} eq 'y') {
|
|---|
| 238 | $page->param(dohere => "default records in group ".grpName($dbh,$webvar{parentid}));
|
|---|
| 239 | } else {
|
|---|
| 240 | $page->param(dohere => domainName($dbh,$webvar{parentid}));
|
|---|
| 241 | }
|
|---|
| 242 |
|
|---|
| 243 | } elsif ($webvar{page} eq 'newrec') {
|
|---|
| 244 | push @debugbits, "whee!\n";
|
|---|
| 245 |
|
|---|
| 246 | # populate most fields as needed. (eg, type list.)
|
|---|
| 247 | stdrecs();
|
|---|
| 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 | }
|
|---|
| 259 | # wtf?
|
|---|
| 260 | # push @recargs,
|
|---|
| 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);
|
|---|
| 269 | stdrecs($webvar{type}); # populate the form... er, mostly.
|
|---|
| 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 |
|
|---|
| 278 | $page->param(defrec => $webvar{defrec});
|
|---|
| 279 |
|
|---|
| 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);
|
|---|
| 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});
|
|---|
| 298 | } else {
|
|---|
| 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);
|
|---|
| 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') {
|
|---|
| 345 | # redirect to dns.cgi?etc&page=reclist
|
|---|
| 346 | changepage(page => "reclist", id => $msg);
|
|---|
| 347 | $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
|
|---|
| 348 | showdomain(0,$msg);
|
|---|
| 349 | ##work
|
|---|
| 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 |
|
|---|
| 358 | } elsif ($webvar{page} eq 'grpman') {
|
|---|
| 359 |
|
|---|
| 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 ".
|
|---|
| 377 | "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
|---|
| 378 | $sth->execute;
|
|---|
| 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 |
|
|---|
| 394 | $page->param(curpage => $webvar{page});
|
|---|
| 395 |
|
|---|
| 396 | } elsif ($webvar{page} eq 'newgrp') {
|
|---|
| 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});
|
|---|
| 405 | }
|
|---|
| 406 | }
|
|---|
| 407 |
|
|---|
| 408 |
|
|---|
| 409 | # start output here so we can redirect pages.
|
|---|
| 410 | print "Content-type: text/html\n\n", $header->output;
|
|---|
| 411 |
|
|---|
| 412 | foreach (@debugbits) { print; }
|
|---|
| 413 |
|
|---|
| 414 | # common bits
|
|---|
| 415 | if ($webvar{page} ne 'login') {
|
|---|
| 416 | $page->param(grp => $group);
|
|---|
| 417 | $page->param(grpname => grpName($dbh,$group));
|
|---|
| 418 | fill_grplist();
|
|---|
| 419 | }
|
|---|
| 420 |
|
|---|
| 421 | # spit it out
|
|---|
| 422 | print $page->output;
|
|---|
| 423 |
|
|---|
| 424 | print "<div id=debug>webvar keys: <pre>\n";
|
|---|
| 425 | foreach my $key (keys %webvar) {
|
|---|
| 426 | print "key: $key\tval: $webvar{$key}\n";
|
|---|
| 427 | }
|
|---|
| 428 | print "</pre>\nENV:\n<pre>\n";
|
|---|
| 429 | foreach my $key (keys %ENV) {
|
|---|
| 430 | print "key: $key\tval: $ENV{$key}\n";
|
|---|
| 431 | }
|
|---|
| 432 | print "</pre></div>\n";
|
|---|
| 433 |
|
|---|
| 434 | print $footer->output;
|
|---|
| 435 |
|
|---|
| 436 | # as per the docs, Just In Case
|
|---|
| 437 | $session->flush();
|
|---|
| 438 |
|
|---|
| 439 | exit 0;
|
|---|
| 440 |
|
|---|
| 441 |
|
|---|
| 442 | sub changepage {
|
|---|
| 443 | my %params = @_; # think this works the way I want...
|
|---|
| 444 |
|
|---|
| 445 | # handle user check
|
|---|
| 446 | my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
|
|---|
| 447 | foreach (keys %params) {
|
|---|
| 448 | $newurl .= "&$_=$params{$_}";
|
|---|
| 449 | }
|
|---|
| 450 |
|
|---|
| 451 | print "Status: 302\nLocation: $newurl\n\n";
|
|---|
| 452 | exit;
|
|---|
| 453 | } # end changepage
|
|---|
| 454 |
|
|---|
| 455 |
|
|---|
| 456 | sub fillsoa {
|
|---|
| 457 | my $def = shift;
|
|---|
| 458 | my $id = shift;
|
|---|
| 459 | my $domname;
|
|---|
| 460 |
|
|---|
| 461 | if ($webvar{domain} == 0) {
|
|---|
| 462 | $domname = "DOMAIN";
|
|---|
| 463 | } else {
|
|---|
| 464 | my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
|
|---|
| 465 | $sth->execute($webvar{domain});
|
|---|
| 466 | ($domname) = $sth->fetchrow_array();
|
|---|
| 467 | }
|
|---|
| 468 |
|
|---|
| 469 | $page->param(domain => $domname);
|
|---|
| 470 | $page->param(defrec => !$webvar{domain});
|
|---|
| 471 | $page->param(group => $DNSDB::group);
|
|---|
| 472 |
|
|---|
| 473 | # defaults
|
|---|
| 474 | $page->param(defcontact => $DNSDB::def{contact});
|
|---|
| 475 | $page->param(defns => $DNSDB::def{prins});
|
|---|
| 476 | $page->param(defsoattl => $DNSDB::def{soattl});
|
|---|
| 477 | $page->param(defrefresh => $DNSDB::def{refresh});
|
|---|
| 478 | $page->param(defretry => $DNSDB::def{retry});
|
|---|
| 479 | $page->param(defexpire => $DNSDB::def{expire});
|
|---|
| 480 | $page->param(defminttl => $DNSDB::def{minttl});
|
|---|
| 481 |
|
|---|
| 482 | # there are probably better ways to do this. TMTOWTDI.
|
|---|
| 483 | my %soa = getSOA($dbh,$def,$id);
|
|---|
| 484 |
|
|---|
| 485 | $page->param(domainid => $webvar{domain});
|
|---|
| 486 | $page->param(recid => $soa{recid});
|
|---|
| 487 | $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
|
|---|
| 488 | $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
|
|---|
| 489 | $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
|
|---|
| 490 | $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
|
|---|
| 491 | $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
|
|---|
| 492 | $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
|
|---|
| 493 | $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
|
|---|
| 494 | }
|
|---|
| 495 |
|
|---|
| 496 | sub showdomain {
|
|---|
| 497 | my $def = shift;
|
|---|
| 498 | my $id = shift;
|
|---|
| 499 |
|
|---|
| 500 | # get the SOA first
|
|---|
| 501 | my %soa = getSOA($dbh,$def,$id);
|
|---|
| 502 |
|
|---|
| 503 | $page->param(recid => $soa{recid});
|
|---|
| 504 | $page->param(contact => $soa{contact});
|
|---|
| 505 | $page->param(prins => $soa{prins});
|
|---|
| 506 | $page->param(refresh => $soa{refresh});
|
|---|
| 507 | $page->param(retry => $soa{retry});
|
|---|
| 508 | $page->param(expire => $soa{expire});
|
|---|
| 509 | $page->param(minttl => $soa{minttl});
|
|---|
| 510 | $page->param(ttl => $soa{ttl});
|
|---|
| 511 |
|
|---|
| 512 | # my @foo2 = getDomRecs($dbh,'def',1);
|
|---|
| 513 | my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
|
|---|
| 514 |
|
|---|
| 515 | my $row = 0;
|
|---|
| 516 | foreach my $rec (@$foo2) {
|
|---|
| 517 | $rec->{type} = $typemap{$rec->{type}};
|
|---|
| 518 | $rec->{row} = $row % 2;
|
|---|
| 519 | $rec->{defrec} = $webvar{defrec};
|
|---|
| 520 | $rec->{sid} = $webvar{sid};
|
|---|
| 521 | $rec->{id} = $id;
|
|---|
| 522 | $row++;
|
|---|
| 523 | }
|
|---|
| 524 | $page->param(reclist => $foo2);
|
|---|
| 525 | }
|
|---|
| 526 |
|
|---|
| 527 | # fill in record type list on add/update/edit record template
|
|---|
| 528 | sub fill_rectypes {
|
|---|
| 529 | my $type = shift || $reverse_typemap{A};
|
|---|
| 530 |
|
|---|
| 531 | my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
|
|---|
| 532 | $sth->execute;
|
|---|
| 533 | my @typelist;
|
|---|
| 534 | while (my ($rval,$rname) = $sth->fetchrow_array()) {
|
|---|
| 535 | my %row = ( recval => $rval, recname => $rname );
|
|---|
| 536 | $row{tselect} = 1 if $rval == $type;
|
|---|
| 537 | push @typelist, \%row;
|
|---|
| 538 | }
|
|---|
| 539 | $page->param(typelist => \@typelist);
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | sub fill_recdata {
|
|---|
| 543 | fill_rectypes($webvar{type});
|
|---|
| 544 |
|
|---|
| 545 | $page->param(name => $webvar{name});
|
|---|
| 546 | $page->param(address => $webvar{address});
|
|---|
| 547 | $page->param(distance => $webvar{distance})
|
|---|
| 548 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
|---|
| 549 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
|---|
| 550 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
|---|
| 551 | $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
|
|---|
| 552 | }
|
|---|
| 553 |
|
|---|
| 554 | sub fill_fpnla {
|
|---|
| 555 | my $count = shift;
|
|---|
| 556 | ##fixme
|
|---|
| 557 | if ($offset eq 'all') {
|
|---|
| 558 | push @debugbits, "foo! wanna see'em all\n";
|
|---|
| 559 | } else {
|
|---|
| 560 | # all these bits only have sensible behaviour if offset is numeric. err, probably.
|
|---|
| 561 | if ($count > $perpage) {
|
|---|
| 562 | # if there are more results than the default, always show the "all" link
|
|---|
| 563 | $page->param(navall => 1);
|
|---|
| 564 |
|
|---|
| 565 | if ($offset > 0) {
|
|---|
| 566 | $page->param(navfirst => 1);
|
|---|
| 567 | $page->param(navprev => 1);
|
|---|
| 568 | $page->param(prevoffs => $offset-1);
|
|---|
| 569 | }
|
|---|
| 570 |
|
|---|
| 571 | # show "next" and "last" links if we're not on the last page of results
|
|---|
| 572 | if ( (($offset+1) * $perpage - $count) < 0 ) {
|
|---|
| 573 | $page->param(navnext => 1);
|
|---|
| 574 | $page->param(nextoffs => $offset+1);
|
|---|
| 575 | $page->param(navlast => 1);
|
|---|
| 576 | $page->param(lastoffs => int (($count-1)/$perpage));
|
|---|
| 577 | }
|
|---|
| 578 | }
|
|---|
| 579 | }
|
|---|
| 580 | } # end fill_fpnla()
|
|---|
| 581 |
|
|---|
| 582 |
|
|---|
| 583 | sub fill_pgcount {
|
|---|
| 584 | my $pgcount = shift;
|
|---|
| 585 | my $pgtype = shift;
|
|---|
| 586 | my $parent = shift;
|
|---|
| 587 |
|
|---|
| 588 | $page->param(ntot => $pgcount);
|
|---|
| 589 | $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
|
|---|
| 590 | $page->param(npglast => ($offset eq 'all' ? $pgcount :
|
|---|
| 591 | ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
|
|---|
| 592 | ));
|
|---|
| 593 | $page->param(pgtype => $pgtype);
|
|---|
| 594 | $page->param(parent => $parent);
|
|---|
| 595 | } # end fill_pgcount()
|
|---|
| 596 |
|
|---|
| 597 |
|
|---|
| 598 | sub listdomains {
|
|---|
| 599 |
|
|---|
| 600 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
|
|---|
| 601 | $sth->execute($group);
|
|---|
| 602 | my ($count) = $sth->fetchrow_array;
|
|---|
| 603 |
|
|---|
| 604 | # fill page count and first-previous-next-last-all bits
|
|---|
| 605 | ##fixme - hardcoded group bit
|
|---|
| 606 | fill_pgcount($count,"domains","default group");
|
|---|
| 607 | fill_fpnla($count);
|
|---|
| 608 |
|
|---|
| 609 | ##fixme - group
|
|---|
| 610 | $page->param(grp => 1);
|
|---|
| 611 | my @domlist;
|
|---|
| 612 | $sth = $dbh->prepare("select domain_id,domain,status,groups.group_name from domains".
|
|---|
| 613 | " inner join groups on domains.group_id=groups.group_id".
|
|---|
| 614 | " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
|
|---|
| 615 | $sth->execute;
|
|---|
| 616 | my $rownum = 0;
|
|---|
| 617 | while (my @data = $sth->fetchrow_array) {
|
|---|
| 618 | my %row;
|
|---|
| 619 | $row{domainid} = $data[0];
|
|---|
| 620 | $row{domain} = $data[1];
|
|---|
| 621 | $row{status} = ($data[2] ? 'Active' : 'Inactive');
|
|---|
| 622 | $row{group} = $data[3];
|
|---|
| 623 | $row{bg} = ($rownum++)%2;
|
|---|
| 624 | # $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
|
|---|
| 625 | $row{mkactive} = !$data[2];
|
|---|
| 626 | $row{sid} = $sid;
|
|---|
| 627 | $row{offset} = $offset;
|
|---|
| 628 | ##fixme: need to clean up status indicator/usage/inversion
|
|---|
| 629 | push @domlist, \%row;
|
|---|
| 630 | }
|
|---|
| 631 | $page->param(domtable => \@domlist);
|
|---|
| 632 | } # end listdomains()
|
|---|
| 633 |
|
|---|
| 634 |
|
|---|
| 635 | sub fill_grplist {
|
|---|
| 636 | # weesa gonna discard parent_group_id for now
|
|---|
| 637 | my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id");
|
|---|
| 638 | push @debugbits, "grp list sth err: ".$sth->errstr."<br>\n";
|
|---|
| 639 | $sth->execute;
|
|---|
| 640 | my @grplist;
|
|---|
| 641 | while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
|
|---|
| 642 | my %row;
|
|---|
| 643 | $row{grpname} = $grpname;
|
|---|
| 644 | $row{grpval} = $grpid;
|
|---|
| 645 | ##fixme: need magic
|
|---|
| 646 | # $row{defgrp} = '';
|
|---|
| 647 | $row{grpactive} = 1 if $grpid eq $curgroup;
|
|---|
| 648 | push @grplist, \%row;
|
|---|
| 649 | }
|
|---|
| 650 |
|
|---|
| 651 | $page->param(grplist => \@grplist);
|
|---|
| 652 |
|
|---|
| 653 | }
|
|---|