Project

General

Profile

Download (33.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services.inc
5
	part of the pfSense project (http://www.pfsense.com)
6

    
7
	originally part of m0n0wall (http://m0n0.ch/wall)
8
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
9
	All rights reserved.
10

    
11
	Redistribution and use in source and binary forms, with or without
12
	modification, are permitted provided that the following conditions are met:
13

    
14
	1. Redistributions of source code must retain the above copyright notice,
15
	   this list of conditions and the following disclaimer.
16

    
17
	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

    
21
	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
/*
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
	pfSense_BUILDER_BINARIES:	/usr/sbin/bsnmpd	/sbin/route	/usr/local/sbin/olsrd
37
	pfSense_BUILDER_BINARIES:	/usr/local/sbin/miniupnpd	
38
	pfSense_MODULE:	utils
39
*/
40

    
41
function services_parse_dhcpd_hostnames() {
42
	global $g, $config;
43

    
44
	// Launch if option enabled
45
	if (isset($config['dnsmasq']['regdhcp'])) {
46
		/* Make sure we do not error out */
47
		@touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
48
		if (!file_exists("{$g['varetc_path']}/hosts"))
49
			system_hosts_generate();
50
		if (file_exists("{$g['varrun_path']}/dhcpleases.pid"))
51
			sigkillbypid("{$g['varrun_path']}/dhcpleases.pid", "HUP");
52
		else
53
			mwexec("/usr/local/sbin/dhcpleases -l {$g['dhcpd_chroot_path']}/var/db/dhcpd.leases -d {$config['system']['domain']} -p {$g['varrun_path']}/dnsmasq.pid -h {$g['varetc_path']}/hosts");
54
	} else {
55
		sigkillbypid("{$g['varrun_path']}/dhcpleases.pid", "TERM");
56
		@unlink("{$g['varrun_path']}/dhcpleases.pid");
57
	}
58
		
59
}
60

    
61
function services_dhcpd_configure() {
62
	global $config, $g;
63
	
64
	if($g['services_dhcp_server_enable'] == false) 
65
		return;
66

    
67
	if(isset($config['system']['developerspew'])) {
68
		$mt = microtime();
69
		echo "services_dhcpd_configure($if) being called $mt\n";
70
	}
71
	
72
	/* kill any running dhcpd */
73
	if(is_process_running("dhcpd"))
74
		mwexec("killall dhcpd", true);
75

    
76
	/* DHCP enabled on any interfaces? */
77
	if (!is_dhcp_server_enabled())
78
		return 0;
79

    
80
	/* if OLSRD is enabled, allow WAN to house DHCP. */
81
	if($config['installedpackages']['olsrd'])
82
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd)
83
				if($olsrd['enable'])
84
					$is_olsr_enabled = true;
85

    
86
	/* configure DHCPD chroot */
87
	$fd = fopen("{$g['tmp_path']}/dhcpd.sh","w");
88
	$status = `mount | grep "{$g['dhcpd_chroot_path']}/dev"`;
89
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}\n");
90
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
91
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
92
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
93
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
94
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/run\n");	
95
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
96
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
97
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/run\n");
98
	fwrite($fd, "chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
99
	fwrite($fd, "cp /lib/libc.so.* {$g['dhcpd_chroot_path']}/lib/\n");
100
	fwrite($fd, "cp /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
101
	fwrite($fd, "chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
102
	if(!trim($status))
103
		fwrite($fd, "mount -t devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
104
	fclose($fd);
105
	mwexec("/bin/sh {$g['tmp_path']}/dhcpd.sh");
106

    
107
	$syscfg = $config['system'];
108
	$dhcpdcfg = $config['dhcpd'];
109
	$Iflist = get_configured_interface_list();
110
		
111
	if ($g['booting'])
112
		echo "Starting DHCP service...";
113
	else
114
		sleep(1);
115

    
116
	/* write dhcpd.conf */
117
	$fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w");
118
	if (!$fd) {
119
		printf("Error: cannot open dhcpd.conf in services_dhcpd_configure().\n");
120
		return 1;
121
	}
122

    
123
	$optcounter = 0;
124
	$custoptions = "";
125
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {	
126
		if(is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
127
			foreach($dhcpifconf['numberoptions']['item'] as $item) {
128
				$custoptions .= "option custom-opt-$optcounter code {$item['number']} = text;\n";
129
				$optcounter++;
130
			}
131
		}
132
	}
133

    
134
	$dhcpdconf = <<<EOD
135
	
136
option domain-name "{$syscfg['domain']}";
137
option ldap-server code 95 = text;
138
option domain-search-list code 119 = text;
139
{$custoptions}
140
default-lease-time 7200;
141
max-lease-time 86400;
142
log-facility local7;
143
ddns-update-style none;
144
one-lease-per-client true;
145
deny duplicates;
146
ping-check true;
147

    
148
EOD;
149

    
150
	if(isset($dhcpifconf['alwaysbroadcast'])) 
151
		$dhcpdconf .= "always-broadcast on\n";
152

    
153
	$dhcpdifs = array();
154

    
155
	/*    loop through and determine if we need to setup
156
	 *    failover peer "bleh" entries
157
	 */
158
	$dhcpnum = 0;
159
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
160

    
161
		if (!isset($dhcpifconf['enable']))
162
			continue;
163

    
164
		if(!isset($dhcpifconf['disableauthoritative']))
165
			$dhcpdconf .= "authoritative;\n";
166

    
167
		if($dhcpifconf['failover_peerip'] <> "") {
168
			/*
169
			 *    yep, failover peer is defined.
170
			 *    does it match up to a defined vip?
171
			 */
172
			$skew = 110;
173
			$a_vip = &$config['virtualip']['vip'];
174
			if(is_array($a_vip)) {
175
				foreach ($a_vip as $vipent) {
176
					$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
177
					$intip = find_interface_ip($int);
178
					$real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif);
179
					if($int == $real_dhcpif) {
180
						/* this is the interface! */
181
						if($vipent['advskew'] < "20")
182
							$skew = 0;
183
					}
184
				}
185
			} else {
186
				log_error("Warning!  DHCP Failover setup and no CARP virtual IP's defined!");
187
			}
188
			if($skew > 10) {
189
				$type = "secondary";
190
				$dhcpdconf_pri  = "mclt 600;\n";
191
				$my_port = "520";
192
				$peer_port = "519";
193
			} else {
194
				$my_port = "519";
195
				$peer_port = "520";
196
				$type = "primary";
197
				$dhcpdconf_pri  = "split 128;\n";
198
				$dhcpdconf_pri .= "  mclt 600;\n";
199
			}
200
			$dhcpdconf .= <<<EOPP
201
failover peer "dhcp{$dhcpnum}" {
202
  {$type};
203
  address {$intip};
204
  port {$my_port};
205
  peer address {$dhcpifconf['failover_peerip']};
206
  peer port {$peer_port};
207
  max-response-delay 10;
208
  max-unacked-updates 10;
209
  {$dhcpdconf_pri}
210
  load balance max seconds 3;
211
}
212

    
213
EOPP;
214
		$dhcpnum++;
215
		}
216
	}
217

    
218
	$dhcpnum = 0;
219

    
220
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
221

    
222
		$ifcfg = $config['interfaces'][$dhcpif];
223

    
224
		if (!isset($dhcpifconf['enable']) || !isset($Iflist[$dhcpif]))
225
			continue;
226
		$ifcfgip = get_interface_ip($dhcpif);
227
		$ifcfgsn = get_interface_subnet($dhcpif);
228
		$subnet = gen_subnet($ifcfgip, $ifcfgsn);
229
		$subnetmask = gen_subnet_mask($ifcfgsn);
230

    
231
		if($is_olsr_enabled == true)
232
			if($dhcpifconf['netmask'])
233
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
234

    
235
		$dnscfg = "";
236

    
237
		if ($dhcpifconf['domain']) {
238
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
239
		}
240
		
241
    		if($dhcpifconf['domainsearchlist'] <> "") {
242
			$dnscfg .= "	option domain-search-list \"{$dhcpifconf['domainsearchlist']}\";\n";
243
    		}
244

    
245
		if (isset($dhcpifconf['ddnsupdate'])) {
246
			if($dhcpifconf['ddnsdomain'] <> "") {
247
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
248
			}
249
			$dnscfg .= "	ddns-update-style interim;\n";
250
		}
251

    
252
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
253
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
254
		} else if (isset($config['dnsmasq']['enable'])) {
255
			$dnscfg .= "	option domain-name-servers {$ifcfgip};";
256
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
257
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
258
		}
259

    
260
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
261
		$dhcpdconf .= "	pool {\n";
262

    
263
		/* is failover dns setup? */
264
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
265
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
266
			if($dhcpifconf['dnsserver'][1] <> "")
267
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
268
			$dhcpdconf .= ";\n";
269
		}
270

    
271
		if($dhcpifconf['failover_peerip'] <> "")
272
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
273

    
274
		if (isset($dhcpifconf['denyunknown']))
275
		   $dhcpdconf .= "		deny unknown clients;\n";
276

    
277
		if ($dhcpifconf['gateway'])
278
			$routers = $dhcpifconf['gateway'];
279
		else
280
			$routers = $ifcfgip;
281

    
282
		if($dhcpifconf['failover_peerip'] <> "") {
283
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
284
			$dhcpnum++;
285
		}
286

    
287
		$dhcpdconf .= <<<EOD
288
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
289
	}
290
	option routers {$routers};
291
$dnscfg
292

    
293
EOD;
294
    
295
		// default-lease-time
296
		if ($dhcpifconf['defaultleasetime'])
297
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
298

    
299
		// max-lease-time
300
		if ($dhcpifconf['maxleasetime'])
301
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
302

    
303
		// netbios-name*
304
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
305
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
306
			$dhcpdconf .= "	option netbios-node-type 8;\n";
307
		}
