Project

General

Profile

Download (60.1 KB) Statistics
| Branch: | Tag: | Revision:
1 17f6eafa Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	services.inc
5 417fc5c4 Scott Ullrich
	part of the pfSense project (http://www.pfsense.com)
6 a25183c5 Scott Ullrich
7 417fc5c4 Scott Ullrich
	originally part of m0n0wall (http://m0n0.ch/wall)
8 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9 88d3861f Podilarius
	Copyright (C) 2010	Ermal Lu�i
10 5b237745 Scott Ullrich
	All rights reserved.
11 a25183c5 Scott Ullrich
12 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14 a25183c5 Scott Ullrich
15 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17 a25183c5 Scott Ullrich
18 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21 a25183c5 Scott Ullrich
22 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33
34 523855b0 Scott Ullrich
/*
35 05c4bfa0 Ermal
	pfSense_BUILDER_BINARIES:	/usr/bin/killall	/bin/pgrep	/bin/sh	/usr/local/sbin/dhcpd	/usr/local/sbin/igmpproxy
36 523855b0 Scott Ullrich
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/usr/sbin/arp	/sbin/ifconfig	/usr/local/sbin/dnsmasq
37 abcb2bed Ermal Lu?i
	pfSense_BUILDER_BINARIES:	/usr/sbin/bsnmpd	/sbin/route	/usr/local/sbin/olsrd
38 3f9cc8e4 smos
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/miniupnpd	/usr/sbin/radvd		/usr/local/sbin/unbound
39 d48ed103 smos
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/dhcleases6
40 523855b0 Scott Ullrich
	pfSense_MODULE:	utils
41
*/
42
43 d57293a4 Seth Mos
/* implement ipv6 route advertising deamon */
44 3f9cc8e4 smos
function services_radvd_configure() {
45 d57293a4 Seth Mos
	global $config, $g;
46 3f9cc8e4 smos
	
47
	if ($g['platform'] == 'jail') 
48 7734aea6 Andrew Thompson
		return;
49
50 d57293a4 Seth Mos
	if(isset($config['system']['developerspew'])) {
51
		$mt = microtime();
52 3f9cc8e4 smos
		echo "services_radvd_configure() being called $mt\n";
53 d57293a4 Seth Mos
	}
54
55 3f9cc8e4 smos
	/* we need to shut down the radvd cleanly, it will send out the prefix
56 c377a909 smos
	 * information with a lifetime of 0 to notify clients of a (possible) new prefix */
57 3f9cc8e4 smos
	if(is_process_running("radvd")) {
58 88d3861f Podilarius
		log_error("Shutting down Router Advertisment daemon cleanly");
59 3f9cc8e4 smos
		mwexec("killall radvd");
60 d57293a4 Seth Mos
	}
61
62
	if (!is_array($config['dhcpdv6']))
63
		$config['dhcpdv6'] = array();
64
65
	$dhcpdv6cfg = $config['dhcpdv6'];
66
	$Iflist = get_configured_interface_list();
67
68 3f9cc8e4 smos
	/* write radvd.conf */
69
	$fd = fopen("{$g['varetc_path']}/radvd.conf", "w");
70 d57293a4 Seth Mos
	if (!$fd) {
71 3f9cc8e4 smos
		printf("Error: cannot open radvd.conf in services_radvd_configure().\n");
72 d57293a4 Seth Mos
		return 1;
73
	}
74
75 3f9cc8e4 smos
	$radvdconf = "# Automatically Generated, do not edit\n";
76 4a3ff493 Seth Mos
77 753bd64d Seth Mos
	/* Process all links which need the router advertise daemon */
78 3f9cc8e4 smos
	$radvdnum = 0;
79
	$radvdifs = array();
80 668e8961 smos
81 3f9cc8e4 smos
	/* handle manually configured DHCP6 server settings first */
82 d57293a4 Seth Mos
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
83 d7d2dc52 smos
		if(!isset($config['interfaces'][$dhcpv6if]['enable']))
84
			continue;
85 20a7cb15 smos
								
86 3f9cc8e4 smos
		/* are router advertisements enabled? */
87 fe838158 smos
		if($dhcpv6ifconf['ramode'] == "disabled")
88 361bb4a9 smos
			continue;
89 d57293a4 Seth Mos
90 fe838158 smos
		/* check if we need to listen on a CARP interface */
91
		$carplist = get_configured_carp_interface_list();
92
		if($dhcpv6ifconf['rainterface'] <> "") {
93
			if($carplist[$dhcpv6ifconf['rainterface']] <> "") {
94
				$realif = $dhcpv6ifconf['rainterface'];
95
			} else {
96
				$realif = get_real_interface($dhcpv6if);
97
			}
98
		}
99 d57293a4 Seth Mos
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
100 c18a10cc smos
		if(!is_ipaddrv6($ifcfgipv6))
101
			continue;
102
103 d57293a4 Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
104
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
105
		$subnetmaskv6 = gen_subnet_mask($ifcfgsnv6);
106
107 c18a10cc smos
		$radvdifs[] = $realif;
108 20a7cb15 smos
		
109 3f9cc8e4 smos
		$radvdconf .= "# Generated for DHCPv6 Server $dhcpv6if\n";
110
		$radvdconf .= "interface {$realif} {\n";
111
		$radvdconf .= "\tAdvSendAdvert on;\n";
112
		$radvdconf .= "\tMinRtrAdvInterval 3;\n";
113
		$radvdconf .= "\tMaxRtrAdvInterval 10;\n";
114
		$radvdconf .= "\tAdvLinkMTU 1280;\n";
115
		// $radvdconf .= "\tDeprecatePrefix on;\n";
116 fe838158 smos
		switch($dhcpv6ifconf['rapriority']) {
117
			case "low":
118
				$radvdconf .= "\tAdvDefaultPreference low;\n";
119
				break;
120
			case "medium":
121
				$radvdconf .= "\tAdvDefaultPreference medium;\n";
122
				break;
123
			case "high":
124
				$radvdconf .= "\tAdvDefaultPreference high;\n";
125
				break;		
126
		}
127
		switch($dhcpv6ifconf['ramode']) {
128 656f1763 Seth Mos
			case "managed":
129 3f9cc8e4 smos
				$radvdconf .= "\tAdvManagedFlag on;\n";
130
				break;
131
			case "assist":
132
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
133
				break;
134
		}
135
		$radvdconf .= "\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
136 fe838158 smos
		switch($dhcpv6ifconf['ramode']) {
137 3f9cc8e4 smos
			case "managed":
138
				$radvdconf .= "\t\tAdvOnLink on;\n";
139
				$radvdconf .= "\t\tAdvAutonomous off;\n";
140
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
141 826ac52c smos
				break;
142
			case "router":
143 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink off;\n";
144
				$radvdconf .= "\t\tAdvAutonomous off;\n";
145
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
146 656f1763 Seth Mos
				break;
147
			case "assist":
148 3f9cc8e4 smos
				$radvdconf .= "\t\tAdvOnLink on;\n";
149
				$radvdconf .= "\t\tAdvAutonomous on;\n";
150
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
151 107e8acc Ovidiu Predescu
				break;
152 3f9cc8e4 smos
			case "unmanaged":
153
				$radvdconf .= "\t\tAdvOnLink on;\n";
154
				$radvdconf .= "\t\tAdvAutonomous on;\n";
155
				$radvdconf .= "\t\tAdvRouterAddr on;\n";
156
				break;				
157 656f1763 Seth Mos
		}
158 3f9cc8e4 smos
		$radvdconf .= "\t};\n";
159
160 f535d5a0 Seth Mos
		/* add DNS servers */
161 3aa114d5 Seth Mos
		$dnslist = array();
162 f535d5a0 Seth Mos
		if(!empty($dhcpv6ifconf['dnsserver'][0])) {
163 3aa114d5 Seth Mos
			foreach($dhcpv6ifconf['dnsserver'] as $server) {
164
				if(is_ipaddrv6($server))
165
					$dnslist[] = $server;
166
			}
167 f535d5a0 Seth Mos
		} elseif (isset($config['dnsmasq']['enable'])) {
168 3aa114d5 Seth Mos
			$dnslist[] = get_interface_ipv6($dhcpv6if);
169 f535d5a0 Seth Mos
		} elseif (!empty($config['system']['dnsserver'][0])) {
170 3aa114d5 Seth Mos
			foreach($config['system']['dnsserver'] as $server) {
171
				if(is_ipaddrv6($server))
172
					$dnslist[] = $server;
173
			}
174
		}
175 3f9cc8e4 smos
		if(count($dnslist) > 0) {
176
			$dnsstring = implode(" ", $dnslist);
177
			if($dnsstring <> "")
178 6c582308 smos
				$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
179 f535d5a0 Seth Mos
		}
180
		if($dhcpv6ifconf['domain'] <> "") {
181 6c582308 smos
			$radvdconf .= "\tDNSSL {$dhcpv6ifconf['domain']} { };\n";
182 f535d5a0 Seth Mos
		} elseif ($config['system']['domain'] <> "") {
183 6c582308 smos
			$radvdconf .= "\tDNSSL {$config['system']['domain']} { };\n";
184 f535d5a0 Seth Mos
		}
185 3f9cc8e4 smos
		$radvdconf .= "};\n";
186
		$radvdnum++;
187 ed395640 Seth Mos
	}
188
189 3f9cc8e4 smos
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
190 ed395640 Seth Mos
	foreach ($Iflist as $if => $ifdescr) {
191 20a7cb15 smos
		if(!isset($config['interfaces'][$if]['track6-interface']))
192 ed395640 Seth Mos
			continue;
193 d7d2dc52 smos
		if(!isset($config['interfaces'][$if]['enable']))
194
			continue;
195 3f9cc8e4 smos
			
196 ed395640 Seth Mos
		$realif = get_real_interface($if);
197 3f9cc8e4 smos
		/* prevent duplicate entries, manual overrides */
198
		if(in_array($realif, $radvdifs))
199 7492f21d smos
			continue;
200
201 ed395640 Seth Mos
		$ifcfgipv6 = get_interface_ipv6($if);
202 c18a10cc smos
		if(!is_ipaddrv6($ifcfgipv6))
203
			continue;
204
205 ed395640 Seth Mos
		$ifcfgsnv6 = get_interface_subnetv6($if);
206
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
207
		$subnetmaskv6 = gen_subnet_mask($ifcfgsnv6);
208 20a7cb15 smos
		$trackif = $config['interfaces'][$if]['track6-interface'];
209 c18a10cc smos
		$radvdifs[] = $realif;
210
211 20a7cb15 smos
		$autotype = $config['interfaces'][$trackif]['ipaddrv6'];
212
	
213
		log_error("configuring RA on {$if} for type {$autotype} radvd subnet {$subnetv6}/{$ifcfgsnv6}");
214 668e8961 smos
215
		$dnslist = array();
216
		if(is_ipaddrv6($subnetv6)) {
217 20a7cb15 smos
			$radvdconf .= "# Generated config for {$autotype} delegation from {$trackif} on {$if}\n";
218 3f9cc8e4 smos
			$radvdconf .= "interface {$realif} {\n";
219
				$radvdconf .= "\tAdvSendAdvert on;\n";
220
				$radvdconf .= "\tMinRtrAdvInterval 3;\n";
221
				$radvdconf .= "\tMaxRtrAdvInterval 10;\n";
222
				$radvdconf .= "\tAdvLinkMTU 1280;\n";
223
				$radvdconf .= "\tAdvOtherConfigFlag on;\n";
224
					$radvdconf .= "\t\tprefix {$subnetv6}/{$ifcfgsnv6} {\n";
225
					$radvdconf .= "\t\tAdvOnLink on;\n";
226
					$radvdconf .= "\t\tAdvAutonomous on;\n";
227
					$radvdconf .= "\t\tAdvRouterAddr on;\n";
228
				$radvdconf .= "\t};\n";
229
230
				/* add DNS servers */
231
				$dnslist = array();
232
				if (isset($config['dnsmasq']['enable'])) {
233
						$dnslist[] = $ifcfgipv6;
234
				} elseif (!empty($config['system']['dnsserver'][0])) {
235
					foreach($config['system']['dnsserver'] as $server) {
236
						if(is_ipaddrv6($server))
237
							$dnslist[] = $server;
238
					}
239 668e8961 smos
				}
240 3f9cc8e4 smos
				if(count($dnslist) > 0) {
241
					$dnsstring = implode(" ", $dnslist);
242
					if($dnsstring <> "")
243 380cd810 smos
						$radvdconf .= "\tRDNSS {$dnsstring} { };\n";
244 3f9cc8e4 smos
				}
245
				if ($config['system']['domain'] <> "") {
246
					$radvdconf .= "\tDNSSL {$config['system']['domain']} { };\n";
247
				}
248
			$radvdconf .= "};\n";
249
			$radvdnum++;
250 668e8961 smos
		}
251
	}
252
253 3f9cc8e4 smos
	fwrite($fd, $radvdconf);
254 d57293a4 Seth Mos
	fclose($fd);
255
256 3f9cc8e4 smos
	if(count($radvdifs) > 0) {
257
		mwexec("/usr/local/sbin/radvd -C {$g['varetc_path']}/radvd.conf -m syslog");
258 d57293a4 Seth Mos
	}
259
	return 0;
260
}
261
262 5b237745 Scott Ullrich
function services_dhcpd_configure() {
263 f19d3b7a Scott Ullrich
	global $config, $g;
264 2fb056d8 Seth Mos
265
	/* configure DHCPD chroot once */
266
	$fd = fopen("{$g['tmp_path']}/dhcpd.sh","w");
267
	$status = `mount | grep "{$g['dhcpd_chroot_path']}/dev"`;
268
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}\n");
269
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
270
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
271
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
272
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
273
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/run\n");
274
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
275
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
276
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/run\n");
277
	fwrite($fd, "chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
278
	fwrite($fd, "cp /lib/libc.so.* {$g['dhcpd_chroot_path']}/lib/\n");
279
	fwrite($fd, "cp /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
280
	fwrite($fd, "chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
281 092462dc smos
282 2fb056d8 Seth Mos
	if(!trim($status))
283
		fwrite($fd, "mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
284
	fclose($fd);
285
	mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh");
286
287
	services_dhcpdv4_configure();
288
	services_dhcpdv6_configure();
289 3f9cc8e4 smos
	services_radvd_configure();
290 2fb056d8 Seth Mos
	return;
291
292
}
293
function services_dhcpdv4_configure() {
294
	global $config, $g;
295 107e8acc Ovidiu Predescu
296
	if($g['services_dhcp_server_enable'] == false)
297 e3a13b00 Scott Ullrich
		return;
298
299 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
300 acd910bf Scott Ullrich
		$mt = microtime();
301 2fb056d8 Seth Mos
		echo "services_dhcpdv4_configure($if) being called $mt\n";
302 acd910bf Scott Ullrich
	}
303 107e8acc Ovidiu Predescu
304 af25d415 Chris Buechler
	/* kill any running dhcpd */
305 2a1bd027 Seth Mos
	if(is_process_running("dhcpd")) {
306 ed395640 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpd.pid");
307 2a1bd027 Seth Mos
	}
308 a25183c5 Scott Ullrich
309 15be1722 Ermal Luçi
	/* DHCP enabled on any interfaces? */
310
	if (!is_dhcp_server_enabled())
311
		return 0;
312
313 48ab0cd2 Scott Ullrich
	/* if OLSRD is enabled, allow WAN to house DHCP. */
314 c7f44ae0 Scott Ullrich
	if($config['installedpackages']['olsrd'])
315
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd)
316
				if($olsrd['enable'])
317 48ab0cd2 Scott Ullrich
					$is_olsr_enabled = true;
318
319 381e43e0 jim-p
	if ($g['booting']) {
320
		if ($g['platform'] != "pfSense") {
321
			/* restore the leases, if we have them */
322
			if (file_exists("{$g['cf_conf_path']}/dhcpleases.tgz")) {
323
				$dhcprestore = "";
324
				$dhcpreturn = "";
325
				exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcpleases.tgz 2>&1", $dhcprestore, $dhcpreturn);
326
				$dhcprestore = implode(" ", $dhcprestore);
327
				if($dhcpreturn <> 0) {
328 addc0439 Renato Botelho
					log_error(sprintf(gettext('DHCP leases restore failed exited with %1$s, the error is: %2$s%3$s'), $dhcpreturn, $dhcprestore, "\n"));
329 381e43e0 jim-p
				}
330
			}
331
		}
332
	}
333
334 5b237745 Scott Ullrich
	$syscfg = $config['system'];
335 a8a98fb4 Seth Mos
	if (!is_array($config['dhcpd']))
336
		$config['dhcpd'] = array();
337 d57293a4 Seth Mos
	$dhcpdcfg = $config['dhcpd'];
338 6f9b8073 Ermal Luçi
	$Iflist = get_configured_interface_list();
339 107e8acc Ovidiu Predescu
340 5b237745 Scott Ullrich
	if ($g['booting'])
341 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP service...");
342 5b237745 Scott Ullrich
	else
343
		sleep(1);
344 a25183c5 Scott Ullrich
345 5b237745 Scott Ullrich
	/* write dhcpd.conf */
346 1d023e00 Scott Ullrich
	$fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w");
347 2fb056d8 Seth Mos
	if (!$fd) {
348 8b6313a4 jim-p
		printf(gettext("Error: cannot open dhcpd.conf in services_dhcpdv4_configure().%s"), "\n");
349 5b237745 Scott Ullrich
		return 1;
350
	}
351 a25183c5 Scott Ullrich
352 518030b3 Scott Ullrich
	$custoptions = "";
353 107e8acc Ovidiu Predescu
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
354 c71f5fcd Ermal
		if(is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
355 5ac2f583 Ermal
			foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
356 678dfd0f Erik Fonnesbeck
				if(!empty($item['type']))
357
					$itemtype = $item['type'];
358
				else
359
					$itemtype = "text";
360
				$custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = {$itemtype};\n";
361 518030b3 Scott Ullrich
			}
362
		}
363
	}
364 4cab31d0 Scott Ullrich
365 5b237745 Scott Ullrich
	$dhcpdconf = <<<EOD
366 107e8acc Ovidiu Predescu
367 5b237745 Scott Ullrich
option domain-name "{$syscfg['domain']}";
368 6c23757b Martin Fuchs
option ldap-server code 95 = text;
369 9be23653 Martin Fuchs
option domain-search-list code 119 = text;
370 518030b3 Scott Ullrich
{$custoptions}
371 5b237745 Scott Ullrich
default-lease-time 7200;
372
max-lease-time 86400;
373
log-facility local7;
374
ddns-update-style none;
375 175fe82b Scott Ullrich
one-lease-per-client true;
376 436a0f50 Scott Ullrich
deny duplicates;
377 9c88328f Scott Ullrich
ping-check true;
378 5b237745 Scott Ullrich
379
EOD;
380 a25183c5 Scott Ullrich
381 d8912c6b Chris Buechler
	if(!isset($dhcpifconf['disableauthoritative']))
382
		$dhcpdconf .= "authoritative;\n";
383
384 107e8acc Ovidiu Predescu
	if(isset($dhcpifconf['alwaysbroadcast']))
385 5252b98d Scott Ullrich
		$dhcpdconf .= "always-broadcast on\n";
386
387 5b237745 Scott Ullrich
	$dhcpdifs = array();
388 c08a5659 smos
	$add_routers = false;
389
	$gateways_arr = return_gateways_array();
390
	/* only add a routers line if the system has any IPv4 gateway at all */
391
	/* a static route has a gateway, manually overriding this field always works */
392
	foreach($gateways_arr as $gwitem) {
393
		if($gwitem['ipprotocol'] == "inet") {
394
			$add_routers = true;
395
			break;
396
		}
397
	}
398 c7f44ae0 Scott Ullrich
399 4494cf6a Chris Buechler
	/*    loop through and determine if we need to setup
400 8fa56d1f Scott Ullrich
	 *    failover peer "bleh" entries
401
	 */
402
	$dhcpnum = 0;
403
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
404 53f32329 Scott Ullrich
405 09f11c71 jim-p
		interfaces_staticarp_configure($dhcpif);
406
407 6f9b8073 Ermal Luçi
		if (!isset($dhcpifconf['enable']))
408
			continue;
409
410 8fa56d1f Scott Ullrich
		if($dhcpifconf['failover_peerip'] <> "") {
411 85b13495 jim-p
			$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
412
			$intip = find_interface_ip($int);
413
			$real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif);
414 8fa56d1f Scott Ullrich
			/*
415
			 *    yep, failover peer is defined.
416
			 *    does it match up to a defined vip?
417
			 */
418 d2edbd8a Scott Ullrich
			$skew = 110;
419 2d314e69 Scott Ullrich
			$a_vip = &$config['virtualip']['vip'];
420 6181b36f Scott Ullrich
			if(is_array($a_vip)) {
421
				foreach ($a_vip as $vipent) {
422
					if($int == $real_dhcpif) {
423
						/* this is the interface! */
424 ad487c48 jim-p
						if(is_numeric($vipent['advskew']) && ($vipent['advskew'] < "20"))
425 6181b36f Scott Ullrich
							$skew = 0;
426
					}
427 8fa56d1f Scott Ullrich
				}
428 25066204 Scott Ullrich
			} else {
429 f1a44a3a Carlos Eduardo Ramos
				log_error(gettext("Warning!  DHCP Failover setup and no CARP virtual IP's defined!"));
430 8fa56d1f Scott Ullrich
			}
431 5e390f54 Scott Ullrich
			if($skew > 10) {
432 8fa56d1f Scott Ullrich
				$type = "secondary";
433 4d3ff974 Scott Ullrich
				$dhcpdconf_pri  = "mclt 600;\n";
434 0e93097a Scott Ullrich
				$my_port = "520";
435
				$peer_port = "519";
436 8fa56d1f Scott Ullrich
			} else {
437 0e93097a Scott Ullrich
				$my_port = "519";
438
				$peer_port = "520";
439 8fa56d1f Scott Ullrich
				$type = "primary";
440 4d3ff974 Scott Ullrich
				$dhcpdconf_pri  = "split 128;\n";
441 1a0bb737 Scott Ullrich
				$dhcpdconf_pri .= "  mclt 600;\n";
442 8fa56d1f Scott Ullrich
			}
443
			$dhcpdconf .= <<<EOPP
444 c7f44ae0 Scott Ullrich
failover peer "dhcp{$dhcpnum}" {
445 8fa56d1f Scott Ullrich
  {$type};
446
  address {$intip};
447 0e93097a Scott Ullrich
  port {$my_port};
448 8fa56d1f Scott Ullrich
  peer address {$dhcpifconf['failover_peerip']};
449 0e93097a Scott Ullrich
  peer port {$peer_port};
450 2cd5ce14 Scott Ullrich
  max-response-delay 10;
451 b865d178 Scott Ullrich
  max-unacked-updates 10;
452
  {$dhcpdconf_pri}
453 b259d1c6 Scott Ullrich
  load balance max seconds 3;
454 8fa56d1f Scott Ullrich
}
455 c286d395 Scott Ullrich
456 8fa56d1f Scott Ullrich
EOPP;
457
		$dhcpnum++;
458
		}
