Project

General

Profile

Download (109 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 38809d47 Renato Botelho do Couto
 * Copyright (c) 2004-2013 BSD Perimeter
7
 * Copyright (c) 2013-2016 Electric Sheep Fencing
8 402c98a2 Reid Linnemann
 * Copyright (c) 2014-2023 Rubicon Communications, LLC (Netgate)
9 ac24dc24 Renato Botelho
 * All rights reserved.
10
 *
11
 * originally part of m0n0wall (http://m0n0.ch/wall)
12 c5d81585 Renato Botelho
 * Copyright (c) 2003-2004 Manuel Kasper <mk@neon1.net>.
13 ac24dc24 Renato Botelho
 * All rights reserved.
14
 *
15 b12ea3fb Renato Botelho
 * Licensed under the Apache License, Version 2.0 (the "License");
16
 * you may not use this file except in compliance with the License.
17
 * You may obtain a copy of the License at
18 ac24dc24 Renato Botelho
 *
19 b12ea3fb Renato Botelho
 * http://www.apache.org/licenses/LICENSE-2.0
20 ac24dc24 Renato Botelho
 *
21 b12ea3fb Renato Botelho
 * Unless required by applicable law or agreed to in writing, software
22
 * distributed under the License is distributed on an "AS IS" BASIS,
23
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
24
 * See the License for the specific language governing permissions and
25
 * limitations under the License.
26 ac24dc24 Renato Botelho
 */
27 5b237745 Scott Ullrich
28 12e3bbce Matthew Fine
29 d9d91d5d Nita Vesa
define('DYNDNS_PROVIDER_VALUES', 'all-inkl azure azurev6 citynetwork cloudflare cloudflare-v6 cloudns custom custom-v6 desec desec-v6 digitalocean digitalocean-v6 dnsexit dnsimple dnsimple-v6 dnsmadeeasy dnsomatic domeneshop domeneshop-v6 dreamhost dreamhost-v6 duiadns duiadns-v6 dyfi dyndns dyndns-custom dyndns-static dyns dynv6 dynv6-v6 easydns easydns-v6 eurodns freedns freedns-v6 freedns2 freedns2-v6 glesys gandi-livedns gandi-livedns-v6 godaddy godaddy-v6 googledomains gratisdns he-net he-net-v6 he-net-tunnelbroker hover linode linode-v6 loopia mythicbeasts mythicbeasts-v6 name.com name.com-v6 namecheap nicru nicru-v6 noip noip-v6 noip-free noip-free-v6 onecom onecom-v6 ods opendns ovh-dynhost route53 route53-v6 selfhost spdyn spdyn-v6 strato yandex yandex-v6 zoneedit porkbun porkbun-v6');
30
define('DYNDNS_PROVIDER_DESCRIPTIONS', 'All-Inkl.com,Azure DNS,Azure DNS (v6),City Network,Cloudflare,Cloudflare (v6),ClouDNS,Custom,Custom (v6),deSEC,deSEC (v6),DigitalOcean,DigitalOcean (v6),DNSexit,DNSimple,DNSimple (v6),DNS Made Easy,DNS-O-Matic,Domeneshop,Domeneshop (v6),DreamHost,Dreamhost (v6),DuiaDns.net,DuiaDns.net (v6),DY.fi,DynDNS (dynamic),DynDNS (custom),DynDNS (static),DyNS,Dynv6,Dynv6 (v6),easyDNS,easyDNS (v6),Euro Dns,freeDNS,freeDNS (v6),freeDNS API Version 2, freeDNS API Version 2 (v6),GleSYS,Gandi Live DNS,Gandi Live DNS (v6),GoDaddy,GoDaddy (v6),Google Domains,GratisDNS,HE.net,HE.net (v6),HE.net Tunnelbroker,Hover,Linode,Linode (v6),Loopia,Mythic Beasts,Mythic Beasts (v6),Name.com,Name.com (v6),Namecheap,NIC.RU,NIC.RU (v6),No-IP,No-IP (v6),No-IP (free),No-IP (free-v6),One.com,One.com (v6),ODS.org,OpenDNS,OVH DynHOST,Route 53,Route 53 (v6),SelfHost,SPDYN,SPDYN (v6),Strato,Yandex,Yandex (v6),ZoneEdit,Porkbun,Porkbun (v6)');
31 0e3aeb6b Phil Davis
32 3aa55bbe Phil Davis
/* implement ipv6 route advertising daemon */
33 92977616 Ermal
function services_radvd_configure($blacklist = array()) {
34 532a1a0e Reid Linnemann
	global $g;
35 61e047a5 Phil Davis
36 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
37 d57293a4 Seth Mos
		$mt = microtime();
38 3f9cc8e4 smos
		echo "services_radvd_configure() being called $mt\n";
39 d57293a4 Seth Mos
	}
40
41 532a1a0e Reid Linnemann
	init_config_arr(['dhcpdv6']);
42 d57293a4 Seth Mos
43
	$Iflist = get_configured_interface_list();
44 e9ab2ddb smos
	$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
45 d57293a4 Seth Mos
46 3f9cc8e4 smos
	$radvdconf = "# Automatically Generated, do not edit\n";
47 4a3ff493 Seth Mos
48 753bd64d Seth Mos
	/* Process all links which need the router advertise daemon */
49 3f9cc8e4 smos
	$radvdifs = array();
50 668e8961 smos
51 3f9cc8e4 smos
	/* handle manually configured DHCP6 server settings first */
52 532a1a0e Reid Linnemann
	foreach (config_get_path('dhcpdv6', []) as $dhcpv6if => $dhcpv6ifconf) {
53 fae6b2c0 jim-p
		if (empty($dhcpv6ifconf)) {
54
			continue;
55
		}
56 532a1a0e Reid Linnemann
		if (!config_path_enabled("interfaces/{$dhcpv6if}")) {
57 d7d2dc52 smos
			continue;
58 61e047a5 Phil Davis
		}
59 5078cd76 smos
60 92977616 Ermal
		/* Do not put in the config an interface which is down */
61 61e047a5 Phil Davis
		if (isset($blacklist[$dhcpv6if])) {
62 92977616 Ermal
			continue;
63 61e047a5 Phil Davis
		}
64
		if (!isset($dhcpv6ifconf['ramode'])) {
65 8ca73e85 smos
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
66 61e047a5 Phil Davis
		}
67 8ca73e85 smos
68 3f9cc8e4 smos
		/* are router advertisements enabled? */
69 61e047a5 Phil Davis
		if ($dhcpv6ifconf['ramode'] == "disabled") {
70 361bb4a9 smos
			continue;
71 61e047a5 Phil Davis
		}
72 d57293a4 Seth Mos
73 61e047a5 Phil Davis
		if (!isset($dhcpv6ifconf['rapriority'])) {
74 8ca73e85 smos
			$dhcpv6ifconf['rapriority'] = "medium";
75 61e047a5 Phil Davis
		}
76 8ca73e85 smos
77 c0509674 Chris Buechler
		$racarpif = false;
78 8d4adafb znerol
		$rasrcaddr = "";
79 c0509674 Chris Buechler
		/* check if binding to CARP IP */
80 abc7b305 Viktor G
		if (!empty($dhcpv6ifconf['rainterface']) && strstr($dhcpv6ifconf['rainterface'], "_vip")) {
81
			if (get_carp_interface_status($dhcpv6ifconf['rainterface']) == "MASTER") {
82 8d4adafb znerol
				if (is_linklocal(get_configured_vip_ipv6($dhcpv6ifconf['rainterface']))) {
83
					$rasrcaddr = get_configured_vip_ipv6($dhcpv6ifconf['rainterface']);
84 abc7b305 Viktor G
				} else {
85 c0509674 Chris Buechler
					$dhcpv6if = $dhcpv6ifconf['rainterface'];
86
					$racarpif = true;
87
				}
88 abc7b305 Viktor G
			} else {
89
				continue;
90 c0509674 Chris Buechler
			}
91
		}
92
93 2a5960b0 Luiz Otavio O Souza
		$realif = get_real_interface($dhcpv6if, "inet6");
94 61e047a5 Phil Davis
95
		if (isset($radvdifs[$realif])) {
96 c18a10cc smos
			continue;
97 61e047a5 Phil Davis
		}
98 c18a10cc smos
99 2626cbd1 Ermal
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
100 61e047a5 Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
101 5078cd76 smos
			continue;
102 61e047a5 Phil Davis
		}
103 5078cd76 smos
104 d57293a4 Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
105
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
106 f8f8d131 Chris Buechler
		if (!is_subnetv6($subnetv6 . "/" . $ifcfgsnv6)) {
107
			log_error("radvd: skipping configuration for interface $dhcpv6if because its subnet or prefix length is invalid.");
108
			continue;
109
		}
110 60c05056 Ermal
		$radvdifs[$realif] = $realif;
111 d57293a4 Seth Mos
112 3f9cc8e4 smos
		$radvdconf .= "# Generated for DHCPv6 Server $dhcpv6if\n";
113
		$radvdconf .= "interface {$realif} {\n";
114 e03b6bbc Chris Buechler
		if (strstr($realif, "ovpn")) {
115
			$radvdconf .= "\tUnicastOnly on;\n";
116
		}
117 3f9cc8e4 smos
		$radvdconf .= "\tAdvSendAdvert on;\n";
118 c3099b79 Chris Buechler
119 8d4adafb znerol
		if ($rasrcaddr) {
120
			$radvdconf .= "\tAdvRASrcAddress {\n";
121
			$radvdconf .= "\t\t{$rasrcaddr};\n";
122
			$radvdconf .= "\t};\n";
123
		}
124
125 c3099b79 Chris Buechler
		if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
126
			$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
127
		} else {
128 762d3cc9 Viktor G
			$radvdconf .= "\tMinRtrAdvInterval 200;\n";
129 c3099b79 Chris Buechler
		}
130
131
		if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
132 54b3109f Viktor G
			$ramaxrtradvinterval = $dhcpv6ifconf['ramaxrtradvinterval'];
133 c3099b79 Chris Buechler
		} else {
134 762d3cc9 Viktor G
			$ramaxrtradvinterval = 600;
135 c3099b79 Chris Buechler
		}
136 54b3109f Viktor G
		$radvdconf .= "\tMaxRtrAdvInterval {$ramaxrtradvinterval};\n";
137 d37bd612 Chris Buechler
		if (is_numericint($dhcpv6ifconf['raadvdefaultlifetime'])) {
138 54b3109f Viktor G
			$raadvdefaultlifetime = $dhcpv6ifconf['raadvdefaultlifetime'];
139
		} else {
140
			$raadvdefaultlifetime = $ramaxrtradvinterval * 3;
141 d37bd612 Chris Buechler
		}
142 54b3109f Viktor G
		$radvdconf .= "\tAdvDefaultLifetime {$raadvdefaultlifetime};\n";
143 c3099b79 Chris Buechler
144 a6bc492f Ermal
		$mtu = get_interface_mtu($realif);
145 61e047a5 Phil Davis
		if (is_numeric($mtu)) {
146 a6bc492f Ermal
			$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
147 61e047a5 Phil Davis
		} else {
148 a6bc492f Ermal
			$radvdconf .= "\tAdvLinkMTU 1280;\n";
149 61e047a5 Phil Davis
		}
150
		switch ($dhcpv6ifconf['rapriority']) {
151 fe838158 smos
			case "low":
152
				$radvdconf .= "\tAdvDefaultPreference low;\n";
153
				break;
154
			case "high":
155
				$radvdconf .= "\tAdvDefaultPreference high;\n";
156 838a1ecb smos
				break;
157
			default:
158
				$radvdconf .= "\tAdvDefaultPreference medium;\n";
159
				break;
160 fe838158 smos
		}
161 61e047a5 Phil Davis
		switch ($dhcpv6ifconf['ramode']) {
162 656f1763 Seth Mos
			case "managed":
163 3f9cc8e4 smos
			case "assist":
164 8c78e692 plinss
				$radvdconf .= "\tAdvManagedFlag on;\n";
165 3f9cc8e4 smos
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
166
				break;
167 bd7ce993 aqueeb
			case "stateless_dhcp":
168
				$radvdconf .= "\tAdvManagedFlag off;\n";
169
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
170
				break;
171 3f9cc8e4 smos
		}
172 9462cc40 Viktor G
		$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
173 ad27159f znerol
		if ($racarpif == true || $rasrcaddr) {
174 c0509674 Chris Buechler
			$radvdconf .= "\t\tDeprecatePrefix off;\n";
175
		} else {
176
			$radvdconf .= "\t\tDeprecatePrefix on;\n";
177
		}
178 61e047a5 Phil Davis
		switch ($dhcpv6ifconf['ramode']) {
179 3f9cc8e4 smos
			case "managed":
180
				$radvdconf .= "\t\tAdvOnLink on;\n";
181
				$radvdconf .= "\t\tAdvAutonomous off;\n";
182 826ac52c smos
				break;
183
			case "router":
184 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink off;\n";
185
				$radvdconf .= "\t\tAdvAutonomous off;\n";
186 656f1763 Seth Mos
				break;
187 bd7ce993 aqueeb
			case "stateless_dhcp":
188 656f1763 Seth Mos
			case "assist":
189 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink on;\n";
190
				$radvdconf .= "\t\tAdvAutonomous on;\n";
191 107e8acc Ovidiu Predescu
				break;
192 3f9cc8e4 smos
			case "unmanaged":
193
				$radvdconf .= "\t\tAdvOnLink on;\n";
194
				$radvdconf .= "\t\tAdvAutonomous on;\n";
195 61e047a5 Phil Davis
				break;
196 656f1763 Seth Mos
		}
197 352defe4 schinken
198 6a461f45 Phil Davis
		if (is_numericint($dhcpv6ifconf['ravalidlifetime'])) {
199 352defe4 schinken
		  $radvdconf .= "\t\tAdvValidLifetime {$dhcpv6ifconf['ravalidlifetime']};\n";
200 be4748a8 schinken
		} else {
201
		  $radvdconf .= "\t\tAdvValidLifetime 86400;\n";
202 352defe4 schinken
		}
203
204 6a461f45 Phil Davis
		if (is_numericint($dhcpv6ifconf['rapreferredlifetime'])) {
205 352defe4 schinken
		  $radvdconf .= "\t\tAdvPreferredLifetime {$dhcpv6ifconf['rapreferredlifetime']};\n";
206 be4748a8 schinken
		} else {
207
		  $radvdconf .= "\t\tAdvPreferredLifetime 14400;\n";
208 352defe4 schinken
		}
209
210 3f9cc8e4 smos
		$radvdconf .= "\t};\n";
211
212 686e53c0 Chris Buechler
		if (is_array($dhcpv6ifconf['subnets']['item'])) {
213
			foreach ($dhcpv6ifconf['subnets']['item'] as $subnet) {
214
				if (is_subnetv6($subnet)) {
215
					$radvdconf .= "\tprefix {$subnet} {\n";
216 2a5960b0 Luiz Otavio O Souza
					$radvdconf .= "\t\tDeprecatePrefix on;\n";
217 cf3904bd Phil Davis
					switch ($dhcpv6ifconf['ramode']) {
218 686e53c0 Chris Buechler
						case "managed":
219
							$radvdconf .= "\t\tAdvOnLink on;\n";
220
							$radvdconf .= "\t\tAdvAutonomous off;\n";
221
							break;
222
						case "router":
223
							$radvdconf .= "\t\tAdvOnLink off;\n";
224
							$radvdconf .= "\t\tAdvAutonomous off;\n";
225
							break;
226
						case "assist":
227
							$radvdconf .= "\t\tAdvOnLink on;\n";
228
							$radvdconf .= "\t\tAdvAutonomous on;\n";
229
							break;
230
						case "unmanaged":
231
							$radvdconf .= "\t\tAdvOnLink on;\n";
232
							$radvdconf .= "\t\tAdvAutonomous on;\n";
233 086cf944 Phil Davis
							break;
234 686e53c0 Chris Buechler
					}
235
					$radvdconf .= "\t};\n";
236
				}
237
			}
238
		}
239 2a5960b0 Luiz Otavio O Souza
		$radvdconf .= "\troute ::/0 {\n";
240 6aefdd43 Tomas Krajca
		switch ($dhcpv6ifconf['rapriority']) {
241
			case "low":
242
				$radvdconf .= "\t\tAdvRoutePreference low;\n";
243
				break;
244
			case "high":
245
				$radvdconf .= "\t\tAdvRoutePreference high;\n";
246
				break;
247
			default:
248
				$radvdconf .= "\t\tAdvRoutePreference medium;\n";
249
				break;
250
		}
251 55b55478 znerol
252
		if ($rasrcaddr) {
253
			$radvdconf .= "\t\tRemoveRoute off;\n";
254
		}
255
		else {
256
			$radvdconf .= "\t\tRemoveRoute on;\n";
257
		}
258 2a5960b0 Luiz Otavio O Souza
		$radvdconf .= "\t};\n";
259 8859c0a6 smos
260 f535d5a0 Seth Mos
		/* add DNS servers */
261 09646aef luckman212
		if ($dhcpv6ifconf['radvd-dns'] != 'disabled') {
262 e26ad76e luckman212
			$dnslist = array();
263
			if (isset($dhcpv6ifconf['rasamednsasdhcp6']) && is_array($dhcpv6ifconf['dnsserver']) && !empty($dhcpv6ifconf['dnsserver'])) {
264
				foreach ($dhcpv6ifconf['dnsserver'] as $server) {
265
					if (is_ipaddrv6($server)) {
266
						$dnslist[] = $server;
267
					}
268
				}
269
			} elseif (!isset($dhcpv6ifconf['rasamednsasdhcp6']) && isset($dhcpv6ifconf['radnsserver']) && is_array($dhcpv6ifconf['radnsserver'])) {
270
				foreach ($dhcpv6ifconf['radnsserver'] as $server) {
271
					if (is_ipaddrv6($server)) {
272
						$dnslist[] = $server;
273
					}
274
				}
275 532a1a0e Reid Linnemann
			} elseif (config_path_enabled('dnsmasq') || config_path_enabled('unbound')) {
276 e26ad76e luckman212
				$dnslist[] = get_interface_ipv6($realif);
277 532a1a0e Reid Linnemann
			} else {
278
				foreach (config_get_path('system/dnsserver', []) as $server) {
279 e26ad76e luckman212
					if (is_ipaddrv6($server)) {
280
						$dnslist[] = $server;
281
					}
282 61e047a5 Phil Davis
				}
283
			}
284 96270d7c Viktor G
			$raadvdnsslifetime = $ramaxrtradvinterval * 3;
285 e26ad76e luckman212
			if (count($dnslist) > 0) {
286
				$dnsstring = implode(" ", $dnslist);
287
				if ($dnsstring <> "") {
288 b63b534c Christian McDonald
					/*
289 54b3109f Viktor G
					 * The value of Lifetime SHOULD by default be at least
290
					 * 3 * MaxRtrAdvInterval, where MaxRtrAdvInterval is the
291
					 * maximum RA interval as defined in [RFC4861].
292 b63b534c Christian McDonald
					 * see https://redmine.pfsense.org/issues/11105
293 54b3109f Viktor G
					 */
294
					$radvdconf .= "\tRDNSS {$dnsstring} {\n";
295 96270d7c Viktor G
					$radvdconf .= "\t\tAdvRDNSSLifetime {$raadvdnsslifetime};\n";
296 54b3109f Viktor G
					$radvdconf .= "\t};\n";
297 61e047a5 Phil Davis
				}
298
			}
299 e26ad76e luckman212
300
			$searchlist = array();
301
			$domainsearchlist = explode(';', $dhcpv6ifconf['radomainsearchlist']);
302
			foreach ($domainsearchlist as $sd) {
303
				$sd = trim($sd);
304
				if (is_hostname($sd)) {
305
					$searchlist[] = $sd;
306 61e047a5 Phil Davis
				}
307 3aa114d5 Seth Mos
			}
308 e26ad76e luckman212
			if (count($searchlist) > 0) {
309
				$searchliststring = trim(implode(" ", $searchlist));
310 3a973ba4 znerol
			} else {
311
				$searchliststring = "";
312 61e047a5 Phil Davis
			}
313 532a1a0e Reid Linnemann
			$domain = config_get_path('system/domain');
314 e26ad76e luckman212
			if (!empty($dhcpv6ifconf['domain'])) {
315 b63b534c Christian McDonald
				/*
316 96270d7c Viktor G
				 * Lifetime SHOULD by default be at least 3 * MaxRtrAdvInterval
317 b63b534c Christian McDonald
				 * see https://redmine.pfsense.org/issues/12173
318 96270d7c Viktor G
				 */
319
				$radvdconf .= "\tDNSSL {$dhcpv6ifconf['domain']} {$searchliststring} {\n";
320
				$radvdconf .= "\t\tAdvDNSSLLifetime {$raadvdnsslifetime};\n";
321
				$radvdconf .= "\t};\n";
322 532a1a0e Reid Linnemann
			} elseif (!empty($domain)) {
323
				$radvdconf .= "\tDNSSL {$domain} {$searchliststring} {\n";
324 96270d7c Viktor G
				$radvdconf .= "\t\tAdvDNSSLLifetime {$raadvdnsslifetime};\n";
325
				$radvdconf .= "\t};\n";
326 1794ecbb jim-p
			}
327
		}
328 3f9cc8e4 smos
		$radvdconf .= "};\n";
329 ed395640 Seth Mos
	}
330
331 3f9cc8e4 smos
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
332 ed395640 Seth Mos
	foreach ($Iflist as $if => $ifdescr) {
333 532a1a0e Reid Linnemann
		if (empty(config_get_path("interfaces/{$if}/track6-interface")) ||
334
		    config_get_path("interfaces/{$if}/ipaddrv6") != 'track6') {
335 ed395640 Seth Mos
			continue;
336 61e047a5 Phil Davis
		}
337 532a1a0e Reid Linnemann
		if (!config_path_enabled("interfaces/{$if}")) {
338 d7d2dc52 smos
			continue;
339 61e047a5 Phil Davis
		}
340 532a1a0e Reid Linnemann
		if (config_get_path("dhcpdv6/{$if}/ramode") == "disabled") {
341 8e187daa Chris Buechler
			continue;
342
		}
343 92977616 Ermal
		/* Do not put in the config an interface which is down */
344 61e047a5 Phil Davis
		if (isset($blacklist[$if])) {
345 92977616 Ermal
			continue;
346 61e047a5 Phil Davis
		}
347 532a1a0e Reid Linnemann
		$trackif = config_get_path("interfaces/{$if}/track6-interface");
348
		if (empty(config_get_path("interfaces/{$trackif}"))) {
349 60c05056 Ermal
			continue;
350 61e047a5 Phil Davis
		}
351 60c05056 Ermal
352 2a5960b0 Luiz Otavio O Souza
		$realif = get_real_interface($if, "inet6");
353 61e047a5 Phil Davis
354 3f9cc8e4 smos
		/* prevent duplicate entries, manual overrides */
355 61e047a5 Phil Davis
		if (isset($radvdifs[$realif])) {
356 7492f21d smos
			continue;
357 61e047a5 Phil Davis
		}
358 7492f21d smos
359 ed395640 Seth Mos
		$ifcfgipv6 = get_interface_ipv6($if);
360 61e047a5 Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
361 75aec77a Ermal
			$subnetv6 = "::";
362
			$ifcfgsnv6 = "64";
363
		} else {
364
			$ifcfgsnv6 = get_interface_subnetv6($if);
365
			$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
366
		}
367 60c05056 Ermal
		$radvdifs[$realif] = $realif;
368 c18a10cc smos
369 532a1a0e Reid Linnemann
		$autotype = config_get_path("interfaces/{$trackif}/ipaddrv6");
370 61e047a5 Phil Davis
371 2568e151 Christian McDonald
		if (g_get('debug')) {
372 dfac167c Ermal
			log_error("configuring RA on {$if} for type {$autotype} radvd subnet {$subnetv6}/{$ifcfgsnv6}");
373 61e047a5 Phil Davis
		}
374 668e8961 smos
375 60c05056 Ermal
		$radvdconf .= "# Generated config for {$autotype} delegation from {$trackif} on {$if}\n";
376
		$radvdconf .= "interface {$realif} {\n";
377
		$radvdconf .= "\tAdvSendAdvert on;\n";
378 c3099b79 Chris Buechler
		if (is_numericint($dhcpv6ifconf['raminrtradvinterval'])) {
379
			$radvdconf .= "\tMinRtrAdvInterval {$dhcpv6ifconf['raminrtradvinterval']};\n";
380
		} else {
381 a1eef308 jim-p
			$radvdconf .= "\tMinRtrAdvInterval 200;\n";
382 c3099b79 Chris Buechler
                }
383
		if (is_numericint($dhcpv6ifconf['ramaxrtradvinterval'])) {
384
			$radvdconf .= "\tMaxRtrAdvInterval {$dhcpv6ifconf['ramaxrtradvinterval']};\n";
385 99dfecb7 Renato Botelho do Couto
			$raadvdnsslifetime = $dhcpv6ifconf['ramaxrtradvinterval'] * 3;
386 c3099b79 Chris Buechler
		} else {
387 a1eef308 jim-p
			$radvdconf .= "\tMaxRtrAdvInterval 600;\n";
388 7628b091 jim-p
			$raadvdnsslifetime = 1800;
389 c3099b79 Chris Buechler
		}
390 60c05056 Ermal
		$mtu = get_interface_mtu($realif);
391 61e047a5 Phil Davis
		if (is_numeric($mtu)) {
392 60c05056 Ermal
			$radvdconf .= "\tAdvLinkMTU {$mtu};\n";
393 61e047a5 Phil Davis
		} else {
394 60c05056 Ermal
			$radvdconf .= "\tAdvLinkMTU 1280;\n";
395 61e047a5 Phil Davis
		}
396 60c05056 Ermal
		$radvdconf .= "\tAdvOtherConfigFlag on;\n";
397 f8f8d131 Chris Buechler
		$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
398 60c05056 Ermal
		$radvdconf .= "\t\tAdvOnLink on;\n";
399
		$radvdconf .= "\t\tAdvAutonomous on;\n";
400
		$radvdconf .= "\t};\n";
401
402
		/* add DNS servers */
