Project

General

Profile

Download (34.3 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	services.inc
5
	part of m0n0wall (http://m0n0.ch/wall)
6

    
7
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9

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

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

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/* include all configuration functions */
33
require_once("functions.inc");
34

    
35
function load_balancer_use_sticky() {
36
	global $config, $g;
37
	if (isset ($config['system']['lb_use_sticky']))
38
		touch("/var/etc/use_pf_pool__stickyaddr");
39
	else
40
		unlink_if_exists("/var/etc/use_pf_pool__stickyaddr");
41
}
42

    
43
function services_dhcpd_configure() {
44
	global $config, $g;
45
	if(isset($config['system']['developerspew'])) {
46
		$mt = microtime();
47
		echo "services_dhcpd_configure($if) being called $mt\n";
48
	}
49

    
50
	/* if OLSRD is enabled, allow WAN to house DHCP. */
51
	if($config['installedpackages']['olsrd'])
52
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd)
53
				if($olsrd['enable'])
54
					$is_olsr_enabled = true;
55

    
56
	/* configure DHCPD chroot */
57
	$fd = fopen("/tmp/dhcpd.sh","w");
58
	$status = `mount | grep "{$g['dhcpd_chroot_path']}/dev"`;
59
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}\n");
60
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/dev\n");
61
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/etc\n");
62
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr/local/sbin\n");
63
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/var/db\n");
64
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/usr\n");
65
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/lib\n");
66
	fwrite($fd, "mkdir -p {$g['dhcpd_chroot_path']}/run\n");
67
	fwrite($fd, "chown -R dhcpd:_dhcp {$g['dhcpd_chroot_path']}/*\n");
68
	fwrite($fd, "cp /lib/libc.so.6 {$g['dhcpd_chroot_path']}/lib/\n");
69
	fwrite($fd, "cp /usr/local/sbin/dhcpd {$g['dhcpd_chroot_path']}/usr/local/sbin/\n");
70
	fwrite($fd, "chmod a+rx {$g['dhcpd_chroot_path']}/usr/local/sbin/dhcpd\n");
71
	if(!trim($status))
72
		fwrite($fd, "mount_devfs devfs {$g['dhcpd_chroot_path']}/dev\n");
73
	fclose($fd);
74
	mwexec("/bin/sh /tmp/dhcpd.sh");
75

    
76
	/* kill any running dhcpd */
77
	if(is_process_running("dhcpd"))
78
		mwexec("killall dhcpd");
79

    
80
	$syscfg = $config['system'];
81
	$dhcpdcfg = $config['dhcpd'];
82

    
83
	/* DHCP enabled on any interfaces? */
84
	$dhcpdenable = false;
85
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
86
		if (isset($dhcpifconf['enable']) &&
87
			(($dhcpif == "lan") ||
88
			(isset($config['interfaces'][$dhcpif]['enable']) &&
89
			$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
90
			$dhcpdenable = true;
91
		if (isset($dhcpifconf['enable']) &&
92
			(($dhcpif == "wan") || (isset($config['interfaces'][$dhcpif]['enable']) &&
93
			$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
94
			$dhcpdenable = true;
95
	}
96

    
97
	if (!$dhcpdenable)
98
		return 0;
99

    
100
	if ($g['booting'])
101
		echo "Starting DHCP service...";
102
	else
103
		sleep(1);
104

    
105
	/* write dhcpd.conf */
106
	$fd = fopen("{$g['dhcpd_chroot_path']}/etc/dhcpd.conf", "w");
107
	if (!$fd) {
108
		printf("Error: cannot open dhcpd.conf in services_dhcpd_configure().\n");
109
		return 1;
110
	}
111

    
112

    
113

    
114
	$dhcpdconf = <<<EOD
115
option domain-name "{$syscfg['domain']}";
116
default-lease-time 7200;
117
max-lease-time 86400;
118
authoritative;
119
log-facility local7;
120
ddns-update-style none;
121
one-lease-per-client true;
122
deny duplicates;
123

    
124
EOD;
125

    
126
	$dhcpdifs = array();
127

    
128
	/*    loop through and deterimine if we need to setup
129
	 *    failover peer "bleh" entries
130
	 */
131
	$dhcpnum = 0;
132
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
133
		if($dhcpifconf['failover_peerip'] <> "") {
134
			/*
135
			 *    yep, failover peer is defined.
136
			 *    does it match up to a defined vip?
137
			 */
138
			$skew = 110;
139
			$a_vip = &$config['virtualip']['vip'];
140
			if(is_array($a_vip)) {
141
				foreach ($a_vip as $vipent) {
142
					$int = guess_interface_from_ip($dhcpifconf['failover_peerip']);
143
					$intip = find_interface_ip($int);
144
					$real_dhcpif = convert_friendly_interface_to_real_interface_name($dhcpif);
145
					if($int == $real_dhcpif) {
146
						/* this is the interface! */
147
						if($vipent['advskew'] < "20")
148
							$skew = 0;
149
					}
150
				}
151
			} else {
152
				log_error("Warning!  DHCP Failover setup and no CARP virtual IP's defined!");
153
			}
154
			if($skew > 10) {
155
				$type = "secondary";
156
				$dhcpdconf_pri  = "mclt 600;\n";
157
				$my_port = "520";
158
				$peer_port = "519";
159
			} else {
160
				$my_port = "519";
161
				$peer_port = "520";
162
				$type = "primary";
163
				$dhcpdconf_pri  = "split 128;\n";
164
				$dhcpdconf_pri .= "  mclt 600;\n";
165
			}
166
			$dhcpdconf .= <<<EOPP
167
failover peer "dhcp{$dhcpnum}" {
168
  {$type};
169
  address {$intip};
170
  port {$my_port};
171
  peer address {$dhcpifconf['failover_peerip']};
172
  peer port {$peer_port};
173
  max-response-delay 60;
174
  max-unacked-updates 10;
175
  {$dhcpdconf_pri}
176
  load balance max seconds 3;
177
}
178

    
179
EOPP;
180
		$dhcpnum++;
181
		}
182
	}
183

    
184
	$dhcpnum = 0;
185

    
186
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
187

    
188
		$ifcfg = $config['interfaces'][$dhcpif];
189

    
190
		if (!isset($dhcpifconf['enable']) ||
191
			($ifcfg['ipaddr'] == "dhcp") ||
192
			(($dhcpif != "lan") &&
193
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
194
			continue;
195

    
196
		if($dhcpif == "lan" && $ifcfg['bridge'])
197
			log_error("NOTE: DHCP Server on LAN is enabled.");
198

    
199
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
200
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
201

    
202
		if($is_olsr_enabled == true)
203
			if($dhcpifconf['netmask'])
204
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
205

    
206
		$dnscfg = "";
207

    
208
		if ($dhcpifconf['domain']) {
209
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
210
		}
211
		if (isset($dhcpifconf['ddnsupdate'])) {
212
			if($dhcpifconf['ddnsdomain'] <> "") {
213
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
214
			}
215
			$dnscfg .= "	ddns-update-style interim;\n";
216
		}
217

    
218

    
219
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
220
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
221
		} else if (isset($config['dnsmasq']['enable'])) {
222
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
223
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
224
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
225
		}
226

    
227
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
228
		$dhcpdconf .= "	pool {\n";
229

    
230
		/* is failover dns setup? */
231
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
232
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
233
			if($dhcpifconf['dnsserver'][1] <> "")
234
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
235
			$dhcpdconf .= ";\n";
236
		}
237

    
238
		if($dhcpifconf['failover_peerip'] <> "")
239
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
240

    
241
		if (isset($dhcpifconf['denyunknown']))
242
		   $dhcpdconf .= "		deny unknown clients;\n";
243

    
244
		if ($dhcpifconf['gateway'])
245
			$routers = $dhcpifconf['gateway'];
246
		else
247
			$routers = $ifcfg['ipaddr'];
248

    
249
		if($dhcpifconf['failover_peerip'] <> "") {
250
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
251
			$dhcpnum++;
252
		}
253

    
254
		$dhcpdconf .= <<<EOD
255
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
256
	}
257
	option routers {$routers};
258
$dnscfg
259

    
260
EOD;
261

    
262
		if ($dhcpifconf['defaultleasetime'])
263
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
264
		if ($dhcpifconf['maxleasetime'])
265
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
266

    
267
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
268
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
269
			$dhcpdconf .= "	option netbios-node-type 8;\n";
270
		}
271

    
272
		if (is_array($dhcpifconf['ntpserver']) && $dhcpifconf['ntpserver'][0])
273
			$dhcpdconf .= "	option ntp-servers " . join(",", $dhcpifconf['ntpserver']) . ";\n";
274

    
275
		if(isset($dhcpifconf['netboot'])) {
276
			if (($dhcpifconf['next-server'] <> "") && ($dhcpifconf['filename'] <> "")) {
277
				$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
278
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
279
			}
280
		}
281
		$dhcpdconf .= <<<EOD
282
}
283

    
284
EOD;
285

    
286
		/* add static mappings */
287
		if (is_array($dhcpifconf['staticmap'])) {
288

    
289
			$i = 0;
290
			foreach ($dhcpifconf['staticmap'] as $sm) {
291
				$dhcpdconf .= <<<EOD
292
host s_{$dhcpif}_{$i} {
293
	hardware ethernet {$sm['mac']};
294

    
295
EOD;
296
				if ($sm['ipaddr'])
297
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
298

    
299
				$dhcpdconf .= "}\n";
300
				$i++;
301
			}
302
		}
303

    
304
		$dhcpdifs[] = $ifcfg['if'];
305
	}
306

    
307
	fwrite($fd, $dhcpdconf);
308
	fclose($fd);
309

    
310
	/* create an empty leases database */
311
	touch("{$g['dhcpd_chroot_path']}/var/db/dhcpd.leases");
312

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

    
317
	if ($g['booting']) {
318
		print "done.\n";
319
	}
320

    
321
	return 0;
322
}
323

    
324
function interfaces_staticarp_configure($if) {
325
	global $config, $g;
326
	if(isset($config['system']['developerspew'])) {
327
		$mt = microtime();
328
		echo "interfaces_staticarp_configure($if) being called $mt\n";
329
	}
330

    
331
        $ifcfg = $config['interfaces'][$if];
332

    
333
        /* Enable staticarp, if enabled */
334
        if(isset($config['dhcpd'][$if]['staticarp'])) {
335
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
336
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
337
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
338

    
339
                        foreach ($config['dhcpd'][$if]['staticmap'] as $arpent) {
340
                                mwexec("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
341
								log_error("/usr/sbin/arp -s " . escapeshellarg($arpent['ipaddr']) . " " . escapeshellarg($arpent['mac']));
342
                        }
343

    
344
                }
345
        } else {
346
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
347
                mwexec("/usr/sbin/arp -da > /dev/null 2>&1 ");
348
        }
349

    
350
        return 0;
351
}
352

    
353
function services_dhcrelay_configure() {
354
	global $config, $g;
355
	if(isset($config['system']['developerspew'])) {
356
		$mt = microtime();
357
		echo "services_dhcrelay_configure() being called $mt\n";
358
	}
359

    
360
	/* kill any running dhcrelay */
361
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
362

    
363
	$dhcrelaycfg = $config['dhcrelay'];
364

    
365
	/* DHCPRelay enabled on any interfaces? */
366
	$dhcrelayenable = false;
367
	if(is_array($dhcrelaycfg)) {
368
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
369
			if (isset($dhcrelayifconf['enable']) &&
370
				(($dhcrelayif == "lan") ||
371
				(isset($config['interfaces'][$dhcrelayif]['enable']) &&
372
				$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
373
				$dhcrelayenable = true;
374
		}
375
	}
376

    
377
	if (!$dhcrelayenable)
378
		return 0;
379

    
380
	if ($g['booting'])
381
		echo "Starting DHCP relay service...";
382
	else
383
		sleep(1);
384

    
385
	$dhcrelayifs = array();
386
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
387

    
388
		$ifcfg = $config['interfaces'][$dhcrelayif];
389

    
390
		if (!isset($dhcrelayifconf['enable']) ||
391
			(($dhcrelayif != "lan") &&
392
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
393
			continue;
394

    
395
		$dhcrelayifs[] = $ifcfg['if'];
396
	}
397

    
398
	/* In order for the relay to work, it needs to be active on the
399
	   interface in which the destination server sits */
400
	foreach ($config['interfaces'] as $ifname) {
401
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
402
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
403
			$destif = $ifname['if'];
404
	}
405

    
406
	if (!isset($destif))
407
		$destif = $config['interfaces']['wan']['if'];
408

    
409
	$dhcrelayifs[] = $destif;
410
	$dhcrelayifs = array_unique($dhcrelayifs);
411

    
412
	/* fire up dhcrelay */
413
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
414

    
415
	if (isset($dhcrelaycfg['agentoption']))
416
		$cmd .=  " -a -m replace";
417

    
418
	$cmd .= " {$dhcrelaycfg['server']}";
419
	mwexec($cmd);
420

    
421
	if (!$g['booting']) {
422
		/* set the reload filter dity flag */
423
		touch("{$g['tmp_path']}/filter_dirty");
424
	}
425

    
426
	return 0;
427
}
428

    
429
function services_dyndns_reset() {
430
	global $config, $g;
431
	if(isset($config['system']['developerspew'])) {
432
		$mt = microtime();
433
		echo "services_dyndns_reset() being called $mt\n";
434
	}
435

    
436
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
437
		conf_mount_rw();
438
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
439
		conf_mount_ro();
440
	}
441

    
442
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
443
		conf_mount_rw();
444
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
445
		conf_mount_ro();
446
	}
447

    
448
	return 0;
449
}
450

    
451
function services_dyndns_configure() {
452
	global $config, $g;
453
	if(isset($config['system']['developerspew'])) {
454
		$mt = microtime();
455
		echo "services_dyndns_configure() being called $mt\n";
456
	}
457

    
458
	$dyndnscfg = $config['dyndns'];
459
	$wancfg = $config['interfaces']['wan'];
460

    
461
	if (isset($dyndnscfg['enable'])) {
462

    
463
		if ($g['booting']) {
464
			echo "Starting DynDNS client...";
465
			if(isset($config['system']['use_old_dyndns'])) {
466
				echo " [Using ez-ipupdate] ";
467
				services_dyndns_configure_old();
468
				return;
469
			}
470
		} else {
471
			sleep(1);
472
			if(isset($config['system']['use_old_dyndns'])) {
473
				services_dyndns_configure_old();
474
				return;
475
			}
476
		}
477

    
478
		/* load up the dyndns.class */
479
		require_once("dyndns.class");
480

    
481
		log_error("DynDns: Running updatedns()");
482

    
483
		/* determine WAN interface name */
484
		$wanif = get_real_wan_interface();
485
		/* get ip */
486
		$ip = find_interface_ip($wanif);
487

    
488
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
489
							 $dnsHost = $config['dyndns']['host'],
490
							 $dnsUser = $config['dyndns']['username'],
491
							 $dnsPass = $config['dyndns']['password'],
492
							 $dnsWilcard = $config['dyndns']['wildcard'],
493
							 $dnsMX = $config['dyndns']['mx']);
494

    
495
		if ($g['booting'])
496
			echo "done.\n";
497
	}
498

    
499
	return 0;
500
}
501

    
502
function services_dyndns_configure_old() {
503
	global $config, $g;
504
	if(isset($config['system']['developerspew'])) {
505
		$mt = microtime();
506
		echo "services_dyndns_configure_old() being called $mt\n";
507
	}
508

    
509
        /* kill any running ez-ipupdate */
510
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
511
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
512

    
513
        $dyndnscfg = $config['dyndns'];
514
        $wancfg = $config['interfaces']['wan'];
515

    
516
        if (isset($dyndnscfg['enable'])) {
517

    
518
                if ($g['booting'])
519
                        echo "Starting DynDNS client...";
520
                else
521
                        sleep(1);
522

    
523
                /* determine WAN interface name */
524
                $wanif = get_real_wan_interface();
525

    
526
                /* write ez-ipupdate.conf */
527
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
528
                if (!$fd) {
529
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
530
                        return 1;
531
                }
532

    
533
                $ezipupdateconf = <<<EOD
534
service-type={$dyndnscfg['type']}
535
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
536
host={$dyndnscfg['host']}
537
interface={$wanif}
538
max-interval=2073600
539
pid-file={$g['varrun_path']}/ez-ipupdate.pid
540
cache-file={$g['vardb_path']}/ez-ipupdate.cache
541
execute=/etc/rc.dyndns.storecache
542
daemon
543

    
544
EOD;
545

    
546
                /* enable server[:port]? */
547
                if ($dyndnscfg['server']) {
548
                        if ($dyndnscfg['port'])
549
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
550
                        else
551
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
552
                }
553

    
554
                /* enable MX? */
555
                if ($dyndnscfg['mx']) {
556
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
557
                }
558

    
559
                /* enable wildcards? */
560
                if (isset($dyndnscfg['wildcard'])) {
561
                        $ezipupdateconf .= "wildcard\n";
562
                }
563

    
564
                fwrite($fd, $ezipupdateconf);
565
                fclose($fd);
566

    
567
                /* if we're booting, copy the cache file from /conf */
568
                if ($g['booting']) {
569
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
570
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
571
                       }
572
                }
573

    
574
                /* run ez-ipupdate */
575
                mwexec("/usr/local/bin/ez-ipupdate -c {$g['varetc_path']}/ez-ipupdate.conf");
576

    
577
                if ($g['booting'])
578
                        echo "done\n";
579
        }
580

    
581
        return 0;
582
}
583

    
584
function services_dnsmasq_configure() {
585
	global $config, $g;
586
	$return = 0;
587
	
588
	if(isset($config['system']['developerspew'])) {
589
		$mt = microtime();
590
		echo "services_dnsmasq_configure() being called $mt\n";
591
	}
592

    
593
	/* kill any running dnsmasq */
594
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
595

    
596
	if (isset($config['dnsmasq']['enable'])) {
597

    
598
		if ($g['booting'])
599
			echo "Starting DNS forwarder...";
600
		else
601
			sleep(1);
602

    
603
		/* generate hosts file */
604
		if(system_hosts_generate()!=0)
605
			$return = 1;
606

    
607
		$args = "";
608

    
609
		if (isset($config['dnsmasq']['regdhcp'])) {
610

    
611
			$args .= " -l {$g['dhcpd_chroot_path']}/var/db/dhcpd.leases" .
612
				" -s {$config['system']['domain']}";
613
		}
614

    
615
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
616
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
617
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
618
                        }
619
                }
