Changeset 3
- Timestamp:
- 08/18/09 18:04:14 (15 years ago)
- Location:
- trunk
- Files:
-
- 6 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r2 r3 23 23 @ISA = qw(Exporter); 24 24 @EXPORT_OK = qw( 25 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec 25 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs 26 &addRec &delRec &domStatus 26 27 %typemap %reverse_typemap 27 28 ); … … 29 30 @EXPORT = (); # Export nothing by default. 30 31 %EXPORT_TAGS = ( ALL => [qw( 31 &initGlobals &connectDB &finish &addDomain &domainName &getSOA &getDomRecs &addRec 32 &initGlobals &connectDB &finish &addDomain &delDomain &domainName &getSOA &getRecLine &getDomRecs 33 &addRec &delRec &domStatus 32 34 %typemap %reverse_typemap 33 35 )] … … 159 161 return ('FAIL',"Need domain status") if !defined($state); 160 162 163 my $dom_id; 164 161 165 # Allow transactions, and raise an exception on errors so we can catch it later. 162 166 # Use local to make sure these get "reset" properly on exiting this block … … 173 177 $sth = $dbh->prepare("select domain_id from domains where domain='$domain'"); 174 178 $sth->execute; 175 my($dom_id) = $sth->fetchrow_array();179 ($dom_id) = $sth->fetchrow_array(); 176 180 177 181 # ... and now we construct the standard records from the default set. NB: group should be variable. 178 $sth = $dbh->prepare("select host,type,val,distance, ttl from default_records where group_id=$group");179 my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance, ttl)".180 " values ($dom_id,?,?,?,?,? )");182 $sth = $dbh->prepare("select host,type,val,distance,weight,port,ttl from default_records where group_id=$group"); 183 my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,weight,port,ttl)". 184 " values ($dom_id,?,?,?,?,?,?,?)"); 181 185 $sth->execute; 182 while (my ($host,$type,$val,$dist,$ ttl) = $sth->fetchrow_array()) {186 while (my ($host,$type,$val,$dist,$weight,$port,$ttl) = $sth->fetchrow_array()) { 183 187 $host =~ s/DOMAIN/$domain/g; 184 $sth_in->execute($host,$type,$val,$dist,$ ttl);188 $sth_in->execute($host,$type,$val,$dist,$weight,$port,$ttl); 185 189 } 190 191 # once we get here, we should have suceeded. 192 $dbh->commit; 193 }; # end eval 194 195 if ($@) { 196 my $msg = $@; 197 eval { $dbh->rollback; }; 198 return ('FAIL',$msg); 199 } else { 200 return ('OK',$dom_id); 201 } 202 } # end addDomain 203 204 205 ## DNSDB::delDomain() 206 # Delete a domain. 207 # for now, just delete the records, then the domain. 208 # later we may want to archive it in some way instead (status code 2, for example?) 209 sub delDomain { 210 my $dbh = shift; 211 my $domain = shift; 212 213 # Allow transactions, and raise an exception on errors so we can catch it later. 214 # Use local to make sure these get "reset" properly on exiting this block 215 local $dbh->{AutoCommit} = 0; 216 local $dbh->{RaiseError} = 1; 217 218 # Wrap all the SQL in a transaction 219 eval { 220 # brute force. 221 my $sth = $dbh->prepare("select domain_id from domains where domain=?"); 222 $sth->execute($domain); 223 die "Domain not found, can't delete\n" if $sth->rows < 1; 224 my ($id) = $sth->fetchrow_array; 225 226 $sth = $dbh->prepare("delete from records where domain_id=$id"); 227 $sth->execute; 228 $sth = $dbh->prepare("delete from domains where domain=?"); 229 $sth->execute($domain); 186 230 187 231 # once we get here, we should have suceeded. … … 196 240 return ('OK','OK'); 197 241 } 198 } # end addDomain 242 243 } # end delDomain() 199 244 200 245 … … 249 294 $sql .= " records where domain_id=$id and type=$reverse_typemap{SOA}"; 250 295 } 251 print "MDEBUG: $sql<br>\n";296 #print "getSOA DEBUG: $sql<br>\n"; 252 297 my $sth = $dbh->prepare($sql); 253 298 $sth->execute; … … 292 337 return undef; 293 338 } 294 my %ret;339 my %ret; 295 340 $ret{recid} = $recid; 296 341 $ret{host} = $host; … … 338 383 339 384 340 ## DN DB::addRec()385 ## DNSDB::addRec() 341 386 # Add a new record to a domain or a group's default records 342 387 # Takes a database handle, default/live flag, group/domain ID, … … 378 423 my $sql = "insert into ".($defrec eq 'y' ? 'default_' : '')."records ($fields) values ($vallist)"; 379 424 # something is bugging me about this... 425 print "DEBUG: $sql<br>\n"; 380 426 my $sth = $dbh->prepare($sql); 381 427 $sth->execute; … … 387 433 388 434 435 ## DNSDB::delRec() 436 # Delete a record. 437 sub delRec { 438 $errstr = ''; 439 my $dbh = shift; 440 my $defrec = shift; 441 my $id = shift; 442 443 my $sth = $dbh->prepare("delete from ".($defrec eq 'y' ? 'default_' : '')."records where record_id=?"); 444 $sth->execute($id); 445 446 return ('FAIL',$sth->errstr) if $sth->err; 447 448 return ('OK','OK'); 449 } # end delRec() 450 451 452 ## DNSDB::domStatus() 453 # Sets and/or returns a domain's status 454 # Takes a database handle, domain ID and optionally a status argument 455 # Returns undef on errors. 456 sub domStatus { 457 my $dbh = shift; 458 my $id = shift; 459 my $newstatus = shift; 460 461 return undef if $id !~ /^\d+$/; 462 463 my $sth; 464 465 # ooo, fun! let's see what we were passed for status 466 if ($newstatus) { 467 $sth = $dbh->prepare("update domains set status=? where domain_id=?"); 468 # ass-u-me caller knows what's going on in full 469 if ($newstatus =~ /^[01]$/) { # only two valid for now. 470 $sth->execute($newstatus,$id); 471 } elsif ($newstatus =~ /^domo(?:n|ff)$/) { 472 $sth->execute(($newstatus eq 'domon' ? 1 : 0),$id); 473 } 474 } 475 476 $sth = $dbh->prepare("select status from domains where domain_id=?"); 477 $sth->execute($id); 478 my ($status) = $sth->fetchrow_array; 479 return $status; 480 } # end domStatus() 481 482 389 483 # shut Perl up 390 484 1; -
trunk/dns.cgi
r2 r3 7 7 # Last update by $Author$ 8 8 ### 9 # Copyright (C) 2008 - Kris Deugau <kdeugau@deepnet.cx>9 # Copyright (C) 2008,2009 - Kris Deugau <kdeugau@deepnet.cx> 10 10 11 11 use strict; … … 63 63 64 64 # default 65 my $perpage = 15; 65 #my $perpage = 15; 66 my $perpage = 3; 66 67 my $offset = ($webvar{offset} ? $webvar{offset} : 0); 67 68 … … 82 83 # Default page is a login page 83 84 my $page; # to be initialized as an HTML::Template entity sooner or later 85 86 84 87 85 88 # decide which page to spit out... … … 93 96 94 97 if ($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 95 104 my $sth = $dbh->prepare("select count(*) from domains"); 96 105 $sth->execute; 97 106 my ($count) = ($sth->fetchrow_array); 98 107 99 if ($count > $perpage) { 100 # if there are more results than the default, always show the "all" link 101 $page->param(navall => 1); 102 103 if ($offset > 0) { 104 $page->param(navfirst => 1); 105 $page->param(navprev => 1); 106 $page->param(prevoffs => $offset-1); 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 } 107 130 } 108 109 # show "next" and "last" links if we're not on the last page of results110 if ( (($offset+1) * $perpage - $count) < 0 ) {111 $page->param(navnext => 1);112 $page->param(nextoffs => $offset+1);113 $page->param(navlast => 1);114 $page->param(lastoffs => int $count/$perpage);115 }116 131 } 117 132 118 133 $page->param(ndomains => $count); 119 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 )); 120 138 my @domlist; 121 139 $sth = $dbh->prepare("select domain_id,domain,status,groups.name from domains". 122 140 " inner join groups on domains.group_id=groups.group_id". 123 " order by domain limit $perpage offset ".$offset*$perpage);141 " order by domain".($offset eq 'all' ? '' : " limit $perpage offset ".$offset*$perpage)); 124 142 $sth->execute; 125 143 my $rownum = 0; … … 128 146 $row{domainid} = $data[0]; 129 147 $row{domain} = $data[1]; 130 $row{status} = $data[2];148 $row{status} = ($data[2] ? 'Active' : 'Inactive'); 131 149 $row{group} = $data[3]; 132 150 $row{bg} = ($rownum++)%2; 133 $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0); 151 # $row{mkactive} = ($data[2] eq 'inactive' ? 1 : 0); 152 $row{mkactive} = !$data[2]; 134 153 $row{sid} = $sid; 154 $row{offset} = $offset; 135 155 ##fixme: need to clean up status indicator/usage/inversion 136 156 push @domlist, \%row; … … 190 210 print "whee!\n"; 191 211 192 newrec(); 212 # populate most fields as needed. (eg, type list.) 213 newrec(); 193 214 194 215 } elsif ($webvar{page} eq 'addrec') { … … 221 242 } 222 243 244 $page->param(defrec => $webvar{defrec}); 245 223 246 } elsif ($webvar{page} eq 'conf_del') { 224 247 … … 236 259 if (!defined($webvar{del})) { 237 260 $page->param(del_getconf => 1); 238 } else { 239 # $page->param( 240 } 241 ##work 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 } 242 275 243 276 } elsif ($webvar{page} eq 'editsoa') { … … 258 291 $sth = $dbh->prepare($sql); 259 292 $sth->execute; 260 print "DEBUG: $sql<br>\n";261 #print "DEBUG: ".$DBI::errstr;262 293 263 294 if ($sth->err) { … … 279 310 # hokay, a bit of magic to decide which page we hit. 280 311 if ($code eq 'OK') { 281 $page = HTML::Template->new(filename => "$templatedir/domlist.tmpl"); 312 $page = HTML::Template->new(filename => "$templatedir/reclist.tmpl"); 313 showdomain(0,$msg); 314 ##work 282 315 } else { 283 316 # oooh, yeah, this is supposed to be a redirect. er, maybe. whee. … … 288 321 } 289 322 290 # my $sth = $dbh->prepare("insert into domains (domain,group_id,status) values (?,?,?)"); 291 # $sth->execute($webvar{domain},1,($webvar{makeactive} ? 'active' : 'inactive')) or die $DBI::errstr; 292 # $sth = $dbh->prepare("select domain_id from domains where domain='$webvar{domain}'"); 293 # $sth->execute; 294 # my ($dom_id) = $sth->fetchrow_array(); 295 # $sth = $dbh->prepare("select host,type,val,distance,ttl from default_records where group_id=1"); 296 # my $sth_in = $dbh->prepare("insert into records (domain_id,host,type,val,distance,ttl)". 297 # " values ($dom_id,?,?,?,?,?)"); 298 # $sth->execute; 299 # while (my ($host,$type,$val,$dist,$ttl) = $sth->fetchrow_array()) { 300 # $host =~ s/DOMAIN/$webvar{domain}/g; 301 # $sth_in->execute($host,$type,$val,$dist,$ttl); 302 # } 303 # $dbh->commit or print "failed to add $webvar{domain}: ".$DBI::errstr."\n"; 304 305 # $page->param(add_failed => 1); 323 306 324 } 307 325 … … 410 428 $page->param(typelist => \@typelist); 411 429 $page->param(domain => domainName($dbh,$webvar{domainid})); 412 $page->param(parentid => $webvar{parentid});413 430 $page->param(defrec => $webvar{defrec}); 414 431 $page->param(ttl => ($webvar{ttl} ? $webvar{ttl} : $DNSDB::def{minttl})); 415 } 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 } -
trunk/templates/adddomain.tmpl
r2 r3 2 2 <TMPL_INCLUDE NAME="newdomain.tmpl"> 3 3 <TMPL_ELSE> 4 <TMPL_INCLUDE NAME=" domdetail.tmpl">4 <TMPL_INCLUDE NAME="reclist.tmpl"> 5 5 </TMPL_IF> -
trunk/templates/delrec.tmpl
r2 r3 4 4 5 5 <td align="center"> 6 <h3>Are you really sure you want to delete <TMPL_VAR NAME=ftype> record <TMPL_VAR NAME=recval>?</h3> 6 <h3>Are you really sure you want to delete record:<br> 7 <TMPL_VAR NAME=host> <TMPL_VAR NAME=ftype> <TMPL_VAR NAME=recval></h3> 7 8 <a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&del=cancel&id=<TMPL_VAR NAME=id>&defrec=<TMPL_VAR NAME=defrec>">cancel</a> | <a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&del=ok&id=<TMPL_VAR NAME=id>&defrec=<TMPL_VAR NAME=defrec>">confirm</a> 8 9 </td></tr></table> … … 10 11 <TMPL_ELSE> 11 12 <TMPL_IF del_failed> 13 <!-- Need to find failure mode to cause/test this --> 14 <h1>FOO!</h1> 15 <TMPL_VAR NAME=errmsg> 12 16 <TMPL_ELSE> 13 17 <TMPL_INCLUDE NAME="reclist.tmpl"> -
trunk/templates/domlist.tmpl
r2 r3 12 12 <TMPL_IF navnext><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=<TMPL_VAR NAME=nextoffs>">next<img src="images/fwd.png" border=0></a><TMPL_ELSE>next<img src="images/fwd.png" border=0></TMPL_IF> 13 13 <TMPL_IF navlast><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=<TMPL_VAR NAME=lastoffs>">last<img src="images/ffwd.png" border=0></a><TMPL_ELSE>last<img src="images/ffwd.png" border=0></TMPL_IF> 14 <TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist ">all</a><TMPL_ELSE>all</TMPL_IF>14 <TMPL_IF navall><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist&offset=all">all</a><TMPL_ELSE>all</TMPL_IF> 15 15 </td><td align=right>insert search box here</td></tr> 16 16 … … 41 41 <td width="1%" nowrap><TMPL_VAR name=status></td> 42 42 <td width="1%" nowrap><TMPL_VAR name=group></td> 43 <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page= chgstatus&id=<TMPL_VAR NAME=domainid>&defrec=n"><TMPL_IF NAME=mkactive>activate<TMPL_ELSE>deactivate</TMPL_IF></a></td>43 <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=domlist<TMPL_IF NAME=offset>&offset=<TMPL_VAR NAME=offset></TMPL_IF>&id=<TMPL_VAR NAME=domainid>&action=<TMPL_IF NAME=mkactive>domon<TMPL_ELSE>domoff</TMPL_IF>"><TMPL_IF NAME=mkactive>activate<TMPL_ELSE>deactivate</TMPL_IF></a></td> 44 44 <td width="1%" nowrap align=center><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=del&id=<TMPL_VAR NAME=domainid>&defrec=w"><img src="images/trash2.png" border=0></a></td> 45 45 </tr> -
trunk/templates/reclist.tmpl
r2 r3 49 49 <td width="5%" nowrap><TMPL_VAR NAME=port></td> 50 50 <td width="5%" nowrap><TMPL_VAR NAME=ttl></td> 51 <td width="5%" align="center" nowrap><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page= conf_del&id=<TMPL_VAR NAME=record_id>&defrec=<TMPL_VAR NAME=defrec>"><img border=0 src="images/trash2.png"></a></td>51 <td width="5%" align="center" nowrap><a href="dns.cgi?sid=<TMPL_VAR NAME=sid>&page=delrec&id=<TMPL_VAR NAME=record_id>&defrec=<TMPL_VAR NAME=defrec>"><img border=0 src="images/trash2.png"></a></td> 52 52 </TMPL_LOOP> 53 53 </table>
Note:
See TracChangeset
for help on using the changeset viewer.