403 09646aef luckman212
		if ($dhcpv6ifconf['radvd-dns'] != 'disabled') {
404 e26ad76e luckman212
			$dnslist = array();
405 532a1a0e Reid Linnemann
			if (config_path_enabled('dnsmasq') || config_path_enabled('unbound')) {
406 e26ad76e luckman212
				$dnslist[] = $ifcfgipv6;
407 532a1a0e Reid Linnemann
			} else {
408
				foreach (config_get_path('system/dnsserver', []) as $server) {
409 e26ad76e luckman212
					if (is_ipaddrv6($server)) {
410
						$dnslist[] = $server;
411
					}
412 61e047a5 Phil Davis
				}
413 60c05056 Ermal
			}
414 e26ad76e luckman212
			if (count($dnslist) > 0) {
415
				$dnsstring = implode(" ", $dnslist);
416
				if (!empty($dnsstring)) {
417
					$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
418
				}
419
			}
420 532a1a0e Reid Linnemann
			$domain = config_get_path('system/domain');
421
			if (!empty($domain)) {
422
				$radvdconf .= "\tDNSSL {$domain} {\n";
423 96270d7c Viktor G
				$radvdconf .= "\t\tAdvDNSSLLifetime {$raadvdnsslifetime};\n";
424
				$radvdconf .= "\t};\n";
425 61e047a5 Phil Davis
			}
426 60c05056 Ermal
		}
427
		$radvdconf .= "};\n";
428 668e8961 smos
	}
429
430 928d4416 Ermal
	/* write radvd.conf */
431 abdd01f5 Ermal
	if (!@file_put_contents("{$g['varetc_path']}/radvd.conf", $radvdconf)) {
432 e8c516a0 Phil Davis
		log_error(gettext("Error: cannot open radvd.conf in services_radvd_configure()."));
433 61e047a5 Phil Davis
		if (platform_booting()) {
434 abdd01f5 Ermal
			printf("Error: cannot open radvd.conf in services_radvd_configure().\n");
435 61e047a5 Phil Davis
		}
436 abdd01f5 Ermal
	}
437 928d4416 Ermal
	unset($radvdconf);
438 d57293a4 Seth Mos
439 abdd01f5 Ermal
	if (count($radvdifs) > 0) {
440 61e047a5 Phil Davis
		if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
441 abdd01f5 Ermal
			sigkillbypid("{$g['varrun_path']}/radvd.pid", "HUP");
442 61e047a5 Phil Davis
		} else {
443 abdd01f5 Ermal
			mwexec("/usr/local/sbin/radvd -p {$g['varrun_path']}/radvd.pid -C {$g['varetc_path']}/radvd.conf -m syslog");
444 61e047a5 Phil Davis
		}
445 6afeb202 smos
	} else {
446
		/* we need to shut down the radvd cleanly, it will send out the prefix
447
		 * information with a lifetime of 0 to notify clients of a (possible) new prefix */
448 abdd01f5 Ermal
		if (isvalidpid("{$g['varrun_path']}/radvd.pid")) {
449 4864d7f6 Josh Soref
			log_error(gettext("Shutting down Router Advertisement daemon cleanly"));
450 abdd01f5 Ermal
			killbypid("{$g['varrun_path']}/radvd.pid");
451 dfac167c Ermal
			@unlink("{$g['varrun_path']}/radvd.pid");
452 6afeb202 smos
		}
453 d57293a4 Seth Mos
	}
454
	return 0;
455
}
456
457 92977616 Ermal
function services_dhcpd_configure($family = "all", $blacklist = array()) {
458 532a1a0e Reid Linnemann
	global $g;
459 2fb056d8 Seth Mos
460 c69ea005 NewEraCracker
	$dhcpdconfigurelck = lock("dhcpdconfigure", LOCK_EX);
461
462 2fb056d8 Seth Mos
	/* configure DHCPD chroot once */
463 4de8f7ba Phil Davis
	$fd = fopen("{$g['tmp_path']}/dhcpd.sh", "w");
464 d6c82882 Ermal
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}\n");
465 d605ac4d Christian McDonald
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
466 d6c82882 Ermal
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
467
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
468
	fwrite($fd, "/bin/mkdir -p {$g['dhcpd_chroot_path']}/var/run\n");
469
	fwrite($fd, "/usr/sbin/chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
470 d605ac4d Christian McDonald
	fwrite($fd, "/sbin/mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
471 2fb056d8 Seth Mos
	fclose($fd);
472 7ddc0080 Christian McDonald
473 2fb056d8 Seth Mos
	mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh");
474
475 61e047a5 Phil Davis
	if ($family == "all" || $family == "inet") {
476 3084ba6e Ermal
		services_dhcpdv4_configure();
477 61e047a5 Phil Davis
	}
478 3084ba6e Ermal
	if ($family == "all" || $family == "inet6") {
479 92977616 Ermal
		services_dhcpdv6_configure($blacklist);
480
		services_radvd_configure($blacklist);
481 3084ba6e Ermal
	}
482 c69ea005 NewEraCracker
483
	unlock($dhcpdconfigurelck);
484 2fb056d8 Seth Mos
}
485 1c903aa4 Ermal
486 2fb056d8 Seth Mos
function services_dhcpdv4_configure() {
487 532a1a0e Reid Linnemann
	global $g;
488 3df2dbfd jim-p
	$need_ddns_updates = false;
489
	$ddns_zones = array();
490 107e8acc Ovidiu Predescu
491 2568e151 Christian McDonald
	if (g_get('services_dhcp_server_enable') == false) {
492 e3a13b00 Scott Ullrich
		return;
493 61e047a5 Phil Davis
	}
494 e3a13b00 Scott Ullrich
495 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
496 acd910bf Scott Ullrich
		$mt = microtime();
497 2fb056d8 Seth Mos
		echo "services_dhcpdv4_configure($if) being called $mt\n";
498 acd910bf Scott Ullrich
	}
499 107e8acc Ovidiu Predescu
500 af25d415 Chris Buechler
	/* kill any running dhcpd */
501 61e047a5 Phil Davis
	if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid")) {
502 ed395640 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
503 61e047a5 Phil Davis
	}
504 a25183c5 Scott Ullrich
505 15be1722 Ermal Luçi
	/* DHCP enabled on any interfaces? */
506 61e047a5 Phil Davis
	if (!is_dhcp_server_enabled()) {
507 15be1722 Ermal Luçi
		return 0;
508 61e047a5 Phil Davis
	}
509 15be1722 Ermal Luçi
510 532a1a0e Reid Linnemann
	$syscfg = config_get_path('system');
511
	init_config_arr(['dhcpd']);
512
	$dhcpdcfg = config_get_path('dhcpd');
513 6f9b8073 Ermal Luçi
	$Iflist = get_configured_interface_list();
514 107e8acc Ovidiu Predescu
515 3ad6b569 Phil Davis
	/* Only consider DNS servers with IPv4 addresses for the IPv4 DHCP server. */
516
	$dns_arrv4 = array();
517 68169a55 jim-p
	if (is_array($syscfg['dnsserver'])) {
518 61e047a5 Phil Davis
		foreach ($syscfg['dnsserver'] as $dnsserver) {
519 68169a55 jim-p
			if (is_ipaddrv4($dnsserver)) {
520
				$dns_arrv4[] = $dnsserver;
521
			}
522 3ad6b569 Phil Davis
		}
523
	}
524
525 61e047a5 Phil Davis
	if (platform_booting()) {
526 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP service...");
527 61e047a5 Phil Davis
	} else {
528 5b237745 Scott Ullrich
		sleep(1);
529 61e047a5 Phil Davis
	}
530 a25183c5 Scott Ullrich
531 518030b3 Scott Ullrich
	$custoptions = "";
532 107e8acc Ovidiu Predescu
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
533 fae6b2c0 jim-p
		if (empty($dhcpifconf)) {
534
			continue;
535
		}
536 284878d7 Viktor G
		$idx = 0;
537
		$httpclient = false;
538 61e047a5 Phil Davis
		if (is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
539
			foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
540
				if (!empty($item['type'])) {
541 678dfd0f Erik Fonnesbeck
					$itemtype = $item['type'];
542 61e047a5 Phil Davis
				} else {
543 678dfd0f Erik Fonnesbeck
					$itemtype = "text";
544 61e047a5 Phil Davis
				}
545 678dfd0f Erik Fonnesbeck
				$custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = {$itemtype};\n";
546 284878d7 Viktor G
				if (($item['type'] == "text") &&
547
				    ($item['number'] == 60) &&
548
				    (base64_decode($item['value']) == "HTTPClient")) {
549
					$httpclient = true;
550 b63b534c Christian McDonald
				}
551 284878d7 Viktor G
				$idx++;
552 518030b3 Scott Ullrich
			}
553
		}
554 b68d8fe6 Viktor G
		if (!empty($dhcpifconf['uefihttpboot']) && isset($dhcpifconf['netboot']) && !$httpclient) {
555 284878d7 Viktor G
			$custoptions .= "option custom-{$dhcpif}-{$idx} code 60 = text;\n";
556
		}
557 28598720 Phil Davis
		if (is_array($dhcpifconf['pool'])) {
558
			foreach ($dhcpifconf['pool'] as $poolidx => $poolconf) {
559 284878d7 Viktor G
				$idx = 0;
560
				$httpclient = false;
561 28598720 Phil Davis
				if (is_array($poolconf['numberoptions']) && is_array($poolconf['numberoptions']['item'])) {
562
					foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
563
						if (!empty($item['type'])) {
564
							$itemtype = $item['type'];
565
						} else {
566
							$itemtype = "text";
567
						}
568
						$custoptions .= "option custom-{$dhcpif}-{$poolidx}-{$itemidx} code {$item['number']} = {$itemtype};\n";
569 284878d7 Viktor G
						if (($item['type'] == "text") &&
570
						    ($item['number'] == 60) &&
571
						    (base64_decode($item['value']) == "HTTPClient")) {
572
							$httpclient = true;
573 b63b534c Christian McDonald
						}
574 284878d7 Viktor G
						$idx++;
575 28598720 Phil Davis
					}
576
				}
577 b68d8fe6 Viktor G
				if (!empty($poolconf['uefihttpboot']) && isset($poolconf['netboot']) && !$httpclient) {
578 284878d7 Viktor G
					$custoptions .= "option custom-{$dhcpif}-{$poolidx}-{$idx} code 60 = text;\n";
579
				}
580 28598720 Phil Davis
			}
581
		}
582 3dd5090f Viktor Gurov
		if (is_array($dhcpifconf['staticmap'])) {
583
			$i = 0;
584
			foreach ($dhcpifconf['staticmap'] as $sm) {
585 8645d4c2 jim-p
				if (empty($sm)) {
586
					continue;
587
				}
588 5c5a7bc8 Viktor G
				$idx = 0;
589
				$httpclient = false;
590 3dd5090f Viktor Gurov
				if (is_array($sm['numberoptions']) && is_array($sm['numberoptions']['item'])) {
591
					foreach ($sm['numberoptions']['item'] as $itemidx => $item) {
592
						if (!empty($item['type'])) {
593
							$itemtype = $item['type'];
594
						} else {
595
							$itemtype = "text";
596
						}
597
						$custoptions .= "option custom-s_{$dhcpif}_{$i}-{$itemidx} code {$item['number']} = {$itemtype};\n";
598
					}
599 5c5a7bc8 Viktor G
					if (($item['type'] == "text") &&
600
					    ($item['number'] == 60) &&
601
					    (base64_decode($item['value']) == "HTTPClient")) {
602
						$httpclient = true;
603 b63b534c Christian McDonald
					}
604 5c5a7bc8 Viktor G
					$idx++;
605
				}
606 b68d8fe6 Viktor G
				if (!empty($sm['uefihttpboot']) && isset($sm['netboot']) && !$httpclient) {
607 5c5a7bc8 Viktor G
					$custoptions .= "option custom-s_{$dhcpif}_{$i}-{$idx} code 60 = text;\n";
608 3dd5090f Viktor Gurov
				}
609
				$i++;
610
			}
611
		}
612 518030b3 Scott Ullrich
	}
613 4cab31d0 Scott Ullrich
614 5b237745 Scott Ullrich
	$dhcpdconf = <<<EOD
615 107e8acc Ovidiu Predescu
616 5b237745 Scott Ullrich
option domain-name "{$syscfg['domain']}";
617 6c23757b Martin Fuchs
option ldap-server code 95 = text;
618 9be23653 Martin Fuchs
option domain-search-list code 119 = text;
619 fdb116a9 Donald A. Cupp Jr
option arch code 93 = unsigned integer 16; # RFC4578
620 518030b3 Scott Ullrich
{$custoptions}
621 5b237745 Scott Ullrich
default-lease-time 7200;
622
max-lease-time 86400;
623
log-facility local7;
624 175fe82b Scott Ullrich
one-lease-per-client true;
625 436a0f50 Scott Ullrich
deny duplicates;
626 87019fc4 Andres Petralli
update-conflict-detection false;
627 5b237745 Scott Ullrich
628
EOD;
629 a25183c5 Scott Ullrich
630 a6e1c192 Viktor G
	/* take these settings from the first DHCP configured interface,
631 b63b534c Christian McDonald
	 * see https://redmine.pfsense.org/issues/10270
632 a6e1c192 Viktor G
	 * TODO: Global Settings tab, see https://redmine.pfsense.org/issues/5080 */
633
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
634 fae6b2c0 jim-p
		if (empty($dhcpifconf)) {
635
			continue;
636
		}
637 a6e1c192 Viktor G
		if (!isset($dhcpifconf['disableauthoritative'])) {
638
			$dhcpdconf .= "authoritative;\n";
639
		}
640 d8912c6b Chris Buechler
641 a6e1c192 Viktor G
		if (isset($dhcpifconf['alwaysbroadcast'])) {
642
			$dhcpdconf .= "always-broadcast on\n";
643
		}
644 5252b98d Scott Ullrich
645 a6e1c192 Viktor G
		// OMAPI Settings
646
		if (isset($dhcpifconf['omapi_port']) && is_numeric($dhcpifconf['omapi_port'])) {
647
			$dhcpdconf .= <<<EOD
648 6df10582 Erik Schaeffer
649
key omapi_key {
650
  algorithm {$dhcpifconf['omapi_key_algorithm']};
651
  secret "{$dhcpifconf['omapi_key']}";
652
};
653
omapi-port {$dhcpifconf['omapi_port']};
654
omapi-key omapi_key;
655
656
EOD;
657 a6e1c192 Viktor G
658
		}
659
		break;
660 6df10582 Erik Schaeffer
	}
661
662 5b237745 Scott Ullrich
	$dhcpdifs = array();
663 3f141c9d Phil Davis
	$enable_add_routers = false;
664 c08a5659 smos
	$gateways_arr = return_gateways_array();
665
	/* only add a routers line if the system has any IPv4 gateway at all */
666
	/* a static route has a gateway, manually overriding this field always works */
667 61e047a5 Phil Davis
	foreach ($gateways_arr as $gwitem) {
668
		if ($gwitem['ipprotocol'] == "inet") {
669 3f141c9d Phil Davis
			$enable_add_routers = true;
670 c08a5659 smos
			break;
671
		}
672
	}
673 c7f44ae0 Scott Ullrich
674 4494cf6a Chris Buechler
	/*    loop through and determine if we need to setup
675 8fa56d1f Scott Ullrich
	 *    failover peer "bleh" entries
676
	 */
677
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
678 fae6b2c0 jim-p
		if (empty($dhcpifconf)) {
679
			continue;
680
		}
681 53f32329 Scott Ullrich
682 532a1a0e Reid Linnemann
		if (!config_path_enabled("interfaces/{$dhcpif}")) {
683 57006646 Renato Botelho
			continue;
684 61e047a5 Phil Davis
		}
685 57006646 Renato Botelho
686 09f11c71 jim-p
		interfaces_staticarp_configure($dhcpif);
687
688 61e047a5 Phil Davis
		if (!isset($dhcpifconf['enable'])) {
689 6f9b8073 Ermal Luçi
			continue;
690 61e047a5 Phil Davis
		}
691 6f9b8073 Ermal Luçi
692 61e047a5 Phil Davis
		if ($dhcpifconf['failover_peerip'] <> "") {
693 a01f8bfc Ermal
			$intip = get_interface_ip($dhcpif);
694 8fa56d1f Scott Ullrich
			/*
695
			 *    yep, failover peer is defined.
696
			 *    does it match up to a defined vip?
697
			 */
698 d2edbd8a Scott Ullrich
			$skew = 110;
699 532a1a0e Reid Linnemann
			$vips = config_get_path('virtualip/vip', []);
700
			if (!empty($vips)) {
701
				foreach ($vips as $vipent) {
702 97e6ec09 Luiz Souza
					if ($vipent['mode'] != 'carp') {
703
						continue;
704
					}
705 61e047a5 Phil Davis
					if ($vipent['interface'] == $dhcpif) {
706 532a1a0e Reid Linnemann
						$ipaddr = config_get_path("interfaces/{$dhcpif}/ipaddr");
707
						$subnet = config_get_path("interfaces/{$dhcpif}/subnet");
708
						$carp_nw = gen_subnet($ipaddr,$subnet);
709
						$carp_nw .= "/{$subnet}";
710 97e6ec09 Luiz Souza
						if (ip_in_subnet($dhcpifconf['failover_peerip'], $carp_nw)) {
711 a01f8bfc Ermal
							/* this is the interface! */
712 61e047a5 Phil Davis
							if (is_numeric($vipent['advskew']) && (intval($vipent['advskew']) < 20)) {
713 a01f8bfc Ermal
								$skew = 0;
714
								break;
715
							}
716
						}
717 6181b36f Scott Ullrich
					}
718 8fa56d1f Scott Ullrich
				}
719 25066204 Scott Ullrich
			} else {
720 959dc96b Chris Buechler
				log_error(gettext("Warning!  DHCP Failover setup and no CARP virtual IPs defined!"));
721 8fa56d1f Scott Ullrich
			}
722 61e047a5 Phil Davis
			if ($skew > 10) {
723 8fa56d1f Scott Ullrich
				$type = "secondary";
724 0e93097a Scott Ullrich
				$my_port = "520";
725
				$peer_port = "519";
726 faab657a jim-p
				$dhcpdconf_pri = '';
727 8fa56d1f Scott Ullrich
			} else {
728 0e93097a Scott Ullrich
				$my_port = "519";
729
				$peer_port = "520";
730 8fa56d1f Scott Ullrich
				$type = "primary";
731 4de8f7ba Phil Davis
				$dhcpdconf_pri = "split 128;\n";
732 1a0bb737 Scott Ullrich
				$dhcpdconf_pri .= "  mclt 600;\n";
733 8fa56d1f Scott Ullrich
			}
734 a01f8bfc Ermal
735
			if (is_ipaddrv4($intip)) {
736 61e047a5 Phil Davis
				$dhcpdconf .= <<<EOPP
737 d5e4f7c9 jim-p
failover peer "dhcp_{$dhcpif}" {
738 8fa56d1f Scott Ullrich
  {$type};
739
  address {$intip};
740 0e93097a Scott Ullrich
  port {$my_port};
741 8fa56d1f Scott Ullrich
  peer address {$dhcpifconf['failover_peerip']};
742 0e93097a Scott Ullrich
  peer port {$peer_port};
743 2cd5ce14 Scott Ullrich
  max-response-delay 10;
744 b865d178 Scott Ullrich
  max-unacked-updates 10;
745
  {$dhcpdconf_pri}
746 b259d1c6 Scott Ullrich
  load balance max seconds 3;
747 8fa56d1f Scott Ullrich
}
748 959dc96b Chris Buechler
\n
749 8fa56d1f Scott Ullrich
EOPP;
750 a01f8bfc Ermal
			}
751 8fa56d1f Scott Ullrich
		}
752
	}
753
754 5b237745 Scott Ullrich
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
755 fae6b2c0 jim-p
		if (empty($dhcpifconf)) {
756
			continue;
757
		}
758 a25183c5 Scott Ullrich
759 3df2dbfd jim-p
		$newzone = array();
760 532a1a0e Reid Linnemann
		$ifcfg = config_get_path("interfaces/{$dhcpif}");
761 a25183c5 Scott Ullrich
762 61e047a5 Phil Davis
		if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif])) {
763 5b237745 Scott Ullrich
			continue;
764 61e047a5 Phil Davis
		}
765 a55e9c70 Ermal Lu?i
		$ifcfgip = get_interface_ip($dhcpif);
766
		$ifcfgsn = get_interface_subnet($dhcpif);
767
		$subnet = gen_subnet($ifcfgip, $ifcfgsn);
768
		$subnetmask = gen_subnet_mask($ifcfgsn);
769 a25183c5 Scott Ullrich
770 61e047a5 Phil Davis
		if (!is_ipaddr($subnet)) {
771 85e3f445 Ermal
			continue;
772 61e047a5 Phil Davis
		}
773 85e3f445 Ermal
774 cba980f6 jim-p
		$all_pools = array();
775
		$all_pools[] = $dhcpifconf;
776
		if (is_array($dhcpifconf['pool'])) {
777
			$all_pools = array_merge($all_pools, $dhcpifconf['pool']);
778
		}
779
780 5b237745 Scott Ullrich
		$dnscfg = "";
781 a25183c5 Scott Ullrich
782 5b237745 Scott Ullrich
		if ($dhcpifconf['domain']) {
783
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
784
		}
785 107e8acc Ovidiu Predescu
786 61e047a5 Phil Davis
		if ($dhcpifconf['domainsearchlist'] <> "") {
787 a3de8b9e Pierre POMES
			$dnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpifconf['domainsearchlist'])) . "\";\n";
788 84931046 jim-p
		}
789 9be23653 Martin Fuchs
790 4e9cd828 Seth Mos
		if (isset($dhcpifconf['ddnsupdate'])) {
791 3df2dbfd jim-p
			$need_ddns_updates = true;
792
			$newzone = array();
793 61e047a5 Phil Davis
			if ($dhcpifconf['ddnsdomain'] <> "") {
794 3df2dbfd jim-p
				$newzone['domain-name'] = $dhcpifconf['ddnsdomain'];
795 4e9cd828 Seth Mos
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
796 3df2dbfd jim-p
			} else {
797 532a1a0e Reid Linnemann
				$newzone['domain-name'] = config_get_path('system/domain');
798 3df2dbfd jim-p
			}
799 cf688344 Renato Botelho
800 67784aa6 Steve Beaver
			if (empty($dhcpifconf['ddnsclientupdates'])) {
801
				$ddnsclientupdates = 'allow';
802
			} else {
803
				$ddnsclientupdates = $dhcpifconf['ddnsclientupdates'];
804
			}
805
806
			$dnscfg .= "	{$ddnsclientupdates} client-updates;\n";
807
808 cf688344 Renato Botelho
			$revsubnet = array_reverse(explode('.',$subnet));
809
810 d95e86dc Viktor Gurov
			$subnet_mask_bits = 32 - $ifcfgsn;
811
			$start_octet = $subnet_mask_bits >> 3;
812
			$octet_mask_bits = $subnet_mask_bits & ($subnet_mask_bits % 8);
813
			if ($octet_mask_bits) {
814
			    $octet_mask = (1 << $octet_mask_bits) - 1;
815
			    $octet_start = $revsubnet[$start_octet] & ~$octet_mask;
816
			    $revsubnet[$start_octet] = $octet_start . "-" . ($octet_start + $octet_mask);
817 cf688344 Renato Botelho
			}
818
819
			$ptr_domain = '';
820
			for ($octet = 0; $octet <= 3; $octet++) {
821
				if ($octet < $start_octet) {
822
					continue;
823 61e047a5 Phil Davis
				}
824 c9fd7ee0 Chris Buechler
				$ptr_domain .= ((empty($ptr_domain) && $ptr_domain !== "0") ? '' : '.');
825 cf688344 Renato Botelho
				$ptr_domain .= $revsubnet[$octet];
826 4e9cd828 Seth Mos
			}
827 cf688344 Renato Botelho
			$ptr_domain .= ".in-addr.arpa";
828
			$newzone['ptr-domain'] = $ptr_domain;
829
			unset($ptr_domain, $revsubnet, $start_octet);
830 4e9cd828 Seth Mos
		}
831
832 aff9d6ab Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
833 8ee01642 Scott Ullrich
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
834 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
835 3df2dbfd jim-p
				$newzone['dns-servers'] = $dhcpifconf['dnsserver'];
836 61e047a5 Phil Davis
			}
837 532a1a0e Reid Linnemann
		} else if (config_path_enabled('dnsmasq')) {
838 a55e9c70 Ermal Lu?i
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
839 61e047a5 Phil Davis
			if ($newzone['domain-name'] && is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
840 3df2dbfd jim-p
				$newzone['dns-servers'] = $syscfg['dnsserver'];
841 61e047a5 Phil Davis
			}
842 532a1a0e Reid Linnemann
		} else if (config_path_enabled('unbound')) {
843 88a0937d Chris Buechler
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
844 3ad6b569 Phil Davis
		} else if (!empty($dns_arrv4)) {
845
			$dnscfg .= "	option domain-name-servers " . join(",", $dns_arrv4) . ";";
846 61e047a5 Phil Davis
			if ($newzone['domain-name']) {
847 3ad6b569 Phil Davis
				$newzone['dns-servers'] = $dns_arrv4;
848 61e047a5 Phil Davis
			}
849 aff9d6ab Scott Ullrich
		}
850
851 61e047a5 Phil Davis
		/* Create classes - These all contain comma separated lists. Join them into one
852 cba980f6 jim-p
		   big comma separated string then split them all up. */
853
		$all_mac_strings = array();
854
		if (is_array($dhcpifconf['pool'])) {
855 61e047a5 Phil Davis
			foreach ($all_pools as $poolconf) {
856 cba980f6 jim-p
				$all_mac_strings[] = $poolconf['mac_allow'];
857
				$all_mac_strings[] = $poolconf['mac_deny'];
858
			}
859
		}
860
		$all_mac_strings[] = $dhcpifconf['mac_allow'];
861
		$all_mac_strings[] = $dhcpifconf['mac_deny'];