620

    
621
		/* suppose that dnsmasq handles our domain and don't send
622
		requests for our local domain to upstream servers */
623
		//if (!empty($config['system']['domain'])) {
624
		//	$args .= sprintf(' --local=/%s/', $config['system']['domain']);
625
		//}
626

    
627
		/* run dnsmasq */
628
		mwexec("/usr/local/sbin/dnsmasq {$args}");
629

    
630
		if ($g['booting'])
631
			echo "done.\n";
632
	}
633

    
634
	if (!$g['booting']) {
635
		if(services_dhcpd_configure()!=0)
636
			$return = 1;
637
	}
638

    
639
	return $return;
640
}
641

    
642
function services_snmpd_configure() {
643
	global $config, $g;
644
	if(isset($config['system']['developerspew'])) {
645
		$mt = microtime();
646
		echo "services_snmpd_configure() being called $mt\n";
647
	}
648

    
649
	/* kill any running snmpd */
650
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
651
	if(is_process_running("bsnmpd")) 
652
		exec("/usr/bin/killall bsnmpd");
653

    
654
	if (isset($config['snmpd']['enable'])) {
655

    
656
		if ($g['booting'])
657
			echo "Starting SNMP daemon... ";
658

    
659
		/* generate snmpd.conf */
660
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
661
		if (!$fd) {
662
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
663
			return 1;
664
		}
665

    
666

    
667
		$snmpdconf = <<<EOD
668
location := "{$config['snmpd']['syslocation']}"
669
contact := "{$config['snmpd']['syscontact']}"
670
read := "{$config['snmpd']['rocommunity']}"
671

    
672
EOD;
673

    
674
/* No docs on what write strings do there for disable for now.
675
		if(isset($config['snmpd']['rwenable']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
676
		    $snmpdconf .= <<<EOD
677
# write string
678
write := "{$config['snmpd']['rwcommunity']}"
679

    
680
EOD;
681
		}
682
*/
683

    
684

    
685
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
686
		    $snmpdconf .= <<<EOD
687
# SNMP Trap support.
688
traphost := {$config['snmpd']['trapserver']}
689
trapport := {$config['snmpd']['trapserverport']}
690
trap := "{$config['snmpd']['trapstring']}"
691

    
692

    
693
EOD;
694
		}
695

    
696

    
697
		$snmpdconf .= <<<EOD
698
system := 1     # pfSense
699
%snmpd
700
begemotSnmpdDebugDumpPdus       = 2
701
begemotSnmpdDebugSyslogPri      = 7
702
begemotSnmpdCommunityString.0.1 = $(read)
703

    
704
EOD;
705

    
706
/* No docs on what write strings do there for disable for now.
707
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
708
		    $snmpdconf .= <<<EOD
709
begemotSnmpdCommunityString.0.2 = $(write)
710

    
711
EOD;
712
		}
713
*/
714

    
715

    
716
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
717
		    $snmpdconf .= <<<EOD
718
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
719
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
720
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
721

    
722
EOD;
723
		}
