Project

General

Profile

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