308

    
309
		// ntp-servers
310
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0])
311
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
312

    
313
		// tftp-server-name
314
		if ($dhcpifconf['tftp'] <> "")
315
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
316

    
317
		// Handle option, number rowhelper values
318
		$optcounter = 0;
319
		$dhcpdconf .= "\n";
320
		if($dhcpifconf['numberoptions']['item']) {
321
			foreach($dhcpifconf['numberoptions']['item'] as $item) {
322
				$dhcpdconf .= "	option custom-opt-$optcounter \"{$item['value']}\";\n";
323
				$optcounter++;
324
			}
325
		}
326

    
327
		// ldap-server
328
		if ($dhcpifconf['ldap'] <> "")
329
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
330

    
331
		// net boot information
332
		if(isset($dhcpifconf['netboot'])) {
333
			if (($dhcpifconf['next-server'] <> "") && ($dhcpifconf['filename'] <> "")) {
334
				$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
335
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
336
			}
337
			if ($dhcpifconf['rootpath'] <> "") {
338
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
339
      		}
340
		}
341
		
342
		$dhcpdconf .= <<<EOD
343
}
344

    
345
EOD;
346

    
347
		/* add static mappings */
348
		if (is_array($dhcpifconf['staticmap'])) {
349

    
350
			$i = 0;
351
			foreach ($dhcpifconf['staticmap'] as $sm) {
352
				$dhcpdconf .= <<<EOD
353
host s_{$dhcpif}_{$i} {
354
	hardware ethernet {$sm['mac']};
355

    
356
EOD;
357
				if ($sm['ipaddr'])
358
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
359

    
360
				if ($sm['hostname']) {
361
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
362
					$dhhostname = str_replace(".", "_", $dhhostname);
363
					$dhcpdconf .= "	option host-name {$dhhostname};\n";
364
				}
365

    
366
				$dhcpdconf .= "}\n";
367
				$i++;
368
			}
369
		}
370

    
371
		$dhcpdifs[] = get_real_interface($dhcpif);
372
	}
