From fe9612061e31ab0cda85e00117a27465dd675c01 Mon Sep 17 00:00:00 2001 From: Alan Garcia Date: Thu, 30 Jan 2014 17:36:16 +0000 Subject: [PATCH] =?UTF-8?q?Premier=20jet=20de=20la=20reg=C3=A9n=C3=A9ratio?= =?UTF-8?q?n=20bind=20en=20php?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitattributes | 1 + bureau/class/m_dom.php | 15 +++ src/generate_bind_conf.php | 192 +++++++++++++++++++++++++++++++++++++ 3 files changed, 208 insertions(+) create mode 100755 src/generate_bind_conf.php diff --git a/.gitattributes b/.gitattributes index 9b976ee2..87a4c065 100644 --- a/.gitattributes +++ b/.gitattributes @@ -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 diff --git a/bureau/class/m_dom.php b/bureau/class/m_dom.php index 4d495d6f..04ee2909 100644 --- a/bureau/class/m_dom.php +++ b/bureau/class/m_dom.php @@ -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 diff --git a/src/generate_bind_conf.php b/src/generate_bind_conf.php new file mode 100755 index 00000000..acee7d83 --- /dev/null +++ b/src/generate_bind_conf.php @@ -0,0 +1,192 @@ +#!/usr/bin/php -q +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); + +