Alternc  3.2
Alternc logiel libre pour l'h├ębergement
 All Data Structures Namespaces Files Functions Variables Pages
system_bind Class Reference

Public Member Functions

 system_bind ()
 conf_from_db ($domain=false)
 get_zone_file_uri ($domain)
 get_zone_file ($domain)
 get_serial ($domain)
 get_persistent ($domain)
 get_zone_header ($domain)
 get_domain_summary ($domain=false)
 dkim_delete ($domain)
 dkim_generate_key ($domain)
 dkim_refresh_list ()
 dkim_entry ($domain)
 mail_autoconfig_entry ($domain)
 get_zone ($domain)
 reload_zone ($domain)
 is_locked ($domain)
 save_zone ($domain)
 delete_zone ($domain)
 reload_named ()
 regenerate_conf ($all=false)

Data Fields

 $ZONE_TEMPLATE = "/etc/alternc/templates/bind/templates/zone.template"
 $NAMED_TEMPLATE = "/etc/alternc/templates/bind/templates/named.template"
 $NAMED_CONF = "/var/lib/alternc/bind/automatic.conf"
 $RNDC = "/usr/sbin/rndc"
 $dkim_trusted_host_file = "/etc/opendkim/TrustedHosts"
 $dkim_keytable_file = "/etc/opendkim/KeyTable"
 $dkim_signingtable_file = "/etc/opendkim/SigningTable"
 $cache_conf_db = array()
 $cache_get_persistent = array()
 $cache_zone_file = array()
 $cache_domain_summary = array()
 $zone_file_directory = '/var/lib/alternc/bind/zones/'

Private Member Functions

 dummy_for_translation ()

Detailed Description

Definition at line 4 of file class_system_bind.php.

Member Function Documentation

conf_from_db (   $domain = false)

Definition at line 25 of file class_system_bind.php.

References $cache_conf_db, $db, $domain, $t, and isset.

Referenced by get_zone(), and mail_autoconfig_entry().

{
global $db;
// Use cache, fill cache if empty
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
}
delete_zone (   $domain)

Definition at line 310 of file class_system_bind.php.

References $domain, dkim_delete(), and get_zone_file_uri().

Referenced by regenerate_conf().

{
$file=$this->get_zone_file_uri($domain);
if (file_exists($file)) {
unlink($file);
}
return;
}
dkim_delete (   $domain)

Definition at line 120 of file class_system_bind.php.

Referenced by delete_zone().

{
$target_dir = "/etc/opendkim/keys/$domain";
if (file_exists($target_dir)) {
@unlink("$target_dir/alternc_private");
@unlink("$target_dir/alternc.txt");
@rmdir($target_dir);
}
return true;
}
dkim_entry (   $domain)

Definition at line 209 of file class_system_bind.php.

References $domain, dkim_generate_key(), and get_domain_summary().

Referenced by get_zone().

{
$keyfile="/etc/opendkim/keys/$domain/alternc.txt";
if (! file_exists($keyfile) && $this->get_domain_summary($domain)['gesmx'] ) {
}
return @file_get_contents($keyfile);
}
dkim_generate_key (   $domain)

Definition at line 131 of file class_system_bind.php.

References $domain, and get_domain_summary().

Referenced by dkim_entry().

{
// Stop here if we do not manage the mail
if ( ! $this->get_domain_summary($domain)['gesmx'] ) return;
$target_dir = "/etc/opendkim/keys/$domain";
if (file_exists($target_dir.'/alternc.txt')) return; // Do not generate if exist
if (! is_dir($target_dir)) mkdir($target_dir); // create dir
// Generate the key
$old_dir=getcwd();
chdir($target_dir);
exec('opendkim-genkey -r -d "'.escapeshellarg($domain).'" -s "alternc" ');
chdir($old_dir);
// opendkim must be owner of the key
chown("$target_dir/alternc.private", 'opendkim');
chgrp("$target_dir/alternc.private", 'opendkim');
return true; // FIXME handle error
}
dkim_refresh_list ( )

Definition at line 155 of file class_system_bind.php.

References $domain, get_domain_summary(), and isset.

Referenced by regenerate_conf().

