Project

General

Profile

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