724

    
725

    
726
		$snmpdconf .= <<<EOD
727
begemotSnmpdCommunityDisable    = 1
728

    
729
EOD;
730

    
731
		if(isset($config['snmpd']['bindlan'])) {
732
			$bind_to_ip = $config['interfaces']['lan']['ipaddr'];
733
		} else {
734
			$bind_to_ip = "0.0.0.0";
735
		}
736

    
737
		if(is_port( $config['snmpd']['pollport'] )) {
738
		    $snmpdconf .= <<<EOD
739
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
740

    
741
EOD;
742

    
743
		}
744

    
745
		$snmpdconf .= <<<EOD
746
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
747
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
748

    
749
# These are bsnmp macros not php vars.
750
sysContact      = $(contact)
751
sysLocation     = $(location)
752
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
753

    
754
snmpEnableAuthenTraps = 2
755

    
756
EOD;
757

    
758
		if (is_array( $config['snmpd']['modules'] )) {
759
		    if(isset($config['snmpd']['modules']['mibii'])) {
760
			$snmpdconf .= <<<EOD
761
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
762

    
763
EOD;
764
		    }
765

    
766
		    if(isset($config['snmpd']['modules']['netgraph'])) {
767
			$snmpdconf .= <<<EOD
768
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
769
%netgraph
770
begemotNgControlNodeName = "snmpd"
771

    
772
EOD;
773
		    }
774

    
775
		    if(isset($config['snmpd']['modules']['pf'])) {
776
			$snmpdconf .= <<<EOD
777
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
778

    
779
EOD;
780
		    }
781

    
782
		    if(isset($config['snmpd']['modules']['hostres'])) {
783
			$snmpdconf .= <<<EOD
784
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
785

    
786
EOD;
787
		    }
788
		    if(isset($config['snmpd']['modules']['bridge'])) {
789
			$snmpdconf .= <<<EOD
790
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
791
# config must end with blank line
792

    
793

    
794
EOD;
795
		    }
796
		}
