Project

General

Profile

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