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
	$custoptions = "";
124
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {	
125
		if(is_array($dhcpifconf['numberoptions']) && is_array($dhcpifconf['numberoptions']['item'])) {
126
			foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
127
				$custoptions .= "option custom-{$dhcpif}-{$itemidx} code {$item['number']} = text;\n";
128
			}
129
		}
130
	}
131

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

    
146
EOD;
147

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

    
151
	$dhcpdifs = array();
152

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

    
159
		if (!isset($dhcpifconf['enable']))
160
			continue;
161

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

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

    
211
EOPP;
212
		$dhcpnum++;
213
		}
214
	}
215

    
216
	$dhcpnum = 0;
217

    
218
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
219

    
220
		$ifcfg = $config['interfaces'][$dhcpif];
221

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

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

    
233
		$dnscfg = "";
234

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

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

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

    
258
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
259
		$dhcpdconf .= "	pool {\n";
260

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

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

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

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

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

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

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

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

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

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

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

    
315
		// Handle option, number rowhelper values
316
		$dhcpdconf .= "\n";
317
		if($dhcpifconf['numberoptions']['item']) {
318
			foreach($dhcpifconf['numberoptions']['item'] as $itemidx => $item) {
319
				$dhcpdconf .= "	option custom-{$dhcpif}-{$itemidx} \"{$item['value']}\";\n";
320
			}
321
		}
322

    
323
		// ldap-server
324
		if ($dhcpifconf['ldap'] <> "")
325
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
326

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

    
341
EOD;
342

    
343
		/* add static mappings */
344
		if (is_array($dhcpifconf['staticmap'])) {
345

    
346
			$i = 0;
347
			foreach ($dhcpifconf['staticmap'] as $sm) {
348
				$dhcpdconf .= <<<EOD
349
host s_{$dhcpif}_{$i} {
350
	hardware ethernet {$sm['mac']};
351

    
352
EOD;
353
				if ($sm['ipaddr'])
354
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
355

    
356
				if ($sm['hostname']) {
357
					$dhhostname = str_replace(" ", "_", $sm['hostname']);
358
					$dhhostname = str_replace(".", "_", $dhhostname);
359
					$dhcpdconf .= "	option host-name {$dhhostname};\n";
360
				}
361

    
362
				$dhcpdconf .= "}\n";
363
				$i++;
364
			}
365
		}
366

    
367
		$dhcpdifs[] = get_real_interface($dhcpif);
368
	}
369

    
370
	fwrite($fd, $dhcpdconf);
371
	fclose($fd);
372

    
373
	/* create an empty leases database */
374
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
375
	touch("{$g['varrun_path']}/dhcpd.pid");
376
	
377

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

    
382
	if ($g['booting']) {
383
		print "done.\n";
384
	}
385

    
386
	return 0;
387
}
388

    
389
function services_igmpproxy_configure() {
390
        global $config, $g;
391

    
392
        $iflist = get_configured_interface_list();
393

    
394
        /* kill any running igmpproxy */
395
        killbyname("igmpproxy");
396

    
397
	if (!is_array($config['igmpproxy']['igmpentry']))
398
		return 1;
399

    
400
        $igmpconf = <<<EOD
401

    
402
##------------------------------------------------------
403
## Enable Quickleave mode (Sends Leave instantly)
404
##------------------------------------------------------
405
quickleave
406

    
407
EOD;
408

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

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

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

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

    
441
        return 0;
442
}
443

    
444
function interfaces_staticarp_configure($if) {
445
	global $config, $g;
446
	if(isset($config['system']['developerspew'])) {
447
		$mt = microtime();
448
		echo "interfaces_staticarp_configure($if) being called $mt\n";
449
	}
450

    
451
        $ifcfg = $config['interfaces'][$if];
452

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

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

    
462
                        }
463

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

    
470
        return 0;
471
}
472

    
473
function services_dhcrelay_configure() {
474
	global $config, $g;
475
	if(isset($config['system']['developerspew'])) {
476
		$mt = microtime();
477
		echo "services_dhcrelay_configure() being called $mt\n";
478
	}
479

    
480
	/* kill any running dhcrelay */
481
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
482

    
483
	$dhcrelaycfg = $config['dhcrelay'];
484

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

    
497
	if (!$dhcrelayenable)
498
		return 0;
499

    
500
	if ($g['booting'])
501
		echo "Starting DHCP relay service...";
502
	else
503
		sleep(1);
504

    
505
	$dhcrelayifs = array();
506
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
507

    
508
		$ifcfg = $config['interfaces'][$dhcrelayif];
509

    
510
		if (!isset($dhcrelayifconf['enable']) ||
511
			(($dhcrelayif != "lan") &&
512
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || 
513
			link_interface_to_bridge($dhcrelayif))))
