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