862 7fd93993 Ermal LUÇI
		if (!empty($all_mac_strings)) {
863 4de8f7ba Phil Davis
			$all_mac_list = array_unique(explode(',', implode(',', $all_mac_strings)));
864
			foreach ($all_mac_list as $mac) {
865
				if (empty($mac)) {
866
					continue;
867
				}
868
				$dhcpdconf .= 'class "' . str_replace(':', '', $mac) . '" {' . "\n";
869
				// Skip the first octet of the MAC address - for media type, typically Ethernet ("01") and match the rest.
870
				$dhcpdconf .= '	match if substring (hardware, 1, ' . (substr_count($mac, ':') + 1) . ') = ' . $mac . ';' . "\n";
871
				$dhcpdconf .= '}' . "\n";
872 61e047a5 Phil Davis
			}
873 1f1a08c8 jim-p
		}
874
875 35bc0edf reb00tz
		// instantiate class before pool definition
876
		$dhcpdconf .= "class \"s_{$dhcpif}\" {\n	match pick-first-value (option dhcp-client-identifier, hardware);\n}\n";
877
878 85e3f445 Ermal
		$dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n";
879 c7f44ae0 Scott Ullrich
880 61e047a5 Phil Davis
		// Setup pool options
881 28598720 Phil Davis
		foreach ($all_pools as $all_pools_idx => $poolconf) {
882 9228166f Phil Davis
			if (!(ip_in_subnet($poolconf['range']['from'], "{$subnet}/{$ifcfgsn}") && ip_in_subnet($poolconf['range']['to'], "{$subnet}/{$ifcfgsn}"))) {
883
				// If the user has changed the subnet from the interfaces page and applied,
884
				// but has not updated the DHCP range, then the range to/from of the pool can be outside the subnet.
885 f8cc55bf Phil Davis
				// This can also happen when implementing the batch of changes when the setup wizard reloads the new settings.
886 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);
887 f8cc55bf Phil Davis
				$do_file_notice = true;
888
				$conf_ipv4_address = $ifcfg['ipaddr'];
889
				$conf_ipv4_subnetmask = $ifcfg['subnet'];
890
				if (is_ipaddrv4($conf_ipv4_address) && is_subnet("{$conf_ipv4_address}/{$conf_ipv4_subnetmask}")) {
891
					$conf_subnet_base = gen_subnet($conf_ipv4_address, $conf_ipv4_subnetmask);
892
					if (ip_in_subnet($poolconf['range']['from'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}") &&
893
					    ip_in_subnet($poolconf['range']['to'], "{$conf_subnet_base}/{$conf_ipv4_subnetmask}")) {
894
						// Even though the running interface subnet does not match the pool range,
895
						// the interface subnet in the config file contains the pool range.
896
						// We are somewhere part-way through a settings reload, e.g. after running the setup wizard.
897
						// services_dhcpdv4_configure will be called again later when the new interface settings from
898
						// the config are applied and at that time everything will match up.
899
						// Ignore this pool on this interface for now and just log the error to the system log.
900
						log_error($error_msg);
901
						$do_file_notice = false;
902
					}
903
				}
904
				if ($do_file_notice) {
905
					file_notice("DHCP", $error_msg);
906
				}
907 9228166f Phil Davis
				continue;
908
			}
909 cba980f6 jim-p
			$dhcpdconf .= "	pool {\n";
910
			/* is failover dns setup? */
911
			if (is_array($poolconf['dnsserver']) && $poolconf['dnsserver'][0] <> "") {
912
				$dhcpdconf .= "		option domain-name-servers {$poolconf['dnsserver'][0]}";
913 61e047a5 Phil Davis
				if ($poolconf['dnsserver'][1] <> "") {
914 cba980f6 jim-p
					$dhcpdconf .= ",{$poolconf['dnsserver'][1]}";
915 61e047a5 Phil Davis
				}
916
				if ($poolconf['dnsserver'][2] <> "") {
917 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][2]}";
918 61e047a5 Phil Davis
				}
919
				if ($poolconf['dnsserver'][3] <> "") {
920 8cbb140a Phil Davis
					$dhcpdconf .= ",{$poolconf['dnsserver'][3]}";
921 61e047a5 Phil Davis
				}
922 cba980f6 jim-p
				$dhcpdconf .= ";\n";
923
			}
924
925
			/* allow/deny MACs */
926
			$mac_allow_list = array_unique(explode(',', $poolconf['mac_allow']));
927
			foreach ($mac_allow_list as $mac) {
928 61e047a5 Phil Davis
				if (empty($mac)) {
929 cba980f6 jim-p
					continue;
930 61e047a5 Phil Davis
				}
931 cba980f6 jim-p
				$dhcpdconf .= "		allow members of \"" . str_replace(':', '', $mac) . "\";\n";
932
			}
933 3475eb04 Andrew Pilloud
			$deny_action = "deny";
934 8209517d jim-p
			if (isset($poolconf['nonak']) && empty($poolconf['failover_peerip'])) {
935 3475eb04 Andrew Pilloud
				$deny_action = "ignore";
936
			}
937 cba980f6 jim-p
			$mac_deny_list = array_unique(explode(',', $poolconf['mac_deny']));
938
			foreach ($mac_deny_list as $mac) {
939 61e047a5 Phil Davis
				if (empty($mac)) {
940 cba980f6 jim-p
					continue;
941 61e047a5 Phil Davis
				}
942 773902ef Viktor G
				$dhcpdconf .= "		deny members of \"" . str_replace(':', '', $mac) . "\";\n";
943 cba980f6 jim-p
			}
944
945 61e047a5 Phil Davis
			if ($poolconf['failover_peerip'] <> "") {
946 3475eb04 Andrew Pilloud
				$dhcpdconf .= "		$deny_action dynamic bootp clients;\n";
947 61e047a5 Phil Davis
			}
948 cba980f6 jim-p
949 35bc0edf reb00tz
			// set pool MAC limitations
950 61e047a5 Phil Davis
			if (isset($poolconf['denyunknown'])) {
951 35bc0edf reb00tz
				if ($poolconf['denyunknown'] == "class") {
952
					$dhcpdconf .= "		allow members of \"s_{$dhcpif}\";\n";
953
					$dhcpdconf .= "		$deny_action unknown-clients;\n";
954
				} else if ($poolconf['denyunknown'] == "disabled") {
955
					// add nothing to $dhcpdconf; condition added to prevent next condition applying if ever engine changes such that: isset("disabled") == true
956
				} else {	// "catch-all" covering "enabled" value post-PR#4066, and covering non-upgraded boolean option (i.e. literal value "enabled")
957
					$dhcpdconf .= "		$deny_action unknown-clients;\n";
958
				}
959 61e047a5 Phil Davis
			}
960 cba980f6 jim-p
961 61e047a5 Phil Davis
			if ($poolconf['gateway'] && $poolconf['gateway'] != "none" && ($poolconf['gateway'] != $dhcpifconf['gateway'])) {
962 f9f6f7d4 jim-p
				$dhcpdconf .= "		option routers {$poolconf['gateway']};\n";
963 61e047a5 Phil Davis
			}
964 cba980f6 jim-p
965 61e047a5 Phil Davis
			if ($dhcpifconf['failover_peerip'] <> "") {
966 d5e4f7c9 jim-p
				$dhcpdconf .= "		failover peer \"dhcp_{$dhcpif}\";\n";
967 cba980f6 jim-p
			}
968
969
			$pdnscfg = "";
970
971
			if ($poolconf['domain'] && ($poolconf['domain'] != $dhcpifconf['domain'])) {
972
				$pdnscfg .= "		option domain-name \"{$poolconf['domain']}\";\n";
973
			}
974
975 61e047a5 Phil Davis
			if (!empty($poolconf['domainsearchlist']) && ($poolconf['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
976 cba980f6 jim-p
				$pdnscfg .= "		option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $poolconf['domainsearchlist'])) . "\";\n";
977
			}
978
979 7309ff39 Renato Botelho
			if (isset($poolconf['ddnsupdate'])) {
980 61e047a5 Phil Davis
				if (($poolconf['ddnsdomain'] <> "") && ($poolconf['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
981 cba980f6 jim-p
					$pdnscfg .= "		ddns-domainname \"{$poolconf['ddnsdomain']}\";\n";
982 61e047a5 Phil Davis
				}
983 cba980f6 jim-p
				$pdnscfg .= "		ddns-update-style interim;\n";
984
			}
985
986
			$dhcpdconf .= "{$pdnscfg}";
987 1f1a08c8 jim-p
988 cba980f6 jim-p
			// default-lease-time
989 61e047a5 Phil Davis
			if ($poolconf['defaultleasetime'] && ($poolconf['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
990 cba980f6 jim-p
				$dhcpdconf .= "		default-lease-time {$poolconf['defaultleasetime']};\n";
991 61e047a5 Phil Davis
			}
992 cba980f6 jim-p
993
			// max-lease-time
994 61e047a5 Phil Davis
			if ($poolconf['maxleasetime'] && ($poolconf['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
995 cba980f6 jim-p
				$dhcpdconf .= "		max-lease-time {$poolconf['maxleasetime']};\n";
996 61e047a5 Phil Davis
			}
997 cba980f6 jim-p
998 6d53301b Jose Luis Duran
			// ignore bootp
999
			if (isset($poolconf['ignorebootp'])) {
1000
				$dhcpdconf .= "		ignore bootp;\n";
1001
			}
1002
1003 11ee0c6d Brett Keller
			// ignore-client-uids
1004
			if (isset($poolconf['ignoreclientuids'])) {
1005
				$dhcpdconf .= "		ignore-client-uids true;\n";
1006
			}
1007
1008 cba980f6 jim-p
			// netbios-name*
1009 fa7b825f Renato Botelho
			if (is_array($poolconf['winsserver']) && $poolconf['winsserver'][0] && ($poolconf['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
1010 cba980f6 jim-p
				$dhcpdconf .= "		option netbios-name-servers " . join(",", $poolconf['winsserver']) . ";\n";
1011
				$dhcpdconf .= "		option netbios-node-type 8;\n";
1012
			}
1013 c7f44ae0 Scott Ullrich
1014 cba980f6 jim-p
			// ntp-servers
1015 61e047a5 Phil Davis
			if (is_array($poolconf['ntpserver']) && $poolconf['ntpserver'][0] && ($poolconf['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
1016 cba980f6 jim-p
				$dhcpdconf .= "		option ntp-servers " . join(",", $poolconf['ntpserver']) . ";\n";
1017 61e047a5 Phil Davis
			}
1018 cba980f6 jim-p
1019
			// tftp-server-name
1020 61e047a5 Phil Davis
			if (!empty($poolconf['tftp']) && ($poolconf['tftp'] != $dhcpifconf['tftp'])) {
1021 cba980f6 jim-p
				$dhcpdconf .= "		option tftp-server-name \"{$poolconf['tftp']}\";\n";
1022 61e047a5 Phil Davis
			}
1023 cba980f6 jim-p
1024 28598720 Phil Davis
			// Handle pool-specific options
1025
			$dhcpdconf .= "\n";
1026
			// Ignore the first pool, which is the "overall" pool when $all_pools_idx is 0 - those are put outside the pool block later
1027 284878d7 Viktor G
			$idx = 0;
1028
			$httpclient = false;
1029 b88050bb jim-p
			if (isset($poolconf['numberoptions']['item']) && is_array($poolconf['numberoptions']['item']) && ($all_pools_idx > 0)) {
1030 28598720 Phil Davis
				// Use the "real" pool index from the config, excluding the "overall" pool, and based from 0.
1031
				// This matches the way $poolidx was used when generating the $custoptions string earlier.
1032
				$poolidx = $all_pools_idx - 1;
1033
				foreach ($poolconf['numberoptions']['item'] as $itemidx => $item) {
1034
					$item_value = base64_decode($item['value']);
1035
					if (empty($item['type']) || $item['type'] == "text") {
1036 c507161d Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} \"{$item_value}\";\n";
1037 28598720 Phil Davis
					} else {
1038 c507161d Phil Davis
						$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$itemidx} {$item_value};\n";
1039 28598720 Phil Davis
					}
1040 284878d7 Viktor G
					if (($item['type'] == "text") &&
1041
					    ($item['number'] == 60) &&
1042
					    (base64_decode($item['value']) == "HTTPClient")) {
1043
						$httpclient = true;
1044 b63b534c Christian McDonald
					}
1045 284878d7 Viktor G
					$idx++;
1046 28598720 Phil Davis
				}
1047
			}
1048 b68d8fe6 Viktor G
			if (!empty($poolconf['uefihttpboot']) && isset($poolconf['netboot']) && !$httpclient &&
1049 284878d7 Viktor G
			    (!isset($dhcpifconf['uefihttpboot']) ||
1050
			    ($poolconf['uefihttpboot'] != $dhcpifconf['uefihttpboot']))) {
1051
				$dhcpdconf .= "		option custom-{$dhcpif}-{$poolidx}-{$idx} \"HTTPClient\";\n";
1052
			}
1053 28598720 Phil Davis
1054 cba980f6 jim-p
			// ldap-server
1055 61e047a5 Phil Davis
			if (!empty($poolconf['ldap']) && ($poolconf['ldap'] != $dhcpifconf['ldap'])) {
1056 cba980f6 jim-p
				$dhcpdconf .= "		option ldap-server \"{$poolconf['ldap']}\";\n";
1057 61e047a5 Phil Davis
			}
1058 cba980f6 jim-p
1059
			// net boot information
1060 61e047a5 Phil Davis
			if (isset($poolconf['netboot'])) {
1061 cba980f6 jim-p
				if (!empty($poolconf['nextserver']) && ($poolconf['nextserver'] != $dhcpifconf['nextserver'])) {
1062
					$dhcpdconf .= "		next-server {$poolconf['nextserver']};\n";
1063
				}
1064 9d775c75 Renato Botelho
1065 e5395534 Viktor G
				$pxe_files = array();
1066 9d775c75 Renato Botelho
				if (!empty($poolconf['filename']) &&
1067
				    (!isset($dhcpifconf['filename']) ||
1068
				    ($poolconf['filename'] != $dhcpifconf['filename']))) {
1069
					$filename = $poolconf['filename'];
1070
				}
1071 cf40cd17 Viktor G
				if (!empty($poolconf['uefihttpboot']) &&
1072
				    (!isset($dhcpifconf['uefihttpboot']) ||
1073
				    ($poolconf['uefihttpboot'] != $dhcpifconf['uefihttpboot']))) {
1074
					$pxe_files[] = array('HTTPClient', $poolconf['uefihttpboot']);
1075
				}
1076 9d775c75 Renato Botelho
				if (!empty($poolconf['filename32']) &&
1077
				    (!isset($dhcpifconf['filename32']) ||
1078
				    ($poolconf['filename32'] != $dhcpifconf['filename32']))) {
1079 e5395534 Viktor G
					$pxe_files[] = array('00:06', $poolconf['filename32']);
1080 9d775c75 Renato Botelho
				}
1081
				if (!empty($poolconf['filename64']) &&
1082
				    (!isset($dhcpifconf['filename64']) ||
1083
				    ($poolconf['filename64'] != $dhcpifconf['filename64']))) {
1084 e5395534 Viktor G
					$pxe_files[] = array('00:07', $poolconf['filename64']);
1085
					$pxe_files[] = array('00:09', $poolconf['filename64']);
1086 cba980f6 jim-p
				}
1087 4c85579b Wasurerarenai
				if (!empty($poolconf['filename32arm']) &&
1088
				    (!isset($dhcpifconf['filename32arm']) ||
1089
				    ($poolconf['filename32arm'] != $dhcpifconf['filename32arm']))) {
1090 e5395534 Viktor G
					$pxe_files[] = array('00:0a', $poolconf['filename32arm']);
1091 4c85579b Wasurerarenai
				}
1092
				if (!empty($poolconf['filename64arm']) &&
1093
				    (!isset($dhcpifconf['filename64arm']) ||
1094
				    ($poolconf['filename64arm'] != $dhcpifconf['filename64arm']))) {
1095 e5395534 Viktor G
					$pxe_files[] = array('00:0b', $poolconf['filename64arm']);
1096 4c85579b Wasurerarenai
				}
1097 9d775c75 Renato Botelho
1098 e5395534 Viktor G
				$pxeif = false;
1099
				if (is_array($pxe_files) && !empty($pxe_files)) {
1100
					foreach ($pxe_files as $pxe) {
1101 cf40cd17 Viktor G
						if ($pxe[0] == 'HTTPClient') {
1102
							$expr = "substring (option vendor-class-identifier, 0, 10) = \"HTTPClient\" {\n";
1103
						} else {
1104
							$expr = "option arch = {$pxe[0]} {\n";
1105
						}
1106 e5395534 Viktor G
						if (!$pxeif) {
1107 cf40cd17 Viktor G
							$dhcpdconf .= "		if " . $expr;
1108 e5395534 Viktor G
							$pxeif = true;
1109
						} else {
1110 cf40cd17 Viktor G
							$dhcpdconf .= " else if " . $expr;
1111 e5395534 Viktor G
						}
1112
						$dhcpdconf .= "			filename \"{$pxe[1]}\";\n";
1113
						$dhcpdconf .= "		}";
1114 4c85579b Wasurerarenai
					}
1115 e5395534 Viktor G
					if ($filename) {
1116
						$dhcpdconf .= " else {\n";
1117
						$dhcpdconf .= "			filename \"{$filename}\";\n";
1118
						$dhcpdconf .= "		}";
1119 4c85579b Wasurerarenai
					}
1120 e5395534 Viktor G
					$dhcpdconf .= "\n\n";
1121 9d775c75 Renato Botelho
				} elseif (!empty($filename)) {
1122
					$dhcpdconf .= "		filename \"{$filename}\";\n";
1123 cba980f6 jim-p
				}
1124 e5395534 Viktor G
				unset($filename);
1125 9d775c75 Renato Botelho
1126 cba980f6 jim-p
				if (!empty($poolconf['rootpath']) && ($poolconf['rootpath'] != $dhcpifconf['rootpath'])) {
1127
					$dhcpdconf .= "		option root-path \"{$poolconf['rootpath']}\";\n";
1128
				}
1129
			}
1130
			$dhcpdconf .= "		range {$poolconf['range']['from']} {$poolconf['range']['to']};\n";
1131
			$dhcpdconf .= "	}\n\n";
1132
		}
1133
// End of settings inside pools
1134 a25183c5 Scott Ullrich
1135 4208f7b1 timdufrane
		if ($dhcpifconf['gateway'] && $dhcpifconf['gateway'] != "none") {
1136 5b237745 Scott Ullrich
			$routers = $dhcpifconf['gateway'];
1137 c08a5659 smos
			$add_routers = true;
1138 4208f7b1 timdufrane
		} elseif ($dhcpifconf['gateway'] == "none") {
1139
			$add_routers = false;
1140 c08a5659 smos
		} else {
1141 3f141c9d Phil Davis
			$add_routers = $enable_add_routers;
1142 a55e9c70 Ermal Lu?i
			$routers = $ifcfgip;
1143 c08a5659 smos
		}
1144 61e047a5 Phil Davis
		if ($add_routers) {
1145 c08a5659 smos
			$dhcpdconf .= "	option routers {$routers};\n";
1146 61e047a5 Phil Davis
		}
1147 cba980f6 jim-p
1148 c08a5659 smos
		$dhcpdconf .= <<<EOD
1149 5b237745 Scott Ullrich
$dnscfg
1150
1151
EOD;
1152 61e047a5 Phil Davis
		// default-lease-time
1153
		if ($dhcpifconf['defaultleasetime']) {
1154 5b237745 Scott Ullrich
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
1155 61e047a5 Phil Davis
		}
1156 518030b3 Scott Ullrich
1157
		// max-lease-time
1158 61e047a5 Phil Davis
		if ($dhcpifconf['maxleasetime']) {
1159 5b237745 Scott Ullrich
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
1160 5197e3e3 Renato Botelho do Couto
		}
1161
1162
		if (!isset($dhcpifconf['disablepingcheck'])) {
1163
			$dhcpdconf .= "	ping-check true;\n";
1164
		} else {
1165
			$dhcpdconf .= "	ping-check false;\n";
1166 61e047a5 Phil Davis
		}
1167 a25183c5 Scott Ullrich
1168 518030b3 Scott Ullrich
		// netbios-name*
1169 5b237745 Scott Ullrich
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
1170
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
1171
			$dhcpdconf .= "	option netbios-node-type 8;\n";
1172
		}
1173 a25183c5 Scott Ullrich
1174 518030b3 Scott Ullrich
		// ntp-servers
1175 61e047a5 Phil Davis
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0]) {
1176 ad171999 Seth Mos
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
1177 61e047a5 Phil Davis
		}
1178 ad171999 Seth Mos
1179 518030b3 Scott Ullrich
		// tftp-server-name
1180 61e047a5 Phil Davis
		if ($dhcpifconf['tftp'] <> "") {
1181 6c23757b Martin Fuchs
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
1182 61e047a5 Phil Davis
		}
1183 6c23757b Martin Fuchs
1184 518030b3 Scott Ullrich
		// Handle option, number rowhelper values
1185
		$dhcpdconf .= "\n";
1186 284878d7 Viktor G
		$idx = 0;
1187
		$httpclient = false;
1188 b88050bb jim-p
		if (isset($dhcpifconf['numberoptions']['item']) && is_array($dhcpifconf['numberoptions']['item'])) {
1189 61e047a5 Phil Davis
			foreach ($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
1190 65cce9d7 Renato Botelho
				$item_value = base64_decode($item['value']);
1191 61e047a5 Phil Davis
				if (empty($item['type']) || $item['type'] == "text") {
1192 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} \"{$item_value}\";\n";
1193 61e047a5 Phil Davis
				} else {
1194 65cce9d7 Renato Botelho
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} {$item_value};\n";
1195 61e047a5 Phil Davis
				}
1196 284878d7 Viktor G
				if (($item['type'] == "text") &&
1197
				    ($item['number'] == 60) &&
1198
				    (base64_decode($item['value']) == "HTTPClient")) {
1199
					$httpclient = true;
1200 b63b534c Christian McDonald
				}
1201 284878d7 Viktor G
				$idx++;
1202 518030b3 Scott Ullrich
			}
1203
		}
1204 b68d8fe6 Viktor G
		if (!empty($dhcpifconf['uefihttpboot']) && isset($dhcpifconf['netboot']) && !$httpclient) {
1205 284878d7 Viktor G
			$dhcpdconf .= "	option custom-{$dhcpif}-{$idx} \"HTTPClient\";\n";
1206
		}
1207 518030b3 Scott Ullrich
1208
		// ldap-server
1209 61e047a5 Phil Davis
		if ($dhcpifconf['ldap'] <> "") {
1210 6c23757b Martin Fuchs
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
1211 61e047a5 Phil Davis
		}
1212 6c23757b Martin Fuchs
1213 518030b3 Scott Ullrich
		// net boot information
1214 61e047a5 Phil Davis
		if (isset($dhcpifconf['netboot'])) {
1215 a2578c27 Anthony Wrather
			if ($dhcpifconf['nextserver'] <> "") {
1216
				$dhcpdconf .= "	next-server {$dhcpifconf['nextserver']};\n";
1217
			}
1218 e5395534 Viktor G
1219
			$pxe_files = array();
1220
			if (!empty($dhcpifconf['filename'])) {
1221
				$filename = $dhcpifconf['filename'];
1222
			}
1223 cf40cd17 Viktor G
			if (!empty($dhcpifconf['uefihttpboot'])) {
1224
				$pxe_files[] = array('HTTPClient', $dhcpifconf['uefihttpboot']);
1225
			}
1226 e5395534 Viktor G
			if (!empty($dhcpifconf['filename32'])) {
1227
				$pxe_files[] = array('00:06', $dhcpifconf['filename32']);
1228
			}
1229
			if (!empty($dhcpifconf['filename64'])) {
1230
				$pxe_files[] = array('00:07', $dhcpifconf['filename64']);
1231
				$pxe_files[] = array('00:09', $dhcpifconf['filename64']);
1232
			}
1233
			if (!empty($dhcpifconf['filename32arm'])) {
1234
				$pxe_files[] = array('00:0a', $dhcpifconf['filename32arm']);
1235
			}
1236
			if (!empty($dhcpifconf['filename64arm'])) {
1237
				$pxe_files[] = array('00:0b', $dhcpifconf['filename64arm']);
1238
			}
1239
1240
			$pxeif = false;
1241
			if (is_array($pxe_files) && !empty($pxe_files)) {
1242
				foreach ($pxe_files as $pxe) {
1243 cf40cd17 Viktor G
					if ($pxe[0] == 'HTTPClient') {
1244
						$expr = "substring (option vendor-class-identifier, 0, 10) = \"HTTPClient\" {\n";
1245
					} else {
1246
						$expr = "option arch = {$pxe[0]} {\n";
1247
					}
1248 e5395534 Viktor G
					if (!$pxeif) {
1249 b63b534c Christian McDonald
						$dhcpdconf .= "	if " . $expr;
1250 e5395534 Viktor G
						$pxeif = true;
1251
					} else {
1252 b63b534c Christian McDonald
						$dhcpdconf .= " else if " . $expr;
1253 e5395534 Viktor G
					}
1254
					$dhcpdconf .= "		filename \"{$pxe[1]}\";\n";
1255
					$dhcpdconf .= "	}";
1256
				}
1257
				if ($filename) {
1258
					$dhcpdconf .= " else {\n";
1259
					$dhcpdconf .= "		filename \"{$filename}\";\n";
1260
					$dhcpdconf .= "	}";
1261
				}
1262
				$dhcpdconf .= "\n\n";
1263
			} elseif (!empty($filename)) {
1264 568fdc9f Viktor G
				$dhcpdconf .= "	filename \"{$filename}\";\n";
1265 4e9cd828 Seth Mos
			}
1266 568fdc9f Viktor G
			unset($filename);
1267 fdb116a9 Donald A. Cupp Jr
			if (!empty($dhcpifconf['rootpath'])) {
1268 ca126e03 Martin Fuchs
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
1269 cba980f6 jim-p
			}
1270 4e9cd828 Seth Mos
		}
1271 107e8acc Ovidiu Predescu
1272 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
1273
}
1274
1275
EOD;
1276
1277
		/* add static mappings */
1278
		if (is_array($dhcpifconf['staticmap'])) {
1279 a25183c5 Scott Ullrich
1280 5b237745 Scott Ullrich
			$i = 0;
1281 e5eba380 Viktor G
			$sm_newzone[] = array();
1282
			$need_sm_ddns_updates = false;
1283 5b237745 Scott Ullrich
			foreach ($dhcpifconf['staticmap'] as $sm) {
1284 8645d4c2 jim-p
				if (empty($sm)) {
1285
					continue;
1286
				}
1287 00318445 Viktor G
				$cid = '';
1288 449f1dd2 Will Boyce
				$dhcpdconf .= "host s_{$dhcpif}_{$i} {\n";
1289
1290 61e047a5 Phil Davis
				if ($sm['mac']) {
1291 35bc0edf reb00tz
					$dhcpdconf .= "	hardware ethernet {$sm['mac']};\n";
1292 61e047a5 Phil Davis
				}
1293 449f1dd2 Will Boyce
1294 61e047a5 Phil Davis
				if ($sm['cid']) {
1295 00318445 Viktor G
					$cid = str_replace('"', '\"', $sm['cid']);
1296
					$dhcpdconf .= "	option dhcp-client-identifier \"{$cid}\";\n";
1297 61e047a5 Phil Davis
				}
1298 5b237745 Scott Ullrich
1299 61e047a5 Phil Davis
				if ($sm['ipaddr']) {
1300 5b237745 Scott Ullrich
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
1301 61e047a5 Phil Davis
				}
1302 a25183c5 Scott Ullrich
1303 ad30055f Ermal Lu?i
				if ($sm['hostname']) {
1304
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1305 46c5b763 pierrepomes
					$dhhostname = str_replace(".", "_", $dhhostname);
1306 2f590513 smos
					$dhcpdconf .= "	option host-name \"{$dhhostname}\";\n";
1307 f0cce276 Ross Williams
					if ((isset($dhcpifconf['ddnsupdate']) || isset($sm['ddnsupdate'])) && (isset($dhcpifconf['ddnsforcehostname']) || isset($sm['ddnsforcehostname']))) {
1308
						$dhcpdconf .= "	ddns-hostname \"{$dhhostname}\";\n";
1309
					}
1310 ad30055f Ermal Lu?i
				}
1311 61e047a5 Phil Davis
				if ($sm['filename']) {
1312 a2578c27 Anthony Wrather
					$dhcpdconf .= "	filename \"{$sm['filename']}\";\n";
1313 61e047a5 Phil Davis
				}
1314 a2578c27 Anthony Wrather
1315 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1316 a2578c27 Anthony Wrather
					$dhcpdconf .= "	option root-path \"{$sm['rootpath']}\";\n";
1317 61e047a5 Phil Davis
				}
1318 80717709 Martin Fuchs
1319 61e047a5 Phil Davis
				if ($sm['gateway'] && ($sm['gateway'] != $dhcpifconf['gateway'])) {
1320 7309ff39 Renato Botelho
					$dhcpdconf .= "	option routers {$sm['gateway']};\n";
1321 61e047a5 Phil Davis
				}
1322 7309ff39 Renato Botelho
1323
				$smdnscfg = "";
1324
1325
				if ($sm['domain'] && ($sm['domain'] != $dhcpifconf['domain'])) {
1326
					$smdnscfg .= "	option domain-name \"{$sm['domain']}\";\n";
1327
				}
1328
1329 61e047a5 Phil Davis
				if (!empty($sm['domainsearchlist']) && ($sm['domainsearchlist'] != $dhcpifconf['domainsearchlist'])) {
1330 7309ff39 Renato Botelho
					$smdnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $sm['domainsearchlist'])) . "\";\n";
1331
				}
1332
1333
				if (isset($sm['ddnsupdate'])) {
1334 61e047a5 Phil Davis
					if (($sm['ddnsdomain'] <> "") && ($sm['ddnsdomain'] != $dhcpifconf['ddnsdomain'])) {
1335 16ea962d Viktor G
						$smdnscfg .= "	ddns-domainname \"{$sm['ddnsdomain']}\";\n";
1336 b63b534c Christian McDonald
				 		$need_sm_ddns_updates = true;
1337 61e047a5 Phil Davis
					}
1338 16ea962d Viktor G
					$smdnscfg .= "	ddns-update-style interim;\n";
1339 7309ff39 Renato Botelho
				}
1340
1341
				if (is_array($sm['dnsserver']) && ($sm['dnsserver'][0]) && ($sm['dnsserver'][0] != $dhcpifconf['dnsserver'][0])) {
1342
					$smdnscfg .= "	option domain-name-servers " . join(",", $sm['dnsserver']) . ";\n";
1343
				}
1344
				$dhcpdconf .= "{$smdnscfg}";
1345
1346
				// default-lease-time
1347 61e047a5 Phil Davis
				if ($sm['defaultleasetime'] && ($sm['defaultleasetime'] != $dhcpifconf['defaultleasetime'])) {
1348 7309ff39 Renato Botelho
					$dhcpdconf .= "	default-lease-time {$sm['defaultleasetime']};\n";
1349 61e047a5 Phil Davis
				}
1350 7309ff39 Renato Botelho
1351
				// max-lease-time
1352 61e047a5 Phil Davis
				if ($sm['maxleasetime'] && ($sm['maxleasetime'] != $dhcpifconf['maxleasetime'])) {
1353 7309ff39 Renato Botelho
					$dhcpdconf .= "	max-lease-time {$sm['maxleasetime']};\n";
1354 61e047a5 Phil Davis
				}
1355 7309ff39 Renato Botelho
1356
				// netbios-name*
1357
				if (is_array($sm['winsserver']) && $sm['winsserver'][0] && ($sm['winsserver'][0] != $dhcpifconf['winsserver'][0])) {
1358
					$dhcpdconf .= "	option netbios-name-servers " . join(",", $sm['winsserver']) . ";\n";
1359
					$dhcpdconf .= "	option netbios-node-type 8;\n";
1360
				}
1361
1362
				// ntp-servers
1363 61e047a5 Phil Davis
				if (is_array($sm['ntpserver']) && $sm['ntpserver'][0] && ($sm['ntpserver'][0] != $dhcpifconf['ntpserver'][0])) {
1364 7309ff39 Renato Botelho
					$dhcpdconf .= "	option ntp-servers " . join(",", $sm['ntpserver']) . ";\n";
1365 61e047a5 Phil Davis
				}
1366 7309ff39 Renato Botelho
1367
				// tftp-server-name
1368 61e047a5 Phil Davis
				if (!empty($sm['tftp']) && ($sm['tftp'] != $dhcpifconf['tftp'])) {
1369 7309ff39 Renato Botelho
					$dhcpdconf .= "	option tftp-server-name \"{$sm['tftp']}\";\n";
1370 61e047a5 Phil Davis
				}
1371 7309ff39 Renato Botelho
1372 3dd5090f Viktor Gurov
				// Handle option, number rowhelper values
1373
				$dhcpdconf .= "\n";
1374 5c5a7bc8 Viktor G
				$idx = 0;
1375
				$httpclient = false;
1376 3dd5090f Viktor Gurov
				if (isset($sm['numberoptions']['item']) && is_array($sm['numberoptions']['item'])) {
1377
					foreach ($sm['numberoptions']['item'] as $itemidx => $item) {
1378
						$item_value = base64_decode($item['value']);
1379
						if (empty($item['type']) || $item['type'] == "text") {
1380
							$dhcpdconf .= "	option custom-s_{$dhcpif}_{$i}-{$itemidx} \"{$item_value}\";\n";
1381
						} else {
1382
							$dhcpdconf .= "	option custom-s_{$dhcpif}_{$i}-{$itemidx} {$item_value};\n";
1383
						}
1384
					}
1385 5c5a7bc8 Viktor G
					if (($item['type'] == "text") &&
1386
					    ($item['number'] == 60) &&
1387
					    (base64_decode($item['value']) == "HTTPClient")) {
1388
						$httpclient = true;
1389 b63b534c Christian McDonald
					}
1390 5c5a7bc8 Viktor G
					$idx++;
1391
				}
1392 e5e1e8f9 jim-p
				if (!empty($sm['uefihttpboot']) && isset($sm['netboot']) && !$httpclient) {
1393 5c5a7bc8 Viktor G
					$dhcpdconf .= "	option custom-s_{$dhcpif}_{$i}-{$idx} \"HTTPClient\";\n";
1394 3dd5090f Viktor Gurov
				}
1395
1396
				// ldap-server
1397
				if (!empty($sm['ldap']) && ($sm['ldap'] != $dhcpifconf['ldap'])) {
1398
					$dhcpdconf .= "	option ldap-server \"{$sm['ldap']}\";\n";
1399
				}
1400
1401
				// net boot information
1402
				if (isset($sm['netboot'])) {
1403
					if ($sm['nextserver'] <> "") {
1404
						$dhcpdconf .= "	next-server {$sm['nextserver']};\n";
1405
					}
1406 c7599055 Viktor G
1407
					$pxe_files = array();
1408
					if (!empty($sm['filename'])) {
1409
						$filename = $sm['filename'];
1410
					}
1411 5c5a7bc8 Viktor G
					if (!empty($sm['uefihttpboot'])) {
1412
						$pxe_files[] = array('HTTPClient', $sm['uefihttpboot']);
1413
					}
1414 c7599055 Viktor G
					if (!empty($sm['filename32'])) {
1415
						$pxe_files[] = array('00:06', $sm['filename32']);
1416
					}
1417
					if (!empty($sm['filename64'])) {
1418
						$pxe_files[] = array('00:07', $sm['filename64']);
1419
						$pxe_files[] = array('00:09', $sm['filename64']);
1420
					}
1421
					if (!empty($sm['filename32arm'])) {
1422
						$pxe_files[] = array('00:0a', $sm['filename32arm']);
1423
					}
1424
					if (!empty($sm['filename64arm'])) {
1425
						$pxe_files[] = array('00:0b', $sm['filename64arm']);
1426
					}
1427
1428
					$pxeif = false;
1429
					if (is_array($pxe_files) && !empty($pxe_files)) {
1430
						foreach ($pxe_files as $pxe) {
1431 5c5a7bc8 Viktor G
							if ($pxe[0] == 'HTTPClient') {
1432
								$expr = "substring (option vendor-class-identifier, 0, 10) = \"HTTPClient\" {\n";
1433
							} else {
1434
								$expr = "option arch = {$pxe[0]} {\n";
1435
							}
1436 c7599055 Viktor G
							if (!$pxeif) {
1437 b63b534c Christian McDonald
								$dhcpdconf .= "	if " . $expr;
1438 c7599055 Viktor G
								$pxeif = true;
1439
							} else {
1440 b63b534c Christian McDonald
								$dhcpdconf .= " else if " . $expr;
1441 c7599055 Viktor G
							}
1442
							$dhcpdconf .= "		filename \"{$pxe[1]}\";\n";
1443
							$dhcpdconf .= "	}";
1444
						}
1445
						if ($filename) {
1446
							$dhcpdconf .= " else {\n";
1447
							$dhcpdconf .= "		filename \"{$filename}\";\n";
1448
							$dhcpdconf .= "	}";
1449
						}
1450
						$dhcpdconf .= "\n\n";
1451
					} elseif (!empty($filename)) {
1452 568fdc9f Viktor G
						$dhcpdconf .= "	filename \"{$filename}\";\n";
1453 3dd5090f Viktor Gurov
					}
1454 568fdc9f Viktor G
					unset($filename);
1455 3dd5090f Viktor Gurov
					if (!empty($dhcpifconf['rootpath'])) {
1456
						$dhcpdconf .= "	option root-path \"{$sm['rootpath']}\";\n";
1457
					}
1458
				}
1459
1460 5b237745 Scott Ullrich
				$dhcpdconf .= "}\n";
1461 35bc0edf reb00tz
1462 e5eba380 Viktor G
				// add zone DDNS key/server to $ddns_zone[] if required
1463
				if ($need_sm_ddns_updates) {
1464
					$ddnsduplicate = false;
1465
					foreach ($ddns_zones as $ddnszone) {
1466
						if ($ddnszone['domain-name'] == $sm['ddnsdomain']) {
1467
							$ddnsduplicate = true;
1468
						}
1469
					}
1470
					if (!$ddnsduplicate) {
1471 381f213e Viktor G
						$sm_newzone['dns-servers'] = array($sm['ddnsdomainprimary'], $sm['ddnsdomainsecondary']);
1472 e5eba380 Viktor G
						$sm_newzone['domain-name'] = $sm['ddnsdomain'];
1473
						$sm_newzone['ddnsdomainkeyname'] = $sm['ddnsdomainkeyname'];
1474
						$sm_newzone['ddnsdomainkeyalgorithm'] = $sm['ddnsdomainkeyalgorithm'];
1475
						$sm_newzone['ddnsdomainkey'] = $sm['ddnsdomainkey'];
1476
						$dhcpdconf .= dhcpdkey($sm_newzone);
1477
						$ddns_zones[] = $sm_newzone;
1478
						$need_ddns_updates = true;
1479
					}
1480
				}
1481
1482 35bc0edf reb00tz
				// subclass for DHCP limiting
1483
				if (!empty($sm['mac'])) {
1484
					// assuming ALL addresses are ethernet hardware type ("1:" prefix)
1485
					$dhcpdconf .= "subclass \"s_{$dhcpif}\" 1:{$sm['mac']};\n";
1486
				}
1487 00318445 Viktor G
				if (!empty($cid)) {
1488
					$dhcpdconf .= "subclass \"s_{$dhcpif}\" \"{$cid}\";\n";
1489 35bc0edf reb00tz
				}
1490
1491
1492 5b237745 Scott Ullrich
				$i++;
1493
			}
1494
		}
