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