Project

General

Profile

Download (89.5 KB) Statistics
| Branch: | Tag: | Revision:
1 17f6eafa Scott Ullrich
<?php
2 5b237745 Scott Ullrich
/*
3 8acd654a Renato Botelho
 * services.inc
4
 *
5
 * part of pfSense (https://www.pfsense.org)
6 2a2396a6 Renato Botelho
 * Copyright (c) 2004-2016 Rubicon Communications, LLC (Netgate)
7 8acd654a Renato Botelho
 * All rights reserved.
8
 *
9
 * originally part of m0n0wall (http://m0n0.ch/wall)
10 aaec5634 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
11 8acd654a Renato Botelho
 * All rights reserved.
12
 *
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 *    this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 *    notice, this list of conditions and the following disclaimer in
21
 *    the documentation and/or other materials provided with the
22
 *    distribution.
23
 *
24
 * 3. All advertising materials mentioning features or use of this software
25
 *    must display the following acknowledgment:
26
 *    "This product includes software developed by the pfSense Project
27
 *    for use in the pfSense® software distribution. (http://www.pfsense.org/).
28
 *
29
 * 4. The names "pfSense" and "pfSense Project" must not be used to
30
 *    endorse or promote products derived from this software without
31
 *    prior written permission. For written permission, please contact
32
 *    coreteam@pfsense.org.
33
 *
34
 * 5. Products derived from this software may not be called "pfSense"
35
 *    nor may "pfSense" appear in their names without prior written
36
 *    permission of the Electric Sheep Fencing, LLC.
37
 *
38
 * 6. Redistributions of any form whatsoever must retain the following
39
 *    acknowledgment:
40
 *
41
 * "This product includes software developed by the pfSense Project
42
 * for use in the pfSense software distribution (http://www.pfsense.org/).
43
 *
44
 * THIS SOFTWARE IS PROVIDED BY THE pfSense PROJECT ``AS IS'' AND ANY
45
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
46
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
47
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE pfSense PROJECT OR
48
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
49
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
50
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
51
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
52
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
53
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
54
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
55
 * OF THE POSSIBILITY OF SUCH DAMAGE.
56
 */
57 5b237745 Scott Ullrich
58 f56fb70c xygrec
define('DYNDNS_PROVIDER_VALUES', 'all-inkl citynetwork cloudflare cloudflare-v6 custom custom-v6 dnsexit dnsimple dnsmadeeasy dnsomatic duiadns duiadns-v6 dyndns dyndns-custom dyndns-static dyns easydns eurodns freedns freedns-v6 glesys googledomains gratisdns he-net he-net-v6 he-net-tunnelbroker hover loopia namecheap noip noip-free ods opendns ovh-dynhost route53 selfhost spdyn spdyn-v6 zoneedit');
59
define('DYNDNS_PROVIDER_DESCRIPTIONS', 'All-Inkl.com,City Network,CloudFlare,CloudFlare (v6),Custom,Custom (v6),DNSexit,DNSimple,DNS Made Easy,DNS-O-Matic,DuiaDns.net,DuiaDns.net (v6),DynDNS (dynamic),DynDNS (custom),DynDNS (static),DyNS,easyDNS,Euro Dns,freeDNS,freeDNS (v6),GleSYS,Google Domains,GratisDNS,HE.net,HE.net (v6),HE.net Tunnelbroker,Hover,Loopia,Namecheap,No-IP,No-IP (free),ODS.org,OpenDNS,OVH DynHOST,Route 53,SelfHost,SPDYN,SPDYN (v6),ZoneEdit');
60 0e3aeb6b Phil Davis
61 3aa55bbe Phil Davis
/* implement ipv6 route advertising daemon */
62 92977616 Ermal
function services_radvd_configure($blacklist = array()) {
63 d57293a4 Seth Mos
	global $config, $g;
64 61e047a5 Phil Davis
65
	if (isset($config['system']['developerspew'])) {
66 d57293a4 Seth Mos
		$mt = microtime();
67 3f9cc8e4 smos
		echo "services_radvd_configure() being called $mt\n";
68 d57293a4 Seth Mos
	}
69
70 61e047a5 Phil Davis
	if (!is_array($config['dhcpdv6'])) {
71 d57293a4 Seth Mos
		$config['dhcpdv6'] = array();
72 61e047a5 Phil Davis
	}
73 d57293a4 Seth Mos
74
	$Iflist = get_configured_interface_list();
75 e9ab2ddb smos
	$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
76 d57293a4 Seth Mos
77 3f9cc8e4 smos
	$radvdconf = "# Automatically Generated, do not edit\n";
78 4a3ff493 Seth Mos
79 753bd64d Seth Mos
	/* Process all links which need the router advertise daemon */
80 3f9cc8e4 smos
	$radvdifs = array();
81 668e8961 smos
82 3f9cc8e4 smos
	/* handle manually configured DHCP6 server settings first */
83 dfac167c Ermal
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
84 61e047a5 Phil Davis
		if (!is_array($config['interfaces'][$dhcpv6if])) {
85 dfac167c Ermal
			continue;
86 61e047a5 Phil Davis
		}
87
		if (!isset($config['interfaces'][$dhcpv6if]['enable'])) {
88 d7d2dc52 smos
			continue;
89 61e047a5 Phil Davis
		}
90 5078cd76 smos
91 92977616 Ermal
		/* Do not put in the config an interface which is down */
92 61e047a5 Phil Davis
		if (isset($blacklist[$dhcpv6if])) {
93 92977616 Ermal
			continue;
94 61e047a5 Phil Davis
		}
95
		if (!isset($dhcpv6ifconf['ramode'])) {
96 8ca73e85 smos
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
97 61e047a5 Phil Davis
		}
98 8ca73e85 smos
99 3f9cc8e4 smos
		/* are router advertisements enabled? */
100 61e047a5 Phil Davis
		if ($dhcpv6ifconf['ramode'] == "disabled") {
101 361bb4a9 smos
			continue;
102 61e047a5 Phil Davis
		}
103 d57293a4 Seth Mos
104 61e047a5 Phil Davis
		if (!isset($dhcpv6ifconf['rapriority'])) {
105 8ca73e85 smos
			$dhcpv6ifconf['rapriority'] = "medium";
106 61e047a5 Phil Davis
		}
107 8ca73e85 smos
108 c0509674 Chris Buechler
		$racarpif = false;
109
		/* check if binding to CARP IP */
110
		if (!empty($dhcpv6ifconf['rainterface'])) {
111
			if (strstr($dhcpv6ifconf['rainterface'], "_vip")) {
112
				if (get_carp_interface_status($dhcpv6ifconf['rainterface']) == "MASTER") {
113
					$dhcpv6if = $dhcpv6ifconf['rainterface'];
114
					$racarpif = true;
115
				} else {
116
					continue;
117
				}
118
			}
119
		}
120
121 2a5960b0 Luiz Otavio O Souza
		$realif = get_real_interface($dhcpv6if, "inet6");
122 61e047a5 Phil Davis
123
		if (isset($radvdifs[$realif])) {
124 c18a10cc smos
			continue;
125 61e047a5 Phil Davis
		}
126 c18a10cc smos
127 2626cbd1 Ermal
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
128 61e047a5 Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
129 5078cd76 smos
			continue;
130 61e047a5 Phil Davis
		}
131 5078cd76 smos
132 d57293a4 Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
133
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
134 e2158306 Chris Buechler
		if (!is_subnetv6($subnetv6 . "/" . $ifcfgsnv6)) {
135
			log_error("radvd: skipping configuration for interface $dhcpv6if because its subnet or prefix length is invalid.");
136
			continue;
137
		}
138 60c05056 Ermal
		$radvdifs[$realif] = $realif;
139 d57293a4 Seth Mos
140 3f9cc8e4 smos
		$radvdconf .= "# Generated for DHCPv6 Server $dhcpv6if\n";
141
		$radvdconf .= "interface {$realif} {\n";
142 e03b6bbc Chris Buechler
		if (strstr($realif, "ovpn")) {
143
			$radvdconf .= "\tUnicastOnly on;\n";
144
		}
145 3f9cc8e4 smos
		$radvdconf .= "\tAdvSendAdvert on;\n";
146 48eb136f Chris Buechler
147
		if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
148
			$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
149
		} else {
150
			$radvdconf .= "\tMinRtrAdvInterval 5;\n";
151
		}
152
153
		if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
154
			$radvdconf .= "\tMaxRtrAdvInterval {$dhcpv6ifconf['ramaxrtradvinterval']};\n";
155
		} else {
156
			$radvdconf .= "\tMaxRtrAdvInterval 20;\n";
157
		}
158 d12fc6cd Chris Buechler
		if (is_numericint($dhcpv6ifconf['raadvdefaultlifetime'])) {
159
			$radvdconf .= "\tAdvDefaultLifetime {$dhcpv6ifconf['raadvdefaultlifetime']};\n";
160
		}
161 48eb136f Chris Buechler
162 a6bc492f Ermal
		$mtu = get_interface_mtu($realif);
163 61e047a5 Phil Davis
		if (is_numeric($mtu)) {
164 a6bc492f Ermal
			$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
165 61e047a5 Phil Davis
		} else {
166 a6bc492f Ermal
			$radvdconf .= "\tAdvLinkMTU 1280;\n";
167 61e047a5 Phil Davis
		}
168
		switch ($dhcpv6ifconf['rapriority']) {
169 fe838158 smos
			case "low":
170
				$radvdconf .= "\tAdvDefaultPreference low;\n";
171
				break;
172
			case "high":
173
				$radvdconf .= "\tAdvDefaultPreference high;\n";
174 838a1ecb smos
				break;
175
			default:
176
				$radvdconf .= "\tAdvDefaultPreference medium;\n";
177
				break;
178 fe838158 smos
		}
179 61e047a5 Phil Davis
		switch ($dhcpv6ifconf['ramode']) {
180 656f1763 Seth Mos
			case "managed":
181 3f9cc8e4 smos
			case "assist":
182 8c78e692 plinss
				$radvdconf .= "\tAdvManagedFlag on;\n";
183 3f9cc8e4 smos
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
184
				break;
185 bd7ce993 aqueeb
			case "stateless_dhcp":
186
				$radvdconf .= "\tAdvManagedFlag off;\n";
187
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
188
				break;
189 3f9cc8e4 smos
		}
190
		$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
191 c0509674 Chris Buechler
		if ($racarpif == true) {
192
			$radvdconf .= "\t\tDeprecatePrefix off;\n";
193
		} else {
194
			$radvdconf .= "\t\tDeprecatePrefix on;\n";
195
		}
196 61e047a5 Phil Davis
		switch ($dhcpv6ifconf['ramode']) {
197 3f9cc8e4 smos
			case "managed":
198
				$radvdconf .= "\t\tAdvOnLink on;\n";
199
				$radvdconf .= "\t\tAdvAutonomous off;\n";
200
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
201 826ac52c smos
				break;
202
			case "router":
203 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink off;\n";
204
				$radvdconf .= "\t\tAdvAutonomous off;\n";
205
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
206 656f1763 Seth Mos
				break;
207 bd7ce993 aqueeb
			case "stateless_dhcp":
208 656f1763 Seth Mos
			case "assist":
209 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink on;\n";
210
				$radvdconf .= "\t\tAdvAutonomous on;\n";
211
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
212 107e8acc Ovidiu Predescu
				break;
213 3f9cc8e4 smos
			case "unmanaged":
214
				$radvdconf .= "\t\tAdvOnLink on;\n";
215
				$radvdconf .= "\t\tAdvAutonomous on;\n";
216
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
217 61e047a5 Phil Davis
				break;
218 656f1763 Seth Mos
		}
219 352defe4 schinken
220 6a461f45 Phil Davis
		if (is_numericint($dhcpv6ifconf['ravalidlifetime'])) {
221 352defe4 schinken
		  $radvdconf .= "\t\tAdvValidLifetime {$dhcpv6ifconf['ravalidlifetime']};\n";
222 be4748a8 schinken
		} else {
223
		  $radvdconf .= "\t\tAdvValidLifetime 86400;\n";
224 352defe4 schinken
		}
225
226 6a461f45 Phil Davis
		if (is_numericint($dhcpv6ifconf['rapreferredlifetime'])) {
227 352defe4 schinken
		  $radvdconf .= "\t\tAdvPreferredLifetime {$dhcpv6ifconf['rapreferredlifetime']};\n";
228 be4748a8 schinken
		} else {
229
		  $radvdconf .= "\t\tAdvPreferredLifetime 14400;\n";
230 352defe4 schinken
		}
231
232 3f9cc8e4 smos
		$radvdconf .= "\t};\n";
233
234 686e53c0 Chris Buechler
		if (is_array($dhcpv6ifconf['subnets']['item'])) {
235
			foreach ($dhcpv6ifconf['subnets']['item'] as $subnet) {
236
				if (is_subnetv6($subnet)) {
237
					$radvdconf .= "\tprefix {$subnet} {\n";
238 2a5960b0 Luiz Otavio O Souza
					$radvdconf .= "\t\tDeprecatePrefix on;\n";
239 cf3904bd Phil Davis
					switch ($dhcpv6ifconf['ramode']) {
240 686e53c0 Chris Buechler
						case "managed":
241
							$radvdconf .= "\t\tAdvOnLink on;\n";
242
							$radvdconf .= "\t\tAdvAutonomous off;\n";
243
							$radvdconf .= "\t\tAdvRouterAddr on;\n";
244
							break;
245
						case "router":
246
							$radvdconf .= "\t\tAdvOnLink off;\n";
247
							$radvdconf .= "\t\tAdvAutonomous off;\n";
248
							$radvdconf .= "\t\tAdvRouterAddr on;\n";
249
							break;
250
						case "assist":
251
							$radvdconf .= "\t\tAdvOnLink on;\n";
252
							$radvdconf .= "\t\tAdvAutonomous on;\n";
253
							$radvdconf .= "\t\tAdvRouterAddr on;\n";
254
							break;
255
						case "unmanaged":
256
							$radvdconf .= "\t\tAdvOnLink on;\n";
257
							$radvdconf .= "\t\tAdvAutonomous on;\n";
258
							$radvdconf .= "\t\tAdvRouterAddr on;\n";
259 086cf944 Phil Davis
							break;
260 686e53c0 Chris Buechler
					}
261
					$radvdconf .= "\t};\n";
262
				}
263
			}
264
		}
265 2a5960b0 Luiz Otavio O Souza
		$radvdconf .= "\troute ::/0 {\n";
266
		$radvdconf .= "\t\tRemoveRoute on;\n";
267
		$radvdconf .= "\t};\n";
268 8859c0a6 smos
269 f535d5a0 Seth Mos
		/* add DNS servers */
270 3aa114d5 Seth Mos
		$dnslist = array();
271 3d73a44d Renato Botelho
		if (isset($dhcpv6ifconf['rasamednsasdhcp6']) && is_array($dhcpv6ifconf['dnsserver']) && !empty($dhcpv6ifconf['dnsserver'])) {
272 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['dnsserver'] as $server) {
273
				if (is_ipaddrv6($server)) {
274 3d73a44d Renato Botelho
					$dnslist[] = $server;
275 61e047a5 Phil Davis
				}
276
			}
277 3d73a44d Renato Botelho
		} elseif (!isset($dhcpv6ifconf['rasamednsasdhcp6']) && isset($dhcpv6ifconf['radnsserver']) && is_array($dhcpv6ifconf['radnsserver'])) {
278 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['radnsserver'] as $server) {
279
				if (is_ipaddrv6($server)) {
280 3aa114d5 Seth Mos
					$dnslist[] = $server;
281 61e047a5 Phil Davis
				}
282
			}
283 f4620b36 Chris Buechler
		} elseif (isset($config['dnsmasq']['enable']) || isset($config['unbound']['enable'])) {
284 9a933304 smos
			$dnslist[] = get_interface_ipv6($realif);
285 abdd01f5 Ermal
		} elseif (is_array($config['system']['dnsserver']) && !empty($config['system']['dnsserver'])) {
286 61e047a5 Phil Davis
			foreach ($config['system']['dnsserver'] as $server) {
287
				if (is_ipaddrv6($server)) {
288 3aa114d5 Seth Mos
					$dnslist[] = $server;
289 61e047a5 Phil Davis
				}
290 3aa114d5 Seth Mos
			}
291
		}
292 abdd01f5 Ermal
		if (count($dnslist) > 0) {
293 3f9cc8e4 smos
			$dnsstring = implode(" ", $dnslist);
294 61e047a5 Phil Davis
			if ($dnsstring <> "") {
295 6c582308 smos
				$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
296 61e047a5 Phil Davis
			}
297 f535d5a0 Seth Mos
		}
298 abdd01f5 Ermal
		if (!empty($dhcpv6ifconf['domain'])) {
299 6c582308 smos
			$radvdconf .= "\tDNSSL {$dhcpv6ifconf['domain']} { };\n";
300 abdd01f5 Ermal
		} elseif (!empty($config['system']['domain'])) {
301 6c582308 smos
			$radvdconf .= "\tDNSSL {$config['system']['domain']} { };\n";
302 f535d5a0 Seth Mos
		}
303 3f9cc8e4 smos
		$radvdconf .= "};\n";
304 ed395640 Seth Mos
	}
305
306 3f9cc8e4 smos
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
307 ed395640 Seth Mos
	foreach ($Iflist as $if => $ifdescr) {
308 08d3034c Renato Botelho
		if (!isset($config['interfaces'][$if]['track6-interface']) ||
309
		    !isset($config['interfaces'][$if]['ipaddrv6']) ||
310
		    $config['interfaces'][$if]['ipaddrv6'] != 'track6') {
311 ed395640 Seth Mos
			continue;
312 61e047a5 Phil Davis
		}
313
		if (!isset($config['interfaces'][$if]['enable'])) {
314 d7d2dc52 smos
			continue;
315 61e047a5 Phil Davis
		}
316 8e187daa Chris Buechler
		if ($config['dhcpdv6'][$if]['ramode'] == "disabled") {
317
			continue;
318
		}
319 92977616 Ermal
		/* Do not put in the config an interface which is down */
320 61e047a5 Phil Davis
		if (isset($blacklist[$if])) {
321 92977616 Ermal
			continue;
322 61e047a5 Phil Davis
		}
323 60c05056 Ermal
		$trackif = $config['interfaces'][$if]['track6-interface'];
324 61e047a5 Phil Davis
		if (empty($config['interfaces'][$trackif])) {
325 60c05056 Ermal
			continue;
326 61e047a5 Phil Davis
		}
327 60c05056 Ermal
328 2a5960b0 Luiz Otavio O Souza
		$realif = get_real_interface($if, "inet6");
329 61e047a5 Phil Davis
330 3f9cc8e4 smos
		/* prevent duplicate entries, manual overrides */
331 61e047a5 Phil Davis
		if (isset($radvdifs[$realif])) {
332 7492f21d smos
			continue;
333 61e047a5 Phil Davis
		}
334 7492f21d smos
335 ed395640 Seth Mos
		$ifcfgipv6 = get_interface_ipv6($if);
336 61e047a5 Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
337 75aec77a Ermal
			$subnetv6 = "::";
338
			$ifcfgsnv6 = "64";
339
		} else {
340
			$ifcfgsnv6 = get_interface_subnetv6($if);
341
			$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
342
		}
343 60c05056 Ermal
		$radvdifs[$realif] = $realif;
344 c18a10cc smos
345 20a7cb15 smos
		$autotype = $config['interfaces'][$trackif]['ipaddrv6'];
346 61e047a5 Phil Davis
347
		if ($g['debug']) {
348 dfac167c Ermal
			log_error("configuring RA on {$if} for type {$autotype} radvd subnet {$subnetv6}/{$ifcfgsnv6}");
349 61e047a5 Phil Davis
		}
350 668e8961 smos
351 60c05056 Ermal
		$radvdconf .= "# Generated config for {$autotype} delegation from {$trackif} on {$if}\n";
352
		$radvdconf .= "interface {$realif} {\n";
353
		$radvdconf .= "\tAdvSendAdvert on;\n";
354 48eb136f Chris Buechler
		if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
355
			$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
356
		} else {
357
			$radvdconf .= "\tMinRtrAdvInterval 5;\n";
358
                }
359
		if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