459
	}
460
461
	$dhcpnum = 0;
462
463 5b237745 Scott Ullrich
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
464 a25183c5 Scott Ullrich
465 5b237745 Scott Ullrich
		$ifcfg = $config['interfaces'][$dhcpif];
466 a25183c5 Scott Ullrich
467 6f9b8073 Ermal Luçi
		if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif]))
468 5b237745 Scott Ullrich
			continue;
469 a55e9c70 Ermal Lu?i
		$ifcfgip = get_interface_ip($dhcpif);
470
		$ifcfgsn = get_interface_subnet($dhcpif);
471
		$subnet = gen_subnet($ifcfgip, $ifcfgsn);
472
		$subnetmask = gen_subnet_mask($ifcfgsn);
473 a25183c5 Scott Ullrich
474 85e3f445 Ermal
		if (!is_ipaddr($subnet))
475
			continue;
476
477 48ab0cd2 Scott Ullrich
		if($is_olsr_enabled == true)
478 c7f44ae0 Scott Ullrich
			if($dhcpifconf['netmask'])
479 9a537862 Scott Ullrich
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
480 48ab0cd2 Scott Ullrich
481 5b237745 Scott Ullrich
		$dnscfg = "";
482 a25183c5 Scott Ullrich
483 5b237745 Scott Ullrich
		if ($dhcpifconf['domain']) {
484
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
485
		}
486 107e8acc Ovidiu Predescu
487 84931046 jim-p
		if($dhcpifconf['domainsearchlist'] <> "") {
488 a3de8b9e Pierre POMES
			$dnscfg .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpifconf['domainsearchlist'])) . "\";\n";
489 84931046 jim-p
		}
490 9be23653 Martin Fuchs
491 4e9cd828 Seth Mos
		if (isset($dhcpifconf['ddnsupdate'])) {
492
			if($dhcpifconf['ddnsdomain'] <> "") {
493
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
494
			}
495
			$dnscfg .= "	ddns-update-style interim;\n";
496
		}
497
498 aff9d6ab Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
499 8ee01642 Scott Ullrich
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
500 aff9d6ab Scott Ullrich
		} else if (isset($config['dnsmasq']['enable'])) {
501 a55e9c70 Ermal Lu?i
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
502 aff9d6ab Scott Ullrich
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
503
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
504
		}
505
506 85e3f445 Ermal
		$dhcpdconf .= "subnet {$subnet} netmask {$subnetmask} {\n";
507 aff9d6ab Scott Ullrich
		$dhcpdconf .= "	pool {\n";
508
509 2589c9bd Scott Ullrich
		/* is failover dns setup? */
510 698f5500 Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
511 aff9d6ab Scott Ullrich
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
512 698f5500 Scott Ullrich
			if($dhcpifconf['dnsserver'][1] <> "")
513
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
514 e35dfd89 Scott Ullrich
			$dhcpdconf .= ";\n";
515 5b237745 Scott Ullrich
		}
516 c7f44ae0 Scott Ullrich
517
		if($dhcpifconf['failover_peerip'] <> "")
518 aff9d6ab Scott Ullrich
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
519 c7f44ae0 Scott Ullrich
520 a25183c5 Scott Ullrich
		if (isset($dhcpifconf['denyunknown']))
