Project

General

Profile

Download (87.7 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 4a77c4ea Ionut
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 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,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 8fa56d1f Scott Ullrich
			} else {
651 0e93097a Scott Ullrich
				$my_port = "519";
652
				$peer_port = "520";
653 8fa56d1f Scott Ullrich
				$type = "primary";
654 4de8f7ba Phil Davis
				$dhcpdconf_pri = "split 128;\n";
655 1a0bb737 Scott Ullrich
				$dhcpdconf_pri .= "  mclt 600;\n";
656 8fa56d1f Scott Ullrich
			}
657 a01f8bfc Ermal
658
			if (is_ipaddrv4($intip)) {
659 61e047a5 Phil Davis
				$dhcpdconf .= <<<EOPP
660 d5e4f7c9 jim-p
failover peer "dhcp_{$dhcpif}" {
661 8fa56d1f Scott Ullrich
  {$type};
662
  address {$intip};
663 0e93097a Scott Ullrich
  port {$my_port};
664 8fa56d1f Scott Ullrich
  peer address {$dhcpifconf['failover_peerip']};
665 0e93097a Scott Ullrich
  peer port {$peer_port};
666 2cd5ce14 Scott Ullrich
  max-response-delay 10;
667 b865d178 Scott Ullrich
  max-unacked-updates 10;
668
  {$dhcpdconf_pri}
669 b259d1c6 Scott Ullrich
  load balance max seconds 3;
670 8fa56d1f Scott Ullrich
}
671 959dc96b Chris Buechler
\n
672 8fa56d1f Scott Ullrich
EOPP;
673 a01f8bfc Ermal
			}
674 8fa56d1f Scott Ullrich
		}
675
	}
676
677 5b237745 Scott Ullrich
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
678 a25183c5 Scott Ullrich
679 3df2dbfd jim-p
		$newzone = array();
680 5b237745 Scott Ullrich
		$ifcfg = $config['interfaces'][$dhcpif];
681 a25183c5 Scott Ullrich
682 61e047a5 Phil Davis
		if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif])) {
683 5b237745 Scott Ullrich
			continue;
684 61e047a5 Phil Davis
		}
685 a55e9c70 Ermal Lu?i
		$ifcfgip = get_interface_ip($dhcpif);
686
		$ifcfgsn = get_interface_subnet($dhcpif);
687
		$subnet = gen_subnet($ifcfgip, $ifcfgsn);
688
		$subnetmask = gen_subnet_mask($ifcfgsn);
689 a25183c5 Scott Ullrich
690 61e047a5 Phil Davis
		if (!is_ipaddr($subnet)) {
691 85e3f445 Ermal
			continue;
692 61e047a5 Phil Davis
		}
693 85e3f445 Ermal
694 61e047a5 Phil Davis
		if ($is_olsr_enabled == true) {
695
			if ($dhcpifconf['netmask']) {
696 9a537862 Scott Ullrich
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
697 61e047a5 Phil Davis
			}
698
		}
699 48ab0cd2 Scott Ullrich
700 cba980f6 jim-p
		$all_pools = array();
701
		$all_pools[] = $dhcpifconf;
702
		if (is_array($dhcpifconf['pool'])) {
703
			$all_pools = array_merge($all_pools, $dhcpifconf['pool']);
704
		}
705
706 5b237745 Scott Ullrich
		$dnscfg = "";
707 a25183c5 Scott Ullrich
708 5b237745 Scott Ullrich
		if ($dhcpifconf['domain']) {
709
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
710
		}
711 107e8acc Ovidiu Predescu
712 61e047a5 Phil Davis
		if ($dhcpifconf['domainsearchlist'] <> "") {
713 a3de8b9e Pierre POMES
			$dnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpifconf['domainsearchlist'])) . "\";\n";
714 84931046 jim-p
		}
715 9be23653 Martin Fuchs
716 4e9cd828 Seth Mos
		if (isset($dhcpifconf['ddnsupdate'])) {
717 3df2dbfd jim-p
			$need_ddns_updates = true;
718
			$newzone = array();
719 61e047a5 Phil Davis
			if ($dhcpifconf['ddnsdomain'] <> "") {
720 3df2dbfd jim-p
				$newzone['domain-name'] = $dhcpifconf['ddnsdomain'];
721 4e9cd828 Seth Mos
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
722 3df2dbfd jim-p
			} else {
723
				$newzone['domain-name'] = $config['system']['domain'];
724
			}
725 cf688344 Renato Botelho
726
			$revsubnet = array_reverse(explode('.',$subnet));
727
728
			/* Take care of full classes first */
729
			switch ($ifcfgsn) {
730
				case 8:
731
					$start_octet = 3;
732
					break;
733
				case 16:
734
					$start_octet = 2;
735
					break;
736
				case 24:
737
					$start_octet = 1;
738 3df2dbfd jim-p
					break;
739 cf688344 Renato Botelho
				default:
740
					$start_octet = 0;
741
					/* Add subnet bitmask to first octet */
742
					$revsubnet[0] .= '-' . $ifcfgsn;
743
					break;
744
745
			}
746
747
			$ptr_domain = '';
748
			for ($octet = 0; $octet <= 3; $octet++) {
749
				if ($octet < $start_octet) {
750
					continue;
751 61e047a5 Phil Davis
				}
752 8ed54652 Chris Buechler
				$ptr_domain .= ((empty($ptr_domain) && $ptr_domain !== "0") ? '' : '.');
753 cf688344 Renato Botelho
				$ptr_domain .= $revsubnet[$octet];
754 4e9cd828 Seth Mos
			}
755 cf688344 Renato Botelho
			$ptr_domain .= ".in-addr.arpa";
756
			$newzone['ptr-domain'] = $ptr_domain;
757
			unset($ptr_domain, $revsubnet, $start_octet);
758 4e9cd828 Seth Mos
		}
759
760 aff9d6ab Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
761 8ee01642 Scott Ullrich
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
762 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
763 3df2dbfd jim-p
				$newzone['dns-servers'] = $dhcpifconf['dnsserver'];
764 61e047a5 Phil Davis
			}
765 aff9d6ab Scott Ullrich
		} else if (isset($config['dnsmasq']['enable'])) {
766 a55e9c70 Ermal Lu?i
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
767 61e047a5 Phil Davis
			if ($newzone['domain-name'] && is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
768 3df2dbfd jim-p
				$newzone['dns-servers'] = $syscfg['dnsserver'];
769 61e047a5 Phil Davis
			}
770 88a0937d Chris Buechler
		} else if (isset($config['unbound']['enable'])) {
771
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
772 3ad6b569 Phil Davis
		} else if (!empty($dns_arrv4)) {
773
			$dnscfg .= "	option domain-name-servers " . join(",", $dns_arrv4) . ";";
774 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
775 3ad6b569 Phil Davis
				$newzone['dns-servers'] = $dns_arrv4;
776 61e047a5 Phil Davis
			}
777 aff9d6ab Scott Ullrich
		}
778
779 61e047a5 Phil Davis
		/* Create classes - These all contain comma separated lists. Join them into one
780 cba980f6 jim-p
		   big comma separated string then split them all up. */
781
		$all_mac_strings = array();
782
		if (is_array($dhcpifconf['pool'])) {
783 61e047a5 Phil Davis
			foreach ($all_pools as $poolconf) {
784 cba980f6 jim-p
				$all_mac_strings[] = $poolconf['mac_allow'];
785
				$all_mac_strings[] = $poolconf['mac_deny'];
786
			}
787
		}
788
		$all_mac_strings[] = $dhcpifconf['mac_allow'];
789
		$all_mac_strings[] = $dhcpifconf['mac_deny'];
790 7fd93993 Ermal LUÇI
		if (!empty($all_mac_strings)) {
791 4de8f7ba Phil Davis
			$all_mac_list = array_unique(explode(',', implode(',', $all_mac_strings)));
792
			foreach ($all_mac_list as $mac) {
793
				if (empty($mac)) {
794
					continue;
795
				}
796
				$dhcpdconf .= 'class "' . str_replace(':', '', $mac) . '" {' . "\n";
797
				// Skip the first octet of the MAC address - for media type, typically Ethernet ("01") and match the rest.
798
				$dhcpdconf .= '	match if substring (hardware, 1, ' . (substr_count($mac, ':') + 1) . ') = ' . $mac . ';' . "\n";
799
				$dhcpdconf .= '}' . "\n";
800 61e047a5 Phil Davis
			}
801 1f1a08c8 jim-p
		}
802
803 85e3f445 Ermal
		$dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n";
804 c7f44ae0 Scott Ullrich
805 61e047a5 Phil Davis
		// Setup pool options
806 dc13f5e0 Phil Davis
		foreach ($all_pools as $all_pools_idx => $poolconf) {
807 9228166f Phil Davis
			if (!(ip_in_subnet($poolconf['range']['from'], "{$subnet}/{$ifcfgsn}") && ip_in_subnet($poolconf['range']['to'], "{$subnet}/{$ifcfgsn}"))) {
808
				// If the user has changed the subnet from the interfaces page and applied,
809
				// but has not updated the DHCP range, then the range to/from of the pool can be outside the subnet.
810 f8cc55bf Phil Davis
				// This can also happen when implementing the batch of changes when the setup wizard reloads the new settings.
811 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);
812 f8cc55bf Phil Davis
				$do_file_notice = true;
813
				$conf_ipv4_address = $ifcfg['ipaddr'];
814
				$conf_ipv4_subnetmask = $ifcfg['subnet'];
815
				if (is_ipaddrv4($conf_ipv4_address) && is_subnet("{$conf_ipv4_address}/{$conf_ipv4_subnetmask}")) {
816
					$conf_subnet_base = gen_subnet($conf_ipv4_address, $conf_ipv4_subnetmask);
817
					if (ip_in_subnet($poolconf['range']['from'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}") &&
818
					    ip_in_subnet($poolconf['range']['to'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}")) {
819
						// Even though the running interface subnet does not match the pool range,
820
						// the interface subnet in the config file contains the pool range.
821
						// We are somewhere part-way through a settings reload, e.g. after running the setup wizard.
822
						// services_dhcpdv4_configure will be called again later when the new interface settings from
823
						// the config are applied and at that time everything will match up.
824
						// Ignore this pool on this interface for now and just log the error to the system log.
825
						log_error($error_msg);
826
						$do_file_notice = false;
827
					}
828
				}
829
				if ($do_file_notice) {
830
					file_notice("DHCP", $error_msg);
831
				}
832 9228166f Phil Davis
				continue;
833
			}
834 cba980f6 jim-p
			$dhcpdconf .= "	pool {\n";
835
			/* is failover dns setup? */
836
			if (is_array($poolconf['dnsserver']) && $poolconf['dnsserver'][0] <> "") {
837
				$dhcpdconf .= "		option domain-name-servers {$poolconf['dnsserver'][0]}";
838 61e047a5 Phil Davis
				if ($poolconf['dnsserver'][1] <> "") {
839 cba980f6 jim-p
					$dhcpdconf .= ",{$poolconf['dnsserver'][1]}";
840 61e047a5 Phil Davis
				}
841
				if ($poolconf['dnsserver'][2] <> "") {
842 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][2]}";
843 61e047a5 Phil Davis
				}
844
				if ($poolconf['dnsserver'][3] <> "") {
845 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][3]}";
846 61e047a5 Phil Davis
				}
847 cba980f6 jim-p
				$dhcpdconf .= ";\n";
848
			}
849
850
			/* allow/deny MACs */
851
			$mac_allow_list = array_unique(explode(',', $poolconf['mac_allow']));
852
			foreach ($mac_allow_list as $mac) {
853 61e047a5 Phil Davis
				if (empty($mac)) {
854 cba980f6 jim-p
					continue;
855 61e047a5 Phil Davis
				}
856 cba980f6 jim-p
				$dhcpdconf .= "		allow members of \"" . str_replace(':', '', $mac) . "\";\n";
857
			}
858 3475eb04 Andrew Pilloud
			$deny_action = "deny";
859 8209517d jim-p
			if (isset($poolconf['nonak']) && empty($poolconf['failover_peerip'])) {
860 3475eb04 Andrew Pilloud
				$deny_action = "ignore";
861
			}
862 cba980f6 jim-p
			$mac_deny_list = array_unique(explode(',', $poolconf['mac_deny']));
863
			foreach ($mac_deny_list as $mac) {
864 61e047a5 Phil Davis
				if (empty($mac)) {
865 cba980f6 jim-p
					continue;
866 61e047a5 Phil Davis
				}
867 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action members of \"" . str_replace(':', '', $mac) . "\";\n";
868 cba980f6 jim-p
			}
869
870 61e047a5 Phil Davis
			if ($poolconf['failover_peerip'] <> "") {
871 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action dynamic bootp clients;\n";
872 61e047a5 Phil Davis
			}
873 cba980f6 jim-p
874 61e047a5 Phil Davis
			if (isset($poolconf['denyunknown'])) {
875 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action unknown-clients;\n";
876 61e047a5 Phil Davis
			}
877 cba980f6 jim-p
878 61e047a5 Phil Davis
			if ($poolconf['gateway'] && $poolconf['gateway'] != "none" && ($poolconf['gateway'] != $dhcpifconf['gateway'])) {
879 f9f6f7d4 jim-p
				$dhcpdconf .= "		option routers {$poolconf['gateway']};\n";
880 61e047a5 Phil Davis
			}
881 cba980f6 jim-p
882 61e047a5 Phil Davis
			if ($dhcpifconf['failover_peerip'] <> "") {
883 d5e4f7c9 jim-p
				$dhcpdconf .= "		failover peer \"dhcp_{$dhcpif}\";\n";
884 cba980f6 jim-p
			}
885
886
			$pdnscfg = "";
887
888
			if ($poolconf['domain'] && ($poolconf['domain'] != $dhcpifconf['domain'])) {
889
				$pdnscfg .= "		option domain-name \"{$poolconf['domain']}\";\n";
890
			}