360
			$radvdconf .= "\tMaxRtrAdvInterval {$dhcpv6ifconf['ramaxrtradvinterval']};\n";
361
		} else {
362
			$radvdconf .= "\tMaxRtrAdvInterval 10;\n";
363
		}
364 60c05056 Ermal
		$mtu = get_interface_mtu($realif);
365 61e047a5 Phil Davis
		if (is_numeric($mtu)) {
366 60c05056 Ermal
			$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
367 61e047a5 Phil Davis
		} else {
368 60c05056 Ermal
			$radvdconf .= "\tAdvLinkMTU 1280;\n";
369 61e047a5 Phil Davis
		}
370 60c05056 Ermal
		$radvdconf .= "\tAdvOtherConfigFlag on;\n";
371 e2158306 Chris Buechler
		$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
372 60c05056 Ermal
		$radvdconf .= "\t\tAdvOnLink on;\n";
373
		$radvdconf .= "\t\tAdvAutonomous on;\n";
374
		$radvdconf .= "\t\tAdvRouterAddr on;\n";
375
		$radvdconf .= "\t};\n";
376
377
		/* add DNS servers */
378 668e8961 smos
		$dnslist = array();
379 f4620b36 Chris Buechler
		if (isset($config['dnsmasq']['enable']) || isset($config['unbound']['enable'])) {
380 60c05056 Ermal
			$dnslist[] = $ifcfgipv6;
381
		} elseif (is_array($config['system']['dnsserver']) && !empty($config['system']['dnsserver'])) {
382 61e047a5 Phil Davis
			foreach ($config['system']['dnsserver'] as $server) {
383
				if (is_ipaddrv6($server)) {
384 60c05056 Ermal
					$dnslist[] = $server;
385 61e047a5 Phil Davis
				}
386 60c05056 Ermal
			}
387 668e8961 smos
		}
388 60c05056 Ermal
		if (count($dnslist) > 0) {
389
			$dnsstring = implode(" ", $dnslist);
390 61e047a5 Phil Davis
			if (!empty($dnsstring)) {
391 60c05056 Ermal
				$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
392 61e047a5 Phil Davis
			}
393 60c05056 Ermal
		}
394
		if (!empty($config['system']['domain'])) {
395
			$radvdconf .= "\tDNSSL {$config['system']['domain']} { };\n";
396
		}
397
		$radvdconf .= "};\n";
398 668e8961 smos
	}
399
400 928d4416 Ermal
	/* write radvd.conf */
401 abdd01f5 Ermal
	if (!@file_put_contents("{$g['varetc_path']}/radvd.conf", $radvdconf)) {
402 e8c516a0 Phil Davis
		log_error(gettext("Error: cannot open radvd.conf in services_radvd_configure()."));
403 61e047a5 Phil Davis
		if (platform_booting()) {
404 abdd01f5 Ermal
			printf("Error: cannot open radvd.conf in services_radvd_configure().\n");
405 61e047a5 Phil Davis
		}
406 abdd01f5 Ermal
	}
407 928d4416 Ermal
	unset($radvdconf);
408 d57293a4 Seth Mos
409 abdd01f5 Ermal
	if (count($radvdifs) > 0) {
410 61e047a5 Phil Davis
		if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
411 abdd01f5 Ermal
			sigkillbypid("{$g['varrun_path']}/radvd.pid", "HUP");
412 61e047a5 Phil Davis
		} else {
413 abdd01f5 Ermal
			mwexec("/usr/local/sbin/radvd -p {$g['varrun_path']}/radvd.pid -C {$g['varetc_path']}/radvd.conf -m syslog");
414 61e047a5 Phil Davis
		}
415 6afeb202 smos
	} else {
416
		/* we need to shut down the radvd cleanly, it will send out the prefix
417
		 * information with a lifetime of 0 to notify clients of a (possible) new prefix */
418 abdd01f5 Ermal
		if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
419 e8c516a0 Phil Davis
			log_error(gettext("Shutting down Router Advertisment daemon cleanly"));
420 abdd01f5 Ermal
			killbypid("{$g['varrun_path']}/radvd.pid");
421 dfac167c Ermal
			@unlink("{$g['varrun_path']}/radvd.pid");
422 6afeb202 smos
		}
423 d57293a4 Seth Mos
	}
424
	return 0;
425
}
426
427 92977616 Ermal
function services_dhcpd_configure($family = "all", $blacklist = array()) {
428 f19d3b7a Scott Ullrich
	global $config, $g;
429 2fb056d8 Seth Mos
430 02809cc6 NewEraCracker
	$dhcpdconfigurelck = lock("dhcpdconfigure", LOCK_EX);
431
432 2fb056d8 Seth Mos
	/* configure DHCPD chroot once */
433 4de8f7ba Phil Davis
	$fd = fopen("{$g['tmp_path']}/dhcpd.sh", "w");
434 d6c82882 Ermal
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}\n");
435
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
436
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
437
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
438
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
439
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/run\n");
440
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
441
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
442
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/run\n");
443
	fwrite($fd, "/usr/sbin/chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
444 06886ae3 Ermal
	fwrite($fd, "/bin/cp -n /lib/libc.so.* {$g['dhcpd_chroot_path']}/lib/\n");
445
	fwrite($fd, "/bin/cp -n /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
446 60214e06 bcyrill
	fwrite($fd, "/bin/chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
447 d6c82882 Ermal
448 4de8f7ba Phil Davis
	$status = `/sbin/mount | /usr/bin/grep -v grep | /usr/bin/grep "{$g['dhcpd_chroot_path']}/dev"`;
449 61e047a5 Phil Davis
	if (!trim($status)) {
450 d6c82882 Ermal
		fwrite($fd, "/sbin/mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
451 61e047a5 Phil Davis
	}
452 2fb056d8 Seth Mos
	fclose($fd);
453
	mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh");
454
455 61e047a5 Phil Davis
	if ($family == "all" || $family == "inet") {
456 3084ba6e Ermal
		services_dhcpdv4_configure();
457 61e047a5 Phil Davis
	}
458 3084ba6e Ermal
	if ($family == "all" || $family == "inet6") {
459 92977616 Ermal
		services_dhcpdv6_configure($blacklist);
460
		services_radvd_configure($blacklist);
461 3084ba6e Ermal
	}
462 02809cc6 NewEraCracker
463
	unlock($dhcpdconfigurelck);
464 2fb056d8 Seth Mos
}
465 1c903aa4 Ermal
466 2fb056d8 Seth Mos
function services_dhcpdv4_configure() {
467
	global $config, $g;
468 3df2dbfd jim-p
	$need_ddns_updates = false;
469
	$ddns_zones = array();
470 107e8acc Ovidiu Predescu
471 61e047a5 Phil Davis
	if ($g['services_dhcp_server_enable'] == false) {
472 e3a13b00 Scott Ullrich
		return;
473 61e047a5 Phil Davis
	}
474 e3a13b00 Scott Ullrich
475 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
476 acd910bf Scott Ullrich
		$mt = microtime();
477 2fb056d8 Seth Mos
		echo "services_dhcpdv4_configure($if) being called $mt\n";
478 acd910bf Scott Ullrich
	}
479 107e8acc Ovidiu Predescu
480 af25d415 Chris Buechler
	/* kill any running dhcpd */
481 61e047a5 Phil Davis
	if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid")) {
482 ed395640 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
483 61e047a5 Phil Davis
	}
484 a25183c5 Scott Ullrich
485 15be1722 Ermal Luçi
	/* DHCP enabled on any interfaces? */
486 61e047a5 Phil Davis
	if (!is_dhcp_server_enabled()) {
487 15be1722 Ermal Luçi
		return 0;
488 61e047a5 Phil Davis
	}
489 15be1722 Ermal Luçi
490 48ab0cd2 Scott Ullrich
	/* if OLSRD is enabled, allow WAN to house DHCP. */
491 61e047a5 Phil Davis
	if (!function_exists('is_package_installed')) {
492 86ce2df7 Renato Botelho
		require_once('pkg-utils.inc');
493 61e047a5 Phil Davis
	}
494
	if (is_package_installed('olsrd') && isset($config['installedpackages']['olsrd'])) {
495
		foreach ($config['installedpackages']['olsrd']['config'] as $olsrd) {
496 86ce2df7 Renato Botelho
			if (isset($olsrd['enable']) && $olsrd['enable'] == "on") {
497
				$is_olsr_enabled = true;
498
				break;
499
			}
500 61e047a5 Phil Davis
		}
501
	}
502 48ab0cd2 Scott Ullrich
503 285ef132 Ermal LUÇI
	if (platform_booting()) {
504 e92e83d4 jim-p
		/* restore the leases, if we have them */
505
		if (file_exists("{$g['cf_conf_path']}/dhcpleases.tgz")) {
506
			$dhcprestore = "";
507
			$dhcpreturn = "";
508
			exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcpleases.tgz 2>&1", $dhcprestore, $dhcpreturn);
509
			$dhcprestore = implode(" ", $dhcprestore);
510 61e047a5 Phil Davis
			if ($dhcpreturn <> 0) {
511 e92e83d4 jim-p
				log_error(sprintf(gettext('DHCP leases restore failed exited with %1$s, the error is: %2$s%3$s'), $dhcpreturn, $dhcprestore, "\n"));
512 381e43e0 jim-p
			}
513
		}
514 e92e83d4 jim-p
		/* If this backup is still there on a full install, but we aren't going to use ram disks, remove the archive since this is a transition. */
515 2344bed4 Renato Botelho
		if (($g['platform'] == $g['product_name']) && !isset($config['system']['use_mfs_tmpvar'])) {
516 e92e83d4 jim-p
			unlink_if_exists("{$g['cf_conf_path']}/dhcpleases.tgz");
517
		}
518 381e43e0 jim-p
	}
519
520 5b237745 Scott Ullrich
	$syscfg = $config['system'];
521 61e047a5 Phil Davis
	if (!is_array($config['dhcpd'])) {
522 a8a98fb4 Seth Mos
		$config['dhcpd'] = array();
523 61e047a5 Phil Davis
	}
524 d57293a4 Seth Mos
	$dhcpdcfg = $config['dhcpd'];
525 6f9b8073 Ermal Luçi
	$Iflist = get_configured_interface_list();
526 107e8acc Ovidiu Predescu
527 3ad6b569 Phil Davis
	/* Only consider DNS servers with IPv4 addresses for the IPv4 DHCP server. */
528
	$dns_arrv4 = array();
529 68169a55 jim-p
	if (is_array($syscfg['dnsserver'])) {
530 61e047a5 Phil Davis
		foreach ($syscfg['dnsserver'] as $dnsserver) {
531 68169a55 jim-p
			if (is_ipaddrv4($dnsserver)) {
532
				$dns_arrv4[] = $dnsserver;
533
			}
534 3ad6b569 Phil Davis
		}
535
	}
536
537 61e047a5 Phil Davis
	if (platform_booting()) {
538 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP service...");
539 61e047a5 Phil Davis
	} else {
540 5b237745 Scott Ullrich
		sleep(1);
541 61e047a5 Phil Davis
	}
542 a25183c5 Scott Ullrich
543 518030b3 Scott Ullrich
	$custoptions = "";
544 107e8acc Ovidiu Predescu
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
545 61e047a5 Phil Davis
		if (is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
546
			foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
547
				if (!empty($item['type'])) {
548 678dfd0f Erik Fonnesbeck
					$itemtype = $item['type'];
549 61e047a5 Phil Davis
				} else {
550 678dfd0f Erik Fonnesbeck
					$itemtype = "text";
551 61e047a5 Phil Davis
				}
552 678dfd0f Erik Fonnesbeck
				$custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = {$itemtype};\n";
553 518030b3 Scott Ullrich
			}
554
		}
555 dc13f5e0 Phil Davis
		if (is_array($dhcpifconf['pool'])) {
556
			foreach ($dhcpifconf['pool'] as $poolidx => $poolconf) {
557
				if (is_array($poolconf['numberoptions']) && is_array($poolconf['numberoptions']['item'])) {
558
					foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
559
						if (!empty($item['type'])) {
560
							$itemtype = $item['type'];
561
						} else {
562
							$itemtype = "text";
563
						}
564
						$custoptions .= "option custom-{$dhcpif}-{$poolidx}-{$itemidx} code {$item['number']} = {$itemtype};\n";
565
					}
566
				}
567
			}
568
		}
569 518030b3 Scott Ullrich
	}
570 4cab31d0 Scott Ullrich
571 5b237745 Scott Ullrich
	$dhcpdconf = <<<EOD
572 107e8acc Ovidiu Predescu
573 5b237745 Scott Ullrich
option domain-name "{$syscfg['domain']}";
574 6c23757b Martin Fuchs
option ldap-server code 95 = text;
575 9be23653 Martin Fuchs
option domain-search-list code 119 = text;
576 fdb116a9 Donald A. Cupp Jr
option arch code 93 = unsigned integer 16; # RFC4578
577 518030b3 Scott Ullrich
{$custoptions}
578 5b237745 Scott Ullrich
default-lease-time 7200;
579
max-lease-time 86400;
580
log-facility local7;
581 175fe82b Scott Ullrich
one-lease-per-client true;
582 436a0f50 Scott Ullrich
deny duplicates;
583 9c88328f Scott Ullrich
ping-check true;
584 87019fc4 Andres Petralli
update-conflict-detection false;
585 5b237745 Scott Ullrich
586
EOD;
587 a25183c5 Scott Ullrich
588 61e047a5 Phil Davis
	if (!isset($dhcpifconf['disableauthoritative'])) {
589 d8912c6b Chris Buechler
		$dhcpdconf .= "authoritative;\n";
590 61e047a5 Phil Davis
	}
591 d8912c6b Chris Buechler
592 61e047a5 Phil Davis
	if (isset($dhcpifconf['alwaysbroadcast'])) {
593 5252b98d Scott Ullrich
		$dhcpdconf .= "always-broadcast on\n";
594 61e047a5 Phil Davis
	}
595 5252b98d Scott Ullrich
596 5b237745 Scott Ullrich
	$dhcpdifs = array();
597 3f141c9d Phil Davis
	$enable_add_routers = false;
598 c08a5659 smos
	$gateways_arr = return_gateways_array();
599
	/* only add a routers line if the system has any IPv4 gateway at all */
600
	/* a static route has a gateway, manually overriding this field always works */
601 61e047a5 Phil Davis
	foreach ($gateways_arr as $gwitem) {
602
		if ($gwitem['ipprotocol'] == "inet") {
603 3f141c9d Phil Davis
			$enable_add_routers = true;
604 c08a5659 smos
			break;
605
		}
606
	}
607 c7f44ae0 Scott Ullrich
608 4494cf6a Chris Buechler
	/*    loop through and determine if we need to setup
609 8fa56d1f Scott Ullrich
	 *    failover peer "bleh" entries
610
	 */
611
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
612 53f32329 Scott Ullrich
613 61e047a5 Phil Davis
		if (!isset($config['interfaces'][$dhcpif]['enable'])) {
614 57006646 Renato Botelho
			continue;
615 61e047a5 Phil Davis
		}
616 57006646 Renato Botelho
617 09f11c71 jim-p
		interfaces_staticarp_configure($dhcpif);
618
619 61e047a5 Phil Davis
		if (!isset($dhcpifconf['enable'])) {
620 6f9b8073 Ermal Luçi
			continue;
621 61e047a5 Phil Davis
		}
622 6f9b8073 Ermal Luçi
623 61e047a5 Phil Davis
		if ($dhcpifconf['failover_peerip'] <> "") {
624 a01f8bfc Ermal
			$intip = get_interface_ip($dhcpif);
625 8fa56d1f Scott Ullrich
			/*
626
			 *    yep, failover peer is defined.
627
			 *    does it match up to a defined vip?
628
			 */
629 d2edbd8a Scott Ullrich
			$skew = 110;
630 61e047a5 Phil Davis
			if (is_array($config['virtualip']['vip'])) {
631 9d7ca11f Ermal
				foreach ($config['virtualip']['vip'] as $vipent) {
632 61e047a5 Phil Davis
					if ($vipent['interface'] == $dhcpif) {
633 a01f8bfc Ermal
						$carp_nw = gen_subnet($vipent['subnet'], $vipent['subnet_bits']);
634
						if (ip_in_subnet($dhcpifconf['failover_peerip'], "{$carp_nw}/{$vipent['subnet_bits']}")) {
635
							/* this is the interface! */
636 61e047a5 Phil Davis
							if (is_numeric($vipent['advskew']) && (intval($vipent['advskew']) < 20)) {
637 a01f8bfc Ermal
								$skew = 0;
638
								break;
639
							}
640
						}
641 6181b36f Scott Ullrich
					}
642 8fa56d1f Scott Ullrich
				}
643 25066204 Scott Ullrich
			} else {
644 959dc96b Chris Buechler
				log_error(gettext("Warning!  DHCP Failover setup and no CARP virtual IPs defined!"));
645 8fa56d1f Scott Ullrich
			}
646 61e047a5 Phil Davis
			if ($skew > 10) {
647 8fa56d1f Scott Ullrich
				$type = "secondary";
648 0e93097a Scott Ullrich
				$my_port = "520";
649
				$peer_port = "519";
650 1a96a3a9 jim-p
				$dhcpdconf_pri = '';
651 8fa56d1f Scott Ullrich
			} else {
652 0e93097a Scott Ullrich
				$my_port = "519";
653
				$peer_port = "520";
654 8fa56d1f Scott Ullrich
				$type = "primary";
655 4de8f7ba Phil Davis
				$dhcpdconf_pri = "split 128;\n";
656 1a0bb737 Scott Ullrich
				$dhcpdconf_pri .= "  mclt 600;\n";
657 8fa56d1f Scott Ullrich
			}
658 a01f8bfc Ermal
659
			if (is_ipaddrv4($intip)) {
660 61e047a5 Phil Davis
				$dhcpdconf .= <<<EOPP
661 d5e4f7c9 jim-p
failover peer "dhcp_{$dhcpif}" {
662 8fa56d1f Scott Ullrich
  {$type};
663
  address {$intip};
664 0e93097a Scott Ullrich
  port {$my_port};
665 8fa56d1f Scott Ullrich
  peer address {$dhcpifconf['failover_peerip']};
666 0e93097a Scott Ullrich
  peer port {$peer_port};
667 2cd5ce14 Scott Ullrich
  max-response-delay 10;
668 b865d178 Scott Ullrich
  max-unacked-updates 10;
669
  {$dhcpdconf_pri}
670 b259d1c6 Scott Ullrich
  load balance max seconds 3;
671 8fa56d1f Scott Ullrich
}
672 959dc96b Chris Buechler
\n
673 8fa56d1f Scott Ullrich
EOPP;
674 a01f8bfc Ermal
			}
675 8fa56d1f Scott Ullrich
		}
676
	}
677
678 5b237745 Scott Ullrich
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
679 a25183c5 Scott Ullrich
680 3df2dbfd jim-p
		$newzone = array();
681 5b237745 Scott Ullrich
		$ifcfg = $config['interfaces'][$dhcpif];
682 a25183c5 Scott Ullrich
683 61e047a5 Phil Davis
		if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif])) {
684 5b237745 Scott Ullrich
			continue;
685 61e047a5 Phil Davis
		}
686 a55e9c70 Ermal Lu?i
		$ifcfgip = get_interface_ip($dhcpif);
687
		$ifcfgsn = get_interface_subnet($dhcpif);
688
		$subnet = gen_subnet($ifcfgip, $ifcfgsn);
689
		$subnetmask = gen_subnet_mask($ifcfgsn);
690 a25183c5 Scott Ullrich
691 61e047a5 Phil Davis
		if (!is_ipaddr($subnet)) {
692 85e3f445 Ermal
			continue;
693 61e047a5 Phil Davis
		}
694 85e3f445 Ermal
695 61e047a5 Phil Davis
		if ($is_olsr_enabled == true) {
696
			if ($dhcpifconf['netmask']) {
697 9a537862 Scott Ullrich
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
698 61e047a5 Phil Davis
			}
699
		}
700 48ab0cd2 Scott Ullrich
701 cba980f6 jim-p
		$all_pools = array();
702
		$all_pools[] = $dhcpifconf;
703
		if (is_array($dhcpifconf['pool'])) {
704
			$all_pools = array_merge($all_pools, $dhcpifconf['pool']);
705
		}