521 f0e80b72 jim-p
		   $dhcpdconf .= "		deny unknown-clients;\n";
522 a25183c5 Scott Ullrich
523 c08a5659 smos
		if ($dhcpifconf['gateway']) {
524 5b237745 Scott Ullrich
			$routers = $dhcpifconf['gateway'];
525 c08a5659 smos
			$add_routers = true;
526
		} else {
527 a55e9c70 Ermal Lu?i
			$routers = $ifcfgip;
528 c08a5659 smos
		}
529 a25183c5 Scott Ullrich
530 8fa56d1f Scott Ullrich
		if($dhcpifconf['failover_peerip'] <> "") {
531
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
532
			$dhcpnum++;
533
		}
534
535 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
536
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
537
	}
538 c08a5659 smos
539
EOD;
540
541
		if($add_routers)
542
			$dhcpdconf .= "	option routers {$routers};\n";
543
		$dhcpdconf .= <<<EOD
544 5b237745 Scott Ullrich
$dnscfg
545
546
EOD;
547 693833cb Seth Mos
    		// default-lease-time
548 518030b3 Scott Ullrich
		if ($dhcpifconf['defaultleasetime'])
549 5b237745 Scott Ullrich
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
550 518030b3 Scott Ullrich
551
		// max-lease-time
552 5b237745 Scott Ullrich
		if ($dhcpifconf['maxleasetime'])
553
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
554 a25183c5 Scott Ullrich
555 518030b3 Scott Ullrich
		// netbios-name*
556 5b237745 Scott Ullrich
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
557
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
558
			$dhcpdconf .= "	option netbios-node-type 8;\n";
559
		}
560 a25183c5 Scott Ullrich
561 518030b3 Scott Ullrich
		// ntp-servers
562 ad171999 Seth Mos
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0])
563
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
564
565 518030b3 Scott Ullrich
		// tftp-server-name
566 ee1b024e Martin Fuchs
		if ($dhcpifconf['tftp'] <> "")
567 6c23757b Martin Fuchs
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
568
569 518030b3 Scott Ullrich
		// Handle option, number rowhelper values
570
		$dhcpdconf .= "\n";
571
		if($dhcpifconf['numberoptions']['item']) {
572 5ac2f583 Ermal
			foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
573 678dfd0f Erik Fonnesbeck
				if(empty($item['type']) || $item['type'] == "text")
574
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} \"{$item['value']}\";\n";
575
				else
576
					$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} {$item['value']};\n";
577 518030b3 Scott Ullrich
			}
578
		}
579
580
		// ldap-server
581
		if ($dhcpifconf['ldap'] <> "")
582 6c23757b Martin Fuchs
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
583
584 518030b3 Scott Ullrich
		// net boot information
585 4e9cd828 Seth Mos
		if(isset($dhcpifconf['netboot'])) {
586 a2578c27 Anthony Wrather
			if ($dhcpifconf['nextserver'] <> "") {
587
				$dhcpdconf .= "	next-server {$dhcpifconf['nextserver']};\n";
588
			}
589
			if ($dhcpifconf['filename'] <> "") {
590 4e9cd828 Seth Mos
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
591
			}
592 ca126e03 Martin Fuchs
			if ($dhcpifconf['rootpath'] <> "") {
593
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
594 518030b3 Scott Ullrich
      		}
595 4e9cd828 Seth Mos
		}
596 107e8acc Ovidiu Predescu
597 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
598
}
599
600
EOD;
601
602
		/* add static mappings */
603
		if (is_array($dhcpifconf['staticmap'])) {
604 a25183c5 Scott Ullrich
605 5b237745 Scott Ullrich
			$i = 0;
606
			foreach ($dhcpifconf['staticmap'] as $sm) {
607
				$dhcpdconf .= <<<EOD
608
host s_{$dhcpif}_{$i} {
609
	hardware ethernet {$sm['mac']};
610
611
EOD;
612
				if ($sm['ipaddr'])
613
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
614 a25183c5 Scott Ullrich
615 ad30055f Ermal Lu?i
				if ($sm['hostname']) {
616
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
617 46c5b763 pierrepomes
					$dhhostname = str_replace(".", "_", $dhhostname);
618 2f590513 smos
					$dhcpdconf .= "	option host-name \"{$dhhostname}\";\n";
619 ad30055f Ermal Lu?i
				}
620 a2578c27 Anthony Wrather
				if ($sm['filename'])
621
					$dhcpdconf .= "	filename \"{$sm['filename']}\";\n";
622
623
				if ($sm['rootpath'])
624
					$dhcpdconf .= "	option root-path \"{$sm['rootpath']}\";\n";
625 80717709 Martin Fuchs
626 5b237745 Scott Ullrich
				$dhcpdconf .= "}\n";
627
				$i++;
628
			}
629
		}
630 a25183c5 Scott Ullrich
631 6f9b8073 Ermal Luçi
		$dhcpdifs[] = get_real_interface($dhcpif);
632 5b237745 Scott Ullrich
	}
633
634 2fb056d8 Seth Mos
	fwrite($fd, $dhcpdconf);
635
	fclose($fd);
636
637
	/* create an empty leases database */
638
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
639 107e8acc Ovidiu Predescu
640 2fb056d8 Seth Mos
641
	/* fire up dhcpd in a chroot */
642
	if(count($dhcpdifs) > 0) {
643
		mwexec("/usr/local/sbin/dhcpd -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf /etc/dhcpd.conf -pf {$g['varrun_path']}/dhcpd.pid " .
644
			join(" ", $dhcpdifs));
645
	}
646
647
	if ($g['booting']) {
648
		print "done.\n";
649
	}
650
651
	return 0;
652
}
653
654
function services_dhcpdv6_configure() {
655
	global $config, $g;
656 107e8acc Ovidiu Predescu
657
	if($g['services_dhcp_server_enable'] == false)
658 2fb056d8 Seth Mos
		return;
659
660
	if(isset($config['system']['developerspew'])) {
661
		$mt = microtime();
662
		echo "services_dhcpd_configure($if) being called $mt\n";
663
	}
664 107e8acc Ovidiu Predescu
665 2fb056d8 Seth Mos
	/* kill any running dhcpd */
666
	if(is_process_running("dhcpd")) {
667 bfb3e717 Seth Mos
		killbypid("{$g['dhcpd_chroot_path']}{$g['varrun_path']}/dhcpdv6.pid");
668 f7cd5647 smos
		killbypid("{$g['varrun_path']}/dhcpleases6.pid");
669 2fb056d8 Seth Mos
	}
670
671
	/* DHCP enabled on any interfaces? */
672 a6610d82 smos
	if (!is_dhcpv6_server_enabled())
673 2fb056d8 Seth Mos
		return 0;
674
675
	if ($g['booting']) {
676
		if ($g['platform'] != "pfSense") {
677
			/* restore the leases, if we have them */
678
			if (file_exists("{$g['cf_conf_path']}/dhcp6leases.tgz")) {
679
				$dhcprestore = "";
680
				$dhcpreturn = "";
681
				exec("cd /;LANG=C /usr/bin/tar -xzf {$g['cf_conf_path']}/dhcp6leases.tgz 2>&1", $dhcprestore, $dhcpreturn);
682
				$dhcprestore = implode(" ", $dhcprestore);
683
				if($dhcpreturn <> 0) {
684
					log_error("DHCP leases v6 restore failed exited with $dhcpreturn, the error is: $dhcprestore\n");
685
				}
686
			}
687
		}
688
	}
689
690
	$syscfg = $config['system'];
691
	if (!is_array($config['dhcpdv6']))
692
		$config['dhcpdv6'] = array();
693
	$dhcpdv6cfg = $config['dhcpdv6'];
694
	$Iflist = get_configured_interface_list();
695 107e8acc Ovidiu Predescu
696 2fb056d8 Seth Mos
	if ($g['booting'])
697
		echo "Starting DHCPv6 service...";
698
	else
699
		sleep(1);
700
701 a6610d82 smos
	/* we add a fake entry for interfaces that are set to track6 another WAN */
702
	foreach($Iflist as $ifname) {
703
		$realif = get_real_interface($ifname);
704 4a41dff7 smos
		$ifcfgipv6 = find_interface_ipv6($realif);
705 a6610d82 smos
		if(!is_ipaddrv6($ifcfgipv6))
706
			continue;
707 4a41dff7 smos
		$ifcfgipv6 = Net_IPv6::getNetmask($ifcfgipv6, 64);
708 a6610d82 smos
		if($config['interfaces'][$ifname]['track6-interface'] <> "") {
709
			$trackifname = $config['interfaces'][$ifname]['track6-interface'];
710
			$trackcfg = $config['interfaces'][$trackifname];
711 319c2bfd smos
			$pdlen = calculate_ipv6_delegation_length($trackifname);
712 a6610d82 smos
			$ifcfgipv6arr =explode(":", $ifcfgipv6);
713
			$dhcpdv6cfg[$ifname] = array();
714
			$dhcpdv6cfg[$ifname]['enable'] = true;
715
			/* range */
716
			$ifcfgipv6arr[7] = "1000";
717
			$dhcpdv6cfg[$ifname]['range']['from'] = Net_IPv6::compress(implode(":", $ifcfgipv6arr));
718
			$ifcfgipv6arr[7] = "2000";
719
			$dhcpdv6cfg[$ifname]['range']['to'] = Net_IPv6::compress(implode(":", $ifcfgipv6arr));;
720
			/* prefix length > 0? We can add dhcp6 prefix delegation server */
721
			if($pdlen > 2) {
722 c45b079d smos
				$pdlenmax = $pdlen;
723 4aab9c6c smos
				$pdlenhalf = $pdlenmax -1;
724
				$pdlenmin = (64 - ceil($pdlenhalf / 4));
725
				$dhcpdv6cfg[$ifname]['prefixrange']['prefixlength'] = $pdlenmin;
726
727
				/* set the delegation start to half the current address block */
728 c45b079d smos
				$range = Net_IPv6::parseAddress($ifcfgipv6, (64 - $pdlenmax));
729 4aab9c6c smos
				$range['start'] = Net_IPv6::getNetmask($range['end'], (64 - $pdlenhalf));
730
731
				/* set the end range to a multiple of the prefix delegation size, required by dhcpd */
732 c45b079d smos
				$range = Net_IPv6::parseAddress($range['end'], (64 - $pdlenhalf));
733 4aab9c6c smos
				$range['end'] = Net_IPv6::getNetmask($range['end'], (64 - round($pdlen / 2)));
734
735 a6610d82 smos
				$dhcpdv6cfg[$ifname]['prefixrange']['from'] = Net_IPv6::compress($range['start']);
736
				$dhcpdv6cfg[$ifname]['prefixrange']['to'] = Net_IPv6::compress($range['end']);
737
			}
738
		}
739
	}
740
741 2fb056d8 Seth Mos
	/* write dhcpdv6.conf */
742
	$fdv6 = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpdv6.conf", "w");
743
	if (! $fdv6) {
744
		printf("Error: cannot open dhcpdv6.conf in services_dhcpdv6_configure().\n");
745
		return 1;
746
	}
747
748
	$custoptionsv6 = "";
749 107e8acc Ovidiu Predescu
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
750 2fb056d8 Seth Mos
		if(is_array($dhcpv6ifconf['numberoptions']) && is_array($dhcpv6ifconf['numberoptions']['item'])) {
751
			foreach($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
752
				$custoptionsv6 .= "option custom-{$dhcpv6if}-{$itemv6idx} code {$itemv6['number']} = text;\n";
753
			}
754
		}
755
	}
756
757
	$dhcpdv6conf = <<<EOD
758 107e8acc Ovidiu Predescu
759 2fb056d8 Seth Mos
option domain-name "{$syscfg['domain']}";
760
option ldap-server code 95 = text;
761
option domain-search-list code 119 = text;
762
{$custoptions}
763
default-lease-time 7200;
764
max-lease-time 86400;
765
log-facility local7;
766
ddns-update-style none;
767
one-lease-per-client true;
768
deny duplicates;
769
ping-check true;
770
771
EOD;
772
773
	if(!isset($dhcpv6ifconf['disableauthoritative']))
774
		$dhcpdv6conf .= "authoritative;\n";
775
776 107e8acc Ovidiu Predescu
	if(isset($dhcpv6ifconf['alwaysbroadcast']))
777 2fb056d8 Seth Mos
		$dhcpdv6conf .= "always-broadcast on\n";
778
779
	$dhcpdv6ifs = array();
780
781
	/*    loop through and determine if we need to setup
782
	 *    failover peer "bleh" entries
783
	 */
784
	$dhcpv6num = 0;
785
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
786
787
		if (!isset($dhcpv6ifconf['enable']))
788
			continue;