{ // so ugly... but there is only 1 pass, not 3. Still ugly.
$trusted_host_new = "# WARNING: this file is auto generated by AlternC.\n# Add your changes after the last line\n";
$keytable_new = "# WARNING: this file is auto generated by AlternC.\n# Add your changes after the last line\n";
$signingtable_new = "# WARNING: this file is auto generated by AlternC.\n# Add your changes after the last line\n";
# Generate automatic entry
foreach ($this->get_domain_summary() as $domain => $ds ) {
// Skip if delete in progress, or if we do not manage dns or mail
if ( ! $ds['gesdns'] || ! $ds['gesmx'] || strtoupper($ds['dns_action']) == 'DELETE' ) continue;
// Skip if there is no key generated
if (! file_exists("/etc/opendkim/keys/$domain/alternc.txt")) continue;
// Modif the files.
$trusted_host_new.="$domain\n";
$keytable_new .="alternc._domainkey.$domain $domain:alternc:/etc/opendkim/keys/$domain/alternc.private\n";
$signingtable_new.="$domain alternc._domainkey.$domain\n";
}
$trusted_host_new.="# END AUTOMATIC FILE. ADD YOUR CHANGES AFTER THIS LINE";
$keytable_new .="# END AUTOMATIC FILE. ADD YOUR CHANGES AFTER THIS LINE";
$signingtable_new.="# END AUTOMATIC FILE. ADD YOUR CHANGES AFTER THIS LINE";
# Get old files
$trusted_host_old=@file_get_contents($this->dkim_trusted_host_file);
$keytable_old =@file_get_contents($this->dkim_keytable_file);
$signingtable_old=@file_get_contents($this->dkim_signingtable_file);
# Keep manuel entry
preg_match_all('/\#\s*END\ AUTOMATIC\ FILE\.\ ADD\ YOUR\ CHANGES\ AFTER\ THIS\ LINE(.*)/s', $trusted_host_old, $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
$trusted_host_new.=$output_array[1][0];
}
preg_match_all('/\#\s*END\ AUTOMATIC\ FILE\.\ ADD\ YOUR\ CHANGES\ AFTER\ THIS\ LINE(.*)/s', $keytable_old, $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
$keytable_new.=$output_array[1][0];
}
preg_match_all('/\#\s*END\ AUTOMATIC\ FILE\.\ ADD\ YOUR\ CHANGES\ AFTER\ THIS\ LINE(.*)/s', $signingtable_old, $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
$signingtable_new.=$output_array[1][0];
}
// Save if there are some diff
if ( $trusted_host_new != $trusted_host_old ) {
file_put_contents($this->dkim_trusted_host_file, $trusted_host_new);
}
if ( $keytable_new != $keytable_old ) {
file_put_contents($this->dkim_keytable_file, $keytable_new);
}
if ( $signingtable_new != $signingtable_old ) {
file_put_contents($this->dkim_signingtable_file, $signingtable_new);
}
}
dummy_for_translation ( )
private

Definition at line 371 of file class_system_bind.php.

{
_("The zone file of this domain is locked. Contact your administrator.");
}
get_domain_summary (   $domain = false)

Definition at line 108 of file class_system_bind.php.

References $cache_domain_summary, $dom, and $domain.

Referenced by dkim_entry(), dkim_generate_key(), dkim_refresh_list(), get_zone(), mail_autoconfig_entry(), regenerate_conf(), and reload_named().

{
global $dom;
// Use cache if is filled, if not, fill it
if (empty($this->cache_domain_summary)) {
$this->cache_domain_summary = $dom->get_domain_all_summary();
}
if ($domain) return $this->cache_domain_summary[$domain];
}
get_persistent (   $domain)

Definition at line 92 of file class_system_bind.php.

References $domain, get_zone_file(), and isset.

Referenced by get_zone(), and mail_autoconfig_entry().

{
if ( ! isset($this->cache_get_persistent[$domain] )) {
preg_match_all('/\;\s*END\sALTERNC\sAUTOGENERATE\sCONFIGURATION(.*)/s', $this->get_zone_file($domain), $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
$this->cache_get_persistent[$domain] = $output_array[1][0];
} else {
$this->cache_get_persistent[$domain] = false;
}
} // isset
return $this->cache_get_persistent[$domain];
}
get_serial (   $domain)