706
707 5b237745 Scott Ullrich
		$dnscfg = "";
708 a25183c5 Scott Ullrich
709 5b237745 Scott Ullrich
		if ($dhcpifconf['domain']) {
710
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
711
		}
712 107e8acc Ovidiu Predescu
713 61e047a5 Phil Davis
		if ($dhcpifconf['domainsearchlist'] <> "") {
714 a3de8b9e Pierre POMES
			$dnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpifconf['domainsearchlist'])) . "\";\n";
715 84931046 jim-p
		}
716 9be23653 Martin Fuchs
717 4e9cd828 Seth Mos
		if (isset($dhcpifconf['ddnsupdate'])) {
718 3df2dbfd jim-p
			$need_ddns_updates = true;
719
			$newzone = array();
720 61e047a5 Phil Davis
			if ($dhcpifconf['ddnsdomain'] <> "") {
721 3df2dbfd jim-p
				$newzone['domain-name'] = $dhcpifconf['ddnsdomain'];
722 4e9cd828 Seth Mos
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
723 3df2dbfd jim-p
			} else {
724
				$newzone['domain-name'] = $config['system']['domain'];
725
			}
726 cf688344 Renato Botelho
727
			$revsubnet = array_reverse(explode('.',$subnet));
728
729
			/* Take care of full classes first */
730
			switch ($ifcfgsn) {
731
				case 8:
732
					$start_octet = 3;
733
					break;
734
				case 16:
735
					$start_octet = 2;
736
					break;
737
				case 24:
738
					$start_octet = 1;
739 3df2dbfd jim-p
					break;
740 cf688344 Renato Botelho
				default:
741
					$start_octet = 0;
742
					/* Add subnet bitmask to first octet */
743
					$revsubnet[0] .= '-' . $ifcfgsn;
744
					break;
745
746
			}
747
748
			$ptr_domain = '';
749
			for ($octet = 0; $octet <= 3; $octet++) {
750
				if ($octet < $start_octet) {
751
					continue;
752 61e047a5 Phil Davis
				}
753 8ed54652 Chris Buechler
				$ptr_domain .= ((empty($ptr_domain) && $ptr_domain !== "0") ? '' : '.');
754 cf688344 Renato Botelho
				$ptr_domain .= $revsubnet[$octet];
755 4e9cd828 Seth Mos
			}
756 cf688344 Renato Botelho
			$ptr_domain .= ".in-addr.arpa";
757
			$newzone['ptr-domain'] = $ptr_domain;
758
			unset($ptr_domain, $revsubnet, $start_octet);
759 4e9cd828 Seth Mos
		}
760
761 aff9d6ab Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
762 8ee01642 Scott Ullrich
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
763 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
764 3df2dbfd jim-p
				$newzone['dns-servers'] = $dhcpifconf['dnsserver'];
765 61e047a5 Phil Davis
			}
766 aff9d6ab Scott Ullrich
		} else if (isset($config['dnsmasq']['enable'])) {
767 a55e9c70 Ermal Lu?i
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
768 61e047a5 Phil Davis
			if ($newzone['domain-name'] && is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
769 3df2dbfd jim-p
				$newzone['dns-servers'] = $syscfg['dnsserver'];
770 61e047a5 Phil Davis
			}
771 88a0937d Chris Buechler
		} else if (isset($config['unbound']['enable'])) {
772
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
773 3ad6b569 Phil Davis
		} else if (!empty($dns_arrv4)) {
774
			$dnscfg .= "	option domain-name-servers " . join(",", $dns_arrv4) . ";";
775 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
776 3ad6b569 Phil Davis
				$newzone['dns-servers'] = $dns_arrv4;
777 61e047a5 Phil Davis
			}
778 aff9d6ab Scott Ullrich
		}
779
780 61e047a5 Phil Davis
		/* Create classes - These all contain comma separated lists. Join them into one
781 cba980f6 jim-p
		   big comma separated string then split them all up. */
782
		$all_mac_strings = array();
783
		if (is_array($dhcpifconf['pool'])) {
784 61e047a5 Phil Davis
			foreach ($all_pools as $poolconf) {
785 cba980f6 jim-p
				$all_mac_strings[] = $poolconf['mac_allow'];
786
				$all_mac_strings[] = $poolconf['mac_deny'];
787
			}
788
		}
789
		$all_mac_strings[] = $dhcpifconf['mac_allow'];
790
		$all_mac_strings[] = $dhcpifconf['mac_deny'];
791 7fd93993 Ermal LUÇI
		if (!empty($all_mac_strings)) {
792 4de8f7ba Phil Davis
			$all_mac_list = array_unique(explode(',', implode(',', $all_mac_strings)));
793
			foreach ($all_mac_list as $mac) {
794
				if (empty($mac)) {
795
					continue;
796
				}
797
				$dhcpdconf .= 'class "' . str_replace(':', '', $mac) . '" {' . "\n";
798
				// Skip the first octet of the MAC address - for media type, typically Ethernet ("01") and match the rest.
799
				$dhcpdconf .= '	match if substring (hardware, 1, ' . (substr_count($mac, ':') + 1) . ') = ' . $mac . ';' . "\n";
800
				$dhcpdconf .= '}' . "\n";
801 61e047a5 Phil Davis
			}
802 1f1a08c8 jim-p
		}
803
804 85e3f445 Ermal
		$dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n";
805 c7f44ae0 Scott Ullrich
806 61e047a5 Phil Davis
		// Setup pool options
807 dc13f5e0 Phil Davis
		foreach ($all_pools as $all_pools_idx => $poolconf) {
808 9228166f Phil Davis
			if (!(ip_in_subnet($poolconf['range']['from'], "{$subnet}/{$ifcfgsn}") && ip_in_subnet($poolconf['range']['to'], "{$subnet}/{$ifcfgsn}"))) {
809
				// If the user has changed the subnet from the interfaces page and applied,
810
				// but has not updated the DHCP range, then the range to/from of the pool can be outside the subnet.
811 f8cc55bf Phil Davis
				// This can also happen when implementing the batch of changes when the setup wizard reloads the new settings.
812 9228166f Phil Davis
				$error_msg = sprintf(gettext("Invalid DHCP pool %s - %s for %s subnet %s/%s detected. Please correct the settings in Services, DHCP Server"), $poolconf['range']['from'], $poolconf['range']['to'], convert_real_interface_to_friendly_descr($dhcpif), $subnet, $ifcfgsn);
813 f8cc55bf Phil Davis
				$do_file_notice = true;
814
				$conf_ipv4_address = $ifcfg['ipaddr'];
815
				$conf_ipv4_subnetmask = $ifcfg['subnet'];
816
				if (is_ipaddrv4($conf_ipv4_address) && is_subnet("{$conf_ipv4_address}/{$conf_ipv4_subnetmask}")) {
817
					$conf_subnet_base = gen_subnet($conf_ipv4_address, $conf_ipv4_subnetmask);
818
					if (ip_in_subnet($poolconf['range']['from'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}") &&
819
					    ip_in_subnet($poolconf['range']['to'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}")) {
820
						// Even though the running interface subnet does not match the pool range,
821
						// the interface subnet in the config file contains the pool range.
822
						// We are somewhere part-way through a settings reload, e.g. after running the setup wizard.
823
						// services_dhcpdv4_configure will be called again later when the new interface settings from
824
						// the config are applied and at that time everything will match up.
825
						// Ignore this pool on this interface for now and just log the error to the system log.
826
						log_error($error_msg);
827
						$do_file_notice = false;
828
					}
829
				}
830
				if ($do_file_notice) {
831
					file_notice("DHCP", $error_msg);
832
				}
833 9228166f Phil Davis
				continue;
834
			}
835 cba980f6 jim-p
			$dhcpdconf .= "	pool {\n";
836
			/* is failover dns setup? */
837
			if (is_array($poolconf['dnsserver']) && $poolconf['dnsserver'][0] <> "") {
838
				$dhcpdconf .= "		option domain-name-servers {$poolconf['dnsserver'][0]}";
839 61e047a5 Phil Davis
				if ($poolconf['dnsserver'][1] <> "") {
840 cba980f6 jim-p
					$dhcpdconf .= ",{$poolconf['dnsserver'][1]}";
841 61e047a5 Phil Davis
				}
842
				if ($poolconf['dnsserver'][2] <> "") {
843 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][2]}";
844 61e047a5 Phil Davis
				}
845
				if ($poolconf['dnsserver'][3] <> "") {
846 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][3]}";
847 61e047a5 Phil Davis
				}
848 cba980f6 jim-p
				$dhcpdconf .= ";\n";
849
			}
850
851
			/* allow/deny MACs */
852
			$mac_allow_list = array_unique(explode(',', $poolconf['mac_allow']));
853
			foreach ($mac_allow_list as $mac) {
854 61e047a5 Phil Davis
				if (empty($mac)) {
855 cba980f6 jim-p
					continue;
856 61e047a5 Phil Davis
				}
857 cba980f6 jim-p
				$dhcpdconf .= "		allow members of \"" . str_replace(':', '', $mac) . "\";\n";
858
			}
859 3475eb04 Andrew Pilloud
			$deny_action = "deny";
860 8209517d jim-p
			if (isset($poolconf['nonak']) && empty($poolconf['failover_peerip'])) {
861 3475eb04 Andrew Pilloud
				$deny_action = "ignore";
862
			}
863 cba980f6 jim-p
			$mac_deny_list = array_unique(explode(',', $poolconf['mac_deny']));
864
			foreach ($mac_deny_list as $mac) {
865 61e047a5 Phil Davis
				if (empty($mac)) {
866 cba980f6 jim-p
					continue;
867 61e047a5 Phil Davis
				}
868 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action members of \"" . str_replace(':', '', $mac) . "\";\n";
869 cba980f6 jim-p
			}
870
871 61e047a5 Phil Davis
			if ($poolconf['failover_peerip'] <> "") {
872 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action dynamic bootp clients;\n";
873 61e047a5 Phil Davis
			}
874 cba980f6 jim-p
875 61e047a5 Phil Davis
			if (isset($poolconf['denyunknown'])) {
876 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action unknown-clients;\n";
877 61e047a5 Phil Davis
			}
878 cba980f6 jim-p
879 61e047a5 Phil Davis
			if ($poolconf['gateway'] && $poolconf['gateway'] != "none" && ($poolconf['gateway'] != $dhcpifconf['gateway'])) {
880 f9f6f7d4 jim-p
				$dhcpdconf .= "		option routers {$poolconf['gateway']};\n";
881 61e047a5 Phil Davis
			}
882 cba980f6 jim-p
883 61e047a5 Phil Davis
			if ($dhcpifconf['failover_peerip'] <> "") {
884 d5e4f7c9 jim-p
				$dhcpdconf .= "		failover peer \"dhcp_{$dhcpif}\";\n";
885 cba980f6 jim-p
			}
886
887
			$pdnscfg = "";
888
889
			if ($poolconf['domain'] && ($poolconf['domain'] != $dhcpifconf['domain'])) {
890
				$pdnscfg .= "		option domain-name \"{$poolconf['domain']}\";\n";
891
			}