891
892 61e047a5 Phil Davis
			if (!empty($poolconf['domainsearchlist']) && ($poolconf['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
893 cba980f6 jim-p
				$pdnscfg .= "		option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $poolconf['domainsearchlist'])) . "\";\n";
894
			}
895
896 7309ff39 Renato Botelho
			if (isset($poolconf['ddnsupdate'])) {
897 61e047a5 Phil Davis
				if (($poolconf['ddnsdomain'] <> "") && ($poolconf['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
898 cba980f6 jim-p
					$pdnscfg .= "		ddns-domainname \"{$poolconf['ddnsdomain']}\";\n";
899 61e047a5 Phil Davis
				}
900 cba980f6 jim-p
				$pdnscfg .= "		ddns-update-style interim;\n";
901
			}
902
903
			$dhcpdconf .= "{$pdnscfg}";
904 1f1a08c8 jim-p
905 cba980f6 jim-p
			// default-lease-time
906 61e047a5 Phil Davis
			if ($poolconf['defaultleasetime'] && ($poolconf['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
907 cba980f6 jim-p
				$dhcpdconf .= "		default-lease-time {$poolconf['defaultleasetime']};\n";
908 61e047a5 Phil Davis
			}
909 cba980f6 jim-p
910
			// max-lease-time
911 61e047a5 Phil Davis
			if ($poolconf['maxleasetime'] && ($poolconf['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
912 cba980f6 jim-p
				$dhcpdconf .= "		max-lease-time {$poolconf['maxleasetime']};\n";
913 61e047a5 Phil Davis
			}
914 cba980f6 jim-p
915 0b95ff4c Jose Luis Duran
			// ignore bootp
916
			if (isset($poolconf['ignorebootp'])) {
917
				$dhcpdconf .= "		ignore bootp;\n";
918
			}
919
920 cba980f6 jim-p
			// netbios-name*
921 fa7b825f Renato Botelho
			if (is_array($poolconf['winsserver']) && $poolconf['winsserver'][0] && ($poolconf['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
922 cba980f6 jim-p
				$dhcpdconf .= "		option netbios-name-servers " . join(",", $poolconf['winsserver']) . ";\n";
923
				$dhcpdconf .= "		option netbios-node-type 8;\n";
924
			}
925 c7f44ae0 Scott Ullrich
926 cba980f6 jim-p
			// ntp-servers
927 61e047a5 Phil Davis
			if (is_array($poolconf['ntpserver']) && $poolconf['ntpserver'][0] && ($poolconf['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
928 cba980f6 jim-p
				$dhcpdconf .= "		option ntp-servers " . join(",", $poolconf['ntpserver']) . ";\n";
929 61e047a5 Phil Davis
			}
930 cba980f6 jim-p
931
			// tftp-server-name
932 61e047a5 Phil Davis
			if (!empty($poolconf['tftp']) && ($poolconf['tftp'] != $dhcpifconf['tftp'])) {
933 cba980f6 jim-p
				$dhcpdconf .= "		option tftp-server-name \"{$poolconf['tftp']}\";\n";
934 61e047a5 Phil Davis
			}
935 cba980f6 jim-p
936 dc13f5e0 Phil Davis
			// Handle pool-specific options
937
			$dhcpdconf .= "\n";
938
			// Ignore the first pool, which is the "overall" pool when $all_pools_idx is 0 - those are put outside the pool block later
939
			if ($poolconf['numberoptions']['item'] && ($all_pools_idx > 0)) {
940
				// Use the "real" pool index from the config, excluding the "overall" pool, and based from 0.
941
				// This matches the way $poolidx was used when generating the $custoptions string earlier.
942
				$poolidx = $all_pools_idx - 1;
943
				foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
944
					$item_value = base64_decode($item['value']);
945
					if (empty($item['type']) || $item['type'] == "text") {
946 34a8cebe Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} \"{$item_value}\";\n";
947 dc13f5e0 Phil Davis
					} else {
948 34a8cebe Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} {$item_value};\n";
949 dc13f5e0 Phil Davis
					}
950
				}
951
			}
952
953 cba980f6 jim-p
			// ldap-server
954 61e047a5 Phil Davis
			if (!empty($poolconf['ldap']) && ($poolconf['ldap'] != $dhcpifconf['ldap'])) {
955 cba980f6 jim-p
				$dhcpdconf .= "		option ldap-server \"{$poolconf['ldap']}\";\n";
956 61e047a5 Phil Davis
			}
957 cba980f6 jim-p
958
			// net boot information
959 61e047a5 Phil Davis
			if (isset($poolconf['netboot'])) {
960 cba980f6 jim-p
				if (!empty($poolconf['nextserver']) && ($poolconf['nextserver'] != $dhcpifconf['nextserver'])) {
961
					$dhcpdconf .= "		next-server {$poolconf['nextserver']};\n";
962
				}
963
				if (!empty($poolconf['filename']) && ($poolconf['filename'] != $dhcpifconf['filename'])) {
964
					$dhcpdconf .= "		filename \"{$poolconf['filename']}\";\n";
965
				}
966
				if (!empty($poolconf['rootpath']) && ($poolconf['rootpath'] != $dhcpifconf['rootpath'])) {
967
					$dhcpdconf .= "		option root-path \"{$poolconf['rootpath']}\";\n";
968
				}
969
			}
970
			$dhcpdconf .= "		range {$poolconf['range']['from']} {$poolconf['range']['to']};\n";
971
			$dhcpdconf .= "	}\n\n";
972
		}
973
// End of settings inside pools
974 a25183c5 Scott Ullrich
975 4208f7b1 timdufrane
		if ($dhcpifconf['gateway'] && $dhcpifconf['gateway'] != "none") {
976 5b237745 Scott Ullrich
			$routers = $dhcpifconf['gateway'];
977 c08a5659 smos
			$add_routers = true;
978 4208f7b1 timdufrane
		} elseif ($dhcpifconf['gateway'] == "none") {
979
			$add_routers = false;
980 c08a5659 smos
		} else {
981 3f141c9d Phil Davis
			$add_routers = $enable_add_routers;
982 a55e9c70 Ermal Lu?i
			$routers = $ifcfgip;
983 c08a5659 smos
		}
984 61e047a5 Phil Davis
		if ($add_routers) {
985 c08a5659 smos
			$dhcpdconf .= "	option routers {$routers};\n";
986 61e047a5 Phil Davis
		}
987 cba980f6 jim-p
988 c08a5659 smos
		$dhcpdconf .= <<<EOD
989 5b237745 Scott Ullrich
$dnscfg
990
991
EOD;
992 61e047a5 Phil Davis
		// default-lease-time
993
		if ($dhcpifconf['defaultleasetime']) {
994 5b237745 Scott Ullrich
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
995 61e047a5 Phil Davis
		}
996 518030b3 Scott Ullrich
997
		// max-lease-time
998 61e047a5 Phil Davis
		if ($dhcpifconf['maxleasetime']) {
999 5b237745 Scott Ullrich
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
1000 61e047a5 Phil Davis
		}
1001 a25183c5 Scott Ullrich
1002 518030b3 Scott Ullrich
		// netbios-name*
1003 5b237745 Scott Ullrich
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
1004
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
1005
			$dhcpdconf .= "	option netbios-node-type 8;\n";
1006
		}
1007 a25183c5 Scott Ullrich
1008 518030b3 Scott Ullrich
		// ntp-servers
1009 61e047a5 Phil Davis
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0]) {
1010 ad171999 Seth Mos
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
1011 61e047a5 Phil Davis
		}
1012 ad171999 Seth Mos
1013 518030b3 Scott Ullrich
		// tftp-server-name
1014 61e047a5 Phil Davis
		if ($dhcpifconf['tftp'] <> "") {
1015 6c23757b Martin Fuchs
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
1016 61e047a5 Phil Davis
		}
1017 6c23757b Martin Fuchs
1018 518030b3 Scott Ullrich
		// Handle option, number rowhelper values
1019
		$dhcpdconf .= "\n";
1020 61e047a5 Phil Davis
		if ($dhcpifconf['numberoptions']['item']) {
1021
			foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
1022 65cce9d7 Renato Botelho
				$item_value = base64_decode($item['value']);
1023 61e047a5 Phil Davis
				if (empty($item['type']) || $item['type'] == "text") {
1024 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} \"{$item_value}\";\n";
1025 61e047a5 Phil Davis
				} else {
1026 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} {$item_value};\n";
1027 61e047a5 Phil Davis
				}
1028 518030b3 Scott Ullrich
			}
1029
		}
1030
1031
		// ldap-server
1032 61e047a5 Phil Davis
		if ($dhcpifconf['ldap'] <> "") {
1033 6c23757b Martin Fuchs
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
1034 61e047a5 Phil Davis
		}
1035 6c23757b Martin Fuchs
1036 518030b3 Scott Ullrich
		// net boot information
1037 61e047a5 Phil Davis
		if (isset($dhcpifconf['netboot'])) {
1038 a2578c27 Anthony Wrather
			if ($dhcpifconf['nextserver'] <> "") {
1039
				$dhcpdconf .= "	next-server {$dhcpifconf['nextserver']};\n";
1040
			}
1041 fdb116a9 Donald A. Cupp Jr
			if (!empty($dhcpifconf['filename']) && !empty($dhcpifconf['filename32']) && !empty($dhcpifconf['filename64'])) {
1042
				$dhcpdconf .= "	if option arch = 00:06 {\n";
1043
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename32']}\";\n";
1044
				$dhcpdconf .= "	} else if option arch = 00:07 {\n";
1045
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename64']}\";\n";
1046 08bcd379 Phil Davis
				$dhcpdconf .= "	} else if option arch = 00:09 {\n";
1047
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename64']}\";\n";
1048 fdb116a9 Donald A. Cupp Jr
				$dhcpdconf .= "	} else {\n";
1049
				$dhcpdconf .= "		filename \"{$dhcpifconf['filename']}\";\n";
1050
				$dhcpdconf .= "	}\n\n";
1051
			} elseif (!empty($dhcpifconf['filename'])) {
1052 4e9cd828 Seth Mos
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
1053
			}
1054 fdb116a9 Donald A. Cupp Jr
			if (!empty($dhcpifconf['rootpath'])) {
1055 ca126e03 Martin Fuchs
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
1056 cba980f6 jim-p
			}
1057 4e9cd828 Seth Mos
		}
1058 107e8acc Ovidiu Predescu
1059 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
1060
}
1061
1062
EOD;
1063
1064
		/* add static mappings */
1065
		if (is_array($dhcpifconf['staticmap'])) {
1066 a25183c5 Scott Ullrich
1067 5b237745 Scott Ullrich
			$i = 0;
1068
			foreach ($dhcpifconf['staticmap'] as $sm) {
1069 449f1dd2 Will Boyce
				$dhcpdconf .= "host s_{$dhcpif}_{$i} {\n";
1070
1071 61e047a5 Phil Davis
				if ($sm['mac']) {
1072
					$dhcpdconf .= "        hardware ethernet {$sm['mac']};\n";
1073
				}
1074 449f1dd2 Will Boyce
1075 61e047a5 Phil Davis
				if ($sm['cid']) {
1076
					$dhcpdconf .= "        option dhcp-client-identifier \"{$sm['cid']}\";\n";
1077
				}
1078 5b237745 Scott Ullrich
1079 61e047a5 Phil Davis
				if ($sm['ipaddr']) {
1080 5b237745 Scott Ullrich
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
1081 61e047a5 Phil Davis
				}
1082 a25183c5 Scott Ullrich
1083 ad30055f Ermal Lu?i
				if ($sm['hostname']) {
1084
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1085 46c5b763 pierrepomes
					$dhhostname = str_replace(".", "_", $dhhostname);
1086 2f590513 smos
					$dhcpdconf .= "	option host-name \"{$dhhostname}\";\n";
1087 ad30055f Ermal Lu?i
				}
1088 61e047a5 Phil Davis
				if ($sm['filename']) {
1089 a2578c27 Anthony Wrather
					$dhcpdconf .= "	filename \"{$sm['filename']}\";\n";
1090 61e047a5 Phil Davis
				}
1091 a2578c27 Anthony Wrather
1092 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1093 a2578c27 Anthony Wrather
					$dhcpdconf .= "	option root-path \"{$sm['rootpath']}\";\n";
1094 61e047a5 Phil Davis
				}
1095 80717709 Martin Fuchs
1096 61e047a5 Phil Davis
				if ($sm['gateway'] && ($sm['gateway'] != $dhcpifconf['gateway'])) {
1097 7309ff39 Renato Botelho
					$dhcpdconf .= "	option routers {$sm['gateway']};\n";
1098 61e047a5 Phil Davis
				}
1099 7309ff39 Renato Botelho
1100
				$smdnscfg = "";
1101
1102
				if ($sm['domain'] && ($sm['domain'] != $dhcpifconf['domain'])) {
1103
					$smdnscfg .= "	option domain-name \"{$sm['domain']}\";\n";
1104
				}
1105
1106 61e047a5 Phil Davis
				if (!empty($sm['domainsearchlist']) && ($sm['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
1107 7309ff39 Renato Botelho
					$smdnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $sm['domainsearchlist'])) . "\";\n";
1108
				}
1109
1110
				if (isset($sm['ddnsupdate'])) {
1111 61e047a5 Phil Davis
					if (($sm['ddnsdomain'] <> "") && ($sm['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
1112 4c62f9e6 Phil Davis
						$smdnscfg .= "		ddns-domainname \"{$sm['ddnsdomain']}\";\n";
1113 61e047a5 Phil Davis
					}
1114 4c62f9e6 Phil Davis
					$smdnscfg .= "		ddns-update-style interim;\n";
1115 7309ff39 Renato Botelho
				}
1116
1117
				if (is_array($sm['dnsserver']) && ($sm['dnsserver'][0]) && ($sm['dnsserver'][0] != $dhcpifconf['dnsserver'][0])) {
1118
					$smdnscfg .= "	option domain-name-servers " . join(",", $sm['dnsserver']) . ";\n";
1119
				}
1120
				$dhcpdconf .= "{$smdnscfg}";
1121
1122
				// default-lease-time
1123 61e047a5 Phil Davis
				if ($sm['defaultleasetime'] && ($sm['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
1124 7309ff39 Renato Botelho
					$dhcpdconf .= "	default-lease-time {$sm['defaultleasetime']};\n";
1125 61e047a5 Phil Davis
				}
1126 7309ff39 Renato Botelho
1127
				// max-lease-time
1128 61e047a5 Phil Davis
				if ($sm['maxleasetime'] && ($sm['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
1129 7309ff39 Renato Botelho
					$dhcpdconf .= "	max-lease-time {$sm['maxleasetime']};\n";
1130 61e047a5 Phil Davis
				}
1131 7309ff39 Renato Botelho
1132
				// netbios-name*
1133
				if (is_array($sm['winsserver']) && $sm['winsserver'][0] && ($sm['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
1134
					$dhcpdconf .= "	option netbios-name-servers " . join(",", $sm['winsserver']) . ";\n";
1135
					$dhcpdconf .= "	option netbios-node-type 8;\n";
1136
				}
1137
1138
				// ntp-servers
1139 61e047a5 Phil Davis
				if (is_array($sm['ntpserver']) && $sm['ntpserver'][0] && ($sm['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
1140 7309ff39 Renato Botelho
					$dhcpdconf .= "	option ntp-servers " . join(",", $sm['ntpserver']) . ";\n";
1141 61e047a5 Phil Davis
				}
1142 7309ff39 Renato Botelho
1143
				// tftp-server-name
1144 61e047a5 Phil Davis
				if (!empty($sm['tftp']) && ($sm['tftp'] != $dhcpifconf['tftp'])) {
1145 7309ff39 Renato Botelho
					$dhcpdconf .= "	option tftp-server-name \"{$sm['tftp']}\";\n";
1146 61e047a5 Phil Davis
				}
1147 7309ff39 Renato Botelho
1148 5b237745 Scott Ullrich
				$dhcpdconf .= "}\n";
1149
				$i++;
1150
			}
1151
		}
1152 a25183c5 Scott Ullrich
1153 6f9b8073 Ermal Luçi
		$dhcpdifs[] = get_real_interface($dhcpif);
1154 61e047a5 Phil Davis
		if ($newzone['domain-name']) {
1155
			if ($need_ddns_updates) {
1156 87019fc4 Andres Petralli
				$newzone['dns-servers'] = array($dhcpifconf['ddnsdomainprimary']);
1157 7c1747a3 Chris Buechler
				$newzone['ddnsdomainkeyname'] = $dhcpifconf['ddnsdomainkeyname'];
1158
				$newzone['ddnsdomainkey'] = $dhcpifconf['ddnsdomainkey'];
1159
				$dhcpdconf .= dhcpdkey($dhcpifconf);
1160 87019fc4 Andres Petralli
			}
1161 3df2dbfd jim-p
			$ddns_zones[] = $newzone;
1162 87019fc4 Andres Petralli
		}
1163 3df2dbfd jim-p
	}
1164
1165
	if ($need_ddns_updates) {
1166
		$dhcpdconf .= "ddns-update-style interim;\n";
1167 3cdef187 Andres Petralli
		$dhcpdconf .= "update-static-leases on;\n";
1168 87019fc4 Andres Petralli
1169 7c1747a3 Chris Buechler
		$dhcpdconf .= dhcpdzones($ddns_zones);
1170 5b237745 Scott Ullrich
	}
1171
1172 abdd01f5 Ermal
	/* write dhcpd.conf */
1173
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", $dhcpdconf)) {
1174
		printf(gettext("Error: cannot open dhcpd.conf in services_dhcpdv4_configure().%s"), "\n");
1175
		unset($dhcpdconf);
1176
		return 1;
1177
	}
1178 928d4416 Ermal
	unset($dhcpdconf);
1179 2fb056d8 Seth Mos
1180
	/* create an empty leases database */
1181 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases")) {
1182 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
1183 61e047a5 Phil Davis
	}
1184 2fb056d8 Seth Mos
1185 b075c1e2 Chris Buechler
	/* make sure there isn't a stale dhcpd.pid file, which can make dhcpd fail to start.   */
1186 61e047a5 Phil Davis
	/* if we get here, dhcpd has been killed and is not started yet                        */
1187 b075c1e2 Chris Buechler
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
1188
1189 2fb056d8 Seth Mos
	/* fire up dhcpd in a chroot */
1190 abdd01f5 Ermal
	if (count($dhcpdifs) > 0) {
1191 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 " .
1192
			join(" ", $dhcpdifs));
1193
	}
1194
1195 61e047a5 Phil Davis
	if (platform_booting()) {
1196 2fb056d8 Seth Mos
		print "done.\n";
1197 61e047a5 Phil Davis
	}
1198 2fb056d8 Seth Mos
1199
	return 0;
1200
}
1201
1202 086cf944 Phil Davis
function dhcpdkey($dhcpifconf) {
1203 87019fc4 Andres Petralli
	$dhcpdconf = "";
1204 61e047a5 Phil Davis
	if ($dhcpifconf['ddnsdomainkeyname'] <> "" && $dhcpifconf['ddnsdomainkey'] <> "") {
1205 87019fc4 Andres Petralli
		$dhcpdconf .= "key {$dhcpifconf['ddnsdomainkeyname']} {\n";
1206
		$dhcpdconf .= "	algorithm hmac-md5;\n";
1207
		$dhcpdconf .= "	secret {$dhcpifconf['ddnsdomainkey']};\n";
1208
		$dhcpdconf .= "}\n";
1209
	}
1210
1211
	return $dhcpdconf;
1212
}
1213
1214 7c1747a3 Chris Buechler
function dhcpdzones($ddns_zones) {
1215 87019fc4 Andres Petralli
	$dhcpdconf = "";
1216
1217
	if (is_array($ddns_zones)) {
1218
		$added_zones = array();
1219
		foreach ($ddns_zones as $zone) {
1220 61e047a5 Phil Davis
			if (!is_array($zone) || empty($zone) || !is_array($zone['dns-servers'])) {
1221 87019fc4 Andres Petralli
				continue;
1222 61e047a5 Phil Davis
			}
1223 87019fc4 Andres Petralli
			$primary = $zone['dns-servers'][0];
1224
			$secondary = empty($zone['dns-servers'][1]) ? "" : $zone['dns-servers'][1];
1225
1226
			// Make sure we aren't using any invalid or IPv6 DNS servers.
1227
			if (!is_ipaddrv4($primary)) {
1228
				if (is_ipaddrv4($secondary)) {
1229
					$primary = $secondary;
1230
					$secondary = "";
1231
				} else {
1232
					continue;
1233
				}
1234
			}
1235
1236
			// We don't need to add zones multiple times.
1237
			if ($zone['domain-name'] && !in_array($zone['domain-name'], $added_zones)) {
1238
				$dhcpdconf .= "zone {$zone['domain-name']}. {\n";
1239
				$dhcpdconf .= "	primary {$primary};\n";
1240 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1241 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1242 61e047a5 Phil Davis
				}
1243 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1244
					$dhcpdconf .= "	key {$zone['ddnsdomainkeyname']};\n";
1245 61e047a5 Phil Davis
				}
1246 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1247
				$added_zones[] = $zone['domain-name'];
1248
			}
1249
			if ($zone['ptr-domain'] && !in_array($zone['ptr-domain'], $added_zones)) {
1250
				$dhcpdconf .= "zone {$zone['ptr-domain']} {\n";
1251
				$dhcpdconf .= "	primary {$primary};\n";
1252 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1253 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1254 61e047a5 Phil Davis
				}
1255 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1256
					$dhcpdconf .= "	key {$zone['ddnsdomainkeyname']};\n";
1257 61e047a5 Phil Davis
				}
1258 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1259
				$added_zones[] = $zone['ptr-domain'];
1260
			}
1261
		}
1262
	}
1263
1264
	return $dhcpdconf;
1265
}
1266
1267 92977616 Ermal
function services_dhcpdv6_configure($blacklist = array()) {
1268 2fb056d8 Seth Mos
	global $config, $g;
1269 107e8acc Ovidiu Predescu
1270 61e047a5 Phil Davis
	if ($g['services_dhcp_server_enable'] == false) {
1271 2fb056d8 Seth Mos
		return;
1272 61e047a5 Phil Davis
	}
1273 2fb056d8 Seth Mos
1274 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1275 2fb056d8 Seth Mos
		$mt = microtime();
1276
		echo "services_dhcpd_configure($if) being called $mt\n";
1277
	}
1278 107e8acc Ovidiu Predescu
1279 2fb056d8 Seth Mos
	/* kill any running dhcpd */
1280 61e047a5 Phil Davis
	if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid")) {
1281 bfb3e717 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
1282 61e047a5 Phil Davis
	}