789
790
		if($dhcpv6ifconf['failover_peerip'] <> "") {
791
			$intv6 = guess_interface_from_ip($dhcpv6ifconf['failover_peerip']);
792
			$intipv6 = find_interface_ipv6($intv6);
793
			$real_dhcpv6if = convert_friendly_interface_to_real_interface_name($dhcpv6if);
794
			/*
795
			 *    yep, failover peer is defined.
796
			 *    does it match up to a defined vip?
797
			 */
798
			$skew = 110;
799
			$a_vip = &$config['virtualip']['vip'];
800
			if(is_array($a_vip)) {
801
				foreach ($a_vip as $vipent) {
802
					if($intv6 == $real_dhcpv6if) {
803
						/* this is the interface! */
804
						if(is_numeric($vipent['advskew']) && ($vipent['advskew'] < "20"))
805
							$skew = 0;
806
					}
807
				}
808
			} else {
809
				log_error("Warning!  DHCPv6 Failover setup and no CARP virtual IPv6's defined!");
810
			}
811
			if($skew > 10) {
812
				$typev6 = "secondary";
813
				$dhcpdv6conf_pri  = "mclt 600;\n";
814
				$my_portv6 = "520";
815
				$peer_portv6 = "519";
816
			} else {
817
				$my_portv6 = "519";
818
				$peer_portv6 = "520";
819
				$typev6 = "primary";
820
				$dhcpdv6conf_pri  = "split 128;\n";
821
				$dhcpdv6conf_pri .= "  mclt 600;\n";
822
			}
823
			$dhcpdv6conf .= <<<EOPP
824
failover peer "dhcpv6{$dhcpv6num}" {
825
  {$typev6};
826
  address {$intipv6};
827
  port {$my_portv6};
828
  peer address {$dhcpv6ifconf['failover_peerip']};
829
  peer port {$peer_portv6};
830
  max-response-delay 10;
831
  max-unacked-updates 10;
832
  {$dhcpdv6conf_pri}
833
  load balance max seconds 3;
834
}
835
836
EOPP;
837
		$dhcpv6num++;
838
		}
839
	}
840
841 693833cb Seth Mos
	$dhcpv6num = 0;
842
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
843
844
		$ifcfgv6 = $config['interfaces'][$dhcpv6if];
845
846
		if (!isset($dhcpv6ifconf['enable']) || !isset($Iflist[$dhcpv6if]))
847
			continue;
848
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
849
		$ifcfgsnv6 = get_interface_subnetv6($dhcpv6if);
850 d57293a4 Seth Mos
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
851
		$subnetmaskv6 = gen_subnet_mask($ifcfgsnv6);
852 693833cb Seth Mos
853
		if($is_olsr_enabled == true)
854
			if($dhcpv6ifconf['netmask'])
855 bfb3e717 Seth Mos
				$subnetmask = gen_subnet_maskv6($dhcpv6ifconf['netmask']);
856 693833cb Seth Mos
857
		$dnscfgv6 = "";
858
859
		if ($dhcpv6ifconf['domain']) {
860 3c009080 Seth Mos
			$dnscfgv6 .= "	option domain-name \"{$dhcpv6ifconf['domain']}\";\n";
861 693833cb Seth Mos
		}
862 107e8acc Ovidiu Predescu
863 693833cb Seth Mos
    		if($dhcpv6ifconf['domainsearchlist'] <> "") {
864 a3de8b9e Pierre POMES
			$dnscfgv6 .= "	option domain-search \"" . join("\",\"", preg_split("/[ ;]+/", $dhcpv6ifconf['domainsearchlist'])) . "\";\n";
865 693833cb Seth Mos
    		}
866
867
		if (isset($dhcpv6ifconf['ddnsupdate'])) {
868
			if($dhcpv6ifconf['ddnsdomain'] <> "") {
869 3c009080 Seth Mos
				$dnscfgv6 .= "	ddns-domainname \"{$dhcpv6ifconf['ddnsdomain']}\";\n";
870 693833cb Seth Mos
			}
871 3c009080 Seth Mos
			$dnscfgv6 .= "	ddns-update-style interim;\n";
872 693833cb Seth Mos
		}
873
874
		if (is_array($dhcpv6ifconf['dnsserver']) && ($dhcpv6ifconf['dnsserver'][0])) {
875
			$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dhcpv6ifconf['dnsserver']) . ";";
876
		} else if (isset($config['dnsmasq']['enable'])) {
877
			$dnscfgv6 .= "	option dhcp6.name-servers {$ifcfgipv6};";
878
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
879 2521266a Seth Mos
			$dns_arrv6 = array();
880
			foreach($syscfg['dnsserver'] as $dnsserver) {
881
				if(is_ipaddrv6($dnsserver)) {
882
					$dns_arrv6[] = $dnsserver;
883
				}
884
			}
885
			if(!empty($dns_arrv6))
886 2816c5a1 Seth Mos
				$dnscfgv6 .= "	option dhcp6.name-servers " . join(",", $dns_arrv6) . ";";
887 693833cb Seth Mos
		}
888
889
		$subnet6 = Net_IPv6::compress(gen_subnetv6($ifcfgipv6, $ifcfgsnv6));
890
		$dhcpdv6conf .= "subnet6 {$subnet6}/{$ifcfgsnv6} {\n";
891
892
		if($dhcpv6ifconf['failover_peerip'] <> "")
893
			$dhcpdv6conf .= "		deny dynamic bootp clients;\n";
894
895
		if (isset($dhcpv6ifconf['denyunknown']))
896 d5bff5e4 Chris Buechler
		   $dhcpdv6conf .= "		deny unknown-clients;\n";
897 693833cb Seth Mos
898
		if($dhcpv6ifconf['failover_peerip'] <> "") {
899
			$dhcpdv6conf .= "		failover peer \"dhcpv6{$dhcpv6num}\";\n";
900
			$dhcpv6num++;
901
		}
902
903
		$dhcpdv6conf .= <<<EOD
904
	range6 {$dhcpv6ifconf['range']['from']} {$dhcpv6ifconf['range']['to']};
905
$dnscfgv6
906
907
EOD;
908 bfb3e717 Seth Mos
909
		if(is_ipaddrv6($dhcpv6ifconf['prefixrange']['from']) && is_ipaddrv6($dhcpv6ifconf['prefixrange']['to'])) {
910 3f9492a7 Seth Mos
			$dhcpdv6conf .= "	prefix6 {$dhcpv6ifconf['prefixrange']['from']} {$dhcpv6ifconf['prefixrange']['to']}/{$dhcpv6ifconf['prefixrange']['prefixlength']};\n";
911 bfb3e717 Seth Mos
912
		}
913 693833cb Seth Mos
    		// default-lease-time
914
		if ($dhcpv6ifconf['defaultleasetime'])
915
			$dhcpdv6conf .= "	default-lease-time {$dhcpv6ifconf['defaultleasetime']};\n";
916
917
		// max-lease-time
918
		if ($dhcpv6ifconf['maxleasetime'])
919
			$dhcpdv6conf .= "	max-lease-time {$dhcpv6ifconf['maxleasetime']};\n";
920
921
		// ntp-servers
922 4096fe5d smos
		if (is_array($dhcpv6ifconf['ntpserver']) && $dhcpv6ifconf['ntpserver'][0]) {
923
			$ntpservers = array();
924
			foreach($dhcpv6ifconf['ntpserver'] as $ntpserver) {
925
				if(is_ipaddrv6($ntpserver))
926
					$ntpservers[] = $ntpserver;
927
			}
928
			if(count($ntpservers) > 0 )
929
				$dhcpdv6conf .= "       option dhcp6.sntp-servers " . join(",", $dhcpv6ifconf['ntpserver']) . ";\n";
930
		}
931 693833cb Seth Mos
		// tftp-server-name
932 7d504365 smos
		/* Needs ISC DHCPD support
933
		 if ($dhcpv6ifconf['tftp'] <> "")
934 693833cb Seth Mos
			$dhcpdv6conf .= "	option tftp-server-name \"{$dhcpv6ifconf['tftp']}\";\n";
935 7d504365 smos
		*/
936 693833cb Seth Mos
937
		// Handle option, number rowhelper values
938
		$dhcpdv6conf .= "\n";
939
		if($dhcpv6ifconf['numberoptions']['item']) {
940
			foreach($dhcpv6ifconf['numberoptions']['item'] as $itemv6idx => $itemv6) {
941
				$dhcpdv6conf .= "	option custom-{$dhcpv6if}-{$itemv6idx} \"{$itemv6['value']}\";\n";
942
			}
943
		}
944
945
		// ldap-server
946
		if ($dhcpv6ifconf['ldap'] <> "")
947
			$dhcpdv6conf .= "	option ldap-server \"{$dhcpv6ifconf['ldap']}\";\n";
948
949
		// net boot information
950
		if(isset($dhcpv6ifconf['netboot'])) {
951 a2578c27 Anthony Wrather
			if ($dhcpv6ifconf['nextserver'] <> "") {
952
				$dhcpdv6conf .= "	next-server {$dhcpv6ifconf['nextserver']};\n";
953
			}
954
			if ($dhcpv6ifconf['filename'] <> "") {
955 693833cb Seth Mos
				$dhcpdv6conf .= "	filename \"{$dhcpv6ifconf['filename']}\";\n";
956
			}
957
			if ($dhcpv6ifconf['rootpath'] <> "") {
958
				$dhcpdv6conf .= "	option root-path \"{$dhcpv6ifconf['rootpath']}\";\n";
959
      		}
960
	}
961 107e8acc Ovidiu Predescu
962 693833cb Seth Mos
		$dhcpdv6conf .= <<<EOD
963
}
964 a2578c27 Anthony Wrather
965 693833cb Seth Mos
EOD;
966
967
		/* add static mappings */
968 2fb056d8 Seth Mos
		/* Needs to use DUID */
969 693833cb Seth Mos
		if (is_array($dhcpv6ifconf['staticmap'])) {
970
971
			$i = 0;
972
			foreach ($dhcpv6ifconf['staticmap'] as $sm) {
973
				$dhcpdv6conf .= <<<EOD
974
host s_{$dhcpv6if}_{$i} {
975 2fb056d8 Seth Mos
	host-identifier option dhcp6.client-id {$sm['duid']};
976 693833cb Seth Mos
977
EOD;
978 2fb056d8 Seth Mos
				if ($sm['ipaddrv6'])
979
					$dhcpdv6conf .= "	fixed-address6 {$sm['ipaddrv6']};\n";
980 693833cb Seth Mos
981
				if ($sm['hostname']) {
982
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
983
					$dhhostname = str_replace(".", "_", $dhhostname);
984
					$dhcpdv6conf .= "	option host-name {$dhhostname};\n";
985
				}
986 a2578c27 Anthony Wrather
				if ($sm['filename'])
987
					$dhcpdv6conf .= "	filename \"{$sm['filename']}\";\n";
988
989
				if ($sm['rootpath'])
990
					$dhcpdv6conf .= "	option root-path \"{$sm['rootpath']}\";\n";
991 693833cb Seth Mos
992
				$dhcpdv6conf .= "}\n";
993
				$i++;
994
			}
995
		}
996 107e8acc Ovidiu Predescu
997 fe838158 smos
		if($config['dhcpdv6'][$dhcpv6if]['ramode'] <> "unmanaged") {
998 656f1763 Seth Mos
			$realif = escapeshellcmd(get_real_interface($dhcpv6if));
999
			$dhcpdv6ifs[] = $realif;
1000 d7bccf3c Seth Mos
			exec("/sbin/ifconfig {$realif} |awk  '/ether/ {print $2}'", $mac);
1001
			$v6address = generate_ipv6_from_mac($mac[0]);
1002 656f1763 Seth Mos
			/* Create link local address for bridges */
1003
			if(stristr("$realif", "bridge")) {
1004 d7bccf3c Seth Mos
				mwexec("/sbin/ifconfig {$realif} inet6 {$v6address}");
1005 656f1763 Seth Mos
			}
1006 de140730 Seth Mos
		}
1007 693833cb Seth Mos
	}
1008
1009
	fwrite($fdv6, $dhcpdv6conf);
1010
	fclose($fdv6);
1011
	/* create an empty leases v6 database */
1012
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1013 107e8acc Ovidiu Predescu
1014 a25183c5 Scott Ullrich
1015 68a0e4fc Scott Ullrich
	/* fire up dhcpd in a chroot */
1016 2a1bd027 Seth Mos
	if(count($dhcpdv6ifs) > 0) {
1017 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 " .
1018 2a1bd027 Seth Mos
			join(" ", $dhcpdv6ifs));
1019 d48ed103 smos
		mwexec("/usr/local/sbin/dhcpleases6 -c \"/usr/local/bin/php -f /usr/local/sbin/prefixes.php|/bin/sh\" -l {$g['dhcpd_chroot_path']}/var/db/dhcpd6.leases");
1020 2a1bd027 Seth Mos
	}
1021 d57293a4 Seth Mos
1022 fc6b5a4d Scott Ullrich
	if ($g['booting']) {
1023 f1a44a3a Carlos Eduardo Ramos
		print gettext("done.") . "\n";
1024 5c6d0f65 Colin Smith
	}
1025 a25183c5 Scott Ullrich
1026 5b237745 Scott Ullrich
	return 0;
