Alternc  latest
Alternc logiel libre pour l'hébergement
m_bind.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  ----------------------------------------------------------------------
5  LICENSE
6 
7  This program is free software; you can redistribute it and/or
8  modify it under the terms of the GNU General Public License (GPL)
9  as published by the Free Software Foundation; either version 2
10  of the License, or (at your option) any later version.
11 
12  This program is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  To read the license please visit http://www.gnu.org/copyleft/gpl.html
18  ----------------------------------------------------------------------
19 */
20 
21 /**
22  * Manages BIND 9+ zone management templates in AlternC 3.5+
23  *
24  * @copyright AlternC-Team 2000-2018 https://alternc.com/
25  */
26 class m_bind {
27 
30 
31  var $ZONE_TEMPLATE ="/etc/alternc/templates/bind/templates/zone.template";
32  var $NAMED_TEMPLATE ="/etc/alternc/templates/bind/templates/named.template";
33  var $NAMED_CONF ="/var/lib/alternc/bind/automatic.conf";
34  var $RNDC ="/usr/sbin/rndc";
35 
36  var $zone_file_directory = '/var/lib/alternc/bind/zones';
37 
38  // ------------------------------------------------------------
39  /** Hook launched before any action by updatedomains
40  * initialize the reload/reconfig flags used by POST
41  * @NOTE launched as ROOT
42  */
44  $this->shouldreload=false;
45  $this->shouldreconfig=false;
46  }
47 
48 
49  // ------------------------------------------------------------
50  /**
51  * Hook launched for each ZONE for which we want a zone update (or create)
52  * update the zone, create it if necessary,
53  * and ask for reload or reconfig of bind9 depending on what happened
54  * @NOTE launched as ROOT
55  */
56  function hook_updatedomains_dns_add($dominfo) {
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  }
122 
123 
124  // ------------------------------------------------------------
125  /**
126  * Hook launched for each ZONE for which we want a zone DELETE
127  * remove the zone and its file,
128  * and if any action happened, ask for bind RECONFIG at posttime
129  * @NOTE launched as ROOT
130  */
131  function hook_updatedomains_dns_del($dominfo) {
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  }
150 
151 
152  // ------------------------------------------------------------
153  /**
154  * Hook function launched at the very end of updatedomains
155  * here, we just reload OR reconfig (or both) bind9 depending
156  * on what happened before.
157  * @NOTE launched as ROOT
158  */
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  }
180 
181 
182  // ------------------------------------------------------------
183  /**
184  * read a zone file for $domain,
185  * @param $domain string the domain name
186  * @return array with 3 informations:
187  * is the domain locked? (boolean), what's the current serial (integer), the data after alternc conf (string of lines)
188  */
189  function read_zone($domain) {
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  }
210 
211 
212  // ------------------------------------------------------------
213  /**
214  * Return the part of the conf we got from the sub_domaines table
215  * @global m_mysql $db
216  * @param string $domain
217  * @return string a zonefile excerpt
218  */
219  function conf_from_db($domain) {
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  }
239 
240 
241 } // m_bind
242 
Manages BIND 9+ zone management templates in AlternC 3.5+.
Definition: m_bind.php:26
hook_updatedomains_dns_pre()
Hook launched before any action by updatedomains initialize the reload/reconfig flags used by POST @N...
Definition: m_bind.php:43
read_zone($domain)
read a zone file for $domain,
Definition: m_bind.php:189
$zone_file_directory
Definition: m_bind.php:36
hook_updatedomains_dns_del($dominfo)
Hook launched for each ZONE for which we want a zone DELETE remove the zone and its file,...
Definition: m_bind.php:131
$NAMED_TEMPLATE
Definition: m_bind.php:32
hook_updatedomains_dns_add($dominfo)
Hook launched for each ZONE for which we want a zone update (or create) update the zone,...
Definition: m_bind.php:56
$NAMED_CONF
Definition: m_bind.php:33
conf_from_db($domain)
Return the part of the conf we got from the sub_domaines table.
Definition: m_bind.php:219
$shouldreload
Definition: m_bind.php:28
$shouldreconfig
Definition: m_bind.php:29
$ZONE_TEMPLATE
Definition: m_bind.php:31
hook_updatedomains_dns_post()
Hook function launched at the very end of updatedomains here, we just reload OR reconfig (or both) bi...
Definition: m_bind.php:159
$RNDC
Definition: m_bind.php:34