1495 a25183c5 Scott Ullrich
1496 6f9b8073 Ermal Luçi
		$dhcpdifs[] = get_real_interface($dhcpif);
1497 61e047a5 Phil Davis
		if ($newzone['domain-name']) {
1498
			if ($need_ddns_updates) {
1499 9fbd8f71 Viktor Gurov
				$newzone['dns-servers'] = array($dhcpifconf['ddnsdomainprimary'], $dhcpifconf['ddnsdomainsecondary']);
1500 7c1747a3 Chris Buechler
				$newzone['ddnsdomainkeyname'] = $dhcpifconf['ddnsdomainkeyname'];
1501 534d7d69 Joeri Capens
				$newzone['ddnsdomainkeyalgorithm'] = $dhcpifconf['ddnsdomainkeyalgorithm'];
1502 7c1747a3 Chris Buechler
				$newzone['ddnsdomainkey'] = $dhcpifconf['ddnsdomainkey'];
1503
				$dhcpdconf .= dhcpdkey($dhcpifconf);
1504 87019fc4 Andres Petralli
			}
1505 3df2dbfd jim-p
			$ddns_zones[] = $newzone;
1506 87019fc4 Andres Petralli
		}
1507 3df2dbfd jim-p
	}
1508
1509
	if ($need_ddns_updates) {
1510
		$dhcpdconf .= "ddns-update-style interim;\n";
1511 3cdef187 Andres Petralli
		$dhcpdconf .= "update-static-leases on;\n";
1512 87019fc4 Andres Petralli
1513 7c1747a3 Chris Buechler
		$dhcpdconf .= dhcpdzones($ddns_zones);
1514 5b237745 Scott Ullrich
	}
1515
1516 abdd01f5 Ermal
	/* write dhcpd.conf */
1517
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", $dhcpdconf)) {
1518
		printf(gettext("Error: cannot open dhcpd.conf in services_dhcpdv4_configure().%s"), "\n");
1519
		unset($dhcpdconf);
1520
		return 1;
1521
	}
1522 928d4416 Ermal
	unset($dhcpdconf);
1523 2fb056d8 Seth Mos
1524
	/* create an empty leases database */
1525 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases")) {
1526 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
1527 61e047a5 Phil Davis
	}
1528 2fb056d8 Seth Mos
1529 b075c1e2 Chris Buechler
	/* make sure there isn't a stale dhcpd.pid file, which can make dhcpd fail to start.   */
1530 61e047a5 Phil Davis
	/* if we get here, dhcpd has been killed and is not started yet                        */
1531 b075c1e2 Chris Buechler
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
1532
1533 2fb056d8 Seth Mos
	/* fire up dhcpd in a chroot */
1534 abdd01f5 Ermal
	if (count($dhcpdifs) > 0) {
1535 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 " .
1536
			join(" ", $dhcpdifs));
1537
	}
1538
1539 61e047a5 Phil Davis
	if (platform_booting()) {
1540 2fb056d8 Seth Mos
		print "done.\n";
1541 61e047a5 Phil Davis
	}
1542 2fb056d8 Seth Mos
1543
	return 0;
1544
}
1545
1546 086cf944 Phil Davis
function dhcpdkey($dhcpifconf) {
1547 87019fc4 Andres Petralli
	$dhcpdconf = "";
1548 7e3bdbaa Joeri Capens
	if (!empty($dhcpifconf['ddnsdomainkeyname']) && !empty($dhcpifconf['ddnsdomainkey'])) {
1549
		$algorithm = empty($dhcpifconf['ddnsdomainkeyalgorithm']) ? 'hmac-md5' : $dhcpifconf['ddnsdomainkeyalgorithm'];
1550 07588052 Viktor G
		$dhcpdconf .= "key \"{$dhcpifconf['ddnsdomainkeyname']}\" {\n";
1551 7e3bdbaa Joeri Capens
		$dhcpdconf .= "	algorithm {$algorithm};\n";
1552 87019fc4 Andres Petralli
		$dhcpdconf .= "	secret {$dhcpifconf['ddnsdomainkey']};\n";
1553
		$dhcpdconf .= "}\n";
1554
	}
1555
1556
	return $dhcpdconf;
1557
}
1558
1559 7c1747a3 Chris Buechler
function dhcpdzones($ddns_zones) {
1560 87019fc4 Andres Petralli
	$dhcpdconf = "";
1561
1562
	if (is_array($ddns_zones)) {
1563
		$added_zones = array();
1564
		foreach ($ddns_zones as $zone) {
1565 61e047a5 Phil Davis
			if (!is_array($zone) || empty($zone) || !is_array($zone['dns-servers'])) {
1566 87019fc4 Andres Petralli
				continue;
1567 61e047a5 Phil Davis
			}
1568 87019fc4 Andres Petralli
			$primary = $zone['dns-servers'][0];
1569
			$secondary = empty($zone['dns-servers'][1]) ? "" : $zone['dns-servers'][1];
1570
1571 9fbd8f71 Viktor Gurov
			// Make sure we aren't using any invalid servers.
1572
			if (!is_ipaddr($primary)) {
1573
				if (is_ipaddr($secondary)) {
1574 87019fc4 Andres Petralli
					$primary = $secondary;
1575
					$secondary = "";
1576
				} else {
1577
					continue;
1578
				}
1579
			}
1580
1581
			// We don't need to add zones multiple times.
1582
			if ($zone['domain-name'] && !in_array($zone['domain-name'], $added_zones)) {
1583
				$dhcpdconf .= "zone {$zone['domain-name']}. {\n";
1584 9fbd8f71 Viktor Gurov
				if (is_ipaddrv4($primary)) {
1585
					$dhcpdconf .= "	primary {$primary};\n";
1586
				} else {
1587
					$dhcpdconf .= "	primary6 {$primary};\n";
1588
				}
1589 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1590 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1591 9fbd8f71 Viktor Gurov
				} elseif (is_ipaddrv6($secondary)) {
1592
					$dhcpdconf .= "	secondary6 {$secondary};\n";
1593 61e047a5 Phil Davis
				}
1594 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1595 07588052 Viktor G
					$dhcpdconf .= "	key \"{$zone['ddnsdomainkeyname']}\";\n";
1596 61e047a5 Phil Davis
				}
1597 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1598
				$added_zones[] = $zone['domain-name'];
1599
			}
1600
			if ($zone['ptr-domain'] && !in_array($zone['ptr-domain'], $added_zones)) {
1601 e733f5b2 Viktor G
				$dhcpdconf .= "zone {$zone['ptr-domain']}. {\n";
1602 9fbd8f71 Viktor Gurov
				if (is_ipaddrv4($primary)) {
1603
					$dhcpdconf .= "	primary {$primary};\n";
1604
				} else {
1605
					$dhcpdconf .= "	primary6 {$primary};\n";
1606
				}
1607 61e047a5 Phil Davis
				if (is_ipaddrv4($secondary)) {
1608 87019fc4 Andres Petralli
					$dhcpdconf .= "	secondary {$secondary};\n";
1609 9fbd8f71 Viktor Gurov
				} elseif (is_ipaddrv6($secondary)) {
1610
					$dhcpdconf .= "	secondary6 {$secondary};\n";
1611 61e047a5 Phil Davis
				}
1612 7c1747a3 Chris Buechler
				if ($zone['ddnsdomainkeyname'] <> "" && $zone['ddnsdomainkey'] <> "") {
1613 07588052 Viktor G
					$dhcpdconf .= "	key \"{$zone['ddnsdomainkeyname']}\";\n";
1614 61e047a5 Phil Davis
				}
1615 87019fc4 Andres Petralli
				$dhcpdconf .= "}\n";
1616
				$added_zones[] = $zone['ptr-domain'];
1617
			}
1618
		}
1619
	}
1620
1621
	return $dhcpdconf;
1622
}
1623
1624 92977616 Ermal
function services_dhcpdv6_configure($blacklist = array()) {
1625 532a1a0e Reid Linnemann
	global $g;
1626 107e8acc Ovidiu Predescu
1627 2568e151 Christian McDonald
	if (g_get('services_dhcp_server_enable') == false) {
1628 2fb056d8 Seth Mos
		return;
1629 61e047a5 Phil Davis
	}
1630 2fb056d8 Seth Mos
1631 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
1632 2fb056d8 Seth Mos
		$mt = microtime();
1633
		echo "services_dhcpd_configure($if) being called $mt\n";
1634
	}
1635 107e8acc Ovidiu Predescu
1636 2fb056d8 Seth Mos
	/* kill any running dhcpd */
1637 61e047a5 Phil Davis
	if (isvalidpid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid")) {
1638 bfb3e717 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
1639 61e047a5 Phil Davis
	}
1640 e9ab2ddb smos
1641 892d939e Christian McDonald
	/* kill any running dhcpleases6 */
1642
	if (isvalidpid("{$g['varrun_path']}/dhcpleases6.pid")) {
1643
		killbypid("{$g['varrun_path']}/dhcpleases6.pid");
1644
	}
1645
1646 2fb056d8 Seth Mos
	/* DHCP enabled on any interfaces? */
1647 61e047a5 Phil Davis
	if (!is_dhcpv6_server_enabled()) {
1648 2fb056d8 Seth Mos
		return 0;
1649 61e047a5 Phil Davis
	}
1650 2fb056d8 Seth Mos
1651 532a1a0e Reid Linnemann
	$syscfg = config_get_path('system');
1652
	init_config_arr(['dhcpdv6']);
1653
	$dhcpdv6cfg = config_get_path('dhcpdv6');
1654 2fb056d8 Seth Mos
	$Iflist = get_configured_interface_list();
1655 e9ab2ddb smos
	$Iflist = array_merge($Iflist, get_configured_pppoe_server_interfaces());
1656
1657 107e8acc Ovidiu Predescu
1658 61e047a5 Phil Davis
	if (platform_booting()) {
1659 2fb056d8 Seth Mos
		echo "Starting DHCPv6 service...";
1660 61e047a5 Phil Davis
	} else {
1661 2fb056d8 Seth Mos
		sleep(1);
1662 61e047a5 Phil Davis
	}
1663 2fb056d8 Seth Mos
1664
	$custoptionsv6 = "";
1665 107e8acc Ovidiu Predescu
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1666 fae6b2c0 jim-p
		if (empty($dhcpv6ifconf)) {
1667
			continue;
1668
		}
1669 61e047a5 Phil Davis
		if (is_array($dhcpv6ifconf['numberoptions']) && is_array($dhcpv6ifconf['numberoptions']['item'])) {
1670
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1671 2fb056d8 Seth Mos
				$custoptionsv6 .= "option custom-{$dhcpv6if}-{$itemv6idx} code {$itemv6['number']} = text;\n";
1672
			}
1673
		}
1674
	}
1675
1676 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['netboot']) && !empty($dhcpv6ifconf['bootfile_url'])) {
1677 bd942860 Renato Botelho
		$custoptionsv6 .= "option dhcp6.bootfile-url code 59 = string;\n";
1678 61e047a5 Phil Davis
	}
1679 bd942860 Renato Botelho
1680 2fb056d8 Seth Mos
	$dhcpdv6conf = <<<EOD
1681 107e8acc Ovidiu Predescu
1682 2fb056d8 Seth Mos
option domain-name "{$syscfg['domain']}";
1683
option ldap-server code 95 = text;
1684
option domain-search-list code 119 = text;
1685 547f1e65 Renato Botelho
{$custoptionsv6}
1686 2fb056d8 Seth Mos
default-lease-time 7200;
1687
max-lease-time 86400;
1688
log-facility local7;
1689
one-lease-per-client true;
1690
deny duplicates;
1691
ping-check true;
1692 87019fc4 Andres Petralli
update-conflict-detection false;
1693 2fb056d8 Seth Mos
1694
EOD;
1695
1696 61e047a5 Phil Davis
	if (!isset($dhcpv6ifconf['disableauthoritative'])) {
1697 2fb056d8 Seth Mos
		$dhcpdv6conf .= "authoritative;\n";
1698 61e047a5 Phil Davis
	}
1699 2fb056d8 Seth Mos
1700 61e047a5 Phil Davis
	if (isset($dhcpv6ifconf['alwaysbroadcast'])) {
1701 2fb056d8 Seth Mos
		$dhcpdv6conf .= "always-broadcast on\n";
1702 61e047a5 Phil Davis
	}
1703 2fb056d8 Seth Mos
1704
	$dhcpdv6ifs = array();
1705
1706 693833cb Seth Mos
	$dhcpv6num = 0;
1707 87019fc4 Andres Petralli
	$nsupdate = false;
1708
1709 693833cb Seth Mos
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1710 fae6b2c0 jim-p
		if (empty($dhcpv6ifconf)) {
1711
			continue;
1712
		}