1027
}
1028
1029 41997fbb Ermal Luci
function services_igmpproxy_configure() {
1030
        global $config, $g;
1031
1032
        /* kill any running igmpproxy */
1033
        killbyname("igmpproxy");
1034
1035
	if (!is_array($config['igmpproxy']['igmpentry']))
1036
		return 1;
1037
1038 f206afb5 Ermal
        $iflist = get_configured_interface_list();
1039
1040 41997fbb Ermal Luci
        $igmpconf = <<<EOD
1041
1042
##------------------------------------------------------
1043
## Enable Quickleave mode (Sends Leave instantly)
1044
##------------------------------------------------------
1045
quickleave
1046
1047
EOD;
1048
1049
        foreach ($config['igmpproxy']['igmpentry'] as $igmpcf) {
1050
                unset($iflist[$igmpcf['ifname']]);
1051
                $realif = get_real_interface($igmpcf['ifname']);
1052
                if (empty($igmpcf['threshold']))
1053
                        $threshld = 1;
1054
                else
1055
                        $threshld = $igmpcf['threshold'];
1056
                $igmpconf .= "phyint {$realif} {$igmpcf['type']} ratelimit 0 threshold {$threshld}\n";
1057
1058
                if ($igmpcf['address'] <> "") {
1059
                        $item = explode(" ", $igmpcf['address']);
1060
                        foreach($item as $iww)
1061
                                $igmpconf .= "altnet {$iww}\n";
1062
                }
1063
                $igmpconf .= "\n";
1064
        }
1065
        foreach ($iflist as $ifn) {
1066
                $realif = get_real_interface($ifn);
1067
                $igmpconf .= "phyint {$realif} disabled\n";
1068
        }
1069 3bae60be Ermal
	$igmpconf .= "\n";
1070 41997fbb Ermal Luci
1071
        $igmpfl = fopen($g['tmp_path'] . "/igmpproxy.conf", "w");
1072
        if (!$igmpfl) {
1073 f1a44a3a Carlos Eduardo Ramos
                log_error(gettext("Could not write Igmpproxy configuration file!"));
1074 41997fbb Ermal Luci
                return;
1075
        }
1076
        fwrite($igmpfl, $igmpconf);
1077
        fclose($igmpfl);
1078
1079
        mwexec("/usr/local/sbin/igmpproxy -c " . $g['tmp_path'] . "/igmpproxy.conf");
1080 d372a32d Chris Buechler
        log_error(gettext("Started IGMP proxy service."));
1081 41997fbb Ermal Luci
1082
        return 0;
1083
}
1084
1085 5b237745 Scott Ullrich
function services_dhcrelay_configure() {
1086 f19d3b7a Scott Ullrich
	global $config, $g;
1087 7734aea6 Andrew Thompson
	if ($g['platform'] == 'jail')
1088
		return;
1089 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1090 acd910bf Scott Ullrich
		$mt = microtime();
1091 f19d3b7a Scott Ullrich
		echo "services_dhcrelay_configure() being called $mt\n";
1092 acd910bf Scott Ullrich
	}
1093 a25183c5 Scott Ullrich
1094 5b237745 Scott Ullrich
	/* kill any running dhcrelay */
1095
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
1096 a25183c5 Scott Ullrich
1097 2f06cc3f Ermal
	$dhcrelaycfg =& $config['dhcrelay'];
1098 a25183c5 Scott Ullrich
1099 5b237745 Scott Ullrich
	/* DHCPRelay enabled on any interfaces? */
1100 2f06cc3f Ermal
	if (!isset($dhcrelaycfg['enable']))
1101 5b237745 Scott Ullrich
		return 0;
1102 a25183c5 Scott Ullrich
1103 5b237745 Scott Ullrich
	if ($g['booting'])
1104 f1a44a3a Carlos Eduardo Ramos
		echo gettext("Starting DHCP relay service...");
1105 5b237745 Scott Ullrich
	else
1106
		sleep(1);
1107 a25183c5 Scott Ullrich
1108 2f06cc3f Ermal
	$iflist = get_configured_interface_list();
1109 a25183c5 Scott Ullrich
1110 2f06cc3f Ermal
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1111
	foreach ($dhcifaces as $dhcrelayif) {
1112
		if (!isset($iflist[$dhcrelayif]) ||
1113
			link_interface_to_bridge($dhcrelayif))
1114 5b237745 Scott Ullrich
			continue;
1115 a25183c5 Scott Ullrich
1116 2f06cc3f Ermal
		if (is_ipaddr(get_interface_ip($dhcrelayif)))
1117
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1118 5b237745 Scott Ullrich
	}
1119
1120 107e8acc Ovidiu Predescu
	/*
1121 2f06cc3f Ermal
	 * In order for the relay to work, it needs to be active
1122
	 * on the interface in which the destination server sits.
1123
	 */
1124
	$srvips = explode(",", $dhcrelaycfg['server']);
1125
	foreach ($srvips as $srcidx => $srvip) {
1126
		unset($destif);
1127
		foreach ($iflist as $ifname) {
1128
			$subnet = get_interface_ip($ifname);
1129
			if (!is_ipaddr($subnet))
1130
				continue;
1131
			$subnet .=  "/" . get_interface_subnet($ifname);
1132
			if (ip_in_subnet($srvip, $subnet)) {
1133
				$destif = get_real_interface($ifname);
1134
				break;
1135
			}
1136 6f9b154b Ermal
		}
1137 2f06cc3f Ermal
		if (!isset($destif)) {
1138 f898c1a9 jim-p
			foreach (get_staticroutes() as $rtent) {
1139
				if (ip_in_subnet($srvip, $rtent['network'])) {
1140
					$a_gateways = return_gateways_array(true);
1141
					$destif = $a_gateways[$rtent['gateway']]['interface'];
1142
					break;
1143 6f9b154b Ermal
				}
1144
			}
1145
		}
1146 a25183c5 Scott Ullrich
1147 2f06cc3f Ermal
		if (!isset($destif)) {
1148
			/* Create a array from the existing route table */
1149
        		exec("/usr/bin/netstat -rnWf inet", $route_str);
1150
        		array_shift($route_str);
1151
        		array_shift($route_str);
1152
        		array_shift($route_str);
1153
        		array_shift($route_str);
1154
        		$route_arr = array();
1155
        		foreach($route_str as $routeline) {
1156
                		$items = preg_split("/[ ]+/i", $routeline);
1157
				if (ip_in_subnet($srvip, $items[0])) {
1158 5e80046a Ermal
					$destif = trim($items[6]);
1159 c935eb77 Ermal
					break;
1160 2f06cc3f Ermal
				}
1161
        		}
1162
		}
1163 107e8acc Ovidiu Predescu
1164 2f06cc3f Ermal
		if (!isset($destif)) {
1165
			if (is_array($config['gateways']['gateway_item'])) {
1166
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1167
					if (isset($gateway['defaultgw'])) {
1168
						$a_gateways = return_gateways_array(true);
1169
                                        	$destif = $a_gateways[$rtent['gateway']]['interface'];
1170
						break;
1171 107e8acc Ovidiu Predescu
					}
1172 2f06cc3f Ermal
				}
1173
			} else
1174
				$destif = get_real_interface("wan");
1175
		}
1176 a25183c5 Scott Ullrich
1177 24997966 Ermal
		if (!empty($destif))
1178
			$dhcrelayifs[] = $destif;
1179 2f06cc3f Ermal
	}
1180 5b237745 Scott Ullrich
	$dhcrelayifs = array_unique($dhcrelayifs);
1181
1182
	/* fire up dhcrelay */
1183 24997966 Ermal
	if (empty($dhcrelayifs)) {
1184 5a171fb7 Warren Baker
		log_error("No suitable interface found for running dhcrelay!");
1185 24997966 Ermal
		return; /* XXX */
1186
	}
1187
1188
	$cmd = "/usr/local/sbin/dhcrelay -i " .  implode(" -i ", $dhcrelayifs);
1189 5b237745 Scott Ullrich
1190 a25183c5 Scott Ullrich
	if (isset($dhcrelaycfg['agentoption']))
1191 5b237745 Scott Ullrich
		$cmd .=  " -a -m replace";
1192
1193 2f06cc3f Ermal
	$cmd .= " " . implode(" ", $srvips);
1194 5b237745 Scott Ullrich
	mwexec($cmd);
1195 a25183c5 Scott Ullrich
1196 5b237745 Scott Ullrich
	return 0;
1197
}
1198
1199 b7a15cf8 Seth Mos
function services_dhcrelay6_configure() {
1200
	global $config, $g;
1201
	if ($g['platform'] == 'jail')
1202
		return;
1203
	if(isset($config['system']['developerspew'])) {
1204
		$mt = microtime();
1205
		echo "services_dhcrelay_configure() being called $mt\n";
1206
	}
1207
1208
	/* kill any running dhcrelay */
1209
	killbypid("{$g['varrun_path']}/dhcrelay6.pid");
1210
1211
	$dhcrelaycfg =& $config['dhcrelay6'];
1212
1213
	/* DHCPv6 Relay enabled on any interfaces? */
1214
	if (!isset($dhcrelaycfg['enable']))
1215
		return 0;
1216
1217
	if ($g['booting'])
1218
		echo gettext("Starting DHCPv6 relay service...");
1219
	else
1220
		sleep(1);
1221
1222
	$iflist = get_configured_interface_list();
1223
1224
	$dhcifaces = explode(",", $dhcrelaycfg['interface']);
1225
	foreach ($dhcifaces as $dhcrelayif) {
1226
		if (!isset($iflist[$dhcrelayif]) ||
1227
			link_interface_to_bridge($dhcrelayif))
1228
			continue;
1229
1230
		if (is_ipaddrv6(get_interface_ipv6($dhcrelayif)))
1231
			$dhcrelayifs[] = get_real_interface($dhcrelayif);
1232
	}
1233
1234 107e8acc Ovidiu Predescu
	/*
1235 b7a15cf8 Seth Mos
	 * In order for the relay to work, it needs to be active
1236
	 * on the interface in which the destination server sits.
1237
	 */
1238
	$srvips = explode(",", $dhcrelaycfg['server']);
1239
	foreach ($srvips as $srcidx => $srvip) {
1240
		unset($destif);
1241
		foreach ($iflist as $ifname) {
1242
			$subnet = get_interface_ipv6($ifname);
1243
			if (!is_ipaddrv6($subnet))
1244
				continue;
1245
			$subnet .=  "/" . get_interface_subnetv6($ifname);
1246
			if (ip_in_subnet($srvip, $subnet)) {
1247
				$destif = get_real_interface($ifname);
1248
				break;
1249
			}
1250
		}
1251
		if (!isset($destif)) {
1252
			if (is_array($config['staticroutes']['route'])) {
1253
				foreach ($config['staticroutes']['route'] as $rtent) {
1254
					if (ip_in_subnet($srvip, $rtent['network'])) {
1255
						$a_gateways = return_gateways_array(true);
1256
						$destif = $a_gateways[$rtent['gateway']]['interface'];
1257
						break;
1258
					}
1259
				}
1260
			}
1261
		}
1262
1263
		if (!isset($destif)) {
1264
			/* Create a array from the existing route table */
1265
        		exec("/usr/bin/netstat -rnWf inet6", $route_str);
1266
        		array_shift($route_str);
1267
        		array_shift($route_str);
1268
        		array_shift($route_str);
1269
        		array_shift($route_str);
1270
        		$route_arr = array();
1271
        		foreach($route_str as $routeline) {
1272
                		$items = preg_split("/[ ]+/i", $routeline);
1273
				if (ip_in_subnet($srvip, $items[0])) {
1274
					$destif = trim($items[6]);
1275
					break;
1276
				}
1277
        		}
1278
		}
1279 107e8acc Ovidiu Predescu
1280 b7a15cf8 Seth Mos
		if (!isset($destif)) {
1281
			if (is_array($config['gateways']['gateway_item'])) {
1282
				foreach ($config['gateways']['gateway_item'] as $gateway) {
1283
					if (isset($gateway['defaultgw'])) {
1284
						$a_gateways = return_gateways_array(true);
1285
                                        	$destif = $a_gateways[$rtent['gateway']]['interface'];
1286
						break;
1287 107e8acc Ovidiu Predescu
					}
1288 b7a15cf8 Seth Mos
				}
1289
			} else
1290
				$destif = get_real_interface("wan");
1291
		}
1292
1293
		if (!empty($destif))
1294
			$dhcrelayifs[] = $destif;
1295
	}
1296
	$dhcrelayifs = array_unique($dhcrelayifs);
1297
1298
	/* fire up dhcrelay */
1299
	if (empty($dhcrelayifs)) {
1300
		log_error("No suitable interface found for running dhcrelay -6!");
1301
		return; /* XXX */
1302
	}
1303
1304 71282744 Seth Mos
	$cmd = "/usr/local/sbin/dhcrelay -6 -pf \"{$g['varetc_path']}/dhcrelay6.pid\" -i " .  implode(" -i ", $dhcrelayifs);
1305 b7a15cf8 Seth Mos
1306
	if (isset($dhcrelaycfg['agentoption']))
1307
		$cmd .=  " -a -m replace";
1308
1309
	$cmd .= " " . implode(" ", $srvips);
1310
	mwexec($cmd);
1311
1312
	return 0;
1313
}
1314
1315 181d7c95 Ermal Luçi
function services_dyndns_configure_client($conf) {
1316
1317 d2946062 Ermal
	if (!isset($conf['enable']))
1318 65996399 Ermal
		return;
1319 d2946062 Ermal
1320 181d7c95 Ermal Luçi
	/* load up the dyndns.class */
1321
	require_once("dyndns.class");
1322
1323
	$dns = new updatedns($dnsService = $conf['type'],
1324
		$dnsHost = $conf['host'],
1325
		$dnsUser = $conf['username'],
1326
		$dnsPass = $conf['password'],
1327
		$dnsWilcard = $conf['wildcard'],
1328 107e8acc Ovidiu Predescu
		$dnsMX = $conf['mx'],
1329 f3b2b2a4 Yehuda Katz
		$dnsIf = "{$conf['interface']}",
1330
		$dnsBackMX = NULL,
1331
		$dnsServer = NULL,
1332
		$dnsPort = NULL,
1333 37f3e704 Matt Corallo
		$dnsUpdateURL = "{$conf['updateurl']}",
1334 cd132e86 Edson Brandi
		$forceUpdate = $conf['force'],
1335
                $dnsZoneID=$conf['zoneid'],
1336 37f3e704 Matt Corallo
                $dnsTTL=$conf['ttl'],
1337
		$dnsResultMatch = "{$conf['resultmatch']}",
1338
		$dnsRequestIf = "{$conf['requestif']}",
1339
		$dnsID = "{$conf['id']}");
1340 181d7c95 Ermal Luçi
}
1341
1342 0be93267 Ermal Lu?i
function services_dyndns_configure($int = "") {
1343 f19d3b7a Scott Ullrich
	global $config, $g;
1344 59a63553 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1345
		$mt = microtime();
1346
		echo "services_dyndns_configure() being called $mt\n";
1347
	}