1283
	if (isvalidpid("{$g['varrun_path']}/dhcpleases6.pid")) {
1284 f7cd5647 smos
		killbypid("{$g['varrun_path']}/dhcpleases6.pid");
1285 61e047a5 Phil Davis
	}
1286 e9ab2ddb smos
1287 2fb056d8 Seth Mos
	/* DHCP enabled on any interfaces? */
1288 61e047a5 Phil Davis
	if (!is_dhcpv6_server_enabled()) {
1289 2fb056d8 Seth Mos
		return 0;
1290 61e047a5 Phil Davis
	}
1291 2fb056d8 Seth Mos
1292 e90ca528 Renato Botelho
	if (platform_booting() &&
1293
	    ($g['platform'] != $g['product_name'] ||
1294
	    isset($config['system']['use_mfs_tmpvar'])) &&
1295
	    file_exists("{$g['cf_conf_path']}/dhcp6leases.tgz")) {
1296
		/* restore the leases, if we have them */
1297
		$dhcprestore = "";
1298
		$dhcpreturn = "";
1299
		exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcp6leases.tgz 2>&1",
1300
		    $dhcprestore, $dhcpreturn);
1301
		$dhcprestore = implode(" ", $dhcprestore);
1302
		if ($dhcpreturn <> 0) {
1303
			log_error(sprintf(gettext(
1304
			    'DHCP leases v6 restore failed exited with %1$s, the error is: %2$s'),
1305
			    $dhcpreturn, $dhcprestore));
1306 2fb056d8 Seth Mos
		}
1307
	}
1308
1309
	$syscfg = $config['system'];
1310 61e047a5 Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1311 2fb056d8 Seth Mos
		$config['dhcpdv6'] = array();
1312 61e047a5 Phil Davis
	}
1313 2fb056d8 Seth Mos
	$dhcpdv6cfg = $config['dhcpdv6'];
1314
	$Iflist = get_configured_interface_list();
1315 e9ab2ddb smos
	$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
1316
1317 107e8acc Ovidiu Predescu
1318 61e047a5 Phil Davis
	if (platform_booting()) {
1319 2fb056d8 Seth Mos
		echo "Starting DHCPv6 service...";
1320 61e047a5 Phil Davis
	} else {
1321 2fb056d8 Seth Mos
		sleep(1);
1322 61e047a5 Phil Davis
	}
1323 2fb056d8 Seth Mos
1324
	$custoptionsv6 = "";
1325 107e8acc Ovidiu Predescu
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1326 61e047a5 Phil Davis
		if (is_array($dhcpv6ifconf['numberoptions']) && is_array($dhcpv6ifconf['numberoptions']['item'])) {
1327
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1328 2fb056d8 Seth Mos
				$custoptionsv6 .= "option custom-{$dhcpv6if}-{$itemv6idx} code {$itemv6['number']} = text;\n";
1329
			}
1330
		}
1331
	}
1332
1333 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['netboot']) && !empty($dhcpv6ifconf['bootfile_url'])) {
1334 bd942860 Renato Botelho
		$custoptionsv6 .= "option dhcp6.bootfile-url code 59 = string;\n";
1335 61e047a5 Phil Davis
	}
1336 bd942860 Renato Botelho
1337 2fb056d8 Seth Mos
	$dhcpdv6conf = <<<EOD
1338 107e8acc Ovidiu Predescu
1339 2fb056d8 Seth Mos
option domain-name "{$syscfg['domain']}";
1340
option ldap-server code 95 = text;
1341
option domain-search-list code 119 = text;
1342 547f1e65 Renato Botelho
{$custoptionsv6}
1343 2fb056d8 Seth Mos
default-lease-time 7200;
1344
max-lease-time 86400;
1345
log-facility local7;
1346
one-lease-per-client true;
1347
deny duplicates;
1348
ping-check true;
1349 87019fc4 Andres Petralli
update-conflict-detection false;
1350 2fb056d8 Seth Mos
1351
EOD;
1352
1353 61e047a5 Phil Davis
	if (!isset($dhcpv6ifconf['disableauthoritative'])) {
1354 2fb056d8 Seth Mos
		$dhcpdv6conf .= "authoritative;\n";
1355 61e047a5 Phil Davis
	}
1356 2fb056d8 Seth Mos
1357 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['alwaysbroadcast'])) {
1358 2fb056d8 Seth Mos
		$dhcpdv6conf .= "always-broadcast on\n";
1359 61e047a5 Phil Davis
	}
1360 2fb056d8 Seth Mos
1361
	$dhcpdv6ifs = array();
1362
1363 693833cb Seth Mos
	$dhcpv6num = 0;
1364 87019fc4 Andres Petralli
	$nsupdate = false;
1365
1366 693833cb Seth Mos
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1367
1368 87019fc4 Andres Petralli
		$ddns_zones = array();
1369
1370 693833cb Seth Mos
		$ifcfgv6 = $config['interfaces'][$dhcpv6if];
1371
1372 61e047a5 Phil Davis
		if (!isset($dhcpv6ifconf['enable']) || !isset($Iflist[$dhcpv6if]) || !isset($ifcfgv6['enable'])) {
1373 693833cb Seth Mos
			continue;
1374 61e047a5 Phil Davis
		}
1375 693833cb Seth Mos
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1376 830ea39a Chris Buechler
		if (!is_ipaddrv6($ifcfgipv6)) {
1377
			continue;
1378
		}
1379 693833cb Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
1380 d57293a4 Seth Mos
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1381 786d411d Phil Davis
		// We might have some prefix-delegation on WAN (e.g. /48),
1382
		// but then it is split and given out to individual interfaces
1383
		// (LAN, OPT1, OPT2...) as multiple /64 subnets. So the size
1384
		// of each subnet here is always /64.
1385
		$pdlen = 64;
1386 15db02a6 Renato Botelho
1387 1944af41 Ermal
		if ($is_olsr_enabled == true) {
1388 61e047a5 Phil Davis
			if ($dhcpv6ifconf['netmask']) {
1389 bfb3e717 Seth Mos
				$subnetmask = gen_subnet_maskv6($dhcpv6ifconf['netmask']);
1390 61e047a5 Phil Davis
			}
1391 1944af41 Ermal
		}
1392 693833cb Seth Mos
1393
		$dnscfgv6 = "";
1394
1395
		if ($dhcpv6ifconf['domain']) {
1396 3c009080 Seth Mos
			$dnscfgv6 .= "	option domain-name \"{$dhcpv6ifconf['domain']}\";\n";
1397 693833cb Seth Mos
		}
1398 107e8acc Ovidiu Predescu
1399 61e047a5 Phil Davis
		if ($dhcpv6ifconf['domainsearchlist'] <> "") {
1400 a3de8b9e Pierre POMES
			$dnscfgv6 .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpv6ifconf['domainsearchlist'])) . "\";\n";
1401 61e047a5 Phil Davis
		}
1402 693833cb Seth Mos
1403
		if (isset($dhcpv6ifconf['ddnsupdate'])) {
1404 61e047a5 Phil Davis
			if ($dhcpv6ifconf['ddnsdomain'] <> "") {
1405 3c009080 Seth Mos
				$dnscfgv6 .= "	ddns-domainname \"{$dhcpv6ifconf['ddnsdomain']}\";\n";
1406 693833cb Seth Mos
			}
1407 391d63da Renato Botelho
			if (empty($dhcpv6ifconf['ddnsclientupdates'])) {
1408
				$ddnsclientupdates = 'allow';
1409
			} else {
1410
				$ddnsclientupdates = $dhcpv6ifconf['ddnsclientupdates'];
1411
			}
1412
			$dnscfgv6 .= "	{$ddnsclientupdates} client-updates;\n";
1413 87019fc4 Andres Petralli
			$nsupdate = true;
1414 391d63da Renato Botelho
		} else {
1415
			$dnscfgv6 .= "	do-forward-updates false;\n";
1416 693833cb Seth Mos
		}
1417
1418
		if (is_array($dhcpv6ifconf['dnsserver']) && ($dhcpv6ifconf['dnsserver'][0])) {
1419
			$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dhcpv6ifconf['dnsserver']) . ";";
1420 f4620b36 Chris Buechler
		} else if (((isset($config['dnsmasq']['enable'])) || isset($config['unbound']['enable'])) && (is_ipaddrv6($ifcfgipv6))) {
1421 693833cb Seth Mos
			$dnscfgv6 .= "	option dhcp6.name-servers {$ifcfgipv6};";
1422
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1423 2521266a Seth Mos
			$dns_arrv6 = array();
1424 61e047a5 Phil Davis
			foreach ($syscfg['dnsserver'] as $dnsserver) {
1425 1944af41 Ermal
				if (is_ipaddrv6($dnsserver)) {
1426 2521266a Seth Mos
					$dns_arrv6[] = $dnsserver;
1427
				}
1428
			}
1429 61e047a5 Phil Davis
			if (!empty($dns_arrv6)) {
1430 2816c5a1 Seth Mos
				$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dns_arrv6) . ";";
1431 61e047a5 Phil Davis
			}