514
			continue;
515

    
516
		$dhcrelayifs[] = get_real_interface($dhcrelayif);
517
	}
518

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

    
528
	if (!isset($destif))
529
		$destif = $config['interfaces']['wan']['if'];
530

    
531
	$dhcrelayifs[] = $destif;
532
	$dhcrelayifs = array_unique($dhcrelayifs);
533

    
534
	/* fire up dhcrelay */
535
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
536

    
537
	if (isset($dhcrelaycfg['agentoption']))
538
		$cmd .=  " -a -m replace";
539

    
540
	$cmd .= " {$dhcrelaycfg['server']}";
541
	mwexec($cmd);
542

    
543
	return 0;
544
}
545

    
546
function services_dyndns_configure_client($conf) {
547

    
548
	if (!isset($conf['enable']))
549
		continue;
550

    
551
	/* load up the dyndns.class */
552
	require_once("dyndns.class");
553

    
554
	log_error("DynDns: Running updatedns()");
555

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

    
564
}
565

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

    
573
	$dyndnscfg = $config['dyndnses']['dyndns'];
574

    
575
	if (is_array($dyndnscfg)) {
576
		if ($g['booting']) 
577
			echo "Starting DynDNS clients...";
578

    
579
		foreach ($dyndnscfg as $dyndns) {
580
			if (!empty($int) && $int != $dyndns['interface'])
581
				continue;
582

    
583
			services_dyndns_configure_client($dyndns);
584

    
585
			sleep(1);
586

    
587
			if (!empty($int))
588
				break;
589
		}
590

    
591
		if ($g['booting'])
592
			echo "done.\n";
593
	}
594

    
595
	return 0;
596
}
597

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

    
607
	/* kill any running dnsmasq */
608
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
609

    
610
	if (isset($config['dnsmasq']['enable'])) {
611

    
612
		if ($g['booting'])
613
			echo "Starting DNS forwarder...";
614
		else
615
			sleep(1);
616

    
617
		/* generate hosts file */
618
		if(system_hosts_generate()!=0)
619
			$return = 1;
620

    
621
		$args = "";
622

    
623
		if (isset($config['dnsmasq']['regdhcp'])) {
624
			$args .= " --dhcp-hostsfile={$g['varetc_path']}/hosts ";
625
		}
626

    
627
		if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
628
			foreach($config['dnsmasq']['domainoverrides'] as $override) {
629
			        $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
630
			}
631
		}
632

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

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

    
642
		if ($g['booting'])
643
			echo "done.\n";
644
	}
645

    
646
	if (!$g['booting']) {
647
		if(services_dhcpd_configure()!=0)
648
			$return = 1;
649
	}
650

    
651
	// restart isc-dhcpd parser
652
	services_parse_dhcpd_hostnames();
653

    
654
	return $return;
655
}
656

    
657
function services_snmpd_configure() {
658
	global $config, $g;
659
	if(isset($config['system']['developerspew'])) {
660
		$mt = microtime();
661
		echo "services_snmpd_configure() being called $mt\n";
662
	}
663

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

    
669
	if (isset($config['snmpd']['enable'])) {
670

    
671
		if ($g['booting'])
672
			echo "Starting SNMP daemon... ";
673

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

    
681

    
682
		$snmpdconf = <<<EOD
683
location := "{$config['snmpd']['syslocation']}"
684
contact := "{$config['snmpd']['syscontact']}"
685
read := "{$config['snmpd']['rocommunity']}"
686

    
687
EOD;
688

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

    
695
EOD;
696
		}
697
*/
698

    
699

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

    
707

    
708
EOD;
709
		}
710

    
711

    
712
		$snmpdconf .= <<<EOD
713
system := 1     # pfSense
714
%snmpd
715
begemotSnmpdDebugDumpPdus       = 2
716
begemotSnmpdDebugSyslogPri      = 7
717
begemotSnmpdCommunityString.0.1 = $(read)
718

    
719
EOD;
720

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

    
726
EOD;
727
		}
728
*/
729

    
730

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

    
737
EOD;
738
		}
739

    
740

    
741
		$snmpdconf .= <<<EOD
742
begemotSnmpdCommunityDisable    = 1
743

    
744
EOD;
745

    
746
		if(isset($config['snmpd']['bindlan'])) {
747
			$bind_to_ip = get_interface_ip("lan");
748
		} else {
749
			$bind_to_ip = "0.0.0.0";
750
		}
751

    
752
		if(is_port( $config['snmpd']['pollport'] )) {
753
		    $snmpdconf .= <<<EOD
754
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
755

    
756
EOD;
757

    
758
		}
759

    
760
		$snmpdconf .= <<<EOD
761
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
762
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
763

    
764
# These are bsnmp macros not php vars.
765
sysContact      = $(contact)
766
sysLocation     = $(location)
767
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
768

    
769
snmpEnableAuthenTraps = 2
770

    
771
EOD;
772

    
773
		if (is_array( $config['snmpd']['modules'] )) {
774
		    if(isset($config['snmpd']['modules']['mibii'])) {
775
			$snmpdconf .= <<<EOD
776
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
777

    
778
EOD;
779
		    }
780

    
781
		    if(isset($config['snmpd']['modules']['netgraph'])) {
782
			$snmpdconf .= <<<EOD
783
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
784
%netgraph
785
begemotNgControlNodeName = "snmpd"
786

    
787
EOD;
788
		    }
789

    
790
		    if(isset($config['snmpd']['modules']['pf'])) {
791
			$snmpdconf .= <<<EOD
792
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
793

    
794
EOD;
795
		    }
796

    
797
		    if(isset($config['snmpd']['modules']['hostres'])) {
798
			$snmpdconf .= <<<EOD
799
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
800

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

    
808

    
809
EOD;
810
		    }
811
		}
812

    
813
		fwrite($fd, $snmpdconf);
814
		fclose($fd);
815

    
816
		if (isset($config['snmpd']['bindlan'])) {
817
			$bindlan = "";
818
		}
819

    
820
		/* run bsnmpd */
821
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
822
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
823

    
824
		if ($g['booting'])
825
			echo "done.\n";
826
	}
827

    
828
	return 0;
829
}
830

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

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

    
846
			/* determine interface name */
847
			$if = get_real_interface($dnsupdate['interface']);
848
			$wanip = get_interface_ip($dnsupdate['interface']);
849
			if ($wanip) {
850

    
851
				$keyname = $dnsupdate['keyname'];
852
				/* trailing dot */
853
				if (substr($keyname, -1) != ".")
854
					$keyname .= ".";
855

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

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

    
870
EOD;
871
				fwrite($fd, $privkey);
872
				fclose($fd);
873

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

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

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

    
898
				$fd = fopen("{$g['varetc_path']}/nsupdatecmds{$i}", "w");
899
				fwrite($fd, $upinst);
900
				fclose($fd);
901

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

    
913
	return 0;
914
}
915

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

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

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

    
952
# This file is an example of a typical
953
# configuration for a mostly static
954
# network(regarding mobility) using
955
# the LQ extention
956

    
957
# Debug level(0-9)
958
# If set to 0 the daemon runs in the background
959

    
960
DebugLevel	2
961

    
962
# IP version to use (4 or 6)
963

    
964
IpVersion	4
965

    
966
# Clear the screen each time the internal state changes
967

    
968
ClearScreen     yes
969

    
970
{$enableannounce}
971

    
972
# Should olsrd keep on running even if there are
973
# no interfaces available? This is a good idea
974
# for a PCMCIA/USB hotswap environment.
975
# "yes" OR "no"
976

    
977
AllowNoInt	yes
978

    
979
# TOS(type of service) value for
980
# the IP header of control traffic.
981
# If not set it will default to 16
982

    
983
#TosValue	16
984

    
985
# The fixed willingness to use(0-7)
986
# If not set willingness will be calculated
987
# dynamically based on battery/power status
988
# if such information is available
989

    
990
#Willingness    	4
991

    
992
# Allow processes like the GUI front-end
993
# to connect to the daemon.
994

    
995
IpcConnect
996
{
997
     # Determines how many simultaneously
998
     # IPC connections that will be allowed
999
     # Setting this to 0 disables IPC
1000

    
1001
     MaxConnections  0
1002

    
1003
     # By default only 127.0.0.1 is allowed
1004
     # to connect. Here allowed hosts can
1005
     # be added
1006

    
1007
     Host            127.0.0.1
1008
     #Host            10.0.0.5
1009

    
1010
     # You can also specify entire net-ranges
1011
     # that are allowed to connect. Multiple
1012
     # entries are allowed
1013

    
1014
     #Net             192.168.1.0 255.255.255.0
1015
}
1016

    
1017
# Wether to use hysteresis or not
1018
# Hysteresis adds more robustness to the
1019
# link sensing but delays neighbor registration.
1020
# Used by default. 'yes' or 'no'
1021

    
1022
UseHysteresis	no
1023

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

    
1033
#HystScaling	0.50
1034
#HystThrHigh	0.80
1035
#HystThrLow	0.30
1036

    
1037

    
1038
# Link quality level
1039
# 0 = do not use link quality
1040
# 1 = use link quality for MPR selection
1041
# 2 = use link quality for MPR selection and routing
1042
# Defaults to 0
1043

    
1044
LinkQualityLevel	{$olsrd['enablelqe']}
1045

    
1046
# Link quality window size
1047
# Defaults to 10
1048

    
1049
LinkQualityWinSize	10
1050

    
1051
# Polling rate in seconds(float).
1052
# Default value 0.05 sec
1053

    
1054
Pollrate	0.05
1055

    
1056

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

    
1067
TcRedundancy	2
1068

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

    
1078
MprCoverage	3
1079

    
1080
# Example plugin entry with parameters:
1081

    
1082
EODA;
1083

    
1084
if($olsrd['enablehttpinfo'] == "on") {
1085
	$olsr .= <<<EODB
1086

    
1087
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1088
{
1089
    PlParam     "port"   "{$olsrd['port']}"
1090
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1091
}
1092

    
1093
EODB;
1094

    
1095
}
1096

    
1097
if($olsrd['enabledsecure'] == "on") {
1098
	$olsr .= <<<EODC
1099

    
1100
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1101
{
1102
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1103
}
1104

    
1105
EODC;
1106

    
1107
}
1108

    
1109
if($olsrd['enabledyngw'] == "on") {
1110

    
1111
	/* unset default route, olsr auto negotiates */
1112
	mwexec("/sbin/route delete default");
1113

    
1114
	$olsr .= <<<EODE
1115

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

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

    
1132
EODE;
1133

    
1134
}
1135

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

    
1144
    # Hello interval in seconds(float)
1145
    HelloInterval    2.0
1146

    
1147
    # HELLO validity time
1148
    HelloValidityTime	20.0
1149

    
1150
    # TC interval in seconds(float)
1151
    TcInterval        5.0
1152

    
1153
    # TC validity time
1154
    TcValidityTime	30.0
1155

    
1156
    # MID interval in seconds(float)
1157
    MidInterval	5.0
1158

    
1159
    # MID validity time
1160
    MidValidityTime	30.0
1161

    
1162
    # HNA interval in seconds(float)
1163
    HnaInterval	5.0
1164

    
1165
    # HNA validity time
1166
    HnaValidityTime 	30.0
1167

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

    
1176
    # Weight 0
1177

    
1178

    
1179
}
1180

    
1181
EODAD;
1182

    
1183
	}
1184
	break;
1185
}
1186
		fwrite($fd, $olsr);
1187
		fclose($fd);
1188
	}
1189

    
1190
	if(is_process_running("olsrd"))
1191
		mwexec("/usr/bin/killall olsrd", true);
1192

    
1193
	sleep(2);
1194

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

    
1197
	conf_mount_ro();
1198
}
1199

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

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

    
1246
function upnp_action ($action) {
1247
	switch($action) {
1248
		case "start":
1249
			if(file_exists('/var/etc/miniupnpd.conf'))
1250
				mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf');
1251
			break;
1252
		case "stop":
1253
			while((int)exec("pgrep miniupnpd | wc -l") > 0)
1254
				mwexec('killall miniupnpd 2>/dev/null', true);
1255
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
1256
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
1257
			break;
1258
		case "restart":
1259
			upnp_action('stop');
1260
			upnp_action('start');
1261
			break;
1262
	}
1263
}
1264

    
1265
function upnp_start() {
1266
	global $config, $g;
1267

    
1268
	if(!isset($config['installedpackages']['miniupnpd']['config']))
1269
		return;
1270

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

    
1284
?>
(36-36/50)