373

    
374
	fwrite($fd, $dhcpdconf);
375
	fclose($fd);
376

    
377
	/* create an empty leases database */
378
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
379
	touch("{$g['varrun_path']}/dhcpd.pid");
380
	
381

    
382
	/* fire up dhcpd in a chroot */
383
	mwexec("/usr/local/sbin/dhcpd -user dhcpd -group _dhcp -chroot {$g['dhcpd_chroot_path']} -cf {$g['dhcpd_chroot_path']}/etc/dhcpd.conf " .
384
		join(" ", $dhcpdifs));
385

    
386
	if ($g['booting']) {
387
		print "done.\n";
388
	}
389

    
390
	return 0;
391
}
392

    
393
function services_igmpproxy_configure() {
394
        global $config, $g;
395

    
396
        $iflist = get_configured_interface_list();
397

    
398
        /* kill any running igmpproxy */
399
        killbyname("igmpproxy");
400

    
401
	if (!is_array($config['igmpproxy']['igmpentry']))
402
		return 1;
403

    
404
        $igmpconf = <<<EOD
405

    
406
##------------------------------------------------------
407
## Enable Quickleave mode (Sends Leave instantly)
408
##------------------------------------------------------
409
quickleave
410

    
411
EOD;
412

    
413
        foreach ($config['igmpproxy']['igmpentry'] as $igmpcf) {
414
                unset($iflist[$igmpcf['ifname']]);
415
                $realif = get_real_interface($igmpcf['ifname']);
416
                if (empty($igmpcf['threshold']))
417
                        $threshld = 1;
418
                else
419
                        $threshld = $igmpcf['threshold'];
420
                $igmpconf .= "phyint {$realif} {$igmpcf['type']} ratelimit 0 threshold {$threshld}\n";
421

    
422
                if ($igmpcf['address'] <> "") {
423
                        $item = explode(" ", $igmpcf['address']);
424
                        foreach($item as $iww)
425
                                $igmpconf .= "altnet {$iww}\n";
426
                }
427
                $igmpconf .= "\n";
428
        }
429
        foreach ($iflist as $ifn) {
430
                $realif = get_real_interface($ifn);
431
                $igmpconf .= "phyint {$realif} disabled\n";
432
        }
433

    
434
        $igmpfl = fopen($g['tmp_path'] . "/igmpproxy.conf", "w");
435
        if (!$igmpfl) {
436
                log_error("Could not write Igmpproxy configuration file!");
437
                return;
438
        }
439
        fwrite($igmpfl, $igmpconf);
440
        fclose($igmpfl);
441

    
442
        mwexec("/usr/local/sbin/igmpproxy -c " . $g['tmp_path'] . "/igmpproxy.conf");
443
        log_error("Started Igmpproxy service sucsesfully.");
444

    
445
        return 0;
446
}
447

    
448
function interfaces_staticarp_configure($if) {
449
	global $config, $g;
450
	if(isset($config['system']['developerspew'])) {
451
		$mt = microtime();
452
		echo "interfaces_staticarp_configure($if) being called $mt\n";
453
	}
454

    
455
        $ifcfg = $config['interfaces'][$if];
456

    
457
        /* Enable staticarp, if enabled */
458
        if(isset($config['dhcpd'][$if]['staticarp'])) {
459
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
460
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
461
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
462

    
463
                        foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
464
                                mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
465

    
466
                        }
467

    
468
                }