1432 693833cb Seth Mos
		}
1433
1434 391d63da Renato Botelho
		if (!is_ipaddrv6($ifcfgipv6)) {
1435
			$ifcfgsnv6 = "64";
1436
			$subnetv6 = gen_subnetv6($dhcpv6ifconf['range']['from'], $ifcfgsnv6);
1437
		}
1438
1439
		$dhcpdv6conf .= "subnet6 {$subnetv6}/{$ifcfgsnv6}";
1440
1441
		if (isset($dhcpv6ifconf['ddnsupdate']) &&
1442
		    !empty($dhcpv6ifconf['ddnsdomain'])) {
1443 87019fc4 Andres Petralli
			$newzone = array();
1444 391d63da Renato Botelho
			$newzone['domain-name'] = $dhcpv6ifconf['ddnsdomain'];
1445 87019fc4 Andres Petralli
			$newzone['dns-servers'][] = $dhcpv6ifconf['ddnsdomainprimary'];
1446 7c1747a3 Chris Buechler
			$newzone['ddnsdomainkeyname'] = $dhcpv6ifconf['ddnsdomainkeyname'];
1447
			$newzone['ddnsdomainkey'] = $dhcpv6ifconf['ddnsdomainkey'];
1448 87019fc4 Andres Petralli
			$ddns_zones[] = $newzone;
1449 391d63da Renato Botelho
			if (isset($dhcpv6ifconf['ddnsreverse'])) {
1450
				$ptr_zones = get_v6_ptr_zones($subnetv6, $ifcfgsnv6);
1451
				foreach ($ptr_zones as $ptr_zone) {
1452
					$reversezone = array();
1453
					$reversezone['domain-name'] = $ptr_zone;
1454
					$reversezone['dns-servers'][] =
1455
					    $dhcpv6ifconf['ddnsdomainprimary'];
1456
					$ddns_zones[] = $reversezone;
1457
				}
1458
			}
1459 87019fc4 Andres Petralli
		}
1460
1461 1944af41 Ermal
		$dhcpdv6conf .= " {\n";
1462 693833cb Seth Mos
1463 2bf455ca Renato Botelho
		$range_from = $dhcpv6ifconf['range']['from'];
1464
		$range_to = $dhcpv6ifconf['range']['to'];
1465
		if ($ifcfgv6['ipaddrv6'] == 'track6') {
1466 15db02a6 Renato Botelho
			$range_from = merge_ipv6_delegated_prefix($ifcfgipv6, $range_from, $pdlen);
1467
			$range_to = merge_ipv6_delegated_prefix($ifcfgipv6, $range_to, $pdlen);
1468 2bf455ca Renato Botelho
		}
1469
1470 693833cb Seth Mos
		$dhcpdv6conf .= <<<EOD
1471 2bf455ca Renato Botelho
	range6 {$range_from} {$range_to};
1472 693833cb Seth Mos
$dnscfgv6
1473
1474
EOD;
1475 bfb3e717 Seth Mos
1476 1944af41 Ermal
		if (is_ipaddrv6($dhcpv6ifconf['prefixrange']['from']) && is_ipaddrv6($dhcpv6ifconf['prefixrange']['to'])) {
1477 978b8f50 Chris Buechler
			$dhcpdv6conf .= "	prefix6 {$dhcpv6ifconf['prefixrange']['from']} {$dhcpv6ifconf['prefixrange']['to']} /{$dhcpv6ifconf['prefixrange']['prefixlength']};\n";
1478 bfb3e717 Seth Mos
		}
1479 70da4172 Jean Cyr
		if (is_ipaddrv6($dhcpv6ifconf['dns6ip'])) {
1480 2bf455ca Renato Botelho
			$dns6ip = $dhcpv6ifconf['dns6ip'];
1481
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1482 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($dns6ip, '::', $pdlen)) {
1483
				$dns6ip = merge_ipv6_delegated_prefix($ifcfgipv6, $dns6ip, $pdlen);
1484 2bf455ca Renato Botelho
			}
1485
			$dhcpdv6conf .= "	option dhcp6.name-servers {$dns6ip};\n";
1486 70da4172 Jean Cyr
		}
1487 61e047a5 Phil Davis
		// default-lease-time
1488
		if ($dhcpv6ifconf['defaultleasetime']) {
1489 693833cb Seth Mos
			$dhcpdv6conf .= "	default-lease-time {$dhcpv6ifconf['defaultleasetime']};\n";
1490 61e047a5 Phil Davis
		}
1491 693833cb Seth Mos
1492
		// max-lease-time
1493 61e047a5 Phil Davis
		if ($dhcpv6ifconf['maxleasetime']) {
1494 693833cb Seth Mos
			$dhcpdv6conf .= "	max-lease-time {$dhcpv6ifconf['maxleasetime']};\n";
1495 61e047a5 Phil Davis
		}
1496 693833cb Seth Mos
1497
		// ntp-servers
1498 4096fe5d smos
		if (is_array($dhcpv6ifconf['ntpserver']) && $dhcpv6ifconf['ntpserver'][0]) {
1499
			$ntpservers = array();
1500 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['ntpserver'] as $ntpserver) {
1501 2bf455ca Renato Botelho
				if (!is_ipaddrv6($ntpserver)) {
1502
					continue;
1503
				}
1504
				if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1505 15db02a6 Renato Botelho
				    Net_IPv6::isInNetmask($ntpserver, '::', $pdlen)) {
1506
					$ntpserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ntpserver, $pdlen);
1507 61e047a5 Phil Davis
				}
1508 2bf455ca Renato Botelho
				$ntpservers[] = $ntpserver;
1509 4096fe5d smos
			}
1510 61e047a5 Phil Davis
			if (count($ntpservers) > 0) {
1511 fe86a663 Chris Buechler
				$dhcpdv6conf .= "        option dhcp6.sntp-servers " . join(",", $dhcpv6ifconf['ntpserver']) . ";\n";
1512 61e047a5 Phil Davis
			}
1513 4096fe5d smos
		}
1514 693833cb Seth Mos
		// tftp-server-name
1515 7d504365 smos
		/* Needs ISC DHCPD support
1516 61e047a5 Phil Davis
		 if ($dhcpv6ifconf['tftp'] <> "") {
1517 693833cb Seth Mos
			$dhcpdv6conf .= "	option tftp-server-name \"{$dhcpv6ifconf['tftp']}\";\n";
1518 61e047a5 Phil Davis
		 }
1519 7d504365 smos
		*/
1520 693833cb Seth Mos
1521
		// Handle option, number rowhelper values
1522
		$dhcpdv6conf .= "\n";
1523 1944af41 Ermal
		if ($dhcpv6ifconf['numberoptions']['item']) {
1524 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1525 65cce9d7 Renato Botelho
				$itemv6_value = base64_decode($itemv6['value']);
1526
				$dhcpdv6conf .= "	option custom-{$dhcpv6if}-{$itemv6idx} \"{$itemv6_value}\";\n";
1527 693833cb Seth Mos
			}
1528
		}
1529
1530
		// ldap-server
1531 61e047a5 Phil Davis
		if ($dhcpv6ifconf['ldap'] <> "") {
1532 2bf455ca Renato Botelho
			$ldapserver = $dhcpv6ifconf['ldap'];
1533
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1534 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($ldapserver, '::', $pdlen)) {
1535
				$ldapserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ldapserver, $pdlen);
1536 2bf455ca Renato Botelho
			}
1537
			$dhcpdv6conf .= "	option ldap-server \"{$ldapserver}\";\n";
1538 61e047a5 Phil Davis
		}
1539 693833cb Seth Mos
1540
		// net boot information
1541 61e047a5 Phil Davis
		if (isset($dhcpv6ifconf['netboot'])) {
1542 bd942860 Renato Botelho
			if (!empty($dhcpv6ifconf['bootfile_url'])) {
1543
				$dhcpdv6conf .= "	option dhcp6.bootfile-url \"{$dhcpv6ifconf['bootfile_url']}\";\n";
1544 abdd01f5 Ermal
			}
1545
		}
1546 107e8acc Ovidiu Predescu
1547 1c903aa4 Ermal
		$dhcpdv6conf .= "}\n";
1548 693833cb Seth Mos
1549
		/* add static mappings */
1550 2fb056d8 Seth Mos
		/* Needs to use DUID */
1551 693833cb Seth Mos
		if (is_array($dhcpv6ifconf['staticmap'])) {
1552
			$i = 0;
1553
			foreach ($dhcpv6ifconf['staticmap'] as $sm) {
1554
				$dhcpdv6conf .= <<<EOD
1555
host s_{$dhcpv6if}_{$i} {
1556 2fb056d8 Seth Mos
	host-identifier option dhcp6.client-id {$sm['duid']};
1557 693833cb Seth Mos
1558
EOD;
1559 61e047a5 Phil Davis
				if ($sm['ipaddrv6']) {
1560 2bf455ca Renato Botelho
					$ipaddrv6 = $sm['ipaddrv6'];
1561
					if ($ifcfgv6['ipaddrv6'] == 'track6') {
1562 15db02a6 Renato Botelho
						$ipaddrv6 = merge_ipv6_delegated_prefix($ifcfgipv6, $ipaddrv6, $pdlen);
1563 2bf455ca Renato Botelho
					}
1564
					$dhcpdv6conf .= "	fixed-address6 {$ipaddrv6};\n";
1565 61e047a5 Phil Davis
				}
1566 693833cb Seth Mos
1567
				if ($sm['hostname']) {
1568
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1569
					$dhhostname = str_replace(".", "_", $dhhostname);
1570
					$dhcpdv6conf .= "	option host-name {$dhhostname};\n";
1571
				}
1572 61e047a5 Phil Davis
				if ($sm['filename']) {
1573 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	filename \"{$sm['filename']}\";\n";
1574 61e047a5 Phil Davis
				}
1575 a2578c27 Anthony Wrather
1576 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1577 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	option root-path \"{$sm['rootpath']}\";\n";
1578 61e047a5 Phil Davis
				}
1579 693833cb Seth Mos
1580
				$dhcpdv6conf .= "}\n";
1581
				$i++;
1582
			}
1583
		}
1584 107e8acc Ovidiu Predescu
1585 391d63da Renato Botelho
		if ($dhcpv6ifconf['ddnsdomain']) {
1586 87019fc4 Andres Petralli
			$dhcpdv6conf .= dhcpdkey($dhcpv6ifconf);
1587 7c1747a3 Chris Buechler
			$dhcpdv6conf .= dhcpdzones($ddns_zones);
1588 87019fc4 Andres Petralli
		}
1589
1590 57006646 Renato Botelho
		if ($config['dhcpdv6'][$dhcpv6if]['ramode'] <> "unmanaged" && isset($config['interfaces'][$dhcpv6if]['enable'])) {
1591 61e047a5 Phil Davis
			if (preg_match("/poes/si", $dhcpv6if)) {
1592 e9ab2ddb smos
				/* magic here */
1593
				$dhcpdv6ifs = array_merge($dhcpdv6ifs, get_pppoes_child_interfaces($dhcpv6if));
1594
			} else {
1595 1944af41 Ermal
				$realif = get_real_interface($dhcpv6if, "inet6");
1596 029b377a Ermal
				if (stristr("$realif", "bridge")) {
1597
					$mac = get_interface_mac($realif);
1598
					$v6address = generate_ipv6_from_mac($mac);
1599
					/* Create link local address for bridges */
1600 e9ab2ddb smos
					mwexec("/sbin/ifconfig {$realif} inet6 {$v6address}");
1601
				}
1602 029b377a Ermal
				$realif = escapeshellcmd($realif);
1603
				$dhcpdv6ifs[] = $realif;
1604 656f1763 Seth Mos
			}
1605 de140730 Seth Mos
		}
1606 693833cb Seth Mos
	}
1607
1608 61e047a5 Phil Davis
	if ($nsupdate) {
1609 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style interim;\n";
1610 61e047a5 Phil Davis
	} else {
1611 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style none;\n";
1612
	}
1613
1614 abdd01f5 Ermal
	/* write dhcpdv6.conf */
1615
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf", $dhcpdv6conf)) {
1616 1c903aa4 Ermal
		log_error("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1617 61e047a5 Phil Davis
		if (platform_booting()) {
1618 1c903aa4 Ermal
			printf("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1619 61e047a5 Phil Davis
		}
1620 abdd01f5 Ermal
		unset($dhcpdv6conf);
1621
		return 1;
1622
	}
1623 928d4416 Ermal
	unset($dhcpdv6conf);
1624
1625 693833cb Seth Mos
	/* create an empty leases v6 database */
1626 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases")) {
1627 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1628 61e047a5 Phil Davis
	}
1629 107e8acc Ovidiu Predescu
1630 61e047a5 Phil Davis
	/* make sure there isn't a stale dhcpdv6.pid file, which may make dhcpdv6 fail to start.  */
1631
	/* if we get here, dhcpdv6 has been killed and is not started yet                         */
1632
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
1633 b075c1e2 Chris Buechler
1634 68a0e4fc Scott Ullrich
	/* fire up dhcpd in a chroot */
1635 abdd01f5 Ermal
	if (count($dhcpdv6ifs) > 0) {
1636 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 " .
1637 2a1bd027 Seth Mos
			join(" ", $dhcpdv6ifs));
1638 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");
1639 2a1bd027 Seth Mos
	}
1640 61e047a5 Phil Davis
	if (platform_booting()) {
1641 f1a44a3a Carlos Eduardo Ramos
		print gettext("done.") . "\n";
1642 61e047a5 Phil Davis
	}
1643 a25183c5 Scott Ullrich
1644 5b237745 Scott Ullrich
	return 0;