1348
1349 67ee1ec5 Ermal Luçi
	$dyndnscfg = $config['dyndnses']['dyndns'];
1350
1351
	if (is_array($dyndnscfg)) {
1352 107e8acc Ovidiu Predescu
		if ($g['booting'])
1353 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DynDNS clients...");
1354 181d7c95 Ermal Luçi
1355 67ee1ec5 Ermal Luçi
		foreach ($dyndnscfg as $dyndns) {
1356 0be93267 Ermal Lu?i
			if (!empty($int) && $int != $dyndns['interface'])
1357
				continue;
1358
1359 181d7c95 Ermal Luçi
			services_dyndns_configure_client($dyndns);
1360 67ee1ec5 Ermal Luçi
1361 181d7c95 Ermal Luçi
			sleep(1);
1362 67ee1ec5 Ermal Luçi
		}
1363 59a63553 Scott Ullrich
1364
		if ($g['booting'])
1365 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1366 59a63553 Scott Ullrich
	}
1367
1368
	return 0;
1369
}
1370
1371 5b237745 Scott Ullrich
function services_dnsmasq_configure() {
1372 f19d3b7a Scott Ullrich
	global $config, $g;
1373 6a01ea44 Bill Marquette
	$return = 0;
1374 107e8acc Ovidiu Predescu
1375 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1376 acd910bf Scott Ullrich
		$mt = microtime();
1377 f19d3b7a Scott Ullrich
		echo "services_dnsmasq_configure() being called $mt\n";
1378 acd910bf Scott Ullrich
	}
1379
1380 5b237745 Scott Ullrich
	/* kill any running dnsmasq */
1381 d224df18 Ermal
	if (file_exists("{$g['varrun_path']}/dnsmasq.pid"))
1382
		sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
1383 5b237745 Scott Ullrich
1384
	if (isset($config['dnsmasq']['enable'])) {
1385 a25183c5 Scott Ullrich
1386 5b237745 Scott Ullrich
		if ($g['booting'])
1387 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting DNS forwarder...");
1388 5b237745 Scott Ullrich
		else
1389
			sleep(1);
1390
1391
		/* generate hosts file */
1392 6a01ea44 Bill Marquette
		if(system_hosts_generate()!=0)
1393
			$return = 1;
1394 a25183c5 Scott Ullrich
1395 5b237745 Scott Ullrich
		$args = "";
1396 a25183c5 Scott Ullrich
1397 0261381a Ermal
		if (isset($config['dnsmasq']['regdhcp'])) {
1398
			$args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts ";
1399
		}
1400 107e8acc Ovidiu Predescu
1401 2c46f11f Scott Ullrich
		/* Setup forwarded domains */
1402 739b97d9 Scott Ullrich
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
1403
			foreach($config['dnsmasq']['domainoverrides'] as $override) {
1404
			        $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
1405
			}
1406
		}
1407 0c2b5df7 Scott Ullrich
1408 2c46f11f Scott Ullrich
		/* Allow DNS Rebind for forwarded domains */
1409 29721fe6 Scott Ullrich
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
1410 30d20e7d Scott Ullrich
			if(!isset($config['system']['webgui']['nodnsrebindcheck'])) {
1411
				foreach($config['dnsmasq']['domainoverrides'] as $override) {
1412
					$args .= ' --rebind-domain-ok=/' . $override['domain'] . '/ ';
1413
				}
1414 2c46f11f Scott Ullrich
			}
1415
		}
1416 91adc5c1 Scott Ullrich
1417 30d20e7d Scott Ullrich
		if(!isset($config['system']['webgui']['nodnsrebindcheck']))
1418
			$dns_rebind = "--rebind-localhost-ok --stop-dns-rebind";
1419
1420 8f9bffbc Andrew Thompson
		if ($config['dnsmasq']['custom_options']) {
1421
			foreach (preg_split('/\s+/', $config['dnsmasq']['custom_options']) as $c)
1422 3d749ab3 Andrew Thompson
				$args .= " --$c";
1423 8f9bffbc Andrew Thompson
		}
1424
1425 5b237745 Scott Ullrich
		/* run dnsmasq */
1426 923d15bf Warren Baker
		mwexec_bg("/usr/local/sbin/dnsmasq --local-ttl 1 --all-servers {$dns_rebind} --dns-forward-max=5000 --cache-size=10000 {$args}");
1427 5b237745 Scott Ullrich
1428
		if ($g['booting'])
1429 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1430 5b237745 Scott Ullrich
	}
1431 a25183c5 Scott Ullrich
1432 5b237745 Scott Ullrich
	if (!$g['booting']) {
1433 6a01ea44 Bill Marquette
		if(services_dhcpd_configure()!=0)
1434
			$return = 1;
1435 5b237745 Scott Ullrich
	}
1436
1437 6a01ea44 Bill Marquette
	return $return;
1438 5b237745 Scott Ullrich
}
1439
1440 8ff4e4d4 Warren Baker
function services_unbound_configure() {
1441
	global $config, $g;
1442
	$return = 0;
1443
1444
	if(isset($config['system']['developerspew'])) {
1445
		$mt = microtime();
1446
		echo "services_unbound_configure() being called $mt\n";
1447
	}
1448
1449
	/* kill any running unbound */
1450
	sigkillbypid("{$g['varrun_path']}/unbound.pid", "TERM");
1451
1452
	if (isset($config['unbound']['enable'])) {
1453
1454
		if ($g['booting'])
1455
			echo "Starting Unbound DNS...";
1456
		else
1457
			sleep(1);
1458
1459 27bb6557 Warren Baker
		/* Setup Unbound DHCP Chroot environment */
1460
		$fd = fopen("{$g['tmp_path']}/unbound.sh","w");
1461
		$status = `/sbin/mount | /usr/bin/grep "{$g['unbound_chroot_path']}/dev"`;
1462
		fwrite($fd, "mkdir -p {$g['unbound_chroot_path']}\n");
1463
		fwrite($fd, "mkdir -p {$g['unbound_chroot_path']}/dev\n");
1464
		fwrite($fd, "mkdir -p {$g['unbound_chroot_path']}/etc\n");
1465
		if(!trim($status))
1466
			fwrite($fd, "mount -t devfs devfs {$g['unbound_chroot_path']}/dev\n");
1467
		fclose($fd);
1468
		mwexec("/bin/sh {$g['tmp_path']}/unbound.sh");
1469
1470 8ff4e4d4 Warren Baker
		/* generate Unbound config file */
1471
		if(unbound_generate_config()!=0) {
1472
			log_error("Problem generating Unbound configuration.");
1473
			$return = 1;
1474
		}
1475
1476
		/* run Unbound */
1477 3f2e3bcf Warren Baker
		mwexec("/usr/local/sbin/unbound -c {$g['unbound_chroot_path']}/etc/unbound.conf");
1478 8ff4e4d4 Warren Baker
1479
		if ($g['booting'])
1480
			echo "done.\n";
1481
	}
1482
1483
	return $return;
1484
}
1485
1486 5b237745 Scott Ullrich
function services_snmpd_configure() {
1487 f19d3b7a Scott Ullrich
	global $config, $g;
1488 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1489 acd910bf Scott Ullrich
		$mt = microtime();
1490 f19d3b7a Scott Ullrich
		echo "services_snmpd_configure() being called $mt\n";
1491
	}
1492 5b237745 Scott Ullrich
1493
	/* kill any running snmpd */
1494
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
1495 dd18038e Ermal
	sleep(2);
1496 107e8acc Ovidiu Predescu
	if(is_process_running("bsnmpd"))
1497 a976fa82 Scott Ullrich
		mwexec("/usr/bin/killall bsnmpd", true);
1498 5b237745 Scott Ullrich
1499
	if (isset($config['snmpd']['enable'])) {
1500 a25183c5 Scott Ullrich
1501 5b237745 Scott Ullrich
		if ($g['booting'])
1502 f1a44a3a Carlos Eduardo Ramos
			echo gettext("Starting SNMP daemon... ");
1503 5b237745 Scott Ullrich
1504
		/* generate snmpd.conf */
1505
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
1506
		if (!$fd) {
1507 f1a44a3a Carlos Eduardo Ramos
			printf(gettext("Error: cannot open snmpd.conf in services_snmpd_configure().%s"),"\n");
1508 5b237745 Scott Ullrich
			return 1;
1509
		}
1510 a25183c5 Scott Ullrich
1511 142da8f7 John Fleming
1512 5b237745 Scott Ullrich
		$snmpdconf = <<<EOD
1513 d47a8a69 Scott Ullrich
location := "{$config['snmpd']['syslocation']}"
1514
contact := "{$config['snmpd']['syscontact']}"
1515
read := "{$config['snmpd']['rocommunity']}"
1516 142da8f7 John Fleming
1517
EOD;
1518
1519
/* No docs on what write strings do there for disable for now.
1520
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
1521
		    $snmpdconf .= <<<EOD
1522
# write string
1523
write := "{$config['snmpd']['rwcommunity']}"
1524
1525
EOD;
1526
		}
1527
*/
1528
1529
1530
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
1531
		    $snmpdconf .= <<<EOD
1532
# SNMP Trap support.
1533 dbeeb008 John Fleming
traphost := {$config['snmpd']['trapserver']}
1534
trapport := {$config['snmpd']['trapserverport']}
1535
trap := "{$config['snmpd']['trapstring']}"
1536 142da8f7 John Fleming
1537
1538
EOD;
1539
		}
1540
1541
1542
		$snmpdconf .= <<<EOD
1543 d47a8a69 Scott Ullrich
system := 1     # pfSense
1544
%snmpd
1545
begemotSnmpdDebugDumpPdus       = 2
1546
begemotSnmpdDebugSyslogPri      = 7
1547
begemotSnmpdCommunityString.0.1 = $(read)
1548 142da8f7 John Fleming
1549
EOD;
1550
1551
/* No docs on what write strings do there for disable for now.
1552
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
1553
		    $snmpdconf .= <<<EOD
1554
begemotSnmpdCommunityString.0.2 = $(write)
1555
1556
EOD;
1557
		}
1558
*/
1559
1560 c7f44ae0 Scott Ullrich
1561 142da8f7 John Fleming
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
1562
		    $snmpdconf .= <<<EOD
1563
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
1564
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
1565
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
1566
1567
EOD;
1568
		}
1569
1570
1571
		$snmpdconf .= <<<EOD
1572 d47a8a69 Scott Ullrich
begemotSnmpdCommunityDisable    = 1
1573 03ba7a0f John Fleming
1574
EOD;
1575
1576 de5aec97 jim-p
		if (isset($config['snmpd']['bindlan'])) {
1577
			$config['snmpd']['bindip'] = 'lan';
1578
			unset($config['snmpd']['bindlan']);
1579
		}
1580 c82b2c3f jim-p
		$bind_to_ip = "0.0.0.0";
1581
		if(isset($config['snmpd']['bindip'])) {
1582
			if (is_ipaddr($config['snmpd']['bindip'])) {
1583
				$bind_to_ip = $config['snmpd']['bindip'];
1584
			} else {
1585
				$if = get_real_interface($config['snmpd']['bindip']);
1586
				if (does_interface_exist($if))
1587
					$bind_to_ip = find_interface_ip($if);
1588
			}
1589 7cbad422 Scott Ullrich
		}
1590
1591 03ba7a0f John Fleming
		if(is_port( $config['snmpd']['pollport'] )) {
1592
		    $snmpdconf .= <<<EOD
1593 7cbad422 Scott Ullrich
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
1594 03ba7a0f John Fleming
1595
EOD;
1596
1597
		}
1598
1599
		$snmpdconf .= <<<EOD