469
        } else {
470
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
471
                mwexec("/usr/sbin/arp -da > /dev/null 2>&1 ");
472
        }
473

    
474
        return 0;
475
}
476

    
477
function services_dhcrelay_configure() {
478
	global $config, $g;
479
	if(isset($config['system']['developerspew'])) {
480
		$mt = microtime();
481
		echo "services_dhcrelay_configure() being called $mt\n";
482
	}
483

    
484
	/* kill any running dhcrelay */
485
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
486

    
487
	$dhcrelaycfg = $config['dhcrelay'];
488

    
489
	/* DHCPRelay enabled on any interfaces? */
490
	$dhcrelayenable = false;
491
	if(is_array($dhcrelaycfg)) {
492
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
493
			if (isset($dhcrelayifconf['enable']) &&
494
				(($dhcrelayif == "lan") ||
495
				(isset($config['interfaces'][$dhcrelayif]['enable']) &&
496
				$config['interfaces'][$dhcrelayif]['if'] && (!link_interface_to_bridge($dhcrelayif)))))
497
				$dhcrelayenable = true;
498
		}
499
	}
500

    
501
	if (!$dhcrelayenable)
502
		return 0;
503

    
504
	if ($g['booting'])
505
		echo "Starting DHCP relay service...";
506
	else
507
		sleep(1);
508

    
509
	$dhcrelayifs = array();
510
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
511

    
512
		$ifcfg = $config['interfaces'][$dhcrelayif];
513

    
514
		if (!isset($dhcrelayifconf['enable']) ||
515
			(($dhcrelayif != "lan") &&
516
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || 
517
			link_interface_to_bridge($dhcrelayif))))
518
			continue;
519

    
520
		$dhcrelayifs[] = get_real_interface($dhcrelayif);
521
	}
522

    
523
	/* In order for the relay to work, it needs to be active on the
524
	   interface in which the destination server sits */
525
	$iflist = get_configured_interface_list();
526
	foreach ($iflist as $ifname) {
527
		$subnet = get_interface_ip($ifname) . "/" . get_interface_subnet($ifname);
528
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
529
			$destif = get_real_interface($ifname);
530
	}
531

    
532
	if (!isset($destif))
533
		$destif = $config['interfaces']['wan']['if'];
534

    
535
	$dhcrelayifs[] = $destif;
536
	$dhcrelayifs = array_unique($dhcrelayifs);
537

    
538
	/* fire up dhcrelay */
539
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
540

    
541
	if (isset($dhcrelaycfg['agentoption']))
542
		$cmd .=  " -a -m replace";
543

    
544
	$cmd .= " {$dhcrelaycfg['server']}";
545
	mwexec($cmd);
546

    
547
	return 0;
548
}
549

    
550
function services_dyndns_configure_client($conf) {
551

    
552
	if (!isset($conf['enable']))
553
		continue;
554

    
555
	/* load up the dyndns.class */
556
	require_once("dyndns.class");
557

    
558
	log_error("DynDns: Running updatedns()");
559

    
560
	$dns = new updatedns($dnsService = $conf['type'],
561
		$dnsHost = $conf['host'],
562
		$dnsUser = $conf['username'],
563
		$dnsPass = $conf['password'],
564
		$dnsWilcard = $conf['wildcard'],
565
		$dnsMX = $conf['mx'], 
566
		$dnsIf = "{$conf['interface']}");
567

    
568
}
569

    
570
function services_dyndns_configure($int = "") {
571
	global $config, $g;
572
	if(isset($config['system']['developerspew'])) {
573
		$mt = microtime();
574
		echo "services_dyndns_configure() being called $mt\n";
575
	}
576

    
577
	$dyndnscfg = $config['dyndnses']['dyndns'];
578

    
579
	if (is_array($dyndnscfg)) {
580
		if ($g['booting']) 
581
			echo "Starting DynDNS clients...";
582

    
583
		foreach ($dyndnscfg as $dyndns) {
584
			if (!empty($int) && $int != $dyndns['interface'])
585
				continue;
586

    
587
			services_dyndns_configure_client($dyndns);
588

    
589
			sleep(1);
590

    
591
			if (!empty($int))
592
				break;
593
		}
594

    
595
		if ($g['booting'])
596
			echo "done.\n";
597
	}
598

    
599
	return 0;
600
}
601

    
602
function services_dnsmasq_configure() {
603
	global $config, $g;
604
	$return = 0;
605
	
606
	if(isset($config['system']['developerspew'])) {
607
		$mt = microtime();
608
		echo "services_dnsmasq_configure() being called $mt\n";
609
	}
610

    
611
	/* kill any running dnsmasq */
612
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
613

    
614
	if (isset($config['dnsmasq']['enable'])) {
615

    
616
		if ($g['booting'])
617
			echo "Starting DNS forwarder...";
618
		else
619
			sleep(1);
620

    
621
		/* generate hosts file */
622
		if(system_hosts_generate()!=0)
623
			$return = 1;
624

    
625
		$args = "";
626

    
627
		if (isset($config['dnsmasq']['regdhcp'])) {
628
			$args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts ";
629
		}
630

    
631
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
632
			foreach($config['dnsmasq']['domainoverrides'] as $override) {
633
			        $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
634
			}
635
		}
636

    
637
		/* suppose that dnsmasq handles our domain and don't send
638
		requests for our local domain to upstream servers */
639
		//if (!empty($config['system']['domain'])) {
640
		//	$args .= sprintf(' --local=/%s/', $config['system']['domain']);
641
		//}
642

    
643
		/* run dnsmasq */
644
		mwexec("/usr/local/sbin/dnsmasq --local-ttl 1 --all-servers --dns-forward-max=5000 --cache-size=10000 {$args}");
645

    
646
		if ($g['booting'])
647
			echo "done.\n";
648
	}