1713 693833cb Seth Mos
1714 87019fc4 Andres Petralli
		$ddns_zones = array();
1715
1716 532a1a0e Reid Linnemann
		$ifcfgv6 = config_get_path("interfaces/{$dhcpv6if}");
1717 693833cb Seth Mos
1718 50b84727 Viktor G
		if (!isset($dhcpv6ifconf['enable']) || !isset($Iflist[$dhcpv6if]) ||
1719
		    (!isset($ifcfgv6['enable']) && !preg_match("/poes/", $dhcpv6if))) {
1720 693833cb Seth Mos
			continue;
1721 61e047a5 Phil Davis
		}
1722 693833cb Seth Mos
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1723 50b84727 Viktor G
		if (!is_ipaddrv6($ifcfgipv6) && !preg_match("/poes/", $dhcpv6if)) {
1724 830ea39a Chris Buechler
			continue;
1725
		}
1726 693833cb Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
1727 d57293a4 Seth Mos
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1728 e89a17fb Phil Davis
		// We might have some prefix-delegation on WAN (e.g. /48),
1729
		// but then it is split and given out to individual interfaces
1730
		// (LAN, OPT1, OPT2...) as multiple /64 subnets. So the size
1731
		// of each subnet here is always /64.
1732
		$pdlen = 64;
1733 15db02a6 Renato Botelho
1734 3dc73d39 jim-p
		$range_from = $dhcpv6ifconf['range']['from'];
1735
		$range_to = $dhcpv6ifconf['range']['to'];
1736
		if ($ifcfgv6['ipaddrv6'] == 'track6') {
1737
			$range_from = merge_ipv6_delegated_prefix($ifcfgipv6, $range_from, $pdlen);
1738
			$range_to = merge_ipv6_delegated_prefix($ifcfgipv6, $range_to, $pdlen);
1739
		}
1740
1741 0c5cf0df Viktor G
		if (is_ipaddrv6($ifcfgipv6)) {
1742
			$subnet_start = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1743
			$subnet_end = gen_subnetv6_max($ifcfgipv6, $ifcfgsnv6);
1744 3dc73d39 jim-p
			if ((!is_inrange_v6($range_from, $subnet_start, $subnet_end)) ||
1745
			    (!is_inrange_v6($range_to, $subnet_start, $subnet_end))) {
1746 0c5cf0df Viktor G
				log_error(gettext("The specified range lies outside of the current subnet. Skipping DHCP6 entry."));
1747
				continue;
1748
			}
1749
		}
1750
1751 693833cb Seth Mos
		$dnscfgv6 = "";
1752
1753
		if ($dhcpv6ifconf['domain']) {
1754 3c009080 Seth Mos
			$dnscfgv6 .= "	option domain-name \"{$dhcpv6ifconf['domain']}\";\n";
1755 693833cb Seth Mos
		}
1756 107e8acc Ovidiu Predescu
1757 61e047a5 Phil Davis
		if ($dhcpv6ifconf['domainsearchlist'] <> "") {
1758 afd8177f Florian Apolloner
			$dnscfgv6 .= "	option dhcp6.domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpv6ifconf['domainsearchlist'])) . "\";\n";
1759 61e047a5 Phil Davis
		}
1760 693833cb Seth Mos
1761
		if (isset($dhcpv6ifconf['ddnsupdate'])) {
1762 61e047a5 Phil Davis
			if ($dhcpv6ifconf['ddnsdomain'] <> "") {
1763 3c009080 Seth Mos
				$dnscfgv6 .= "	ddns-domainname \"{$dhcpv6ifconf['ddnsdomain']}\";\n";
1764 693833cb Seth Mos
			}
1765 391d63da Renato Botelho
			if (empty($dhcpv6ifconf['ddnsclientupdates'])) {
1766
				$ddnsclientupdates = 'allow';
1767
			} else {
1768
				$ddnsclientupdates = $dhcpv6ifconf['ddnsclientupdates'];
1769
			}
1770
			$dnscfgv6 .= "	{$ddnsclientupdates} client-updates;\n";
1771 87019fc4 Andres Petralli
			$nsupdate = true;
1772 391d63da Renato Botelho
		} else {
1773
			$dnscfgv6 .= "	do-forward-updates false;\n";
1774 693833cb Seth Mos
		}
1775
1776 09646aef luckman212
		if ($dhcpv6ifconf['dhcp6c-dns'] != 'disabled') {
1777 e26ad76e luckman212
			if (is_array($dhcpv6ifconf['dnsserver']) && ($dhcpv6ifconf['dnsserver'][0])) {
1778 610cbfdc Viktor G
				$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dhcpv6ifconf['dnsserver']) . ";\n";
1779 532a1a0e Reid Linnemann
			} else if (((config_path_enabled('dnsmasq')) || config_path_enabled('unbound')) && is_ipaddrv6($ifcfgipv6)) {
1780 610cbfdc Viktor G
				$dnscfgv6 .= "	option dhcp6.name-servers {$ifcfgipv6};\n";
1781 e26ad76e luckman212
			} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
1782
				$dns_arrv6 = array();
1783 9f6432f0 luckman212
				foreach ($syscfg['dnsserver'] as $dnsserver) {
1784 e26ad76e luckman212
					if (is_ipaddrv6($dnsserver)) {
1785 7339f154 Viktor G
						if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1786
						    Net_IPv6::isInNetmask($dnsserver, '::', $pdlen)) {
1787
							$dnsserver = merge_ipv6_delegated_prefix($ifcfgipv6, $dnsserver, $pdlen);
1788
						}
1789 e26ad76e luckman212
						$dns_arrv6[] = $dnsserver;
1790
					}
1791
				}
1792
				if (!empty($dns_arrv6)) {
1793 610cbfdc Viktor G
					$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dns_arrv6) . ";\n";
1794 2521266a Seth Mos
				}
1795
			}
1796 e26ad76e luckman212
		} else {
1797 610cbfdc Viktor G
			$dnscfgv6 .= "	#option dhcp6.name-servers --;\n";
1798 693833cb Seth Mos
		}
1799
1800 391d63da Renato Botelho
		if (!is_ipaddrv6($ifcfgipv6)) {
1801
			$ifcfgsnv6 = "64";
1802 3dc73d39 jim-p
			$subnetv6 = gen_subnetv6($range_from, $ifcfgsnv6);
1803 391d63da Renato Botelho
		}
1804
1805
		$dhcpdv6conf .= "subnet6 {$subnetv6}/{$ifcfgsnv6}";
1806
1807
		if (isset($dhcpv6ifconf['ddnsupdate']) &&
1808
		    !empty($dhcpv6ifconf['ddnsdomain'])) {
1809 87019fc4 Andres Petralli
			$newzone = array();
1810 391d63da Renato Botelho
			$newzone['domain-name'] = $dhcpv6ifconf['ddnsdomain'];
1811 9fbd8f71 Viktor Gurov
			$newzone['dns-servers'] = array($dhcpv6ifconf['ddnsdomainprimary'], $dhcpv6ifconf['ddnsdomainsecondary']);
1812 7c1747a3 Chris Buechler
			$newzone['ddnsdomainkeyname'] = $dhcpv6ifconf['ddnsdomainkeyname'];
1813
			$newzone['ddnsdomainkey'] = $dhcpv6ifconf['ddnsdomainkey'];
1814 87019fc4 Andres Petralli
			$ddns_zones[] = $newzone;
1815 391d63da Renato Botelho
			if (isset($dhcpv6ifconf['ddnsreverse'])) {
1816
				$ptr_zones = get_v6_ptr_zones($subnetv6, $ifcfgsnv6);
1817
				foreach ($ptr_zones as $ptr_zone) {
1818
					$reversezone = array();
1819 3b46a9cf Joeri Capens
					$reversezone['ptr-domain'] = $ptr_zone;
1820 9fbd8f71 Viktor Gurov
					$reversezone['dns-servers'] = array($dhcpv6ifconf['ddnsdomainprimary'], $dhcpv6ifconf['ddnsdomainsecondary']);
1821 3b46a9cf Joeri Capens
					$reversezone['ddnsdomainkeyname'] = $dhcpv6ifconf['ddnsdomainkeyname'];
1822
					$reversezone['ddnsdomainkey'] = $dhcpv6ifconf['ddnsdomainkey'];
1823 391d63da Renato Botelho
					$ddns_zones[] = $reversezone;
1824
				}
1825
			}
1826 87019fc4 Andres Petralli
		}
1827
1828 1944af41 Ermal
		$dhcpdv6conf .= " {\n";
1829 693833cb Seth Mos
1830 3dc73d39 jim-p
		if (!empty($range_from) && !empty($range_to)) {
1831 5c533d72 Viktor G
			$dhcpdv6conf .= "	range6 {$range_from} {$range_to};\n";
1832
		}
1833 693833cb Seth Mos
1834 5c533d72 Viktor G
		$dhcpdv6conf .= $dnscfgv6;
1835 bfb3e717 Seth Mos
1836 1944af41 Ermal
		if (is_ipaddrv6($dhcpv6ifconf['prefixrange']['from']) && is_ipaddrv6($dhcpv6ifconf['prefixrange']['to'])) {
1837 978b8f50 Chris Buechler
			$dhcpdv6conf .= "	prefix6 {$dhcpv6ifconf['prefixrange']['from']} {$dhcpv6ifconf['prefixrange']['to']} /{$dhcpv6ifconf['prefixrange']['prefixlength']};\n";
1838 bfb3e717 Seth Mos
		}
1839 70da4172 Jean Cyr
		if (is_ipaddrv6($dhcpv6ifconf['dns6ip'])) {
1840 2bf455ca Renato Botelho
			$dns6ip = $dhcpv6ifconf['dns6ip'];
1841
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1842 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($dns6ip, '::', $pdlen)) {
1843
				$dns6ip = merge_ipv6_delegated_prefix($ifcfgipv6, $dns6ip, $pdlen);
1844 2bf455ca Renato Botelho
			}
1845
			$dhcpdv6conf .= "	option dhcp6.name-servers {$dns6ip};\n";
1846 70da4172 Jean Cyr
		}
1847 61e047a5 Phil Davis
		// default-lease-time
1848
		if ($dhcpv6ifconf['defaultleasetime']) {
1849 693833cb Seth Mos
			$dhcpdv6conf .= "	default-lease-time {$dhcpv6ifconf['defaultleasetime']};\n";
1850 61e047a5 Phil Davis
		}
1851 693833cb Seth Mos
1852
		// max-lease-time
1853 61e047a5 Phil Davis
		if ($dhcpv6ifconf['maxleasetime']) {
1854 693833cb Seth Mos
			$dhcpdv6conf .= "	max-lease-time {$dhcpv6ifconf['maxleasetime']};\n";
1855 61e047a5 Phil Davis
		}
1856 693833cb Seth Mos
1857
		// ntp-servers
1858 4096fe5d smos
		if (is_array($dhcpv6ifconf['ntpserver']) && $dhcpv6ifconf['ntpserver'][0]) {
1859
			$ntpservers = array();
1860 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['ntpserver'] as $ntpserver) {
1861 2bf455ca Renato Botelho
				if (!is_ipaddrv6($ntpserver)) {
1862
					continue;
1863
				}
1864
				if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1865 15db02a6 Renato Botelho
				    Net_IPv6::isInNetmask($ntpserver, '::', $pdlen)) {
1866
					$ntpserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ntpserver, $pdlen);
1867 61e047a5 Phil Davis
				}
1868 2bf455ca Renato Botelho
				$ntpservers[] = $ntpserver;
1869 4096fe5d smos
			}
1870 61e047a5 Phil Davis
			if (count($ntpservers) > 0) {
1871 2605e6d7 Chris Buechler
				$dhcpdv6conf .= "        option dhcp6.sntp-servers " . join(",", $dhcpv6ifconf['ntpserver']) . ";\n";
1872 61e047a5 Phil Davis
			}
1873 4096fe5d smos
		}
1874 693833cb Seth Mos
		// tftp-server-name
1875 7d504365 smos
		/* Needs ISC DHCPD support
1876 61e047a5 Phil Davis
		 if ($dhcpv6ifconf['tftp'] <> "") {
1877 693833cb Seth Mos
			$dhcpdv6conf .= "	option tftp-server-name \"{$dhcpv6ifconf['tftp']}\";\n";
1878 61e047a5 Phil Davis
		 }
1879 7d504365 smos
		*/
1880 693833cb Seth Mos
1881
		// Handle option, number rowhelper values
1882
		$dhcpdv6conf .= "\n";
1883 b88050bb jim-p
		if (isset($dhcpv6ifconf['numberoptions']['item']) && is_array($dhcpv6ifconf['numberoptions']['item'])) {
1884 61e047a5 Phil Davis
			foreach ($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
1885 65cce9d7 Renato Botelho
				$itemv6_value = base64_decode($itemv6['value']);
1886
				$dhcpdv6conf .= "	option custom-{$dhcpv6if}-{$itemv6idx} \"{$itemv6_value}\";\n";
1887 693833cb Seth Mos
			}
1888
		}
1889
1890
		// ldap-server
1891 61e047a5 Phil Davis
		if ($dhcpv6ifconf['ldap'] <> "") {
1892 2bf455ca Renato Botelho
			$ldapserver = $dhcpv6ifconf['ldap'];
1893
			if ($ifcfgv6['ipaddrv6'] == 'track6' &&
1894 15db02a6 Renato Botelho
			    Net_IPv6::isInNetmask($ldapserver, '::', $pdlen)) {
1895
				$ldapserver = merge_ipv6_delegated_prefix($ifcfgipv6, $ldapserver, $pdlen);
1896 2bf455ca Renato Botelho
			}
1897
			$dhcpdv6conf .= "	option ldap-server \"{$ldapserver}\";\n";
1898 61e047a5 Phil Davis
		}
1899 693833cb Seth Mos
1900
		// net boot information
1901 61e047a5 Phil Davis
		if (isset($dhcpv6ifconf['netboot'])) {
1902 bd942860 Renato Botelho
			if (!empty($dhcpv6ifconf['bootfile_url'])) {
1903
				$dhcpdv6conf .= "	option dhcp6.bootfile-url \"{$dhcpv6ifconf['bootfile_url']}\";\n";
1904 abdd01f5 Ermal
			}
1905
		}
1906 107e8acc Ovidiu Predescu
1907 1c903aa4 Ermal
		$dhcpdv6conf .= "}\n";
1908 693833cb Seth Mos
1909
		/* add static mappings */
1910 2fb056d8 Seth Mos
		/* Needs to use DUID */
1911 693833cb Seth Mos
		if (is_array($dhcpv6ifconf['staticmap'])) {
1912
			$i = 0;
1913
			foreach ($dhcpv6ifconf['staticmap'] as $sm) {
1914 8645d4c2 jim-p
				if (empty($sm)) {
1915
					continue;
1916
				}
1917 693833cb Seth Mos
				$dhcpdv6conf .= <<<EOD
1918
host s_{$dhcpv6if}_{$i} {
1919 2fb056d8 Seth Mos
	host-identifier option dhcp6.client-id {$sm['duid']};
1920 693833cb Seth Mos
1921
EOD;
1922 61e047a5 Phil Davis
				if ($sm['ipaddrv6']) {
1923 2bf455ca Renato Botelho
					$ipaddrv6 = $sm['ipaddrv6'];
1924
					if ($ifcfgv6['ipaddrv6'] == 'track6') {
1925 15db02a6 Renato Botelho
						$ipaddrv6 = merge_ipv6_delegated_prefix($ifcfgipv6, $ipaddrv6, $pdlen);
1926 2bf455ca Renato Botelho
					}
1927
					$dhcpdv6conf .= "	fixed-address6 {$ipaddrv6};\n";
1928 61e047a5 Phil Davis
				}
1929 693833cb Seth Mos
1930
				if ($sm['hostname']) {
1931
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
1932
					$dhhostname = str_replace(".", "_", $dhhostname);
1933
					$dhcpdv6conf .= "	option host-name {$dhhostname};\n";
1934 bad77fc0 jim-p
					if (isset($dhcpv6ifconf['ddnsupdate']) &&
1935
					    isset($dhcpv6ifconf['ddnsforcehostname'])) {
1936
						$dhcpdv6conf .= "	ddns-hostname \"{$dhhostname}\";\n";
1937
					}
1938 693833cb Seth Mos
				}
1939 61e047a5 Phil Davis
				if ($sm['filename']) {
1940 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	filename \"{$sm['filename']}\";\n";
1941 61e047a5 Phil Davis
				}
1942 a2578c27 Anthony Wrather
1943 61e047a5 Phil Davis
				if ($sm['rootpath']) {
1944 a2578c27 Anthony Wrather
					$dhcpdv6conf .= "	option root-path \"{$sm['rootpath']}\";\n";
1945 61e047a5 Phil Davis
				}
1946 693833cb Seth Mos
1947
				$dhcpdv6conf .= "}\n";
1948
				$i++;
1949
			}
1950
		}
1951 107e8acc Ovidiu Predescu
1952 391d63da Renato Botelho
		if ($dhcpv6ifconf['ddnsdomain']) {
1953 87019fc4 Andres Petralli
			$dhcpdv6conf .= dhcpdkey($dhcpv6ifconf);
1954 7c1747a3 Chris Buechler
			$dhcpdv6conf .= dhcpdzones($ddns_zones);
1955 87019fc4 Andres Petralli
		}
1956
1957 532a1a0e Reid Linnemann
		if ((config_get_path("dhcpdv6/{$dhcpv6if}/ramode") != "unmanaged") &&
1958
		    (config_path_enabled("interfaces/{$dhcpv6if}") ||
1959 50b84727 Viktor G
		    preg_match("/poes/", $dhcpv6if))) {
1960 61e047a5 Phil Davis
			if (preg_match("/poes/si", $dhcpv6if)) {
1961 e9ab2ddb smos
				/* magic here */
1962
				$dhcpdv6ifs = array_merge($dhcpdv6ifs, get_pppoes_child_interfaces($dhcpv6if));
1963
			} else {
1964 1944af41 Ermal
				$realif = get_real_interface($dhcpv6if, "inet6");
1965 029b377a Ermal
				if (stristr("$realif", "bridge")) {
1966
					$mac = get_interface_mac($realif);
1967
					$v6address = generate_ipv6_from_mac($mac);
1968
					/* Create link local address for bridges */
1969 e9ab2ddb smos
					mwexec("/sbin/ifconfig {$realif} inet6 {$v6address}");
1970
				}
1971 029b377a Ermal
				$realif = escapeshellcmd($realif);
1972
				$dhcpdv6ifs[] = $realif;
1973 656f1763 Seth Mos
			}
1974 de140730 Seth Mos
		}
1975 693833cb Seth Mos
	}
1976
1977 61e047a5 Phil Davis
	if ($nsupdate) {
1978 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style interim;\n";
1979 1a618dc0 Viktor Gurov
		$dhcpdv6conf .= "update-static-leases on;\n";
1980 61e047a5 Phil Davis
	} else {
1981 87019fc4 Andres Petralli
		$dhcpdv6conf .= "ddns-update-style none;\n";
1982
	}
1983
1984 abdd01f5 Ermal
	/* write dhcpdv6.conf */