1600 d47a8a69 Scott Ullrich
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
1601
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
1602 142da8f7 John Fleming
1603 03ba7a0f John Fleming
# These are bsnmp macros not php vars.
1604 9cc8c59e Scott Ullrich
sysContact      = $(contact)
1605
sysLocation     = $(location)
1606
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
1607 142da8f7 John Fleming
1608 d47a8a69 Scott Ullrich
snmpEnableAuthenTraps = 2
1609 03ba7a0f John Fleming
1610
EOD;
1611
1612
		if (is_array( $config['snmpd']['modules'] )) {
1613
		    if(isset($config['snmpd']['modules']['mibii'])) {
1614
			$snmpdconf .= <<<EOD
1615 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
1616 03ba7a0f John Fleming
1617
EOD;
1618
		    }
1619
1620
		    if(isset($config['snmpd']['modules']['netgraph'])) {
1621
			$snmpdconf .= <<<EOD
1622 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
1623
%netgraph
1624
begemotNgControlNodeName = "snmpd"
1625 03ba7a0f John Fleming
1626
EOD;
1627
		    }
1628
1629
		    if(isset($config['snmpd']['modules']['pf'])) {
1630
			$snmpdconf .= <<<EOD
1631 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
1632 95fb49e8 Seth Mos
1633
EOD;
1634
		    }
1635
1636
		    if(isset($config['snmpd']['modules']['hostres'])) {
1637
			$snmpdconf .= <<<EOD
1638
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
1639
1640
EOD;
1641
		    }
1642
		    if(isset($config['snmpd']['modules']['bridge'])) {
1643
			$snmpdconf .= <<<EOD
1644
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
1645 d47a8a69 Scott Ullrich
# config must end with blank line
1646 5b237745 Scott Ullrich
1647 03ba7a0f John Fleming
1648 5b237745 Scott Ullrich
EOD;
1649 03ba7a0f John Fleming
		    }
1650
		}
1651 5b237745 Scott Ullrich
1652
		fwrite($fd, $snmpdconf);
1653
		fclose($fd);
1654
1655 7cbad422 Scott Ullrich
		if (isset($config['snmpd']['bindlan'])) {
1656
			$bindlan = "";
1657
		}
1658
1659 853e003a Scott Ullrich
		/* run bsnmpd */
1660
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
1661 7cbad422 Scott Ullrich
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
1662 5b237745 Scott Ullrich
1663
		if ($g['booting'])
1664 f1a44a3a Carlos Eduardo Ramos
			echo gettext("done.") . "\n";
1665 5b237745 Scott Ullrich
	}
1666
1667
	return 0;
1668
}
1669
1670 2ec2a374 Ermal Lu?i
function services_dnsupdate_process($int = "") {
1671 f19d3b7a Scott Ullrich
	global $config, $g;
1672 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1673 acd910bf Scott Ullrich
		$mt = microtime();
1674 f19d3b7a Scott Ullrich
		echo "services_dnsupdate_process() being called $mt\n";
1675 acd910bf Scott Ullrich
	}
1676 f19d3b7a Scott Ullrich
1677 a23d7248 Scott Ullrich
	/* Dynamic DNS updating active? */
1678 67ee1ec5 Ermal Luçi
	if (is_array($config['dnsupdates']['dnsupdate'])) {
1679
		foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
1680 26586f7a Ermal Lu?i
			if (!isset($dnsupdate['enable']))
1681 2ec2a374 Ermal Lu?i
				continue;
1682 26586f7a Ermal Lu?i
			if (!empty($int) && $int != $dnsupdate['interface'])
1683 67ee1ec5 Ermal Luçi
				continue;
1684
1685 2ec2a374 Ermal Lu?i
			/* determine interface name */
1686 26586f7a Ermal Lu?i
			$if = get_real_interface($dnsupdate['interface']);
1687
			$wanip = get_interface_ip($dnsupdate['interface']);
1688 67ee1ec5 Ermal Luçi
			if ($wanip) {
1689
1690
				$keyname = $dnsupdate['keyname'];
1691
				/* trailing dot */
1692
				if (substr($keyname, -1) != ".")
1693
					$keyname .= ".";
1694
1695
				$hostname = $dnsupdate['host'];
1696
				/* trailing dot */
1697
				if (substr($hostname, -1) != ".")
1698
					$hostname .= ".";
1699
1700
				/* write private key file
1701
				   this is dumb - public and private keys are the same for HMAC-MD5,
1702
				   but nsupdate insists on having both */
1703
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
1704 8ec0a8bc jim-p
				$privkey = <<<EOD
1705 a23d7248 Scott Ullrich
Private-key-format: v1.2
1706
Algorithm: 157 (HMAC)
1707 67ee1ec5 Ermal Luçi
Key: {$dnsupdate['keydata']}
1708 a23d7248 Scott Ullrich
1709
EOD;
1710 67ee1ec5 Ermal Luçi
				fwrite($fd, $privkey);
1711
				fclose($fd);
1712
1713
				/* write public key file */
1714
				if ($dnsupdate['keytype'] == "zone") {
1715
					$flags = 257;
1716
					$proto = 3;
1717
				} else if ($dnsupdate['keytype'] == "host") {
1718
					$flags = 513;
1719
					$proto = 3;
1720
				} else if ($dnsupdate['keytype'] == "user") {
1721
					$flags = 0;
1722
					$proto = 2;
1723
				}
1724 c7f44ae0 Scott Ullrich
1725 26586f7a Ermal Lu?i
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
1726 67ee1ec5 Ermal Luçi
				fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
1727
				fclose($fd);
1728
1729
				/* generate update instructions */
1730
				$upinst = "";
1731
				if (!empty($dnsupdate['server']))
1732
					$upinst .= "server {$dnsupdate['server']}\n";
1733
				$upinst .= "update delete {$dnsupdate['host']} A\n";
1734
				$upinst .= "update add {$dnsupdate['host']} {$dnsupdate['ttl']} A {$wanip}\n";
1735
				$upinst .= "\n";	/* mind that trailing newline! */
1736
1737
				$fd = fopen("{$g['varetc_path']}/nsupdatecmds{$i}", "w");
1738
				fwrite($fd, $upinst);
1739
				fclose($fd);
1740
1741
				/* invoke nsupdate */
1742 f6d0ad0f Chris Buechler
				$cmd = "/usr/bin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
1743 67ee1ec5 Ermal Luçi
				if (isset($dnsupdate['usetcp']))
1744
					$cmd .= " -v";
1745
				$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
1746 107e8acc Ovidiu Predescu
1747 67ee1ec5 Ermal Luçi
				mwexec_bg($cmd);
1748
			}
1749 a23d7248 Scott Ullrich
		}
1750
	}
1751 c7f44ae0 Scott Ullrich
1752 a23d7248 Scott Ullrich
	return 0;
1753 5b237745 Scott Ullrich
}
1754
1755 ac809adb Scott Ullrich
function setup_wireless_olsr() {
1756 f19d3b7a Scott Ullrich
	global $config, $g;
1757 7734aea6 Andrew Thompson
	if ($g['platform'] == 'jail' || !$config['installedpackages']['olsrd'] || !$config['installedpackages'])
1758 c7f44ae0 Scott Ullrich
		return;
1759 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
1760 acd910bf Scott Ullrich
		$mt = microtime();
1761 f19d3b7a Scott Ullrich
		echo "setup_wireless_olsr($interface) being called $mt\n";
1762 acd910bf Scott Ullrich
	}
1763 eebeaf0d Scott Ullrich
	conf_mount_rw();
1764 78e07da7 Scott Ullrich
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
1765 8fb40409 Scott Ullrich
		$olsr_enable = $olsrd['enable'];
1766 7cca77de Ermal
		if($olsr_enable <> "on") {
1767
			if (is_process_running("olsrd"))
1768
				mwexec("/usr/bin/killall olsrd", true);
1769 c7f44ae0 Scott Ullrich
			return;
1770 7cca77de Ermal
		}
1771 ac809adb Scott Ullrich
		$fd = fopen("{$g['varetc_path']}/olsr.conf", "w");
1772 48ab0cd2 Scott Ullrich
1773 dad93b75 Scott Ullrich
		if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") {
1774
			$enableannounce .= "\nHna4\n";
1775
			$enableannounce .= "{\n";
1776 bdd25a82 Scott Ullrich
		if($olsrd['announcedynamicroute'])
1777
			$enableannounce .= "\t{$olsrd['announcedynamicroute']}\n";
1778
		if($olsrd['enableannounce'] == "on")
1779
			$enableannounce .= "0.0.0.0 0.0.0.0";
1780 dad93b75 Scott Ullrich
			$enableannounce .= "\n}\n";
1781
		} else {
1782 48ab0cd2 Scott Ullrich
			$enableannounce = "";
1783 dad93b75 Scott Ullrich
		}
1784 48ab0cd2 Scott Ullrich
1785 db5fbdb7 Scott Ullrich
		$olsr .= <<<EODA