1645
}
1646
1647 41997fbb Ermal Luci
function services_igmpproxy_configure() {
1648 61e047a5 Phil Davis
	global $config, $g;
1649 41997fbb Ermal Luci
1650 61e047a5 Phil Davis
	/* kill any running igmpproxy */
1651
	killbyname("igmpproxy");
1652 41997fbb Ermal Luci
1653 61e047a5 Phil Davis
	if (!is_array($config['igmpproxy']['igmpentry']) || (count($config['igmpproxy']['igmpentry']) == 0)) {
1654 41997fbb Ermal Luci
		return 1;
1655 61e047a5 Phil Davis
	}
1656 41997fbb Ermal Luci
1657 61e047a5 Phil Davis
	$iflist = get_configured_interface_list();
1658 f206afb5 Ermal
1659 61e047a5 Phil Davis
	$igmpconf = <<<EOD
1660 41997fbb Ermal Luci
1661
##------------------------------------------------------
1662
## Enable Quickleave mode (Sends Leave instantly)
1663
##------------------------------------------------------
1664
quickleave
1665
1666
EOD;
1667
1668 61e047a5 Phil Davis
	foreach ($config['igmpproxy']['igmpentry'] as $igmpcf) {
1669
		unset($iflist[$igmpcf['ifname']]);
1670
		$realif = get_real_interface($igmpcf['ifname']);
1671
		if (empty($igmpcf['threshold'])) {
1672
			$threshld = 1;
1673
		} else {
1674
			$threshld = $igmpcf['threshold'];
1675
		}
1676
		$igmpconf .= "phyint {$realif} {$igmpcf['type']} ratelimit 0 threshold {$threshld}\n";
1677
1678
		if ($igmpcf['address'] <> "") {
1679
			$item = explode(" ", $igmpcf['address']);
1680
			foreach ($item as $iww) {
1681
				$igmpconf .= "altnet {$iww}\n";
1682
			}
1683
		}
1684
		$igmpconf .= "\n";
1685
	}
1686
	foreach ($iflist as $ifn) {
1687
		$realif = get_real_interface($ifn);
1688
		$igmpconf .= "phyint {$realif} disabled\n";
1689
	}
1690 3bae60be Ermal
	$igmpconf .= "\n";
1691 41997fbb Ermal Luci
1692 61e047a5 Phil Davis
	$igmpfl = fopen($g['tmp_path'] . "/igmpproxy.conf", "w");
1693
	if (!$igmpfl) {
1694
		log_error(gettext("Could not write Igmpproxy configuration file!"));
1695
		return;
1696
	}
1697
	fwrite($igmpfl, $igmpconf);
1698
	fclose($igmpfl);
1699 928d4416 Ermal
	unset($igmpconf);
1700 41997fbb Ermal Luci
1701 61d6a840 Stephen Beaver
	if (isset($config['syslog']['igmpxverbose'])) {
1702
		mwexec_bg("/usr/local/sbin/igmpproxy -v {$g['tmp_path']}/igmpproxy.conf");
1703
	} else {
1704
		mwexec_bg("/usr/local/sbin/igmpproxy {$g['tmp_path']}/igmpproxy.conf");
1705
	}
1706
1707 61e047a5 Phil Davis
	log_error(gettext("Started IGMP proxy service."));
1708 41997fbb Ermal Luci
1709 61e047a5 Phil Davis
	return 0;
1710 41997fbb Ermal Luci
}
1711
1712 5b237745 Scott Ullrich
function services_dhcrelay_configure() {
1713 f19d3b7a Scott Ullrich
	global $config, $g;
1714 6fa9f38c Renato Botelho
1715 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1716 acd910bf Scott Ullrich
		$mt = microtime();
1717 f19d3b7a Scott Ullrich
		echo "services_dhcrelay_configure() being called $mt\n";
1718 acd910bf Scott Ullrich
	}
1719 a25183c5 Scott Ullrich
1720 5b237745 Scott Ullrich
	/* kill any running dhcrelay */
1721
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
1722 a25183c5 Scott Ullrich
1723 2f06cc3f Ermal
	$dhcrelaycfg =& $config['dhcrelay'];
1724 a25183c5 Scott Ullrich
1725 5b237745 Scott Ullrich
	/* DHCPRelay enabled on any interfaces? */
1726 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
1727 5b237745 Scott Ullrich
		return 0;
1728 61e047a5 Phil Davis
	}
1729 a25183c5 Scott Ullrich
1730 61e047a5 Phil Davis
	if (platform_booting()) {
1731 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP relay service...");
1732 61e047a5 Phil Davis
	} else {
1733 5b237745 Scott Ullrich
		sleep(1);
1734 61e047a5 Phil Davis
	}
1735 a25183c5 Scott Ullrich
1736 2f06cc3f Ermal
	$iflist = get_configured_interface_list();
1737 a25183c5 Scott Ullrich
1738 2f06cc3f Ermal
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1739
	foreach ($dhcifaces as $dhcrelayif) {
1740
		if (!isset($iflist[$dhcrelayif]) ||
1741 61e047a5 Phil Davis
		    link_interface_to_bridge($dhcrelayif)) {
1742 5b237745 Scott Ullrich
			continue;
1743 61e047a5 Phil Davis
		}
1744 a25183c5 Scott Ullrich
1745 61e047a5 Phil Davis
		if (is_ipaddr(get_interface_ip($dhcrelayif))) {
1746 2f06cc3f Ermal
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1747 61e047a5 Phil Davis
		}
1748 5b237745 Scott Ullrich
	}
1749
1750 d7827421 Renato Botelho
	/*
1751
	 * In order for the relay to work, it needs to be active
1752
	 * on the interface in which the destination server sits.
1753
	 */
1754 2f06cc3f Ermal
	$srvips = explode(",", $dhcrelaycfg['server']);
1755 c58879a9 Ermal LUÇI
	if (!is_array($srvips)) {
1756 e8c516a0 Phil Davis
		log_error(gettext("No destination IP has been configured!"));
1757 c58879a9 Ermal LUÇI
		return;
1758 4de8f7ba Phil Davis
	}
1759 a25183c5 Scott Ullrich
1760 d7827421 Renato Botelho
	foreach ($srvips as $srcidx => $srvip) {
1761
		unset($destif);
1762
		foreach ($iflist as $ifname) {
1763
			$subnet = get_interface_ip($ifname);
1764
			if (!is_ipaddr($subnet)) {
1765
				continue;
1766
			}
1767
			$subnet .= "/" . get_interface_subnet($ifname);
1768
			if (ip_in_subnet($srvip, $subnet)) {
1769
				$destif = get_real_interface($ifname);
1770
				break;
1771
			}
1772
		}
1773
		if (!isset($destif)) {
1774
			foreach (get_staticroutes() as $rtent) {
1775
				if (ip_in_subnet($srvip, $rtent['network'])) {
1776
					$a_gateways = return_gateways_array(true);
1777
					$destif = $a_gateways[$rtent['gateway']]['interface'];
1778
					break;
1779
				}
1780
			}
1781
		}
1782
1783
		if (!isset($destif)) {
1784
			/* Create a array from the existing route table */
1785
			exec("/usr/bin/netstat -rnWf inet", $route_str);
1786
			array_shift($route_str);
1787
			array_shift($route_str);
1788
			array_shift($route_str);
1789
			array_shift($route_str);
1790
			$route_arr = array();
1791
			foreach ($route_str as $routeline) {
1792
				$items = preg_split("/[ ]+/i", $routeline);
1793
				if (is_subnetv4($items[0])) {
1794
					$subnet = $items[0];
1795
				} elseif (is_ipaddrv4($items[0])) {
1796
					$subnet = "{$items[0]}/32";
1797
				} else {
1798
					// Not a subnet or IP address, skip to the next line.
1799
					continue;
1800
				}
1801
				if (ip_in_subnet($srvip, $subnet)) {
1802
					$destif = trim($items[6]);
1803
					break;
1804
				}
1805
			}
1806
		}
1807
1808
		if (!isset($destif)) {
1809
			if (is_array($config['gateways']['gateway_item'])) {
1810
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1811
					if (isset($gateway['defaultgw'])) {
1812
						$destif = get_real_interface($gateway['interface']);
1813
						break;
1814
					}
1815
				}
1816
			} else {
1817
				$destif = get_real_interface("wan");
1818
			}
1819
		}
1820
1821
		if (!empty($destif)) {
1822
			$dhcrelayifs[] = $destif;
1823
		}
1824
	}
1825 5b237745 Scott Ullrich
	$dhcrelayifs = array_unique($dhcrelayifs);
1826
1827
	/* fire up dhcrelay */
1828 24997966 Ermal
	if (empty($dhcrelayifs)) {
1829 e8c516a0 Phil Davis
		log_error(gettext("No suitable interface found for running dhcrelay!"));
1830 24997966 Ermal
		return; /* XXX */
1831
	}
1832
1833 4de8f7ba Phil Davis
	$cmd = "/usr/local/sbin/dhcrelay -i " . implode(" -i ", $dhcrelayifs);
1834 5b237745 Scott Ullrich
1835 61e047a5 Phil Davis
	if (isset($dhcrelaycfg['agentoption'])) {
1836 4de8f7ba Phil Davis
		$cmd .= " -a -m replace";
1837 61e047a5 Phil Davis
	}
1838 5b237745 Scott Ullrich
1839 2f06cc3f Ermal
	$cmd .= " " . implode(" ", $srvips);
1840 5b237745 Scott Ullrich
	mwexec($cmd);
1841 928d4416 Ermal
	unset($cmd);
1842 a25183c5 Scott Ullrich
1843 5b237745 Scott Ullrich
	return 0;
1844
}
1845
1846 b7a15cf8 Seth Mos
function services_dhcrelay6_configure() {
1847
	global $config, $g;
1848 6fa9f38c Renato Botelho
1849 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
1850 b7a15cf8 Seth Mos
		$mt = microtime();
1851 874f099a Phil Davis
		echo "services_dhcrelay6_configure() being called $mt\n";
1852 b7a15cf8 Seth Mos
	}
1853
1854
	/* kill any running dhcrelay */
1855
	killbypid("{$g['varrun_path']}/dhcrelay6.pid");
1856
1857
	$dhcrelaycfg =& $config['dhcrelay6'];
1858
1859
	/* DHCPv6 Relay enabled on any interfaces? */
1860 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
1861 b7a15cf8 Seth Mos
		return 0;
1862 61e047a5 Phil Davis
	}
1863 b7a15cf8 Seth Mos
1864 61e047a5 Phil Davis
	if (platform_booting()) {
1865 b7a15cf8 Seth Mos
		echo gettext("Starting DHCPv6 relay service...");
1866 61e047a5 Phil Davis
	} else {
1867 b7a15cf8 Seth Mos
		sleep(1);
1868 61e047a5 Phil Davis
	}
1869 b7a15cf8 Seth Mos
1870
	$iflist = get_configured_interface_list();
1871
1872
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1873
	foreach ($dhcifaces as $dhcrelayif) {
1874
		if (!isset($iflist[$dhcrelayif]) ||
1875 086cf944 Phil Davis
		    link_interface_to_bridge($dhcrelayif)) {
1876 b7a15cf8 Seth Mos
			continue;
1877 086cf944 Phil Davis
		}
1878 b7a15cf8 Seth Mos
1879 61e047a5 Phil Davis
		if (is_ipaddrv6(get_interface_ipv6($dhcrelayif))) {
1880 b7a15cf8 Seth Mos
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1881 61e047a5 Phil Davis
		}
1882 b7a15cf8 Seth Mos
	}
1883 69dd7088 Michael Tharp
	$dhcrelayifs = array_unique($dhcrelayifs);
1884 b7a15cf8 Seth Mos
1885 d7827421 Renato Botelho
	/*
1886
	 * In order for the relay to work, it needs to be active
1887
	 * on the interface in which the destination server sits.
1888
	 */
1889 b7a15cf8 Seth Mos
	$srvips = explode(",", $dhcrelaycfg['server']);
1890 d7827421 Renato Botelho
	$srvifaces = array();
1891
	foreach ($srvips as $srcidx => $srvip) {
1892
		unset($destif);
1893
		foreach ($iflist as $ifname) {
1894
			$subnet = get_interface_ipv6($ifname);
1895
			if (!is_ipaddrv6($subnet)) {
1896
				continue;
1897
			}
1898
			$subnet .= "/" . get_interface_subnetv6($ifname);
1899
			if (ip_in_subnet($srvip, $subnet)) {
1900
				$destif = get_real_interface($ifname);
1901
				break;
1902
			}
1903
		}
1904
		if (!isset($destif)) {
1905
			if (is_array($config['staticroutes']['route'])) {
1906
				foreach ($config['staticroutes']['route'] as $rtent) {
1907
					if (ip_in_subnet($srvip, $rtent['network'])) {
1908
						$a_gateways = return_gateways_array(true);
1909
						$destif = $a_gateways[$rtent['gateway']]['interface'];
1910
						break;
1911
					}
1912
				}
1913
			}
1914
		}
1915
1916
		if (!isset($destif)) {
1917
			/* Create a array from the existing route table */
1918
			exec("/usr/bin/netstat -rnWf inet6", $route_str);
1919
			array_shift($route_str);
1920
			array_shift($route_str);
1921
			array_shift($route_str);
1922
			array_shift($route_str);
1923
			$route_arr = array();
1924
			foreach ($route_str as $routeline) {
1925
				$items = preg_split("/[ ]+/i", $routeline);
1926
				if (ip_in_subnet($srvip, $items[0])) {
1927
					$destif = trim($items[6]);
1928
					break;
1929
				}
1930
			}
1931
		}
1932
1933
		if (!isset($destif)) {
1934
			if (is_array($config['gateways']['gateway_item'])) {
1935
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1936
					if (isset($gateway['defaultgw'])) {
1937
						$destif = get_real_interface($gateway['interface']);
1938
						break;
1939
					}
1940
				}
1941
			} else {
1942
				$destif = get_real_interface("wan");
1943
			}
1944
		}
1945
1946
		if (!empty($destif)) {
1947
			$srvifaces[] = "{$srvip}%{$destif}";
1948
		}
1949 b7a15cf8 Seth Mos
	}
1950
1951
	/* fire up dhcrelay */
1952 61e047a5 Phil Davis
	if (empty($dhcrelayifs) || empty($srvifaces)) {
1953 e8c516a0 Phil Davis
		log_error(gettext("No suitable interface found for running dhcrelay -6!"));
1954 b7a15cf8 Seth Mos
		return; /* XXX */
1955
	}
1956
1957 54a9d71d Phil Davis
	$cmd = "/usr/local/sbin/dhcrelay -6 -pf \"{$g['varrun_path']}/dhcrelay6.pid\"";
1958 69dd7088 Michael Tharp
	foreach ($dhcrelayifs as $dhcrelayif) {
1959
		$cmd .= " -l {$dhcrelayif}";
1960
	}
1961
	foreach ($srvifaces as $srviface) {
1962
		$cmd .= " -u \"{$srviface}\"";
1963
	}
1964 b7a15cf8 Seth Mos
	mwexec($cmd);
1965 928d4416 Ermal
	unset($cmd);
1966 b7a15cf8 Seth Mos
1967
	return 0;
1968
}
1969
1970 181d7c95 Ermal Luçi
function services_dyndns_configure_client($conf) {
1971
1972 61e047a5 Phil Davis
	if (!isset($conf['enable'])) {
1973 65996399 Ermal
		return;
1974 61e047a5 Phil Davis
	}
1975 d2946062 Ermal
1976 181d7c95 Ermal Luçi
	/* load up the dyndns.class */
1977
	require_once("dyndns.class");
1978
1979
	$dns = new updatedns($dnsService = $conf['type'],
1980
		$dnsHost = $conf['host'],
1981 b24a0251 Chris Buechler
		$dnsDomain = $conf['domainname'],
1982 181d7c95 Ermal Luçi
		$dnsUser = $conf['username'],
1983
		$dnsPass = $conf['password'],
1984 3aa55bbe Phil Davis
		$dnsWildcard = $conf['wildcard'],
1985 107e8acc Ovidiu Predescu
		$dnsMX = $conf['mx'],
1986 f3b2b2a4 Yehuda Katz
		$dnsIf = "{$conf['interface']}",
1987
		$dnsBackMX = NULL,
1988
		$dnsServer = NULL,
1989
		$dnsPort = NULL,
1990 37f3e704 Matt Corallo
		$dnsUpdateURL = "{$conf['updateurl']}",
1991 cd132e86 Edson Brandi
		$forceUpdate = $conf['force'],
1992 4de8f7ba Phil Davis
		$dnsZoneID = $conf['zoneid'],
1993
		$dnsTTL = $conf['ttl'],
1994 37f3e704 Matt Corallo
		$dnsResultMatch = "{$conf['resultmatch']}",
1995
		$dnsRequestIf = "{$conf['requestif']}",
1996 1e503870 Phil Davis
		$dnsID = "{$conf['id']}",
1997 aa79f351 Sebastian Chrostek
		$dnsVerboseLog = $conf['verboselog'],
1998
		$curlIpresolveV4 = $conf['curl_ipresolve_v4'],
1999
		$curlSslVerifypeer = $conf['curl_ssl_verifypeer']);
2000 181d7c95 Ermal Luçi
}
2001
2002 0be93267 Ermal Lu?i
function services_dyndns_configure($int = "") {
2003 f19d3b7a Scott Ullrich
	global $config, $g;
2004 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2005 59a63553 Scott Ullrich
		$mt = microtime();
2006
		echo "services_dyndns_configure() being called $mt\n";
2007
	}
