- Timestamp:
- 03/02/12 17:51:29 (13 years ago)
- Location:
- trunk
- Files:
-
- 2 added
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/DNSDB.pm
r259 r260 29 29 &changeGroup 30 30 &loadConfig &connectDB &finish 31 &addDomain &delDomain &domainName &revName &domainID 31 &addDomain &delDomain &domainName &revName &domainID &addRDNS 32 32 &getZoneCount &getZoneList 33 33 &addGroup &delGroup &getChildren &groupName … … 51 51 &changeGroup 52 52 &loadConfig &connectDB &finish 53 &addDomain &delDomain &domainName &revName &domainID 53 &addDomain &delDomain &domainName &revName &domainID &addRDNS 54 54 &getZoneCount &getZoneList 55 55 &addGroup &delGroup &getChildren &groupName … … 1215 1215 return $domid if $domid; 1216 1216 } # end domainID() 1217 1218 1219 ## DNSDB::addRDNS 1220 # Adds a reverse DNS zone 1221 # Takes a database handle, CIDR block, numeric group, boolean(ish) state (active/inactive), 1222 # and user info hash (for logging). 1223 # Returns a status code and message 1224 sub addRDNS { 1225 my $dbh = shift; 1226 my $zone = NetAddr::IP->new(shift); 1227 return ('FAIL',"Zone name must be a valid CIDR netblock") unless ($zone && $zone->addr !~ /^0/); 1228 my $revpatt = shift; 1229 my $group = shift; 1230 my $state = shift; 1231 1232 my %userinfo = @_; # remaining bits. 1233 # user ID, username, user full name 1234 1235 $state = 1 if $state =~ /^active$/; 1236 $state = 1 if $state =~ /^on$/; 1237 $state = 0 if $state =~ /^inactive$/; 1238 $state = 0 if $state =~ /^off$/; 1239 1240 return ('FAIL',"Invalid zone status") if $state !~ /^\d+$/; 1241 1242 # quick check to start to see if we've already got one 1243 my ($rdns_id) = $dbh->selectrow_array("SELECT rdns_id FROM revzones WHERE revzone=?", undef, ("$zone")); 1244 1245 return ('FAIL', "Zone already exists") if $rdns_id; 1246 1247 # Allow transactions, and raise an exception on errors so we can catch it later. 1248 # Use local to make sure these get "reset" properly on exiting this block 1249 local $dbh->{AutoCommit} = 0; 1250 local $dbh->{RaiseError} = 1; 1251 1252 #$dbh->selectrow_array("SELECT currval('users_user_id_seq')"); 1253 # Wrap all the SQL in a transaction 1254 eval { 1255 # insert the domain... 1256 $dbh->do("INSERT INTO revzones (revnet,group_id,status) VALUES (?,?,?)", undef, ($zone, $group, $state)); 1257 1258 # get the ID... 1259 ($rdns_id) = $dbh->selectrow_array("SELECT currval('revzones_rdns_id_seq')"); 1260 1261 _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group, username => $userinfo{name}, 1262 entry => "Added ".($state ? 'active' : 'inactive')." reverse zone $zone")); 1263 1264 # ... and now we construct the standard records from the default set. NB: group should be variable. 1265 my $sth = $dbh->prepare("SELECT host,type,val,ttl FROM default_rev_records WHERE group_id=?"); 1266 my $sth_in = $dbh->prepare("INSERT INTO records (rdns_id,host,type,val,ttl)". 1267 " VALUES ($rdns_id,?,?,?,?)"); 1268 $sth->execute($group); 1269 while (my ($host,$type,$val,$ttl) = $sth->fetchrow_array()) { 1270 $host =~ s/ADMINDOMAIN/$config{domain}/g; 1271 ##work 1272 # - replace ZONE in $val 1273 # - skip records not appropriate for the zone (skip A+PTR on v6 zones, and AAAA+PTR on v4 zones) 1274 # $val =~ s/DOMAIN/$domain/g; 1275 $sth_in->execute($host,$type,$val,$ttl); 1276 if ($typemap{$type} eq 'SOA') { 1277 my @tmp1 = split /:/, $host; 1278 my @tmp2 = split /:/, $val; 1279 _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group, 1280 username => $userinfo{name}, entry => 1281 "[new $zone] Added SOA record [contact $tmp1[0]] [master $tmp1[1]] ". 1282 "[refresh $tmp2[0]] [retry $tmp2[1]] [expire $tmp2[2]] [minttl $tmp2[3]], TTL $ttl")); 1283 } else { 1284 my $logentry = "[new $zone] Added record '$host $typemap{$type}"; 1285 # $logentry .= " [distance $dist]" if $typemap{$type} eq 'MX'; 1286 # $logentry .= " [priority $dist] [weight $weight] [port $port]" if $typemap{$type} eq 'SRV'; 1287 _log($dbh, (rdns_id => $rdns_id, user_id => $userinfo{id}, group_id => $group, 1288 username => $userinfo{name}, entry => 1289 $logentry." $val', TTL $ttl")); 1290 } 1291 } 1292 1293 # once we get here, we should have suceeded. 1294 $dbh->commit; 1295 }; # end eval 1296 1297 if ($@) { 1298 my $msg = $@; 1299 eval { $dbh->rollback; }; 1300 return ('FAIL',$msg); 1301 } else { 1302 return ('OK',$rdns_id); 1303 } 1304 1305 } # end addRDNS() 1217 1306 1218 1307 -
trunk/dns.cgi
r259 r260 426 426 $page->param(curpage => $webvar{page}); 427 427 listzones(); 428 429 } elsif ($webvar{page} eq 'newrevzone') { 430 431 ## scope/access check - use domain settings? invent new (bleh) 432 changepage(page => "revzones", errmsg => "You are not permitted to add reverse zones") 433 unless ($permissions{admin} || $permissions{domain_create}); 434 435 fill_grouplist("grouplist"); 436 437 if ($webvar{add_failed}) { 438 $page->param(add_failed => 1); 439 $page->param(errmsg => $webvar{errmsg}); 440 $page->param(revzone => $webvar{revzone}); 441 $page->param(revpatt => $webvar{revpatt}); 442 } 443 444 } elsif ($webvar{page} eq 'addrevzone') { 445 446 changepage(page => "revzones", errmsg => "You are not permitted to add reverse zones") 447 unless ($permissions{admin} || $permissions{domain_create}); 448 449 # security check - does the user have permission to access this entity? 450 if (!check_scope(id => $webvar{group}, type => 'group')) { 451 changepage(page => "newrevzone", add_failed => 1, revzone => $webvar{revzone}, revpatt => $webvar{revpatt}, 452 errmsg => "You do not have permission to add a reverse zone to the requested group"); 453 } 454 455 my ($code,$msg) = addRDNS($dbh, $webvar{revzone}, $webvar{revpatt}, $webvar{group}, 456 ($webvar{makeactive} eq 'on' ? 1 : 0), 457 (username => $session->param("username"), id => $session->param("uid")) ); 458 459 if ($code eq 'OK') { 460 logaction(0, $session->param("username"), $webvar{group}, "Added reverse zone $webvar{revzone}", $msg); 461 changepage(page => "reclist", id => $msg, revrec => 'y'); 462 } else { 463 logaction(0, $session->param("username"), $webvar{group}, "Failed adding reverse zone $webvar{revzone} ($msg)"); 464 changepage(page => "newrevzone", add_failed => 1, revzone => $webvar{revzone}, revpatt => $webvar{revpatt}, 465 errmsg => $msg); 466 } 467 468 #} elsif ($webvar{page} eq 'delrevzone') { 428 469 429 470 } elsif ($webvar{page} eq 'reclist') {
Note:
See TracChangeset
for help on using the changeset viewer.