Alternc  latest
Alternc logiel libre pour l'hébergement
m_bind Class Reference

Manages BIND 9+ zone management templates in AlternC 3.5+. More...

Public Member Functions

 hook_updatedomains_dns_pre ()
 Hook launched before any action by updatedomains initialize the reload/reconfig flags used by POST @NOTE launched as ROOT. More...
 
 hook_updatedomains_dns_add ($dominfo)
 Hook launched for each ZONE for which we want a zone update (or create) update the zone, create it if necessary, and ask for reload or reconfig of bind9 depending on what happened @NOTE launched as ROOT. More...
 
 hook_updatedomains_dns_del ($dominfo)
 Hook launched for each ZONE for which we want a zone DELETE remove the zone and its file, and if any action happened, ask for bind RECONFIG at posttime @NOTE launched as ROOT. More...
 
 hook_updatedomains_dns_post ()
 Hook function launched at the very end of updatedomains here, we just reload OR reconfig (or both) bind9 depending on what happened before. More...
 
 read_zone ($domain)
 read a zone file for $domain, More...
 
 conf_from_db ($domain)
 Return the part of the conf we got from the sub_domaines table. More...
 

Public Attributes

 $shouldreload
 
 $shouldreconfig
 
 $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"
 
 $zone_file_directory = '/var/lib/alternc/bind/zones'
 

Detailed Description

Manages BIND 9+ zone management templates in AlternC 3.5+.

Definition at line 26 of file m_bind.php.

Member Function Documentation

◆ conf_from_db()

m_bind::conf_from_db (   $domain)

Return the part of the conf we got from the sub_domaines table.

Parameters
globalm_mysql $db
string$domain
Returns
string a zonefile excerpt

Definition at line 219 of file m_bind.php.