649

    
650
	if (!$g['booting']) {
651
		if(services_dhcpd_configure()!=0)
652
			$return = 1;
653
	}
654

    
655
	// restart isc-dhcpd parser
656
	services_parse_dhcpd_hostnames();
657

    
658
	return $return;
659
}
660

    
661
function services_snmpd_configure() {
662
	global $config, $g;
663
	if(isset($config['system']['developerspew'])) {
664
		$mt = microtime();
665
		echo "services_snmpd_configure() being called $mt\n";
666
	}
667

    
668
	/* kill any running snmpd */
669
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
670
	if(is_process_running("bsnmpd")) 
671
		mwexec("/usr/bin/killall bsnmpd", true);
672

    
673
	if (isset($config['snmpd']['enable'])) {
674

    
675
		if ($g['booting'])
676
			echo "Starting SNMP daemon... ";
677

    
678
		/* generate snmpd.conf */
679
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
680
		if (!$fd) {
681
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
682
			return 1;
683
		}
684

    
685

    
686
		$snmpdconf = <<<EOD
687
location := "{$config['snmpd']['syslocation']}"
688
contact := "{$config['snmpd']['syscontact']}"
689
read := "{$config['snmpd']['rocommunity']}"
690

    
691
EOD;
692

    
693
/* No docs on what write strings do there for disable for now.
694
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
695
		    $snmpdconf .= <<<EOD
696
# write string
697
write := "{$config['snmpd']['rwcommunity']}"
698

    
699
EOD;
700
		}
701
*/
702

    
703

    
704
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
705
		    $snmpdconf .= <<<EOD
706
# SNMP Trap support.
707
traphost := {$config['snmpd']['trapserver']}
708
trapport := {$config['snmpd']['trapserverport']}
709
trap := "{$config['snmpd']['trapstring']}"
710

    
711

    
712
EOD;
713
		}
714

    
715

    
716
		$snmpdconf .= <<<EOD
717
system := 1     # pfSense
718
%snmpd
719
begemotSnmpdDebugDumpPdus       = 2
720
begemotSnmpdDebugSyslogPri      = 7
721
begemotSnmpdCommunityString.0.1 = $(read)
722

    
723
EOD;
724

    
725
/* No docs on what write strings do there for disable for now.
726
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
727
		    $snmpdconf .= <<<EOD
728
begemotSnmpdCommunityString.0.2 = $(write)
729

    
730
EOD;
731
		}
732
*/
733

    
734

    
735
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
736
		    $snmpdconf .= <<<EOD
737
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
738
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
739
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
740

    
741
EOD;
742
		}
743

    
744

    
745
		$snmpdconf .= <<<EOD
746
begemotSnmpdCommunityDisable    = 1
747

    
748
EOD;
749

    
750
		if(isset($config['snmpd']['bindlan'])) {
751
			$bind_to_ip = get_interface_ip("lan");
752
		} else {
753
			$bind_to_ip = "0.0.0.0";
754
		}
755

    
756
		if(is_port( $config['snmpd']['pollport'] )) {
757
		    $snmpdconf .= <<<EOD
758
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
759

    
760
EOD;
761

    
762
		}
763

    
764
		$snmpdconf .= <<<EOD
765
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
766
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
767

    
768
# These are bsnmp macros not php vars.
769
sysContact      = $(contact)
770
sysLocation     = $(location)
771
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
772

    
773
snmpEnableAuthenTraps = 2
774

    
775
EOD;
776

    
777
		if (is_array( $config['snmpd']['modules'] )) {
778
		    if(isset($config['snmpd']['modules']['mibii'])) {
779
			$snmpdconf .= <<<EOD
780
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
781

    
782
EOD;
783
		    }
784

    
785
		    if(isset($config['snmpd']['modules']['netgraph'])) {
786
			$snmpdconf .= <<<EOD
787
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
788
%netgraph
789
begemotNgControlNodeName = "snmpd"
790

    
791
EOD;
792
		    }
793

    
794
		    if(isset($config['snmpd']['modules']['pf'])) {
795
			$snmpdconf .= <<<EOD
796
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
797

    
798
EOD;
799
		    }
800

    
801
		    if(isset($config['snmpd']['modules']['hostres'])) {
802
			$snmpdconf .= <<<EOD
803
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
804

    
805
EOD;
806
		    }
807
		    if(isset($config['snmpd']['modules']['bridge'])) {
808
			$snmpdconf .= <<<EOD
809
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
810
# config must end with blank line
811

    
812

    
813
EOD;
814
		    }
815
		}