2008
2009 67ee1ec5 Ermal Luçi
	$dyndnscfg = $config['dyndnses']['dyndns'];
2010 017817c2 smos
	$gwgroups = return_gateway_groups_array();
2011 67ee1ec5 Ermal Luçi
	if (is_array($dyndnscfg)) {
2012 61e047a5 Phil Davis
		if (platform_booting()) {
2013 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DynDNS clients...");
2014 61e047a5 Phil Davis
		}
2015 181d7c95 Ermal Luçi
2016 67ee1ec5 Ermal Luçi
		foreach ($dyndnscfg as $dyndns) {
2017 05741e45 Renato Botelho
			/*
2018
			 * If it's using a gateway group, check if interface is
2019
			 * the active gateway for that group
2020
			 */
2021
			$group_int = '';
2022
			if (is_array($gwgroups[$dyndns['interface']])) {
2023
				if (!empty($gwgroups[$dyndns['interface']][0]['vip'])) {
2024
					$group_int = $gwgroups[$dyndns['interface']][0]['vip'];
2025
				} else {
2026
					$group_int = $gwgroups[$dyndns['interface']][0]['int'];
2027
				}
2028
			}
2029
			if ((empty($int)) || ($int == $dyndns['interface']) || ($int == $group_int)) {
2030 1e503870 Phil Davis
				$dyndns['verboselog'] = isset($dyndns['verboselog']);
2031 d9bdc020 Sebastian Chrostek
				$dyndns['curl_ipresolve_v4'] = isset($dyndns['curl_ipresolve_v4']);
2032
				$dyndns['curl_ssl_verifypeer'] = isset($dyndns['curl_ssl_verifypeer']);
2033 768eb89c smos
				services_dyndns_configure_client($dyndns);
2034
				sleep(1);
2035
			}
2036 67ee1ec5 Ermal Luçi
		}
2037 59a63553 Scott Ullrich
2038 61e047a5 Phil Davis
		if (platform_booting()) {
2039 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2040 61e047a5 Phil Davis
		}
2041 59a63553 Scott Ullrich
	}
2042
2043
	return 0;
2044
}
2045
2046 0e3aeb6b Phil Davis
function dyndnsCheckIP($int) {
2047 b89bc607 Phil Davis
	global $config, $factory_default_checkipservice;
2048 0e3aeb6b Phil Davis
	$ip_address = get_interface_ip($int);
2049
	if (is_private_ip($ip_address)) {
2050 873e4b28 phildd
		$gateways_status = return_gateways_status(true);
2051
		// If the gateway for this interface is down, then the external check cannot work.
2052
		// Avoid the long wait for the external check to timeout.
2053 61e047a5 Phil Davis
		if (stristr($gateways_status[$config['interfaces'][$int]['gateway']]['status'], "down")) {
2054 873e4b28 phildd
			return "down";
2055 61e047a5 Phil Davis
		}
2056 b89bc607 Phil Davis
2057
		// Append the factory default check IP service to the list (if not disabled).
2058
		if (!isset($config['checkipservices']['disable_factory_default'])) {
2059
			$config['checkipservices']['checkipservice'][] = $factory_default_checkipservice;
2060
		}
2061
2062
		// Use the first enabled check IP service as the default.
2063
		if (is_array($config['checkipservices']['checkipservice'])) {
2064
			foreach ($config['checkipservices']['checkipservice'] as $i => $checkipservice) {
2065
				if (isset($checkipservice['enable'])) {
2066
					$url = $checkipservice['url'];
2067
					$username = $checkipservice['username'];
2068
					$password = $checkipservice['password'];
2069
					$verifysslpeer = isset($checkipservice['verifysslpeer']);
2070
					break;
2071
				}
2072
			}
2073
		}
2074
2075
		$hosttocheck = $url;
2076 5244c510 Florian Asche
		$ip_ch = curl_init($hosttocheck);
2077 0e3aeb6b Phil Davis
		curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
2078 b89bc607 Phil Davis
		curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, $verifysslpeer);
2079 e1eee3d2 Renato Botelho
		curl_setopt($ip_ch, CURLOPT_INTERFACE, 'host!' . $ip_address);
2080 181386d6 Florian Asche
		curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
2081
		curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
2082 3c6f29c0 Florian Asche
		curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
2083 b89bc607 Phil Davis
		curl_setopt($ip_ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
2084
		curl_setopt($ip_ch, CURLOPT_USERPWD, "{$username}:{$password}");
2085 0e3aeb6b Phil Davis
		$ip_result_page = curl_exec($ip_ch);
2086
		curl_close($ip_ch);
2087
		$ip_result_decoded = urldecode($ip_result_page);
2088
		preg_match('=Current IP Address: (.*)</body>=siU', $ip_result_decoded, $matches);
2089
		$ip_address = trim($matches[1]);
2090
	}
2091
	return $ip_address;
2092
}
2093
2094 57b5da70 jim-p
function services_dnsmasq_configure($restart_dhcp = true) {
2095 f19d3b7a Scott Ullrich
	global $config, $g;
2096 6a01ea44 Bill Marquette
	$return = 0;
2097 107e8acc Ovidiu Predescu
2098 683992fc stilez
	// hard coded args: will be removed to avoid duplication if specified in custom_options
2099
	$standard_args = array(
2100
		"dns-forward-max" => "--dns-forward-max=5000",
2101
		"cache-size" => "--cache-size=10000",
2102
		"local-ttl" => "--local-ttl=1"
2103
	);
2104
2105
2106 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2107 acd910bf Scott Ullrich
		$mt = microtime();
2108 f19d3b7a Scott Ullrich
		echo "services_dnsmasq_configure() being called $mt\n";
2109 acd910bf Scott Ullrich
	}
2110
2111 5b237745 Scott Ullrich
	/* kill any running dnsmasq */
2112 61e047a5 Phil Davis
	if (file_exists("{$g['varrun_path']}/dnsmasq.pid")) {
2113 d224df18 Ermal
		sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
2114 61e047a5 Phil Davis
	}
2115 5b237745 Scott Ullrich
2116
	if (isset($config['dnsmasq']['enable'])) {
2117 a25183c5 Scott Ullrich
2118 61e047a5 Phil Davis
		if (platform_booting()) {
2119 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DNS forwarder...");
2120 61e047a5 Phil Davis
		} else {
2121 5b237745 Scott Ullrich
			sleep(1);
2122 61e047a5 Phil Davis
		}
2123 5b237745 Scott Ullrich
2124 61e047a5 Phil Davis
		/* generate hosts file */
2125 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2126 61e047a5 Phil Davis
			$return = 1;
2127
		}
2128 cbc6a13f Chris Buechler
2129 5b237745 Scott Ullrich
		$args = "";
2130 a25183c5 Scott Ullrich
2131 0261381a Ermal
		if (isset($config['dnsmasq']['regdhcp'])) {
2132
			$args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts ";
2133
		}
2134 107e8acc Ovidiu Predescu
2135 e6c49e3d jim-p
		/* Setup listen port, if non-default */
2136 61e047a5 Phil Davis
		if (is_port($config['dnsmasq']['port'])) {
2137 e6c49e3d jim-p
			$args .= " --port={$config['dnsmasq']['port']} ";
2138 61e047a5 Phil Davis
		}
2139 e6c49e3d jim-p
2140 b4323f39 jim-p
		$listen_addresses = "";
2141 61e047a5 Phil Davis
		if (isset($config['dnsmasq']['interface'])) {
2142 b4323f39 jim-p
			$interfaces = explode(",", $config['dnsmasq']['interface']);
2143
			foreach ($interfaces as $interface) {
2144 2a5960b0 Luiz Otavio O Souza
				$if = get_real_interface($interface);
2145
				if (does_interface_exist($if)) {
2146
					$laddr = get_interface_ip($interface);
2147
					if (is_ipaddrv4($laddr)) {
2148 9e7e2c94 Chris Buechler
						$listen_addresses .= " --listen-address={$laddr} ";
2149 61e047a5 Phil Davis
					}
2150 2a5960b0 Luiz Otavio O Souza
					$laddr6 = get_interface_ipv6($interface);
2151
					if (is_ipaddrv6($laddr6) && !isset($config['dnsmasq']['strictbind'])) {
2152
						/*
2153
						 * XXX: Since dnsmasq does not support link-local address
2154
						 * with scope specified. These checks are being done.
2155
						 */
2156
						if (is_linklocal($laddr6) && strstr($laddr6, "%")) {
2157
							$tmpaddrll6 = explode("%", $laddr6);
2158
							$listen_addresses .= " --listen-address={$tmpaddrll6[0]} ";
2159
						} else {
2160
							$listen_addresses .= " --listen-address={$laddr6} ";
2161 b77d19c7 Ermal
						}
2162 b4323f39 jim-p
					}
2163
				}
2164
			}
2165
			if (!empty($listen_addresses)) {
2166
				$args .= " {$listen_addresses} ";
2167 61e047a5 Phil Davis
				if (isset($config['dnsmasq']['strictbind'])) {
2168 b4323f39 jim-p
					$args .= " --bind-interfaces ";
2169 61e047a5 Phil Davis
				}
2170 b4323f39 jim-p
			}
2171
		}
2172
2173 fc27d3f4 Phil Davis
		/* If selected, then first forward reverse lookups for private IPv4 addresses to nowhere. */
2174 153613e3 Phil Davis
		/* Only make entries for reverse domains that do not have a matching domain override. */
2175 0a7985ba Phil Davis
		if (isset($config['dnsmasq']['no_private_reverse'])) {
2176
			/* Note: Carrier Grade NAT (CGN) addresses 100.64.0.0/10 are intentionally not here. */
2177
			/* End-users should not be aware of CGN addresses, so reverse lookups for these should not happen. */
2178
			/* Just the pfSense WAN might get a CGN address from an ISP. */
2179 153613e3 Phil Davis
2180
			// Build an array of domain overrides to help in checking for matches.
2181
			$override_a = array();
2182
			if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2183
				foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2184
					$override_a[$override['domain']] = "y";
2185
				}
2186 0a7985ba Phil Davis
			}
2187 153613e3 Phil Davis
2188
			// Build an array of the private reverse lookup domain names
2189
			$reverse_domain_a = array("10.in-addr.arpa", "168.192.in-addr.arpa");
2190
			// Unfortunately the 172.16.0.0/12 range does not map nicely to the in-addr.arpa scheme.
2191 61e047a5 Phil Davis
			for ($subnet_num = 16; $subnet_num < 32; $subnet_num++) {
2192 153613e3 Phil Davis
				$reverse_domain_a[] = "$subnet_num.172.in-addr.arpa";
2193
			}
2194
2195
			// Set the --server parameter to nowhere for each reverse domain name that was not specifically specified in a domain override.
2196 61e047a5 Phil Davis
			foreach ($reverse_domain_a as $reverse_domain) {
2197
				if (!isset($override_a[$reverse_domain])) {
2198 153613e3 Phil Davis
					$args .= " --server=/$reverse_domain/ ";
2199 61e047a5 Phil Davis
				}
2200 0a7985ba Phil Davis
			}
2201 153613e3 Phil Davis
			unset($override_a);
2202
			unset($reverse_domain_a);
2203 0a7985ba Phil Davis
		}
2204
2205 fc27d3f4 Phil Davis
		/* Setup forwarded domains */
2206
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2207 61e047a5 Phil Davis
			foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2208
				if ($override['ip'] == "!") {
2209 fc27d3f4 Phil Davis
					$override[ip] = "";
2210 61e047a5 Phil Davis
				}
2211 fc27d3f4 Phil Davis
				$args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
2212
			}
2213
		}
2214
2215 2c46f11f Scott Ullrich
		/* Allow DNS Rebind for forwarded domains */
2216 29721fe6 Scott Ullrich
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
2217 61e047a5 Phil Davis
			if (!isset($config['system']['webgui']['nodnsrebindcheck'])) {
2218
				foreach ($config['dnsmasq']['domainoverrides'] as $override) {
2219 30d20e7d Scott Ullrich
					$args .= ' --rebind-domain-ok=/' . $override['domain'] . '/ ';
2220
				}
2221 2c46f11f Scott Ullrich
			}
2222
		}
2223 91adc5c1 Scott Ullrich
2224 61e047a5 Phil Davis
		if (!isset($config['system']['webgui']['nodnsrebindcheck'])) {
2225 30d20e7d Scott Ullrich
			$dns_rebind = "--rebind-localhost-ok --stop-dns-rebind";
2226 61e047a5 Phil Davis
		}
2227 30d20e7d Scott Ullrich
2228 96ea7162 N0YB
		if (isset($config['dnsmasq']['strict_order'])) {
2229
			$args .= " --strict-order ";
2230
		}
2231
2232
		if (isset($config['dnsmasq']['domain_needed'])) {
2233
			$args .= " --domain-needed ";
2234
		}
2235
2236 61e047a5 Phil Davis
		if ($config['dnsmasq']['custom_options']) {
2237 683992fc stilez
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c) {
2238 071f6059 jim-p
				$args .= " " . escapeshellarg("--{$c}");
2239 683992fc stilez
				$p = explode('=', $c);
2240 61e047a5 Phil Davis
				if (array_key_exists($p[0], $standard_args)) {
2241 683992fc stilez
					unset($standard_args[$p[0]]);
2242 61e047a5 Phil Davis
				}
2243 683992fc stilez
			}
2244 61e047a5 Phil Davis
		}
2245 41567e06 jim-p
		$args .= ' ' . implode(' ', array_values($standard_args));
2246 8f9bffbc Andrew Thompson
2247 a95e5334 jim-p
		/* run dnsmasq. Use "-C /dev/null" since we use command line args only (Issue #6730) */
2248
		$cmd = "/usr/local/sbin/dnsmasq --all-servers -C /dev/null {$dns_rebind} {$args}";
2249 b4323f39 jim-p
		//log_error("dnsmasq command: {$cmd}");
2250
		mwexec_bg($cmd);
2251 928d4416 Ermal
		unset($args);
2252 5b237745 Scott Ullrich
2253 61e047a5 Phil Davis
		system_dhcpleases_configure();
2254 0a5a8df9 Warren Baker
2255 61e047a5 Phil Davis
		if (platform_booting()) {
2256 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2257 61e047a5 Phil Davis
		}
2258 5b237745 Scott Ullrich
	}
2259 a25183c5 Scott Ullrich
2260 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2261 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2262 6a01ea44 Bill Marquette
			$return = 1;
2263 61e047a5 Phil Davis
		}