1985
	if (!@file_put_contents("{$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf", $dhcpdv6conf)) {
1986 1c903aa4 Ermal
		log_error("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1987 61e047a5 Phil Davis
		if (platform_booting()) {
1988 1c903aa4 Ermal
			printf("Error: cannot open {$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf in services_dhcpdv6_configure().\n");
1989 61e047a5 Phil Davis
		}
1990 abdd01f5 Ermal
		unset($dhcpdv6conf);
1991
		return 1;
1992
	}
1993 928d4416 Ermal
	unset($dhcpdv6conf);
1994
1995 693833cb Seth Mos
	/* create an empty leases v6 database */
1996 61e047a5 Phil Davis
	if (!file_exists("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases")) {
1997 abdd01f5 Ermal
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1998 61e047a5 Phil Davis
	}
1999 107e8acc Ovidiu Predescu
2000 61e047a5 Phil Davis
	/* make sure there isn't a stale dhcpdv6.pid file, which may make dhcpdv6 fail to start.  */
2001
	/* if we get here, dhcpdv6 has been killed and is not started yet                         */
2002
	unlink_if_exists("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
2003 b075c1e2 Chris Buechler
2004 68a0e4fc Scott Ullrich
	/* fire up dhcpd in a chroot */
2005 abdd01f5 Ermal
	if (count($dhcpdv6ifs) > 0) {
2006 b63b534c Christian McDonald
		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 " . join(" ", $dhcpdv6ifs));
2007 892d939e Christian McDonald
		mwexec("/usr/local/sbin/dhcpleases6 -c \"/usr/local/bin/php-cgi -f /usr/local/sbin/prefixes.php\" -l {$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
2008 2a1bd027 Seth Mos
	}
2009 61e047a5 Phil Davis
	if (platform_booting()) {
2010 f1a44a3a Carlos Eduardo Ramos
		print gettext("done.") . "\n";
2011 61e047a5 Phil Davis
	}
2012 a25183c5 Scott Ullrich
2013 5b237745 Scott Ullrich
	return 0;
2014
}
2015
2016 1098cb94 Viktor G
function services_igmpproxy_configure($interface='') {
2017 532a1a0e Reid Linnemann
	global $g;
2018 41997fbb Ermal Luci
2019 532a1a0e Reid Linnemann
	if (!empty($interface) && !empty(config_get_path('igmpproxy/igmpentry'))) {
2020
		$igmpinf = "";
2021
		foreach (config_get_path('igmpproxy/igmpentry', []) as $igmpentry) {
2022 1098cb94 Viktor G
			if ($igmpentry['ifname'] == $interface) {
2023
				$igmpinf = true;
2024
				break;
2025
			}
2026
		}
2027
		if (!$igmpinf) {
2028
			return false;
2029
		}
2030
	}
2031 41997fbb Ermal Luci
2032 532a1a0e Reid Linnemann
	if (!config_path_enabled('igmpproxy')) {
2033 1098cb94 Viktor G
		if (isvalidproc("igmpproxy")) {
2034
			log_error(gettext("Stopping IGMP Proxy service."));
2035
			killbyname("igmpproxy");
2036
		}
2037
		return true;
2038 6b3e3bc5 PiBa-NL
	}
2039 532a1a0e Reid Linnemann
	if (count(config_get_path('igmpproxy/igmpentry', [])) == 0) {
2040 1098cb94 Viktor G
		return false;
2041
	}
2042
2043
	if (platform_booting()) {
2044
		echo gettext("Starting IGMP Proxy service...");
2045
	}
2046
2047
	if (isvalidproc("igmpproxy")) {
2048
		log_error(gettext("Restarting IGMP Proxy service."));
2049
		killbyname("igmpproxy");
2050 61e047a5 Phil Davis
	}
2051 41997fbb Ermal Luci
2052 61e047a5 Phil Davis
	$iflist = get_configured_interface_list();
2053 f206afb5 Ermal
2054 61e047a5 Phil Davis
	$igmpconf = <<<EOD
2055 41997fbb Ermal Luci
2056
##------------------------------------------------------
2057
## Enable Quickleave mode (Sends Leave instantly)
2058
##------------------------------------------------------
2059
quickleave
2060
2061
EOD;
2062
2063 532a1a0e Reid Linnemann
	foreach (config_get_path('igmpproxy/igmpentry', []) as $igmpcf) {
2064
		if (empty(config_get_path("interfaces/{$igmpcf['ifname']}/ipaddr"))) {
2065 1098cb94 Viktor G
			continue;
2066
		}
2067 61e047a5 Phil Davis
		unset($iflist[$igmpcf['ifname']]);
2068
		$realif = get_real_interface($igmpcf['ifname']);
2069
		if (empty($igmpcf['threshold'])) {
2070
			$threshld = 1;
2071
		} else {
2072
			$threshld = $igmpcf['threshold'];
2073
		}
2074
		$igmpconf .= "phyint {$realif} {$igmpcf['type']} ratelimit 0 threshold {$threshld}\n";
2075
2076
		if ($igmpcf['address'] <> "") {
2077
			$item = explode(" ", $igmpcf['address']);
2078
			foreach ($item as $iww) {
2079
				$igmpconf .= "altnet {$iww}\n";
2080
			}
2081
		}
2082
		$igmpconf .= "\n";
2083 1098cb94 Viktor G
		if ($igmpcf['type'] == 'upstream') {
2084 b63b534c Christian McDonald
		       $upstream = true;
2085 1098cb94 Viktor G
		} else {
2086 b63b534c Christian McDonald
		       $downstream = true;
2087 1098cb94 Viktor G
		}
2088 61e047a5 Phil Davis
	}
2089
	foreach ($iflist as $ifn) {
2090
		$realif = get_real_interface($ifn);
2091
		$igmpconf .= "phyint {$realif} disabled\n";
2092
	}
2093 3bae60be Ermal
	$igmpconf .= "\n";
2094 41997fbb Ermal Luci
2095 1098cb94 Viktor G
	if (!$upstream || !$downstream) {
2096
		log_error(gettext("Could not find upstream or downstream IGMP Proxy interfaces!"));
2097
		return;
2098
	}
2099
2100 2568e151 Christian McDonald
	$igmpfl = fopen(g_get('varetc_path') . "/igmpproxy.conf", "w");
2101 61e047a5 Phil Davis
	if (!$igmpfl) {
2102 1098cb94 Viktor G
		log_error(gettext("Could not write IGMP Proxy configuration file!"));
2103 61e047a5 Phil Davis
		return;
2104
	}
2105
	fwrite($igmpfl, $igmpconf);
2106
	fclose($igmpfl);
2107 928d4416 Ermal
	unset($igmpconf);
2108 41997fbb Ermal Luci
2109 532a1a0e Reid Linnemann
	if (config_path_enabled('syslog','igmpxverbose')) {
2110 a51dd381 Luiz Otavio O Souza
		mwexec_bg("/usr/local/sbin/igmpproxy -v {$g['varetc_path']}/igmpproxy.conf");
2111 2bd0585e Stephen Beaver
	} else {
2112 a51dd381 Luiz Otavio O Souza
		mwexec_bg("/usr/local/sbin/igmpproxy {$g['varetc_path']}/igmpproxy.conf");
2113 2bd0585e Stephen Beaver
	}
2114
2115 1098cb94 Viktor G
	log_error(gettext("Started IGMP Proxy service."));
2116 41997fbb Ermal Luci
2117 1098cb94 Viktor G
	if (platform_booting()) {
2118
		echo gettext("done.") . "\n";
2119
	}
2120
2121
	return true;
2122 41997fbb Ermal Luci
}
2123
2124 5b237745 Scott Ullrich
function services_dhcrelay_configure() {
2125 532a1a0e Reid Linnemann
	global $g;
2126 6fa9f38c Renato Botelho
2127 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
2128 acd910bf Scott Ullrich
		$mt = microtime();
2129 f19d3b7a Scott Ullrich
		echo "services_dhcrelay_configure() being called $mt\n";
2130 acd910bf Scott Ullrich
	}
2131 a25183c5 Scott Ullrich
2132 5b237745 Scott Ullrich
	/* kill any running dhcrelay */
2133
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
2134 a25183c5 Scott Ullrich
2135 c6c398c6 jim-p
	init_config_arr(array('dhcrelay'));
2136 532a1a0e Reid Linnemann
	$dhcrelaycfg = config_get_path('dhcrelay');
2137 a25183c5 Scott Ullrich
2138 5b237745 Scott Ullrich
	/* DHCPRelay enabled on any interfaces? */
2139 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
2140 5b237745 Scott Ullrich
		return 0;
2141 61e047a5 Phil Davis
	}
2142 a25183c5 Scott Ullrich
2143 30169caa Viktor G
	/* Start/Restart DHCP Relay, if a CARP VIP is set, check its status and act
2144
	* appropriately. */
2145
	if (isset($dhcrelaycfg['carpstatusvip']) && ($dhcrelaycfg['carpstatusvip'] != "none")) {
2146
		$status = get_carp_interface_status($dhcrelaycfg['carpstatusvip']);
2147
		switch (strtoupper($status)) {
2148
			// Do not start DHCP Relay service if the VIP is in BACKUP or INIT state.
2149
			case "BACKUP":
2150
			case "INIT":
2151
				log_error("Stopping DHCP Relay (CARP BACKUP/INIT)");
2152
				return 0;
2153
				break;
2154
			// Start the service if the VIP is MASTER state.
2155
			case "MASTER":
2156
			// Assume it's up if the status can't be determined.
2157
			default:
2158
				break;
2159
		}
2160
	}
2161
2162 61e047a5 Phil Davis
	if (platform_booting()) {
2163 30169caa Viktor G
		echo gettext("Starting DHCP Relay service...");
2164 61e047a5 Phil Davis
	} else {
2165 5b237745 Scott Ullrich
		sleep(1);
2166 61e047a5 Phil Davis
	}
2167 a25183c5 Scott Ullrich
2168 2f06cc3f Ermal
	$iflist = get_configured_interface_list();
2169 a25183c5 Scott Ullrich
2170 f427d68d jim-p
	$dhcrelayifs = array();
2171 2f06cc3f Ermal
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
2172
	foreach ($dhcifaces as $dhcrelayif) {
2173 5285aa84 Viktor G
		if (!isset($iflist[$dhcrelayif])) {
2174 5b237745 Scott Ullrich
			continue;
2175 61e047a5 Phil Davis
		}
2176 a25183c5 Scott Ullrich
2177 5285aa84 Viktor G
		if (get_interface_ip($dhcrelayif)) {
2178 2f06cc3f Ermal
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
2179 61e047a5 Phil Davis
		}
2180 5b237745 Scott Ullrich
	}
2181 f427d68d jim-p
	$dhcrelayifs = array_unique($dhcrelayifs);
2182 5b237745 Scott Ullrich
2183 d7827421 Renato Botelho
	/*
2184
	 * In order for the relay to work, it needs to be active
2185
	 * on the interface in which the destination server sits.
2186
	 */
2187 2f06cc3f Ermal
	$srvips = explode(",", $dhcrelaycfg['server']);
2188 c58879a9 Ermal LUÇI
	if (!is_array($srvips)) {
2189 e8c516a0 Phil Davis
		log_error(gettext("No destination IP has been configured!"));
2190 c58879a9 Ermal LUÇI
		return;
2191 4de8f7ba Phil Davis
	}
2192 f427d68d jim-p
	$srvifaces = array();
2193 d7827421 Renato Botelho
	foreach ($srvips as $srcidx => $srvip) {
2194 9fa31c24 PiBa-NL
		$destif = guess_interface_from_ip($srvip);
2195 5285aa84 Viktor G
		if (!empty($destif) && !is_pseudo_interface($destif)) {
2196 f427d68d jim-p
			$srvifaces[] = $destif;
2197 d7827421 Renato Botelho
		}
2198
	}
2199 f427d68d jim-p
	$srvifaces = array_unique($srvifaces);
2200
2201 a76e6114 jim-p
	/* Check for relays in the same subnet as clients so they can bind for
2202
	 * either direction (up or down) */
2203
	$srvrelayifs = array_intersect($dhcrelayifs, $srvifaces);
2204
2205 f427d68d jim-p
	/* The server interface(s) should not be in this list */
2206
	$dhcrelayifs = array_diff($dhcrelayifs, $srvifaces);
2207 5b237745 Scott Ullrich
2208 a76e6114 jim-p
	/* Remove the dual-role interfaces from up and down lists */
2209
	$srvifaces = array_diff($srvifaces, $srvrelayifs);
2210
	$dhcrelayifs = array_diff($dhcrelayifs, $srvrelayifs);
2211
2212 5b237745 Scott Ullrich
	/* fire up dhcrelay */
2213 a76e6114 jim-p
	if (empty($dhcrelayifs) && empty($srvrelayifs)) {
2214 f427d68d jim-p
		log_error(gettext("No suitable downstream interfaces found for running dhcrelay!"));
2215
		return; /* XXX */
2216
	}
2217 a76e6114 jim-p
	if (empty($srvifaces) && empty($srvrelayifs)) {
2218 f427d68d jim-p
		log_error(gettext("No suitable upstream interfaces found for running dhcrelay!"));
2219 24997966 Ermal
		return; /* XXX */
2220
	}
2221
2222 f427d68d jim-p
	$cmd = "/usr/local/sbin/dhcrelay";
2223 a76e6114 jim-p
2224
	if (!empty($dhcrelayifs)) {
2225
		$cmd .= " -id " . implode(" -id ", $dhcrelayifs);
2226
	}
2227
	if (!empty($srvifaces)) {
2228
		$cmd .= " -iu " . implode(" -iu ", $srvifaces);
2229
	}
2230
	if (!empty($srvrelayifs)) {
2231
		$cmd .= " -i " . implode(" -i ", $srvrelayifs);
2232
	}
2233 5b237745 Scott Ullrich
2234 61e047a5 Phil Davis
	if (isset($dhcrelaycfg['agentoption'])) {
2235 4de8f7ba Phil Davis
		$cmd .= " -a -m replace";
2236 61e047a5 Phil Davis
	}
2237 5b237745 Scott Ullrich
2238 2f06cc3f Ermal
	$cmd .= " " . implode(" ", $srvips);
2239 5b237745 Scott Ullrich
	mwexec($cmd);
2240 928d4416 Ermal
	unset($cmd);
2241 a25183c5 Scott Ullrich
2242 5b237745 Scott Ullrich
	return 0;
2243
}
2244
2245 b7a15cf8 Seth Mos
function services_dhcrelay6_configure() {
2246 532a1a0e Reid Linnemann
	global $g;
2247 6fa9f38c Renato Botelho
2248 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
2249 b7a15cf8 Seth Mos
		$mt = microtime();
2250 874f099a Phil Davis
		echo "services_dhcrelay6_configure() being called $mt\n";
2251 b7a15cf8 Seth Mos
	}
2252
2253
	/* kill any running dhcrelay */
2254
	killbypid("{$g['varrun_path']}/dhcrelay6.pid");
2255
2256 c6c398c6 jim-p
	init_config_arr(array('dhcrelay6'));
2257 532a1a0e Reid Linnemann
	$dhcrelaycfg = config_get_path('dhcrelay6');
2258 b7a15cf8 Seth Mos
2259
	/* DHCPv6 Relay enabled on any interfaces? */
2260 61e047a5 Phil Davis
	if (!isset($dhcrelaycfg['enable'])) {
2261 b7a15cf8 Seth Mos
		return 0;
2262 61e047a5 Phil Davis
	}
2263 b7a15cf8 Seth Mos
2264 30169caa Viktor G
	/* Start/Restart DHCPv6 Relay, if a CARP VIP is set, check its status and act
2265
	* appropriately. */
2266
	if (isset($dhcrelaycfg['carpstatusvip']) && ($dhcrelaycfg['carpstatusvip'] != "none")) {
2267
		$status = get_carp_interface_status($dhcrelaycfg['carpstatusvip']);
2268
		switch (strtoupper($status)) {
2269
			// Do not start DHCP Relay service if the VIP is in BACKUP or INIT state.
2270
			case "BACKUP":
2271
			case "INIT":
2272
				log_error("Stopping DHCPv6 Relay (CARP BACKUP/INIT)");
2273
				return 0;
2274
				break;
2275
			// Start the service if the VIP is MASTER state.
2276
			case "MASTER":
2277
			// Assume it's up if the status can't be determined.
2278
			default:
2279
				break;
2280
		}
2281
	}
2282
2283 61e047a5 Phil Davis
	if (platform_booting()) {
2284 30169caa Viktor G
		echo gettext("Starting DHCPv6 Relay service...");
2285 61e047a5 Phil Davis
	} else {
2286 b7a15cf8 Seth Mos
		sleep(1);
2287 61e047a5 Phil Davis
	}
2288 b7a15cf8 Seth Mos
2289
	$iflist = get_configured_interface_list();
2290
2291
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
2292
	foreach ($dhcifaces as $dhcrelayif) {
2293 5285aa84 Viktor G
		if (!isset($iflist[$dhcrelayif])) {
2294 b7a15cf8 Seth Mos
			continue;
2295 086cf944 Phil Davis
		}
2296 b7a15cf8 Seth Mos
2297 5285aa84 Viktor G
		if (get_interface_ipv6($dhcrelayif)) {
2298 b7a15cf8 Seth Mos
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
2299 61e047a5 Phil Davis
		}
2300 b7a15cf8 Seth Mos
	}
2301 69dd7088 Michael Tharp
	$dhcrelayifs = array_unique($dhcrelayifs);
2302 b7a15cf8 Seth Mos
2303 d7827421 Renato Botelho
	/*
2304
	 * In order for the relay to work, it needs to be active
2305
	 * on the interface in which the destination server sits.
2306
	 */
2307 b7a15cf8 Seth Mos
	$srvips = explode(",", $dhcrelaycfg['server']);
2308 d7827421 Renato Botelho
	$srvifaces = array();
2309
	foreach ($srvips as $srcidx => $srvip) {
2310 9fa31c24 PiBa-NL
		$destif = guess_interface_from_ip($srvip);
2311 5285aa84 Viktor G
		if (!empty($destif) && !is_pseudo_interface($destif)) {
2312 d7827421 Renato Botelho
			$srvifaces[] = "{$srvip}%{$destif}";
2313
		}
2314 b7a15cf8 Seth Mos
	}
2315
2316
	/* fire up dhcrelay */
2317 61e047a5 Phil Davis
	if (empty($dhcrelayifs) || empty($srvifaces)) {
2318 e8c516a0 Phil Davis
		log_error(gettext("No suitable interface found for running dhcrelay -6!"));
2319 b7a15cf8 Seth Mos
		return; /* XXX */
2320
	}
2321
2322 54a9d71d Phil Davis
	$cmd = "/usr/local/sbin/dhcrelay -6 -pf \"{$g['varrun_path']}/dhcrelay6.pid\"";
2323 69dd7088 Michael Tharp
	foreach ($dhcrelayifs as $dhcrelayif) {
2324
		$cmd .= " -l {$dhcrelayif}";
2325
	}
2326
	foreach ($srvifaces as $srviface) {
2327
		$cmd .= " -u \"{$srviface}\"";
2328
	}
2329 b7a15cf8 Seth Mos
	mwexec($cmd);
2330 928d4416 Ermal
	unset($cmd);
2331 b7a15cf8 Seth Mos
2332
	return 0;
2333
}
2334
2335 181d7c95 Ermal Luçi
function services_dyndns_configure_client($conf) {
2336
2337 61e047a5 Phil Davis
	if (!isset($conf['enable'])) {
2338 65996399 Ermal
		return;
2339 61e047a5 Phil Davis
	}
2340 d2946062 Ermal
2341 181d7c95 Ermal Luçi
	/* load up the dyndns.class */
2342
	require_once("dyndns.class");
2343
2344
	$dns = new updatedns($dnsService = $conf['type'],
2345
		$dnsHost = $conf['host'],
2346 b24a0251 Chris Buechler
		$dnsDomain = $conf['domainname'],
2347 181d7c95 Ermal Luçi
		$dnsUser = $conf['username'],
2348
		$dnsPass = $conf['password'],
2349 3aa55bbe Phil Davis
		$dnsWildcard = $conf['wildcard'],
2350 e10d25b4 CarlGill
		$dnsProxied = $conf['proxied'],
2351 107e8acc Ovidiu Predescu
		$dnsMX = $conf['mx'],
2352 f3b2b2a4 Yehuda Katz
		$dnsIf = "{$conf['interface']}",
2353
		$dnsBackMX = NULL,
2354
		$dnsServer = NULL,
2355
		$dnsPort = NULL,
2356 37f3e704 Matt Corallo
		$dnsUpdateURL = "{$conf['updateurl']}",
2357 cd132e86 Edson Brandi
		$forceUpdate = $conf['force'],
2358 4de8f7ba Phil Davis
		$dnsZoneID = $conf['zoneid'],
2359
		$dnsTTL = $conf['ttl'],
2360 37f3e704 Matt Corallo
		$dnsResultMatch = "{$conf['resultmatch']}",
2361
		$dnsRequestIf = "{$conf['requestif']}",
2362 82caf945 Jaakko Kantojärvi
		$dnsMaxCacheAge = $conf['maxcacheage'],
2363 1e503870 Phil Davis
		$dnsID = "{$conf['id']}",
2364 aa79f351 Sebastian Chrostek
		$dnsVerboseLog = $conf['verboselog'],
2365
		$curlIpresolveV4 = $conf['curl_ipresolve_v4'],
2366 fd331bdc Viktor G
		$curlSslVerifypeer = $conf['curl_ssl_verifypeer'],
2367
		$curlProxy = $conf['curl_proxy']);
2368 181d7c95 Ermal Luçi
}
2369
2370 0be93267 Ermal Lu?i
function services_dyndns_configure($int = "") {
2371 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
2372 59a63553 Scott Ullrich
		$mt = microtime();
2373
		echo "services_dyndns_configure() being called $mt\n";
2374
	}
2375
2376 532a1a0e Reid Linnemann
	$dyndnscfg = config_get_path('dyndnses/dyndns');
2377
	if (empty($dyndnscfg)) {
2378 b88050bb jim-p
		return 0;
2379
	}
2380 43a9b03d PiBa-NL
	$gwgroups = return_gateway_groups_array(true);
2381 67ee1ec5 Ermal Luçi
	if (is_array($dyndnscfg)) {
2382 61e047a5 Phil Davis
		if (platform_booting()) {
2383 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DynDNS clients...");
2384 61e047a5 Phil Davis
		}
2385 181d7c95 Ermal Luçi
2386 67ee1ec5 Ermal Luçi
		foreach ($dyndnscfg as $dyndns) {
2387 00d3003d jim-p
			if (!is_array($dyndns) || empty($dyndns)) {
2388
				continue;
2389
			}
2390 e8382f7f Renato Botelho
			/*
2391
			 * If it's using a gateway group, check if interface is
2392
			 * the active gateway for that group
2393
			 */
2394
			$group_int = '';
2395 46583aba Renato Botelho
			$friendly_group_int = '';
2396 3b41c8f3 Pulcov
			$gwgroup_member = false;
2397 e8382f7f Renato Botelho
			if (is_array($gwgroups[$dyndns['interface']])) {
2398
				if (!empty($gwgroups[$dyndns['interface']][0]['vip'])) {
2399
					$group_int = $gwgroups[$dyndns['interface']][0]['vip'];
2400
				} else {
2401
					$group_int = $gwgroups[$dyndns['interface']][0]['int'];
2402 46583aba Renato Botelho
					$friendly_group_int =
2403 271fc45e jim-p
					    convert_real_interface_to_friendly_interface_name(
2404 46583aba Renato Botelho
						$group_int);
2405 3b41c8f3 Pulcov
					if (!empty($int)) {
2406
						$gwgroup_member =
2407
						    interface_gateway_group_member(get_real_interface($int),
2408
						    $dyndns['interface']);
2409
					}
2410 e8382f7f Renato Botelho
				}
2411
			}
2412 3b41c8f3 Pulcov
			if ((empty($int)) || ($int == $dyndns['interface']) || $gwgroup_member ||
2413 46583aba Renato Botelho
			    ($int == $group_int) || ($int == $friendly_group_int)) {
2414 1e503870 Phil Davis
				$dyndns['verboselog'] = isset($dyndns['verboselog']);
2415 d9bdc020 Sebastian Chrostek
				$dyndns['curl_ipresolve_v4'] = isset($dyndns['curl_ipresolve_v4']);
2416
				$dyndns['curl_ssl_verifypeer'] = isset($dyndns['curl_ssl_verifypeer']);
2417 88863533 Robert Resch
				$dyndns['proxied'] = isset($dyndns['proxied']);
2418 591b3cba Thomas Lovén
				$dyndns['wildcard'] = isset($dyndns['wildcard']);
2419 768eb89c smos
				services_dyndns_configure_client($dyndns);
2420
				sleep(1);
2421
			}
2422 67ee1ec5 Ermal Luçi
		}
2423 59a63553 Scott Ullrich
2424 61e047a5 Phil Davis
		if (platform_booting()) {
2425 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2426 61e047a5 Phil Davis
		}
2427 59a63553 Scott Ullrich
	}
2428
2429
	return 0;
2430
}
2431
2432 0e3aeb6b Phil Davis
function dyndnsCheckIP($int) {
2433 532a1a0e Reid Linnemann
	global $factory_default_checkipservice;
2434 0e3aeb6b Phil Davis
	$ip_address = get_interface_ip($int);
2435 475feb89 Christian McDonald
2436 0e3aeb6b Phil Davis
	if (is_private_ip($ip_address)) {
2437 873e4b28 phildd
		$gateways_status = return_gateways_status(true);
2438
		// If the gateway for this interface is down, then the external check cannot work.
2439
		// Avoid the long wait for the external check to timeout.
2440 17ad89f4 jim-p
		if (stristr(array_get_path($gateways_status, config_get_path("interfaces/{$int}/gateway") . "/status"), "down")) {
2441 873e4b28 phildd
			return "down";
2442 61e047a5 Phil Davis
		}
2443 39d2f39d NOYB
2444 1e7eb5d1 jim-p
		$available_ci_services = config_get_path('checkipservices/checkipservice', []);
2445 39d2f39d NOYB
		// Append the factory default check IP service to the list (if not disabled).
2446 532a1a0e Reid Linnemann
		if (!config_path_enabled('checkipservices','disable_factory_default')) {
2447 1e7eb5d1 jim-p
			$available_ci_services[] = $factory_default_checkipservice;
2448 39d2f39d NOYB
		}
2449
2450
		// Use the first enabled check IP service as the default.
2451 1e7eb5d1 jim-p
		foreach ($available_ci_services as $checkipservice) {
2452 532a1a0e Reid Linnemann
			if (isset($checkipservice['enable'])) {
2453
				$url = $checkipservice['url'];
2454
				$username = $checkipservice['username'];
2455
				$password = $checkipservice['password'];
2456
				$verifysslpeer = isset($checkipservice['verifysslpeer']);
2457
				$curl_proxy = isset($checkipservice['curl_proxy']);
2458
				break;
2459 39d2f39d NOYB
			}
2460
		}
2461
2462
		$hosttocheck = $url;
2463 5244c510 Florian Asche
		$ip_ch = curl_init($hosttocheck);
2464 0e3aeb6b Phil Davis
		curl_setopt($ip_ch, CURLOPT_RETURNTRANSFER, 1);
2465 39d2f39d NOYB
		curl_setopt($ip_ch, CURLOPT_SSL_VERIFYPEER, $verifysslpeer);
2466 e1eee3d2 Renato Botelho
		curl_setopt($ip_ch, CURLOPT_INTERFACE, 'host!' . $ip_address);
2467 181386d6 Florian Asche
		curl_setopt($ip_ch, CURLOPT_CONNECTTIMEOUT, '30');
2468
		curl_setopt($ip_ch, CURLOPT_TIMEOUT, 120);
2469 3c6f29c0 Florian Asche
		curl_setopt($ip_ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
2470 39d2f39d NOYB
		curl_setopt($ip_ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
2471
		curl_setopt($ip_ch, CURLOPT_USERPWD, "{$username}:{$password}");
2472 67fedb90 Viktor G
		if ($curl_proxy) {
2473
			set_curlproxy($ip_ch);
2474
		}
2475 0e3aeb6b Phil Davis
		$ip_result_page = curl_exec($ip_ch);
2476
		curl_close($ip_ch);
2477
		$ip_result_decoded = urldecode($ip_result_page);
2478
		preg_match('=Current IP Address: (.*)</body>=siU', $ip_result_decoded, $matches);
2479 c12f206d Johan van der Vyver
2480
		if ($matches[1]) {
2481
			$parsed_ip = trim($matches[1]);
2482
		} else {
2483
			$parsed_ip = trim($ip_result_decoded);
2484
		}
2485
2486
		preg_match('=((25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])\.){3,3}(25[0-5]|(2[0-4]|1{0,1}[0-9]){0,1}[0-9])=', $parsed_ip, $matches);
2487
		if ($matches[0]) {
2488
			$ip_address = $matches[0];
2489
		}
2490 0e3aeb6b Phil Davis
	}
2491
	return $ip_address;
2492
}
2493
2494 57b5da70 jim-p
function services_dnsmasq_configure($restart_dhcp = true) {
2495 532a1a0e Reid Linnemann
	global $g;
2496 6a01ea44 Bill Marquette
	$return = 0;
2497 107e8acc Ovidiu Predescu
2498 683992fc stilez
	// hard coded args: will be removed to avoid duplication if specified in custom_options
2499
	$standard_args = array(
2500
		"dns-forward-max" => "--dns-forward-max=5000",
2501
		"cache-size" => "--cache-size=10000",
2502
		"local-ttl" => "--local-ttl=1"
2503
	);
2504
2505
2506 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
2507 acd910bf Scott Ullrich
		$mt = microtime();
2508 f19d3b7a Scott Ullrich
		echo "services_dnsmasq_configure() being called $mt\n";
2509 acd910bf Scott Ullrich
	}
2510
2511 5b237745 Scott Ullrich
	/* kill any running dnsmasq */
2512 61e047a5 Phil Davis
	if (file_exists("{$g['varrun_path']}/dnsmasq.pid")) {
2513 d224df18 Ermal
		sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
2514 61e047a5 Phil Davis
	}
2515 5b237745 Scott Ullrich
2516 532a1a0e Reid Linnemann
	if (config_path_enabled('dnsmasq')) {
2517 a25183c5 Scott Ullrich
2518 61e047a5 Phil Davis
		if (platform_booting()) {
2519 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DNS forwarder...");
2520 61e047a5 Phil Davis
		} else {
2521 5b237745 Scott Ullrich
			sleep(1);
2522 61e047a5 Phil Davis
		}
2523 5b237745 Scott Ullrich
2524 61e047a5 Phil Davis
		/* generate hosts file */
2525 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2526 61e047a5 Phil Davis
			$return = 1;
2527
		}
2528 cbc6a13f Chris Buechler
2529 5b237745 Scott Ullrich
		$args = "";
2530 a25183c5 Scott Ullrich
2531 532a1a0e Reid Linnemann
		if (config_path_enabled('dnsmasq','regdhcp')) {
2532 fc84b222 Renato Botelho
			$args .= " --dhcp-hostsfile={$g['etc_path']}/hosts ";
2533 0261381a Ermal
		}
2534 107e8acc Ovidiu Predescu
2535 e6c49e3d jim-p
		/* Setup listen port, if non-default */
2536 532a1a0e Reid Linnemann
		$port = config_get_path('dnsmasq/port');
2537
		if (is_port($port)) {
2538
			$args .= " --port={$port} ";
2539 61e047a5 Phil Davis
		}
2540 e6c49e3d jim-p
2541 b4323f39 jim-p
		$listen_addresses = "";
2542 532a1a0e Reid Linnemann
2543
		$interfaces = explode(",", config_get_path('dnsmasq/interface', ""));
2544
		foreach ($interfaces as $interface) {
2545
			$if = get_real_interface($interface);
2546
			if (does_interface_exist($if)) {
2547
				$laddr = get_interface_ip($interface);
2548
				if (is_ipaddrv4($laddr)) {
2549
					$listen_addresses .= " --listen-address={$laddr} ";
2550
				}
2551
				$laddr6 = get_interface_ipv6($interface);
2552
				if (is_ipaddrv6($laddr6) && !config_path_enabled('dnsmasq','strictbind')) {
2553
					/*
2554
					 * XXX: Since dnsmasq does not support link-local address
2555
					 * with scope specified. These checks are being done.
2556
					 */
2557
					if (is_linklocal($laddr6) && strstr($laddr6, "%")) {
2558
						$tmpaddrll6 = explode("%", $laddr6);
2559
						$listen_addresses .= " --listen-address={$tmpaddrll6[0]} ";
2560
					} else {
2561
						$listen_addresses .= " --listen-address={$laddr6} ";
2562 b4323f39 jim-p
					}
2563
				}
2564
			}
2565 532a1a0e Reid Linnemann
		}
2566
		if (!empty($listen_addresses)) {
2567
			$args .= " {$listen_addresses} ";
2568
			if (config_path_enabled('dnsmasq','strictbind')) {
2569
				$args .= " --bind-interfaces ";
2570 b4323f39 jim-p
			}
2571
		}
2572
2573 fc27d3f4 Phil Davis
		/* If selected, then first forward reverse lookups for private IPv4 addresses to nowhere. */
2574 153613e3 Phil Davis
		/* Only make entries for reverse domains that do not have a matching domain override. */
2575 532a1a0e Reid Linnemann
		if (config_path_enabled('dnsmasq','no_private_reverse')) {
2576 0a7985ba Phil Davis
			/* Note: Carrier Grade NAT (CGN) addresses 100.64.0.0/10 are intentionally not here. */
2577
			/* End-users should not be aware of CGN addresses, so reverse lookups for these should not happen. */
2578
			/* Just the pfSense WAN might get a CGN address from an ISP. */
2579 153613e3 Phil Davis
2580
			// Build an array of domain overrides to help in checking for matches.
2581
			$override_a = array();
2582 532a1a0e Reid Linnemann
			foreach (config_get_path('dnsmasq/domainoverrides', []) as $override) {
2583
				$override_a[$override['domain']] = "y";
2584 0a7985ba Phil Davis
			}
2585 153613e3 Phil Davis
2586
			// Build an array of the private reverse lookup domain names
2587
			$reverse_domain_a = array("10.in-addr.arpa", "168.192.in-addr.arpa");
2588
			// Unfortunately the 172.16.0.0/12 range does not map nicely to the in-addr.arpa scheme.
2589 61e047a5 Phil Davis
			for ($subnet_num = 16; $subnet_num < 32; $subnet_num++) {
2590 153613e3 Phil Davis
				$reverse_domain_a[] = "$subnet_num.172.in-addr.arpa";
2591
			}
2592
2593
			// Set the --server parameter to nowhere for each reverse domain name that was not specifically specified in a domain override.
2594 61e047a5 Phil Davis
			foreach ($reverse_domain_a as $reverse_domain) {
2595
				if (!isset($override_a[$reverse_domain])) {
2596 153613e3 Phil Davis
					$args .= " --server=/$reverse_domain/ ";
2597 61e047a5 Phil Davis
				}
2598 0a7985ba Phil Davis
			}
2599 153613e3 Phil Davis
			unset($override_a);
2600
			unset($reverse_domain_a);
2601 0a7985ba Phil Davis
		}
2602
2603 fc27d3f4 Phil Davis
		/* Setup forwarded domains */
2604 532a1a0e Reid Linnemann
		foreach (config_get_path('dnsmasq/domainoverrides', []) as $override) {
2605
			if ($override['ip'] == "!") {
2606
				$override['ip'] = "";
2607 fc27d3f4 Phil Davis
			}
2608 532a1a0e Reid Linnemann
			$args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
2609 fc27d3f4 Phil Davis
		}
2610
2611 9a36d901 Viktor G
		/* avoid 127.0.0.1 dns loop,
2612
		 * see https://redmine.pfsense.org/issues/12902 */
2613
		$args .= ' --no-resolv';
2614
		foreach (get_dns_nameservers(false, true) as $dns) {
2615
			if ($dns != '127.0.0.1') {
2616
				$args .= ' --server=' . $dns;
2617
			}
2618
		}
2619
2620 2c46f11f Scott Ullrich
		/* Allow DNS Rebind for forwarded domains */
2621 532a1a0e Reid Linnemann
		if (!config_path_enabled('system/webgui','nodnsrebindcheck')) {
2622
			foreach (config_get_path('dnsmasq/domainoverrides', []) as $override) {
2623
				$args .= ' --rebind-domain-ok=/' . $override['domain'] . '/ ';
2624 2c46f11f Scott Ullrich
			}
2625
		}
2626 91adc5c1 Scott Ullrich
2627 532a1a0e Reid Linnemann
		if (!config_path_enabled('system/webgui', 'nodnsrebindcheck')) {
2628 30d20e7d Scott Ullrich
			$dns_rebind = "--rebind-localhost-ok --stop-dns-rebind";
2629 61e047a5 Phil Davis
		}
2630 30d20e7d Scott Ullrich
2631 532a1a0e Reid Linnemann
		if (config_path_enabled('dnsmasq','strict_order')) {
2632 96ea7162 N0YB
			$args .= " --strict-order ";
2633 f48271e0 jim-p
		} else {
2634
			$args .= " --all-servers ";
2635 96ea7162 N0YB
		}
2636
2637 532a1a0e Reid Linnemann
		if (config_path_enabled('dnsmasq','domain_needed')) {
2638 96ea7162 N0YB
			$args .= " --domain-needed ";
2639
		}
2640
2641 532a1a0e Reid Linnemann
		foreach (preg_split('/\s+/', base64_decode(config_get_path('dnsmasq/custom_options', ""))) as $c) {
2642 29e53480 jim-p
			if (empty($c)) {
2643
				continue;
2644
			}
2645 532a1a0e Reid Linnemann
			$args .= " " . escapeshellarg("--{$c}");
2646
			$p = explode('=', $c);
2647
			if (array_key_exists($p[0], $standard_args)) {
2648
				unset($standard_args[$p[0]]);
2649 683992fc stilez
			}
2650 61e047a5 Phil Davis
		}
2651 41567e06 jim-p
		$args .= ' ' . implode(' ', array_values($standard_args));
2652 8f9bffbc Andrew Thompson
2653 68ce71f2 jim-p
		/* run dnsmasq. Use "-C /dev/null" since we use command line args only (Issue #6730) */
2654 f48271e0 jim-p
		$cmd = "/usr/local/sbin/dnsmasq -C /dev/null {$dns_rebind} {$args}";
2655 b4323f39 jim-p
		//log_error("dnsmasq command: {$cmd}");
2656
		mwexec_bg($cmd);
2657 928d4416 Ermal
		unset($args);
2658 5b237745 Scott Ullrich
2659 61e047a5 Phil Davis
		system_dhcpleases_configure();
2660 0a5a8df9 Warren Baker
2661 61e047a5 Phil Davis
		if (platform_booting()) {
2662 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
2663 61e047a5 Phil Davis
		}
2664 5b237745 Scott Ullrich
	}
2665 a25183c5 Scott Ullrich
2666 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2667 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2668 6a01ea44 Bill Marquette
			$return = 1;
2669 61e047a5 Phil Davis
		}