892
893 61e047a5 Phil Davis
			if (!empty($poolconf['domainsearchlist']) && ($poolconf['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
894 cba980f6 jim-p
				$pdnscfg .= "		option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $poolconf['domainsearchlist'])) . "\";\n";
895
			}
896
897 7309ff39 Renato Botelho
			if (isset($poolconf['ddnsupdate'])) {
898 61e047a5 Phil Davis
				if (($poolconf['ddnsdomain'] <> "") && ($poolconf['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
899 cba980f6 jim-p
					$pdnscfg .= "		ddns-domainname \"{$poolconf['ddnsdomain']}\";\n";
900 61e047a5 Phil Davis
				}
901 cba980f6 jim-p
				$pdnscfg .= "		ddns-update-style interim;\n";
902
			}
903
904
			$dhcpdconf .= "{$pdnscfg}";
905 1f1a08c8 jim-p
906 cba980f6 jim-p
			// default-lease-time
907 61e047a5 Phil Davis
			if ($poolconf['defaultleasetime'] && ($poolconf['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
908 cba980f6 jim-p
				$dhcpdconf .= "		default-lease-time {$poolconf['defaultleasetime']};\n";
909 61e047a5 Phil Davis
			}
910 cba980f6 jim-p
911
			// max-lease-time
912 61e047a5 Phil Davis
			if ($poolconf['maxleasetime'] && ($poolconf['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
913 cba980f6 jim-p
				$dhcpdconf .= "		max-lease-time {$poolconf['maxleasetime']};\n";
914 61e047a5 Phil Davis
			}
915 cba980f6 jim-p
916 0b95ff4c Jose Luis Duran
			// ignore bootp
917
			if (isset($poolconf['ignorebootp'])) {
918
				$dhcpdconf .= "		ignore bootp;\n";
919
			}
920
921 c729f32a Brett Keller
			// ignore-client-uids
922
			if (isset($poolconf['ignoreclientuids'])) {
923
				$dhcpdconf .= "		ignore-client-uids true;\n";
924
			}
925
926 cba980f6 jim-p
			// netbios-name*
927 fa7b825f Renato Botelho
			if (is_array($poolconf['winsserver']) && $poolconf['winsserver'][0] && ($poolconf['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
928 cba980f6 jim-p
				$dhcpdconf .= "		option netbios-name-servers " . join(",", $poolconf['winsserver']) . ";\n";
929
				$dhcpdconf .= "		option netbios-node-type 8;\n";
930
			}
931 c7f44ae0 Scott Ullrich
932 cba980f6 jim-p
			// ntp-servers
933 61e047a5 Phil Davis
			if (is_array($poolconf['ntpserver']) && $poolconf['ntpserver'][0] && ($poolconf['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
934 cba980f6 jim-p
				$dhcpdconf .= "		option ntp-servers " . join(",", $poolconf['ntpserver']) . ";\n";
935 61e047a5 Phil Davis
			}
936 cba980f6 jim-p
937
			// tftp-server-name
938 61e047a5 Phil Davis
			if (!empty($poolconf['tftp']) && ($poolconf['tftp'] != $dhcpifconf['tftp'])) {
939 cba980f6 jim-p
				$dhcpdconf .= "		option tftp-server-name \"{$poolconf['tftp']}\";\n";
940 61e047a5 Phil Davis
			}
941 cba980f6 jim-p
942 dc13f5e0 Phil Davis
			// Handle pool-specific options
943
			$dhcpdconf .= "\n";
944
			// Ignore the first pool, which is the "overall" pool when $all_pools_idx is 0 - those are put outside the pool block later
945
			if ($poolconf['numberoptions']['item'] && ($all_pools_idx > 0)) {
946
				// Use the "real" pool index from the config, excluding the "overall" pool, and based from 0.
947
				// This matches the way $poolidx was used when generating the $custoptions string earlier.
948
				$poolidx = $all_pools_idx - 1;
949
				foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
950
					$item_value = base64_decode($item['value']);
951
					if (empty($item['type']) || $item['type'] == "text") {
952 34a8cebe Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} \"{$item_value}\";\n";
953 dc13f5e0 Phil Davis
					} else {
954 34a8cebe Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} {$item_value};\n";
955 dc13f5e0 Phil Davis
					}
956
				}
957
			}
958
959 cba980f6 jim-p
			// ldap-server
960 61e047a5 Phil Davis
			if (!empty($poolconf['ldap']) && ($poolconf['ldap'] != $dhcpifconf['ldap'])) {
961 cba980f6 jim-p
				$dhcpdconf .= "		option ldap-server \"{$poolconf['ldap']}\";\n";
962 61e047a5 Phil Davis
			}
963 cba980f6 jim-p
964
			// net boot information
965 61e047a5 Phil Davis
			if (isset($poolconf['netboot'])) {
966 cba980f6 jim-p
				if (!empty($poolconf['nextserver']) && ($poolconf['nextserver'] != $dhcpifconf['nextserver'])) {
967
					$dhcpdconf .= "		next-server {$poolconf['nextserver']};\n";
968
				}
969
				if (!empty($poolconf['filename']) && ($poolconf['filename'] != $dhcpifconf['filename'])) {
970
					$dhcpdconf .= "		filename \"{$poolconf['filename']}\";\n";
971
				}
972
				if (!empty($poolconf['rootpath']) && ($poolconf['rootpath'] != $dhcpifconf['rootpath'])) {
973
					$dhcpdconf .= "		option root-path \"{$poolconf['rootpath']}\";\n";
974
				}
975
			}
976
			$dhcpdconf .= "		range {$poolconf['range']['from']} {$poolconf['range']['to']};\n";
977
			$dhcpdconf .= "	}\n\n";
978
		}
979
// End of settings inside pools
980 a25183c5 Scott Ullrich
981 4208f7b1 timdufrane
		if ($dhcpifconf['gateway'] && $dhcpifconf['gateway'] != "none") {
982 5b237745 Scott Ullrich
			$routers = $dhcpifconf['gateway'];
983 c08a5659 smos
			$add_routers = true;
984 4208f7b1 timdufrane
		} elseif ($dhcpifconf['gateway'] == "none") {
985
			$add_routers = false;
986 c08a5659 smos
		} else {
987 3f141c9d Phil Davis
			$add_routers = $enable_add_routers;
988 a55e9c70 Ermal Lu?i
			$routers = $ifcfgip;
989 c08a5659 smos
		}
990 61e047a5 Phil Davis
		if ($add_routers) {
991 c08a5659 smos
			$dhcpdconf .= "	option routers {$routers};\n";
992 61e047a5 Phil Davis
		}
993 cba980f6 jim-p
994 c08a5659 smos
		$dhcpdconf .= <<<EOD
995 5b237745 Scott Ullrich
$dnscfg
996
997
EOD;
998 61e047a5 Phil Davis
		// default-lease-time
999
		if ($dhcpifconf['defaultleasetime']) {
1000 5b237745 Scott Ullrich
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
1001 61e047a5 Phil Davis
		}
1002 518030b3 Scott Ullrich
1003
		// max-lease-time
1004 61e047a5 Phil Davis
		if ($dhcpifconf['maxleasetime']) {
1005 5b237745 Scott Ullrich
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
1006 61e047a5 Phil Davis
		}
1007 a25183c5 Scott Ullrich
1008 518030b3 Scott Ullrich
		// netbios-name*
1009 5b237745 Scott Ullrich
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
1010
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
1011
			$dhcpdconf .= "	option netbios-node-type 8;\n";
1012
		}
1013 a25183c5 Scott Ullrich
1014 518030b3 Scott Ullrich
		// ntp-servers
1015 61e047a5 Phil Davis
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0]) {
1016 ad171999 Seth Mos
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
1017 61e047a5 Phil Davis
		}
1018 ad171999 Seth Mos
1019 518030b3 Scott Ullrich
		// tftp-server-name
1020 61e047a5 Phil Davis
		if ($dhcpifconf['tftp'] <> "") {
1021 6c23757b Martin Fuchs
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
1022 61e047a5 Phil Davis
		}
1023 6c23757b Martin Fuchs
1024 518030b3 Scott Ullrich
		// Handle option, number rowhelper values
1025
		$dhcpdconf .= "\n";
1026 61e047a5 Phil Davis
		if ($dhcpifconf['numberoptions']['item']) {
1027
			foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
1028 65cce9d7 Renato Botelho
				$item_value = base64_decode($item['value']);
1029 61e047a5 Phil Davis
				if (empty($item['type']) || $item['type'] == "text") {
1030 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} \"{$item_value}\";\n";
1031 61e047a5 Phil Davis
				} else {
1032 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} {$item_value};\n";
1033 61e047a5 Phil Davis
				}
1034 518030b3 Scott Ullrich
			}
1035
		}
1036
1037
		// ldap-server
1038 61e047a5 Phil Davis
		if ($dhcpifconf['ldap'] <> "") {
1039 6c23757b Martin Fuchs
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
1040 61e047a5 Phil Davis
		}
1041 6c23757b Martin Fuchs
1042 518030b3 Scott Ullrich
		// net boot information
1043 61e047a5 Phil Davis
		if (isset($dhcpifconf['netboot'])) {
1044 a2578c27 Anthony Wrather
			if ($dhcpifconf['nextserver'] <> "") {
1045
				$dhcpdconf .= "	next-server {$dhcpifconf['nextserver']};\n";
1046
			}
1047 fdb116a9 Donald A. Cupp Jr
			if (!empty($dhcpifconf['filename']) && !empty($dhcpifconf['filename32']) && !empty($dhcpifconf['filename64'])) {
1048
				$dhcpdconf .= "	if option arch = 00:06 {\n";
1049
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename32']}\";\n";
1050
				$dhcpdconf .= "	} else if option arch = 00:07 {\n";
1051
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename64']}\";\n";
1052 08bcd379 Phil Davis
				$dhcpdconf .= "	} else if option arch = 00:09 {\n";
1053
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename64']}\";\n";
1054 fdb116a9 Donald A. Cupp Jr
				$dhcpdconf .= "	} else {\n";
1055
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename']}\";\n";
1056
				$dhcpdconf .= "	}\n\n";
1057
			} elseif (!empty($dhcpifconf['filename'])) {
1058 4e9cd828 Seth Mos
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
1059
			}
1060 fdb116a9 Donald A. Cupp Jr
			if (!empty($dhcpifconf['rootpath'])) {
1061 ca126e03 Martin Fuchs
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
1062 cba980f6 jim-p
			}
1063 4e9cd828 Seth Mos
		}
1064 107e8acc Ovidiu Predescu
1065 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
1066
}
1067
1068
EOD;
1069
1070
		/* add static mappings */
1071
		if (is_array($dhcpifconf['staticmap'])) {
1072 a25183c5 Scott Ullrich
1073 5b237745 Scott Ullrich
			$i = 0;
1074
			foreach ($dhcpifconf['staticmap'] as $sm) {
1075 449f1dd2 Will Boyce
				$dhcpdconf .= "host s_{$dhcpif}_{$i} {\n";
1076
1077 61e047a5 Phil Davis
				if ($sm['mac']) {
1078
					$dhcpdconf .= "        hardware ethernet {$sm['mac']};\n";
1079
				}
1080 449f1dd2 Will Boyce
1081 61e047a5 Phil Davis
				if ($sm['cid']) {
1082
					$dhcpdconf .= "        option dhcp-client-identifier \"{$sm['cid']}\";\n";
1083
				}
1084 5b237745 Scott Ullrich
1085 61e047a5 Phil Davis
				if ($sm['ipaddr']) {
1086 5b237745 Scott Ullrich
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
1087 61e047a5 Phil Davis
				}
1088 a25183c5 Scott Ullrich
1089 ad30055f Ermal Lu?i
				if ($sm['hostname']) {
1090
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1091 46c5b763 pierrepomes
					$dhhostname = str_replace(".", "_", $dhhostname);
1092 2f590513 smos
					$dhcpdconf .= "	option host-name \"{$dhhostname}\";\n";
1093 8960e397 Ross Williams
					if ((isset($dhcpifconf['ddnsupdate']) || isset($sm['ddnsupdate'])) && (isset($dhcpifconf['ddnsforcehostname']) || isset($sm['ddnsforcehostname']))) {
1094
						$dhcpdconf .= "	ddns-hostname \"{$dhhostname}\";\n";
1095
					}
1096 ad30055f Ermal Lu?i
				}
1097 61e047a5 Phil Davis
				if ($sm['filename']) {
1098 a2578c27 Anthony Wrather
					$dhcpdconf .= "	filename \"{$sm['filename']}\";\n";
1099 61e047a5 Phil Davis
				}
1100 a2578c27 Anthony Wrather
1101 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1102 a2578c27 Anthony Wrather
					$dhcpdconf .= "	option root-path \"{$sm['rootpath']}\";\n";
1103 61e047a5 Phil Davis
				}
1104 80717709 Martin Fuchs
1105 61e047a5 Phil Davis
				if ($sm['gateway'] && ($sm['gateway'] != $dhcpifconf['gateway'])) {
1106 7309ff39 Renato Botelho
					$dhcpdconf .= "	option routers {$sm['gateway']};\n";
1107 61e047a5 Phil Davis
				}
1108 7309ff39 Renato Botelho
1109
				$smdnscfg = "";
1110
1111
				if ($sm['domain'] && ($sm['domain'] != $dhcpifconf['domain'])) {
1112
					$smdnscfg .= "	option domain-name \"{$sm['domain']}\";\n";
1113
				}
1114
1115 61e047a5 Phil Davis
				if (!empty($sm['domainsearchlist']) && ($sm['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
1116 7309ff39 Renato Botelho
					$smdnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $sm['domainsearchlist'])) . "\";\n";
1117
				}
1118
1119
				if (isset($sm['ddnsupdate'])) {
1120 61e047a5 Phil Davis
					if (($sm['ddnsdomain'] <> "") && ($sm['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
1121 4c62f9e6 Phil Davis
						$smdnscfg .= "		ddns-domainname \"{$sm['ddnsdomain']}\";\n";
1122 61e047a5 Phil Davis
					}
1123 4c62f9e6 Phil Davis
					$smdnscfg .= "		ddns-update-style interim;\n";
1124 7309ff39 Renato Botelho
				}
1125
1126
				if (is_array($sm['dnsserver']) && ($sm['dnsserver'][0]) && ($sm['dnsserver'][0] != $dhcpifconf['dnsserver'][0])) {
1127
					$smdnscfg .= "	option domain-name-servers " . join(",", $sm['dnsserver']) . ";\n";
1128
				}
1129
				$dhcpdconf .= "{$smdnscfg}";
1130
1131
				// default-lease-time
1132 61e047a5 Phil Davis
				if ($sm['defaultleasetime'] && ($sm['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
1133 7309ff39 Renato Botelho
					$dhcpdconf .= "	default-lease-time {$sm['defaultleasetime']};\n";
1134 61e047a5 Phil Davis
				}
1135 7309ff39 Renato Botelho
1136
				// max-lease-time
1137 61e047a5 Phil Davis
				if ($sm['maxleasetime'] && ($sm['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
1138 7309ff39 Renato Botelho
					$dhcpdconf .= "	max-lease-time {$sm['maxleasetime']};\n";
1139 61e047a5 Phil Davis
				}
1140 7309ff39 Renato Botelho
1141
				// netbios-name*
1142
				if (is_array($sm['winsserver']) && $sm['winsserver'][0] && ($sm['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
1143
					$dhcpdconf .= "	option netbios-name-servers " . join(",", $sm['winsserver']) . ";\n";
1144
					$dhcpdconf .= "	option netbios-node-type 8;\n";
1145
				}
1146
1147
				// ntp-servers
1148 61e047a5 Phil Davis
				if (is_array($sm['ntpserver']) && $sm['ntpserver'][0] && ($sm['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
1149 7309ff39 Renato Botelho
					$dhcpdconf .= "	option ntp-servers " . join(",", $sm['ntpserver']) . ";\n";
1150 61e047a5 Phil Davis
				}
1151 7309ff39 Renato Botelho
1152
				// tftp-server-name
1153 61e047a5 Phil Davis
				if (!empty($sm['tftp']) && ($sm['tftp'] != $dhcpifconf['tftp'])) {
1154 7309ff39 Renato Botelho
					$dhcpdconf .= "	option tftp-server-name \"{$sm['tftp']}\";\n";
1155 61e047a5 Phil Davis
				}
1156 7309ff39 Renato Botelho
1157 5b237745 Scott Ullrich
				$dhcpdconf .= "}\n";
1158
				$i++;
1159
			}
1160
		}
1161 a25183c5 Scott Ullrich
1162 6f9b8073 Ermal Luçi
		$dhcpdifs[] = get_real_interface($dhcpif);
1163 61e047a5 Phil Davis
		if ($newzone['domain-name']) {
1164
			if ($need_ddns_updates) {
1165 87019fc4 Andres Petralli
				$newzone['dns-servers'] = array($dhcpifconf['ddnsdomainprimary']);
1166 7c1747a3 Chris Buechler
				$newzone['ddnsdomainkeyname'] = $dhcpifconf['ddnsdomainkeyname'];
1167
				$newzone['ddnsdomainkey'] = $dhcpifconf['ddnsdomainkey'];
1168
				$dhcpdconf .= dhcpdkey($dhcpifconf);
1169 87019fc4 Andres Petralli
			}
1170 3df2dbfd jim-p
			$ddns_zones[] = $newzone;
1171 87019fc4 Andres Petralli
		}
1172 3df2dbfd jim-p
	}
1173
1174
	if ($need_ddns_updates) {
1175
		$dhcpdconf .= "ddns-update-style interim;\n";
1176 3cdef187 Andres Petralli
		$dhcpdconf .= "update-static-leases on;\n";
1177 87019fc4 Andres Petralli
1178 7c1747a3 Chris Buechler
		$dhcpdconf .= dhcpdzones($ddns_zones);
1179 5b237745 Scott Ullrich
	}
1180
1181 abdd01f5 Ermal
	/* write dhcpd.conf */
1182
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", $dhcpdconf)) {
1183
		printf(gettext("Error: cannot open dhcpd.conf in services_dhcpdv4_configure().%s"), "\n");
1184
		unset($dhcpdconf);
1185
		return 1;
1186
	}
1187 928d4416 Ermal
	unset($dhcpdconf);
1188 2fb056d8 Seth Mos
1189
	/* create an empty leases database */
1190 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases")) {
1191 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
1192 61e047a5 Phil Davis
	}
1193 2fb056d8 Seth Mos
1194 b075c1e2 Chris Buechler
	/* make sure there isn't a stale dhcpd.pid file, which can make dhcpd fail to start.   */
1195 61e047a5 Phil Davis
	/* if we get here, dhcpd has been killed and is not started yet                        */
1196 b075c1e2 Chris Buechler
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
1197
1198 2fb056d8 Seth Mos
	/* fire up dhcpd in a chroot */
1199 abdd01f5 Ermal
	if (count($dhcpdifs) > 0) {
1200 2fb056d8 Seth Mos
		mwexec("/usr/local/sbin/dhcpd -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf /etc/dhcpd.conf -pf {$g['varrun_path']}/dhcpd.pid " .
1201
			join(" ", $dhcpdifs));
1202
	}
1203
1204 61e047a5 Phil Davis
	if (platform_booting()) {
1205 2fb056d8 Seth Mos
		print "done.\n";
1206 61e047a5 Phil Davis
	}
1207 2fb056d8 Seth Mos
1208
	return 0;
1209
}
1210
1211 086cf944 Phil Davis
function dhcpdkey($dhcpifconf) {
1212 87019fc4 Andres Petralli
	$dhcpdconf = "";
1213 61e047a5 Phil Davis
	if ($dhcpifconf['ddnsdomainkeyname'] <> "" && $dhcpifconf['ddnsdomainkey'] <> "") {
1214 87019fc4 Andres Petralli
		$dhcpdconf .= "key {$dhcpifconf['ddnsdomainkeyname']} {\n";
1215
		$dhcpdconf .= "	algorithm hmac-md5;\n";
1216
		$dhcpdconf .= "	secret {$dhcpifconf['ddnsdomainkey']};\n";
1217
		$dhcpdconf .= "}\n";
1218
	}
1219
1220
	return $dhcpdconf;
1221
}
1222
1223 7c1747a3 Chris Buechler
function dhcpdzones($ddns_zones) {
1224 87019fc4 Andres Petralli
	$dhcpdconf = "";
1225
1226
	if (is_array($ddns_zones)) {
1227
		$added_zones = array();
1228
		foreach ($ddns_zones as $zone) {
1229 61e047a5 Phil Davis
			if (!is_array($zone) || empty($zone) || !is_array($zone['dns-servers'])) {
1230 87019fc4 Andres Petralli
				continue;
1231 61e047a5 Phil Davis
			}
1232 87019fc4 Andres Petralli
			$primary = $zone['dns-servers'][0];
1233
			$secondary = empty($zone['dns-servers'][1]) ? "" : $zone['dns-servers'][1];
1234
1235
			// Make sure we aren't using any invalid or IPv6 DNS servers.
1236
			if (!is_ipaddrv4($primary)) {
1237
				if (is_ipaddrv4($secondary)) {
1238
					$primary = $secondary;
1239
					$secondary = "";
1240
				} else {
1241
					continue;
1242
				}
1243
			}
1244
1245
			// We don't need to add zones multiple times.
1246
			if ($zone['domain-name'] && !in_array($zone['domain-name'], $added_zones)) {
1247
				$dhcpdconf .= "zone {$zone['domain-name']}. {\n";
1248
				$dhcpdconf .= "	primary {$primary};\n";
1249 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1250 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1251 61e047a5 Phil Davis
				}
1252 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1253
					$dhcpdconf .= "	key {$zone['ddnsdomainkeyname']};\n";
1254 61e047a5 Phil Davis
				}
1255 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1256
				$added_zones[] = $zone['domain-name'];
1257
			}
1258
			if ($zone['ptr-domain'] && !in_array($zone['ptr-domain'], $added_zones)) {
1259
				$dhcpdconf .= "zone {$zone['ptr-domain']} {\n";
1260
				$dhcpdconf .= "	primary {$primary};\n";
1261 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1262 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1263 61e047a5 Phil Davis
				}
1264 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1265
					$dhcpdconf .= "	key {$zone['ddnsdomainkeyname']};\n";
1266 61e047a5 Phil Davis
				}
1267 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1268
				$added_zones[] = $zone['ptr-domain'];
1269
			}
1270
		}
1271
	}
1272
1273
	return $dhcpdconf;
1274
}
1275
1276 92977616 Ermal
function services_dhcpdv6_configure($blacklist = array()) {
1277 2fb056d8 Seth Mos
	global $config, $g;
1278 107e8acc Ovidiu Predescu
1279 61e047a5 Phil Davis
	if ($g['services_dhcp_server_enable'] == false) {
1280 2fb056d8 Seth Mos
		return;
1281 61e047a5 Phil Davis
	}
1282 2fb056d8 Seth Mos
1283 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1284 2fb056d8 Seth Mos
		$mt = microtime();
1285
		echo "services_dhcpd_configure($if) being called $mt\n";
1286
	}
1287 107e8acc Ovidiu Predescu
1288 2fb056d8 Seth Mos
	/* kill any running dhcpd */
1289 61e047a5 Phil Davis
	if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid")) {
1290 bfb3e717 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
1291 61e047a5 Phil Davis
	}
1292
	if (isvalidpid("{$g['varrun_path']}/dhcpleases6.pid")) {
1293 f7cd5647 smos
		killbypid("{$g['varrun_path']}/dhcpleases6.pid");
1294 61e047a5 Phil Davis
	}
1295 e9ab2ddb smos
1296 2fb056d8 Seth Mos
	/* DHCP enabled on any interfaces? */
1297 61e047a5 Phil Davis
	if (!is_dhcpv6_server_enabled()) {
1298 2fb056d8 Seth Mos
		return 0;
1299 61e047a5 Phil Davis
	}
1300 2fb056d8 Seth Mos
1301 e90ca528 Renato Botelho
	if (platform_booting() &&
1302
	    ($g['platform'] != $g['product_name'] ||
1303
	    isset($config['system']['use_mfs_tmpvar'])) &&
1304
	    file_exists("{$g['cf_conf_path']}/dhcp6leases.tgz")) {
1305
		/* restore the leases, if we have them */
1306
		$dhcprestore = "";
1307
		$dhcpreturn = "";
1308
		exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcp6leases.tgz 2>&1",
1309
		    $dhcprestore, $dhcpreturn);
1310
		$dhcprestore = implode(" ", $dhcprestore);
1311
		if ($dhcpreturn <> 0) {
1312
			log_error(sprintf(gettext(
1313
			    'DHCP leases v6 restore failed exited with %1$s, the error is: %2$s'),
1314
			    $dhcpreturn, $dhcprestore));
1315 2fb056d8 Seth Mos
		}
1316
	}
1317
1318
	$syscfg = $config['system'];
1319 61e047a5 Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1320 2fb056d8 Seth Mos
		$config['dhcpdv6'] = array();
1321 61e047a5 Phil Davis
	}
1322 2fb056d8 Seth Mos
	$dhcpdv6cfg = $config['dhcpdv6'];
1323
	$Iflist = get_configured_interface_list();
1324 e9ab2ddb smos
	$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
1325
1326 107e8acc Ovidiu Predescu
1327 61e047a5 Phil Davis
	if (platform_booting()) {
1328 2fb056d8 Seth Mos
		echo "Starting DHCPv6 service...";
1329 61e047a5 Phil Davis
	} else {
1330 2fb056d8 Seth Mos
		sleep(1);
1331 61e047a5 Phil Davis
	}
1332 2fb056d8 Seth Mos
1333
	$custoptionsv6 = "";
1334 107e8acc Ovidiu Predescu
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1335 61e047a5 Phil Davis
		if (is_array($dhcpv6ifconf['numberoptions']) && is_array($dhcpv6ifconf['numberoptions']['item'])) {
1336
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1337 2fb056d8 Seth Mos
				$custoptionsv6 .= "option custom-{$dhcpv6if}-{$itemv6idx} code {$itemv6['number']} = text;\n";
1338
			}
1339
		}
1340
	}
1341
1342 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['netboot']) && !empty($dhcpv6ifconf['bootfile_url'])) {
1343 bd942860 Renato Botelho
		$custoptionsv6 .= "option dhcp6.bootfile-url code 59 = string;\n";
1344 61e047a5 Phil Davis
	}
1345 bd942860 Renato Botelho
1346 2fb056d8 Seth Mos
	$dhcpdv6conf = <<<EOD
1347 107e8acc Ovidiu Predescu
1348 2fb056d8 Seth Mos
option domain-name "{$syscfg['domain']}";
1349
option ldap-server code 95 = text;
1350
option domain-search-list code 119 = text;
1351 547f1e65 Renato Botelho
{$custoptionsv6}
1352 2fb056d8 Seth Mos
default-lease-time 7200;
1353
max-lease-time 86400;
1354
log-facility local7;
1355
one-lease-per-client true;
1356
deny duplicates;
1357
ping-check true;
1358 87019fc4 Andres Petralli
update-conflict-detection false;
1359 2fb056d8 Seth Mos
1360
EOD;
1361
1362 61e047a5 Phil Davis
	if (!isset($dhcpv6ifconf['disableauthoritative'])) {
1363 2fb056d8 Seth Mos
		$dhcpdv6conf .= "authoritative;\n";
1364 61e047a5 Phil Davis
	}
1365 2fb056d8 Seth Mos
1366 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['alwaysbroadcast'])) {
1367 2fb056d8 Seth Mos
		$dhcpdv6conf .= "always-broadcast on\n";
1368 61e047a5 Phil Davis
	}
1369 2fb056d8 Seth Mos
1370
	$dhcpdv6ifs = array();
1371
1372 693833cb Seth Mos
	$dhcpv6num = 0;
1373 87019fc4 Andres Petralli
	$nsupdate = false;
1374
1375 693833cb Seth Mos
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1376
1377 87019fc4 Andres Petralli
		$ddns_zones = array();
1378
1379 693833cb Seth Mos
		$ifcfgv6 = $config['interfaces'][$dhcpv6if];
1380
1381 61e047a5 Phil Davis
		if (!isset($dhcpv6ifconf['enable']) || !isset($Iflist[$dhcpv6if]) || !isset($ifcfgv6['enable'])) {
1382 693833cb Seth Mos
			continue;
1383 61e047a5 Phil Davis
		}
1384 693833cb Seth Mos
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1385 830ea39a Chris Buechler
		if (!is_ipaddrv6($ifcfgipv6)) {
1386
			continue;
1387
		}
1388 693833cb Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
1389 d57293a4 Seth Mos
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1390 786d411d Phil Davis
		// We might have some prefix-delegation on WAN (e.g. /48),
1391
		// but then it is split and given out to individual interfaces
1392
		// (LAN, OPT1, OPT2...) as multiple /64 subnets. So the size
1393
		// of each subnet here is always /64.
1394
		$pdlen = 64;
1395 15db02a6 Renato Botelho
1396 1944af41 Ermal
		if ($is_olsr_enabled == true) {
1397 61e047a5 Phil Davis
			if ($dhcpv6ifconf['netmask']) {
1398 bfb3e717 Seth Mos
				$subnetmask = gen_subnet_maskv6($dhcpv6ifconf['netmask']);
1399 61e047a5 Phil Davis
			}
1400 1944af41 Ermal
		}
