Project

General

Profile

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