2264 5b237745 Scott Ullrich
	}
2265
2266 6a01ea44 Bill Marquette
	return $return;
2267 5b237745 Scott Ullrich
}
2268
2269 57b5da70 jim-p
function services_unbound_configure($restart_dhcp = true) {
2270 175dc861 Warren Baker
	global $config, $g;
2271
	$return = 0;
2272
2273
	if (isset($config['system']['developerspew'])) {
2274
		$mt = microtime();
2275
		echo "services_unbound_configure() being called $mt\n";
2276
	}
2277
2278
	// kill any running Unbound instance
2279 61e047a5 Phil Davis
	if (file_exists("{$g['varrun_path']}/unbound.pid")) {
2280 175dc861 Warren Baker
		sigkillbypid("{$g['varrun_path']}/unbound.pid", "TERM");
2281 61e047a5 Phil Davis
	}
2282 175dc861 Warren Baker
2283
	if (isset($config['unbound']['enable'])) {
2284 61e047a5 Phil Davis
		if (platform_booting()) {
2285 5bb1c495 Warren Baker
			echo gettext("Starting DNS Resolver...");
2286 61e047a5 Phil Davis
		} else {
2287 175dc861 Warren Baker
			sleep(1);
2288 61e047a5 Phil Davis
		}
2289 175dc861 Warren Baker
2290 b3c6783f Renato Botelho
		/* generate hosts file */
2291 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2292 b3c6783f Renato Botelho
			$return = 1;
2293 61e047a5 Phil Davis
		}
2294 b3c6783f Renato Botelho
2295 175dc861 Warren Baker
		require_once('/etc/inc/unbound.inc');
2296
		sync_unbound_service();
2297 61e047a5 Phil Davis
		if (platform_booting()) {
2298 175dc861 Warren Baker
			echo gettext("done.") . "\n";
2299 61e047a5 Phil Davis
		}
2300 0a5a8df9 Warren Baker
2301 61e047a5 Phil Davis
		system_dhcpleases_configure();
2302 175dc861 Warren Baker
	}
2303
2304 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2305 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2306 175dc861 Warren Baker
			$return = 1;
2307 61e047a5 Phil Davis
		}
2308 175dc861 Warren Baker
	}
2309
2310
	return $return;
2311
}
2312
2313 5b237745 Scott Ullrich
function services_snmpd_configure() {
2314 f19d3b7a Scott Ullrich
	global $config, $g;
2315 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2316 acd910bf Scott Ullrich
		$mt = microtime();
2317 f19d3b7a Scott Ullrich
		echo "services_snmpd_configure() being called $mt\n";
2318
	}
2319 5b237745 Scott Ullrich
2320
	/* kill any running snmpd */
2321
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
2322 dd18038e Ermal
	sleep(2);
2323 61e047a5 Phil Davis
	if (is_process_running("bsnmpd")) {
2324 a976fa82 Scott Ullrich
		mwexec("/usr/bin/killall bsnmpd", true);
2325 61e047a5 Phil Davis
	}
2326 5b237745 Scott Ullrich
2327
	if (isset($config['snmpd']['enable'])) {
2328 a25183c5 Scott Ullrich
2329 61e047a5 Phil Davis
		if (platform_booting()) {
2330 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting SNMP daemon... ");
2331 61e047a5 Phil Davis
		}
2332 5b237745 Scott Ullrich
2333
		/* generate snmpd.conf */
2334
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
2335
		if (!$fd) {
2336 4de8f7ba Phil Davis
			printf(gettext("Error: cannot open snmpd.conf in services_snmpd_configure().%s"), "\n");
2337 5b237745 Scott Ullrich
			return 1;
2338
		}
2339 a25183c5 Scott Ullrich
2340 142da8f7 John Fleming
2341 5b237745 Scott Ullrich
		$snmpdconf = <<<EOD
2342 d47a8a69 Scott Ullrich
location := "{$config['snmpd']['syslocation']}"
2343
contact := "{$config['snmpd']['syscontact']}"
2344
read := "{$config['snmpd']['rocommunity']}"
2345 142da8f7 John Fleming
2346
EOD;
2347
2348 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2349 61e047a5 Phil Davis
		if (isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])) {
2350
			$snmpdconf .= <<<EOD
2351 142da8f7 John Fleming
# write string
2352
write := "{$config['snmpd']['rwcommunity']}"
2353
2354
EOD;
2355
		}
2356
*/
2357
2358
2359 61e047a5 Phil Davis
		if (isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])) {
2360
			$snmpdconf .= <<<EOD
2361 142da8f7 John Fleming
# SNMP Trap support.
2362 dbeeb008 John Fleming
traphost := {$config['snmpd']['trapserver']}
2363
trapport := {$config['snmpd']['trapserverport']}
2364
trap := "{$config['snmpd']['trapstring']}"
2365 142da8f7 John Fleming
2366
2367
EOD;
2368
		}
2369
2370 dadf8ebb jim-p
		$platform = trim(file_get_contents('/etc/platform'));
2371 61e047a5 Phil Davis
		if (($platform == "pfSense") && ($g['product_name'] != "pfSense")) {
2372 e52cb3a4 jim-p
			$platform = $g['product_name'];
2373 61e047a5 Phil Davis
		}
2374 dadf8ebb jim-p
		$sysDescr = "{$g['product_name']} " . php_uname("n") .
2375 5779ade6 Renato Botelho
			" {$g['product_version']} {$platform} " . php_uname("s") .
2376 dadf8ebb jim-p
			" " . php_uname("r") . " " . php_uname("m");
2377 142da8f7 John Fleming
2378
		$snmpdconf .= <<<EOD
2379 d47a8a69 Scott Ullrich
system := 1     # pfSense
2380
%snmpd
2381 dadf8ebb jim-p
sysDescr			= "{$sysDescr}"
2382 d47a8a69 Scott Ullrich
begemotSnmpdDebugDumpPdus       = 2
2383
begemotSnmpdDebugSyslogPri      = 7
2384
begemotSnmpdCommunityString.0.1 = $(read)
2385 142da8f7 John Fleming
2386
EOD;
2387
2388 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2389 61e047a5 Phil Davis
		if (isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])) {
2390
			$snmpdconf .= <<<EOD
2391 142da8f7 John Fleming
begemotSnmpdCommunityString.0.2 = $(write)
2392
2393
EOD;
2394
		}
2395
*/
2396
2397 c7f44ae0 Scott Ullrich
2398 61e047a5 Phil Davis
		if (isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])) {
2399
			$snmpdconf .= <<<EOD
2400 142da8f7 John Fleming
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
2401
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
2402
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
2403
2404
EOD;
2405
		}
2406
2407
2408
		$snmpdconf .= <<<EOD
2409 d47a8a69 Scott Ullrich
begemotSnmpdCommunityDisable    = 1
2410 03ba7a0f John Fleming
2411
EOD;
2412
2413 c82b2c3f jim-p
		$bind_to_ip = "0.0.0.0";
2414 61e047a5 Phil Davis
		if (isset($config['snmpd']['bindip'])) {
2415 c82b2c3f jim-p
			if (is_ipaddr($config['snmpd']['bindip'])) {
2416
				$bind_to_ip = $config['snmpd']['bindip'];
2417
			} else {
2418
				$if = get_real_interface($config['snmpd']['bindip']);
2419 61e047a5 Phil Davis
				if (does_interface_exist($if)) {
2420 89f171b0 Ermal LUÇI
					$bind_to_ip = get_interface_ip($config['snmpd']['bindip']);
2421 61e047a5 Phil Davis
				}
2422 c82b2c3f jim-p
			}
2423 7cbad422 Scott Ullrich
		}
2424
2425 61e047a5 Phil Davis
		if (is_port($config['snmpd']['pollport'])) {
2426
			$snmpdconf .= <<<EOD
2427 7cbad422 Scott Ullrich
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
2428 03ba7a0f John Fleming
2429
EOD;
2430
2431
		}
2432
2433
		$snmpdconf .= <<<EOD
2434 d47a8a69 Scott Ullrich
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
2435
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
2436 142da8f7 John Fleming
2437 03ba7a0f John Fleming
# These are bsnmp macros not php vars.
2438 9cc8c59e Scott Ullrich
sysContact      = $(contact)
2439
sysLocation     = $(location)
2440
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
2441 142da8f7 John Fleming
2442 d47a8a69 Scott Ullrich
snmpEnableAuthenTraps = 2
2443 03ba7a0f John Fleming
2444
EOD;
2445
2446 61e047a5 Phil Davis
		if (is_array($config['snmpd']['modules'])) {
2447
			if (isset($config['snmpd']['modules']['mibii'])) {
2448 03ba7a0f John Fleming
			$snmpdconf .= <<<EOD
2449 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
2450 03ba7a0f John Fleming
2451
EOD;
2452 61e047a5 Phil Davis
			}
2453 03ba7a0f John Fleming
2454 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['netgraph'])) {
2455
				$snmpdconf .= <<<EOD
2456 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
2457
%netgraph
2458
begemotNgControlNodeName = "snmpd"
2459 03ba7a0f John Fleming
2460
EOD;
2461 61e047a5 Phil Davis
			}
2462 03ba7a0f John Fleming
2463 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['pf'])) {
2464
				$snmpdconf .= <<<EOD
2465 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
2466 95fb49e8 Seth Mos
2467
EOD;
2468 61e047a5 Phil Davis
			}
2469 95fb49e8 Seth Mos
2470 a03974a3 Renato Botelho
			if (isset($config['snmpd']['modules']['hostres'])) {
2471 c64e401c jim-p
				$snmpdconf .= <<<EOD
2472 95fb49e8 Seth Mos
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
2473
2474
EOD;
2475 61e047a5 Phil Davis
			}
2476 05036071 Renato Botelho
2477 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['bridge'])) {
2478
				$snmpdconf .= <<<EOD
2479 95fb49e8 Seth Mos
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
2480 d47a8a69 Scott Ullrich
# config must end with blank line
2481 5b237745 Scott Ullrich
2482
EOD;
2483 61e047a5 Phil Davis
			}
2484
			if (isset($config['snmpd']['modules']['ucd'])) {
2485 671914b2 jim-p
				$snmpdconf .= <<<EOD
2486
begemotSnmpdModulePath."ucd"     = "/usr/local/lib/snmp_ucd.so"
2487
2488
EOD;
2489
			}
2490 61e047a5 Phil Davis
			if (isset($config['snmpd']['modules']['regex'])) {
2491 671914b2 jim-p
				$snmpdconf .= <<<EOD
2492
begemotSnmpdModulePath."regex"     = "/usr/local/lib/snmp_regex.so"
2493
2494
EOD;
2495
			}
2496 03ba7a0f John Fleming
		}
2497 5b237745 Scott Ullrich
2498
		fwrite($fd, $snmpdconf);
2499
		fclose($fd);
2500 928d4416 Ermal
		unset($snmpdconf);
2501 5b237745 Scott Ullrich
2502 853e003a Scott Ullrich
		/* run bsnmpd */
2503
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
2504 2073c2d5 Phil Davis
			" -p {$g['varrun_path']}/snmpd.pid");
2505 5b237745 Scott Ullrich
2506 61e047a5 Phil Davis
		if (platform_booting()) {
2507 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2508 61e047a5 Phil Davis
		}
2509 5b237745 Scott Ullrich
	}
2510
2511
	return 0;