1401 693833cb Seth Mos
1402
		$dnscfgv6 = "";
1403
1404
		if ($dhcpv6ifconf['domain']) {
1405 3c009080 Seth Mos
			$dnscfgv6 .= "	option domain-name \"{$dhcpv6ifconf['domain']}\";\n";
1406 693833cb Seth Mos
		}
1407 107e8acc Ovidiu Predescu
1408 61e047a5 Phil Davis
		if ($dhcpv6ifconf['domainsearchlist'] <> "") {
1409 a3de8b9e Pierre POMES
			$dnscfgv6 .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpv6ifconf['domainsearchlist'])) . "\";\n";
1410 61e047a5 Phil Davis
		}
1411 693833cb Seth Mos
1412
		if (isset($dhcpv6ifconf['ddnsupdate'])) {
1413 61e047a5 Phil Davis
			if ($dhcpv6ifconf['ddnsdomain'] <> "") {
1414 3c009080 Seth Mos
				$dnscfgv6 .= "	ddns-domainname \"{$dhcpv6ifconf['ddnsdomain']}\";\n";
1415 693833cb Seth Mos
			}
1416 391d63da Renato Botelho
			if (empty($dhcpv6ifconf['ddnsclientupdates'])) {
1417
				$ddnsclientupdates = 'allow';
1418
			} else {
1419
				$ddnsclientupdates = $dhcpv6ifconf['ddnsclientupdates'];
1420
			}
1421
			$dnscfgv6 .= "	{$ddnsclientupdates} client-updates;\n";
1422 87019fc4 Andres Petralli
			$nsupdate = true;
1423 391d63da Renato Botelho
		} else {
1424
			$dnscfgv6 .= "	do-forward-updates false;\n";
1425 693833cb Seth Mos
		}
1426
1427
		if (is_array($dhcpv6ifconf['dnsserver']) && ($dhcpv6ifconf['dnsserver'][0])) {
1428
			$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dhcpv6ifconf['dnsserver']) . ";";
1429 f4620b36 Chris Buechler
		} else if (((isset($config['dnsmasq']['enable'])) || isset($config['unbound']['enable'])) && (is_ipaddrv6($ifcfgipv6))) {
1430 693833cb Seth Mos
			$dnscfgv6 .= "	option dhcp6.name-servers {$ifcfgipv6};";
1431
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1432 2521266a Seth Mos
			$dns_arrv6 = array();
1433 61e047a5 Phil Davis
			foreach ($syscfg['dnsserver'] as $dnsserver) {
1434 1944af41 Ermal
				if (is_ipaddrv6($dnsserver)) {
1435 2521266a Seth Mos
					$dns_arrv6[] = $dnsserver;
1436
				}
1437
			}
1438 61e047a5 Phil Davis
			if (!empty($dns_arrv6)) {
1439 2816c5a1 Seth Mos
				$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dns_arrv6) . ";";
1440 61e047a5 Phil Davis
			}
1441 693833cb Seth Mos
		}
1442
1443 391d63da Renato Botelho
		if (!is_ipaddrv6($ifcfgipv6)) {
1444
			$ifcfgsnv6 = "64";
1445
			$subnetv6 = gen_subnetv6($dhcpv6ifconf['range']['from'], $ifcfgsnv6);
1446
		}
1447
1448
		$dhcpdv6conf .= "subnet6 {$subnetv6}/{$ifcfgsnv6}";
1449
1450
		if (isset($dhcpv6ifconf['ddnsupdate']) &&
1451
		    !empty($dhcpv6ifconf['ddnsdomain'])) {
1452 87019fc4 Andres Petralli
			$newzone = array();
1453 391d63da Renato Botelho
			$newzone['domain-name'] = $dhcpv6ifconf['ddnsdomain'];
1454 87019fc4 Andres Petralli
			$newzone['dns-servers'][] = $dhcpv6ifconf['ddnsdomainprimary'];
1455 7c1747a3 Chris Buechler
			$newzone['ddnsdomainkeyname'] = $dhcpv6ifconf['ddnsdomainkeyname'];
1456
			$newzone['ddnsdomainkey'] = $dhcpv6ifconf['ddnsdomainkey'];
1457 87019fc4 Andres Petralli
			$ddns_zones[] = $newzone;
1458 391d63da Renato Botelho
			if (isset($dhcpv6ifconf['ddnsreverse'])) {
1459
				$ptr_zones = get_v6_ptr_zones($subnetv6, $ifcfgsnv6);
1460
				foreach ($ptr_zones as $ptr_zone) {
1461
					$reversezone = array();
1462
					$reversezone['domain-name'] = $ptr_zone;
1463
					$reversezone['dns-servers'][] =
1464
					    $dhcpv6ifconf['ddnsdomainprimary'];
1465
					$ddns_zones[] = $reversezone;
1466
				}
1467
			}
1468 87019fc4 Andres Petralli
		}
1469
1470 1944af41 Ermal
		$dhcpdv6conf .= " {\n";
1471 693833cb Seth Mos
1472 2bf455ca Renato Botelho
		$range_from = $dhcpv6ifconf['range']['from'];
1473
		$range_to = $dhcpv6ifconf['range']['to'];
1474
		if ($ifcfgv6['ipaddrv6'] == 'track6') {
1475 15db02a6 Renato Botelho
			$range_from = merge_ipv6_delegated_prefix($ifcfgipv6, $range_from, $pdlen);
1476
			$range_to = merge_ipv6_delegated_prefix($ifcfgipv6, $range_to, $pdlen);
1477 2bf455ca Renato Botelho
		}
1478
1479 693833cb Seth Mos
		$dhcpdv6conf .= <<<EOD
1480 2bf455ca Renato Botelho
	range6 {$range_from} {$range_to};
1481 693833cb Seth Mos
$dnscfgv6
1482
1483
EOD;
1484 bfb3e717 Seth Mos
1485 1944af41 Ermal
		if (is_ipaddrv6($dhcpv6ifconf['prefixrange']['from']) && is_ipaddrv6($dhcpv6ifconf['prefixrange']['to'])) {
1486 978b8f50 Chris Buechler
			$dhcpdv6conf .= "	prefix6 {$dhcpv6ifconf['prefixrange']['from']} {$dhcpv6ifconf['prefixrange']['to']} /{$dhcpv6ifconf['prefixrange']['prefixlength']};\n";
1487 bfb3e717 Seth Mos
		}
1488 70da4172 Jean Cyr
		if (is_ipaddrv6($dhcpv6ifconf['dns6ip'])) {
1489 2bf455ca Renato Botelho
			$dns6ip = $dhcpv6ifconf['dns6ip'];
1490
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1491 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($dns6ip, '::', $pdlen)) {
1492
				$dns6ip = merge_ipv6_delegated_prefix($ifcfgipv6, $dns6ip, $pdlen);
1493 2bf455ca Renato Botelho
			}
1494
			$dhcpdv6conf .= "	option dhcp6.name-servers {$dns6ip};\n";
1495 70da4172 Jean Cyr
		}
1496 61e047a5 Phil Davis
		// default-lease-time
1497
		if ($dhcpv6ifconf['defaultleasetime']) {
1498 693833cb Seth Mos
			$dhcpdv6conf .= "	default-lease-time {$dhcpv6ifconf['defaultleasetime']};\n";
1499 61e047a5 Phil Davis
		}
1500 693833cb Seth Mos
1501
		// max-lease-time
1502 61e047a5 Phil Davis
		if ($dhcpv6ifconf['maxleasetime']) {
1503 693833cb Seth Mos
			$dhcpdv6conf .= "	max-lease-time {$dhcpv6ifconf['maxleasetime']};\n";
1504 61e047a5 Phil Davis
		}
1505 693833cb Seth Mos
1506
		// ntp-servers
1507 4096fe5d smos
		if (is_array($dhcpv6ifconf['ntpserver']) && $dhcpv6ifconf['ntpserver'][0]) {
1508
			$ntpservers = array();
1509 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['ntpserver'] as $ntpserver) {
1510 2bf455ca Renato Botelho
				if (!is_ipaddrv6($ntpserver)) {
1511
					continue;
1512
				}
1513
				if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1514 15db02a6 Renato Botelho
				    Net_IPv6::isInNetmask($ntpserver, '::', $pdlen)) {
1515
					$ntpserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ntpserver, $pdlen);
1516 61e047a5 Phil Davis
				}
1517 2bf455ca Renato Botelho
				$ntpservers[] = $ntpserver;
1518 4096fe5d smos
			}
1519 61e047a5 Phil Davis
			if (count($ntpservers) > 0) {
1520 fe86a663 Chris Buechler
				$dhcpdv6conf .= "        option dhcp6.sntp-servers " . join(",", $dhcpv6ifconf['ntpserver']) . ";\n";
1521 61e047a5 Phil Davis
			}
1522 4096fe5d smos
		}
1523 693833cb Seth Mos
		// tftp-server-name
1524 7d504365 smos
		/* Needs ISC DHCPD support
1525 61e047a5 Phil Davis
		 if ($dhcpv6ifconf['tftp'] <> "") {
1526 693833cb Seth Mos
			$dhcpdv6conf .= "	option tftp-server-name \"{$dhcpv6ifconf['tftp']}\";\n";
1527 61e047a5 Phil Davis
		 }
1528 7d504365 smos
		*/
1529 693833cb Seth Mos
1530
		// Handle option, number rowhelper values
1531
		$dhcpdv6conf .= "\n";
1532 1944af41 Ermal
		if ($dhcpv6ifconf['numberoptions']['item']) {
1533 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1534 65cce9d7 Renato Botelho
				$itemv6_value = base64_decode($itemv6['value']);
1535
				$dhcpdv6conf .= "	option custom-{$dhcpv6if}-{$itemv6idx} \"{$itemv6_value}\";\n";
1536 693833cb Seth Mos
			}
1537
		}
1538
1539
		// ldap-server
1540 61e047a5 Phil Davis
		if ($dhcpv6ifconf['ldap'] <> "") {
1541 2bf455ca Renato Botelho
			$ldapserver = $dhcpv6ifconf['ldap'];
1542
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1543 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($ldapserver, '::', $pdlen)) {
1544
				$ldapserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ldapserver, $pdlen);
1545 2bf455ca Renato Botelho
			}
1546
			$dhcpdv6conf .= "	option ldap-server \"{$ldapserver}\";\n";
1547 61e047a5 Phil Davis
		}
1548 693833cb Seth Mos
1549
		// net boot information
1550 61e047a5 Phil Davis
		if (isset($dhcpv6ifconf['netboot'])) {
1551 bd942860 Renato Botelho
			if (!empty($dhcpv6ifconf['bootfile_url'])) {
1552
				$dhcpdv6conf .= "	option dhcp6.bootfile-url \"{$dhcpv6ifconf['bootfile_url']}\";\n";
1553 abdd01f5 Ermal
			}
1554
		}
1555 107e8acc Ovidiu Predescu
1556 1c903aa4 Ermal
		$dhcpdv6conf .= "}\n";
1557 693833cb Seth Mos
1558
		/* add static mappings */
1559 2fb056d8 Seth Mos
		/* Needs to use DUID */
1560 693833cb Seth Mos
		if (is_array($dhcpv6ifconf['staticmap'])) {
1561
			$i = 0;
1562
			foreach ($dhcpv6ifconf['staticmap'] as $sm) {
1563
				$dhcpdv6conf .= <<<EOD
1564
host s_{$dhcpv6if}_{$i} {
1565 2fb056d8 Seth Mos
	host-identifier option dhcp6.client-id {$sm['duid']};
1566 693833cb Seth Mos
1567
EOD;
1568 61e047a5 Phil Davis
				if ($sm['ipaddrv6']) {
1569 2bf455ca Renato Botelho
					$ipaddrv6 = $sm['ipaddrv6'];
1570
					if ($ifcfgv6['ipaddrv6'] == 'track6') {
1571 15db02a6 Renato Botelho
						$ipaddrv6 = merge_ipv6_delegated_prefix($ifcfgipv6, $ipaddrv6, $pdlen);
1572 2bf455ca Renato Botelho
					}
1573
					$dhcpdv6conf .= "	fixed-address6 {$ipaddrv6};\n";
1574 61e047a5 Phil Davis
				}
1575 693833cb Seth Mos
1576
				if ($sm['hostname']) {
1577
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1578
					$dhhostname = str_replace(".", "_", $dhhostname);
1579
					$dhcpdv6conf .= "	option host-name {$dhhostname};\n";
1580
				}
1581 61e047a5 Phil Davis
				if ($sm['filename']) {
1582 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	filename \"{$sm['filename']}\";\n";
1583 61e047a5 Phil Davis
				}
1584 a2578c27 Anthony Wrather
1585 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1586 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	option root-path \"{$sm['rootpath']}\";\n";
1587 61e047a5 Phil Davis
				}
1588 693833cb Seth Mos
1589
				$dhcpdv6conf .= "}\n";
1590
				$i++;
1591
			}
1592
		}
1593 107e8acc Ovidiu Predescu
1594 391d63da Renato Botelho
		if ($dhcpv6ifconf['ddnsdomain']) {
1595 87019fc4 Andres Petralli
			$dhcpdv6conf .= dhcpdkey($dhcpv6ifconf);
1596 7c1747a3 Chris Buechler
			$dhcpdv6conf .= dhcpdzones($ddns_zones);
1597 87019fc4 Andres Petralli
		}
1598
1599 57006646 Renato Botelho
		if ($config['dhcpdv6'][$dhcpv6if]['ramode'] <> "unmanaged" && isset($config['interfaces'][$dhcpv6if]['enable'])) {
1600 61e047a5 Phil Davis
			if (preg_match("/poes/si", $dhcpv6if)) {
1601 e9ab2ddb smos
				/* magic here */
1602
				$dhcpdv6ifs = array_merge($dhcpdv6ifs, get_pppoes_child_interfaces($dhcpv6if));
1603
			} else {
1604 1944af41 Ermal
				$realif = get_real_interface($dhcpv6if, "inet6");
1605 029b377a Ermal
				if (stristr("$realif", "bridge")) {
1606
					$mac = get_interface_mac($realif);
1607
					$v6address = generate_ipv6_from_mac($mac);
1608
					/* Create link local address for bridges */
1609 e9ab2ddb smos
					mwexec("/sbin/ifconfig {$realif} inet6 {$v6address}");
1610
				}
1611 029b377a Ermal
				$realif = escapeshellcmd($realif);
1612
				$dhcpdv6ifs[] = $realif;
1613 656f1763 Seth Mos
			}
1614 de140730 Seth Mos
		}
1615 693833cb Seth Mos
	}
1616
1617 61e047a5 Phil Davis
	if ($nsupdate) {
1618 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style interim;\n";
1619 61e047a5 Phil Davis
	} else {
1620 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style none;\n";
1621
	}
1622
1623 abdd01f5 Ermal
	/* write dhcpdv6.conf */
1624
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf", $dhcpdv6conf)) {
1625 1c903aa4 Ermal
		log_error("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1626 61e047a5 Phil Davis
		if (platform_booting()) {
1627 1c903aa4 Ermal
			printf("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1628 61e047a5 Phil Davis
		}
1629 abdd01f5 Ermal
		unset($dhcpdv6conf);
1630
		return 1;
1631
	}
1632 928d4416 Ermal
	unset($dhcpdv6conf);
1633
1634 693833cb Seth Mos
	/* create an empty leases v6 database */
1635 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases")) {
1636 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1637 61e047a5 Phil Davis
	}
1638 107e8acc Ovidiu Predescu
1639 61e047a5 Phil Davis
	/* make sure there isn't a stale dhcpdv6.pid file, which may make dhcpdv6 fail to start.  */
1640
	/* if we get here, dhcpdv6 has been killed and is not started yet                         */
1641
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
1642 b075c1e2 Chris Buechler
1643 68a0e4fc Scott Ullrich
	/* fire up dhcpd in a chroot */
1644 abdd01f5 Ermal
	if (count($dhcpdv6ifs) > 0) {
1645 2fb056d8 Seth Mos
		mwexec("/usr/local/sbin/dhcpd -6 -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf /etc/dhcpdv6.conf -pf {$g['varrun_path']}/dhcpdv6.pid " .
1646 2a1bd027 Seth Mos
			join(" ", $dhcpdv6ifs));
1647 cb7d18d5 Renato Botelho
		mwexec("/usr/local/sbin/dhcpleases6 -c \"/usr/local/bin/php-cgi -f /usr/local/sbin/prefixes.php|/bin/sh\" -l {$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1648 2a1bd027 Seth Mos
	}
1649 61e047a5 Phil Davis
	if (platform_booting()) {
1650 f1a44a3a Carlos Eduardo Ramos
		print gettext("done.") . "\n";
1651 61e047a5 Phil Davis
	}
1652 a25183c5 Scott Ullrich
1653 5b237745 Scott Ullrich
	return 0;
1654
}
1655
1656 41997fbb Ermal Luci
function services_igmpproxy_configure() {
1657 61e047a5 Phil Davis
	global $config, $g;
1658 41997fbb Ermal Luci
1659 61e047a5 Phil Davis
	/* kill any running igmpproxy */
1660
	killbyname("igmpproxy");
1661 41997fbb Ermal Luci
1662 61e047a5 Phil Davis
	if (!is_array($config['igmpproxy']['igmpentry']) || (count($config['igmpproxy']['igmpentry']) == 0)) {
1663 41997fbb Ermal Luci
		return 1;
1664 61e047a5 Phil Davis
	}
1665 41997fbb Ermal Luci
1666 61e047a5 Phil Davis
	$iflist = get_configured_interface_list();
1667 f206afb5 Ermal
1668 61e047a5 Phil Davis
	$igmpconf = <<<EOD
1669 41997fbb Ermal Luci
1670
##------------------------------------------------------
1671
## Enable Quickleave mode (Sends Leave instantly)
1672
##------------------------------------------------------
1673
quickleave
1674
1675
EOD;
1676
1677 61e047a5 Phil Davis
	foreach ($config['igmpproxy']['igmpentry'] as $igmpcf) {
1678
		unset($iflist[$igmpcf['ifname']]);
1679
		$realif = get_real_interface($igmpcf['ifname']);
1680
		if (empty($igmpcf['threshold'])) {
1681
			$threshld = 1;
1682
		} else {
1683
			$threshld = $igmpcf['threshold'];
1684
		}
1685
		$igmpconf .= "phyint {$realif} {$igmpcf['type']} ratelimit 0 threshold {$threshld}\n";
1686
1687
		if ($igmpcf['address'] <> "") {
1688
			$item = explode(" ", $igmpcf['address']);
1689
			foreach ($item as $iww) {
1690
				$igmpconf .= "altnet {$iww}\n";
1691
			}
1692
		}
1693
		$igmpconf .= "\n";
1694
	}
1695
	foreach ($iflist as $ifn) {
1696
		$realif = get_real_interface($ifn);
1697
		$igmpconf .= "phyint {$realif} disabled\n";
1698
	}
1699 3bae60be Ermal
	$igmpconf .= "\n";
1700 41997fbb Ermal Luci
1701 61e047a5 Phil Davis
	$igmpfl = fopen($g['tmp_path'] . "/igmpproxy.conf", "w");
1702
	if (!$igmpfl) {
1703
		log_error(gettext("Could not write Igmpproxy configuration file!"));
1704
		return;
1705
	}
1706
	fwrite($igmpfl, $igmpconf);
1707
	fclose($igmpfl);
1708 928d4416 Ermal
	unset($igmpconf);
1709 41997fbb Ermal Luci
1710 61d6a840 Stephen Beaver
	if (isset($config['syslog']['igmpxverbose'])) {
1711
		mwexec_bg("/usr/local/sbin/igmpproxy -v {$g['tmp_path']}/igmpproxy.conf");
1712
	} else {
1713
		mwexec_bg("/usr/local/sbin/igmpproxy {$g['tmp_path']}/igmpproxy.conf");
1714
	}
1715
1716 61e047a5 Phil Davis
	log_error(gettext("Started IGMP proxy service."));
1717 41997fbb Ermal Luci
1718 61e047a5 Phil Davis
	return 0;
1719 41997fbb Ermal Luci
}
1720
1721 5b237745 Scott Ullrich
function services_dhcrelay_configure() {
1722 f19d3b7a Scott Ullrich
	global $config, $g;
1723 6fa9f38c Renato Botelho
1724 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1725 acd910bf Scott Ullrich
		$mt = microtime();
1726 f19d3b7a Scott Ullrich
		echo "services_dhcrelay_configure() being called $mt\n";
1727 acd910bf Scott Ullrich
	}