2670 5b237745 Scott Ullrich
	}
2671
2672 6a01ea44 Bill Marquette
	return $return;
2673 5b237745 Scott Ullrich
}
2674
2675 6ac625e8 Viktor G
function services_unbound_configure($restart_dhcp = true, $interface = '') {
2676 532a1a0e Reid Linnemann
	global $g;
2677 175dc861 Warren Baker
	$return = 0;
2678
2679 532a1a0e Reid Linnemann
	if (config_path_enabled('system','developerspew')) {
2680 175dc861 Warren Baker
		$mt = microtime();
2681
		echo "services_unbound_configure() being called $mt\n";
2682
	}
2683
2684 532a1a0e Reid Linnemann
	if (!empty($interface) && config_path_enabled('unbound') &&
2685
	    !in_array($interface, explode(',', config_get_path('unbound/active_interface'))) &&
2686
	    !in_array($interface, explode(',', config_get_path('unbound/outgoing_interface')))) {
2687 6ac625e8 Viktor G
		return $return;
2688
	}
2689
2690 532a1a0e Reid Linnemann
	if (config_path_enabled('unbound')) {
2691 782453b4 jim-p
		require_once('/etc/inc/unbound.inc');
2692
2693
		/* Stop Unbound using TERM */
2694
		if (file_exists("{$g['varrun_path']}/unbound.pid")) {
2695
			sigkillbypid("{$g['varrun_path']}/unbound.pid", "TERM");
2696
		}
2697
2698
		/* If unbound is still running, wait up to 30 seconds for it to terminate. */
2699
		for ($i=1; $i <= 30; $i++) {
2700
			if (is_process_running('unbound')) {
2701
				sleep(1);
2702
			}
2703
		}
2704
2705 6a4635fc BBcan177
		$python_mode = false;
2706 19c90883 Kristof Provost
		$python_script = config_get_path('unbound/python_script');
2707 532a1a0e Reid Linnemann
		if (config_path_enabled('unbound','python') && !empty($python_script)) {
2708 6a4635fc BBcan177
			$python_mode = true;
2709
		}
2710
2711
		/* Include any additional functions as defined by python script include file */
2712 532a1a0e Reid Linnemann
		if (file_exists("{$g['unbound_chroot_path']}/{$python_script}_include.inc")) {
2713
			exec("/usr/local/bin/php -l " . escapeshellarg("{$g['unbound_chroot_path']}/{$python_script}_include.inc")
2714 6a4635fc BBcan177
				. " 2>&1", $py_output, $py_retval);
2715
			if ($py_retval == 0) {
2716 532a1a0e Reid Linnemann
				require_once("{$g['unbound_chroot_path']}/{$python_script}_include.inc");
2717 6a4635fc BBcan177
			}
2718
		}
2719
2720 a4ca3a94 BBcan177
		/* DNS Resolver python integration */
2721 d83d2280 jim-p
		if ($python_mode) {
2722
			if (!is_dir("{$g['unbound_chroot_path']}/dev")) {
2723
				safe_mkdir("{$g['unbound_chroot_path']}/dev");
2724
			}
2725 a72b320e Viktor G
			$status = "/sbin/mount | /usr/bin/grep " .  escapeshellarg("{$g['unbound_chroot_path']}/dev");
2726
			if (!trim($status)) {
2727
				exec("/sbin/mount -t devfs devfs " . escapeshellarg("{$g['unbound_chroot_path']}/dev"));
2728
			}
2729 d83d2280 jim-p
		} else {
2730
			if (is_dir("{$g['unbound_chroot_path']}/dev")) {
2731
				exec("/sbin/umount -f " . escapeshellarg("{$g['unbound_chroot_path']}/dev"));
2732
			}
2733
		}
2734 a4ca3a94 BBcan177
		$base_folder = '/usr/local';
2735
		foreach (array('/bin', '/lib') as $dir) {
2736 6a4635fc BBcan177
			$validate = exec("/sbin/mount | /usr/bin/grep " . escapeshellarg("{$g['unbound_chroot_path']}{$base_folder}{$dir} (nullfs") . " 2>&1");
2737
			if ($python_mode) {
2738 a4ca3a94 BBcan177
2739
				// Add DNS Resolver python integration
2740
				if (empty($validate)) {
2741
					if (!is_dir("{$g['unbound_chroot_path']}{$base_folder}{$dir}")) {
2742 14b1c98d BBcan177
						safe_mkdir("{$g['unbound_chroot_path']}{$base_folder}{$dir}");
2743 a4ca3a94 BBcan177
					}
2744 6a4635fc BBcan177
					$output = $retval = '';
2745
					exec("/sbin/mount_nullfs -o ro " . escapeshellarg("/usr/local{$dir}") . ' '
2746
					    . escapeshellarg("{$g['unbound_chroot_path']}{$base_folder}{$dir}") . " 2>&1", $output, $retval);
2747
2748 a4ca3a94 BBcan177
					// Disable Unbound python on mount failure
2749
					if ($retval != 0) {
2750 532a1a0e Reid Linnemann
						config_set_path('unbound/python','');
2751 a4ca3a94 BBcan177
						$log_msg = "[Unbound-pymod]: Disabling Unbound python due to failed mount";
2752
						write_config($log_msg);
2753
						log_error($log_msg);
2754
					}
2755
				}
2756
			}
2757
2758
			// Remove DNS Resolver python integration
2759
			elseif (!empty($validate)) {
2760 14b1c98d BBcan177
				exec("/sbin/umount -t nullfs " . escapeshellarg("{$g['unbound_chroot_path']}{$base_folder}{$dir}") . " 2>&1", $output, $retval);
2761 a4ca3a94 BBcan177
				if ($retval == 0) {
2762
					foreach (array( "/usr/local{$dir}", '/usr/local', '/usr') as $folder) {
2763 2568e151 Christian McDonald
						if (!empty(g_get('unbound_chroot_path')) && g_get('unbound_chroot_path') != '/' && is_dir("{$g['unbound_chroot_path']}{$folder}")) {
2764 6a4635fc BBcan177
							@rmdir(escapeshellarg("{$g['unbound_chroot_path']}{$folder}"));
2765 a4ca3a94 BBcan177
						}
2766
2767
						// Delete remaining subfolders on next loop
2768
						if ($dir == '/bin') {
2769
							break;
2770
						}
2771
					}
2772
				}
2773
				else {
2774
					log_error("[Unbound-pymod]: Failed to unmount!");
2775
				}
2776
			}
2777
		}
2778
2779 61e047a5 Phil Davis
		if (platform_booting()) {
2780 5bb1c495 Warren Baker
			echo gettext("Starting DNS Resolver...");
2781 61e047a5 Phil Davis
		} else {
2782 175dc861 Warren Baker
			sleep(1);
2783 61e047a5 Phil Davis
		}
2784 175dc861 Warren Baker
2785 b3c6783f Renato Botelho
		/* generate hosts file */
2786 4de8f7ba Phil Davis
		if (system_hosts_generate() != 0) {
2787 b3c6783f Renato Botelho
			$return = 1;
2788 61e047a5 Phil Davis
		}
2789 b3c6783f Renato Botelho
2790 881fb186 marjohn56
		/* Check here for dhcp6 complete - wait upto 10 seconds */
2791 532a1a0e Reid Linnemann
		if(config_get_path('interfaces/wan/ipaddrv6') == 'dhcp6') {
2792 881fb186 marjohn56
			$wanif = get_real_interface("wan", "inet6");
2793
			if (platform_booting()) {
2794
				for ($i=1; $i <= 10; $i++) {
2795 67784aa6 Steve Beaver
					if (!file_exists("/tmp/{$wanif}_dhcp6_complete")) {
2796 881fb186 marjohn56
						log_error(gettext("Unbound start waiting on dhcp6c."));
2797
						sleep(1);
2798
					} else {
2799
						unlink_if_exists("/tmp/{$wanif}_dhcp6_complete");
2800
						log_error(gettext("dhcp6 init complete. Continuing"));
2801
						break;
2802 67784aa6 Steve Beaver
					}
2803 881fb186 marjohn56
				}
2804
			}
2805
		}
2806 67784aa6 Steve Beaver
2807 175dc861 Warren Baker
		sync_unbound_service();
2808 61e047a5 Phil Davis
		if (platform_booting()) {
2809 881fb186 marjohn56
			log_error(gettext("sync unbound done."));
2810 175dc861 Warren Baker
			echo gettext("done.") . "\n";
2811 61e047a5 Phil Davis
		}
2812 0a5a8df9 Warren Baker
2813 61e047a5 Phil Davis
		system_dhcpleases_configure();
2814 782453b4 jim-p
	} else {
2815
		/* kill Unbound since it should not be enabled */
2816
		if (file_exists("{$g['varrun_path']}/unbound.pid")) {
2817
			sigkillbypid("{$g['varrun_path']}/unbound.pid", "KILL");
2818
		}
2819 175dc861 Warren Baker
	}
2820
2821 57b5da70 jim-p
	if (!platform_booting() && $restart_dhcp) {
2822 4de8f7ba Phil Davis
		if (services_dhcpd_configure() != 0) {
2823 175dc861 Warren Baker
			$return = 1;
2824 61e047a5 Phil Davis
		}
2825 175dc861 Warren Baker
	}
2826
2827
	return $return;
2828
}
2829
2830 dc6a9ddc Viktor G
function services_snmpd_configure($interface='') {
2831 532a1a0e Reid Linnemann
	global $g;
2832
	if (config_path_enabled('system','developerspew')) {
2833 acd910bf Scott Ullrich
		$mt = microtime();
2834 f19d3b7a Scott Ullrich
		echo "services_snmpd_configure() being called $mt\n";
2835
	}
2836 5b237745 Scott Ullrich
2837 532a1a0e Reid Linnemann
	$bindip = config_get_path('snmpd/bindip', "");
2838 dc6a9ddc Viktor G
	if (!empty($interface) &&
2839 532a1a0e Reid Linnemann
	    !empty($bind_ip) &&
2840
	    config_path_enabled('snmpd')) {
2841
		foreach (explode(",", $bindip) as $bind_to_ip) {
2842 dc6a9ddc Viktor G
			if ($bind_to_ip == $interface) {
2843
				$interface_restart = true;
2844
				break;
2845
			}
2846
		}
2847
		if (!$interface_restart) {
2848
			return 0;
2849
		}
2850
	}
2851
2852 5b237745 Scott Ullrich
	/* kill any running snmpd */
2853
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
2854 dd18038e Ermal
	sleep(2);
2855 61e047a5 Phil Davis
	if (is_process_running("bsnmpd")) {
2856 a976fa82 Scott Ullrich
		mwexec("/usr/bin/killall bsnmpd", true);
2857 61e047a5 Phil Davis
	}
2858 5b237745 Scott Ullrich
2859 532a1a0e Reid Linnemann
	if (config_path_enabled('snmpd')) {
2860 a25183c5 Scott Ullrich
2861 61e047a5 Phil Davis
		if (platform_booting()) {
2862 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting SNMP daemon... ");
2863 61e047a5 Phil Davis
		}
2864 5b237745 Scott Ullrich
2865 63b44eed jim-p
		/* Make sure a printcap file exists or else bsnmpd will log errors. See https://redmine.pfsense.org/issues/6838 */
2866
		if (!file_exists('/etc/printcap')) {
2867
			@file_put_contents('/etc/printcap', "# Empty file to prevent bsnmpd from logging errors.\n");
2868
		}
2869
2870 5b237745 Scott Ullrich
		/* generate snmpd.conf */
2871
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
2872
		if (!$fd) {
2873 4de8f7ba Phil Davis
			printf(gettext("Error: cannot open snmpd.conf in services_snmpd_configure().%s"), "\n");
2874 5b237745 Scott Ullrich
			return 1;
2875
		}
2876 a25183c5 Scott Ullrich
2877 532a1a0e Reid Linnemann
		$snmpdcfg = config_get_path('snmpd');
2878 5b237745 Scott Ullrich
		$snmpdconf = <<<EOD
2879 532a1a0e Reid Linnemann
location := "{$snmpdcfg['syslocation']}"
2880
contact := "{$snmpdcfg['syscontact']}"
2881
read := "{$snmpdcfg['rocommunity']}"
2882 142da8f7 John Fleming
2883
EOD;
2884
2885 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2886 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd','rwenable') && preg_match('/^\S+$/', $snmpdcfg['rwcommunity'])) {
2887 61e047a5 Phil Davis
			$snmpdconf .= <<<EOD
2888 142da8f7 John Fleming
# write string
2889 532a1a0e Reid Linnemann
write := "{$snmpdcfg['rwcommunity']}"
2890 142da8f7 John Fleming
2891
EOD;
2892
		}
2893
*/
2894
2895
2896 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd','trapenable') && preg_match('/^\S+$/', $snmpdcfg['trapserver'])) {
2897 61e047a5 Phil Davis
			$snmpdconf .= <<<EOD
2898 142da8f7 John Fleming
# SNMP Trap support.
2899 532a1a0e Reid Linnemann
traphost := {$snmpdcfg['trapserver']}
2900
trapport := {$snmpdcfg['trapserverport']}
2901
trap := "{$snmpdcfg['trapstring']}"
2902 142da8f7 John Fleming
2903
2904
EOD;
2905
		}
2906
2907 532a1a0e Reid Linnemann
		$sysDescr = "{$g['product_label']} " .
2908
				  config_get_path('system/hostname') . '.' .config_get_path('system/domain') .
2909 cda2ef35 Renato Botelho do Couto
			" {$g['product_version_string']} " . php_uname("s") .
2910 dadf8ebb jim-p
			" " . php_uname("r") . " " . php_uname("m");
2911 142da8f7 John Fleming
2912
		$snmpdconf .= <<<EOD
2913 d47a8a69 Scott Ullrich
system := 1     # pfSense
2914
%snmpd
2915 dadf8ebb jim-p
sysDescr			= "{$sysDescr}"
2916 d47a8a69 Scott Ullrich
begemotSnmpdDebugDumpPdus       = 2
2917
begemotSnmpdDebugSyslogPri      = 7
2918
begemotSnmpdCommunityString.0.1 = $(read)
2919 142da8f7 John Fleming
2920
EOD;
2921
2922 e8c516a0 Phil Davis
/* No docs on what write strings do there so disable for now.
2923 61e047a5 Phil Davis
		if (isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])) {
2924
			$snmpdconf .= <<<EOD
2925 142da8f7 John Fleming
begemotSnmpdCommunityString.0.2 = $(write)
2926
2927
EOD;
2928
		}
2929
*/
2930
2931 c7f44ae0 Scott Ullrich
2932 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd','trapenable') && preg_match('/^\S+$/', config_get_path('snmpd/trapserver'))) {
2933 61e047a5 Phil Davis
			$snmpdconf .= <<<EOD
2934 142da8f7 John Fleming
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
2935
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
2936
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
2937
2938
EOD;
2939
		}
2940
2941
2942
		$snmpdconf .= <<<EOD
2943 d47a8a69 Scott Ullrich
begemotSnmpdCommunityDisable    = 1
2944 03ba7a0f John Fleming
2945
EOD;
2946
2947 df8ebedc skrude61
		$bind_to_ips = array();
2948 cd974f08 Viktor G
		$bind_to_ip6s = array();
2949 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd','bindip')) {
2950
			$ipproto = config_get_path('snmpd/ipprotocol', "4");
2951
			if (strstr($ipproto, "4")) {
2952 cd974f08 Viktor G
				$inet4 = true;
2953
			}
2954 532a1a0e Reid Linnemann
			if (strstr($ipproto, "6")) {
2955 cd974f08 Viktor G
				$inet6 = true;
2956
			}
2957 532a1a0e Reid Linnemann
			foreach (explode(",", config_get_path('snmpd/bindip', "")) as $bind_to_ip) {
2958 df8ebedc skrude61
				if (is_ipaddr($bind_to_ip)) {
2959
					$bind_to_ips[] = $bind_to_ip;
2960
				} else {
2961
					$if = get_real_interface($bind_to_ip);
2962
					if (does_interface_exist($if)) {
2963 cd974f08 Viktor G
						if ($inet4) {
2964
							$bindip = get_interface_ip($bind_to_ip);
2965
							if (is_ipaddrv4($bindip)) {
2966
								$bind_to_ips[] = $bindip;
2967
							}
2968
						}
2969
						if ($inet6) {
2970
							$bindip6 = get_interface_ipv6($bind_to_ip);
2971
							if (is_ipaddrv6($bindip6)) {
2972
								$bind_to_ip6s[] = $bindip6;
2973
							}
2974 df8ebedc skrude61
						}
2975
					}
2976 61e047a5 Phil Davis
				}
2977 c82b2c3f jim-p
			}
2978 7cbad422 Scott Ullrich
		}
2979 cd974f08 Viktor G
		if (!count($bind_to_ips) && $inet4) {
2980 df8ebedc skrude61
			$bind_to_ips = array("0.0.0.0");
2981
		}
2982 cd974f08 Viktor G
		if (!count($bind_to_ip6s) && $inet6) {
2983
			$bind_to_ip6s = array("::");
2984
		}
2985 7cbad422 Scott Ullrich
2986 532a1a0e Reid Linnemann
		$pollport = config_get_path('snmpd/pollport');
2987
		if (is_port($pollport)) {
2988 df8ebedc skrude61
			foreach ($bind_to_ips as $bind_to_ip) {
2989 8ec77040 jim-p
				$snmpdconf .= <<<EOD
2990 532a1a0e Reid Linnemann
begemotSnmpdPortStatus.{$bind_to_ip}.{$pollport} = 1
2991 03ba7a0f John Fleming
2992 cd974f08 Viktor G
EOD;
2993
2994
			}
2995
			foreach ($bind_to_ip6s as $bind_to_ip6) {
2996
				$bind_to_ip6 = ip6_to_asn1($bind_to_ip6);
2997
				$snmpdconf .= <<<EOD
2998 532a1a0e Reid Linnemann
begemotSnmpdTransInetStatus.2.16.{$bind_to_ip6}{$pollport}.1 = 4
2999 cd974f08 Viktor G
3000 03ba7a0f John Fleming
EOD;
3001
3002 df8ebedc skrude61
			}
3003 03ba7a0f John Fleming
		}
3004
3005
		$snmpdconf .= <<<EOD
3006 d47a8a69 Scott Ullrich
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
3007
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
3008 142da8f7 John Fleming
3009 03ba7a0f John Fleming
# These are bsnmp macros not php vars.
3010 9cc8c59e Scott Ullrich
sysContact      = $(contact)
3011
sysLocation     = $(location)
3012
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
3013 142da8f7 John Fleming
3014 d47a8a69 Scott Ullrich
snmpEnableAuthenTraps = 2
3015 03ba7a0f John Fleming
3016
EOD;
3017
3018 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd/modules', 'mibii')) {
3019 03ba7a0f John Fleming
			$snmpdconf .= <<<EOD
3020 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
3021 03ba7a0f John Fleming
3022
EOD;
3023 532a1a0e Reid Linnemann
		}
3024 03ba7a0f John Fleming
3025 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd/modules', 'netgraph')) {
3026
			$snmpdconf .= <<<EOD
3027 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
3028
%netgraph
3029
begemotNgControlNodeName = "snmpd"
3030 03ba7a0f John Fleming
3031
EOD;
3032 532a1a0e Reid Linnemann
		}
3033 03ba7a0f John Fleming
3034 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd/modules', 'pf')) {
3035
			$snmpdconf .= <<<EOD
3036 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
3037 95fb49e8 Seth Mos
3038
EOD;
3039 532a1a0e Reid Linnemann
		}
3040 95fb49e8 Seth Mos
3041 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd/modules', 'hostres')) {
3042
			$snmpdconf .= <<<EOD
3043 95fb49e8 Seth Mos
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
3044
3045
EOD;
3046 532a1a0e Reid Linnemann
		}
3047 05036071 Renato Botelho
3048 532a1a0e Reid Linnemann
		if (config_path_enabled('snmpd/modules', 'bridge')) {
3049
			$snmpdconf .= <<<EOD
3050 95fb49e8 Seth Mos
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
3051 d47a8a69 Scott Ullrich
# config must end with blank line
3052 5b237745 Scott Ullrich
3053
EOD;
3054 532a1a0e Reid Linnemann
		}