816

    
817
		fwrite($fd, $snmpdconf);
818
		fclose($fd);
819

    
820
		if (isset($config['snmpd']['bindlan'])) {
821
			$bindlan = "";
822
		}
823

    
824
		/* run bsnmpd */
825
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
826
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
827

    
828
		if ($g['booting'])
829
			echo "done.\n";
830
	}
831

    
832
	return 0;
833
}
834

    
835
function services_dnsupdate_process($int = "") {
836
	global $config, $g;
837
	if(isset($config['system']['developerspew'])) {
838
		$mt = microtime();
839
		echo "services_dnsupdate_process() being called $mt\n";
840
	}
841

    
842
	/* Dynamic DNS updating active? */
843
	if (is_array($config['dnsupdates']['dnsupdate'])) {
844
		foreach ($config['dnsupdates']['dnsupdate'] as $i => $dnsupdate) {
845
			if (!isset($dnsupdate['enable']))
846
				continue;
847
			if (!empty($int) && $int != $dnsupdate['interface'])
848
				continue;
849

    
850
			/* determine interface name */
851
			$if = get_real_interface($dnsupdate['interface']);
852
			$wanip = get_interface_ip($dnsupdate['interface']);
853
			if ($wanip) {
854

    
855
				$keyname = $dnsupdate['keyname'];
856
				/* trailing dot */
857
				if (substr($keyname, -1) != ".")
858
					$keyname .= ".";
859

    
860
				$hostname = $dnsupdate['host'];
861
				/* trailing dot */
862
				if (substr($hostname, -1) != ".")
863
					$hostname .= ".";
864

    
865
				/* write private key file
866
				   this is dumb - public and private keys are the same for HMAC-MD5,
867
				   but nsupdate insists on having both */
868
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.private", "w");
869
				$privkey .= <<<EOD
870
Private-key-format: v1.2
871
Algorithm: 157 (HMAC)
872
Key: {$dnsupdate['keydata']}
873

    
874
EOD;
875
				fwrite($fd, $privkey);
876
				fclose($fd);
877

    
878
				/* write public key file */
879
				if ($dnsupdate['keytype'] == "zone") {
880
					$flags = 257;
881
					$proto = 3;
882
				} else if ($dnsupdate['keytype'] == "host") {
883
					$flags = 513;
884
					$proto = 3;
885
				} else if ($dnsupdate['keytype'] == "user") {
886
					$flags = 0;
887
					$proto = 2;
888
				}
889

    
890
				$fd = fopen("{$g['varetc_path']}/K{$i}{$keyname}+157+00000.key", "w");
891
				fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$dnsupdate['keydata']}\n");
892
				fclose($fd);
893

    
894
				/* generate update instructions */
895
				$upinst = "";
896
				if (!empty($dnsupdate['server']))
897
					$upinst .= "server {$dnsupdate['server']}\n";
898
				$upinst .= "update delete {$dnsupdate['host']} A\n";
899
				$upinst .= "update add {$dnsupdate['host']} {$dnsupdate['ttl']} A {$wanip}\n";
900
				$upinst .= "\n";	/* mind that trailing newline! */
901

    
902
				$fd = fopen("{$g['varetc_path']}/nsupdatecmds{$i}", "w");
903
				fwrite($fd, $upinst);
904
				fclose($fd);
905

    
906
				/* invoke nsupdate */
907
				$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}/K{$i}{$keyname}+157+00000.key";
908
				if (isset($dnsupdate['usetcp']))
909
					$cmd .= " -v";
910
				$cmd .= " {$g['varetc_path']}/nsupdatecmds{$i}";
911
	
912
				mwexec_bg($cmd);
913
			}
914
		}
915
	}
916

    
917
	return 0;
918
}
919

    
920
function setup_wireless_olsr() {
921
	global $config, $g;
922
	if(!$config['installedpackages']['olsrd'] || !$config['installedpackages'])
923
		return;
924
	if(isset($config['system']['developerspew'])) {
925
		$mt = microtime();
926
		echo "setup_wireless_olsr($interface) being called $mt\n";
927
	}
928
	conf_mount_rw();
929
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
930
		$olsr_enable = $olsrd['enable'];
931
		if($olsr_enable <> "on")
932
			return;
933
		$fd = fopen("{$g['varetc_path']}/olsr.conf", "w");
934

    
935
		if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") {
936
			$enableannounce .= "\nHna4\n";
937
			$enableannounce .= "{\n";
938
		if($olsrd['announcedynamicroute'])
939
			$enableannounce .= "\t{$olsrd['announcedynamicroute']}\n";
940
		if($olsrd['enableannounce'] == "on")
941
			$enableannounce .= "0.0.0.0 0.0.0.0";
942
			$enableannounce .= "\n}\n";
943
		} else {
944
			$enableannounce = "";
945
		}
946

    
947
		$olsr .= <<<EODA