1728 a25183c5 Scott Ullrich
1729 5b237745 Scott Ullrich
	/* kill any running dhcrelay */
1730
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
1731 a25183c5 Scott Ullrich
1732 2f06cc3f Ermal
	$dhcrelaycfg =& $config['dhcrelay'];
1733 a25183c5 Scott Ullrich
1734 5b237745 Scott Ullrich
	/* DHCPRelay enabled on any interfaces? */
1735 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
1736 5b237745 Scott Ullrich
		return 0;
1737 61e047a5 Phil Davis
	}
1738 a25183c5 Scott Ullrich
1739 61e047a5 Phil Davis
	if (platform_booting()) {
1740 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP relay service...");
1741 61e047a5 Phil Davis
	} else {
1742 5b237745 Scott Ullrich
		sleep(1);
1743 61e047a5 Phil Davis
	}
1744 a25183c5 Scott Ullrich
1745 2f06cc3f Ermal
	$iflist = get_configured_interface_list();
1746 a25183c5 Scott Ullrich
1747 2f06cc3f Ermal
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1748
	foreach ($dhcifaces as $dhcrelayif) {
1749
		if (!isset($iflist[$dhcrelayif]) ||
1750 61e047a5 Phil Davis
		    link_interface_to_bridge($dhcrelayif)) {
1751 5b237745 Scott Ullrich
			continue;
1752 61e047a5 Phil Davis
		}
1753 a25183c5 Scott Ullrich
1754 61e047a5 Phil Davis
		if (is_ipaddr(get_interface_ip($dhcrelayif))) {
1755 2f06cc3f Ermal
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1756 61e047a5 Phil Davis
		}
1757 5b237745 Scott Ullrich
	}
1758
1759 d7827421 Renato Botelho
	/*
1760
	 * In order for the relay to work, it needs to be active
1761
	 * on the interface in which the destination server sits.
1762
	 */
1763 2f06cc3f Ermal
	$srvips = explode(",", $dhcrelaycfg['server']);
1764 c58879a9 Ermal LUÇI
	if (!is_array($srvips)) {
1765 e8c516a0 Phil Davis
		log_error(gettext("No destination IP has been configured!"));
1766 c58879a9 Ermal LUÇI
		return;
1767 4de8f7ba Phil Davis
	}
1768 a25183c5 Scott Ullrich
1769 d7827421 Renato Botelho
	foreach ($srvips as $srcidx => $srvip) {
1770
		unset($destif);
1771
		foreach ($iflist as $ifname) {
1772
			$subnet = get_interface_ip($ifname);
1773
			if (!is_ipaddr($subnet)) {
1774
				continue;
1775
			}
1776
			$subnet .= "/" . get_interface_subnet($ifname);
1777
			if (ip_in_subnet($srvip, $subnet)) {
1778
				$destif = get_real_interface($ifname);
1779
				break;
1780
			}
1781
		}
1782
		if (!isset($destif)) {
1783 25e5d826 Phil Davis
			// For each enabled static route
1784
			foreach (get_staticroutes(false, false, true) as $rtent) {
1785 d7827421 Renato Botelho
				if (ip_in_subnet($srvip, $rtent['network'])) {
1786
					$a_gateways = return_gateways_array(true);
1787
					$destif = $a_gateways[$rtent['gateway']]['interface'];
1788
					break;
1789
				}
1790
			}
1791
		}
1792
1793
		if (!isset($destif)) {
1794
			/* Create a array from the existing route table */
1795
			exec("/usr/bin/netstat -rnWf inet", $route_str);
1796
			array_shift($route_str);
1797
			array_shift($route_str);
1798
			array_shift($route_str);
1799
			array_shift($route_str);
1800
			$route_arr = array();
1801
			foreach ($route_str as $routeline) {
1802
				$items = preg_split("/[ ]+/i", $routeline);
1803
				if (is_subnetv4($items[0])) {
1804
					$subnet = $items[0];
1805
				} elseif (is_ipaddrv4($items[0])) {
1806
					$subnet = "{$items[0]}/32";
1807
				} else {
1808
					// Not a subnet or IP address, skip to the next line.
1809
					continue;
1810
				}
1811
				if (ip_in_subnet($srvip, $subnet)) {
1812
					$destif = trim($items[6]);
1813
					break;
1814
				}
1815
			}
1816
		}
1817
1818
		if (!isset($destif)) {
1819
			if (is_array($config['gateways']['gateway_item'])) {
1820
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1821
					if (isset($gateway['defaultgw'])) {
1822
						$destif = get_real_interface($gateway['interface']);
1823
						break;
1824
					}
1825
				}
1826
			} else {
1827
				$destif = get_real_interface("wan");
1828
			}
1829
		}
1830
1831
		if (!empty($destif)) {
1832
			$dhcrelayifs[] = $destif;
1833
		}
1834
	}
1835 5b237745 Scott Ullrich
	$dhcrelayifs = array_unique($dhcrelayifs);
1836
1837
	/* fire up dhcrelay */
1838 24997966 Ermal
	if (empty($dhcrelayifs)) {
1839 e8c516a0 Phil Davis
		log_error(gettext("No suitable interface found for running dhcrelay!"));
1840 24997966 Ermal
		return; /* XXX */
1841
	}
1842
1843 4de8f7ba Phil Davis
	$cmd = "/usr/local/sbin/dhcrelay -i " . implode(" -i ", $dhcrelayifs);
1844 5b237745 Scott Ullrich
1845 61e047a5 Phil Davis
	if (isset($dhcrelaycfg['agentoption'])) {
1846 4de8f7ba Phil Davis
		$cmd .= " -a -m replace";
1847 61e047a5 Phil Davis
	}
1848 5b237745 Scott Ullrich
1849 2f06cc3f Ermal
	$cmd .= " " . implode(" ", $srvips);
1850 5b237745 Scott Ullrich
	mwexec($cmd);
1851 928d4416 Ermal
	unset($cmd);
1852 a25183c5 Scott Ullrich
1853 5b237745 Scott Ullrich
	return 0;
1854
}
1855
1856 b7a15cf8 Seth Mos
function services_dhcrelay6_configure() {
1857
	global $config, $g;
1858 6fa9f38c Renato Botelho
1859 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1860 b7a15cf8 Seth Mos
		$mt = microtime();
1861 874f099a Phil Davis
		echo "services_dhcrelay6_configure() being called $mt\n";
1862 b7a15cf8 Seth Mos
	}
1863
1864
	/* kill any running dhcrelay */
1865
	killbypid("{$g['varrun_path']}/dhcrelay6.pid");
1866
1867
	$dhcrelaycfg =& $config['dhcrelay6'];
1868
1869
	/* DHCPv6 Relay enabled on any interfaces? */
1870 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
1871 b7a15cf8 Seth Mos
		return 0;
1872 61e047a5 Phil Davis
	}
1873 b7a15cf8 Seth Mos
1874 61e047a5 Phil Davis
	if (platform_booting()) {
1875 b7a15cf8 Seth Mos
		echo gettext("Starting DHCPv6 relay service...");
1876 61e047a5 Phil Davis
	} else {
1877 b7a15cf8 Seth Mos
		sleep(1);
1878 61e047a5 Phil Davis
	}
1879 b7a15cf8 Seth Mos
1880
	$iflist = get_configured_interface_list();
1881
1882
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1883
	foreach ($dhcifaces as $dhcrelayif) {
1884
		if (!isset($iflist[$dhcrelayif]) ||
1885 086cf944 Phil Davis
		    link_interface_to_bridge($dhcrelayif)) {
1886 b7a15cf8 Seth Mos
			continue;
1887 086cf944 Phil Davis
		}
1888 b7a15cf8 Seth Mos
1889 61e047a5 Phil Davis
		if (is_ipaddrv6(get_interface_ipv6($dhcrelayif))) {
1890 b7a15cf8 Seth Mos
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1891 61e047a5 Phil Davis
		}
1892 b7a15cf8 Seth Mos
	}
1893 69dd7088 Michael Tharp
	$dhcrelayifs = array_unique($dhcrelayifs);
1894 b7a15cf8 Seth Mos
1895 d7827421 Renato Botelho
	/*
1896
	 * In order for the relay to work, it needs to be active
1897
	 * on the interface in which the destination server sits.
1898
	 */
1899 b7a15cf8 Seth Mos
	$srvips = explode(",", $dhcrelaycfg['server']);
1900 d7827421 Renato Botelho
	$srvifaces = array();
1901
	foreach ($srvips as $srcidx => $srvip) {
1902
		unset($destif);
1903
		foreach ($iflist as $ifname) {
1904
			$subnet = get_interface_ipv6($ifname);
1905
			if (!is_ipaddrv6($subnet)) {
1906
				continue;
1907
			}
1908
			$subnet .= "/" . get_interface_subnetv6($ifname);
1909
			if (ip_in_subnet($srvip, $subnet)) {
1910
				$destif = get_real_interface($ifname);
1911
				break;
1912
			}
1913
		}
1914
		if (!isset($destif)) {
1915
			if (is_array($config['staticroutes']['route'])) {
1916
				foreach ($config['staticroutes']['route'] as $rtent) {
1917 25e5d826 Phil Davis
					if (isset($rtent['disabled'])) {
1918
						continue;
1919
					}
1920 d7827421 Renato Botelho
					if (ip_in_subnet($srvip, $rtent['network'])) {
1921
						$a_gateways = return_gateways_array(true);
1922
						$destif = $a_gateways[$rtent['gateway']]['interface'];
1923
						break;
1924
					}
1925
				}
1926
			}
1927
		}
1928
1929
		if (!isset($destif)) {
1930
			/* Create a array from the existing route table */
1931
			exec("/usr/bin/netstat -rnWf inet6", $route_str);
1932
			array_shift($route_str);
1933
			array_shift($route_str);
1934
			array_shift($route_str);
1935
			array_shift($route_str);
1936
			$route_arr = array();
1937
			foreach ($route_str as $routeline) {
1938
				$items = preg_split("/[ ]+/i", $routeline);
1939
				if (ip_in_subnet($srvip, $items[0])) {
1940
					$destif = trim($items[6]);
1941
					break;
1942
				}
1943
			}
1944
		}
1945
1946
		if (!isset($destif)) {
1947
			if (is_array($config['gateways']['gateway_item'])) {
1948
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1949
					if (isset($gateway['defaultgw'])) {
1950
						$destif = get_real_interface($gateway['interface']);
1951
						break;
1952
					}
1953
				}
1954
			} else {
1955
				$destif = get_real_interface("wan");
1956
			}
1957
		}
1958
1959
		if (!empty($destif)) {
1960
			$srvifaces[] = "{$srvip}%{$destif}";
1961
		}
1962 b7a15cf8 Seth Mos
	}
1963
1964
	/* fire up dhcrelay */
1965 61e047a5 Phil Davis
	if (empty($dhcrelayifs) || empty($srvifaces)) {
1966 e8c516a0 Phil Davis
		log_error(gettext("No suitable interface found for running dhcrelay -6!"));
1967 b7a15cf8 Seth Mos
		return; /* XXX */
1968
	}
1969
1970 54a9d71d Phil Davis
	$cmd = "/usr/local/sbin/dhcrelay -6 -pf \"{$g['varrun_path']}/dhcrelay6.pid\"";
1971 69dd7088 Michael Tharp
	foreach ($dhcrelayifs as $dhcrelayif) {
1972
		$cmd .= " -l {$dhcrelayif}";
1973
	}
1974
	foreach ($srvifaces as $srviface) {
1975
		$cmd .= " -u \"{$srviface}\"";
1976
	}
1977 b7a15cf8 Seth Mos
	mwexec($cmd);
1978 928d4416 Ermal
	unset($cmd);
1979 b7a15cf8 Seth Mos
1980
	return 0;
1981
}
1982
1983 181d7c95 Ermal Luçi
function services_dyndns_configure_client($conf) {
1984
1985 61e047a5 Phil Davis
	if (!isset($conf['enable'])) {
1986 65996399 Ermal
		return;
1987 61e047a5 Phil Davis
	}
1988 d2946062 Ermal
1989 181d7c95 Ermal Luçi
	/* load up the dyndns.class */
1990
	require_once("dyndns.class");
1991
1992
	$dns = new updatedns($dnsService = $conf['type'],
1993
		$dnsHost = $conf['host'],
1994 b24a0251 Chris Buechler
		$dnsDomain = $conf['domainname'],
1995 181d7c95 Ermal Luçi
		$dnsUser = $conf['username'],
1996
		$dnsPass = $conf['password'],
1997 3aa55bbe Phil Davis
		$dnsWildcard = $conf['wildcard'],
1998 23bd0f9d CarlGill
		$dnsProxied = $conf['proxied'],
1999 107e8acc Ovidiu Predescu
		$dnsMX = $conf['mx'],
2000 f3b2b2a4 Yehuda Katz
		$dnsIf = "{$conf['interface']}",
2001
		$dnsBackMX = NULL,
2002
		$dnsServer = NULL,
2003
		$dnsPort = NULL,
2004 37f3e704 Matt Corallo
		$dnsUpdateURL = "{$conf['updateurl']}",
2005 cd132e86 Edson Brandi
		$forceUpdate = $conf['force'],
2006 4de8f7ba Phil Davis
		$dnsZoneID = $conf['zoneid'],
2007
		$dnsTTL = $conf['ttl'],
2008 37f3e704 Matt Corallo
		$dnsResultMatch = "{$conf['resultmatch']}",
2009
		$dnsRequestIf = "{$conf['requestif']}",
2010 1e503870 Phil Davis
		$dnsID = "{$conf['id']}",
2011 aa79f351 Sebastian Chrostek
		$dnsVerboseLog = $conf['verboselog'],
2012
		$curlIpresolveV4 = $conf['curl_ipresolve_v4'],
2013
		$curlSslVerifypeer = $conf['curl_ssl_verifypeer']);
2014 181d7c95 Ermal Luçi
}
2015
2016 0be93267 Ermal Lu?i
function services_dyndns_configure($int = "") {
2017 f19d3b7a Scott Ullrich
	global $config, $g;
2018 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2019 59a63553 Scott Ullrich
		$mt = microtime();
2020
		echo "services_dyndns_configure() being called $mt\n";
2021
	}
2022
2023 67ee1ec5 Ermal Luçi
	$dyndnscfg = $config['dyndnses']['dyndns'];
2024 017817c2 smos
	$gwgroups = return_gateway_groups_array();
2025 67ee1ec5 Ermal Luçi
	if (is_array($dyndnscfg)) {
2026 61e047a5 Phil Davis
		if (platform_booting()) {
2027 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DynDNS clients...");
2028 61e047a5 Phil Davis
		}
2029 181d7c95 Ermal Luçi
2030 67ee1ec5 Ermal Luçi
		foreach ($dyndnscfg as $dyndns) {
2031 05741e45 Renato Botelho
			/*
2032
			 * If it's using a gateway group, check if interface is
2033
			 * the active gateway for that group
2034
			 */
2035
			$group_int = '';
2036
			if (is_array($gwgroups[$dyndns['interface']])) {
2037
				if (!empty($gwgroups[$dyndns['interface']][0]['vip'])) {
2038
					$group_int = $gwgroups[$dyndns['interface']][0]['vip'];
2039
				} else {
2040
					$group_int = $gwgroups[$dyndns['interface']][0]['int'];
2041
				}
2042
			}
2043
			if ((empty($int)) || ($int == $dyndns['interface']) || ($int == $group_int)) {
2044 1e503870 Phil Davis
				$dyndns['verboselog'] = isset($dyndns['verboselog']);
2045 d9bdc020 Sebastian Chrostek
				$dyndns['curl_ipresolve_v4'] = isset($dyndns['curl_ipresolve_v4']);
2046
				$dyndns['curl_ssl_verifypeer'] = isset($dyndns['curl_ssl_verifypeer']);
2047 768eb89c smos
				services_dyndns_configure_client($dyndns);
2048
				sleep(1);
2049
			}
2050 67ee1ec5 Ermal Luçi
		}
2051 59a63553 Scott Ullrich
2052 61e047a5 Phil Davis
		if (platform_booting()) {
2053 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2054 61e047a5 Phil Davis
		}
2055 59a63553 Scott Ullrich
	}
2056
2057
	return 0;
2058
}
2059
2060 0e3aeb6b Phil Davis
function dyndnsCheckIP($int) {
2061 b89bc607 Phil Davis
	global $config, $factory_default_checkipservice;
2062 0e3aeb6b Phil Davis
	$ip_address = get_interface_ip($int);
2063
	if (is_private_ip($ip_address)) {
2064 873e4b28 phildd
		$gateways_status = return_gateways_status(true);
2065
		// If the gateway for this interface is down, then the external check cannot work.
2066
		// Avoid the long wait for the external check to timeout.
2067 61e047a5 Phil Davis
		if (stristr($gateways_status[$config['interfaces'][$int]['gateway']]['status'], "down")) {
2068 873e4b28 phildd
			return "down";
2069 61e047a5 Phil Davis
		}
2070 b89bc607 Phil Davis
2071
		// Append the factory default check IP service to the list (if not disabled).
2072
		if (!isset($config['checkipservices']['disable_factory_default'])) {
2073
			$config['checkipservices']['checkipservice'][] = $factory_default_checkipservice;
2074
		}
2075
2076
		// Use the first enabled check IP service as the default.
2077
		if (is_array($config['checkipservices']['checkipservice'])) {
2078
			foreach ($config['checkipservices']['checkipservice'] as $i => $checkipservice) {
2079
				if (isset($checkipservice['enable'])) {
2080
					$url = $checkipservice['url'];
2081
					$username = $checkipservice['username'];
2082
					$password = $checkipservice['password'];
2083
					$verifysslpeer = isset($checkipservice['verifysslpeer']);
2084
					break;
2085
				}
2086
			}
2087
		}
2088
2089
		$hosttocheck = $url;
2090 5244c510 Florian Asche
		$ip_ch = curl_init($hosttocheck);
2091 0e3aeb6b Phil Davis
		curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
2092 b89bc607 Phil Davis
		curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, $verifysslpeer);
2093 e1eee3d2 Renato Botelho
		curl_setopt($ip_ch, CURLOPT_INTERFACE, 'host!' . $ip_address);
2094 181386d6 Florian Asche
		curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
2095
		curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
2096 3c6f29c0 Florian Asche
		curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
2097 b89bc607 Phil Davis
		curl_setopt($ip_ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
2098
		curl_setopt($ip_ch, CURLOPT_USERPWD, "{$username}:{$password}");
2099 0e3aeb6b Phil Davis
		$ip_result_page = curl_exec($ip_ch);
2100
		curl_close($ip_ch);
2101
		$ip_result_decoded = urldecode($ip_result_page);
2102
		preg_match('=Current IP Address: (.*)</body>=siU', $ip_result_decoded, $matches);
2103
		$ip_address = trim($matches[1]);
2104
	}
2105
	return $ip_address;
2106
}
2107
2108 57b5da70 jim-p
function services_dnsmasq_configure($restart_dhcp = true) {
2109 f19d3b7a Scott Ullrich
	global $config, $g;
2110 6a01ea44 Bill Marquette
	$return = 0;
2111 107e8acc Ovidiu Predescu
2112 683992fc stilez
	// hard coded args: will be removed to avoid duplication if specified in custom_options
2113
	$standard_args = array(
2114
		"dns-forward-max" => "--dns-forward-max=5000",
2115
		"cache-size" => "--cache-size=10000",
2116
		"local-ttl" => "--local-ttl=1"
2117
	);
2118
2119
2120 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2121 acd910bf Scott Ullrich
		$mt = microtime();
2122 f19d3b7a Scott Ullrich
		echo "services_dnsmasq_configure() being called $mt\n";
2123 acd910bf Scott Ullrich
	}
2124
2125 5b237745 Scott Ullrich
	/* kill any running dnsmasq */