1786 80ce93c6 Scott Ullrich
#
1787
# olsr.org OLSR daemon config file
1788
#
1789
# Lines starting with a # are discarded
1790
#
1791 ac809adb Scott Ullrich
# This file was generated by setup_wireless_olsr() in services.inc
1792 80ce93c6 Scott Ullrich
#
1793
1794 6d0433a5 Scott Ullrich
# This file is an example of a typical
1795
# configuration for a mostly static
1796
# network(regarding mobility) using
1797
# the LQ extention
1798
1799 80ce93c6 Scott Ullrich
# Debug level(0-9)
1800
# If set to 0 the daemon runs in the background
1801
1802 6d0433a5 Scott Ullrich
DebugLevel	2
1803 80ce93c6 Scott Ullrich
1804
# IP version to use (4 or 6)
1805
1806 6d0433a5 Scott Ullrich
IpVersion	4
1807 80ce93c6 Scott Ullrich
1808
# Clear the screen each time the internal state changes
1809
1810
ClearScreen     yes
1811
1812 fa4a6253 Scott Ullrich
{$enableannounce}
1813 80ce93c6 Scott Ullrich
1814
# Should olsrd keep on running even if there are
1815
# no interfaces available? This is a good idea
1816
# for a PCMCIA/USB hotswap environment.
1817
# "yes" OR "no"
1818
1819 6d0433a5 Scott Ullrich
AllowNoInt	yes
1820 80ce93c6 Scott Ullrich
1821
# TOS(type of service) value for
1822
# the IP header of control traffic.
1823
# If not set it will default to 16
1824
1825 6d0433a5 Scott Ullrich
#TosValue	16
1826 80ce93c6 Scott Ullrich
1827
# The fixed willingness to use(0-7)
1828
# If not set willingness will be calculated
1829
# dynamically based on battery/power status
1830
# if such information is available
1831
1832 6d0433a5 Scott Ullrich
#Willingness    	4
1833 80ce93c6 Scott Ullrich
1834
# Allow processes like the GUI front-end
1835
# to connect to the daemon.
1836
1837
IpcConnect
1838
{
1839
     # Determines how many simultaneously
1840
     # IPC connections that will be allowed
1841
     # Setting this to 0 disables IPC
1842
1843
     MaxConnections  0
1844
1845
     # By default only 127.0.0.1 is allowed
1846
     # to connect. Here allowed hosts can
1847
     # be added
1848
1849
     Host            127.0.0.1
1850
     #Host            10.0.0.5
1851
1852 c7f44ae0 Scott Ullrich
     # You can also specify entire net-ranges
1853 80ce93c6 Scott Ullrich
     # that are allowed to connect. Multiple
1854
     # entries are allowed
1855
1856 c7f44ae0 Scott Ullrich
     #Net             192.168.1.0 255.255.255.0
1857 80ce93c6 Scott Ullrich
}
1858
1859
# Wether to use hysteresis or not
1860
# Hysteresis adds more robustness to the
1861
# link sensing but delays neighbor registration.
1862
# Used by default. 'yes' or 'no'
1863
1864 6d0433a5 Scott Ullrich
UseHysteresis	no
1865 80ce93c6 Scott Ullrich
1866
# Hysteresis parameters
1867 c7f44ae0 Scott Ullrich
# Do not alter these unless you know
1868 80ce93c6 Scott Ullrich
# what you are doing!
1869
# Set to auto by default. Allowed
1870
# values are floating point values
1871
# in the interval 0,1
1872
# THR_LOW must always be lower than
1873
# THR_HIGH.
1874
1875 6d0433a5 Scott Ullrich
#HystScaling	0.50
1876
#HystThrHigh	0.80
1877
#HystThrLow	0.30
1878 80ce93c6 Scott Ullrich
1879
1880
# Link quality level
1881
# 0 = do not use link quality
1882
# 1 = use link quality for MPR selection
1883
# 2 = use link quality for MPR selection and routing
1884
# Defaults to 0
1885
1886 11bc553c Scott Ullrich
LinkQualityLevel	{$olsrd['enablelqe']}
1887 80ce93c6 Scott Ullrich
1888
# Link quality window size
1889
# Defaults to 10
1890
1891 6d0433a5 Scott Ullrich
LinkQualityWinSize	10
1892 80ce93c6 Scott Ullrich
1893 c7f44ae0 Scott Ullrich
# Polling rate in seconds(float).
1894 80ce93c6 Scott Ullrich
# Default value 0.05 sec
1895
1896 6d0433a5 Scott Ullrich
Pollrate	0.05
1897 80ce93c6 Scott Ullrich
1898
1899
# TC redundancy
1900
# Specifies how much neighbor info should
1901
# be sent in TC messages
1902
# Possible values are:
1903
# 0 - only send MPR selectors
1904
# 1 - send MPR selectors and MPRs
1905
# 2 - send all neighbors
1906
#
1907
# defaults to 0
1908
1909 6d0433a5 Scott Ullrich
TcRedundancy	2
1910 80ce93c6 Scott Ullrich
1911
#
1912
# MPR coverage
1913
# Specifies how many MPRs a node should
1914
# try select to reach every 2 hop neighbor
1915
#
1916
# Can be set to any integer >0
1917
#
1918
# defaults to 1
1919
1920 6d0433a5 Scott Ullrich
MprCoverage	3
1921 80ce93c6 Scott Ullrich
1922 db5fbdb7 Scott Ullrich
# Example plugin entry with parameters:
1923 80ce93c6 Scott Ullrich
1924 db5fbdb7 Scott Ullrich
EODA;
1925 80ce93c6 Scott Ullrich
1926 1a2369f4 Scott Ullrich
if($olsrd['enablehttpinfo'] == "on") {
1927 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODB
1928
1929
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1930
{
1931 78e07da7 Scott Ullrich
    PlParam     "port"   "{$olsrd['port']}"
1932
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1933 db5fbdb7 Scott Ullrich
}
1934 80ce93c6 Scott Ullrich
1935 db5fbdb7 Scott Ullrich
EODB;
1936
1937
}
1938
1939 aac0e00c Scott Ullrich
if($olsrd['enabledsecure'] == "on") {
1940 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODC
1941
1942 e2894d7f Scott Ullrich
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1943 db5fbdb7 Scott Ullrich
{
1944
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1945
}
1946 80ce93c6 Scott Ullrich
1947 db5fbdb7 Scott Ullrich
EODC;
1948
1949
}
1950
1951 1a2369f4 Scott Ullrich
if($olsrd['enabledyngw'] == "on") {
1952 10c6e7a8 Scott Ullrich
1953
	/* unset default route, olsr auto negotiates */
1954
	mwexec("/sbin/route delete default");
1955 c7f44ae0 Scott Ullrich
1956 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODE
1957
1958
LoadPlugin "/usr/local/lib/olsrd_dyn_gw.so.0.4"
1959
{
1960
    # how often to look for a inet gw, in seconds
1961
    # defaults to 5 secs, if commented out
1962 78e07da7 Scott Ullrich
    PlParam     "Interval"   "{$olsrd['polling']}"
1963 80ce93c6 Scott Ullrich
1964
    # if one or more IPv4 addresses are given, do a ping on these in
1965
    # descending order to validate that there is not only an entry in
1966
    # routing table, but also a real internet connection. If any of
1967
    # these addresses could be pinged successfully, the test was
1968
    # succesful, i.e. if the ping on the 1st address was successful,the
1969
    # 2nd won't be pinged
1970 78e07da7 Scott Ullrich
    PlParam     "Ping"       "{$olsrd['ping']}"
1971 db5fbdb7 Scott Ullrich
    #PlParam     "HNA"   "192.168.81.0 255.255.255.0"
1972
}
1973 80ce93c6 Scott Ullrich
1974 db5fbdb7 Scott Ullrich
EODE;
1975 80ce93c6 Scott Ullrich
1976 db5fbdb7 Scott Ullrich
}
1977 80ce93c6 Scott Ullrich
1978 a4904847 Scott Ullrich
foreach($config['installedpackages']['olsrd']['config'] as $conf) {
1979 c7f44ae0 Scott Ullrich
	$interfaces = explode(',', $conf['iface_array']);
1980
	foreach($interfaces as $interface) {
1981 015b7184 Scott Ullrich
		$realinterface = convert_friendly_interface_to_real_interface_name($interface);
1982 c7f44ae0 Scott Ullrich
$olsr .= <<<EODAD
1983
Interface "{$realinterface}"
1984 80ce93c6 Scott Ullrich
{
1985
1986
    # Hello interval in seconds(float)
1987 6d0433a5 Scott Ullrich
    HelloInterval    2.0
1988 80ce93c6 Scott Ullrich
1989
    # HELLO validity time
1990 6d0433a5 Scott Ullrich
    HelloValidityTime	20.0
1991 80ce93c6 Scott Ullrich
1992
    # TC interval in seconds(float)
1993 6d0433a5 Scott Ullrich
    TcInterval        5.0
1994 80ce93c6 Scott Ullrich
1995
    # TC validity time
1996 6d0433a5 Scott Ullrich
    TcValidityTime	30.0
1997 80ce93c6 Scott Ullrich
1998
    # MID interval in seconds(float)
1999 6d0433a5 Scott Ullrich
    MidInterval	5.0
2000 80ce93c6 Scott Ullrich
2001
    # MID validity time
2002 6d0433a5 Scott Ullrich
    MidValidityTime	30.0
2003 80ce93c6 Scott Ullrich
2004
    # HNA interval in seconds(float)
2005 6d0433a5 Scott Ullrich
    HnaInterval	5.0
2006 80ce93c6 Scott Ullrich
2007
    # HNA validity time
2008 6d0433a5 Scott Ullrich
    HnaValidityTime 	30.0
2009 80ce93c6 Scott Ullrich
2010
    # When multiple links exist between hosts
2011
    # the weight of interface is used to determine
2012
    # the link to use. Normally the weight is
2013
    # automatically calculated by olsrd based
2014
    # on the characteristics of the interface,
2015
    # but here you can specify a fixed value.
2016
    # Olsrd will choose links with the lowest value.
2017
2018
    # Weight 0
2019
2020
2021
}
2022 ac809adb Scott Ullrich
2023 c7f44ae0 Scott Ullrich
EODAD;
2024 ac809adb Scott Ullrich
2025
	}
2026 d70f19da Scott Ullrich
	break;
2027 ac809adb Scott Ullrich
}
2028 db5fbdb7 Scott Ullrich
		fwrite($fd, $olsr);
2029
		fclose($fd);
2030
	}
2031 c7f44ae0 Scott Ullrich
2032 7cca77de Ermal
	if (is_process_running("olsrd"))
2033 73239086 Seth Mos
		mwexec("/usr/bin/killall olsrd", true);
2034 97fd5cb8 Scott Ullrich
2035
	sleep(2);
2036
2037
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/olsr.conf");
2038 c7f44ae0 Scott Ullrich
2039 eebeaf0d Scott Ullrich
	conf_mount_ro();
2040 80ce93c6 Scott Ullrich
}
2041
2042 1071e028 Scott Ullrich
/* configure cron service */
2043
function configure_cron() {
2044
	global $g, $config;
2045 e7d3fc15 Ermal
2046 251ca022 Scott Ullrich
	conf_mount_rw();
2047 1071e028 Scott Ullrich
	/* preserve existing crontab entries */
2048 e7d3fc15 Ermal
	$crontab_contents = file("/etc/crontab", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
2049 107e8acc Ovidiu Predescu
2050 e7d3fc15 Ermal
	for ($i = 0; $i < count($crontab_contents); $i++) {
2051
		$cron_item =& $crontab_contents[$i];
2052
		if (strpos($cron_item, "# pfSense specific crontab entries") !== false) {
2053
			array_splice($crontab_contents, $i - 1);
2054 1071e028 Scott Ullrich
			break;
2055
		}
2056
	}
2057 e7d3fc15 Ermal
	$crontab_contents = implode("\n", $crontab_contents) . "\n";
2058 107e8acc Ovidiu Predescu
2059
2060 1071e028 Scott Ullrich
	if (is_array($config['cron']['item'])) {
2061
		$crontab_contents .= "#\n";
2062 f1a44a3a Carlos Eduardo Ramos
		$crontab_contents .= "# " . gettext("pfSense specific crontab entries") . "\n";
2063
		$crontab_contents .= "# " .gettext( "Created:") . " " . date("F j, Y, g:i a") . "\n";
2064 1071e028 Scott Ullrich
		$crontab_contents .= "#\n";
2065
2066
		foreach ($config['cron']['item'] as $item) {
2067
			$crontab_contents .= "\n{$item['minute']}\t";
2068
			$crontab_contents .= "{$item['hour']}\t";
2069
			$crontab_contents .= "{$item['mday']}\t";
2070
			$crontab_contents .= "{$item['month']}\t";
2071
			$crontab_contents .= "{$item['wday']}\t";
2072
			$crontab_contents .= "{$item['who']}\t";
2073
			$crontab_contents .= "{$item['command']}";
2074
		}
2075 107e8acc Ovidiu Predescu
2076 1071e028 Scott Ullrich
		$crontab_contents .= "\n#\n";
2077 f1a44a3a Carlos Eduardo Ramos
		$crontab_contents .= "# " . gettext("If possible do not add items to this file manually.") . "\n";
2078
		$crontab_contents .= "# " . gettext("If you do so, this file must be terminated with a blank line (e.g. new line)") . "\n";
2079 1071e028 Scott Ullrich
		$crontab_contents .= "#\n\n";
2080
	}
2081 107e8acc Ovidiu Predescu
2082 1071e028 Scott Ullrich
	/* please maintain the newline at the end of file */
2083
	file_put_contents("/etc/crontab", $crontab_contents);
2084 41d507a5 Scott Ullrich
2085
	/* do a HUP kill to force sync changes */
2086 a147b1e0 jim-p
	exec('/bin/pkill -HUP cron');
2087 41d507a5 Scott Ullrich
2088 6d6bca27 Ermal Lu?i
	conf_mount_ro();
2089 1071e028 Scott Ullrich
}
2090
2091 431484c8 Ryan Wagoner
function upnp_action ($action) {
2092 aa6798c0 Scott Ullrich
	global $g, $config;
2093 431484c8 Ryan Wagoner
	switch($action) {
2094
		case "start":
2095 c1ac2424 Ermal
			if (file_exists('/var/etc/miniupnpd.conf')) {
2096
				@unlink("{$g['varrun_path']}/miniupnpd.pid");
2097
				mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf -P {$g['varrun_path']}/miniupnpd.pid");
2098
			}
2099 431484c8 Ryan Wagoner
			break;
2100
		case "stop":
2101 c1ac2424 Ermal
			killbypid("{$g['varrun_path']}/miniupnpd.pid");
2102 05c4bfa0 Ermal
			while((int)exec("/bin/pgrep -a miniupnpd | wc -l") > 0)
2103 73239086 Seth Mos
				mwexec('killall miniupnpd 2>/dev/null', true);
2104 431484c8 Ryan Wagoner
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
2105
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
2106
			break;
2107
		case "restart":
2108
			upnp_action('stop');
2109
			upnp_action('start');
2110
			break;
2111
	}
2112
}
2113
2114 6f20377b Scott Ullrich
function upnp_start() {
2115 dcc897e5 Ermal
	global $config;
2116 0c331f1e Ermal Lu?i
2117
	if(!isset($config['installedpackages']['miniupnpd']['config']))
2118
		return;
2119
2120 431484c8 Ryan Wagoner
	if($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
2121 54bdff75 Vinicius Coque
		echo gettext("Starting UPnP service... ");
2122 dcc897e5 Ermal
		require_once('/usr/local/pkg/miniupnpd.inc');
2123
		sync_package_miniupnpd();
2124
		echo "done.\n";
2125 6f20377b Scott Ullrich
	}
2126
}
2127
2128 85405c11 jim-p
function install_cron_job($command, $active=false, $minute="0", $hour="*", $monthday="*", $month="*", $weekday="*", $who="root") {
2129
	global $config, $g;
2130
2131
	$is_installed = false;
2132
2133
	if(!$config['cron']['item'])
2134
		return;
2135
2136
	$x=0;
2137
	foreach($config['cron']['item'] as $item) {
2138
		if(strstr($item['command'], $command)) {
2139
			$is_installed = true;
2140
			break;
2141
		}
2142
		$x++;
2143
	}
2144
2145
	if($active) {
2146
		$cron_item = array();
2147
		$cron_item['minute'] = $minute;
2148
		$cron_item['hour'] = $hour;
2149
		$cron_item['mday'] = $monthday;
2150
		$cron_item['month'] = $month;
2151
		$cron_item['wday'] = $weekday;
2152
		$cron_item['who'] = $who;
2153
		$cron_item['command'] = $command;
2154
		if(!$is_installed) {
2155
			$config['cron']['item'][] = $cron_item;
2156 f1a44a3a Carlos Eduardo Ramos
			write_config(sprintf(gettext("Installed cron job for %s"), $command));
2157 85405c11 jim-p
		} else {
2158
			$config['cron']['item'][$x] = $cron_item;
2159 f1a44a3a Carlos Eduardo Ramos
			write_config(sprintf(gettext("Updated cron job for %s"), $command));
2160 85405c11 jim-p
		}
2161
	} else {
2162
		if(($is_installed == true) && ($x > 0)) {
2163
			unset($config['cron']['item'][$x]);
2164 f1a44a3a Carlos Eduardo Ramos
			write_config(sprintf(gettext("Remvoed cron job for %s"), $command));
2165 85405c11 jim-p
		}
2166
	}
2167
	configure_cron();
2168
}
2169
2170 693833cb Seth Mos
?>