3055
		if (config_path_enabled('snmpd/modules', 'ucd')) {
3056
			$snmpdconf .= <<<EOD
3057 671914b2 jim-p
begemotSnmpdModulePath."ucd"     = "/usr/local/lib/snmp_ucd.so"
3058
3059
EOD;
3060 532a1a0e Reid Linnemann
		}
3061
		if (config_path_enabled('snmpd/modules', 'regex')) {
3062 671914b2 jim-p
				$snmpdconf .= <<<EOD
3063
begemotSnmpdModulePath."regex"     = "/usr/local/lib/snmp_regex.so"
3064
3065
EOD;
3066 03ba7a0f John Fleming
		}
3067 5b237745 Scott Ullrich
3068
		fwrite($fd, $snmpdconf);
3069
		fclose($fd);
3070 928d4416 Ermal
		unset($snmpdconf);
3071 5b237745 Scott Ullrich
3072 853e003a Scott Ullrich
		/* run bsnmpd */
3073
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
3074 2073c2d5 Phil Davis
			" -p {$g['varrun_path']}/snmpd.pid");
3075 5b237745 Scott Ullrich
3076 61e047a5 Phil Davis
		if (platform_booting()) {
3077 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
3078 61e047a5 Phil Davis
		}
3079 5b237745 Scott Ullrich
	}
3080
3081
	return 0;
3082
}
3083
3084 7c9da7be jim-p
function services_dnsupdate_process($int = "", $updatehost = "", $forced = false) {
3085 532a1a0e Reid Linnemann
	global $g;
3086
	if (config_path_enabled('system','developerspew')) {
3087 acd910bf Scott Ullrich
		$mt = microtime();
3088 f19d3b7a Scott Ullrich
		echo "services_dnsupdate_process() being called $mt\n";
3089 acd910bf Scott Ullrich
	}
3090 f19d3b7a Scott Ullrich
3091 a23d7248 Scott Ullrich
	/* Dynamic DNS updating active? */
3092 532a1a0e Reid Linnemann
	if (empty(config_get_path('dnsupdates/dnsupdate'))) {
3093 858e0d8d Renato Botelho
		return 0;
3094
	}
3095 61e047a5 Phil Davis
3096 858e0d8d Renato Botelho
	$notify_text = "";
3097 43a9b03d PiBa-NL
	$gwgroups = return_gateway_groups_array(true);
3098 532a1a0e Reid Linnemann
	foreach (config_get_path('dnsupdates/dnsupdate', []) as $i => $dnsupdate) {
3099 00d3003d jim-p
		if (!is_array($dnsupdate) ||
3100
		    empty($dnsupdate) ||
3101
		    !isset($dnsupdate['enable'])) {
3102 858e0d8d Renato Botelho
			continue;
3103
		}
3104
		/*
3105
		 * If it's using a gateway group, check if interface is
3106
		 * the active gateway for that group
3107
		 */
3108
		$group_int = '';
3109 46583aba Renato Botelho
		$friendly_group_int = '';
3110 3b41c8f3 Pulcov
		$gwgroup_member = false;
3111 858e0d8d Renato Botelho
		if (is_array($gwgroups[$dnsupdate['interface']])) {
3112
			if (!empty($gwgroups[$dnsupdate['interface']][0]['vip'])) {
3113
				$group_int = $gwgroups[$dnsupdate['interface']][0]['vip'];
3114 61e047a5 Phil Davis
			} else {
3115 858e0d8d Renato Botelho
				$group_int = $gwgroups[$dnsupdate['interface']][0]['int'];
3116 46583aba Renato Botelho
				$friendly_group_int =
3117 271fc45e jim-p
				    convert_real_interface_to_friendly_interface_name(
3118 46583aba Renato Botelho
					$group_int);
3119 3b41c8f3 Pulcov
				if (!empty($int)) {
3120
					$gwgroup_member =
3121
					    interface_gateway_group_member(get_real_interface($int),
3122
					    $dnsupdate['interface']);
3123
				}
3124 61e047a5 Phil Davis
			}
3125 858e0d8d Renato Botelho
		}
3126 3b41c8f3 Pulcov
		if (!empty($int) && ($int != $dnsupdate['interface']) && !$gwgroup_member &&
3127 46583aba Renato Botelho
		    ($int != $group_int) && ($int != $friendly_group_int)) {
3128 858e0d8d Renato Botelho
			continue;
3129
		}
3130
		if (!empty($updatehost) && ($updatehost != $dnsupdate['host'])) {
3131
			continue;
3132
		}
3133 61e047a5 Phil Davis
3134 858e0d8d Renato Botelho
		/* determine interface name */
3135
		$if = get_failover_interface($dnsupdate['interface']);
3136 7c9da7be jim-p
3137 900663a4 jim-p
		/* Determine address to update and default binding address */
3138 858e0d8d Renato Botelho
		if (isset($dnsupdate['usepublicip'])) {
3139
			$wanip = dyndnsCheckIP($if);
3140 070fb1a8 Viktor G
			if (is_private_ip($wanip)) {
3141
				log_error(sprintf(gettext(
3142
				    "phpDynDNS: Not updating %s A record because the public IP address cannot be determined."),
3143
				    $dnsupdate['host']));
3144
				continue;
3145
			}
3146 900663a4 jim-p
			$bindipv4 = get_interface_ip($if);
3147 858e0d8d Renato Botelho
		} else {
3148
			$wanip = get_interface_ip($if);
3149 900663a4 jim-p
			$bindipv4 = $wanip;
3150 858e0d8d Renato Botelho
		}
3151 b63b534c Christian McDonald
		if (is_stf_interface($dnsupdate['interface'])) {
3152 30466aef Viktor G
			$wanipv6 = get_interface_ipv6($dnsupdate['interface'] . '_stf');
3153
		} else {
3154
			$wanipv6 = get_interface_ipv6($if);
3155
		}
3156 900663a4 jim-p
		$bindipv6 = $wanipv6;
3157
3158
		/* Handle non-default interface bindings */
3159
		if ($dnsupdate['updatesource'] == "none") {
3160
			/* When empty, the directive will be omitted. */
3161
			$bindipv4 = "";
3162
			$bindipv6 = "";
3163
		} elseif (!empty($dnsupdate['updatesource'])) {
3164
			/* Find the alternate binding address */
3165
			$bindipv4 = get_interface_ip($dnsupdate['updatesource']);
3166 b63b534c Christian McDonald
			if (is_stf_interface($dnsupdate['interface'])) {
3167 30466aef Viktor G
				$bindipv6 = get_interface_ipv6($dnsupdate['updatesource'] . '_stf');
3168
			} else {
3169
				$bindipv6 = get_interface_ipv6($dnsupdate['updatesource']);
3170
			}
3171 900663a4 jim-p
		}
3172
3173
		/* Handle IPv4/IPv6 selection for the update source interface/VIP */
3174
		switch ($dnsupdate['updatesourcefamily']) {
3175
			case "inet":
3176
				$bindip = $bindipv4;
3177
				break;
3178
			case "inet6":
3179
				$bindip = $bindipv6;
3180
				break;
3181
			case "":
3182
			default:
3183
				/* Try IPv4 first, if that is empty, try IPv6. */
3184
				/* Only specify the address if it's present, otherwise omit. */
3185
				if (!empty($bindipv4)) {
3186
					$bindip = $bindipv4;
3187
				} elseif (!empty($bindipv6)) {
3188
					$bindip = $bindipv6;
3189
				}
3190
				break;
3191
		}
3192
3193 2568e151 Christian McDonald
		$cacheFile = g_get('conf_path') .
3194 23adb26d Renato Botelho
		    "/dyndns_{$dnsupdate['interface']}_rfc2136_" .
3195
		    escapeshellarg($dnsupdate['host']) .
3196
		    "_{$dnsupdate['server']}.cache";
3197 2568e151 Christian McDonald
		$cacheFilev6 = g_get('conf_path') .
3198 474def89 Renato Botelho
		    "/dyndns_{$dnsupdate['interface']}_rfc2136_" .
3199
		    escapeshellarg($dnsupdate['host']) .
3200
		    "_{$dnsupdate['server']}_v6.cache";
3201 858e0d8d Renato Botelho
		$currentTime = time();
3202
3203
		if (!$wanip && !$wanipv6) {
3204
			continue;
3205
		}
3206
3207
		$keyname = $dnsupdate['keyname'];
3208
		/* trailing dot */
3209
		if (substr($keyname, -1) != ".") {
3210
			$keyname .= ".";
3211
		}
3212
3213
		$hostname = $dnsupdate['host'];
3214
		/* trailing dot */
3215
		if (substr($hostname, -1) != ".") {
3216
			$hostname .= ".";
3217
		}
3218 67ee1ec5 Ermal Luçi
3219 7ca8845b Joeri Capens
		/* write key file */
3220 ab1112ee Joeri Capens
		$algorithm = empty($dnsupdate['keyalgorithm']) ? 'hmac-md5' : $dnsupdate['keyalgorithm'];
3221 7ca8845b Joeri Capens
		$upkey = <<<EOD
3222
key "{$keyname}" {
3223 ab1112ee Joeri Capens
	algorithm {$algorithm};
3224 7ca8845b Joeri Capens
	secret "{$dnsupdate['keydata']}";
3225
};
3226 a23d7248 Scott Ullrich
3227
EOD;
3228 7ca8845b Joeri Capens
		@file_put_contents("{$g['varetc_path']}/nsupdatekey{$i}", $upkey);
3229 67ee1ec5 Ermal Luçi
3230 858e0d8d Renato Botelho
		/* generate update instructions */
3231
		$upinst = "";
3232
		if (!empty($dnsupdate['server'])) {
3233
			$upinst .= "server {$dnsupdate['server']}\n";
3234
		}
3235 7c9da7be jim-p
3236 07bbe19b Viktor G
		if (!empty($dnsupdate['zone'])) {
3237
			$upinst .= "zone {$dnsupdate['zone']}\n";
3238
		}
3239
3240 ed680fda Renato Botelho
		$cachedipv4 = '';
3241
		$cacheTimev4 = 0;
3242 858e0d8d Renato Botelho
		if (file_exists($cacheFile)) {
3243 23adb26d Renato Botelho
			list($cachedipv4, $cacheTimev4) = explode("|",
3244
			    file_get_contents($cacheFile));
3245 858e0d8d Renato Botelho
		}
3246 ed680fda Renato Botelho
		$cachedipv6 = '';
3247
		$cacheTimev6 = 0;
3248 858e0d8d Renato Botelho
		if (file_exists($cacheFilev6)) {
3249 23adb26d Renato Botelho
			list($cachedipv6, $cacheTimev6) = explode("|",
3250
			    file_get_contents($cacheFilev6));
3251 858e0d8d Renato Botelho
		}
3252 7c9da7be jim-p
3253 858e0d8d Renato Botelho
		// 25 Days
3254
		$maxCacheAgeSecs = 25 * 24 * 60 * 60;
3255
		$need_update = false;
3256 819a603c jim-p
3257 858e0d8d Renato Botelho
		/* Update IPv4 if we have it. */
3258
		if (is_ipaddrv4($wanip) && $dnsupdate['recordtype'] != "AAAA") {
3259 23adb26d Renato Botelho
			if (($wanip != $cachedipv4) || $forced ||
3260
			    (($currentTime - $cacheTimev4) > $maxCacheAgeSecs)) {
3261
				$upinst .= "update delete " .
3262
				    "{$dnsupdate['host']}. A\n";
3263
				$upinst .= "update add {$dnsupdate['host']}. " .
3264
				    "{$dnsupdate['ttl']} A {$wanip}\n";
3265 900663a4 jim-p
				if (!empty($bindip)) {
3266
					$upinst .= "local {$bindip}\n";
3267
				}
3268 858e0d8d Renato Botelho
				$need_update = true;
3269
			} else {
3270 23adb26d Renato Botelho
				log_error(sprintf(gettext(
3271
				    "phpDynDNS: Not updating %s A record because the IP address has not changed."),
3272
				    $dnsupdate['host']));
3273 858e0d8d Renato Botelho
			}
3274
		} else {
3275
			@unlink($cacheFile);
3276
			unset($cacheFile);
3277
		}
3278 7c9da7be jim-p
3279 858e0d8d Renato Botelho
		/* Update IPv6 if we have it. */
3280
		if (is_ipaddrv6($wanipv6) && $dnsupdate['recordtype'] != "A") {
3281 23adb26d Renato Botelho
			if (($wanipv6 != $cachedipv6) || $forced ||
3282
			    (($currentTime - $cacheTimev6) > $maxCacheAgeSecs)) {
3283
				$upinst .= "update delete " .
3284
				    "{$dnsupdate['host']}. AAAA\n";
3285
				$upinst .= "update add {$dnsupdate['host']}. " .
3286
				    "{$dnsupdate['ttl']} AAAA {$wanipv6}\n";
3287 858e0d8d Renato Botelho
				$need_update = true;
3288
			} else {
3289 23adb26d Renato Botelho
				log_error(sprintf(gettext(
3290
				    "phpDynDNS: Not updating %s AAAA record because the IPv6 address has not changed."),
3291
				    $dnsupdate['host']));
3292 858e0d8d Renato Botelho
			}
3293
		} else {
3294
			@unlink($cacheFilev6);
3295
			unset($cacheFilev6);
3296
		}
3297 67ee1ec5 Ermal Luçi
3298 858e0d8d Renato Botelho
		$upinst .= "\n";	/* mind that trailing newline! */
3299 107e8acc Ovidiu Predescu
3300 858e0d8d Renato Botelho
		if (!$need_update) {
3301
			continue;
3302
		}
3303
3304 7ca8845b Joeri Capens
		@file_put_contents("{$g['varetc_path']}/nsupdatecmds{$i}", $upinst);
3305 858e0d8d Renato Botelho
		unset($upinst);
3306
		/* invoke nsupdate */
3307 7ca8845b Joeri Capens
		$cmd = "/usr/local/bin/nsupdate -k {$g['varetc_path']}/nsupdatekey{$i}";
3308 23adb26d Renato Botelho
3309 858e0d8d Renato Botelho
		if (isset($dnsupdate['usetcp'])) {
3310
			$cmd .= " -v";
3311 a23d7248 Scott Ullrich
		}
3312 23adb26d Renato Botelho
3313 858e0d8d Renato Botelho
		$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
3314 23adb26d Renato Botelho
3315 858e0d8d Renato Botelho
		if (mwexec($cmd) == 0) {
3316
			if (!empty($cacheFile)) {
3317 23adb26d Renato Botelho
				@file_put_contents($cacheFile,
3318
				    "{$wanip}|{$currentTime}");
3319
				log_error(sprintf(gettext(
3320
				    'phpDynDNS: updating cache file %1$s: %2$s'),
3321
				    $cacheFile, $wanip));
3322
				$notify_text .= sprintf(gettext(
3323
				    'DynDNS updated IP Address (A) for %1$s on %2$s (%3$s) to %4$s'),
3324
				    $dnsupdate['host'],
3325
				    convert_real_interface_to_friendly_descr($if),
3326
				    $if, $wanip) . "\n";
3327 858e0d8d Renato Botelho
			}
3328
			if (!empty($cacheFilev6)) {
3329 23adb26d Renato Botelho
				@file_put_contents($cacheFilev6,
3330
				    "{$wanipv6}|{$currentTime}");
3331
				log_error(sprintf(gettext(
3332
				    'phpDynDNS: updating cache file %1$s: %2$s'),
3333
				    $cacheFilev6, $wanipv6));
3334
				$notify_text .= sprintf(gettext(
3335
				    'DynDNS updated IPv6 Address (AAAA) for %1$s on %2$s (%3$s) to %4$s'),
3336
				    $dnsupdate['host'],
3337
				    convert_real_interface_to_friendly_descr($if),
3338
				    $if, $wanipv6) . "\n";
3339 858e0d8d Renato Botelho
			}
3340
		} else {
3341
			if (!empty($cacheFile)) {
3342 23adb26d Renato Botelho
				log_error(sprintf(gettext(
3343
				    'phpDynDNS: ERROR while updating IP Address (A) for %1$s (%2$s)'),
3344
				    $dnsupdate['host'], $wanip));
3345 858e0d8d Renato Botelho
			}
3346
			if (!empty($cacheFilev6)) {
3347 23adb26d Renato Botelho
				log_error(sprintf(gettext(
3348
				    'phpDynDNS: ERROR while updating IP Address (AAAA) for %1$s (%2$s)'),
3349
				    $dnsupdate['host'], $wanipv6));
3350 858e0d8d Renato Botelho
			}
3351 7c9da7be jim-p
		}
3352 858e0d8d Renato Botelho
		unset($cmd);
3353
	}
3354
3355
	if (!empty($notify_text)) {
3356
		notify_all_remote($notify_text);
3357 a23d7248 Scott Ullrich
	}
3358 c7f44ae0 Scott Ullrich
3359 a23d7248 Scott Ullrich
	return 0;
3360 5b237745 Scott Ullrich
}
3361
3362 1071e028 Scott Ullrich
/* configure cron service */
3363
function configure_cron() {
3364 532a1a0e Reid Linnemann
	global $g;
3365 e7d3fc15 Ermal
3366 ff715efc jim-p
	$crontab_contents = "";
3367 107e8acc Ovidiu Predescu
3368 532a1a0e Reid Linnemann
	if (!empty(config_get_path('cron/item', []))) {
3369 1071e028 Scott Ullrich
		$crontab_contents .= "#\n";
3370 38dccf78 Renato Botelho
		$crontab_contents .= "# pfSense specific crontab entries\n";
3371 61e047a5 Phil Davis
		$crontab_contents .= "# " .gettext("Created:") . " " . date("F j, Y, g:i a") . "\n";
3372 1071e028 Scott Ullrich
		$crontab_contents .= "#\n";
3373 ff715efc jim-p
		$crontab_contents .= "SHELL=/bin/sh\n";
3374
		$crontab_contents .= "PATH=/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin:/usr/local/bin\n";
3375 1071e028 Scott Ullrich
3376 532a1a0e Reid Linnemann
		$http_proxy = config_get_path('system/proxyurl');
3377
		$http_proxyport = config_get_path('system/proxyport');
3378
		if (!empty($http_proxy)) {
3379
			if (!empty($http_proxyport)) {
3380
				$http_proxy .= ':' . $http_proxyport;
3381 61e047a5 Phil Davis
			}
3382 992f60d0 Renato Botelho
			$crontab_contents .= "HTTP_PROXY={$http_proxy}";
3383 1060378f jim-p
3384 532a1a0e Reid Linnemann
			$proxyuser = config_get_path('system/proxyuser');
3385
			$proxypass = config_get_path('system/proxypass');
3386
			if (!empty($proxyuser) && !empty($proxypass)) {
3387
				$crontab_contents .= "HTTP_PROXY_AUTH=basic:*:{$proxyuser}:{$proxypass}";
3388 1060378f jim-p
			}
3389 992f60d0 Renato Botelho
		}
3390
3391 532a1a0e Reid Linnemann
		foreach (config_get_path('cron/item', []) as $item) {
3392 1071e028 Scott Ullrich
			$crontab_contents .= "\n{$item['minute']}\t";
3393
			$crontab_contents .= "{$item['hour']}\t";
3394
			$crontab_contents .= "{$item['mday']}\t";
3395
			$crontab_contents .= "{$item['month']}\t";
3396
			$crontab_contents .= "{$item['wday']}\t";
3397
			$crontab_contents .= "{$item['who']}\t";
3398
			$crontab_contents .= "{$item['command']}";
3399
		}
3400 107e8acc Ovidiu Predescu
3401 1071e028 Scott Ullrich
		$crontab_contents .= "\n#\n";
3402 ff715efc jim-p
		$crontab_contents .= "# " . gettext("DO NOT EDIT THIS FILE MANUALLY!") . "\n";
3403
		$crontab_contents .= "# " . gettext("Use the cron package or create files in /etc/cron.d/.") . "\n";
3404 1071e028 Scott Ullrich
		$crontab_contents .= "#\n\n";
3405
	}
3406 107e8acc Ovidiu Predescu
3407 1071e028 Scott Ullrich
	/* please maintain the newline at the end of file */
3408
	file_put_contents("/etc/crontab", $crontab_contents);
3409 c2d97111 Ermal
	unset($crontab_contents);
3410 41d507a5 Scott Ullrich
3411 8fe38524 doktornotor
	/* make sure that cron is running and start it if it got killed somehow */
3412
	if (!is_process_running("cron")) {
3413
		exec("cd /tmp && /usr/sbin/cron -s 2>/dev/null");
3414
	} else {
3415 41d507a5 Scott Ullrich
	/* do a HUP kill to force sync changes */
3416 c0020b97 doktornotor
		sigkillbypid("{$g['varrun_path']}/cron.pid", "HUP");
3417 8fe38524 doktornotor
	}
3418 41d507a5 Scott Ullrich
3419 1071e028 Scott Ullrich
}
3420
3421 431484c8 Ryan Wagoner
function upnp_action ($action) {
3422 532a1a0e Reid Linnemann
	global $g;
3423 61e047a5 Phil Davis
	switch ($action) {
3424 431484c8 Ryan Wagoner
		case "start":
3425 c1ac2424 Ermal
			if (file_exists('/var/etc/miniupnpd.conf')) {
3426
				@unlink("{$g['varrun_path']}/miniupnpd.pid");
3427
				mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P {$g['varrun_path']}/miniupnpd.pid");
3428
			}
3429 431484c8 Ryan Wagoner
			break;
3430
		case "stop":
3431 c1ac2424 Ermal
			killbypid("{$g['varrun_path']}/miniupnpd.pid");
3432 61e047a5 Phil Davis
			while ((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0) {
3433 3459814a doktornotor
				mwexec('/usr/bin/killall miniupnpd 2>/dev/null', true);
3434 61e047a5 Phil Davis
			}
3435 431484c8 Ryan Wagoner
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
3436
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
3437
			break;
3438
		case "restart":
3439
			upnp_action('stop');
3440
			upnp_action('start');
3441
			break;
3442
	}
3443
}
3444
3445 6f20377b Scott Ullrich
function upnp_start() {
3446 532a1a0e Reid Linnemann
	if (empty(config_get_path('installedpackages/miniupnpd/config'))) {
3447 0c331f1e Ermal Lu?i
		return;
3448 61e047a5 Phil Davis
	}
3449 0c331f1e Ermal Lu?i
3450 374dd9fe jim-p
	if (config_get_path('installedpackages/miniupnpd/config/0/enable') == 'on') {
3451 54bdff75 Vinicius Coque
		echo gettext("Starting UPnP service... ");
3452 dcc897e5 Ermal
		require_once('/usr/local/pkg/miniupnpd.inc');
3453
		sync_package_miniupnpd();
3454
		echo "done.\n";
3455 6f20377b Scott Ullrich
	}
3456
}
3457
3458 b2bb4970 jim-p
function install_cron_job($command, $active = false, $minute = "0", $hour = "*", $monthday = "*", $month = "*", $weekday = "*", $who = "root", $write_config = true) {
3459 85405c11 jim-p
	$is_installed = false;
3460 4de8f7ba Phil Davis
	$cron_changed = true;
3461 b2bb4970 jim-p
	$change_message = "";
3462 85405c11 jim-p
3463 532a1a0e Reid Linnemann
	init_config_arr(['cron','item']);
3464 85405c11 jim-p
3465 e00916c1 jim-p
	$job = null;
3466
	foreach (config_get_path('cron/item', []) as $idx => $item) {
3467 61e047a5 Phil Davis
		if (strstr($item['command'], $command)) {
3468 85405c11 jim-p
			$is_installed = true;
3469 e00916c1 jim-p
			$job = $idx;
3470 85405c11 jim-p
			break;
3471
		}
3472
	}
3473
3474 61e047a5 Phil Davis
	if ($active) {
3475 85405c11 jim-p
		$cron_item = array();
3476
		$cron_item['minute'] = $minute;
3477
		$cron_item['hour'] = $hour;
3478
		$cron_item['mday'] = $monthday;
3479
		$cron_item['month'] = $month;
3480
		$cron_item['wday'] = $weekday;
3481
		$cron_item['who'] = $who;
3482
		$cron_item['command'] = $command;
3483 61e047a5 Phil Davis
		if (!$is_installed) {
3484 532a1a0e Reid Linnemann
			$cron_items = config_get_path('cron/item', []);
3485
			$cron_items[] = $cron_item;
3486
			config_set_path('cron/item', $cron_items);
3487 b2bb4970 jim-p
			$change_message = "Installed cron job for %s";
3488 85405c11 jim-p
		} else {
3489 e00916c1 jim-p
			if (config_get_path("cron/item/{$job}") == $cron_item) {
3490 4de8f7ba Phil Davis
				$cron_changed = false;
3491
			} else {
3492 e00916c1 jim-p
				config_set_path("cron/item/{$job}", $cron_item);
3493 b2bb4970 jim-p
				$change_message = "Updated cron job for %s";
3494 4de8f7ba Phil Davis
			}
3495 85405c11 jim-p
		}
3496
	} else {
3497 61e047a5 Phil Davis
		if ($is_installed == true) {
3498 e00916c1 jim-p
			config_del_path("cron/item/{$job}");
3499 b2bb4970 jim-p
			$change_message = "Removed cron job for %s";
3500 65b9347b PiBa-NL
		} else {
3501
			$cron_changed = false;
3502 85405c11 jim-p
		}
3503
	}
3504 994a0644 Phil Davis
3505 61e047a5 Phil Davis
	if ($cron_changed) {
3506 b2bb4970 jim-p
		/* Optionally write the configuration if this function made changes.
3507
		 * Performing a write_config() in this way can have unintended side effects. See #7146
3508
		 * Base system instances of this function do not need to write, packages may.
3509
		 */
3510
		if ($write_config) {
3511
			write_config(sprintf(gettext($change_message), $command));
3512
		}
3513 994a0644 Phil Davis
		configure_cron();
3514 61e047a5 Phil Davis
	}
3515 85405c11 jim-p
}
3516
3517 693833cb Seth Mos
?>