2126 61e047a5 Phil Davis
	if (file_exists("{$g['varrun_path']}/dnsmasq.pid")) {
2127 d224df18 Ermal
		sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
2128 61e047a5 Phil Davis
	}
2129 5b237745 Scott Ullrich
2130
	if (isset($config['dnsmasq']['enable'])) {
2131 a25183c5 Scott Ullrich
2132 61e047a5 Phil Davis
		if (platform_booting()) {
2133 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DNS forwarder...");
2134 61e047a5 Phil Davis
		} else {
2135 5b237745 Scott Ullrich
			sleep(1);
2136 61e047a5 Phil Davis
		}
2137 5b237745 Scott Ullrich
2138 61e047a5 Phil Davis
		/* generate hosts file */
2139 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2140 61e047a5 Phil Davis
			$return = 1;
2141
		}
2142 cbc6a13f Chris Buechler
2143 5b237745 Scott Ullrich
		$args = "";
2144 a25183c5 Scott Ullrich
2145 0261381a Ermal
		if (isset($config['dnsmasq']['regdhcp'])) {
2146
			$args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts ";
2147
		}
2148 107e8acc Ovidiu Predescu
2149 e6c49e3d jim-p
		/* Setup listen port, if non-default */
2150 61e047a5 Phil Davis
		if (is_port($config['dnsmasq']['port'])) {
2151 e6c49e3d jim-p
			$args .= " --port={$config['dnsmasq']['port']} ";
2152 61e047a5 Phil Davis
		}
2153 e6c49e3d jim-p
2154 b4323f39 jim-p
		$listen_addresses = "";
2155 61e047a5 Phil Davis
		if (isset($config['dnsmasq']['interface'])) {
2156 b4323f39 jim-p
			$interfaces = explode(",", $config['dnsmasq']['interface']);
2157
			foreach ($interfaces as $interface) {
2158 2a5960b0 Luiz Otavio O Souza
				$if = get_real_interface($interface);
2159
				if (does_interface_exist($if)) {
2160
					$laddr = get_interface_ip($interface);
2161
					if (is_ipaddrv4($laddr)) {
2162 9e7e2c94 Chris Buechler
						$listen_addresses .= " --listen-address={$laddr} ";
2163 61e047a5 Phil Davis
					}
2164 2a5960b0 Luiz Otavio O Souza
					$laddr6 = get_interface_ipv6($interface);
2165
					if (is_ipaddrv6($laddr6) && !isset($config['dnsmasq']['strictbind'])) {
2166
						/*
2167
						 * XXX: Since dnsmasq does not support link-local address
2168
						 * with scope specified. These checks are being done.
2169
						 */
2170
						if (is_linklocal($laddr6) && strstr($laddr6, "%")) {
2171
							$tmpaddrll6 = explode("%", $laddr6);
2172
							$listen_addresses .= " --listen-address={$tmpaddrll6[0]} ";
2173
						} else {
2174
							$listen_addresses .= " --listen-address={$laddr6} ";
2175 b77d19c7 Ermal
						}
2176 b4323f39 jim-p
					}
2177
				}
2178
			}
2179
			if (!empty($listen_addresses)) {
2180
				$args .= " {$listen_addresses} ";
2181 61e047a5 Phil Davis
				if (isset($config['dnsmasq']['strictbind'])) {
2182 b4323f39 jim-p
					$args .= " --bind-interfaces ";
2183 61e047a5 Phil Davis
				}
2184 b4323f39 jim-p
			}
2185
		}
2186
2187 fc27d3f4 Phil Davis
		/* If selected, then first forward reverse lookups for private IPv4 addresses to nowhere. */
2188 153613e3 Phil Davis
		/* Only make entries for reverse domains that do not have a matching domain override. */
2189 0a7985ba Phil Davis
		if (isset($config['dnsmasq']['no_private_reverse'])) {
2190
			/* Note: Carrier Grade NAT (CGN) addresses 100.64.0.0/10 are intentionally not here. */
2191
			/* End-users should not be aware of CGN addresses, so reverse lookups for these should not happen. */
2192
			/* Just the pfSense WAN might get a CGN address from an ISP. */
2193 153613e3 Phil Davis
2194
			// Build an array of domain overrides to help in checking for matches.
2195
			$override_a = array();
2196
			if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2197
				foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2198
					$override_a[$override['domain']] = "y";
2199
				}
2200 0a7985ba Phil Davis
			}
2201 153613e3 Phil Davis
2202
			// Build an array of the private reverse lookup domain names
2203
			$reverse_domain_a = array("10.in-addr.arpa", "168.192.in-addr.arpa");
2204
			// Unfortunately the 172.16.0.0/12 range does not map nicely to the in-addr.arpa scheme.
2205 61e047a5 Phil Davis
			for ($subnet_num = 16; $subnet_num < 32; $subnet_num++) {
2206 153613e3 Phil Davis
				$reverse_domain_a[] = "$subnet_num.172.in-addr.arpa";
2207
			}
2208
2209
			// Set the --server parameter to nowhere for each reverse domain name that was not specifically specified in a domain override.
2210 61e047a5 Phil Davis
			foreach ($reverse_domain_a as $reverse_domain) {
2211
				if (!isset($override_a[$reverse_domain])) {
2212 153613e3 Phil Davis
					$args .= " --server=/$reverse_domain/ ";
2213 61e047a5 Phil Davis
				}
2214 0a7985ba Phil Davis
			}
2215 153613e3 Phil Davis
			unset($override_a);
2216
			unset($reverse_domain_a);
2217 0a7985ba Phil Davis
		}
2218
2219 fc27d3f4 Phil Davis
		/* Setup forwarded domains */
2220
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2221 61e047a5 Phil Davis
			foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2222
				if ($override['ip'] == "!") {
2223 fc27d3f4 Phil Davis
					$override[ip] = "";
2224 61e047a5 Phil Davis
				}
2225 fc27d3f4 Phil Davis
				$args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
2226
			}
2227
		}
2228
2229 2c46f11f Scott Ullrich
		/* Allow DNS Rebind for forwarded domains */
2230 29721fe6 Scott Ullrich
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2231 61e047a5 Phil Davis
			if (!isset($config['system']['webgui']['nodnsrebindcheck'])) {
2232
				foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2233 30d20e7d Scott Ullrich
					$args .= ' --rebind-domain-ok=/' . $override['domain'] . '/ ';
2234
				}
2235 2c46f11f Scott Ullrich
			}
2236
		}
2237 91adc5c1 Scott Ullrich
2238 61e047a5 Phil Davis
		if (!isset($config['system']['webgui']['nodnsrebindcheck'])) {
2239 30d20e7d Scott Ullrich
			$dns_rebind = "--rebind-localhost-ok --stop-dns-rebind";
2240 61e047a5 Phil Davis
		}
2241 30d20e7d Scott Ullrich
2242 96ea7162 N0YB
		if (isset($config['dnsmasq']['strict_order'])) {
2243
			$args .= " --strict-order ";
2244
		}
2245
2246
		if (isset($config['dnsmasq']['domain_needed'])) {
2247
			$args .= " --domain-needed ";
2248
		}
2249
2250 61e047a5 Phil Davis
		if ($config['dnsmasq']['custom_options']) {
2251 683992fc stilez
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
2252 071f6059 jim-p
				$args .= " " . escapeshellarg("--{$c}");
2253 683992fc stilez
				$p = explode('=', $c);
2254 61e047a5 Phil Davis
				if (array_key_exists($p[0], $standard_args)) {
2255 683992fc stilez
					unset($standard_args[$p[0]]);
2256 61e047a5 Phil Davis
				}
2257 683992fc stilez
			}
2258 61e047a5 Phil Davis
		}
2259 41567e06 jim-p
		$args .= ' ' . implode(' ', array_values($standard_args));
2260 8f9bffbc Andrew Thompson
2261 a95e5334 jim-p
		/* run dnsmasq. Use "-C /dev/null" since we use command line args only (Issue #6730) */
2262
		$cmd = "/usr/local/sbin/dnsmasq --all-servers -C /dev/null {$dns_rebind} {$args}";
2263 b4323f39 jim-p
		//log_error("dnsmasq command: {$cmd}");
2264
		mwexec_bg($cmd);
2265 928d4416 Ermal
		unset($args);
2266 5b237745 Scott Ullrich
2267 61e047a5 Phil Davis
		system_dhcpleases_configure();
2268 0a5a8df9 Warren Baker
2269 61e047a5 Phil Davis
		if (platform_booting()) {
2270 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2271 61e047a5 Phil Davis
		}
2272 5b237745 Scott Ullrich
	}
2273 a25183c5 Scott Ullrich
2274 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2275 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2276 6a01ea44 Bill Marquette
			$return = 1;
2277 61e047a5 Phil Davis
		}
2278 5b237745 Scott Ullrich
	}
2279
2280 6a01ea44 Bill Marquette
	return $return;
2281 5b237745 Scott Ullrich
}
2282
2283 57b5da70 jim-p
function services_unbound_configure($restart_dhcp = true) {
2284 175dc861 Warren Baker
	global $config, $g;
2285
	$return = 0;
2286
2287
	if (isset($config['system']['developerspew'])) {
2288
		$mt = microtime();
2289
		echo "services_unbound_configure() being called $mt\n";
2290
	}
2291
2292 53e138c0 jim-p
	if (isset($config['unbound']['enable'])) {
2293 db3b3afb jim-p
		require_once('/etc/inc/unbound.inc');
2294
2295
		/* Stop Unbound using TERM */
2296
		if (file_exists("{$g['varrun_path']}/unbound.pid")) {
2297
			sigkillbypid("{$g['varrun_path']}/unbound.pid", "TERM");
2298
		}
2299
2300
		/* If unbound is still running, wait up to 30 seconds for it to terminate. */
2301
		for ($i=1; $i <= 30; $i++) {
2302
			if (is_process_running('unbound')) {
2303
				sleep(1);
2304
			}
2305
		}
2306
2307 61e047a5 Phil Davis
		if (platform_booting()) {
2308 5bb1c495 Warren Baker
			echo gettext("Starting DNS Resolver...");
2309 61e047a5 Phil Davis
		} else {
2310 175dc861 Warren Baker
			sleep(1);
2311 61e047a5 Phil Davis
		}
2312 175dc861 Warren Baker
2313 b3c6783f Renato Botelho
		/* generate hosts file */
2314 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2315 b3c6783f Renato Botelho
			$return = 1;
2316 61e047a5 Phil Davis
		}
2317 b3c6783f Renato Botelho
2318 175dc861 Warren Baker
		sync_unbound_service();
2319 61e047a5 Phil Davis
		if (platform_booting()) {
2320 175dc861 Warren Baker
			echo gettext("done.") . "\n";
2321 61e047a5 Phil Davis
		}
2322 0a5a8df9 Warren Baker
2323 61e047a5 Phil Davis
		system_dhcpleases_configure();
2324 db3b3afb jim-p
	} else {
2325
		/* kill Unbound since it should not be enabled */
2326
		if (file_exists("{$g['varrun_path']}/unbound.pid")) {
2327
			sigkillbypid("{$g['varrun_path']}/unbound.pid", "KILL");
2328
		}
2329 175dc861 Warren Baker
	}
2330
2331 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2332 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2333 175dc861 Warren Baker
			$return = 1;
2334 61e047a5 Phil Davis
		}
2335 175dc861 Warren Baker
	}
2336
2337
	return $return;
2338
}
2339
2340 5b237745 Scott Ullrich
function services_snmpd_configure() {
2341 f19d3b7a Scott Ullrich
	global $config, $g;
2342 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2343 acd910bf Scott Ullrich
		$mt = microtime();
2344 f19d3b7a Scott Ullrich
		echo "services_snmpd_configure() being called $mt\n";
2345
	}
2346 5b237745 Scott Ullrich
2347
	/* kill any running snmpd */
2348
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
2349 dd18038e Ermal
	sleep(2);
2350 61e047a5 Phil Davis
	if (is_process_running("bsnmpd")) {
2351 a976fa82 Scott Ullrich
		mwexec("/usr/bin/killall bsnmpd", true);
2352 61e047a5 Phil Davis
	}
2353 5b237745 Scott Ullrich
2354
	if (isset($config['snmpd']['enable'])) {
2355 a25183c5 Scott Ullrich
2356 61e047a5 Phil Davis
		if (platform_booting()) {
2357 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting SNMP daemon... ");
2358 61e047a5 Phil Davis
		}
2359 5b237745 Scott Ullrich
2360 26be03d7 jim-p
		/* Make sure a printcap file exists or else bsnmpd will log errors. See https://redmine.pfsense.org/issues/6838 */
2361
		if (!file_exists('/etc/printcap')) {
2362
			@file_put_contents('/etc/printcap', "# Empty file to prevent bsnmpd from logging errors.\n");
2363
		}
2364
2365 5b237745 Scott Ullrich
		/* generate snmpd.conf */
2366
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
2367
		if (!$fd) {
2368 4de8f7ba Phil Davis
			printf(gettext("Error: cannot open snmpd.conf in services_snmpd_configure().%s"), "\n");
2369 5b237745 Scott Ullrich
			return 1;
2370
		}
2371 a25183c5 Scott Ullrich
2372 142da8f7 John Fleming
2373 5b237745 Scott Ullrich
		$snmpdconf = <<<EOD
2374 d47a8a69 Scott Ullrich
location := "{$config['snmpd']['syslocation']}"
2375
contact := "{$config['snmpd']['syscontact']}"
2376
read := "{$config['snmpd']['rocommunity']}"
2377 142da8f7 John Fleming
2378
EOD;
2379
2380 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2381 61e047a5 Phil Davis
		if (isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])) {
2382
			$snmpdconf .= <<<EOD
2383 142da8f7 John Fleming
# write string
2384
write := "{$config['snmpd']['rwcommunity']}"
2385
2386
EOD;
2387
		}
2388
*/
2389
2390
2391 61e047a5 Phil Davis
		if (isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])) {
2392
			$snmpdconf .= <<<EOD
2393 142da8f7 John Fleming
# SNMP Trap support.
2394 dbeeb008 John Fleming
traphost := {$config['snmpd']['trapserver']}
2395
trapport := {$config['snmpd']['trapserverport']}
2396
trap := "{$config['snmpd']['trapstring']}"
2397 142da8f7 John Fleming
2398
2399
EOD;
2400
		}
2401
2402 dadf8ebb jim-p
		$platform = trim(file_get_contents('/etc/platform'));
2403 61e047a5 Phil Davis
		if (($platform == "pfSense") && ($g['product_name'] != "pfSense")) {
2404 e52cb3a4 jim-p
			$platform = $g['product_name'];
2405 61e047a5 Phil Davis
		}
2406 dadf8ebb jim-p
		$sysDescr = "{$g['product_name']} " . php_uname("n") .
2407 5779ade6 Renato Botelho
			" {$g['product_version']} {$platform} " . php_uname("s") .
2408 dadf8ebb jim-p
			" " . php_uname("r") . " " . php_uname("m");
2409 142da8f7 John Fleming
2410
		$snmpdconf .= <<<EOD
2411 d47a8a69 Scott Ullrich
system := 1     # pfSense
2412
%snmpd
2413 dadf8ebb jim-p
sysDescr			= "{$sysDescr}"
2414 d47a8a69 Scott Ullrich
begemotSnmpdDebugDumpPdus       = 2
2415
begemotSnmpdDebugSyslogPri      = 7
2416
begemotSnmpdCommunityString.0.1 = $(read)
2417 142da8f7 John Fleming
2418
EOD;
2419
2420 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2421 61e047a5 Phil Davis
		if (isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])) {
2422
			$snmpdconf .= <<<EOD
2423 142da8f7 John Fleming
begemotSnmpdCommunityString.0.2 = $(write)
2424
2425
EOD;
2426
		}
2427
*/
2428
2429 c7f44ae0 Scott Ullrich
2430 61e047a5 Phil Davis
		if (isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])) {
2431
			$snmpdconf .= <<<EOD
2432 142da8f7 John Fleming
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
2433
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
2434
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
2435
2436
EOD;
2437
		}
2438
2439
2440
		$snmpdconf .= <<<EOD
2441 d47a8a69 Scott Ullrich
begemotSnmpdCommunityDisable    = 1
2442 03ba7a0f John Fleming
2443
EOD;
2444
2445 c82b2c3f jim-p
		$bind_to_ip = "0.0.0.0";
2446 61e047a5 Phil Davis
		if (isset($config['snmpd']['bindip'])) {
2447 c82b2c3f jim-p
			if (is_ipaddr($config['snmpd']['bindip'])) {
2448
				$bind_to_ip = $config['snmpd']['bindip'];
2449
			} else {
2450
				$if = get_real_interface($config['snmpd']['bindip']);
2451 61e047a5 Phil Davis
				if (does_interface_exist($if)) {
2452 89f171b0 Ermal LUÇI
					$bind_to_ip = get_interface_ip($config['snmpd']['bindip']);
2453 61e047a5 Phil Davis
				}
2454 c82b2c3f jim-p
			}
2455 7cbad422 Scott Ullrich
		}
2456
2457 61e047a5 Phil Davis
		if (is_port($config['snmpd']['pollport'])) {
2458
			$snmpdconf .= <<<EOD
2459 7cbad422 Scott Ullrich
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
2460 03ba7a0f John Fleming
2461
EOD;
2462
2463
		}
2464
2465
		$snmpdconf .= <<<EOD
2466 d47a8a69 Scott Ullrich
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
2467
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
2468 142da8f7 John Fleming
2469 03ba7a0f John Fleming
# These are bsnmp macros not php vars.
2470 9cc8c59e Scott Ullrich
sysContact      = $(contact)
2471
sysLocation     = $(location)
2472
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
2473 142da8f7 John Fleming
2474 d47a8a69 Scott Ullrich
snmpEnableAuthenTraps = 2
2475 03ba7a0f John Fleming
2476
EOD;
2477
2478 61e047a5 Phil Davis
		if (is_array($config['snmpd']['modules'])) {
2479
			if (isset($config['snmpd']['modules']['mibii'])) {
2480 03ba7a0f John Fleming
			$snmpdconf .= <<<EOD
2481 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
2482 03ba7a0f John Fleming
2483
EOD;
2484 61e047a5 Phil Davis
			}
2485 03ba7a0f John Fleming
2486 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['netgraph'])) {
2487
				$snmpdconf .= <<<EOD
2488 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
2489
%netgraph
2490
begemotNgControlNodeName = "snmpd"
2491 03ba7a0f John Fleming
2492
EOD;
2493 61e047a5 Phil Davis
			}
2494 03ba7a0f John Fleming
2495 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['pf'])) {
2496
				$snmpdconf .= <<<EOD
2497 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
2498 95fb49e8 Seth Mos
2499
EOD;
2500 61e047a5 Phil Davis
			}
2501 95fb49e8 Seth Mos
2502 a03974a3 Renato Botelho
			if (isset($config['snmpd']['modules']['hostres'])) {
2503 c64e401c jim-p
				$snmpdconf .= <<<EOD
2504 95fb49e8 Seth Mos
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
2505
2506
EOD;
2507 61e047a5 Phil Davis
			}
2508 05036071 Renato Botelho
2509 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['bridge'])) {
2510
				$snmpdconf .= <<<EOD
2511 95fb49e8 Seth Mos
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
2512 d47a8a69 Scott Ullrich
# config must end with blank line
2513 5b237745 Scott Ullrich
2514
EOD;
2515 61e047a5 Phil Davis
			}
2516
			if (isset($config['snmpd']['modules']['ucd'])) {
2517 671914b2 jim-p
				$snmpdconf .= <<<EOD
2518
begemotSnmpdModulePath."ucd"     = "/usr/local/lib/snmp_ucd.so"
2519
2520
EOD;
2521
			}
2522 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['regex'])) {
2523 671914b2 jim-p
				$snmpdconf .= <<<EOD
2524
begemotSnmpdModulePath."regex"     = "/usr/local/lib/snmp_regex.so"
2525
2526
EOD;
2527
			}
2528 03ba7a0f John Fleming
		}
2529 5b237745 Scott Ullrich
2530
		fwrite($fd, $snmpdconf);
2531
		fclose($fd);
2532 928d4416 Ermal
		unset($snmpdconf);
2533 5b237745 Scott Ullrich
2534 853e003a Scott Ullrich
		/* run bsnmpd */