Definition at line 72 of file class_system_bind.php.

References $domain, get_zone_file(), and isset.

Referenced by get_zone().

{
// 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 between newly calculated, and old one incremented
return max(array($calc,$old)) + 1 ;
}
get_zone (   $domain)

Definition at line 239 of file class_system_bind.php.

References $domain, conf_from_db(), dkim_entry(), get_domain_summary(), get_persistent(), get_serial(), get_zone_header(), and mail_autoconfig_entry().

Referenced by save_zone().

{
global $L_FQDN, $L_NS1_HOSTNAME, $L_NS2_HOSTNAME, $L_DEFAULT_MX, $L_DEFAULT_SECONDARY_MX, $L_PUBLIC_IP;
$zone =$this->get_zone_header($domain);
$zone.=implode("\n",$this->conf_from_db($domain));
$zone.="\n;;;HOOKED ENTRY\n";
$zone.= $this->dkim_entry($domain);
$zone.= $this->mail_autoconfig_entry($domain);
$zone.="\n;;;END ALTERNC AUTOGENERATE CONFIGURATION";
$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;
}
get_zone_file (   $domain)

Definition at line 60 of file class_system_bind.php.

References $domain, get_zone_file_uri(), and isset.

Referenced by get_persistent(), get_serial(), and is_locked().

{
// Use cache, fill cache if empty
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] ;
}
get_zone_file_uri (   $domain)

Definition at line 56 of file class_system_bind.php.

Referenced by delete_zone(), get_zone_file(), reload_named(), and save_zone().

{
return $this->zone_file_directory.$domain;
}
get_zone_header (   $domain)

Definition at line 104 of file class_system_bind.php.

Referenced by get_zone().

{
return file_get_contents($this->ZONE_TEMPLATE);
}
is_locked (   $domain)

Definition at line 281 of file class_system_bind.php.

References $domain, get_zone_file(), and isset.

Referenced by save_zone().

{
preg_match_all("/(\;\s*LOCKED:YES)/i", $this->get_zone_file($domain), $output_array);
if (isset($output_array[1][0]) && !empty($output_array[1][0])) {
return true;
}
return false;
}
mail_autoconfig_entry (   $domain)

Definition at line 219 of file class_system_bind.php.

References $domain, conf_from_db(), get_domain_summary(), and get_persistent().

Referenced by get_zone().

{
$zone= implode("\n",$this->conf_from_db($domain))."\n".$this->get_persistent($domain);
$entry='';
if ( $this->get_domain_summary($domain)['gesmx'] ) {
// If we manage the mail
// Check if there is no the same entry (defined or manual)
// can be toto IN A or toto.fqdn.tld. IN A
if (! preg_match("/autoconfig(\s|\.".str_replace('.','\.',$domain)."\.)/", $zone )) {
$entry.="autoconfig IN CNAME %%fqdn%%.\n";
}
if (! preg_match("/autodiscover(\s|\.".str_replace('.','\.',$domain)."\.)/", $zone )) {
$entry.="autodiscover IN CNAME %%fqdn%%.\n";
}
} // if gesmx
return $entry;
}
regenerate_conf (   $all = false)

Definition at line 345 of file class_system_bind.php.

References $domain, $hooks, delete_zone(), dkim_refresh_list(), get_domain_summary(), reload_named(), reload_zone(), and save_zone().

{
global $hooks;
foreach ($this->get_domain_summary() as $domain => $ds ) {
if ( ! $ds['gesdns'] && strtoupper($ds['dns_action']) == 'OK' ) continue; // Skip if we do not manage DNS and is up-to-date for this domain
if ( (strtoupper($ds['dns_action']) == 'DELETE' ) ||
(strtoupper($ds['dns_action']) == 'UPDATE' && $ds['gesdns']==false ) // in case we update the zone to disable DNS management
) {
continue;
}
if ( ( $all || strtoupper($ds['dns_action']) == 'UPDATE' ) && $ds['gesdns'] ) {
$this->save_zone($domain);
$hooks->invoke_scripts("/usr/lib/alternc/reload.d", array('dns_reload_zone', $domain) );
}
} // end foreach domain
$this->reload_named();
return;
}
reload_named ( )