219  {
220  global $db;
221  $db->query("
222  SELECT
223  REPLACE(REPLACE(dt.entry,'%TARGET%',sd.valeur), '%SUB%', if(length(sd.sub)>0,sd.sub,'@')) AS ENTRY
224  FROM
225  sub_domaines sd,
226  domaines_type dt
227  WHERE
228  sd.type=dt.name
229  AND sd.enable IN ('ENABLE', 'ENABLED')
230  AND sd.web_action NOT IN ('DELETE')
231  AND sd.domaine = ?
232  ORDER BY ENTRY ;", array($domain));
233  $t="";
234  while ($db->next_record()) {
235  $t.= $db->f('ENTRY')."\n";
236  }
237  return $t;
238  }

Referenced by hook_updatedomains_dns_add().

◆ hook_updatedomains_dns_add()

m_bind::hook_updatedomains_dns_add (   $dominfo)

Hook launched for each ZONE for which we want a zone update (or create) update the zone, create it if necessary, and ask for reload or reconfig of bind9 depending on what happened @NOTE launched as ROOT.

Definition at line 56 of file m_bind.php.

56  {
57  global $L_FQDN,$L_NS1_HOSTNAME,$L_NS2_HOSTNAME,$L_DEFAULT_MX,$L_DEFAULT_SECONDARY_MX,$L_PUBLIC_IP,$L_PUBLIC_IPV6;
58 
59  $domain = $dominfo["domaine"];
60  $ttl = $dominfo["zonettl"];
61 
62  // does it already exist?
63  if (file_exists($this->zone_file_directory."/".$domain)) {
64  list($islocked,$serial,$more)=$this->read_zone($domain);
65  $serial++; // only increment serial for new zones
66  } else {
67  $more="";
68  $serial=date("Ymd")."00";
69  $islocked=false;
70  }
71  if ($islocked) return 1;
72 
73  // Prepare a new zonefile from a template
74  $zone = file_get_contents($this->ZONE_TEMPLATE);
75 
76  // add the SUBDOMAIN entries
77  $zone .= $this->conf_from_db($domain);
78 
79  // substitute ALTERNC & domain variables
80  $zone = strtr($zone, array(
81  "%%fqdn%%" => "$L_FQDN",
82  "%%ns1%%" => "$L_NS1_HOSTNAME",
83  "%%ns2%%" => "$L_NS2_HOSTNAME",
84  "%%DEFAULT_MX%%" => "$L_DEFAULT_MX",
85  "%%DEFAULT_SECONDARY_MX%%" => "$L_DEFAULT_SECONDARY_MX",
86  "@@fqdn@@" => "$L_FQDN",
87  "@@ns1@@" => "$L_NS1_HOSTNAME",
88  "@@ns2@@" => "$L_NS2_HOSTNAME",
89  "@@DEFAULT_MX@@" => "$L_DEFAULT_MX",
90  "@@DEFAULT_SECONDARY_MX@@" => "$L_DEFAULT_SECONDARY_MX",
91  "@@DOMAINE@@" => $domain,
92  "@@SERIAL@@" => $serial,
93  "@@PUBLIC_IP@@" => "$L_PUBLIC_IP",
94  "@@PUBLIC_IPV6@@" => "$L_PUBLIC_IPV6",
95  "@@ZONETTL@@" => $ttl,
96  ));
97 
98  // add the "END ALTERNC CONF line";
99  $zone .= ";;; END ALTERNC AUTOGENERATE CONFIGURATION\n";
100 
101  // add the manually entered info:
102  $zone .= $more;
103  file_put_contents($this->zone_file_directory."/".$domain,$zone);
104 
105  // add the line into bind9 conf:
106  if (add_line_to_file(
107  $this->NAMED_CONF,
108  trim(strtr(
109  file_get_contents($this->NAMED_TEMPLATE),
110  array(
111  "@@DOMAIN@@" => $domain,
112  "@@ZONE_FILE@@" => $this->zone_file_directory."/".$domain
113  )
114  )))
115  ) {
116  $this->shouldreconfig=true;
117  } else {
118  $this->shouldreload=true;
119  }
120  return 0;
121  }
read_zone($domain)
read a zone file for $domain,
Definition: m_bind.php:189
conf_from_db($domain)
Return the part of the conf we got from the sub_domaines table.
Definition: m_bind.php:219

References conf_from_db(), and read_zone().

◆ hook_updatedomains_dns_del()

m_bind::hook_updatedomains_dns_del (   $dominfo)

Hook launched for each ZONE for which we want a zone DELETE remove the zone and its file, and if any action happened, ask for bind RECONFIG at posttime @NOTE launched as ROOT.

Definition at line 131 of file m_bind.php.

131  {
132  $domain = $dominfo["domaine"];
133  if (del_line_from_file(
134  $this->NAMED_CONF,
135  trim(strtr(
136  file_get_contents($this->NAMED_TEMPLATE),
137  array(
138  "@@DOMAIN@@" => $domain,
139  "@@ZONE_FILE@@" => $this->zone_file_directory."/".$domain
140  )
141  )))
142  ) {
143  $this->shouldreconfig=true;
144  } else {
145  return 0;
146  }
147  @unlink($this->zone_file_directory."/".$domain);
148  return 0;
149  }

◆ hook_updatedomains_dns_post()

m_bind::hook_updatedomains_dns_post ( )

Hook function launched at the very end of updatedomains here, we just reload OR reconfig (or both) bind9 depending on what happened before.

@NOTE launched as ROOT

Definition at line 159 of file m_bind.php.

159  {
160  global $msg;
161  if ($this->shouldreload) {
162  $ret=0;
163  exec($this->RNDC." reload 2>&1",$out,$ret);
164  if ($ret!=0) {
165  $msg->raise("ERROR","bind","Error while reloading bind, error code is $ret\n".implode("\n",$out));
166  } else {
167  $msg->raise("INFO","bind","Bind reloaded");
168  }
169  }
170  if ($this->shouldreconfig) {
171  $ret=0;
172  exec($this->RNDC." reconfig 2>&1",$out,$ret);
173  if ($ret!=0) {
174  $msg->raise("ERROR","bind","Error while reconfiguring bind, error code is $ret\n".implode("\n",$out));
175  } else {
176  $msg->raise("INFO","bind","Bind reconfigured");
177  }
178  }
179  }

◆ hook_updatedomains_dns_pre()

m_bind::hook_updatedomains_dns_pre ( )

Hook launched before any action by updatedomains initialize the reload/reconfig flags used by POST @NOTE launched as ROOT.

Definition at line 43 of file m_bind.php.

43  {
44  $this->shouldreload=false;
45  $this->shouldreconfig=false;
46  }

◆ read_zone()

m_bind::read_zone (   $domain)

read a zone file for $domain,

Parameters
$domainstring the domain name
Returns
array with 3 informations: is the domain locked? (boolean), what's the current serial (integer), the data after alternc conf (string of lines)

Definition at line 189 of file m_bind.php.

189  {
190  $f=fopen($this->zone_file_directory."/".$domain,"rb");
191  $islocked=false;
192  $more="";
193  $serial=date("Ymd")."00";
194  while ($s=fgets($f,4096)) {
195  if (preg_match("#\;\s*LOCKED:YES#i",$s)) {
196  $islocked=true;
197  }
198  if (preg_match("/\s*(\d{10})\s+\;\sserial\s?/", $s,$mat)) {
199  $serial=$mat[1];
200  }
201  if (preg_match('/\;\s*END\sALTERNC\sAUTOGENERATE\sCONFIGURATION(.*)/s', $s)) {
202  break;
203  }
204  }
205  while ($s=fgets($f,4096)) {
206  $more.=$s;
207  }
208  return array($islocked,$serial,$more);
209  }

Referenced by hook_updatedomains_dns_add().

Member Data Documentation

◆ $NAMED_CONF

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

Definition at line 33 of file m_bind.php.

◆ $NAMED_TEMPLATE

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

Definition at line 32 of file m_bind.php.

◆ $RNDC

m_bind::$RNDC ="/usr/sbin/rndc"

Definition at line 34 of file m_bind.php.

◆ $shouldreconfig

m_bind::$shouldreconfig

Definition at line 29 of file m_bind.php.

◆ $shouldreload

m_bind::$shouldreload

Definition at line 28 of file m_bind.php.

◆ $zone_file_directory

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

Definition at line 36 of file m_bind.php.

◆ $ZONE_TEMPLATE

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

Definition at line 31 of file m_bind.php.


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