source: trunk/dns.cgi@ 5

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

/trunk

checkpoint
Delete domain mostly complete - need to fix up "redirection" after completion

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