948
#
949
# olsr.org OLSR daemon config file
950
#
951
# Lines starting with a # are discarded
952
#
953
# This file was generated by setup_wireless_olsr() in services.inc
954
#
955

    
956
# This file is an example of a typical
957
# configuration for a mostly static
958
# network(regarding mobility) using
959
# the LQ extention
960

    
961
# Debug level(0-9)
962
# If set to 0 the daemon runs in the background
963

    
964
DebugLevel	2
965

    
966
# IP version to use (4 or 6)
967

    
968
IpVersion	4
969

    
970
# Clear the screen each time the internal state changes
971

    
972
ClearScreen     yes
973

    
974
{$enableannounce}
975

    
976
# Should olsrd keep on running even if there are
977
# no interfaces available? This is a good idea
978
# for a PCMCIA/USB hotswap environment.
979
# "yes" OR "no"
980

    
981
AllowNoInt	yes
982

    
983
# TOS(type of service) value for
984
# the IP header of control traffic.
985
# If not set it will default to 16
986

    
987
#TosValue	16
988

    
989
# The fixed willingness to use(0-7)
990
# If not set willingness will be calculated
991
# dynamically based on battery/power status
992
# if such information is available
993

    
994
#Willingness    	4
995

    
996
# Allow processes like the GUI front-end
997
# to connect to the daemon.
998

    
999
IpcConnect
1000
{
1001
     # Determines how many simultaneously
1002
     # IPC connections that will be allowed
1003
     # Setting this to 0 disables IPC
1004

    
1005
     MaxConnections  0
1006

    
1007
     # By default only 127.0.0.1 is allowed
1008
     # to connect. Here allowed hosts can
1009
     # be added
1010

    
1011
     Host            127.0.0.1
1012
     #Host            10.0.0.5
1013

    
1014
     # You can also specify entire net-ranges
1015
     # that are allowed to connect. Multiple
1016
     # entries are allowed
1017

    
1018
     #Net             192.168.1.0 255.255.255.0
1019
}
1020

    
1021
# Wether to use hysteresis or not
1022
# Hysteresis adds more robustness to the
1023
# link sensing but delays neighbor registration.
1024
# Used by default. 'yes' or 'no'
1025

    
1026
UseHysteresis	no
1027

    
1028
# Hysteresis parameters
1029
# Do not alter these unless you know
1030
# what you are doing!
1031
# Set to auto by default. Allowed
1032
# values are floating point values
1033
# in the interval 0,1
1034
# THR_LOW must always be lower than
1035
# THR_HIGH.
1036

    
1037
#HystScaling	0.50
1038
#HystThrHigh	0.80
1039
#HystThrLow	0.30
1040

    
1041

    
1042
# Link quality level
1043
# 0 = do not use link quality
1044
# 1 = use link quality for MPR selection
1045
# 2 = use link quality for MPR selection and routing
1046
# Defaults to 0
1047

    
1048
LinkQualityLevel	{$olsrd['enablelqe']}
1049

    
1050
# Link quality window size
1051
# Defaults to 10
1052

    
1053
LinkQualityWinSize	10
1054

    
1055
# Polling rate in seconds(float).
1056
# Default value 0.05 sec
1057

    
1058
Pollrate	0.05
1059

    
1060

    
1061
# TC redundancy
1062
# Specifies how much neighbor info should
1063
# be sent in TC messages
1064
# Possible values are:
1065
# 0 - only send MPR selectors
1066
# 1 - send MPR selectors and MPRs
1067
# 2 - send all neighbors
1068
#
1069
# defaults to 0
1070

    
1071
TcRedundancy	2
1072

    
1073
#
1074
# MPR coverage
1075
# Specifies how many MPRs a node should
1076
# try select to reach every 2 hop neighbor
1077
#
1078
# Can be set to any integer >0
1079
#
1080
# defaults to 1
1081

    
1082
MprCoverage	3
1083

    
1084
# Example plugin entry with parameters:
1085

    
1086
EODA;
1087

    
1088
if($olsrd['enablehttpinfo'] == "on") {
1089
	$olsr .= <<<EODB
1090

    
1091
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1092
{
1093
    PlParam     "port"   "{$olsrd['port']}"
1094
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1095
}
1096

    
1097
EODB;
1098

    
1099
}
1100

    
1101
if($olsrd['enabledsecure'] == "on") {
1102
	$olsr .= <<<EODC
1103

    
1104
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1105
{
1106
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1107
}
1108

    
1109
EODC;
1110

    
1111
}
1112

    
1113
if($olsrd['enabledyngw'] == "on") {
1114

    
1115
	/* unset default route, olsr auto negotiates */
1116
	mwexec("/sbin/route delete default");
1117

    
1118
	$olsr .= <<<EODE
1119

    
1120
LoadPlugin "/usr/local/lib/olsrd_dyn_gw.so.0.4"
1121
{
1122
    # how often to look for a inet gw, in seconds
1123
    # defaults to 5 secs, if commented out
1124
    PlParam     "Interval"   "{$olsrd['polling']}"
1125

    
1126
    # if one or more IPv4 addresses are given, do a ping on these in
1127
    # descending order to validate that there is not only an entry in
1128
    # routing table, but also a real internet connection. If any of
1129
    # these addresses could be pinged successfully, the test was
1130
    # succesful, i.e. if the ping on the 1st address was successful,the
1131
    # 2nd won't be pinged
1132
    PlParam     "Ping"       "{$olsrd['ping']}"
1133
    #PlParam     "HNA"   "192.168.81.0 255.255.255.0"
1134
}
1135

    
1136
EODE;
1137

    
1138
}
1139

    
1140
foreach($config['installedpackages']['olsrd']['config'] as $conf) {
1141
	$interfaces = explode(',', $conf['iface_array']);
1142
	foreach($interfaces as $interface) {
1143
		$realinterface = convert_friendly_interface_to_real_interface_name($interface);
1144
$olsr .= <<<EODAD
1145
Interface "{$realinterface}"
1146
{
1147

    
1148
    # Hello interval in seconds(float)
1149
    HelloInterval    2.0
1150

    
1151
    # HELLO validity time
1152
    HelloValidityTime	20.0
1153

    
1154
    # TC interval in seconds(float)
1155
    TcInterval        5.0
1156

    
1157
    # TC validity time
1158
    TcValidityTime	30.0
1159

    
1160
    # MID interval in seconds(float)
1161
    MidInterval	5.0
1162

    
1163
    # MID validity time
1164
    MidValidityTime	30.0
1165

    
1166
    # HNA interval in seconds(float)
1167
    HnaInterval	5.0
1168

    
1169
    # HNA validity time
1170
    HnaValidityTime 	30.0
1171

    
1172
    # When multiple links exist between hosts
1173
    # the weight of interface is used to determine
1174
    # the link to use. Normally the weight is
1175
    # automatically calculated by olsrd based
1176
    # on the characteristics of the interface,
1177
    # but here you can specify a fixed value.
1178
    # Olsrd will choose links with the lowest value.
1179

    
1180
    # Weight 0
1181

    
1182

    
1183
}
1184

    
1185
EODAD;
1186

    
1187
	}
1188
	break;
1189
}
1190
		fwrite($fd, $olsr);