2512
}
2513
2514 7c9da7be jim-p
function services_dnsupdate_process($int = "", $updatehost = "", $forced = false) {
2515 f19d3b7a Scott Ullrich
	global $config, $g;
2516 61e047a5 Phil Davis
	if (isset($config['system']['developerspew'])) {
2517 acd910bf Scott Ullrich
		$mt = microtime();
2518 f19d3b7a Scott Ullrich
		echo "services_dnsupdate_process() being called $mt\n";
2519 acd910bf Scott Ullrich
	}
2520 f19d3b7a Scott Ullrich
2521 a23d7248 Scott Ullrich
	/* Dynamic DNS updating active? */
2522 67ee1ec5 Ermal Luçi
	if (is_array($config['dnsupdates']['dnsupdate'])) {
2523 7c9da7be jim-p
		$notify_text = "";
2524 c7127a13 jim-p
		$gwgroups = return_gateway_groups_array();
2525 67ee1ec5 Ermal Luçi
		foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
2526 61e047a5 Phil Davis
			if (!isset($dnsupdate['enable'])) {
2527 2ec2a374 Ermal Lu?i
				continue;
2528 61e047a5 Phil Davis
			}
2529 05741e45 Renato Botelho
			/*
2530
			 * If it's using a gateway group, check if interface is
2531
			 * the active gateway for that group
2532
			 */
2533
			$group_int = '';
2534
			if (is_array($gwgroups[$dnsupdate['interface']])) {
2535
				if (!empty($gwgroups[$dnsupdate['interface']][0]['vip'])) {
2536
					$group_int = $gwgroups[$dnsupdate['interface']][0]['vip'];
2537
				} else {
2538
					$group_int = $gwgroups[$dnsupdate['interface']][0]['int'];
2539
				}
2540
			}
2541
			if (!empty($int) && ($int != $dnsupdate['interface']) && ($int != $group_int)) {
2542 67ee1ec5 Ermal Luçi
				continue;
2543 61e047a5 Phil Davis
			}
2544
			if (!empty($updatehost) && ($updatehost != $dnsupdate['host'])) {
2545 7c9da7be jim-p
				continue;
2546 61e047a5 Phil Davis
			}
2547 67ee1ec5 Ermal Luçi
2548 2ec2a374 Ermal Lu?i
			/* determine interface name */
2549 c7127a13 jim-p
			$if = get_failover_interface($dnsupdate['interface']);
2550 61e047a5 Phil Davis
2551
			if (isset($dnsupdate['usepublicip'])) {
2552 c7127a13 jim-p
				$wanip = dyndnsCheckIP($if);
2553 61e047a5 Phil Davis
			} else {
2554 c7127a13 jim-p
				$wanip = get_interface_ip($if);
2555 61e047a5 Phil Davis
			}
2556
2557 c7127a13 jim-p
			$wanipv6 = get_interface_ipv6($if);
2558 7c9da7be jim-p
			$cacheFile = "{$g['conf_path']}/dyndns_{$dnsupdate['interface']}_rfc2136_" . escapeshellarg($dnsupdate['host']) . "_{$dnsupdate['server']}.cache";
2559
			$currentTime = time();
2560
2561
			if ($wanip || $wanipv6) {
2562 67ee1ec5 Ermal Luçi
				$keyname = $dnsupdate['keyname'];
2563
				/* trailing dot */
2564 61e047a5 Phil Davis
				if (substr($keyname, -1) != ".") {
2565 67ee1ec5 Ermal Luçi
					$keyname .= ".";
2566 61e047a5 Phil Davis
				}
2567 67ee1ec5 Ermal Luçi
2568
				$hostname = $dnsupdate['host'];
2569
				/* trailing dot */
2570 61e047a5 Phil Davis
				if (substr($hostname, -1) != ".") {
2571 67ee1ec5 Ermal Luçi
					$hostname .= ".";
2572 61e047a5 Phil Davis
				}
2573 67ee1ec5 Ermal Luçi
2574
				/* write private key file
2575
				   this is dumb - public and private keys are the same for HMAC-MD5,
2576
				   but nsupdate insists on having both */
2577
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
2578 8ec0a8bc jim-p
				$privkey = <<<EOD
2579 a23d7248 Scott Ullrich
Private-key-format: v1.2
2580
Algorithm: 157 (HMAC)
2581 67ee1ec5 Ermal Luçi
Key: {$dnsupdate['keydata']}
2582 a23d7248 Scott Ullrich
2583
EOD;
2584 67ee1ec5 Ermal Luçi
				fwrite($fd, $privkey);
2585
				fclose($fd);
2586
2587
				/* write public key file */
2588
				if ($dnsupdate['keytype'] == "zone") {
2589
					$flags = 257;
2590
					$proto = 3;
2591
				} else if ($dnsupdate['keytype'] == "host") {
2592
					$flags = 513;
2593
					$proto = 3;
2594
				} else if ($dnsupdate['keytype'] == "user") {
2595
					$flags = 0;
2596
					$proto = 2;
2597
				}
2598 c7f44ae0 Scott Ullrich
2599 26586f7a Ermal Lu?i
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
2600 67ee1ec5 Ermal Luçi
				fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
2601
				fclose($fd);
2602
2603
				/* generate update instructions */
2604
				$upinst = "";
2605 61e047a5 Phil Davis
				if (!empty($dnsupdate['server'])) {
2606 67ee1ec5 Ermal Luçi
					$upinst .= "server {$dnsupdate['server']}\n";
2607 61e047a5 Phil Davis
				}
2608 7c9da7be jim-p
2609
				if (file_exists($cacheFile)) {
2610
					list($cachedipv4, $cacheTimev4) = explode("|", file_get_contents($cacheFile));
2611
				}
2612
				if (file_exists("{$cacheFile}.ipv6")) {
2613
					list($cachedipv6, $cacheTimev6) = explode("|", file_get_contents("{$cacheFile}.ipv6"));
2614
				}
2615
2616
				// 25 Days
2617
				$maxCacheAgeSecs = 25 * 24 * 60 * 60;
2618
				$need_update = false;
2619 819a603c jim-p
2620
				conf_mount_rw();
2621 fc1f4960 jim-p
				/* Update IPv4 if we have it. */
2622 ea08d2b2 Robert Nelson
				if (is_ipaddrv4($wanip) && $dnsupdate['recordtype'] != "AAAA") {
2623 7c9da7be jim-p
					if (($wanip != $cachedipv4) || (($currentTime - $cacheTimev4) > $maxCacheAgeSecs) || $forced) {
2624
						$upinst .= "update delete {$dnsupdate['host']}. A\n";
2625
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} A {$wanip}\n";
2626 e8c516a0 Phil Davis
						$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";
2627 7c9da7be jim-p
						@file_put_contents($cacheFile, "{$wanip}|{$currentTime}");
2628 e8c516a0 Phil Davis
						log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s: %2$s'), $cacheFile, $wanip));
2629 7c9da7be jim-p
						$need_update = true;
2630
					} else {
2631 e8c516a0 Phil Davis
						log_error(sprintf(gettext("phpDynDNS: Not updating %s A record because the IP address has not changed."), $dnsupdate['host']));
2632 7c9da7be jim-p
					}
2633 61e047a5 Phil Davis
				} else {
2634 7c9da7be jim-p
					@unlink($cacheFile);
2635 61e047a5 Phil Davis
				}
2636 7c9da7be jim-p
2637 fc1f4960 jim-p
				/* Update IPv6 if we have it. */
2638 ea08d2b2 Robert Nelson
				if (is_ipaddrv6($wanipv6) && $dnsupdate['recordtype'] != "A") {
2639 7c9da7be jim-p
					if (($wanipv6 != $cachedipv6) || (($currentTime - $cacheTimev6) > $maxCacheAgeSecs) || $forced) {
2640
						$upinst .= "update delete {$dnsupdate['host']}. AAAA\n";
2641
						$upinst .= "update add {$dnsupdate['host']}. {$dnsupdate['ttl']} AAAA {$wanipv6}\n";
2642 e8c516a0 Phil Davis
						$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";
2643 7c9da7be jim-p
						@file_put_contents("{$cacheFile}.ipv6", "{$wanipv6}|{$currentTime}");
2644 e8c516a0 Phil Davis
						log_error(sprintf(gettext('phpDynDNS: updating cache file %1$s.ipv6: %2$s'), $cacheFile, $wanipv6));
2645 7c9da7be jim-p
						$need_update = true;
2646
					} else {
2647 e8c516a0 Phil Davis
						log_error(sprintf(gettext("phpDynDNS: Not updating %s AAAA record because the IPv6 address has not changed."), $dnsupdate['host']));
2648 7c9da7be jim-p
					}
2649 61e047a5 Phil Davis
				} else {
2650 7c9da7be jim-p
					@unlink("{$cacheFile}.ipv6");
2651 61e047a5 Phil Davis
				}
2652 819a603c jim-p
				conf_mount_ro();
2653 67ee1ec5 Ermal Luçi
2654 7c9da7be jim-p
				$upinst .= "\n";	/* mind that trailing newline! */
2655 107e8acc Ovidiu Predescu
2656 7c9da7be jim-p
				if ($need_update) {
2657
					@file_put_contents("{$g['varetc_path']}/nsupdatecmds{$i}", $upinst);
2658
					unset($upinst);
2659
					/* invoke nsupdate */
2660 22cc6582 Renato Botelho
					$cmd = "/usr/local/bin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
2661 61e047a5 Phil Davis
					if (isset($dnsupdate['usetcp'])) {
2662 7c9da7be jim-p
						$cmd .= " -v";
2663 61e047a5 Phil Davis
					}
2664 7c9da7be jim-p
					$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
2665
					mwexec_bg($cmd);
2666
					unset($cmd);
2667
				}
2668 67ee1ec5 Ermal Luçi
			}
2669 a23d7248 Scott Ullrich
		}
2670 7c9da7be jim-p
		if (!empty($notify_text)) {
2671
			notify_all_remote($notify_text);
2672
		}
2673 a23d7248 Scott Ullrich
	}
2674 c7f44ae0 Scott Ullrich
2675 a23d7248 Scott Ullrich
	return 0;
2676 5b237745 Scott Ullrich
}
2677
2678 1071e028 Scott Ullrich
/* configure cron service */
2679
function configure_cron() {
2680
	global $g, $config;
2681 e7d3fc15 Ermal
2682 251ca022 Scott Ullrich
	conf_mount_rw();
2683 1071e028 Scott Ullrich
	/* preserve existing crontab entries */
2684 e7d3fc15 Ermal
	$crontab_contents = file("/etc/crontab", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
2685 107e8acc Ovidiu Predescu
2686 e7d3fc15 Ermal
	for ($i = 0; $i < count($crontab_contents); $i++) {
2687
		$cron_item =& $crontab_contents[$i];
2688
		if (strpos($cron_item, "# pfSense specific crontab entries") !== false) {
2689
			array_splice($crontab_contents, $i - 1);
2690 1071e028 Scott Ullrich
			break;
2691
		}
2692
	}
2693 e7d3fc15 Ermal
	$crontab_contents = implode("\n", $crontab_contents) . "\n";
2694 107e8acc Ovidiu Predescu
2695
2696 1071e028 Scott Ullrich
	if (is_array($config['cron']['item'])) {
2697
		$crontab_contents .= "#\n";
2698 7eea2e5f Renato Botelho
		$crontab_contents .= "# pfSense specific crontab entries\n";
2699 61e047a5 Phil Davis
		$crontab_contents .= "# " .gettext("Created:") . " " . date("F j, Y, g:i a") . "\n";
2700 1071e028 Scott Ullrich
		$crontab_contents .= "#\n";
2701
2702 992f60d0 Renato Botelho
		if (isset($config['system']['proxyurl']) && !empty($config['system']['proxyurl'])) {
2703
			$http_proxy = $config['system']['proxyurl'];
2704 61e047a5 Phil Davis
			if (isset($config['system']['proxyport']) && !empty($config['system']['proxyport'])) {
2705 992f60d0 Renato Botelho
				$http_proxy .= ':' . $config['system']['proxyport'];
2706 61e047a5 Phil Davis
			}
2707 992f60d0 Renato Botelho
			$crontab_contents .= "HTTP_PROXY={$http_proxy}";
2708
		}
2709
2710 1071e028 Scott Ullrich
		foreach ($config['cron']['item'] as $item) {
2711
			$crontab_contents .= "\n{$item['minute']}\t";
2712
			$crontab_contents .= "{$item['hour']}\t";
2713
			$crontab_contents .= "{$item['mday']}\t";
2714
			$crontab_contents .= "{$item['month']}\t";
2715
			$crontab_contents .= "{$item['wday']}\t";
2716
			$crontab_contents .= "{$item['who']}\t";
2717
			$crontab_contents .= "{$item['command']}";
2718
		}
2719 107e8acc Ovidiu Predescu
2720 1071e028 Scott Ullrich
		$crontab_contents .= "\n#\n";
2721 f1a44a3a Carlos Eduardo Ramos
		$crontab_contents .= "# " . gettext("If possible do not add items to this file manually.") . "\n";
2722 1c92c5b1 Stephen Beaver
		$crontab_contents .= "# " . gettext("If done so, this file must be terminated with a blank line (e.g. new line)") . "\n";
2723 1071e028 Scott Ullrich
		$crontab_contents .= "#\n\n";
2724
	}
2725 107e8acc Ovidiu Predescu
2726 1071e028 Scott Ullrich
	/* please maintain the newline at the end of file */
2727
	file_put_contents("/etc/crontab", $crontab_contents);
2728 c2d97111 Ermal
	unset($crontab_contents);
2729 41d507a5 Scott Ullrich
2730 8fe38524 doktornotor
	/* make sure that cron is running and start it if it got killed somehow */
2731
	if (!is_process_running("cron")) {
2732
		exec("cd /tmp && /usr/sbin/cron -s 2>/dev/null");
2733
	} else {
2734 41d507a5 Scott Ullrich
	/* do a HUP kill to force sync changes */
2735 c0020b97 doktornotor
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
2736 8fe38524 doktornotor
	}
2737 41d507a5 Scott Ullrich
2738 6d6bca27 Ermal Lu?i
	conf_mount_ro();
2739 1071e028 Scott Ullrich
}
2740
2741 431484c8 Ryan Wagoner
function upnp_action ($action) {
2742 aa6798c0 Scott Ullrich
	global $g, $config;
2743 61e047a5 Phil Davis
	switch ($action) {
2744 431484c8 Ryan Wagoner
		case "start":
2745 c1ac2424 Ermal
			if (file_exists('/var/etc/miniupnpd.conf')) {
2746
				@unlink("{$g['varrun_path']}/miniupnpd.pid");
2747
				mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P {$g['varrun_path']}/miniupnpd.pid");
2748
			}
2749 431484c8 Ryan Wagoner
			break;
2750
		case "stop":
2751 c1ac2424 Ermal
			killbypid("{$g['varrun_path']}/miniupnpd.pid");
2752 61e047a5 Phil Davis
			while ((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0) {
2753 3459814a doktornotor
				mwexec('/usr/bin/killall miniupnpd 2>/dev/null', true);
2754 61e047a5 Phil Davis
			}
2755 431484c8 Ryan Wagoner
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
2756
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
2757
			break;
2758
		case "restart":
2759
			upnp_action('stop');
2760
			upnp_action('start');
2761
			break;
2762
	}
2763
}
2764
2765 6f20377b Scott Ullrich
function upnp_start() {
2766 dcc897e5 Ermal
	global $config;
2767 0c331f1e Ermal Lu?i
2768 61e047a5 Phil Davis
	if (!isset($config['installedpackages']['miniupnpd']['config'])) {
2769 0c331f1e Ermal Lu?i
		return;
2770 61e047a5 Phil Davis
	}
2771 0c331f1e Ermal Lu?i
2772 61e047a5 Phil Davis
	if ($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
2773 54bdff75 Vinicius Coque
		echo gettext("Starting UPnP service... ");
2774 dcc897e5 Ermal
		require_once('/usr/local/pkg/miniupnpd.inc');
2775
		sync_package_miniupnpd();
2776
		echo "done.\n";
2777 6f20377b Scott Ullrich
	}
2778
}
2779
2780 4de8f7ba Phil Davis
function install_cron_job($command, $active = false, $minute = "0", $hour = "*", $monthday = "*", $month = "*", $weekday = "*", $who = "root") {
2781 85405c11 jim-p
	global $config, $g;
2782
2783
	$is_installed = false;
2784 4de8f7ba Phil Davis
	$cron_changed = true;
2785 85405c11 jim-p
2786 61e047a5 Phil Davis
	if (!is_array($config['cron'])) {
2787 c2d97111 Ermal
		$config['cron'] = array();
2788 61e047a5 Phil Davis
	}
2789
	if (!is_array($config['cron']['item'])) {
2790 c2d97111 Ermal
		$config['cron']['item'] = array();
2791 61e047a5 Phil Davis
	}
2792 85405c11 jim-p
2793 4de8f7ba Phil Davis
	$x = 0;
2794 61e047a5 Phil Davis
	foreach ($config['cron']['item'] as $item) {
2795
		if (strstr($item['command'], $command)) {
2796 85405c11 jim-p
			$is_installed = true;
2797
			break;
2798
		}
2799
		$x++;
2800
	}
2801
2802 61e047a5 Phil Davis
	if ($active) {
2803 85405c11 jim-p
		$cron_item = array();
2804
		$cron_item['minute'] = $minute;
2805
		$cron_item['hour'] = $hour;
2806
		$cron_item['mday'] = $monthday;
2807
		$cron_item['month'] = $month;
2808
		$cron_item['wday'] = $weekday;
2809
		$cron_item['who'] = $who;
2810
		$cron_item['command'] = $command;
2811 61e047a5 Phil Davis
		if (!$is_installed) {
2812 85405c11 jim-p
			$config['cron']['item'][] = $cron_item;
2813 f1a44a3a Carlos Eduardo Ramos
			write_config(sprintf(gettext("Installed cron job for %s"), $command));
2814 85405c11 jim-p
		} else {
2815 4de8f7ba Phil Davis
			if ($config['cron']['item'][$x] == $cron_item) {
2816
				$cron_changed = false;
2817
				log_error(sprintf(gettext("Checked cron job for %s, no change needed"), $command));
2818
			} else {
2819
				$config['cron']['item'][$x] = $cron_item;
2820
				write_config(sprintf(gettext("Updated cron job for %s"), $command));
2821
			}
2822 85405c11 jim-p
		}
2823
	} else {
2824 61e047a5 Phil Davis
		if ($is_installed == true) {
2825 85405c11 jim-p
			unset($config['cron']['item'][$x]);
2826 149ed85e bcyrill
			write_config(sprintf(gettext("Removed cron job for %s"), $command));
2827 85405c11 jim-p
		}
2828
	}
2829 994a0644 Phil Davis
2830 61e047a5 Phil Davis
	if ($cron_changed) {
2831 994a0644 Phil Davis
		configure_cron();
2832 61e047a5 Phil Davis
	}
2833 85405c11 jim-p
}
2834
2835 693833cb Seth Mos
?>