source: trunk/dns.cgi@ 4

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

/trunk

checkpoint

  • Property svn:executable set to *
  • Property svn:keywords set to Date Rev Author Id
File size: 14.6 KB
Line 
1#!/usr/bin/perl -w -T
2# dns/cgi-bin/dns.cgi
3###
4# SVN revision info
5# $Date: 2009-08-31 20:16:27 +0000 (Mon, 31 Aug 2009) $
6# SVN revision $Rev: 4 $
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 'editsoa') {
303
304 fillsoa($webvar{defrec},$webvar{recid});
305
306} elsif ($webvar{page} eq 'updatesoa') {
307 print "ooooo!\n";
308
309 my $sth;
310 my $sql = '';
311 # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
312 # plus a bit of magic to update the appropriate table
313 $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
314 " set host='$webvar{prins}:$webvar{contact}',".
315 " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
316 " ttl=$webvar{ttl} where record_id=$webvar{recid}";
317 $sth = $dbh->prepare($sql);
318 $sth->execute;
319
320 if ($sth->err) {
321 $page->param(update_failed => 1);
322 $page->param(msg => $DBI::errstr);
323 fillsoa($webvar{defrec},1);
324 } else {
325 $page->param(update_failed => 0);
326##fixme! need to set group ID properly here
327 showdomain('y',1);
328 }
329
330} elsif ($webvar{page} eq 'adddomain') {
331 # Need some magic here.
332
333##fixme: Group should be variable
334 my ($code,$msg) = addDomain($dbh,$webvar{domain},1,($webvar{makeactive} eq 'on' ? 1 : 0));
335
336# hokay, a bit of magic to decide which page we hit.
337 if ($code eq 'OK') {
338 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
339 showdomain(0,$msg);
340##work
341 } else {
342# oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
343 $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
344 $page->param(add_failed => 1);
345 $page->param(domain => $webvar{domain});
346 $page->param(errmsg => $msg);
347 }
348
349
350}
351
352
353# spit it out
354print $page->output;
355
356print "<div id=debug>webvar keys: <pre>\n";
357foreach my $key (keys %webvar) {
358 print "key: $key\tval: $webvar{$key}\n";
359}
360print "</pre>\nENV:\n<pre>\n";
361foreach my $key (keys %ENV) {
362 print "key: $key\tval: $ENV{$key}\n";
363}
364print "</pre></div>\n";
365
366print $footer->output;
367
368
369exit 0;
370
371
372sub fillsoa {
373 my $def = shift;
374 my $id = shift;
375 my $domname;
376
377 if ($webvar{domain} == 0) {
378 $domname = "DOMAIN";
379 } else {
380 my $sth = $dbh->prepare("select domain from domains where domain_id=$webvar{domain}");
381 $sth->execute();
382 ($domname) = $sth->fetchrow_array();
383 }
384
385 $page->param(domain => $domname);
386 $page->param(defrec => !$webvar{domain});
387 $page->param(group => $DNSDB::group);
388
389# defaults
390 $page->param(defcontact => $DNSDB::def{contact});
391 $page->param(defns => $DNSDB::def{prins});
392 $page->param(defsoattl => $DNSDB::def{soattl});
393 $page->param(defrefresh => $DNSDB::def{refresh});
394 $page->param(defretry => $DNSDB::def{retry});
395 $page->param(defexpire => $DNSDB::def{expire});
396 $page->param(defminttl => $DNSDB::def{minttl});
397
398 # there are probably better ways to do this. TMTOWTDI.
399 my %soa = getSOA($dbh,$def,$id);
400
401 $page->param(domainid => $webvar{domain});
402 $page->param(recid => $soa{recid});
403 $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
404 $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
405 $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
406 $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
407 $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
408 $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
409 $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
410}
411
412sub showdomain {
413 my $def = shift;
414 my $id = shift;
415
416 # get the SOA first
417 my %soa = getSOA($dbh,$def,$id);
418
419 $page->param(recid => $soa{recid});
420 $page->param(contact => $soa{contact});
421 $page->param(prins => $soa{prins});
422 $page->param(refresh => $soa{refresh});
423 $page->param(retry => $soa{retry});
424 $page->param(expire => $soa{expire});
425 $page->param(minttl => $soa{minttl});
426 $page->param(ttl => $soa{ttl});
427
428##fixme!
429my $roffset = 0;
430
431# my @foo2 = getDomRecs($dbh,'def',1);
432 my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
433
434# $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains".
435# " inner join groups on domains.group_id=groups.group_id".
436# " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage));
437# $sth->execute;
438# my $rownum = 0;
439# while (my @data = $sth->fetchrow_array) {
440# my %row;
441# $row{domainid} = $data[0];
442# $row{domain} = $data[1];
443# $row{status} = ($data[2] ? 'Active' : 'Inactive');
444# $row{group} = $data[3];
445# $row{bg} = ($rownum++)%2;
446## $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
447# $row{mkactive} = !$data[2];
448# $row{sid} = $sid;
449# $row{offset} = $offset;
450
451 my $row = 0;
452 foreach my $rec (@$foo2) {
453 $rec->{type} = $typemap{$rec->{type}};
454 $rec->{row} = $row % 2;
455 # Feh.
456 $rec->{defrec} = $webvar{defrec};
457 # And **FEH!!**
458 $rec->{sid} = $webvar{sid};
459 $row++;
460 }
461 $page->param(reclist => $foo2);
462}
463
464sub newrec {
465 my $sth = $dbh->prepare("select val,name from rectypes where stdflag=1 order by listorder");
466 $sth->execute;
467
468 my @typelist;
469 while (my ($rval,$rname) = $sth->fetchrow_array()) {
470 my %row = ( recval => $rval, recname => $rname );
471 $row{tselect} = 1 if $rval == $webvar{type};
472 push @typelist, \%row;
473 }
474 $page->param(typelist => \@typelist);
475 $page->param(domain => domainName($dbh,$webvar{domainid}));
476 $page->param(defrec => $webvar{defrec});
477 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
478 if ($webvar{defrec} eq 'y') {
479 ##fixme - should be groupid
480 $page->param(parentid => 1);
481 } else {
482print "DEBUG: foobar: parentid $webvar{parentid}<br>\n";
483 $page->param(parentid => $webvar{parentid});
484 }
485}
Note: See TracBrowser for help on using the repository browser.