Skip to content

Commit f6ec140

Browse files
committed
Refactor multiple TODOs into two.
This removes duplication of the zone_file_prefix var. It might be nice to make it configurable in a follow up.
1 parent 57acfd0 commit f6ec140

File tree

1 file changed

+23
-74
lines changed

1 file changed

+23
-74
lines changed

server/plugins-available/bind_plugin.inc.php

Lines changed: 23 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,8 @@ function soa_dnssec_create(&$data) {
8686
//* load the server configuration options
8787
$dns_config = $app->getconf->get_server_config($conf["server_id"], 'dns');
8888

89-
//TODO : change this when distribution information has been integrated into server record
90-
$filespre = (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.';
91-
9289
$domain = substr($data['new']['origin'], 0, strlen($data['new']['origin'])-1);
93-
if (!file_exists($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain)) return false;
90+
if (!file_exists($dns_config['bind_zonefiles_dir'].'/'.$this->zone_file_prefix().$domain)) return false;
9491

9592
//* Check Entropy
9693
if (file_get_contents('/proc/sys/kernel/random/entropy_avail') < 400) {
@@ -129,9 +126,7 @@ function soa_dnssec_sign(&$data) {
129126
//* load the server configuration options
130127
$dns_config = $app->getconf->get_server_config($conf["server_id"], 'dns');
131128

132-
//TODO : change this when distribution information has been integrated into server record
133-
$filespre = (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.';
134-
129+
$filespre = $this->zone_file_prefix();
135130
$domain = substr($data['new']['origin'], 0, strlen($data['new']['origin'])-1);
136131
if (!file_exists($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain)) return false;
137132

@@ -168,9 +163,7 @@ function soa_dnssec_update(&$data, $new=false) {
168163
//* load the server configuration options
169164
$dns_config = $app->getconf->get_server_config($conf["server_id"], 'dns');
170165

171-
//TODO : change this when distribution information has been integrated into server record
172-
$filespre = (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.';
173-
166+
$filespre = $this->zone_file_prefix();
174167
$domain = substr($data['new']['origin'], 0, strlen($data['new']['origin'])-1);
175168
if (!file_exists($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain)) return false;
176169

@@ -203,13 +196,10 @@ function soa_dnssec_delete(&$data) {
203196
//* load the server configuration options
204197
$dns_config = $app->getconf->get_server_config($conf["server_id"], 'dns');
205198

206-
//TODO : change this when distribution information has been integrated into server record
207-
$filespre = (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.';
208-
209199
$domain = substr($data['new']['origin'], 0, strlen($data['new']['origin'])-1);
210200

211201
unlink($dns_config['bind_zonefiles_dir'].'/K'.$domain.'.+*');
212-
unlink($dns_config['bind_zonefiles_dir'].'/'.$filespre.$domain.'.signed');
202+
unlink($dns_config['bind_zonefiles_dir'].'/'.$this->zone_file_prefix().$domain.'.signed');
213203
unlink($dns_config['bind_zonefiles_dir'].'/dsset-'.$domain.'.');
214204

215205
if ($app->dbmaster !== $app->db) $app->dbmaster->query('UPDATE dns_soa SET dnssec_info=\'\', dnssec_initialized=\'N\' WHERE id=?', intval($data['new']['id']));
@@ -278,13 +268,7 @@ function soa_update($event_name, $data) {
278268
}
279269
$tpl->setLoop('zones', $records);
280270

281-
//TODO : change this when distribution information has been integrated into server record
282-
if (file_exists('/etc/gentoo-release')) {
283-
$filename = $dns_config['bind_zonefiles_dir'].'/pri/'.str_replace("/", "_", substr($zone['origin'], 0, -1));
284-
}
285-
else {
286-
$filename = $dns_config['bind_zonefiles_dir'].'/pri.'.str_replace("/", "_", substr($zone['origin'], 0, -1));
287-
}
271+
$filename = $dns_config['bind_zonefiles_dir'].'/' . $this->zone_file_prefix() . str_replace("/", "_", substr($zone['origin'], 0, -1));
288272

289273
$old_zonefile = @file_get_contents($filename);
290274
file_put_contents($filename, $tpl->grab());
@@ -327,13 +311,7 @@ function soa_update($event_name, $data) {
327311
}
328312
else if ($data['new']['dnssec_wanted'] == 'Y' && $data['old']['dnssec_initialized'] == 'N') $this->soa_dnssec_create($data);
329313
else if ($data['new']['dnssec_wanted'] == 'N' && $data['old']['dnssec_initialized'] == 'Y') { //delete old signed file if dnssec is no longer wanted
330-
//TODO : change this when distribution information has been integrated into server record
331-
if (file_exists('/etc/gentoo-release')) {
332-
$filename = $dns_config['bind_zonefiles_dir'].'/pri/'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
333-
}
334-
else {
335-
$filename = $dns_config['bind_zonefiles_dir'].'/pri.'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
336-
}
314+
$filename = $dns_config['bind_zonefiles_dir'].'/' . $this->zone_file_prefix() . str_replace("/", "_", substr($data['old']['origin'], 0, -1));
337315
if(is_file($filename.'.signed')) unlink($filename.'.signed');
338316
} else if ($data['new']['dnssec_wanted'] == 'Y') $this->soa_dnssec_update($data);
339317
// END DNSSEC
@@ -345,13 +323,7 @@ function soa_update($event_name, $data) {
345323

346324
//* Delete old domain file, if domain name has been changed
347325
if($data['old']['origin'] != $data['new']['origin']) {
348-
//TODO : change this when distribution information has been integrated into server record
349-
if (file_exists('/etc/gentoo-release')) {
350-
$filename = $dns_config['bind_zonefiles_dir'].'/pri/'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
351-
}
352-
else {
353-
$filename = $dns_config['bind_zonefiles_dir'].'/pri.'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
354-
}
326+
$filename = $dns_config['bind_zonefiles_dir'].'/' . $this->zone_file_prefix() . str_replace("/", "_", substr($data['old']['origin'], 0, -1));
355327

356328
if(is_file($filename)) unlink($filename);
357329
if(is_file($filename.'.err')) unlink($filename.'.err');
@@ -378,14 +350,7 @@ function soa_delete($event_name, $data) {
378350
$this->write_named_conf($data, $dns_config);
379351

380352
//* Delete the domain file
381-
//TODO : change this when distribution information has been integrated into server record
382-
if (file_exists('/etc/gentoo-release')) {
383-
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/pri/'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
384-
}
385-
else {
386-
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/pri.'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
387-
}
388-
353+
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/' . $this->zone_file_prefix() . str_replace("/", "_", substr($data['old']['origin'], 0, -1));
389354
if(is_file($zone_file_name)) unlink($zone_file_name);
390355
if(is_file($zone_file_name.'.err')) unlink($zone_file_name.'.err');
391356
$app->log("Deleting BIND domain file: ".$zone_file_name, LOGLEVEL_DEBUG);
@@ -425,23 +390,12 @@ function slave_update($event_name, $data) {
425390

426391
//* Delete old domain file, if domain name has been changed
427392
if($data['old']['origin'] != $data['new']['origin']) {
428-
//TODO : change this when distribution information has been integrated into server record
429-
if (file_exists('/etc/gentoo-release')) {
430-
$filename = $dns_config['bind_zonefiles_dir'].'/sec/'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
431-
}
432-
else {
433-
$filename = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
434-
}
435-
393+
$filename = $dns_config['bind_zonefiles_dir'].'/' . $this->zone_file_prefix() . str_replace("/", "_", substr($data['old']['origin'], 0, -1));
436394
if(is_file($filename)) unset($filename);
437395
}
438396

439397
//* Ensure that the named slave directory is writable by the named user
440-
if (file_exists('/etc/gentoo-release')) {
441-
$slave_record_dir = $dns_config['bind_zonefiles_dir'].'/sec';
442-
} else {
443-
$slave_record_dir = $dns_config['bind_zonefiles_dir'].'/slave';
444-
}
398+
$slave_record_dir = $dns_config['bind_zonefiles_dir'].'/'.$this->slave_zone_file_prefix();
445399
if(!@is_dir($slave_record_dir)) mkdir($slave_record_dir, 0770);
446400
chown($slave_record_dir, $dns_config['bind_user']);
447401
chgrp($slave_record_dir, $dns_config['bind_group']);
@@ -463,14 +417,7 @@ function slave_delete($event_name, $data) {
463417
$this->write_named_conf($data, $dns_config);
464418

465419
//* Delete the domain file
466-
//TODO : change this when distribution information has been integrated into server record
467-
if (file_exists('/etc/gentoo-release')) {
468-
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/sec/'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
469-
}
470-
else {
471-
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/slave/sec.'.str_replace("/", "_", substr($data['old']['origin'], 0, -1));
472-
}
473-
420+
$zone_file_name = $dns_config['bind_zonefiles_dir'].'/' . $this->slave_zone_file_prefix() . str_replace("/", "_", substr($data['old']['origin'], 0, -1));
474421
if(is_file($zone_file_name)) unlink($zone_file_name);
475422
$app->log("Deleting BIND domain file for secondary zone: ".$zone_file_name, LOGLEVEL_DEBUG);
476423

@@ -527,16 +474,8 @@ function write_named_conf($data, $dns_config) {
527474

528475
//* Check if the current zone that triggered this function has at least one NS record
529476

530-
//TODO : change this when distribution information has been integrated into server record
531-
if (file_exists('/etc/gentoo-release')) {
532-
$pri_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/pri/';
533-
$sec_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/sec/';
534-
535-
}
536-
else {
537-
$pri_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/pri.';
538-
$sec_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/slave/sec.';
539-
}
477+
$pri_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/'.$this->zone_file_prefix();
478+
$sec_zonefiles_path = $dns_config['bind_zonefiles_dir'].'/'.$this->slave_zone_file_prefix();
540479

541480
//* Loop trough zones
542481
foreach($tmps as $tmp) {
@@ -603,6 +542,16 @@ function write_named_conf($data, $dns_config) {
603542
}
604543

605544

545+
function zone_file_prefix() {
546+
//TODO : change this when distribution information has been integrated into server record
547+
return (file_exists('/etc/gentoo-release')) ? 'pri/' : 'pri.';
548+
}
549+
function slave_zone_file_prefix() {
550+
//TODO : change this when distribution information has been integrated into server record
551+
return (file_exists('/etc/gentoo-release')) ? 'sec/' : 'slave/sec.';
552+
}
553+
554+
606555

607556

608557
} // end class

0 commit comments

Comments
 (0)