1 | #!/usr/bin/perl -w -T
|
---|
2 | # dns/cgi-bin/dns.cgi
|
---|
3 | ###
|
---|
4 | # SVN revision info
|
---|
5 | # $Date: 2009-10-30 21:52:55 +0000 (Fri, 30 Oct 2009) $
|
---|
6 | # SVN revision $Rev: 24 $
|
---|
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') ? $session->param('logingroup') : 1);
|
---|
52 | my $curgroup = ($session->param('curgroup') ? $session->param('curgroup') : $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 | if ($webvar{action} && $webvar{action} eq 'chgroup') {
|
---|
62 | # fiddle session-stored group data
|
---|
63 | # magic incantation to... uhhh...
|
---|
64 | $session->param('curgroup', $webvar{group});
|
---|
65 | $curgroup = ($webvar{group} ? $webvar{group} : $session->param('curgroup'));
|
---|
66 | }
|
---|
67 |
|
---|
68 | my $header = HTML::Template->new(filename => "$templatedir/header.tmpl");
|
---|
69 | my $footer = HTML::Template->new(filename => "$templatedir/footer.tmpl");
|
---|
70 |
|
---|
71 | # default
|
---|
72 | #my $perpage = 15;
|
---|
73 | my $perpage = 3;
|
---|
74 | my $offset = ($webvar{offset} ? $webvar{offset} : 0);
|
---|
75 |
|
---|
76 | # NB: these must match the field name and SQL ascend/descend syntax respectively
|
---|
77 | my $sortfield = "domains";
|
---|
78 | my $sortorder = "asc";
|
---|
79 |
|
---|
80 | my ($dbh,$msg) = connectDB("dnsdb","dnsdb","secret");
|
---|
81 | #my $dbh = DBI->connect("DBI:mysql:database=vegadns","vegadns","secret",
|
---|
82 | # { AutoCommit => 0 }) or die $DBI::errstr;
|
---|
83 |
|
---|
84 | ##fixme. PLEASE! <G>
|
---|
85 | print $msg if !$dbh;
|
---|
86 |
|
---|
87 | # fiddle hardcoded "defaults" as per system/user (?) prefs
|
---|
88 | initGlobals($dbh);
|
---|
89 |
|
---|
90 | ## Default page is a login page
|
---|
91 | #my $page; # to be initialized as an HTML::Template entity sooner or later
|
---|
92 |
|
---|
93 |
|
---|
94 |
|
---|
95 | # decide which page to spit out...
|
---|
96 | $webvar{page} = 'login' if !$webvar{page};
|
---|
97 | #if (!$webvar{page}) {
|
---|
98 | # $page = HTML::Template->new(filename => "$templatedir/login.tmpl");
|
---|
99 | #} else {
|
---|
100 | #}
|
---|
101 |
|
---|
102 | my $page = HTML::Template->new(filename => "$templatedir/$webvar{page}.tmpl");
|
---|
103 |
|
---|
104 | $page->param(sid => $sid);
|
---|
105 |
|
---|
106 | if ($webvar{page} eq 'domlist' or $webvar{page} eq 'index') {
|
---|
107 |
|
---|
108 | # hmm. seeing problems in some possibly-not-so-corner cases.
|
---|
109 | # this currently only handles "domain on", "domain off"
|
---|
110 | if (defined($webvar{action})) {
|
---|
111 | domStatus($dbh,$webvar{id},$webvar{action});
|
---|
112 | }
|
---|
113 |
|
---|
114 | $page->param(curpage => $webvar{page});
|
---|
115 |
|
---|
116 | listdomains();
|
---|
117 |
|
---|
118 | } elsif ($webvar{page} eq 'reclist') {
|
---|
119 |
|
---|
120 | # Handle record list for both default records (per-group) and live domain records
|
---|
121 |
|
---|
122 | $page->param(defrec => $webvar{defrec});
|
---|
123 | $page->param(id => $webvar{id});
|
---|
124 | $page->param(curpage => $webvar{page});
|
---|
125 |
|
---|
126 | # select count(*) from (default_)?records where (group|domain)_id=?
|
---|
127 | my $sth = $dbh->prepare("SELECT count(*) FROM ".
|
---|
128 | ($webvar{defrec} eq 'y' ? 'default_' : '')."records ".
|
---|
129 | "WHERE ".($webvar{defrec} eq 'y' ? 'group' : 'domain')."_id=? ".
|
---|
130 | "AND NOT type=$reverse_typemap{SOA}");
|
---|
131 | $sth->execute($webvar{id});
|
---|
132 | my ($count) = ($sth->fetchrow_array);
|
---|
133 |
|
---|
134 | # fill the page-count and first-previous-next-last-all details
|
---|
135 | fill_pgcount($count,"records",domainName($dbh,$webvar{id}));
|
---|
136 | fill_fpnla($count); # should put some params on this sub...
|
---|
137 |
|
---|
138 | $page->param(defrec => $webvar{defrec});
|
---|
139 | if ($webvar{defrec} eq 'y') {
|
---|
140 | ##fixme: hardcoded group
|
---|
141 | showdomain('y',$curgroup);
|
---|
142 | } else {
|
---|
143 | showdomain('n',$webvar{id});
|
---|
144 | }
|
---|
145 |
|
---|
146 | } elsif ($webvar{page} eq 'newdomain') {
|
---|
147 |
|
---|
148 |
|
---|
149 | } elsif ($webvar{page} eq 'deldom') {
|
---|
150 |
|
---|
151 | $page->param(id => $webvar{id});
|
---|
152 | # first pass = confirm y/n (sorta)
|
---|
153 | if (!defined($webvar{del})) {
|
---|
154 | $page->param(del_getconf => 1);
|
---|
155 | $page->param(domain => domainName($dbh,$webvar{id}));
|
---|
156 | # print some neato things?
|
---|
157 |
|
---|
158 | # } else {
|
---|
159 | # #whether actually deleting or cancelling we redirect to the domain list, default format
|
---|
160 |
|
---|
161 | } elsif ($webvar{del} eq 'ok') {
|
---|
162 | my ($code,$msg) = delDomain($dbh, $webvar{id});
|
---|
163 | if ($code ne 'OK') {
|
---|
164 | # need to find failure mode
|
---|
165 | $page->param(del_failed => 1);
|
---|
166 | $page->param(errmsg => $msg);
|
---|
167 | listdomains($curgroup);
|
---|
168 | } else {
|
---|
169 | # success. go back to the domain list, do not pass "GO"
|
---|
170 | changepage(page => "domlist");
|
---|
171 | }
|
---|
172 | } else {
|
---|
173 | # cancelled. whee!
|
---|
174 | changepage(page => "domlist");
|
---|
175 | }
|
---|
176 |
|
---|
177 | } elsif ($webvar{page} eq 'record') {
|
---|
178 |
|
---|
179 | if ($webvar{recact} eq 'new') {
|
---|
180 |
|
---|
181 | $page->param(todo => "Add record to");
|
---|
182 | $page->param(recact => "add");
|
---|
183 | fill_rectypes();
|
---|
184 |
|
---|
185 | } elsif ($webvar{recact} eq 'add') {
|
---|
186 |
|
---|
187 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
|
---|
188 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
|
---|
189 | push @recargs, $webvar{distance};
|
---|
190 | if ($webvar{type} == $reverse_typemap{SRV}) {
|
---|
191 | push @recargs, $webvar{weight};
|
---|
192 | push @recargs, $webvar{port};
|
---|
193 | }
|
---|
194 | }
|
---|
195 | my ($code,$msg) = addRec(@recargs);
|
---|
196 |
|
---|
197 | if ($code eq 'OK') {
|
---|
198 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
199 | } else {
|
---|
200 |
|
---|
201 | $page->param(failed => 1);
|
---|
202 | $page->param(errmsg => $msg);
|
---|
203 | $page->param(wastrying => "adding");
|
---|
204 | $page->param(todo => "Add record to");
|
---|
205 | $page->param(recact => "add");
|
---|
206 | $page->param(parentid => $webvar{parentid});
|
---|
207 | $page->param(defrec => $webvar{defrec});
|
---|
208 | $page->param(id => $webvar{id});
|
---|
209 | fill_recdata(); # populate the form... er, mostly.
|
---|
210 | }
|
---|
211 |
|
---|
212 | } elsif ($webvar{recact} eq 'edit') {
|
---|
213 |
|
---|
214 | $page->param(todo => "Update record");
|
---|
215 | $page->param(recact => "update");
|
---|
216 | $page->param(parentid => $webvar{parentid});
|
---|
217 | $page->param(id => $webvar{id});
|
---|
218 | $page->param(defrec => $webvar{defrec});
|
---|
219 | my $sth = $dbh->prepare("SELECT host,type,val,distance,weight,port,ttl FROM ".
|
---|
220 | ($webvar{defrec} eq 'y' ? 'default_' : '')."records WHERE record_id=?");
|
---|
221 | $sth->execute($webvar{id});
|
---|
222 | my ($host,$type,$val,$distance,$weight,$port,$ttl) = $sth->fetchrow_array;
|
---|
223 | $page->param(name => $host);
|
---|
224 | $page->param(address => $val);
|
---|
225 | $page->param(distance => $distance);
|
---|
226 | $page->param(weight => $weight);
|
---|
227 | $page->param(port => $port);
|
---|
228 | $page->param(ttl => $ttl);
|
---|
229 | fill_rectypes($type);
|
---|
230 |
|
---|
231 | } elsif ($webvar{recact} eq 'update') {
|
---|
232 |
|
---|
233 | my ($code,$msg) = updateRec($dbh,$webvar{defrec},$webvar{id},
|
---|
234 | $webvar{name},$webvar{type},$webvar{address},$webvar{ttl},
|
---|
235 | $webvar{distance},$webvar{weight},$webvar{port});
|
---|
236 |
|
---|
237 | if ($code eq 'OK') {
|
---|
238 | changepage(page => "reclist", id => $webvar{parentid}, defrec => $webvar{defrec});
|
---|
239 | } else {
|
---|
240 | $page->param(failed => 1);
|
---|
241 | $page->param(errmsg => $msg);
|
---|
242 | $page->param(wastrying => "updating");
|
---|
243 | $page->param(todo => "Update record");
|
---|
244 | $page->param(recact => "update");
|
---|
245 | $page->param(parentid => $webvar{parentid});
|
---|
246 | $page->param(defrec => $webvar{defrec});
|
---|
247 | $page->param(id => $webvar{id});
|
---|
248 | fill_recdata();
|
---|
249 | }
|
---|
250 | }
|
---|
251 |
|
---|
252 | if ($webvar{defrec} eq 'y') {
|
---|
253 | $page->param(dohere => "default records in group ".groupName($dbh,$webvar{parentid}));
|
---|
254 | } else {
|
---|
255 | $page->param(parentid => $webvar{parentid});
|
---|
256 | # $page->param(id => $webvar{id});
|
---|
257 | $page->param(dohere => domainName($dbh,$webvar{parentid}));
|
---|
258 | }
|
---|
259 |
|
---|
260 | } elsif ($webvar{page} eq 'newrec') {
|
---|
261 | push @debugbits, "whee!\n";
|
---|
262 |
|
---|
263 | # populate most fields as needed. (eg, type list.)
|
---|
264 | stdrecs();
|
---|
265 |
|
---|
266 | } elsif ($webvar{page} eq 'addrec') {
|
---|
267 |
|
---|
268 | my @recargs = ($dbh,$webvar{defrec},$webvar{parentid},$webvar{name},$webvar{type},$webvar{address},$webvar{ttl});
|
---|
269 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV}) {
|
---|
270 | push @recargs, $webvar{distance};
|
---|
271 | if ($webvar{type} == $reverse_typemap{SRV}) {
|
---|
272 | push @recargs, $webvar{weight};
|
---|
273 | push @recargs, $webvar{port};
|
---|
274 | }
|
---|
275 | }
|
---|
276 | # wtf?
|
---|
277 | # push @recargs,
|
---|
278 | my ($code,$msg) = addRec(@recargs);
|
---|
279 |
|
---|
280 | if ($code eq 'OK') {
|
---|
281 | showdomain($webvar{defrec},$webvar{parentid});
|
---|
282 | # NB: should **really** redirect here, in case of reload. >_< eyowch.
|
---|
283 | } else {
|
---|
284 | $page->param(add_failed => 1);
|
---|
285 | $page->param(errmsg => $msg);
|
---|
286 | stdrecs($webvar{type}); # populate the form... er, mostly.
|
---|
287 | $page->param(name => $webvar{name});
|
---|
288 | $page->param(address => $webvar{address});
|
---|
289 | $page->param(distance => $webvar{distance})
|
---|
290 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
---|
291 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
292 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
293 | }
|
---|
294 |
|
---|
295 | $page->param(defrec => $webvar{defrec});
|
---|
296 |
|
---|
297 | } elsif ($webvar{page} eq 'conf_del') {
|
---|
298 |
|
---|
299 | $page->param(id => $webvar{id});
|
---|
300 | $page->param(defrec => $webvar{defrec});
|
---|
301 |
|
---|
302 | my @tmp = getrecdata($dbh,$webvar{id},$webvar{defrec});
|
---|
303 |
|
---|
304 | } elsif ($webvar{page} eq 'delrec') {
|
---|
305 |
|
---|
306 | $page->param(id => $webvar{id});
|
---|
307 | $page->param(defrec => $webvar{defrec});
|
---|
308 | # first pass = confirm y/n (sorta)
|
---|
309 | if (!defined($webvar{del})) {
|
---|
310 | $page->param(del_getconf => 1);
|
---|
311 | my %rec = getRecLine($dbh,$webvar{defrec},$webvar{id});
|
---|
312 | $page->param(host => $rec{host});
|
---|
313 | $page->param(ftype => $typemap{$rec{type}});
|
---|
314 | $page->param(recval => $rec{val});
|
---|
315 | } else {
|
---|
316 | my ($code,$msg) = delRec($dbh,$webvar{defrec},$webvar{id});
|
---|
317 | if ($code ne 'OK') {
|
---|
318 | ## need to find failure mode
|
---|
319 | $page->param(del_failed => 1);
|
---|
320 | $page->param(errmsg => $msg);
|
---|
321 | }
|
---|
322 | ##fixme: group/parent instead of hardcoded 1
|
---|
323 | showdomain('y',1);
|
---|
324 | }
|
---|
325 |
|
---|
326 | } elsif ($webvar{page} eq 'editsoa') {
|
---|
327 |
|
---|
328 | fillsoa($webvar{defrec},$webvar{recid});
|
---|
329 |
|
---|
330 | } elsif ($webvar{page} eq 'updatesoa') {
|
---|
331 | print "ooooo!\n";
|
---|
332 |
|
---|
333 | my $sth;
|
---|
334 | my $sql = '';
|
---|
335 | # no domain ID, so we're editing the default SOA for a group (we don't care which one here)
|
---|
336 | # plus a bit of magic to update the appropriate table
|
---|
337 | $sql = "update ".($webvar{domainid} eq '' ? "default_records" : "records").
|
---|
338 | " set host='$webvar{prins}:$webvar{contact}',".
|
---|
339 | " val='$webvar{refresh}:$webvar{retry}:$webvar{expire}:$webvar{minttl}',".
|
---|
340 | " ttl=$webvar{ttl} where record_id=$webvar{recid}";
|
---|
341 | $sth = $dbh->prepare($sql);
|
---|
342 | $sth->execute;
|
---|
343 |
|
---|
344 | if ($sth->err) {
|
---|
345 | $page->param(update_failed => 1);
|
---|
346 | $page->param(msg => $DBI::errstr);
|
---|
347 | fillsoa($webvar{defrec},1);
|
---|
348 | } else {
|
---|
349 | $page->param(update_failed => 0);
|
---|
350 | ##fixme! need to set group ID properly here
|
---|
351 | showdomain('y',1);
|
---|
352 | }
|
---|
353 |
|
---|
354 | } elsif ($webvar{page} eq 'adddomain') {
|
---|
355 | # Need some magic here.
|
---|
356 |
|
---|
357 | ##fixme: Group should be variable
|
---|
358 | my ($code,$msg) = addDomain($dbh,$webvar{domain},$webvar{group},($webvar{makeactive} eq 'on' ? 1 : 0));
|
---|
359 |
|
---|
360 | # hokay, a bit of magic to decide which page we hit.
|
---|
361 | if ($code eq 'OK') {
|
---|
362 | # redirect to dns.cgi?etc&page=reclist
|
---|
363 | changepage(page => "reclist", id => $msg);
|
---|
364 | # $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
|
---|
365 | # showdomain(0,$msg);
|
---|
366 | } else {
|
---|
367 | # oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
|
---|
368 | ##fixme: session ID
|
---|
369 | $page = HTML::Template->new(filename => "$templatedir/newdomain.tmpl");
|
---|
370 | $page->param(add_failed => 1);
|
---|
371 | $page->param(domain => $webvar{domain});
|
---|
372 | $page->param(errmsg => $msg);
|
---|
373 | }
|
---|
374 |
|
---|
375 | } elsif ($webvar{page} eq 'grpman') {
|
---|
376 |
|
---|
377 | listgroups();
|
---|
378 | $page->param(curpage => $webvar{page});
|
---|
379 |
|
---|
380 | } elsif ($webvar{page} eq 'newgrp') {
|
---|
381 |
|
---|
382 | # do.. uhh.. stuff.. if we have no webvar{action}
|
---|
383 | if ($webvar{action} && $webvar{action} eq 'add') {
|
---|
384 | # not gonna provide the 4th param: template-or-clone flag, just yet
|
---|
385 | my ($code,$msg) = addGroup($dbh, $webvar{newgroup}, $webvar{pargroup});
|
---|
386 | changepage(page => "grpman") if $code eq 'OK';
|
---|
387 | $page->param(add_failed => 1);
|
---|
388 | $page->param(errmsg => $msg);
|
---|
389 | $page->param(newgroup => $webvar{newgroup});
|
---|
390 | fill_grouplist('pargroup',$webvar{pargroup});
|
---|
391 | } else {
|
---|
392 | # $page->param
|
---|
393 | fill_grouplist('pargroup',$curgroup);
|
---|
394 |
|
---|
395 | }
|
---|
396 |
|
---|
397 | } elsif ($webvar{page} eq 'delgrp') {
|
---|
398 |
|
---|
399 | $page->param(id => $webvar{id});
|
---|
400 | # first pass = confirm y/n (sorta)
|
---|
401 | if (!defined($webvar{del})) {
|
---|
402 | $page->param(del_getconf => 1);
|
---|
403 | # $page->param(groupname => groupName($dbh,$webvar{id}));
|
---|
404 | # print some neato things?
|
---|
405 |
|
---|
406 | # } else {
|
---|
407 | # #whether actually deleting or cancelling we redirect to the group list, default format
|
---|
408 |
|
---|
409 | } elsif ($webvar{del} eq 'ok') {
|
---|
410 | my ($code,$msg) = delGroup($dbh, $webvar{id});
|
---|
411 | push @debugbits, groupName($dbh, $webvar{id});
|
---|
412 | if ($code ne 'OK') {
|
---|
413 | # need to find failure mode
|
---|
414 | $page->param(del_failed => 1);
|
---|
415 | $page->param(errmsg => $msg);
|
---|
416 | $page->param(curpage => $webvar{page});
|
---|
417 | listgroups();
|
---|
418 | } else {
|
---|
419 | # success. go back to the domain list, do not pass "GO"
|
---|
420 | changepage(page => "grpman");
|
---|
421 | }
|
---|
422 | } else {
|
---|
423 | # cancelled. whee!
|
---|
424 | changepage(page => "grpman");
|
---|
425 | }
|
---|
426 | $page->param(delgroupname => groupName($dbh, $webvar{id}));
|
---|
427 |
|
---|
428 | } elsif ($webvar{page} eq 'useradmin') {
|
---|
429 |
|
---|
430 | list_users();
|
---|
431 | $page->param(curpage => $webvar{page});
|
---|
432 |
|
---|
433 | } elsif ($webvar{page} eq 'newuser') {
|
---|
434 |
|
---|
435 | # foo?
|
---|
436 | fill_actypelist();
|
---|
437 |
|
---|
438 | } elsif ($webvar{page} eq 'adduser') {
|
---|
439 |
|
---|
440 | my ($code,$msg);
|
---|
441 |
|
---|
442 | if ($webvar{pass1} ne $webvar{pass2}) {
|
---|
443 | $code = 'FAIL';
|
---|
444 | $msg = "Passwords don't match";
|
---|
445 | } else {
|
---|
446 | ($code,$msg) = addUser($dbh,$webvar{username}, $webvar{group}, $webvar{pass1},
|
---|
447 | ($webvar{makeactive} eq 'on' ? 1 : 0), $webvar{fname}, $webvar{lname});
|
---|
448 | }
|
---|
449 |
|
---|
450 | # hokay, a bit of magic to decide which page we hit.
|
---|
451 | if ($code eq 'OK') {
|
---|
452 | # redirect to dns.cgi?etc&page=reclist
|
---|
453 | changepage(page => "useradmin");
|
---|
454 | # $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl");
|
---|
455 | # showdomain(0,$msg);
|
---|
456 | ##work
|
---|
457 | } else {
|
---|
458 | # oooh, yeah, this is supposed to be a redirect. er, maybe. whee.
|
---|
459 | # $page = HTML::Template->new(filename => "$templatedir/newuser.tmpl");
|
---|
460 | $page->param(add_failed => 1);
|
---|
461 | $page->param(username => $webvar{username});
|
---|
462 | $page->param(fname => $webvar{fname});
|
---|
463 | $page->param(lname => $webvar{lname});
|
---|
464 | $page->param(pass1 => $webvar{pass1});
|
---|
465 | $page->param(pass2 => $webvar{pass2});
|
---|
466 | $page->param(errmsg => $msg);
|
---|
467 | fill_actypelist();
|
---|
468 | }
|
---|
469 |
|
---|
470 | $page->param(add_failed => 1);
|
---|
471 | }
|
---|
472 |
|
---|
473 |
|
---|
474 | # start output here so we can redirect pages.
|
---|
475 | print "Content-type: text/html\n\n", $header->output;
|
---|
476 |
|
---|
477 | ##common bits
|
---|
478 | if ($webvar{page} ne 'login') {
|
---|
479 | $page->param(group => $curgroup);
|
---|
480 | $page->param(groupname => groupName($dbh,$curgroup));
|
---|
481 |
|
---|
482 | # group tree. should go elsewhere, probably
|
---|
483 | my $tmpgrplist = fill_grptree($logingroup,$curgroup);
|
---|
484 | $page->param(grptree => $tmpgrplist);
|
---|
485 |
|
---|
486 | # stuff for menu group change. nb: this is icky.
|
---|
487 | fill_grouplist("grouplist");
|
---|
488 | $page->param(whereami => $ENV{REQUEST_URI});
|
---|
489 | }
|
---|
490 |
|
---|
491 | foreach (@debugbits) { print; }
|
---|
492 |
|
---|
493 | # spit it out
|
---|
494 | print $page->output;
|
---|
495 |
|
---|
496 | print "<div id=debug>webvar keys: <pre>\n";
|
---|
497 | foreach my $key (keys %webvar) {
|
---|
498 | print "key: $key\tval: $webvar{$key}\n";
|
---|
499 | }
|
---|
500 | print "</pre>\nsession:\n<pre>\n";
|
---|
501 | my $sesdata = $session->dataref();
|
---|
502 | foreach my $key (keys %$sesdata) {
|
---|
503 | print "key: $key\tval: ".$sesdata->{$key}."\n";
|
---|
504 | }
|
---|
505 | print "</pre>\nENV:\n<pre>\n";
|
---|
506 | foreach my $key (keys %ENV) {
|
---|
507 | print "key: $key\tval: $ENV{$key}\n";
|
---|
508 | }
|
---|
509 | print "</pre></div>\n";
|
---|
510 |
|
---|
511 | print $footer->output;
|
---|
512 |
|
---|
513 | # as per the docs, Just In Case
|
---|
514 | $session->flush();
|
---|
515 |
|
---|
516 | exit 0;
|
---|
517 |
|
---|
518 |
|
---|
519 | sub fill_grptree {
|
---|
520 | my $root = shift;
|
---|
521 | my $cur = shift;
|
---|
522 |
|
---|
523 | my @childlist;
|
---|
524 |
|
---|
525 | my $grptree = HTML::Template->new(filename => 'templates/grptree.tmpl');
|
---|
526 | getChildren($dbh,$root,\@childlist,'immediate');
|
---|
527 | return if $#childlist == -1;
|
---|
528 | my @grouplist;
|
---|
529 | foreach (@childlist) {
|
---|
530 | my %row;
|
---|
531 | $row{grpname} = groupName($dbh,$_);
|
---|
532 | $row{grpname} = "<b>$row{grpname}</b>" if $_ == $cur;
|
---|
533 | $row{subs} = fill_grptree($_,$cur);
|
---|
534 | push @grouplist, \%row;
|
---|
535 | }
|
---|
536 | $grptree->param(treelvl => \@grouplist);
|
---|
537 | return $grptree->output;
|
---|
538 | }
|
---|
539 |
|
---|
540 |
|
---|
541 | sub changepage {
|
---|
542 | my %params = @_; # think this works the way I want...
|
---|
543 |
|
---|
544 | # handle user check
|
---|
545 | my $newurl = "http://$ENV{HTTP_HOST}$ENV{SCRIPT_NAME}?sid=$sid";
|
---|
546 | foreach (keys %params) {
|
---|
547 | $newurl .= "&$_=$params{$_}";
|
---|
548 | }
|
---|
549 |
|
---|
550 | print "Status: 302\nLocation: $newurl\n\n";
|
---|
551 | exit;
|
---|
552 | } # end changepage
|
---|
553 |
|
---|
554 |
|
---|
555 | sub fillsoa {
|
---|
556 | my $def = shift;
|
---|
557 | my $id = shift;
|
---|
558 | my $domname;
|
---|
559 |
|
---|
560 | if ($webvar{domain} == 0) {
|
---|
561 | $domname = "DOMAIN";
|
---|
562 | } else {
|
---|
563 | my $sth = $dbh->prepare("SELECT domain FROM domains WHERE domain_id=?");
|
---|
564 | $sth->execute($webvar{domain});
|
---|
565 | ($domname) = $sth->fetchrow_array();
|
---|
566 | }
|
---|
567 |
|
---|
568 | $page->param(domain => $domname);
|
---|
569 | $page->param(defrec => !$webvar{domain});
|
---|
570 | $page->param(group => $DNSDB::group);
|
---|
571 |
|
---|
572 | # defaults
|
---|
573 | $page->param(defcontact => $DNSDB::def{contact});
|
---|
574 | $page->param(defns => $DNSDB::def{prins});
|
---|
575 | $page->param(defsoattl => $DNSDB::def{soattl});
|
---|
576 | $page->param(defrefresh => $DNSDB::def{refresh});
|
---|
577 | $page->param(defretry => $DNSDB::def{retry});
|
---|
578 | $page->param(defexpire => $DNSDB::def{expire});
|
---|
579 | $page->param(defminttl => $DNSDB::def{minttl});
|
---|
580 |
|
---|
581 | # there are probably better ways to do this. TMTOWTDI.
|
---|
582 | my %soa = getSOA($dbh,$def,$id);
|
---|
583 |
|
---|
584 | $page->param(domainid => $webvar{domain});
|
---|
585 | $page->param(recid => $soa{recid});
|
---|
586 | $page->param(prins => ($soa{prins} ? $soa{prins} : $DNSDB::def{prins}));
|
---|
587 | $page->param(contact => ($soa{contact} ? $soa{contact} : $DNSDB::def{contact}));
|
---|
588 | $page->param(refresh => ($soa{refresh} ? $soa{refresh} : $DNSDB::def{refresh}));
|
---|
589 | $page->param(retry => ($soa{retry} ? $soa{retry} : $DNSDB::def{retry}));
|
---|
590 | $page->param(expire => ($soa{expire} ? $soa{expire} : $DNSDB::def{expire}));
|
---|
591 | $page->param(minttl => ($soa{minttl} ? $soa{minttl} : $DNSDB::def{minttl}));
|
---|
592 | $page->param(ttl => ($soa{ttl} ? $soa{ttl} : $DNSDB::def{soattl}));
|
---|
593 | }
|
---|
594 |
|
---|
595 | sub showdomain {
|
---|
596 | my $def = shift;
|
---|
597 | my $id = shift;
|
---|
598 |
|
---|
599 | # get the SOA first
|
---|
600 | my %soa = getSOA($dbh,$def,$id);
|
---|
601 |
|
---|
602 | $page->param(recid => $soa{recid});
|
---|
603 | $page->param(contact => $soa{contact});
|
---|
604 | $page->param(prins => $soa{prins});
|
---|
605 | $page->param(refresh => $soa{refresh});
|
---|
606 | $page->param(retry => $soa{retry});
|
---|
607 | $page->param(expire => $soa{expire});
|
---|
608 | $page->param(minttl => $soa{minttl});
|
---|
609 | $page->param(ttl => $soa{ttl});
|
---|
610 |
|
---|
611 | # my @foo2 = getDomRecs($dbh,'def',1);
|
---|
612 | my $foo2 = getDomRecs($dbh,$def,$id,$perpage,$webvar{offset});
|
---|
613 |
|
---|
614 | my $row = 0;
|
---|
615 | foreach my $rec (@$foo2) {
|
---|
616 | $rec->{type} = $typemap{$rec->{type}};
|
---|
617 | $rec->{row} = $row % 2;
|
---|
618 | $rec->{defrec} = $webvar{defrec};
|
---|
619 | $rec->{sid} = $webvar{sid};
|
---|
620 | $rec->{id} = $id;
|
---|
621 | $rec->{distance} = 'n/a' unless ($rec->{type} eq 'MX' || $rec->{type} eq 'SRV');
|
---|
622 | $rec->{weight} = 'n/a' unless ($rec->{type} eq 'SRV');
|
---|
623 | $rec->{port} = 'n/a' unless ($rec->{type} eq 'SRV');
|
---|
624 | $row++;
|
---|
625 | }
|
---|
626 | $page->param(reclist => $foo2);
|
---|
627 | }
|
---|
628 |
|
---|
629 |
|
---|
630 | # fill in record type list on add/update/edit record template
|
---|
631 | sub fill_rectypes {
|
---|
632 | my $type = shift || $reverse_typemap{A};
|
---|
633 |
|
---|
634 | my $sth = $dbh->prepare("SELECT val,name FROM rectypes WHERE stdflag=1 ORDER BY listorder");
|
---|
635 | $sth->execute;
|
---|
636 | my @typelist;
|
---|
637 | while (my ($rval,$rname) = $sth->fetchrow_array()) {
|
---|
638 | my %row = ( recval => $rval, recname => $rname );
|
---|
639 | $row{tselect} = 1 if $rval == $type;
|
---|
640 | push @typelist, \%row;
|
---|
641 | }
|
---|
642 | $page->param(typelist => \@typelist);
|
---|
643 | }
|
---|
644 |
|
---|
645 | sub fill_recdata {
|
---|
646 | fill_rectypes($webvar{type});
|
---|
647 |
|
---|
648 | $page->param(name => $webvar{name});
|
---|
649 | $page->param(address => $webvar{address});
|
---|
650 | $page->param(distance => $webvar{distance})
|
---|
651 | if ($webvar{type} == $reverse_typemap{MX} or $webvar{type} == $reverse_typemap{SRV});
|
---|
652 | $page->param(weight => $webvar{weight}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
653 | $page->param(port => $webvar{port}) if $webvar{type} == $reverse_typemap{SRV};
|
---|
654 | $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl}));
|
---|
655 | }
|
---|
656 |
|
---|
657 |
|
---|
658 | sub fill_actypelist {
|
---|
659 | my @actypes;
|
---|
660 |
|
---|
661 | my %row1 = (actypeval => 'u', actypename => 'user');
|
---|
662 | $row1{typesel} = 1 if $webvar{accttype} eq 'u';
|
---|
663 | push @actypes, \%row1;
|
---|
664 |
|
---|
665 | my %row2 = (actypeval => 'S', actypename => 'superuser');
|
---|
666 | $row2{typesel} = 1 if $webvar{accttype} eq 'S';
|
---|
667 | push @actypes, \%row2;
|
---|
668 |
|
---|
669 | $page->param(actypelist => \@actypes);
|
---|
670 | }
|
---|
671 |
|
---|
672 |
|
---|
673 | sub fill_fpnla {
|
---|
674 | my $count = shift;
|
---|
675 | ##fixme
|
---|
676 | if ($offset eq 'all') {
|
---|
677 | push @debugbits, "foo! wanna see'em all\n";
|
---|
678 | } else {
|
---|
679 | # all these bits only have sensible behaviour if offset is numeric. err, probably.
|
---|
680 | if ($count > $perpage) {
|
---|
681 | # if there are more results than the default, always show the "all" link
|
---|
682 | $page->param(navall => 1);
|
---|
683 |
|
---|
684 | if ($offset > 0) {
|
---|
685 | $page->param(navfirst => 1);
|
---|
686 | $page->param(navprev => 1);
|
---|
687 | $page->param(prevoffs => $offset-1);
|
---|
688 | }
|
---|
689 |
|
---|
690 | # show "next" and "last" links if we're not on the last page of results
|
---|
691 | if ( (($offset+1) * $perpage - $count) < 0 ) {
|
---|
692 | $page->param(navnext => 1);
|
---|
693 | $page->param(nextoffs => $offset+1);
|
---|
694 | $page->param(navlast => 1);
|
---|
695 | $page->param(lastoffs => int (($count-1)/$perpage));
|
---|
696 | }
|
---|
697 | }
|
---|
698 | }
|
---|
699 | } # end fill_fpnla()
|
---|
700 |
|
---|
701 |
|
---|
702 | sub fill_pgcount {
|
---|
703 | my $pgcount = shift;
|
---|
704 | my $pgtype = shift;
|
---|
705 | my $parent = shift;
|
---|
706 |
|
---|
707 | $page->param(ntot => $pgcount);
|
---|
708 | $page->param(nfirst => (($offset eq 'all' ? 0 : $offset)*$perpage+1));
|
---|
709 | $page->param(npglast => ($offset eq 'all' ? $pgcount :
|
---|
710 | ( (($offset+1)*$perpage) > $pgcount ? $pgcount : (($offset+1)*$perpage) )
|
---|
711 | ));
|
---|
712 | $page->param(pgtype => $pgtype);
|
---|
713 | $page->param(parent => $parent);
|
---|
714 | } # end fill_pgcount()
|
---|
715 |
|
---|
716 |
|
---|
717 | sub listdomains {
|
---|
718 | my $sth = $dbh->prepare("SELECT count(*) FROM domains WHERE group_id=?");
|
---|
719 | $sth->execute($curgroup);
|
---|
720 | my ($count) = $sth->fetchrow_array;
|
---|
721 |
|
---|
722 | # fill page count and first-previous-next-last-all bits
|
---|
723 | ##fixme - hardcoded group bit
|
---|
724 | fill_pgcount($count,"domains",groupName($dbh,$curgroup));
|
---|
725 | fill_fpnla($count);
|
---|
726 |
|
---|
727 | ##fixme - group
|
---|
728 | $page->param(group => $curgroup);
|
---|
729 | my @domlist;
|
---|
730 | $sth = $dbh->prepare("SELECT domain_id,domain,status,groups.group_name FROM domains".
|
---|
731 | " INNER JOIN groups ON domains.group_id=groups.group_id".
|
---|
732 | " WHERE domains.group_id=?".
|
---|
733 | " ORDER BY domain".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
---|
734 | $sth->execute($curgroup);
|
---|
735 | my $rownum = 0;
|
---|
736 | while (my @data = $sth->fetchrow_array) {
|
---|
737 | my %row;
|
---|
738 | $row{domainid} = $data[0];
|
---|
739 | $row{domain} = $data[1];
|
---|
740 | $row{status} = ($data[2] ? 'Active' : 'Inactive');
|
---|
741 | $row{group} = $data[3];
|
---|
742 | $row{bg} = ($rownum++)%2;
|
---|
743 | # $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0);
|
---|
744 | $row{mkactive} = !$data[2];
|
---|
745 | $row{sid} = $sid;
|
---|
746 | $row{offset} = $offset;
|
---|
747 | ##fixme: need to clean up status indicator/usage/inversion
|
---|
748 | push @domlist, \%row;
|
---|
749 | }
|
---|
750 | $page->param(domtable => \@domlist);
|
---|
751 | } # end listdomains()
|
---|
752 |
|
---|
753 |
|
---|
754 | sub listgroups {
|
---|
755 | my $sth = $dbh->prepare("select count(*) from groups");
|
---|
756 | $sth->execute;
|
---|
757 | my ($count) = ($sth->fetchrow_array);
|
---|
758 |
|
---|
759 | # fill page count and first-previous-next-last-all bits
|
---|
760 | ##fixme - hardcoded group bit
|
---|
761 | fill_pgcount($count,"groups",'');
|
---|
762 | fill_fpnla($count);
|
---|
763 |
|
---|
764 | my @grouplist;
|
---|
765 | $sth = $dbh->prepare("SELECT g.group_id, g.group_name, g2.group_name, ".
|
---|
766 | "count(distinct(u.email)), count(distinct(d.domain)) ".
|
---|
767 | "FROM groups g ".
|
---|
768 | "INNER JOIN groups g2 ON g2.group_id=g.parent_group_id ".
|
---|
769 | "LEFT OUTER JOIN users u ON u.group_id=g.group_id ".
|
---|
770 | "LEFT OUTER JOIN domains d ON d.group_id=g.group_id ".
|
---|
771 | "GROUP BY g.group_id, g.group_name, g2.group_name ".
|
---|
772 | "ORDER BY g.group_id".($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
---|
773 | $sth->execute;
|
---|
774 |
|
---|
775 | my $rownum = 0;
|
---|
776 | while (my @data = $sth->fetchrow_array) {
|
---|
777 | my %row;
|
---|
778 | $row{groupid} = $data[0];
|
---|
779 | $row{groupname} = $data[1];
|
---|
780 | $row{pgroup} = $data[2];
|
---|
781 | $row{nusers} = $data[3];
|
---|
782 | $row{ndomains} = $data[4];
|
---|
783 | $row{bg} = ($rownum++)%2;
|
---|
784 | $row{sid} = $sid;
|
---|
785 | push @grouplist, \%row;
|
---|
786 | }
|
---|
787 | $page->param(grouptable => \@grouplist);
|
---|
788 | } # end listgroups()
|
---|
789 |
|
---|
790 |
|
---|
791 | sub fill_grouplist {
|
---|
792 | my $template_var = shift;
|
---|
793 | my $cur = shift || $curgroup;
|
---|
794 | # weesa gonna discard parent_group_id for now
|
---|
795 | my $sth = $dbh->prepare("SELECT group_id,parent_group_id,group_name FROM groups ORDER BY group_id");
|
---|
796 | $sth->execute;
|
---|
797 | my @grouplist;
|
---|
798 | while (my ($groupid,$pargroup,$groupname) = $sth->fetchrow_array()) {
|
---|
799 | my %row;
|
---|
800 | $row{groupname} = $groupname;
|
---|
801 | $row{groupval} = $groupid;
|
---|
802 | ##fixme: need magic
|
---|
803 | # $row{defgroup} = '';
|
---|
804 | $row{groupactive} = 1 if $groupid == $cur;
|
---|
805 | push @grouplist, \%row;
|
---|
806 | }
|
---|
807 |
|
---|
808 | $page->param("$template_var" => \@grouplist);
|
---|
809 |
|
---|
810 | } # end fill_grouplist()
|
---|
811 |
|
---|
812 | sub list_users {
|
---|
813 | my $sth = $dbh->prepare("select count(*) from users where group_id=?");
|
---|
814 | $sth->execute($curgroup);
|
---|
815 | my ($count) = ($sth->fetchrow_array);
|
---|
816 |
|
---|
817 | # fill page count and first-previous-next-last-all bits
|
---|
818 | ##fixme - hardcoded group bit
|
---|
819 | fill_pgcount($count,"users",'');
|
---|
820 | fill_fpnla($count);
|
---|
821 |
|
---|
822 | my @userlist;
|
---|
823 | $sth = $dbh->prepare("SELECT u.user_id, u.email, u.firstname, u.lastname, u.type, g.group_name, u.status ".
|
---|
824 | "FROM users u ".
|
---|
825 | "INNER JOIN groups g ON u.group_id=g.group_id ".
|
---|
826 | "WHERE u.group_id=?".
|
---|
827 | ($offset eq 'all' ? '' : " LIMIT $perpage OFFSET ".$offset*$perpage));
|
---|
828 | $sth->execute($curgroup);
|
---|
829 |
|
---|
830 | my $rownum = 0;
|
---|
831 | while (my @data = $sth->fetchrow_array) {
|
---|
832 | my %row;
|
---|
833 | $row{userid} = $data[0];
|
---|
834 | $row{username} = $data[1];
|
---|
835 | $row{userfull} = "$data[2] $data[3]";
|
---|
836 | $row{usertype} = ($data[4] eq 'S' ? 'superuser' : "user");
|
---|
837 | $row{usergroup} = $data[5];
|
---|
838 | $row{mkactive} = $data[6];
|
---|
839 | $row{bg} = ($rownum++)%2;
|
---|
840 | $row{sid} = $sid;
|
---|
841 | push @userlist, \%row;
|
---|
842 | }
|
---|
843 | $page->param(usertable => \@userlist);
|
---|
844 | }
|
---|