source: trunk/dns.cgi@ 3

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

/trunk

Checkpoint
Continued development to reach ~ VegaDNS equivalence

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