source: trunk/dns.cgi@ 11

Last change on this file since 11 was 11, checked in by Kris Deugau, 15 years ago

/trunk

Shuffle some segments for logical grouping
Rearrange domain-delete endpoints to redirect to domlist page instead

of including it - still needs template page fixups and other tweaks

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 14.4 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-09-10 19:27:29 +0000 (Thu, 10 Sep 2009) $
6# SVN revision $Rev: 11 $
7# Last update by $Author: kdeugau $
8###
9# Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx>
10
11use strict;
12use warnings;
13
14use CGI::Carp qw (fatalsToBrowser);
15use CGI::Simple;
16use HTML::Template;
17use CGI::Session;
18use DBI;
19
20use lib '.';
21# custom modules
22use DNSDB qw(:ALL);
23
24# Let's do these templates right...
25my $templatedir = "templates";
26my $sessiondir = "session";
27
28# Set up the CGI object...
29my $q = new CGI::Simple;
30# ... and get query-string params as well as POST params if necessary
31$q->parse_query_string;
32
33# This is probably excessive fiddling, but it puts the parameters somewhere my fingers know about...
34my %webvar = $q->Vars;
35
36my $sid = ($webvar{sid} ? $webvar{sid} : undef);
37my $session = new CGI::Session("driver:File", $sid, {Directory => $sessiondir});
38#$sid = $session->id() if !$sid;
39if (!$sid) {
40 # init stuff. can probably axe this down to just above if'n'when user manipulation happens
41 $sid = $session->id();
42# need to know the "upper" group the user can deal with; may as well
43# stick this in the session rather than calling out to the DB every time.
44 $session->param('logingroupid',1);
45 $session->param('workinggroupid',1); # yes, we *do* need to track this too. er, probably.
46}
47
48# handle login redirect
49if ($webvar{action} && $webvar{action} eq 'login') {
50 ##fixme: need to actually do a user/pass check
51 changepage(page => "domlist");
52}
53
54my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
55my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
56
57# default
58#my $perpage = 15;
59my $perpage = 4;
60my $offset = ($webvar{offset} ? $webvar{offset} : 0);
61
62# NB: these must match the field name and SQL ascend/descend syntax respectively
63my $sortfield = "domains";
64my $sortorder = "asc";
65
66my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
67#my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
68# { AutoCommit => 0 }) or die $DBI::errstr;
69
70##fixme. PLEASE! <G>
71print $msg if !$dbh;
72
73# fiddle hardcoded "defaults" as per system/user (?) prefs
74initGlobals($dbh);
75
76# Default page is a login page
77my $page; # to be initialized as an HTML::Template entity sooner or later
78
79
80
81# decide which page to spit out...
82if (!$webvar{page}) {
83 $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
84} else {
85 $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
86}
87
88$page->param(sid => $sid);
89
90if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
91
92# hmm. seeing problems in some possibly-not-so-corner cases.
93# this currently only handles "domain on", "domain off"
94 if (defined($webvar{action})) {
95 domStatus($dbh,$webvar{id},$webvar{action});
96 }
97
98 listdomains();
99
100} elsif ($webvar{page} eq 'reclist') {
101
102 # Handle record list for both default records (per-group) and live domain records
103
104 $page->param(defrec => $webvar{defrec});
105 $page->param(id => $webvar{id});
106 $page->param(curpage => 'reclist');
107
108# select count(*) from (default_)?records where (group|domain)_id=?
109 my $sth = $dbh->prepare("SELECT count(*) FROM ".
110 ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
111 "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
112 "AND NOT type=$reverse_typemap{SOA}");
113 $sth->execute($webvar{id});
114 my ($count) = ($sth->fetchrow_array);
115
116 $page->param(ntot => $count);
117 $page->param(nfirst => 1);
118 $page->param(nlast => 10);
119
120 fill_fpnla($count); # should put some params on this sub...
121
122##fixme
123 $page->param(domain => domainName($dbh,$webvar{id}));
124 $page->param(id => $webvar{id});
125##fixme
126
127 $page->param(defrec => $webvar{defrec});
128 if ($webvar{defrec} eq 'y') {
129 showdomain('y',1);
130 } else {
131 showdomain('n',$webvar{id});
132 }
133
134} elsif ($webvar{page} eq 'newdomain') {
135
136 # weesa gonna discard parent_group_id for now
137 my $sth = $dbh->prepare("select group_id,parent_group_id,name from groups order by group_id");
138 $sth->execute;
139 my @grplist;
140 while (my ($grpid,$pargrp,$grpname) = $sth->fetchrow_array()) {
141 my %row;
142 $row{grpname} = $grpname;
143 $row{grpval} = $grpid;
144##fixme: need magic
145# $row{defgrp} = '';
146 push @grplist, \%row;
147 }
148
149 $page->param(grplist => \@grplist);
150
151} elsif ($webvar{page} eq 'deldom') {
152
153 $page->param(id => $webvar{id});
154 # first pass = confirm y/n (sorta)
155 if (!defined($webvar{del})) {
156 $page->param(del_getconf => 1);
157 $page->param(domain => domainName($dbh,$webvar{id}));
158# print some neato things?
159
160# } else {
161# #whether actually deleting or cancelling we redirect to the domain list, default format
162
163 } elsif ($webvar{del} eq 'ok') {
164 my ($code,$msg) = delDomain($dbh, $webvar{id});
165 if ($code ne 'OK') {
166# need to find failure mode
167 $page->param(del_failed => 1);
168 $page->param(errmsg => $msg);
169 } else {
170 # success. go back to the domain list, do not pass "GO"
171 changepage(page => "domlist");
172 }
173 } else {
174 # cancelled. whee!
175 changepage(page => "domlist");
176 }
177
178} elsif ($webvar{page} eq 'newrec') {
179 print "whee!\n";
180
181 # populate most fields as needed. (eg, type list.)
182 newrec();
183
184} elsif ($webvar{page} eq 'addrec') {
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 push @recargs,
195 my ($code,$msg) = addRec(@recargs);
196
197 if ($code eq 'OK') {
198 showdomain($webvar{defrec},$webvar{parentid});
199# NB: should **really** redirect here, in case of reload. >_< eyowch.
200 } else {
201 $page->param(add_failed => 1);
202 $page->param(errmsg => $msg);
203 newrec(); # populate the form... er, mostly.
204 $page->param(name => $webvar{name});
205 $page->param(address => $webvar{address});
206 $page->param(distance => $webvar{distance})
207 if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
208 $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
209 $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
210 }
211
212 $page->param(defrec => $webvar{defrec});
213
214} elsif ($webvar{page} eq 'conf_del') {
215
216 $page->param(id => $webvar{id});
217 $page->param(defrec => $webvar{defrec});
218
219 my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
220
221} elsif ($webvar{page} eq 'delrec') {
222
223 $page->param(id => $webvar{id});
224 $page->param(defrec => $webvar{defrec});
225 # first pass = confirm y/n (sorta)
226 if (!defined($webvar{del})) {
227 $page->param(del_getconf => 1);
228 my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
229 $page->param(host => $rec{host});
230 $page->param(ftype => $typemap{$rec{type}});
231 $page->param(recval => $rec{val});
232 } else {
233 my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
234 if ($code ne 'OK') {
235## need to find failure mode
236 $page->param(del_failed => 1);
237 $page->param(errmsg => $msg);
238 }
239##fixme: group/parent instead of hardcoded 1
240 showdomain('y',1);
241 }
242
243} elsif ($webvar{page} eq 'editsoa') {
244
245 fillsoa($webvar{defrec},$webvar{recid});
246
247} elsif ($webvar{page} eq 'updatesoa') {
248 print "ooooo!\n";
249
250 my $sth;
251 my $sql = '';
252 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
253 # plus a bit of magic to update the appropriate table
254 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
255 " set host='$webvar{prins}:$webvar{contact}',".
256 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
257 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
258 $sth = $dbh->prepare($sql);
259 $sth->execute;
260
261 if ($sth->err) {
262 $page->param(update_failed => 1);
263 $page->param(msg => $DBI::errstr);
264 fillsoa($webvar{defrec},1);
265 } else {
266 $page->param(update_failed => 0);
267##fixme! need to set group ID properly here
268 showdomain('y',1);
269 }
270
271} elsif ($webvar{page} eq 'adddomain') {
272 # Need some magic here.
273
274##fixme: Group should be variable
275 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
276
277# hokay, a bit of magic to decide which page we hit.
278 if ($code eq 'OK') {
279# redirect to dns.cgi?etc&page=reclist
280 print $q->redirect("");
281 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
282 showdomain(0,$msg);
283##work
284 } else {
285# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
286 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
287 $page->param(add_failed => 1);
288 $page->param(domain => $webvar{domain});
289 $page->param(errmsg => $msg);
290 }
291
292
293}
294
295
296## hmm. may want to move this so we can redirect after adding/deleting/etc
297print "Content-type: text/html\n\n", $header->output;
298
299# spit it out
300print $page->output;
301
302print "<div id=debug>webvar keys: <pre>\n";
303foreach my $key (keys %webvar) {
304 print "key: $key\tval: $webvar{$key}\n";
305}
306print "</pre>\nENV:\n<pre>\n";
307foreach my $key (keys %ENV) {
308 print "key: $key\tval: $ENV{$key}\n";
309}
310print "</pre></div>\n";
311
312print $footer->output;
313
314
315exit 0;
316
317
318sub changepage {
319 my %params = @_; # think this works the way I want...
320
321 # handle user check
322 my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
323 foreach (keys %params) {
324 $newurl .= "&$_=$params{$_}";
325 }
326
327 print "Status: 302\nLocation: $newurl\n\n";
328 exit;
329} # end changepage
330
331
332sub fillsoa {
333 my $def = shift;
334 my $id = shift;
335 my $domname;
336
337 if ($webvar{domain} == 0) {
338 $domname = "DOMAIN";
339 } else {
340 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
341 $sth->execute();
342 ($domname) = $sth->fetchrow_array();
343 }
344
345 $page->param(domain => $domname);
346 $page->param(defrec => !$webvar{domain});
347 $page->param(group => $DNSDB::group);
348
349# defaults
350 $page->param(defcontact => $DNSDB::def{contact});
351 $page->param(defns => $DNSDB::def{prins});
352 $page->param(defsoattl => $DNSDB::def{soattl});
353 $page->param(defrefresh => $DNSDB::def{refresh});
354 $page->param(defretry => $DNSDB::def{retry});
355 $page->param(defexpire => $DNSDB::def{expire});
356 $page->param(defminttl => $DNSDB::def{minttl});
357
358 # there are probably better ways to do this. TMTOWTDI.
359 my %soa = getSOA($dbh,$def,$id);
360
361 $page->param(domainid => $webvar{domain});
362 $page->param(recid => $soa{recid});
363 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
364 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
365 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
366 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
367 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
368 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
369 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
370}
371
372sub showdomain {
373 my $def = shift;
374 my $id = shift;
375
376 # get the SOA first
377 my %soa = getSOA($dbh,$def,$id);
378
379 $page->param(recid => $soa{recid});
380 $page->param(contact => $soa{contact});
381 $page->param(prins => $soa{prins});
382 $page->param(refresh => $soa{refresh});
383 $page->param(retry => $soa{retry});
384 $page->param(expire => $soa{expire});
385 $page->param(minttl => $soa{minttl});
386 $page->param(ttl => $soa{ttl});
387
388# my @foo2 = getDomRecs($dbh,'def',1);
389 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
390
391 my $row = 0;
392 foreach my $rec (@$foo2) {
393 $rec->{type} = $typemap{$rec->{type}};
394 $rec->{row} = $row % 2;
395 # Feh.
396 $rec->{defrec} = $webvar{defrec};
397 # And **FEH!!**
398 $rec->{sid} = $webvar{sid};
399 $row++;
400 }
401 $page->param(reclist => $foo2);
402}
403
404sub newrec {
405 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
406 $sth->execute;
407
408 my @typelist;
409 while (my ($rval,$rname) = $sth->fetchrow_array()) {
410 my %row = ( recval => $rval, recname => $rname );
411 $row{tselect} = 1 if $rval == $webvar{type};
412 push @typelist, \%row;
413 }
414 $page->param(typelist => \@typelist);
415 $page->param(domain => domainName($dbh,$webvar{domainid}));
416 $page->param(defrec => $webvar{defrec});
417 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
418 if ($webvar{defrec} eq 'y') {
419 ##fixme - should be groupid
420 $page->param(parentid => 1);
421 } else {
422print "DEBUG: foobar: parentid $webvar{parentid}<br>\n";
423 $page->param(parentid => $webvar{parentid});
424 }
425}
426
427sub fill_fpnla {
428 my $count = shift;
429##fixme
430 if ($offset eq 'all') {
431 #print "foo! wanna see'em all\n";
432 } else {
433 # all these bits only have sensible behaviour if offset is numeric. err, probably.
434 if ($count > $perpage) {
435 # if there are more results than the default, always show the "all" link
436 $page->param(navall => 1);
437
438 if ($offset > 0) {
439 $page->param(navfirst => 1);
440 $page->param(navprev => 1);
441 $page->param(prevoffs => $offset-1);
442 }
443
444 # show "next" and "last" links if we're not on the last page of results
445 if ( (($offset+1) * $perpage - $count) < 0 ) {
446 $page->param(navnext => 1);
447 $page->param(nextoffs => $offset+1);
448 $page->param(navlast => 1);
449 $page->param(lastoffs => int (($count-1)/$perpage));
450 }
451 }
452 }
453} # end fill_fpnla()
454
455
456sub listdomains {
457 my $sth = $dbh->prepare("select count(*) from domains");
458 $sth->execute;
459 my ($count) = ($sth->fetchrow_array);
460
461 fill_fpnla($count);
462
463 $page->param(ndomains => $count);
464 $page->param(nstart => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
465 $page->param(npglast => ($offset eq 'all' ? $count :
466 ( (($offset+1)*$perpage) > $count ? $count : (($offset+1)*$perpage) )
467 ));
468
469##fixme - group
470 $page->param(id => 1);
471 my @domlist;
472 my $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
473 " inner join groups on domains.group_id=groups.group_id".
474 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
475 $sth->execute;
476 my $rownum = 0;
477 while (my @data = $sth->fetchrow_array) {
478 my %row;
479 $row{domainid} = $data[0];
480 $row{domain} = $data[1];
481 $row{status} = ($data[2] ? 'Active' : 'Inactive');
482 $row{group} = $data[3];
483 $row{bg} = ($rownum++)%2;
484# $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
485 $row{mkactive} = !$data[2];
486 $row{sid} = $sid;
487 $row{offset} = $offset;
488##fixme: need to clean up status indicator/usage/inversion
489 push @domlist, \%row;
490 }
491 $page->param(domtable => \@domlist);
492} # end listdomains()
Note: See TracBrowser for help on using the repository browser.