1191
		fclose($fd);
1192
	}
1193

    
1194
	if(is_process_running("olsrd"))
1195
		mwexec("/usr/bin/killall olsrd", true);
1196

    
1197
	sleep(2);
1198

    
1199
	mwexec_bg("/usr/local/sbin/olsrd -f {$g['varetc_path']}/olsr.conf");
1200

    
1201
	conf_mount_ro();
1202
}
1203

    
1204
/* configure cron service */
1205
function configure_cron() {
1206
	global $g, $config;
1207
	conf_mount_rw();
1208
	/* preserve existing crontab entries */
1209
	$crontab_contents = file_get_contents("/etc/crontab");
1210
	$crontab_contents_a = split("\n", $crontab_contents);
1211
	
1212
	for ($i = 0; $i < count($crontab_contents_a); $i++) {
1213
		$item =& $crontab_contents_a[$i];
1214
		if (strpos($item, "# pfSense specific crontab entries") !== false) {
1215
			array_splice($crontab_contents_a, $i - 1);
1216
			break;
1217
		}
1218
	}
1219
	$crontab_contents = implode("\n", $crontab_contents_a) . "\n";
1220
	
1221
	
1222
	if (is_array($config['cron']['item'])) {
1223
		$crontab_contents .= "#\n";
1224
		$crontab_contents .= "# pfSense specific crontab entries\n";
1225
		$crontab_contents .= "# Created: " . date("F j, Y, g:i a") . "\n";
1226
		$crontab_contents .= "#\n";
1227

    
1228
		foreach ($config['cron']['item'] as $item) {
1229
			$crontab_contents .= "\n{$item['minute']}\t";
1230
			$crontab_contents .= "{$item['hour']}\t";
1231
			$crontab_contents .= "{$item['mday']}\t";
1232
			$crontab_contents .= "{$item['month']}\t";
1233
			$crontab_contents .= "{$item['wday']}\t";
1234
			$crontab_contents .= "{$item['who']}\t";
1235
			$crontab_contents .= "{$item['command']}";
1236
		}
1237
    
1238
		$crontab_contents .= "\n#\n";
1239
		$crontab_contents .= "# If possible do not add items to this file manually.\n";
1240
		$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
1241
		$crontab_contents .= "#\n\n";
1242
	}
1243
	
1244
	/* please maintain the newline at the end of file */
1245
	file_put_contents("/etc/crontab", $crontab_contents);
1246
	
1247
	conf_mount_ro();
1248
}
1249

    
1250
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
				mwexec('killall miniupnpd 2>/dev/null', true);
1259
			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
function upnp_start() {
1270
	global $config, $g;
1271

    
1272
	if(!isset($config['installedpackages']['miniupnpd']['config']))
1273
		return;
1274

    
1275
	if($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
1276
		if($g['booting']) {
1277
			echo "Starting UPnP service... ";
1278
			require_once('/usr/local/pkg/miniupnpd.inc');
1279
			sync_package_miniupnpd();
1280
			echo "done.\n";
1281
		}
1282
		else {
1283
			upnp_action('start');
1284
		}
1285
	}
1286
}
1287

    
1288
?>
(36-36/50)