Definition at line 319 of file class_system_bind.php.

References $domain, $hooks, get_domain_summary(), and get_zone_file_uri().

Referenced by regenerate_conf().

{
global $hooks;
// Generate the new conf file
$new_named_conf="// DO NOT EDIT\n// This file is generated by Alternc.\n// Every changes you'll make will be overwrited.\n";
$tpl=file_get_contents($this->NAMED_TEMPLATE);
foreach ($this->get_domain_summary() as $domain => $ds ) {
if ( ! $ds['gesdns'] || strtoupper($ds['dns_action']) == 'DELETE' ) continue;
$new_named_conf.=strtr($tpl, array("@@DOMAINE@@"=>$domain, "@@ZONE_FILE@@"=>$this->get_zone_file_uri($domain)));
}
// Get the actual conf file
$old_named_conf = @file_get_contents($this->NAMED_CONF);
// Apply new configuration only if there are some differences
if ($old_named_conf != $new_named_conf ) {
file_put_contents($this->NAMED_CONF,$new_named_conf);
chown($this->NAMED_CONF, 'bind');
chmod($this->NAMED_CONF, 0640);
exec($this->RNDC." reconfig");
$hooks->invoke_scripts("/usr/lib/alternc/reload.d", array('dns_reconfig') );
}
return true;
}
reload_zone (   $domain)

Definition at line 273 of file class_system_bind.php.

References $domain.

Referenced by regenerate_conf().

{
exec($this->RNDC." reload ".escapeshellarg($domain), $output, $return_value);
if ($return_value != 0 ) {
echo "ERROR: Reload zone failed for zone $domain\n";
}
}
save_zone (   $domain)

Definition at line 289 of file class_system_bind.php.

References $db, $dom, $domain, get_zone(), get_zone_file_uri(), and is_locked().

Referenced by regenerate_conf().

{
global $db, $dom;
// Do not save if the zone is LOCKED
if ( $this->is_locked($domain)) {
$dom->set_dns_result($domain, "The zone file of this domain is locked. Contact your administrator."); // If edit, change dummy_for_translation
$dom->set_dns_action($domain, 'OK');
return false;
}
// Save file, and apply chmod/chown
$file=$this->get_zone_file_uri($domain);
file_put_contents($file, $this->get_zone($domain));
chown($file, 'bind');
chmod($file, 0640);
$dom->set_dns_action($domain, 'OK');
return true; // fixme add tests
}

Definition at line 20 of file class_system_bind.php.

{
// Constructeur
}

Field Documentation

$cache_conf_db = array()

Definition at line 14 of file class_system_bind.php.

Referenced by conf_from_db().

$cache_domain_summary = array()

Definition at line 17 of file class_system_bind.php.

Referenced by get_domain_summary().

$cache_get_persistent = array()

Definition at line 15 of file class_system_bind.php.

$cache_zone_file = array()

Definition at line 16 of file class_system_bind.php.

$dkim_keytable_file = "/etc/opendkim/KeyTable"

Definition at line 11 of file class_system_bind.php.

$dkim_signingtable_file = "/etc/opendkim/SigningTable"

Definition at line 12 of file class_system_bind.php.

$dkim_trusted_host_file = "/etc/opendkim/TrustedHosts"

Definition at line 10 of file class_system_bind.php.

$NAMED_CONF = "/var/lib/alternc/bind/automatic.conf"

Definition at line 7 of file class_system_bind.php.

$NAMED_TEMPLATE = "/etc/alternc/templates/bind/templates/named.template"

Definition at line 6 of file class_system_bind.php.

$RNDC = "/usr/sbin/rndc"

Definition at line 8 of file class_system_bind.php.

$zone_file_directory = '/var/lib/alternc/bind/zones/'

Definition at line 18 of file class_system_bind.php.

$ZONE_TEMPLATE = "/etc/alternc/templates/bind/templates/zone.template"

Definition at line 5 of file class_system_bind.php.


The documentation for this class was generated from the following file: