Project

General

Profile

Download (33.7 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	services.inc
5
	part of m0n0wall (http://m0n0.ch/wall)
6 a25183c5 Scott Ullrich
7 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9 a25183c5 Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 a25183c5 Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 a25183c5 Scott Ullrich
16 5b237745 Scott Ullrich
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19 a25183c5 Scott Ullrich
20 5b237745 Scott Ullrich
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31
32
/* include all configuration functions */
33
require_once("functions.inc");
34
35 9534ea8b Scott Ullrich
function load_balancer_use_sticky() {
36
	global $config, $g;
37
	if (isset ($config['system']['lb_use_sticky']))
38
		touch("/var/etc/use_pf_pool__stickyaddr");
39
	else
40
		unlink_if_exists("/var/etc/use_pf_pool__stickyaddr");
41
}
42
43 5b237745 Scott Ullrich
function services_dhcpd_configure() {
44 f19d3b7a Scott Ullrich
	global $config, $g;
45 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
46 acd910bf Scott Ullrich
		$mt = microtime();
47 f19d3b7a Scott Ullrich
		echo "services_dhcpd_configure($if) being called $mt\n";
48 acd910bf Scott Ullrich
	}
49 a25183c5 Scott Ullrich
50 48ab0cd2 Scott Ullrich
	/* if OLSRD is enabled, allow WAN to house DHCP. */
51 c7f44ae0 Scott Ullrich
	if($config['installedpackages']['olsrd'])
52
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd)
53
				if($olsrd['enable'])
54 48ab0cd2 Scott Ullrich
					$is_olsr_enabled = true;
55
56 68a0e4fc Scott Ullrich
	/* configure DHCPD chroot */
57
	$fd = fopen("/tmp/dhcpd.sh","w");
58 1d023e00 Scott Ullrich
	$status = `mount | grep "{$g['dhcpd_chroot_path']}/dev"`;
59
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}\n");
60
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
61
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
62
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
63
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
64
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
65
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
66
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/run\n");
67
	fwrite($fd, "chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
68
	fwrite($fd, "cp /lib/libc.so.6 {$g['dhcpd_chroot_path']}/lib/\n");
69
	fwrite($fd, "cp /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
70
	fwrite($fd, "chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
71 c7f44ae0 Scott Ullrich
	if(!trim($status))
72 1d023e00 Scott Ullrich
		fwrite($fd, "mount_devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
73 68a0e4fc Scott Ullrich
	fclose($fd);
74
	mwexec("/bin/sh /tmp/dhcpd.sh");
75
76 5b237745 Scott Ullrich
	/* kill any running dhcpd */
77 a3046c54 Scott Ullrich
	if(is_process_running("dhcpd"))
78
		mwexec("killall dhcpd");
79 a25183c5 Scott Ullrich
80 5b237745 Scott Ullrich
	$syscfg = $config['system'];
81
	$dhcpdcfg = $config['dhcpd'];
82 a25183c5 Scott Ullrich
83 5b237745 Scott Ullrich
	/* DHCP enabled on any interfaces? */
84
	$dhcpdenable = false;
85
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
86
		if (isset($dhcpifconf['enable']) &&
87
			(($dhcpif == "lan") ||
88
			(isset($config['interfaces'][$dhcpif]['enable']) &&
89
			$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
90
			$dhcpdenable = true;
91 77f32eff Scott Ullrich
		if (isset($dhcpifconf['enable']) &&
92
			(($dhcpif == "wan") || (isset($config['interfaces'][$dhcpif]['enable']) &&
93
			$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
94 c7f44ae0 Scott Ullrich
			$dhcpdenable = true;
95 5b237745 Scott Ullrich
	}
96 a25183c5 Scott Ullrich
97 5b237745 Scott Ullrich
	if (!$dhcpdenable)
98
		return 0;
99 a25183c5 Scott Ullrich
100 5b237745 Scott Ullrich
	if ($g['booting'])
101 f05740c1 Scott Ullrich
		echo "Starting DHCP service...";
102 5b237745 Scott Ullrich
	else
103
		sleep(1);
104 a25183c5 Scott Ullrich
105 5b237745 Scott Ullrich
	/* write dhcpd.conf */
106 1d023e00 Scott Ullrich
	$fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w");
107 5b237745 Scott Ullrich
	if (!$fd) {
108
		printf("Error: cannot open dhcpd.conf in services_dhcpd_configure().\n");
109
		return 1;
110
	}
111 a25183c5 Scott Ullrich
112 c7f44ae0 Scott Ullrich
113 4cab31d0 Scott Ullrich
114 5b237745 Scott Ullrich
	$dhcpdconf = <<<EOD
115
option domain-name "{$syscfg['domain']}";
116
default-lease-time 7200;
117
max-lease-time 86400;
118
log-facility local7;
119
ddns-update-style none;
120 175fe82b Scott Ullrich
one-lease-per-client true;
121 436a0f50 Scott Ullrich
deny duplicates;
122 5b237745 Scott Ullrich
123
EOD;
124 a25183c5 Scott Ullrich
125 5b237745 Scott Ullrich
	$dhcpdifs = array();
126 c7f44ae0 Scott Ullrich
127 8fa56d1f Scott Ullrich
	/*    loop through and deterimine if we need to setup
128
	 *    failover peer "bleh" entries
129
	 */
130
	$dhcpnum = 0;
131
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
132 53f32329 Scott Ullrich
133
		if(!isset($dhcpifconf['disableauthoritative']))
134
			$dhcpdconf .= "authoritative;\n";
135
136 8fa56d1f Scott Ullrich
		if($dhcpifconf['failover_peerip'] <> "") {
137
			/*
138
			 *    yep, failover peer is defined.
139
			 *    does it match up to a defined vip?
140
			 */
141 d2edbd8a Scott Ullrich
			$skew = 110;
142 2d314e69 Scott Ullrich
			$a_vip = &$config['virtualip']['vip'];
143 6181b36f Scott Ullrich
			if(is_array($a_vip)) {
144
				foreach ($a_vip as $vipent) {
145
					$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
146
					$intip = find_interface_ip($int);
147
					$real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif);
148
					if($int == $real_dhcpif) {
149
						/* this is the interface! */
150 54c47ec5 Scott Ullrich
						if($vipent['advskew'] < "20")
151 6181b36f Scott Ullrich
							$skew = 0;
152
					}
153 8fa56d1f Scott Ullrich
				}
154 25066204 Scott Ullrich
			} else {
155
				log_error("Warning!  DHCP Failover setup and no CARP virtual IP's defined!");
156 8fa56d1f Scott Ullrich
			}
157 5e390f54 Scott Ullrich
			if($skew > 10) {
158 8fa56d1f Scott Ullrich
				$type = "secondary";
159 4d3ff974 Scott Ullrich
				$dhcpdconf_pri  = "mclt 600;\n";
160 0e93097a Scott Ullrich
				$my_port = "520";
161
				$peer_port = "519";
162 8fa56d1f Scott Ullrich
			} else {
163 0e93097a Scott Ullrich
				$my_port = "519";
164
				$peer_port = "520";
165 8fa56d1f Scott Ullrich
				$type = "primary";
166 4d3ff974 Scott Ullrich
				$dhcpdconf_pri  = "split 128;\n";
167 1a0bb737 Scott Ullrich
				$dhcpdconf_pri .= "  mclt 600;\n";
168 8fa56d1f Scott Ullrich
			}
169
			$dhcpdconf .= <<<EOPP
170 c7f44ae0 Scott Ullrich
failover peer "dhcp{$dhcpnum}" {
171 8fa56d1f Scott Ullrich
  {$type};
172
  address {$intip};
173 0e93097a Scott Ullrich
  port {$my_port};
174 8fa56d1f Scott Ullrich
  peer address {$dhcpifconf['failover_peerip']};
175 0e93097a Scott Ullrich
  peer port {$peer_port};
176 8fa56d1f Scott Ullrich
  max-response-delay 60;
177 b865d178 Scott Ullrich
  max-unacked-updates 10;
178
  {$dhcpdconf_pri}
179 b259d1c6 Scott Ullrich
  load balance max seconds 3;
180 8fa56d1f Scott Ullrich
}
181 c286d395 Scott Ullrich
182 8fa56d1f Scott Ullrich
EOPP;
183
		$dhcpnum++;
184
		}
185
	}
186
187
	$dhcpnum = 0;
188
189 5b237745 Scott Ullrich
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
190 a25183c5 Scott Ullrich
191 5b237745 Scott Ullrich
		$ifcfg = $config['interfaces'][$dhcpif];
192 a25183c5 Scott Ullrich
193 5b237745 Scott Ullrich
		if (!isset($dhcpifconf['enable']) ||
194 2d5614de Seth Mos
			($ifcfg['ipaddr'] == "dhcp") ||
195 5b237745 Scott Ullrich
			(($dhcpif != "lan") &&
196
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
197
			continue;
198 d254fe7b Scott Ullrich
199
		if($dhcpif == "lan" && $ifcfg['bridge'])
200 bcc49d35 Scott Ullrich
			log_error("NOTE: DHCP Server on LAN is enabled.");
201 a25183c5 Scott Ullrich
202 5b237745 Scott Ullrich
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
203
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
204 a25183c5 Scott Ullrich
205 48ab0cd2 Scott Ullrich
		if($is_olsr_enabled == true)
206 c7f44ae0 Scott Ullrich
			if($dhcpifconf['netmask'])
207 9a537862 Scott Ullrich
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
208 48ab0cd2 Scott Ullrich
209 5b237745 Scott Ullrich
		$dnscfg = "";
210 a25183c5 Scott Ullrich
211 5b237745 Scott Ullrich
		if ($dhcpifconf['domain']) {
212
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
213
		}
214 4e9cd828 Seth Mos
		if (isset($dhcpifconf['ddnsupdate'])) {
215
			if($dhcpifconf['ddnsdomain'] <> "") {
216
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
217
			}
218
			$dnscfg .= "	ddns-update-style interim;\n";
219
		}
220
221 a25183c5 Scott Ullrich
222 aff9d6ab Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
223 8ee01642 Scott Ullrich
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
224 aff9d6ab Scott Ullrich
		} else if (isset($config['dnsmasq']['enable'])) {
225
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
226
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
227
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
228
		}
229
230
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
231
		$dhcpdconf .= "	pool {\n";
232
233 2589c9bd Scott Ullrich
		/* is failover dns setup? */
234 698f5500 Scott Ullrich
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
235 aff9d6ab Scott Ullrich
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
236 698f5500 Scott Ullrich
			if($dhcpifconf['dnsserver'][1] <> "")
237
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
238 e35dfd89 Scott Ullrich
			$dhcpdconf .= ";\n";
239 5b237745 Scott Ullrich
		}
240 c7f44ae0 Scott Ullrich
241
		if($dhcpifconf['failover_peerip'] <> "")
242 aff9d6ab Scott Ullrich
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
243 c7f44ae0 Scott Ullrich
244 a25183c5 Scott Ullrich
		if (isset($dhcpifconf['denyunknown']))
245 5b237745 Scott Ullrich
		   $dhcpdconf .= "		deny unknown clients;\n";
246 a25183c5 Scott Ullrich
247 5b237745 Scott Ullrich
		if ($dhcpifconf['gateway'])
248
			$routers = $dhcpifconf['gateway'];
249
		else
250
			$routers = $ifcfg['ipaddr'];
251 a25183c5 Scott Ullrich
252 8fa56d1f Scott Ullrich
		if($dhcpifconf['failover_peerip'] <> "") {
253
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
254
			$dhcpnum++;
255
		}
256
257 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
258
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
259
	}
260
	option routers {$routers};
261
$dnscfg
262
263
EOD;
264
265
		if ($dhcpifconf['defaultleasetime'])
266
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
267
		if ($dhcpifconf['maxleasetime'])
268
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
269 a25183c5 Scott Ullrich
270 5b237745 Scott Ullrich
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
271
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
272
			$dhcpdconf .= "	option netbios-node-type 8;\n";
273
		}
274 a25183c5 Scott Ullrich
275 ad171999 Seth Mos
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0])
276
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
277
278 4e9cd828 Seth Mos
		if(isset($dhcpifconf['netboot'])) {
279
			if (($dhcpifconf['next-server'] <> "") && ($dhcpifconf['filename'] <> "")) {
280
				$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
281
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
282
			}
283
		}
284 5b237745 Scott Ullrich
		$dhcpdconf .= <<<EOD
285
}
286
287
EOD;
288
289
		/* add static mappings */
290
		if (is_array($dhcpifconf['staticmap'])) {
291 a25183c5 Scott Ullrich
292 5b237745 Scott Ullrich
			$i = 0;
293
			foreach ($dhcpifconf['staticmap'] as $sm) {
294
				$dhcpdconf .= <<<EOD
295
host s_{$dhcpif}_{$i} {
296
	hardware ethernet {$sm['mac']};
297
298
EOD;
299
				if ($sm['ipaddr'])
300
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
301 a25183c5 Scott Ullrich
302 5b237745 Scott Ullrich
				$dhcpdconf .= "}\n";
303
				$i++;
304
			}
305
		}
306 a25183c5 Scott Ullrich
307 5b237745 Scott Ullrich
		$dhcpdifs[] = $ifcfg['if'];
308
	}
309
310
	fwrite($fd, $dhcpdconf);
311
	fclose($fd);
312
313
	/* create an empty leases database */
314 1d023e00 Scott Ullrich
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
315 a25183c5 Scott Ullrich
316 68a0e4fc Scott Ullrich
	/* fire up dhcpd in a chroot */
317 1d023e00 Scott Ullrich
	mwexec("/usr/local/sbin/dhcpd -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf {$g['dhcpd_chroot_path']}/etc/dhcpd.conf " .
318 5b237745 Scott Ullrich
		join(" ", $dhcpdifs));
319 a25183c5 Scott Ullrich
320 fc6b5a4d Scott Ullrich
	if ($g['booting']) {
321 68a0e4fc Scott Ullrich
		print "done.\n";
322 5c6d0f65 Colin Smith
	}
323 a25183c5 Scott Ullrich
324 5b237745 Scott Ullrich
	return 0;
325
}
326
327 80933129 Bill Marquette
function interfaces_staticarp_configure($if) {
328 f19d3b7a Scott Ullrich
	global $config, $g;
329 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
330 acd910bf Scott Ullrich
		$mt = microtime();
331 dcf0598e Scott Ullrich
		echo "interfaces_staticarp_configure($if) being called $mt\n";
332 acd910bf Scott Ullrich
	}
333 c7f44ae0 Scott Ullrich
334 80933129 Bill Marquette
        $ifcfg = $config['interfaces'][$if];
335 5c0538e0 Bill Marquette
336
        /* Enable staticarp, if enabled */
337 80933129 Bill Marquette
        if(isset($config['dhcpd'][$if]['staticarp'])) {
338
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
339 5c0538e0 Bill Marquette
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
340 80933129 Bill Marquette
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
341 5c0538e0 Bill Marquette
342 80933129 Bill Marquette
                        foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
343 5c0538e0 Bill Marquette
                                mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
344
345
                        }
346 c7f44ae0 Scott Ullrich
347 5c0538e0 Bill Marquette
                }
348
        } else {
349 80933129 Bill Marquette
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
350 d4d6caab Scott Ullrich
                mwexec("/usr/sbin/arp -da > /dev/null 2>&1 ");
351 5c0538e0 Bill Marquette
        }
352
353
        return 0;
354
}
355
356 5b237745 Scott Ullrich
function services_dhcrelay_configure() {
357 f19d3b7a Scott Ullrich
	global $config, $g;
358 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
359 acd910bf Scott Ullrich
		$mt = microtime();
360 f19d3b7a Scott Ullrich
		echo "services_dhcrelay_configure() being called $mt\n";
361 acd910bf Scott Ullrich
	}
362 a25183c5 Scott Ullrich
363 5b237745 Scott Ullrich
	/* kill any running dhcrelay */
364
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
365 a25183c5 Scott Ullrich
366 5b237745 Scott Ullrich
	$dhcrelaycfg = $config['dhcrelay'];
367 a25183c5 Scott Ullrich
368 5b237745 Scott Ullrich
	/* DHCPRelay enabled on any interfaces? */
369
	$dhcrelayenable = false;
370 0ab19c0e Scott Ullrich
	if(is_array($dhcrelaycfg)) {
371
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
372
			if (isset($dhcrelayifconf['enable']) &&
373
				(($dhcrelayif == "lan") ||
374
				(isset($config['interfaces'][$dhcrelayif]['enable']) &&
375
				$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
376
				$dhcrelayenable = true;
377
		}
378 5b237745 Scott Ullrich
	}
379 a25183c5 Scott Ullrich
380 5b237745 Scott Ullrich
	if (!$dhcrelayenable)
381
		return 0;
382 a25183c5 Scott Ullrich
383 5b237745 Scott Ullrich
	if ($g['booting'])
384 f05740c1 Scott Ullrich
		echo "Starting DHCP relay service...";
385 5b237745 Scott Ullrich
	else
386
		sleep(1);
387 a25183c5 Scott Ullrich
388 5b237745 Scott Ullrich
	$dhcrelayifs = array();
389
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
390 a25183c5 Scott Ullrich
391 5b237745 Scott Ullrich
		$ifcfg = $config['interfaces'][$dhcrelayif];
392 a25183c5 Scott Ullrich
393 5b237745 Scott Ullrich
		if (!isset($dhcrelayifconf['enable']) ||
394
			(($dhcrelayif != "lan") &&
395
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
396
			continue;
397 a25183c5 Scott Ullrich
398 5b237745 Scott Ullrich
		$dhcrelayifs[] = $ifcfg['if'];
399
	}
400
401 a25183c5 Scott Ullrich
	/* In order for the relay to work, it needs to be active on the
402 5b237745 Scott Ullrich
	   interface in which the destination server sits */
403
	foreach ($config['interfaces'] as $ifname) {
404
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
405 a25183c5 Scott Ullrich
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
406
			$destif = $ifname['if'];
407 5b237745 Scott Ullrich
	}
408 a25183c5 Scott Ullrich
409
	if (!isset($destif))
410 5b237745 Scott Ullrich
		$destif = $config['interfaces']['wan']['if'];
411 a25183c5 Scott Ullrich
412 5b237745 Scott Ullrich
	$dhcrelayifs[] = $destif;
413
	$dhcrelayifs = array_unique($dhcrelayifs);
414
415
	/* fire up dhcrelay */
416
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
417
418 a25183c5 Scott Ullrich
	if (isset($dhcrelaycfg['agentoption']))
419 5b237745 Scott Ullrich
		$cmd .=  " -a -m replace";
420
421
	$cmd .= " {$dhcrelaycfg['server']}";
422
	mwexec($cmd);
423 a25183c5 Scott Ullrich
424 5b237745 Scott Ullrich
	if (!$g['booting']) {
425 e239df5a Scott Ullrich
		/* set the reload filter dity flag */
426 f229e20f Scott Ullrich
		touch("{$g['tmp_path']}/filter_dirty");
427 5c6d0f65 Colin Smith
	}
428 a25183c5 Scott Ullrich
429 5b237745 Scott Ullrich
	return 0;
430
}
431
432
function services_dyndns_reset() {
433 f19d3b7a Scott Ullrich
	global $config, $g;
434 59a63553 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
435
		$mt = microtime();
436
		echo "services_dyndns_reset() being called $mt\n";
437
	}
438 5b237745 Scott Ullrich
439
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
440 59a63553 Scott Ullrich
		conf_mount_rw();
441 5b237745 Scott Ullrich
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
442 59a63553 Scott Ullrich
		conf_mount_ro();
443
	}
444
445 5b237745 Scott Ullrich
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
446
		conf_mount_rw();
447
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
448
		conf_mount_ro();
449
	}
450 59a63553 Scott Ullrich
451 5b237745 Scott Ullrich
	return 0;
452
}
453
454
function services_dyndns_configure() {
455 f19d3b7a Scott Ullrich
	global $config, $g;
456 59a63553 Scott Ullrich
	if(isset($config['system']['developerspew'])) {
457
		$mt = microtime();
458
		echo "services_dyndns_configure() being called $mt\n";
459
	}
460
461 5b237745 Scott Ullrich
	$dyndnscfg = $config['dyndns'];
462 9cc8c59e Scott Ullrich
	$wancfg = $config['interfaces']['wan'];
463 59a63553 Scott Ullrich
464 5b237745 Scott Ullrich
	if (isset($dyndnscfg['enable'])) {
465 59a63553 Scott Ullrich
466
		if ($g['booting']) {
467 f05740c1 Scott Ullrich
			echo "Starting DynDNS client...";
468 59a63553 Scott Ullrich
			if(isset($config['system']['use_old_dyndns'])) {
469
				echo " [Using ez-ipupdate] ";
470
				services_dyndns_configure_old();
471
				return;
472 c7f44ae0 Scott Ullrich
			}
473 59a63553 Scott Ullrich
		} else {
474 5b237745 Scott Ullrich
			sleep(1);
475 59a63553 Scott Ullrich
			if(isset($config['system']['use_old_dyndns'])) {
476
				services_dyndns_configure_old();
477
				return;
478 c7f44ae0 Scott Ullrich
			}
479 5e2f59ed Scott Ullrich
		}
480 c7f44ae0 Scott Ullrich
481 59a63553 Scott Ullrich
		/* load up the dyndns.class */
482
		require_once("dyndns.class");
483
484
		log_error("DynDns: Running updatedns()");
485
486 6b35646f Scott Ullrich
		/* determine WAN interface name */
487
		$wanif = get_real_wan_interface();
488
		/* get ip */
489
		$ip = find_interface_ip($wanif);
490
491 59a63553 Scott Ullrich
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
492
							 $dnsHost = $config['dyndns']['host'],
493
							 $dnsUser = $config['dyndns']['username'],
494
							 $dnsPass = $config['dyndns']['password'],
495
							 $dnsWilcard = $config['dyndns']['wildcard'],
496
							 $dnsMX = $config['dyndns']['mx']);
497
498
		if ($g['booting'])
499
			echo "done.\n";
500
	}
501
502
	return 0;
503
}
504
505
function services_dyndns_configure_old() {
506
	global $config, $g;
507
	if(isset($config['system']['developerspew'])) {
508
		$mt = microtime();
509
		echo "services_dyndns_configure_old() being called $mt\n";
510
	}
511
512
        /* kill any running ez-ipupdate */
513
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
514
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
515
516
        $dyndnscfg = $config['dyndns'];
517
        $wancfg = $config['interfaces']['wan'];
518
519
        if (isset($dyndnscfg['enable'])) {
520
521
                if ($g['booting'])
522 f05740c1 Scott Ullrich
                        echo "Starting DynDNS client...";
523 59a63553 Scott Ullrich
                else
524
                        sleep(1);
525
526
                /* determine WAN interface name */
527
                $wanif = get_real_wan_interface();
528
529
                /* write ez-ipupdate.conf */
530
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
531
                if (!$fd) {
532
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
533
                        return 1;
534
                }
535
536
                $ezipupdateconf = <<<EOD
537 0d5f2f3e Scott Ullrich
service-type={$dyndnscfg['type']}
538
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
539
host={$dyndnscfg['host']}
540 59a63553 Scott Ullrich
interface={$wanif}
541 0d5f2f3e Scott Ullrich
max-interval=2073600
542
pid-file={$g['varrun_path']}/ez-ipupdate.pid
543
cache-file={$g['vardb_path']}/ez-ipupdate.cache
544
execute=/etc/rc.dyndns.storecache
545
daemon
546
547
EOD;
548
549 59a63553 Scott Ullrich
                /* enable server[:port]? */
550
                if ($dyndnscfg['server']) {
551
                        if ($dyndnscfg['port'])
552
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
553
                        else
554
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
555
                }
556 0d5f2f3e Scott Ullrich
557 59a63553 Scott Ullrich
                /* enable MX? */
558
                if ($dyndnscfg['mx']) {
559
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
560
                }
561 0d5f2f3e Scott Ullrich
562 59a63553 Scott Ullrich
                /* enable wildcards? */
563
                if (isset($dyndnscfg['wildcard'])) {
564
                        $ezipupdateconf .= "wildcard\n";
565
                }
566
567
                fwrite($fd, $ezipupdateconf);
568
                fclose($fd);
569
570
                /* if we're booting, copy the cache file from /conf */
571
                if ($g['booting']) {
572
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
573
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
574
                       }
575
                }
576
577
                /* run ez-ipupdate */
578
                mwexec("/usr/local/bin/ez-ipupdate -c {$g['varetc_path']}/ez-ipupdate.conf");
579
580
                if ($g['booting'])
581
                        echo "done\n";
582
        }
583
584
        return 0;
585 0d5f2f3e Scott Ullrich
}
586
587 5b237745 Scott Ullrich
function services_dnsmasq_configure() {
588 f19d3b7a Scott Ullrich
	global $config, $g;
589 6a01ea44 Bill Marquette
	$return = 0;
590
	
591 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
592 acd910bf Scott Ullrich
		$mt = microtime();
593 f19d3b7a Scott Ullrich
		echo "services_dnsmasq_configure() being called $mt\n";
594 acd910bf Scott Ullrich
	}
595
596 5b237745 Scott Ullrich
	/* kill any running dnsmasq */
597
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
598
599
	if (isset($config['dnsmasq']['enable'])) {
600 a25183c5 Scott Ullrich
601 5b237745 Scott Ullrich
		if ($g['booting'])
602 f05740c1 Scott Ullrich
			echo "Starting DNS forwarder...";
603 5b237745 Scott Ullrich
		else
604
			sleep(1);
605
606
		/* generate hosts file */
607 6a01ea44 Bill Marquette
		if(system_hosts_generate()!=0)
608
			$return = 1;
609 a25183c5 Scott Ullrich
610 5b237745 Scott Ullrich
		$args = "";
611 a25183c5 Scott Ullrich
612 5b237745 Scott Ullrich
		if (isset($config['dnsmasq']['regdhcp'])) {
613 a25183c5 Scott Ullrich
614 d097bb38 Scott Ullrich
			$args .= " -l {$g['dhcpd_chroot_path']}/var/db/dhcpd.leases" .
615 5b237745 Scott Ullrich
				" -s {$config['system']['domain']}";
616
		}
617
618 0c2b5df7 Scott Ullrich
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
619
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
620
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
621
                        }
622
                }
623
624 91adc5c1 Scott Ullrich
		/* suppose that dnsmasq handles our domain and don't send
625
		requests for our local domain to upstream servers */
626 a704b650 Scott Ullrich
		//if (!empty($config['system']['domain'])) {
627
		//	$args .= sprintf(' --local=/%s/', $config['system']['domain']);
628
		//}
629 91adc5c1 Scott Ullrich
630 5b237745 Scott Ullrich
		/* run dnsmasq */
631
		mwexec("/usr/local/sbin/dnsmasq {$args}");
632
633
		if ($g['booting'])
634 5c6d0f65 Colin Smith
			echo "done.\n";
635 5b237745 Scott Ullrich
	}
636 a25183c5 Scott Ullrich
637 5b237745 Scott Ullrich
	if (!$g['booting']) {
638 6a01ea44 Bill Marquette
		if(services_dhcpd_configure()!=0)
639
			$return = 1;
640 5b237745 Scott Ullrich
	}
641
642 6a01ea44 Bill Marquette
	return $return;
643 5b237745 Scott Ullrich
}
644
645
function services_snmpd_configure() {
646 f19d3b7a Scott Ullrich
	global $config, $g;
647 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
648 acd910bf Scott Ullrich
		$mt = microtime();
649 f19d3b7a Scott Ullrich
		echo "services_snmpd_configure() being called $mt\n";
650
	}
651 5b237745 Scott Ullrich
652
	/* kill any running snmpd */
653
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
654 bc95f193 Scott Ullrich
	if(is_process_running("bsnmpd")) 
655
		exec("/usr/bin/killall bsnmpd");
656 5b237745 Scott Ullrich
657
	if (isset($config['snmpd']['enable'])) {
658 a25183c5 Scott Ullrich
659 5b237745 Scott Ullrich
		if ($g['booting'])
660 5c6d0f65 Colin Smith
			echo "Starting SNMP daemon... ";
661 5b237745 Scott Ullrich
662
		/* generate snmpd.conf */
663
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
664
		if (!$fd) {
665
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
666
			return 1;
667
		}
668 a25183c5 Scott Ullrich
669 142da8f7 John Fleming
670 5b237745 Scott Ullrich
		$snmpdconf = <<<EOD
671 d47a8a69 Scott Ullrich
location := "{$config['snmpd']['syslocation']}"
672
contact := "{$config['snmpd']['syscontact']}"
673
read := "{$config['snmpd']['rocommunity']}"
674 142da8f7 John Fleming
675
EOD;
676
677
/* No docs on what write strings do there for disable for now.
678
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
679
		    $snmpdconf .= <<<EOD
680
# write string
681
write := "{$config['snmpd']['rwcommunity']}"
682
683
EOD;
684
		}
685
*/
686
687
688
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
689
		    $snmpdconf .= <<<EOD
690
# SNMP Trap support.
691 dbeeb008 John Fleming
traphost := {$config['snmpd']['trapserver']}
692
trapport := {$config['snmpd']['trapserverport']}
693
trap := "{$config['snmpd']['trapstring']}"
694 142da8f7 John Fleming
695
696
EOD;
697
		}
698
699
700
		$snmpdconf .= <<<EOD
701 d47a8a69 Scott Ullrich
system := 1     # pfSense
702
%snmpd
703
begemotSnmpdDebugDumpPdus       = 2
704
begemotSnmpdDebugSyslogPri      = 7
705
begemotSnmpdCommunityString.0.1 = $(read)
706 142da8f7 John Fleming
707
EOD;
708
709
/* No docs on what write strings do there for disable for now.
710
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
711
		    $snmpdconf .= <<<EOD
712
begemotSnmpdCommunityString.0.2 = $(write)
713
714
EOD;
715
		}
716
*/
717
718 c7f44ae0 Scott Ullrich
719 142da8f7 John Fleming
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
720
		    $snmpdconf .= <<<EOD
721
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
722
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
723
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
724
725
EOD;
726
		}
727
728
729
		$snmpdconf .= <<<EOD
730 d47a8a69 Scott Ullrich
begemotSnmpdCommunityDisable    = 1
731 03ba7a0f John Fleming
732
EOD;
733
734 7cbad422 Scott Ullrich
		if(isset($config['snmpd']['bindlan'])) {
735
			$bind_to_ip = $config['interfaces']['lan']['ipaddr'];
736
		} else {
737
			$bind_to_ip = "0.0.0.0";
738
		}
739
740 03ba7a0f John Fleming
		if(is_port( $config['snmpd']['pollport'] )) {
741
		    $snmpdconf .= <<<EOD
742 7cbad422 Scott Ullrich
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
743 03ba7a0f John Fleming
744
EOD;
745
746
		}
747
748
		$snmpdconf .= <<<EOD
749 d47a8a69 Scott Ullrich
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
750
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
751 142da8f7 John Fleming
752 03ba7a0f John Fleming
# These are bsnmp macros not php vars.
753 9cc8c59e Scott Ullrich
sysContact      = $(contact)
754
sysLocation     = $(location)
755
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
756 142da8f7 John Fleming
757 d47a8a69 Scott Ullrich
snmpEnableAuthenTraps = 2
758 03ba7a0f John Fleming
759
EOD;
760
761
		if (is_array( $config['snmpd']['modules'] )) {
762
		    if(isset($config['snmpd']['modules']['mibii'])) {
763
			$snmpdconf .= <<<EOD
764 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
765 03ba7a0f John Fleming
766
EOD;
767
		    }
768
769
		    if(isset($config['snmpd']['modules']['netgraph'])) {
770
			$snmpdconf .= <<<EOD
771 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
772
%netgraph
773
begemotNgControlNodeName = "snmpd"
774 03ba7a0f John Fleming
775
EOD;
776
		    }
777
778
		    if(isset($config['snmpd']['modules']['pf'])) {
779
			$snmpdconf .= <<<EOD
780 d47a8a69 Scott Ullrich
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
781 95fb49e8 Seth Mos
782
EOD;
783
		    }
784
785
		    if(isset($config['snmpd']['modules']['hostres'])) {
786
			$snmpdconf .= <<<EOD
787
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
788
789
EOD;
790
		    }
791
		    if(isset($config['snmpd']['modules']['bridge'])) {
792
			$snmpdconf .= <<<EOD
793
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
794 d47a8a69 Scott Ullrich
# config must end with blank line
795 5b237745 Scott Ullrich
796 03ba7a0f John Fleming
797 5b237745 Scott Ullrich
EOD;
798 03ba7a0f John Fleming
		    }
799
		}
800 5b237745 Scott Ullrich
801
		fwrite($fd, $snmpdconf);
802
		fclose($fd);
803
804 7cbad422 Scott Ullrich
		if (isset($config['snmpd']['bindlan'])) {
805
			$bindlan = "";
806
		}
807
808 853e003a Scott Ullrich
		/* run bsnmpd */
809
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
810 7cbad422 Scott Ullrich
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
811 5b237745 Scott Ullrich
812
		if ($g['booting'])
813 5c6d0f65 Colin Smith
			echo "done.\n";
814 5b237745 Scott Ullrich
	}
815
816
	return 0;
817
}
818
819 f19d3b7a Scott Ullrich
function services_proxyarp_configure() {
820
	global $config, $g;
821 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
822 acd910bf Scott Ullrich
		$mt = microtime();
823 f19d3b7a Scott Ullrich
		echo "services_proxyarp_configure() being called $mt\n";
824 acd910bf Scott Ullrich
	}
825
826 5b237745 Scott Ullrich
	/* kill any running choparp */
827
	killbyname("choparp");
828 a25183c5 Scott Ullrich
829 1425e067 Bill Marquette
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
830 a23d7248 Scott Ullrich
		$paa = array();
831 a25183c5 Scott Ullrich
832 a23d7248 Scott Ullrich
		/* group by interface */
833 1425e067 Bill Marquette
		foreach ($config['virtualip']['vip'] as $vipent) {
834
			if ($vipent['mode'] === "proxyarp") {
835
				if ($vipent['interface'])
836
					$if = $vipent['interface'];
837
				else
838
					$if = "wan";
839 a23d7248 Scott Ullrich
840 1425e067 Bill Marquette
				if (!is_array($paa[$if]))
841
					$paa[$if] = array();
842 a23d7248 Scott Ullrich
843 1425e067 Bill Marquette
				$paa[$if][] = $vipent;
844
			}
845 e4b7e011 Bill Marquette
		}
846 a23d7248 Scott Ullrich
847 1425e067 Bill Marquette
		if (count($paa))
848 e4b7e011 Bill Marquette
		foreach ($paa as $paif => $paents) {
849
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
850 a23d7248 Scott Ullrich
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
851
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
852
                               continue;
853
854 e4b7e011 Bill Marquette
			$args = $config['interfaces'][$paif]['if'] . " auto";
855 a23d7248 Scott Ullrich
856 e4b7e011 Bill Marquette
			foreach ($paents as $paent) {
857 a23d7248 Scott Ullrich
858 1425e067 Bill Marquette
				if (isset($paent['subnet']))
859
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
860 e4b7e011 Bill Marquette
				else if (isset($paent['range']))
861
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
862
						$paent['range']['to']);
863
			}
864 a25183c5 Scott Ullrich
865 e4b7e011 Bill Marquette
			mwexec_bg("/usr/local/sbin/choparp " . $args);
866
		}
867 a23d7248 Scott Ullrich
	}
868
}
869
870
function services_dnsupdate_process() {
871 f19d3b7a Scott Ullrich
	global $config, $g;
872 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
873 acd910bf Scott Ullrich
		$mt = microtime();
874 f19d3b7a Scott Ullrich
		echo "services_dnsupdate_process() being called $mt\n";
875 acd910bf Scott Ullrich
	}
876 f19d3b7a Scott Ullrich
877 a23d7248 Scott Ullrich
	/* Dynamic DNS updating active? */
878
	if (isset($config['dnsupdate']['enable'])) {
879 c7f44ae0 Scott Ullrich
880 a23d7248 Scott Ullrich
		$wanip = get_current_wan_address();
881
		if ($wanip) {
882 c7f44ae0 Scott Ullrich
883 a23d7248 Scott Ullrich
			$keyname = $config['dnsupdate']['keyname'];
884
			/* trailing dot */
885
			if (substr($keyname, -1) != ".")
886
				$keyname .= ".";
887 c7f44ae0 Scott Ullrich
888 a23d7248 Scott Ullrich
			$hostname = $config['dnsupdate']['host'];
889
			/* trailing dot */
890
			if (substr($hostname, -1) != ".")
891
				$hostname .= ".";
892 c7f44ae0 Scott Ullrich
893 a23d7248 Scott Ullrich
			/* write private key file
894
			   this is dumb - public and private keys are the same for HMAC-MD5,
895
			   but nsupdate insists on having both */
896
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
897
			$privkey .= <<<EOD
898
Private-key-format: v1.2
899
Algorithm: 157 (HMAC)
900
Key: {$config['dnsupdate']['keydata']}
901
902
EOD;
903
			fwrite($fd, $privkey);
904
			fclose($fd);
905 c7f44ae0 Scott Ullrich
906 a23d7248 Scott Ullrich
			/* write public key file */
907
			if ($config['dnsupdate']['keytype'] == "zone") {
908
				$flags = 257;
909
				$proto = 3;
910
			} else if ($config['dnsupdate']['keytype'] == "host") {
911
				$flags = 513;
912
				$proto = 3;
913
			} else if ($config['dnsupdate']['keytype'] == "user") {
914
				$flags = 0;
915
				$proto = 2;
916
			}
917 c7f44ae0 Scott Ullrich
918 a23d7248 Scott Ullrich
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
919
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
920
			fclose($fd);
921 c7f44ae0 Scott Ullrich
922 a23d7248 Scott Ullrich
			/* generate update instructions */
923
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
924
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
925
			$upinst .= "\n";	/* mind that trailing newline! */
926 c7f44ae0 Scott Ullrich
927 a23d7248 Scott Ullrich
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
928
			fwrite($fd, $upinst);
929
			fclose($fd);
930 c7f44ae0 Scott Ullrich
931 a23d7248 Scott Ullrich
			/* invoke nsupdate */
932 2c9b965a Scott Ullrich
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}/K{$keyname}+157+00000.key";
933 a23d7248 Scott Ullrich
			if (isset($config['dnsupdate']['usetcp']))
934
				$cmd .= " -v";
935
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
936 c7f44ae0 Scott Ullrich
937 a23d7248 Scott Ullrich
			mwexec_bg($cmd);
938
		}
939
	}
940 c7f44ae0 Scott Ullrich
941 a23d7248 Scott Ullrich
	return 0;
942 5b237745 Scott Ullrich
}
943
944 ac809adb Scott Ullrich
function setup_wireless_olsr() {
945 f19d3b7a Scott Ullrich
	global $config, $g;
946 834c3bbd Scott Ullrich
	if(!$config['installedpackages']['olsrd'] || !$config['installedpackages'])
947 c7f44ae0 Scott Ullrich
		return;
948 58c7450e Scott Ullrich
	if(isset($config['system']['developerspew'])) {
949 acd910bf Scott Ullrich
		$mt = microtime();
950 f19d3b7a Scott Ullrich
		echo "setup_wireless_olsr($interface) being called $mt\n";
951 acd910bf Scott Ullrich
	}
952 eebeaf0d Scott Ullrich
	conf_mount_rw();
953 78e07da7 Scott Ullrich
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
954 8fb40409 Scott Ullrich
		$olsr_enable = $olsrd['enable'];
955
		if($olsr_enable <> "on")
956 c7f44ae0 Scott Ullrich
			return;
957 ac809adb Scott Ullrich
		$fd = fopen("{$g['varetc_path']}/olsr.conf", "w");
958 48ab0cd2 Scott Ullrich
959 dad93b75 Scott Ullrich
		if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") {
960
			$enableannounce .= "\nHna4\n";
961
			$enableannounce .= "{\n";
962 bdd25a82 Scott Ullrich
		if($olsrd['announcedynamicroute'])
963
			$enableannounce .= "\t{$olsrd['announcedynamicroute']}\n";
964
		if($olsrd['enableannounce'] == "on")
965
			$enableannounce .= "0.0.0.0 0.0.0.0";
966 dad93b75 Scott Ullrich
			$enableannounce .= "\n}\n";
967
		} else {
968 48ab0cd2 Scott Ullrich
			$enableannounce = "";
969 dad93b75 Scott Ullrich
		}
970 48ab0cd2 Scott Ullrich
971 db5fbdb7 Scott Ullrich
		$olsr .= <<<EODA
972 80ce93c6 Scott Ullrich
#
973
# olsr.org OLSR daemon config file
974
#
975
# Lines starting with a # are discarded
976
#
977 ac809adb Scott Ullrich
# This file was generated by setup_wireless_olsr() in services.inc
978 80ce93c6 Scott Ullrich
#
979
980 6d0433a5 Scott Ullrich
# This file is an example of a typical
981
# configuration for a mostly static
982
# network(regarding mobility) using
983
# the LQ extention
984
985 80ce93c6 Scott Ullrich
# Debug level(0-9)
986
# If set to 0 the daemon runs in the background
987
988 6d0433a5 Scott Ullrich
DebugLevel	2
989 80ce93c6 Scott Ullrich
990
# IP version to use (4 or 6)
991
992 6d0433a5 Scott Ullrich
IpVersion	4
993 80ce93c6 Scott Ullrich
994
# Clear the screen each time the internal state changes
995
996
ClearScreen     yes
997
998 fa4a6253 Scott Ullrich
{$enableannounce}
999 80ce93c6 Scott Ullrich
1000
# Should olsrd keep on running even if there are
1001
# no interfaces available? This is a good idea
1002
# for a PCMCIA/USB hotswap environment.
1003
# "yes" OR "no"
1004
1005 6d0433a5 Scott Ullrich
AllowNoInt	yes
1006 80ce93c6 Scott Ullrich
1007
# TOS(type of service) value for
1008
# the IP header of control traffic.
1009
# If not set it will default to 16
1010
1011 6d0433a5 Scott Ullrich
#TosValue	16
1012 80ce93c6 Scott Ullrich
1013
# The fixed willingness to use(0-7)
1014
# If not set willingness will be calculated
1015
# dynamically based on battery/power status
1016
# if such information is available
1017
1018 6d0433a5 Scott Ullrich
#Willingness    	4
1019 80ce93c6 Scott Ullrich
1020
# Allow processes like the GUI front-end
1021
# to connect to the daemon.
1022
1023
IpcConnect
1024
{
1025
     # Determines how many simultaneously
1026
     # IPC connections that will be allowed
1027
     # Setting this to 0 disables IPC
1028
1029
     MaxConnections  0
1030
1031
     # By default only 127.0.0.1 is allowed
1032
     # to connect. Here allowed hosts can
1033
     # be added
1034
1035
     Host            127.0.0.1
1036
     #Host            10.0.0.5
1037
1038 c7f44ae0 Scott Ullrich
     # You can also specify entire net-ranges
1039 80ce93c6 Scott Ullrich
     # that are allowed to connect. Multiple
1040
     # entries are allowed
1041
1042 c7f44ae0 Scott Ullrich
     #Net             192.168.1.0 255.255.255.0
1043 80ce93c6 Scott Ullrich
}
1044
1045
# Wether to use hysteresis or not
1046
# Hysteresis adds more robustness to the
1047
# link sensing but delays neighbor registration.
1048
# Used by default. 'yes' or 'no'
1049
1050 6d0433a5 Scott Ullrich
UseHysteresis	no
1051 80ce93c6 Scott Ullrich
1052
# Hysteresis parameters
1053 c7f44ae0 Scott Ullrich
# Do not alter these unless you know
1054 80ce93c6 Scott Ullrich
# what you are doing!
1055
# Set to auto by default. Allowed
1056
# values are floating point values
1057
# in the interval 0,1
1058
# THR_LOW must always be lower than
1059
# THR_HIGH.
1060
1061 6d0433a5 Scott Ullrich
#HystScaling	0.50
1062
#HystThrHigh	0.80
1063
#HystThrLow	0.30
1064 80ce93c6 Scott Ullrich
1065
1066
# Link quality level
1067
# 0 = do not use link quality
1068
# 1 = use link quality for MPR selection
1069
# 2 = use link quality for MPR selection and routing
1070
# Defaults to 0
1071
1072 11bc553c Scott Ullrich
LinkQualityLevel	{$olsrd['enablelqe']}
1073 80ce93c6 Scott Ullrich
1074
# Link quality window size
1075
# Defaults to 10
1076
1077 6d0433a5 Scott Ullrich
LinkQualityWinSize	10
1078 80ce93c6 Scott Ullrich
1079 c7f44ae0 Scott Ullrich
# Polling rate in seconds(float).
1080 80ce93c6 Scott Ullrich
# Default value 0.05 sec
1081
1082 6d0433a5 Scott Ullrich
Pollrate	0.05
1083 80ce93c6 Scott Ullrich
1084
1085
# TC redundancy
1086
# Specifies how much neighbor info should
1087
# be sent in TC messages
1088
# Possible values are:
1089
# 0 - only send MPR selectors
1090
# 1 - send MPR selectors and MPRs
1091
# 2 - send all neighbors
1092
#
1093
# defaults to 0
1094
1095 6d0433a5 Scott Ullrich
TcRedundancy	2
1096 80ce93c6 Scott Ullrich
1097
#
1098
# MPR coverage
1099
# Specifies how many MPRs a node should
1100
# try select to reach every 2 hop neighbor
1101
#
1102
# Can be set to any integer >0
1103
#
1104
# defaults to 1
1105
1106 6d0433a5 Scott Ullrich
MprCoverage	3
1107 80ce93c6 Scott Ullrich
1108 db5fbdb7 Scott Ullrich
# Example plugin entry with parameters:
1109 80ce93c6 Scott Ullrich
1110 db5fbdb7 Scott Ullrich
EODA;
1111 80ce93c6 Scott Ullrich
1112 1a2369f4 Scott Ullrich
if($olsrd['enablehttpinfo'] == "on") {
1113 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODB
1114
1115
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1116
{
1117 78e07da7 Scott Ullrich
    PlParam     "port"   "{$olsrd['port']}"
1118
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1119 db5fbdb7 Scott Ullrich
}
1120 80ce93c6 Scott Ullrich
1121 db5fbdb7 Scott Ullrich
EODB;
1122
1123
}
1124
1125 aac0e00c Scott Ullrich
if($olsrd['enabledsecure'] == "on") {
1126 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODC
1127
1128 e2894d7f Scott Ullrich
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1129 db5fbdb7 Scott Ullrich
{
1130
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1131
}
1132 80ce93c6 Scott Ullrich
1133 db5fbdb7 Scott Ullrich
EODC;
1134
1135
}
1136
1137 1a2369f4 Scott Ullrich
if($olsrd['enabledyngw'] == "on") {
1138 10c6e7a8 Scott Ullrich
1139
	/* unset default route, olsr auto negotiates */
1140
	mwexec("/sbin/route delete default");
1141 c7f44ae0 Scott Ullrich
1142 db5fbdb7 Scott Ullrich
	$olsr .= <<<EODE
1143
1144
LoadPlugin "/usr/local/lib/olsrd_dyn_gw.so.0.4"
1145
{
1146
    # how often to look for a inet gw, in seconds
1147
    # defaults to 5 secs, if commented out
1148 78e07da7 Scott Ullrich
    PlParam     "Interval"   "{$olsrd['polling']}"
1149 80ce93c6 Scott Ullrich
1150
    # if one or more IPv4 addresses are given, do a ping on these in
1151
    # descending order to validate that there is not only an entry in
1152
    # routing table, but also a real internet connection. If any of
1153
    # these addresses could be pinged successfully, the test was
1154
    # succesful, i.e. if the ping on the 1st address was successful,the
1155
    # 2nd won't be pinged
1156 78e07da7 Scott Ullrich
    PlParam     "Ping"       "{$olsrd['ping']}"
1157 db5fbdb7 Scott Ullrich
    #PlParam     "HNA"   "192.168.81.0 255.255.255.0"
1158
}
1159 80ce93c6 Scott Ullrich
1160 db5fbdb7 Scott Ullrich
EODE;
1161 80ce93c6 Scott Ullrich
1162 db5fbdb7 Scott Ullrich
}
1163 80ce93c6 Scott Ullrich
1164 a4904847 Scott Ullrich
foreach($config['installedpackages']['olsrd']['config'] as $conf) {
1165 c7f44ae0 Scott Ullrich
	$interfaces = explode(',', $conf['iface_array']);
1166
	foreach($interfaces as $interface) {
1167 015b7184 Scott Ullrich
		$realinterface = convert_friendly_interface_to_real_interface_name($interface);
1168 c7f44ae0 Scott Ullrich
$olsr .= <<<EODAD
1169
Interface "{$realinterface}"
1170 80ce93c6 Scott Ullrich
{
1171
1172
    # Hello interval in seconds(float)
1173 6d0433a5 Scott Ullrich
    HelloInterval    2.0
1174 80ce93c6 Scott Ullrich
1175
    # HELLO validity time
1176 6d0433a5 Scott Ullrich
    HelloValidityTime	20.0
1177 80ce93c6 Scott Ullrich
1178
    # TC interval in seconds(float)
1179 6d0433a5 Scott Ullrich
    TcInterval        5.0
1180 80ce93c6 Scott Ullrich
1181
    # TC validity time
1182 6d0433a5 Scott Ullrich
    TcValidityTime	30.0
1183 80ce93c6 Scott Ullrich
1184
    # MID interval in seconds(float)
1185 6d0433a5 Scott Ullrich
    MidInterval	5.0
1186 80ce93c6 Scott Ullrich
1187
    # MID validity time
1188 6d0433a5 Scott Ullrich
    MidValidityTime	30.0
1189 80ce93c6 Scott Ullrich
1190
    # HNA interval in seconds(float)
1191 6d0433a5 Scott Ullrich
    HnaInterval	5.0
1192 80ce93c6 Scott Ullrich
1193
    # HNA validity time
1194 6d0433a5 Scott Ullrich
    HnaValidityTime 	30.0
1195 80ce93c6 Scott Ullrich
1196
    # When multiple links exist between hosts
1197
    # the weight of interface is used to determine
1198
    # the link to use. Normally the weight is
1199
    # automatically calculated by olsrd based
1200
    # on the characteristics of the interface,
1201
    # but here you can specify a fixed value.
1202
    # Olsrd will choose links with the lowest value.
1203
1204
    # Weight 0
1205
1206
1207
}
1208 ac809adb Scott Ullrich
1209 c7f44ae0 Scott Ullrich
EODAD;
1210 ac809adb Scott Ullrich
1211
	}
1212 d70f19da Scott Ullrich
	break;
1213 ac809adb Scott Ullrich
}
1214 db5fbdb7 Scott Ullrich
		fwrite($fd, $olsr);
1215
		fclose($fd);
1216
	}
1217 c7f44ae0 Scott Ullrich
1218 5b991209 Scott Ullrich
	if(is_process_running("olsrd"))
1219 97fd5cb8 Scott Ullrich
		mwexec("/usr/bin/killall olsrd");
1220
1221
	sleep(2);
1222
1223
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/olsr.conf");
1224 c7f44ae0 Scott Ullrich
1225 eebeaf0d Scott Ullrich
	conf_mount_ro();
1226 80ce93c6 Scott Ullrich
}
1227
1228 1071e028 Scott Ullrich
/* configure cron service */
1229
function configure_cron() {
1230
	global $g, $config;
1231 251ca022 Scott Ullrich
	conf_mount_rw();
1232 1071e028 Scott Ullrich
	/* preserve existing crontab entries */
1233
	$crontab_contents = file_get_contents("/etc/crontab");
1234
	$crontab_contents_a = split("\n", $crontab_contents);
1235
	
1236
	for ($i = 0; $i < count($crontab_contents_a); $i++) {
1237
		$item =& $crontab_contents_a[$i];
1238
		if (strpos($item, "# pfSense specific crontab entries") !== false) {
1239
			array_splice($crontab_contents_a, $i - 1);
1240
			break;
1241
		}
1242
	}
1243
	$crontab_contents = implode("\n", $crontab_contents_a) . "\n";
1244
	
1245
	
1246
	if (is_array($config['cron']['item'])) {
1247
		$crontab_contents .= "#\n";
1248
		$crontab_contents .= "# pfSense specific crontab entries\n";
1249
		$crontab_contents .= "# Created: " . date("F j, Y, g:i a") . "\n";
1250
		$crontab_contents .= "#\n";
1251
1252
		foreach ($config['cron']['item'] as $item) {
1253
			$crontab_contents .= "\n{$item['minute']}\t";
1254
			$crontab_contents .= "{$item['hour']}\t";
1255
			$crontab_contents .= "{$item['mday']}\t";
1256
			$crontab_contents .= "{$item['month']}\t";
1257
			$crontab_contents .= "{$item['wday']}\t";
1258
			$crontab_contents .= "{$item['who']}\t";
1259
			$crontab_contents .= "{$item['command']}";
1260
		}
1261
    
1262
		$crontab_contents .= "\n#\n";
1263
		$crontab_contents .= "# If possible do not add items to this file manually.\n";
1264
		$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
1265
		$crontab_contents .= "#\n\n";
1266
	}
1267
	
1268
	/* please maintain the newline at the end of file */
1269
	file_put_contents("/etc/crontab", $crontab_contents);
1270
	
1271
	if (!$g['booting'])
1272
		conf_mount_ro();
1273
}
1274
1275 6f20377b Scott Ullrich
function upnp_start() {
1276
	if($config['installedpackages']['miniupnpd']['config'][0]["enable"]) {
1277
		mwexec_bg("/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf");
1278
	}
1279
}
1280
1281 cbe2ebe0 Scott Ullrich
?>