2535
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
2536 2073c2d5 Phil Davis
			" -p {$g['varrun_path']}/snmpd.pid");
2537 5b237745 Scott Ullrich
2538 61e047a5 Phil Davis
		if (platform_booting()) {
2539 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2540 61e047a5 Phil Davis
		}
2541 5b237745 Scott Ullrich
	}
2542
2543
	return 0;
2544
}
2545
2546 7c9da7be jim-p
function services_dnsupdate_process($int = "", $updatehost = "", $forced = false) {
2547 f19d3b7a Scott Ullrich
	global $config, $g;
2548 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2549 acd910bf Scott Ullrich
		$mt = microtime();
2550 f19d3b7a Scott Ullrich
		echo "services_dnsupdate_process() being called $mt\n";
2551 acd910bf Scott Ullrich
	}
2552 f19d3b7a Scott Ullrich
2553 a23d7248 Scott Ullrich
	/* Dynamic DNS updating active? */
2554 67ee1ec5 Ermal Luçi
	if (is_array($config['dnsupdates']['dnsupdate'])) {
2555 7c9da7be jim-p
		$notify_text = "";
2556 c7127a13 jim-p
		$gwgroups = return_gateway_groups_array();
2557 67ee1ec5 Ermal Luçi
		foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
2558 61e047a5 Phil Davis
			if (!isset($dnsupdate['enable'])) {
2559 2ec2a374 Ermal Lu?i
				continue;
2560 61e047a5 Phil Davis
			}
2561 05741e45 Renato Botelho
			/*
2562
			 * If it's using a gateway group, check if interface is
2563
			 * the active gateway for that group
2564
			 */
2565
			$group_int = '';
2566
			if (is_array($gwgroups[$dnsupdate['interface']])) {
2567
				if (!empty($gwgroups[$dnsupdate['interface']][0]['vip'])) {
2568
					$group_int = $gwgroups[$dnsupdate['interface']][0]['vip'];
2569
				} else {
2570
					$group_int = $gwgroups[$dnsupdate['interface']][0]['int'];
2571
				}
2572
			}
2573
			if (!empty($int) && ($int != $dnsupdate['interface']) && ($int != $group_int)) {
2574 67ee1ec5 Ermal Luçi
				continue;
2575 61e047a5 Phil Davis
			}
2576
			if (!empty($updatehost) && ($updatehost != $dnsupdate['host'])) {
2577 7c9da7be jim-p
				continue;
2578 61e047a5 Phil Davis
			}
2579 67ee1ec5 Ermal Luçi
2580 2ec2a374 Ermal Lu?i
			/* determine interface name */
2581 c7127a13 jim-p
			$if = get_failover_interface($dnsupdate['interface']);
2582 61e047a5 Phil Davis
2583
			if (isset($dnsupdate['usepublicip'])) {
2584 c7127a13 jim-p
				$wanip = dyndnsCheckIP($if);
2585 61e047a5 Phil Davis
			} else {
2586 c7127a13 jim-p
				$wanip = get_interface_ip($if);
2587 61e047a5 Phil Davis
			}
2588
2589 c7127a13 jim-p
			$wanipv6 = get_interface_ipv6($if);
2590 7c9da7be jim-p
			$cacheFile = "{$g['conf_path']}/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}.cache";
2591 877676ee Renato Botelho
			$cacheFilev6 = "{$g['conf_path']}/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}_v6.cache";
2592 7c9da7be jim-p
			$currentTime = time();
2593
2594
			if ($wanip || $wanipv6) {
2595 67ee1ec5 Ermal Luçi
				$keyname = $dnsupdate['keyname'];
2596
				/* trailing dot */
2597 61e047a5 Phil Davis
				if (substr($keyname, -1) != ".") {
2598 67ee1ec5 Ermal Luçi
					$keyname .= ".";
2599 61e047a5 Phil Davis
				}
2600 67ee1ec5 Ermal Luçi
2601
				$hostname = $dnsupdate['host'];
2602
				/* trailing dot */
2603 61e047a5 Phil Davis
				if (substr($hostname, -1) != ".") {
2604 67ee1ec5 Ermal Luçi
					$hostname .= ".";
2605 61e047a5 Phil Davis
				}
2606 67ee1ec5 Ermal Luçi
2607
				/* write private key file
2608
				   this is dumb - public and private keys are the same for HMAC-MD5,
2609
				   but nsupdate insists on having both */
2610
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
2611 8ec0a8bc jim-p
				$privkey = <<<EOD
2612 a23d7248 Scott Ullrich
Private-key-format: v1.2
2613
Algorithm: 157 (HMAC)
2614 67ee1ec5 Ermal Luçi
Key: {$dnsupdate['keydata']}
2615 a23d7248 Scott Ullrich
2616
EOD;
2617 67ee1ec5 Ermal Luçi
				fwrite($fd, $privkey);
2618
				fclose($fd);
2619
2620
				/* write public key file */
2621
				if ($dnsupdate['keytype'] == "zone") {
2622
					$flags = 257;
2623
					$proto = 3;
2624
				} else if ($dnsupdate['keytype'] == "host") {
2625
					$flags = 513;
2626
					$proto = 3;
2627
				} else if ($dnsupdate['keytype'] == "user") {
2628
					$flags = 0;
2629
					$proto = 2;
2630
				}
2631 c7f44ae0 Scott Ullrich
2632 26586f7a Ermal Lu?i
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
2633 67ee1ec5 Ermal Luçi
				fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
2634
				fclose($fd);
2635
2636
				/* generate update instructions */
2637
				$upinst = "";
2638 61e047a5 Phil Davis
				if (!empty($dnsupdate['server'])) {
2639 67ee1ec5 Ermal Luçi
					$upinst .= "server {$dnsupdate['server']}\n";
2640 61e047a5 Phil Davis
				}
2641 7c9da7be jim-p
2642 e2a059e2 Renato Botelho
				$cachedipv4 = '';
2643
				$cacheTimev4 = 0;
2644 7c9da7be jim-p
				if (file_exists($cacheFile)) {
2645
					list($cachedipv4, $cacheTimev4) = explode("|", file_get_contents($cacheFile));
2646
				}
2647 e2a059e2 Renato Botelho
				$cachedipv6 = '';
2648
				$cacheTimev6 = 0;
2649 3a92e10c Renato Botelho
				if (file_exists($cacheFilev6)) {
2650
					list($cachedipv6, $cacheTimev6) = explode("|", file_get_contents($cacheFilev6));
2651 7c9da7be jim-p
				}
2652
2653
				// 25 Days
2654
				$maxCacheAgeSecs = 25 * 24 * 60 * 60;
2655
				$need_update = false;
2656 819a603c jim-p
2657
				conf_mount_rw();
2658 fc1f4960 jim-p
				/* Update IPv4 if we have it. */
2659 ea08d2b2 Robert Nelson
				if (is_ipaddrv4($wanip) && $dnsupdate['recordtype'] != "AAAA") {
2660 7c9da7be jim-p
					if (($wanip != $cachedipv4) || (($currentTime - $cacheTimev4) > $maxCacheAgeSecs) || $forced) {
2661
						$upinst .= "update delete {$dnsupdate['host']}. A\n";
2662
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} A {$wanip}\n";
2663
						$need_update = true;
2664
					} else {
2665 e8c516a0 Phil Davis
						log_error(sprintf(gettext("phpDynDNS: Not updating %s A record because the IP address has not changed."), $dnsupdate['host']));
2666 7c9da7be jim-p
					}
2667 61e047a5 Phil Davis
				} else {
2668 7c9da7be jim-p
					@unlink($cacheFile);
2669 3a92e10c Renato Botelho
					unset($cacheFile);
2670 61e047a5 Phil Davis
				}
2671 7c9da7be jim-p
2672 fc1f4960 jim-p
				/* Update IPv6 if we have it. */
2673 ea08d2b2 Robert Nelson
				if (is_ipaddrv6($wanipv6) && $dnsupdate['recordtype'] != "A") {
2674 7c9da7be jim-p
					if (($wanipv6 != $cachedipv6) || (($currentTime - $cacheTimev6) > $maxCacheAgeSecs) || $forced) {
2675
						$upinst .= "update delete {$dnsupdate['host']}. AAAA\n";
2676
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} AAAA {$wanipv6}\n";
2677
						$need_update = true;
2678
					} else {
2679 e8c516a0 Phil Davis
						log_error(sprintf(gettext("phpDynDNS: Not updating %s AAAA record because the IPv6 address has not changed."), $dnsupdate['host']));
2680 7c9da7be jim-p
					}
2681 61e047a5 Phil Davis
				} else {
2682 3a92e10c Renato Botelho
					@unlink($cacheFilev6);
2683
					unset($cacheFilev6);
2684 61e047a5 Phil Davis
				}
2685 819a603c jim-p
				conf_mount_ro();
2686 67ee1ec5 Ermal Luçi
2687 7c9da7be jim-p
				$upinst .= "\n";	/* mind that trailing newline! */
2688 107e8acc Ovidiu Predescu
2689 7c9da7be jim-p
				if ($need_update) {
2690
					@file_put_contents("{$g['varetc_path']}/nsupdatecmds{$i}", $upinst);
2691
					unset($upinst);
2692
					/* invoke nsupdate */
2693 22cc6582 Renato Botelho
					$cmd = "/usr/local/bin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
2694 61e047a5 Phil Davis
					if (isset($dnsupdate['usetcp'])) {
2695 7c9da7be jim-p
						$cmd .= " -v";
2696 61e047a5 Phil Davis
					}
2697 7c9da7be jim-p
					$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
2698 3a92e10c Renato Botelho
					if (mwexec($cmd) == 0) {
2699
						if (!empty($cacheFile)) {
2700
							@file_put_contents($cacheFile, "{$wanip}|{$currentTime}");
2701
							log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFile, $wanip));
2702
							$notify_text .= sprintf(gettext('DynDNS updated IP Address (A) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanip) . "\n";
2703
						}
2704
						if (!empty($cacheFilev6)) {
2705
							@file_put_contents($cacheFilev6, "{$wanipv6}|{$currentTime}");
2706
							log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFilev6, $wanipv6));
2707
							$notify_text .= sprintf(gettext('DynDNS updated IPv6 Address (AAAA) for %1$s on %2$s (%3$s) to %4$s'), $dnsupdate['host'], convert_real_interface_to_friendly_descr($if), $if, $wanipv6) . "\n";
2708
						}
2709
					} else {
2710
						if (!empty($cacheFile)) {
2711
							log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (A) for %1$s (%2$s)'), $dnsupdate['host'], $wanip));
2712
						}
2713
						if (!empty($cacheFilev6)) {
2714
							log_error(sprintf(gettext('phpDynDNS: ERROR while updating IP Address (AAAA) for %1$s (%2$s)'), $dnsupdate['host'], $wanipv6));
2715
						}
2716
					}
2717 7c9da7be jim-p
					unset($cmd);
2718
				}
2719 67ee1ec5 Ermal Luçi
			}
2720 a23d7248 Scott Ullrich
		}
2721 7c9da7be jim-p
		if (!empty($notify_text)) {
2722
			notify_all_remote($notify_text);
2723
		}
2724 a23d7248 Scott Ullrich
	}
2725 c7f44ae0 Scott Ullrich
2726 a23d7248 Scott Ullrich
	return 0;
2727 5b237745 Scott Ullrich
}
2728
2729 1071e028 Scott Ullrich
/* configure cron service */
2730
function configure_cron() {
2731
	global $g, $config;
2732 e7d3fc15 Ermal
2733 251ca022 Scott Ullrich
	conf_mount_rw();
2734 1071e028 Scott Ullrich
	/* preserve existing crontab entries */
2735 e7d3fc15 Ermal
	$crontab_contents = file("/etc/crontab", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
2736 107e8acc Ovidiu Predescu
2737 e7d3fc15 Ermal
	for ($i = 0; $i < count($crontab_contents); $i++) {
2738
		$cron_item =& $crontab_contents[$i];
2739
		if (strpos($cron_item, "# pfSense specific crontab entries") !== false) {
2740
			array_splice($crontab_contents, $i - 1);
2741 1071e028 Scott Ullrich
			break;
2742
		}
2743
	}
2744 e7d3fc15 Ermal
	$crontab_contents = implode("\n", $crontab_contents) . "\n";
2745 107e8acc Ovidiu Predescu
2746
2747 1071e028 Scott Ullrich
	if (is_array($config['cron']['item'])) {
2748
		$crontab_contents .= "#\n";
2749 7eea2e5f Renato Botelho
		$crontab_contents .= "# pfSense specific crontab entries\n";
2750 61e047a5 Phil Davis
		$crontab_contents .= "# " .gettext("Created:") . " " . date("F j, Y, g:i a") . "\n";
2751 1071e028 Scott Ullrich
		$crontab_contents .= "#\n";
2752
2753 992f60d0 Renato Botelho
		if (isset($config['system']['proxyurl']) && !empty($config['system']['proxyurl'])) {
2754
			$http_proxy = $config['system']['proxyurl'];
2755 61e047a5 Phil Davis
			if (isset($config['system']['proxyport']) && !empty($config['system']['proxyport'])) {
2756 992f60d0 Renato Botelho
				$http_proxy .= ':' . $config['system']['proxyport'];
2757 61e047a5 Phil Davis
			}
2758 992f60d0 Renato Botelho
			$crontab_contents .= "HTTP_PROXY={$http_proxy}";
2759
		}
2760
2761 1071e028 Scott Ullrich
		foreach ($config['cron']['item'] as $item) {
2762
			$crontab_contents .= "\n{$item['minute']}\t";
2763
			$crontab_contents .= "{$item['hour']}\t";
2764
			$crontab_contents .= "{$item['mday']}\t";
2765
			$crontab_contents .= "{$item['month']}\t";
2766
			$crontab_contents .= "{$item['wday']}\t";
2767
			$crontab_contents .= "{$item['who']}\t";
2768
			$crontab_contents .= "{$item['command']}";
2769
		}
2770 107e8acc Ovidiu Predescu
2771 1071e028 Scott Ullrich
		$crontab_contents .= "\n#\n";
2772 f1a44a3a Carlos Eduardo Ramos
		$crontab_contents .= "# " . gettext("If possible do not add items to this file manually.") . "\n";
2773 1c92c5b1 Stephen Beaver
		$crontab_contents .= "# " . gettext("If done so, this file must be terminated with a blank line (e.g. new line)") . "\n";
2774 1071e028 Scott Ullrich
		$crontab_contents .= "#\n\n";
2775
	}
2776 107e8acc Ovidiu Predescu
2777 1071e028 Scott Ullrich
	/* please maintain the newline at the end of file */
2778
	file_put_contents("/etc/crontab", $crontab_contents);
2779 c2d97111 Ermal
	unset($crontab_contents);
2780 41d507a5 Scott Ullrich
2781 8fe38524 doktornotor
	/* make sure that cron is running and start it if it got killed somehow */
2782
	if (!is_process_running("cron")) {
2783
		exec("cd /tmp && /usr/sbin/cron -s 2>/dev/null");
2784
	} else {
2785 41d507a5 Scott Ullrich
	/* do a HUP kill to force sync changes */
2786 c0020b97 doktornotor
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
2787 8fe38524 doktornotor
	}
2788 41d507a5 Scott Ullrich
2789 6d6bca27 Ermal Lu?i
	conf_mount_ro();
2790 1071e028 Scott Ullrich
}
2791
2792 431484c8 Ryan Wagoner
function upnp_action ($action) {
2793 aa6798c0 Scott Ullrich
	global $g, $config;
2794 61e047a5 Phil Davis
	switch ($action) {
2795 431484c8 Ryan Wagoner
		case "start":
2796 c1ac2424 Ermal
			if (file_exists('/var/etc/miniupnpd.conf')) {
2797
				@unlink("{$g['varrun_path']}/miniupnpd.pid");
2798
				mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P {$g['varrun_path']}/miniupnpd.pid");
2799
			}
2800 431484c8 Ryan Wagoner
			break;
2801
		case "stop":
2802 c1ac2424 Ermal
			killbypid("{$g['varrun_path']}/miniupnpd.pid");
2803 61e047a5 Phil Davis
			while ((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0) {
2804 3459814a doktornotor
				mwexec('/usr/bin/killall miniupnpd 2>/dev/null', true);
2805 61e047a5 Phil Davis
			}
2806 431484c8 Ryan Wagoner
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
2807
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
2808
			break;
2809
		case "restart":
2810
			upnp_action('stop');
2811
			upnp_action('start');
2812
			break;
2813
	}
2814
}
2815
2816 6f20377b Scott Ullrich
function upnp_start() {
2817 dcc897e5 Ermal
	global $config;
2818 0c331f1e Ermal Lu?i
2819 61e047a5 Phil Davis
	if (!isset($config['installedpackages']['miniupnpd']['config'])) {
2820 0c331f1e Ermal Lu?i
		return;
2821 61e047a5 Phil Davis
	}
2822 0c331f1e Ermal Lu?i
2823 61e047a5 Phil Davis
	if ($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
2824 54bdff75 Vinicius Coque
		echo gettext("Starting UPnP service... ");
2825 dcc897e5 Ermal
		require_once('/usr/local/pkg/miniupnpd.inc');
2826
		sync_package_miniupnpd();
2827
		echo "done.\n";
2828 6f20377b Scott Ullrich
	}
2829
}
2830
2831 4de8f7ba Phil Davis
function install_cron_job($command, $active = false, $minute = "0", $hour = "*", $monthday = "*", $month = "*", $weekday = "*", $who = "root") {
2832 85405c11 jim-p
	global $config, $g;
2833
2834
	$is_installed = false;
2835 4de8f7ba Phil Davis
	$cron_changed = true;
2836 85405c11 jim-p
2837 61e047a5 Phil Davis
	if (!is_array($config['cron'])) {
2838 c2d97111 Ermal
		$config['cron'] = array();
2839 61e047a5 Phil Davis
	}
2840
	if (!is_array($config['cron']['item'])) {
2841 c2d97111 Ermal
		$config['cron']['item'] = array();
2842 61e047a5 Phil Davis
	}
2843 85405c11 jim-p
2844 4de8f7ba Phil Davis
	$x = 0;
2845 61e047a5 Phil Davis
	foreach ($config['cron']['item'] as $item) {
2846
		if (strstr($item['command'], $command)) {
2847 85405c11 jim-p
			$is_installed = true;
2848
			break;
2849
		}
2850
		$x++;
2851
	}
2852
2853 61e047a5 Phil Davis
	if ($active) {
2854 85405c11 jim-p
		$cron_item = array();
2855
		$cron_item['minute'] = $minute;
2856
		$cron_item['hour'] = $hour;
2857
		$cron_item['mday'] = $monthday;
2858
		$cron_item['month'] = $month;
2859
		$cron_item['wday'] = $weekday;
2860
		$cron_item['who'] = $who;
2861
		$cron_item['command'] = $command;
2862 61e047a5 Phil Davis
		if (!$is_installed) {
2863 85405c11 jim-p
			$config['cron']['item'][] = $cron_item;
2864 f1a44a3a Carlos Eduardo Ramos
			write_config(sprintf(gettext("Installed cron job for %s"), $command));
2865 85405c11 jim-p
		} else {
2866 4de8f7ba Phil Davis
			if ($config['cron']['item'][$x] == $cron_item) {
2867
				$cron_changed = false;
2868
				log_error(sprintf(gettext("Checked cron job for %s, no change needed"), $command));
2869
			} else {
2870
				$config['cron']['item'][$x] = $cron_item;
2871
				write_config(sprintf(gettext("Updated cron job for %s"), $command));
2872
			}
2873 85405c11 jim-p
		}
2874
	} else {
2875 61e047a5 Phil Davis
		if ($is_installed == true) {
2876 85405c11 jim-p
			unset($config['cron']['item'][$x]);
2877 149ed85e bcyrill
			write_config(sprintf(gettext("Removed cron job for %s"), $command));
2878 85405c11 jim-p
		}
2879
	}
2880 994a0644 Phil Davis
2881 61e047a5 Phil Davis
	if ($cron_changed) {
2882 994a0644 Phil Davis
		configure_cron();
2883 61e047a5 Phil Davis
	}
2884 85405c11 jim-p
}
2885
2886 693833cb Seth Mos
?>