Premier jet de la regénération bind en php

This commit is contained in:
Alan Garcia 2014-01-30 17:36:16 +00:00
parent 0f4da7e62a
commit fe9612061e
3 changed files with 208 additions and 0 deletions

1
.gitattributes vendored
View File

@ -646,6 +646,7 @@ src/functions.sh -text
src/functions_dns.sh -text
src/functions_hosting.sh -text
src/generate_apache_conf.php -text
src/generate_bind_conf.php -text
src/inotify_do_actions.sh -text
src/inotify_update_domains.sh -text
src/lxc_stopexpired.php -text

View File

@ -1598,6 +1598,21 @@ class m_dom {
return $res;
}
function get_domain_all_summary() {
global $db,$err;
$res=array();
$db->query("SELECT domaine, gesdns, gesmx, dns_action, zonettl FROM domaines ORDER BY domaine");
while ($db->next_record()) {
$res[$db->f("domaine")] = array(
"gesdns" => $db->f("gesdns"),
"gesmx" => $db->f("gesmx"),
"dns_action" => $db->f("dns_action"),
"zonettl" => $db->f("zonettl"),
);
}
return $res;
}
/* ----------------------------------------------------------------- */
/** Returns the name of a domain for the current user, from it's domain_id

192
src/generate_bind_conf.php Executable file
View File

@ -0,0 +1,192 @@
#!/usr/bin/php -q
<?php
/**
*
* Generate Bind configuration for AlternC
*
* To force generation, /launch/generate_bind_conf.php force
*
*
**/
require_once("/usr/share/alternc/panel/class/config_nochk.php");
ini_set("display_errors", 1);
class m_bind_regenerate {
var $ZONE_TEMPLATE ="/etc/alternc/templates/bind/templates/zone.template";
var $NAMED_TEMPLATE ="/etc/alternc/templates/bind/templates/named.template";
var $NAMED_CONF ="/var/lib/alternc/bind/automatic.conf";
var $RNDC ="/usr/sbin/rndc";
var $cache_conf_db = array();
var $cache_zone_file = array();
var $cache_domain_summary = array();
var $zone_file_directory = '/var/lib/alternc/bind/zones/';
function m_bind_regenerate() {
// Constructeur
}
// Return the part of the conf we got from the database
function conf_from_db($domain=false) {
global $db;
if (empty($this->cache_conf_db)) {
$db->query("
select
sd.domaine,
replace(replace(dt.entry,'%TARGET%',sd.valeur), '%SUB%', if(length(sd.sub)>0,sd.sub,'@')) as entry
from
sub_domaines sd,
domaines_type dt
where
sd.type=dt.name
and sd.enable in ('ENABLE', 'ENABLED')
order by entry ;");
while ($db->next_record()) {
$t[$db->f('domaine')][] = $db->f('entry');
}
$this->cache_conf_db = $t;
}
if ($domain) {
if (isset($this->cache_conf_db[$domain])) {
return $this->cache_conf_db[$domain];
} else {
return array();
}
} // if domain
return $this->cache_conf_db;
}
function get_zone_file_uri($domain) {
return $this->zone_file_directory.$domain;
}
function get_zone_file($domain) {
if (!isset($this->cache_zone_file[$domain]) ) {
if (file_exists($this->get_zone_file_uri($domain))) {
$this->cache_zone_file[$domain] = @file_get_contents($this->get_zone_file_uri($domain));
} else {
$this->cache_zone_file[$domain] = false;
}
}
return $this->cache_zone_file[$domain] ;
}
function get_serial($domain) {
// Return the next serial the domain must have.
// Choose between a generated and an incremented.
// Calculated :
$calc = date('Ymd').'00'."\n";
// Old one :
$old=$calc; // default value
$file = $this->get_zone_file($domain);
preg_match_all("/\s*(\d{10})\s+\;\sserial\s?/", $file, $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
$old = $output_array[1][0];
}
return max(array($calc,$old)) + 1 ;
}
// Return lines that are after ;;;END ALTERNC AUTOGENERATE CONFIGURATION
function get_persistent($domain) {
preg_match_all('/\;\sEND\sALTERNC\sAUTOGENERATE\sCONFIGURATION(.*)/s', $this->get_zone_file($domain), $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
return $output_array[1][0];
}
return;
}
function get_zone_header($domain) {
return file_get_contents($this->ZONE_TEMPLATE);
}
function get_domain_summary($domain=false) {
global $dom;
if (empty($this->cache_domain_summary)) {
$this->cache_domain_summary = $dom->get_domain_all_summary();
}
if ($domain) return $this->cache_domain_summary[$domain];
else return $this->cache_domain_summary;
}
// Return a fully generated zone
function get_zone($domain) {
global $L_FQDN, $L_NS1_HOSTNAME, $L_NS2_HOSTNAME, $L_DEFAULT_MX, $L_DEFAULT_SECONDARY_MX, $L_PUBLIC_IP;
$zone='';
$zone.=$this->get_zone_header($domain);
$zone.=implode("\n",$this->conf_from_db($domain));
$zone.="\n;;;HOOKED ENTRY\n";
// FIXME ADD HOOKS opendkim, autoconfig toussa....
$zone.="\n;;;END ALTERNC AUTOGENERATE CONFIGURATION\n";
$zone.=$this->get_persistent($domain);
// FIXME check those vars
$zone = strtr($zone, array(
"%%fqdn%%"=>"$L_FQDN",
"%%ns1%%"=>"$L_NS1_HOSTNAME",
"%%ns2%%"=>"$L_NS2_HOSTNAME",
"%%DEFAULT_MX%%"=>"$L_DEFAULT_MX",
"%%DEFAULT_SECONDARY_MX%%"=>"$L_DEFAULT_SECONDARY_MX",
"@@fqdn@@"=>"$L_FQDN",
"@@ns1@@"=>"$L_NS1_HOSTNAME",
"@@ns2@@"=>"$L_NS2_HOSTNAME",
"@@DEFAULT_MX@@"=>"$L_DEFAULT_MX",
"@@DEFAULT_SECONDARY_MX@@"=>"$L_DEFAULT_SECONDARY_MX",
"@@DOMAINE@@"=>"$domain",
"@@SERIAL@@"=>$this->get_serial($domain),
"@@PUBLIC_IP@@"=>"$L_PUBLIC_IP",
"@@ZONETTL@@"=> $this->get_domain_summary($domain)['zonettl'],
));
return $zone;
}
function reload_zone($domain) {
exec($this->RNDC." reload ".escapeshellarg($domain));
}
function save_zone($domain) {
//FIXME check lock
return file_put_contents($this->get_zone_file_uri($domain), $this->get_zone($domain));
}
function reload_named() {
$new_named_conf="// DO NOT EDIT\n// This file is generated by Alternc.\n// Every changes you'll make will be overwrited.\n";
foreach ($this->get_domain_summary() as $domain => $ds ) {
if ( ! $ds['gesdns'] ) continue;
$new_named_conf.=strtr(file_get_contents($this->NAMED_TEMPLATE), array("@@DOMAINE@@"=>$domain, "@@ZONE_FILE@@"=>$this->get_zone_file_uri($domain)));
}
$old_named_conf = @file_get_contents($this->NAMED_CONF);
if ($old_named_conf != $new_named_conf ) {
file_put_contents($this->NAMED_CONF,$new_named_conf);
exec($this->RNDC." reconfig");
}
}
function regenerate_conf($all=false) {
foreach ($this->get_domain_summary() as $domain => $ds ) {
if ( ! $ds['gesdns'] ) continue;
if ($all || $ds['dns_action'] == 'UPDATE' ) {
$this->save_zone($domain);
$this->reload_zone($domain);
}
}
$this->reload_named();
}
} // class
$bind = new m_bind_regenerate();
#echo $bind->get_zone('coin.fr');
echo $bind->regenerate_conf(true);