797

    
798
		fwrite($fd, $snmpdconf);
799
		fclose($fd);
800

    
801
		if (isset($config['snmpd']['bindlan'])) {
802
			$bindlan = "";
803
		}
804

    
805
		/* run bsnmpd */
806
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
807
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
808

    
809
		if ($g['booting'])
810
			echo "done.\n";
811
	}
812

    
813
	return 0;
814
}
815

    
816
function services_proxyarp_configure() {
817
	global $config, $g;
818
	if(isset($config['system']['developerspew'])) {
819
		$mt = microtime();
820
		echo "services_proxyarp_configure() being called $mt\n";
821
	}
822

    
823
	/* kill any running choparp */
824
	killbyname("choparp");
825

    
826
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
827
		$paa = array();
828

    
829
		/* group by interface */
830
		foreach ($config['virtualip']['vip'] as $vipent) {
831
			if ($vipent['mode'] === "proxyarp") {
832
				if ($vipent['interface'])
833
					$if = $vipent['interface'];
834
				else
835
					$if = "wan";
836

    
837
				if (!is_array($paa[$if]))
838
					$paa[$if] = array();
839

    
840
				$paa[$if][] = $vipent;
841
			}
842
		}
843

    
844
		if (count($paa))
845
		foreach ($paa as $paif => $paents) {
846
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
847
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
848
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
849
                               continue;
850

    
851
			$args = $config['interfaces'][$paif]['if'] . " auto";
852

    
853
			foreach ($paents as $paent) {
854

    
855
				if (isset($paent['subnet']))
856
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
857
				else if (isset($paent['range']))
858
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
859
						$paent['range']['to']);
860
			}
861

    
862
			mwexec_bg("/usr/local/sbin/choparp " . $args);
863
		}
864
	}
865
}
866

    
867
function services_dnsupdate_process() {
868
	global $config, $g;
869
	if(isset($config['system']['developerspew'])) {
870
		$mt = microtime();
871
		echo "services_dnsupdate_process() being called $mt\n";
872
	}
873

    
874
	/* Dynamic DNS updating active? */
875
	if (isset($config['dnsupdate']['enable'])) {
876

    
877
		$wanip = get_current_wan_address();
878
		if ($wanip) {
879

    
880
			$keyname = $config['dnsupdate']['keyname'];
881
			/* trailing dot */
882
			if (substr($keyname, -1) != ".")
883
				$keyname .= ".";
884

    
885
			$hostname = $config['dnsupdate']['host'];
886
			/* trailing dot */
887
			if (substr($hostname, -1) != ".")
888
				$hostname .= ".";
889

    
890
			/* write private key file
891
			   this is dumb - public and private keys are the same for HMAC-MD5,
892
			   but nsupdate insists on having both */
893
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
894
			$privkey .= <<<EOD
895
Private-key-format: v1.2
896
Algorithm: 157 (HMAC)
897
Key: {$config['dnsupdate']['keydata']}
898

    
899
EOD;
900
			fwrite($fd, $privkey);
901
			fclose($fd);
902

    
903
			/* write public key file */
904
			if ($config['dnsupdate']['keytype'] == "zone") {
905
				$flags = 257;
906
				$proto = 3;
907
			} else if ($config['dnsupdate']['keytype'] == "host") {
908
				$flags = 513;
909
				$proto = 3;
910
			} else if ($config['dnsupdate']['keytype'] == "user") {
911
				$flags = 0;
912
				$proto = 2;
913
			}
914

    
915
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.key", "w");
916
			fwrite($fd, "{$keyname} IN KEY {$flags} {$proto} 157 {$config['dnsupdate']['keydata']}\n");
917
			fclose($fd);
918

    
919
			/* generate update instructions */
920
			$upinst =  "update delete {$config['dnsupdate']['host']} A\n";
921
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
922
			$upinst .= "\n";	/* mind that trailing newline! */
923

    
924
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
925
			fwrite($fd, $upinst);
926
			fclose($fd);
927

    
928
			/* invoke nsupdate */
929
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}/K{$keyname}+157+00000.key";
930
			if (isset($config['dnsupdate']['usetcp']))
931
				$cmd .= " -v";
932
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
933

    
934
			mwexec_bg($cmd);
935
		}
936
	}
937

    
938
	return 0;
939
}
940

    
941
function setup_wireless_olsr() {
942
	global $config, $g;
943
	if(!$config['installedpackages']['olsrd'] || !$config['installedpackages'])
944
		return;
945
	if(isset($config['system']['developerspew'])) {
946
		$mt = microtime();
947
		echo "setup_wireless_olsr($interface) being called $mt\n";
948
	}
949
	conf_mount_rw();
950
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
951
		$olsr_enable = $olsrd['enable'];
952
		if($olsr_enable <> "on")
953
			return;
954
		$fd = fopen("{$g['varetc_path']}/olsr.conf", "w");
955

    
956
		if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") {
957
			$enableannounce .= "\nHna4\n";
958
			$enableannounce .= "{\n";
959
		if($olsrd['announcedynamicroute'])
960
			$enableannounce .= "\t{$olsrd['announcedynamicroute']}\n";
961
		if($olsrd['enableannounce'] == "on")
962
			$enableannounce .= "0.0.0.0 0.0.0.0";
963
			$enableannounce .= "\n}\n";
964
		} else {
965
			$enableannounce = "";
966
		}
967

    
968
		$olsr .= <<<EODA
969
#
970
# olsr.org OLSR daemon config file
971
#
972
# Lines starting with a # are discarded
973
#
974
# This file was generated by setup_wireless_olsr() in services.inc
975
#
976

    
977
# This file is an example of a typical
978
# configuration for a mostly static
979
# network(regarding mobility) using
980
# the LQ extention
981

    
982
# Debug level(0-9)
983
# If set to 0 the daemon runs in the background
984

    
985
DebugLevel	2
986

    
987
# IP version to use (4 or 6)
988

    
989
IpVersion	4
990

    
991
# Clear the screen each time the internal state changes
992

    
993
ClearScreen     yes
994

    
995
{$enableannounce}
996

    
997
# Should olsrd keep on running even if there are
998
# no interfaces available? This is a good idea
999
# for a PCMCIA/USB hotswap environment.
1000
# "yes" OR "no"
1001

    
1002
AllowNoInt	yes
1003

    
1004
# TOS(type of service) value for
1005
# the IP header of control traffic.
1006
# If not set it will default to 16
1007

    
1008
#TosValue	16
1009

    
1010
# The fixed willingness to use(0-7)
1011
# If not set willingness will be calculated
1012
# dynamically based on battery/power status
1013
# if such information is available
1014

    
1015
#Willingness    	4
1016

    
1017
# Allow processes like the GUI front-end
1018
# to connect to the daemon.
1019

    
1020
IpcConnect
1021
{
1022
     # Determines how many simultaneously
1023
     # IPC connections that will be allowed
1024
     # Setting this to 0 disables IPC
1025

    
1026
     MaxConnections  0
1027

    
1028
     # By default only 127.0.0.1 is allowed
1029
     # to connect. Here allowed hosts can
1030
     # be added
1031

    
1032
     Host            127.0.0.1
1033
     #Host            10.0.0.5
1034

    
1035
     # You can also specify entire net-ranges
1036
     # that are allowed to connect. Multiple
1037
     # entries are allowed
1038

    
1039
     #Net             192.168.1.0 255.255.255.0
1040
}
1041

    
1042
# Wether to use hysteresis or not
1043
# Hysteresis adds more robustness to the
1044
# link sensing but delays neighbor registration.
1045
# Used by default. 'yes' or 'no'
1046

    
1047
UseHysteresis	no
1048

    
1049
# Hysteresis parameters
1050
# Do not alter these unless you know
1051
# what you are doing!
1052
# Set to auto by default. Allowed
1053
# values are floating point values
1054
# in the interval 0,1
1055
# THR_LOW must always be lower than
1056
# THR_HIGH.
1057

    
1058
#HystScaling	0.50
1059
#HystThrHigh	0.80
1060
#HystThrLow	0.30
1061

    
1062

    
1063
# Link quality level
1064
# 0 = do not use link quality
1065
# 1 = use link quality for MPR selection
1066
# 2 = use link quality for MPR selection and routing
1067
# Defaults to 0
1068

    
1069
LinkQualityLevel	{$olsrd['enablelqe']}
1070

    
1071
# Link quality window size
1072
# Defaults to 10
1073

    
1074
LinkQualityWinSize	10
1075

    
1076
# Polling rate in seconds(float).
1077
# Default value 0.05 sec
1078

    
1079
Pollrate	0.05
1080

    
1081

    
1082
# TC redundancy
1083
# Specifies how much neighbor info should
1084
# be sent in TC messages
1085
# Possible values are:
1086
# 0 - only send MPR selectors
1087
# 1 - send MPR selectors and MPRs
1088
# 2 - send all neighbors
1089
#
1090
# defaults to 0
1091

    
1092
TcRedundancy	2
1093

    
1094
#
1095
# MPR coverage
1096
# Specifies how many MPRs a node should
1097
# try select to reach every 2 hop neighbor
1098
#
1099
# Can be set to any integer >0
1100
#
1101
# defaults to 1
1102

    
1103
MprCoverage	3
1104

    
1105
# Example plugin entry with parameters:
1106

    
1107
EODA;
1108

    
1109
if($olsrd['enablehttpinfo'] == "on") {
1110
	$olsr .= <<<EODB
1111

    
1112
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1113
{
1114
    PlParam     "port"   "{$olsrd['port']}"
1115
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1116
}
1117

    
1118
EODB;
1119

    
1120
}
1121

    
1122
if($olsrd['enabledsecure'] == "on") {
1123
	$olsr .= <<<EODC
1124

    
1125
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1126
{
1127
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1128
}
1129

    
1130
EODC;
1131

    
1132
}
1133

    
1134
if($olsrd['enabledyngw'] == "on") {
1135

    
1136
	/* unset default route, olsr auto negotiates */
1137
	mwexec("/sbin/route delete default");
1138

    
1139
	$olsr .= <<<EODE
1140

    
1141
LoadPlugin "/usr/local/lib/olsrd_dyn_gw.so.0.4"
1142
{
1143
    # how often to look for a inet gw, in seconds
1144
    # defaults to 5 secs, if commented out
1145
    PlParam     "Interval"   "{$olsrd['polling']}"
1146

    
1147
    # if one or more IPv4 addresses are given, do a ping on these in
1148
    # descending order to validate that there is not only an entry in
1149
    # routing table, but also a real internet connection. If any of
1150
    # these addresses could be pinged successfully, the test was
1151
    # succesful, i.e. if the ping on the 1st address was successful,the
1152
    # 2nd won't be pinged
1153
    PlParam     "Ping"       "{$olsrd['ping']}"
1154
    #PlParam     "HNA"   "192.168.81.0 255.255.255.0"
1155
}
1156

    
1157
EODE;
1158

    
1159
}
1160

    
1161
foreach($config['installedpackages']['olsrd']['config'] as $conf) {
1162
	$interfaces = explode(',', $conf['iface_array']);
1163
	foreach($interfaces as $interface) {
1164
		$realinterface = convert_friendly_interface_to_real_interface_name($interface);
1165
$olsr .= <<<EODAD
1166
Interface "{$realinterface}"
1167
{
1168

    
1169
    # Hello interval in seconds(float)
1170
    HelloInterval    2.0
1171

    
1172
    # HELLO validity time
1173
    HelloValidityTime	20.0
1174

    
1175
    # TC interval in seconds(float)
1176
    TcInterval        5.0
1177

    
1178
    # TC validity time
1179
    TcValidityTime	30.0
1180

    
1181
    # MID interval in seconds(float)
1182
    MidInterval	5.0
1183

    
1184
    # MID validity time
1185
    MidValidityTime	30.0
1186

    
1187
    # HNA interval in seconds(float)
1188
    HnaInterval	5.0
1189

    
1190
    # HNA validity time
1191
    HnaValidityTime 	30.0
1192

    
1193
    # When multiple links exist between hosts
1194
    # the weight of interface is used to determine
1195
    # the link to use. Normally the weight is
1196
    # automatically calculated by olsrd based
1197
    # on the characteristics of the interface,
1198
    # but here you can specify a fixed value.
1199
    # Olsrd will choose links with the lowest value.
1200

    
1201
    # Weight 0
1202

    
1203

    
1204
}
1205

    
1206
EODAD;
1207

    
1208
	}
1209
	break;
1210
}
1211
		fwrite($fd, $olsr);
1212
		fclose($fd);
1213
	}
1214

    
1215
	if(is_process_running("olsrd"))
1216
		mwexec("/usr/bin/killall olsrd");
1217

    
1218
	sleep(2);
1219

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

    
1222
	conf_mount_ro();
1223
}
1224

    
1225
/* configure cron service */
1226
function configure_cron() {
1227
	global $g, $config;
1228
	conf_mount_rw();
1229
	/* preserve existing crontab entries */
1230
	$crontab_contents = file_get_contents("/etc/crontab");
1231
	$crontab_contents_a = split("\n", $crontab_contents);
1232
	
1233
	for ($i = 0; $i < count($crontab_contents_a); $i++) {
1234
		$item =& $crontab_contents_a[$i];
1235
		if (strpos($item, "# pfSense specific crontab entries") !== false) {
1236
			array_splice($crontab_contents_a, $i - 1);
1237
			break;
1238
		}
1239
	}
1240
	$crontab_contents = implode("\n", $crontab_contents_a) . "\n";
1241
	
1242
	
1243
	if (is_array($config['cron']['item'])) {
1244
		$crontab_contents .= "#\n";
1245
		$crontab_contents .= "# pfSense specific crontab entries\n";
1246
		$crontab_contents .= "# Created: " . date("F j, Y, g:i a") . "\n";
1247
		$crontab_contents .= "#\n";
1248

    
1249
		foreach ($config['cron']['item'] as $item) {
1250
			$crontab_contents .= "\n{$item['minute']}\t";
1251
			$crontab_contents .= "{$item['hour']}\t";
1252
			$crontab_contents .= "{$item['mday']}\t";
1253
			$crontab_contents .= "{$item['month']}\t";
1254
			$crontab_contents .= "{$item['wday']}\t";
1255
			$crontab_contents .= "{$item['who']}\t";
1256
			$crontab_contents .= "{$item['command']}";
1257
		}
1258
    
1259
		$crontab_contents .= "\n#\n";
1260
		$crontab_contents .= "# If possible do not add items to this file manually.\n";
1261
		$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
1262
		$crontab_contents .= "#\n\n";
1263
	}
1264
	
1265
	/* please maintain the newline at the end of file */
1266
	file_put_contents("/etc/crontab", $crontab_contents);
1267
	
1268
	if (!$g['booting'])
1269
		conf_mount_ro();
1270
}
1271

    
1272
function upnp_action ($action) {
1273
	switch($action) {
1274
		case "start":
1275
			if(file_exists('/var/etc/miniupnpd.conf'))
1276
				mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf');
1277
			break;
1278
		case "stop":
1279
			while((int)exec("pgrep miniupnpd | wc -l") > 0)
1280
				mwexec('killall miniupnpd 2>/dev/null');
1281
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
1282
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
1283
			break;
1284
		case "restart":
1285
			upnp_action('stop');
1286
			upnp_action('start');
1287
			break;
1288
	}
1289
}
1290

    
1291
function upnp_start() {
1292
	global $config, $g;
1293
	if($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
1294
		if($g['booting']) {
1295
			echo "Starting UPnP service...";
1296
			include('/usr/local/pkg/miniupnpd.inc');
1297
			sync_package_miniupnpd();
1298
			echo "done.\n";
1299
		}
1300
		else {
1301
			upnp_action('start');
1302
		}
1303
	}
1304
}
1305

    
1306
?>
(18-18/27)