Project

General

Profile

Download (35.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
	if(is_array($dhcpdcfg))
86
		foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
87
			if (isset($dhcpifconf['enable']) &&
88
				(($dhcpif == "lan") ||
89
				(isset($config['interfaces'][$dhcpif]['enable']) &&
90
				$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
91
				$dhcpdenable = true;
92
			if (isset($dhcpifconf['enable']) &&
93
				(($dhcpif == "wan") || (isset($config['interfaces'][$dhcpif]['enable']) &&
94
				$config['interfaces'][$dhcpif]['if'] && (!$config['interfaces'][$dhcpif]['bridge']))))
95
				$dhcpdenable = true;
96
		}
97

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

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

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

    
113

    
114

    
115
	$dhcpdconf = <<<EOD
116
	
117
option domain-name "{$syscfg['domain']}";
118
option ldap-server code 95 = text;
119
option domain-search-list code 119 = text;
120
default-lease-time 7200;
121
max-lease-time 86400;
122
log-facility local7;
123
ddns-update-style none;
124
one-lease-per-client true;
125
deny duplicates;
126
ping-check true;
127

    
128
EOD;
129

    
130
	$dhcpdifs = array();
131

    
132
	/*    loop through and deterimine if we need to setup
133
	 *    failover peer "bleh" entries
134
	 */
135
	$dhcpnum = 0;
136
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
137

    
138
		if(!isset($dhcpifconf['disableauthoritative']))
139
			$dhcpdconf .= "authoritative;\n";
140

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

    
187
EOPP;
188
		$dhcpnum++;
189
		}
190
	}
191

    
192
	$dhcpnum = 0;
193

    
194
	foreach ($dhcpdcfg as $dhcpif => $dhcpifconf) {
195

    
196
		$ifcfg = $config['interfaces'][$dhcpif];
197

    
198
		if (!isset($dhcpifconf['enable']) ||
199
			($ifcfg['ipaddr'] == "dhcp") ||
200
			(($dhcpif != "lan") &&
201
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
202
			continue;
203

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

    
207
		$subnet = gen_subnet($ifcfg['ipaddr'], $ifcfg['subnet']);
208
		$subnetmask = gen_subnet_mask($ifcfg['subnet']);
209

    
210
		if($is_olsr_enabled == true)
211
			if($dhcpifconf['netmask'])
212
				$subnetmask = gen_subnet_mask($dhcpifconf['netmask']);
213

    
214
		$dnscfg = "";
215

    
216
		if ($dhcpifconf['domain']) {
217
			$dnscfg .= "	option domain-name \"{$dhcpifconf['domain']}\";\n";
218
		}
219
		
220
    if($dhcpifconf['domainsearchlist'] <> "") {
221
			$dnscfg .= "	option domain-search-list \"{$dhcpifconf['domainsearchlist']}\";\n";
222
    }
223

    
224
		if (isset($dhcpifconf['ddnsupdate'])) {
225
			if($dhcpifconf['ddnsdomain'] <> "") {
226
				$dnscfg .= "	ddns-domainname \"{$dhcpifconf['ddnsdomain']}\";\n";
227
			}
228
			$dnscfg .= "	ddns-update-style interim;\n";
229
		}
230

    
231
		if (is_array($dhcpifconf['dnsserver']) && ($dhcpifconf['dnsserver'][0])) {
232
			$dnscfg .= "	option domain-name-servers " . join(",", $dhcpifconf['dnsserver']) . ";";
233
		} else if (isset($config['dnsmasq']['enable'])) {
234
			$dnscfg .= "	option domain-name-servers " . $ifcfg['ipaddr'] . ";";
235
		} else if (is_array($syscfg['dnsserver']) && ($syscfg['dnsserver'][0])) {
236
			$dnscfg .= "	option domain-name-servers " . join(",", $syscfg['dnsserver']) . ";";
237
		}
238

    
239
		$dhcpdconf .= "subnet $subnet netmask $subnetmask {\n";
240
		$dhcpdconf .= "	pool {\n";
241

    
242
		/* is failover dns setup? */
243
		if (is_array($dhcpifconf['dnsserver']) && $dhcpifconf['dnsserver'][0] <> "") {
244
			$dhcpdconf .= "		option domain-name-servers {$dhcpifconf['dnsserver'][0]}";
245
			if($dhcpifconf['dnsserver'][1] <> "")
246
				$dhcpdconf .= ",{$dhcpifconf['dnsserver'][1]}";
247
			$dhcpdconf .= ";\n";
248
		}
249

    
250
		if($dhcpifconf['failover_peerip'] <> "")
251
			$dhcpdconf .= "		deny dynamic bootp clients;\n";
252

    
253
		if (isset($dhcpifconf['denyunknown']))
254
		   $dhcpdconf .= "		deny unknown clients;\n";
255

    
256
		if ($dhcpifconf['gateway'])
257
			$routers = $dhcpifconf['gateway'];
258
		else
259
			$routers = $ifcfg['ipaddr'];
260

    
261
		if($dhcpifconf['failover_peerip'] <> "") {
262
			$dhcpdconf .= "		failover peer \"dhcp{$dhcpnum}\";\n";
263
			$dhcpnum++;
264
		}
265

    
266
		$dhcpdconf .= <<<EOD
267
		range {$dhcpifconf['range']['from']} {$dhcpifconf['range']['to']};
268
	}
269
	option routers {$routers};
270
$dnscfg
271

    
272
EOD;
273
    
274
    if ($dhcpifconf['defaultleasetime'])
275
			$dhcpdconf .= "	default-lease-time {$dhcpifconf['defaultleasetime']};\n";
276
		if ($dhcpifconf['maxleasetime'])
277
			$dhcpdconf .= "	max-lease-time {$dhcpifconf['maxleasetime']};\n";
278

    
279
		if (is_array($dhcpifconf['winsserver']) && $dhcpifconf['winsserver'][0]) {
280
			$dhcpdconf .= "	option netbios-name-servers " . join(",", $dhcpifconf['winsserver']) . ";\n";
281
			$dhcpdconf .= "	option netbios-node-type 8;\n";
282
		}
283

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

    
287
		if ($dhcpifconf['tftp'] <> "")
288
			$dhcpdconf .= "	option tftp-server-name \"{$dhcpifconf['tftp']}\";\n";
289

    
290
    if ($dhcpifconf['ldap'] <> "")
291
			$dhcpdconf .= "	option ldap-server \"{$dhcpifconf['ldap']}\";\n";
292

    
293
		if(isset($dhcpifconf['netboot'])) {
294
			if (($dhcpifconf['next-server'] <> "") && ($dhcpifconf['filename'] <> "")) {
295
				$dhcpdconf .= "	next-server {$dhcpifconf['next-server']};\n";
296
				$dhcpdconf .= "	filename \"{$dhcpifconf['filename']}\";\n";
297
			}
298
			if ($dhcpifconf['rootpath'] <> "") {
299
				$dhcpdconf .= "	option root-path \"{$dhcpifconf['rootpath']}\";\n";
300
      }
301
		}
302
		
303
		$dhcpdconf .= <<<EOD
304
}
305

    
306
EOD;
307

    
308
		/* add static mappings */
309
		if (is_array($dhcpifconf['staticmap'])) {
310

    
311
			$i = 0;
312
			foreach ($dhcpifconf['staticmap'] as $sm) {
313
				$dhcpdconf .= <<<EOD
314
host s_{$dhcpif}_{$i} {
315
	hardware ethernet {$sm['mac']};
316

    
317
EOD;
318
				if ($sm['ipaddr'])
319
					$dhcpdconf .= "	fixed-address {$sm['ipaddr']};\n";
320

    
321
				if ($sm['hostname'])
322
					$dhcpdconf .= "	option host-name {$sm['hostname']};\n";
323

    
324
				$dhcpdconf .= "}\n";
325
				$i++;
326
			}
327
		}
328

    
329
		$dhcpdifs[] = $ifcfg['if'];
330
	}
331

    
332
	fwrite($fd, $dhcpdconf);
333
	fclose($fd);
334

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

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

    
342
	if ($g['booting']) {
343
		print "done.\n";
344
	}
345

    
346
	return 0;
347
}
348

    
349
function interfaces_staticarp_configure($if) {
350
	global $config, $g;
351
	if(isset($config['system']['developerspew'])) {
352
		$mt = microtime();
353
		echo "interfaces_staticarp_configure($if) being called $mt\n";
354
	}
355

    
356
        $ifcfg = $config['interfaces'][$if];
357

    
358
        /* Enable staticarp, if enabled */
359
        if(isset($config['dhcpd'][$if]['staticarp'])) {
360
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " staticarp " );
361
                mwexec("/usr/sbin/arp -ad > /dev/null 2>&1 ");
362
                if (is_array($config['dhcpd'][$if]['staticmap'])) {
363

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

    
367
                        }
368

    
369
                }
370
        } else {
371
                mwexec("/sbin/ifconfig " . escapeshellarg($ifcfg['if']) . " -staticarp " );
372
                mwexec("/usr/sbin/arp -da > /dev/null 2>&1 ");
373
        }
374

    
375
        return 0;
376
}
377

    
378
function services_dhcrelay_configure() {
379
	global $config, $g;
380
	if(isset($config['system']['developerspew'])) {
381
		$mt = microtime();
382
		echo "services_dhcrelay_configure() being called $mt\n";
383
	}
384

    
385
	/* kill any running dhcrelay */
386
	killbypid("{$g['varrun_path']}/dhcrelay.pid");
387

    
388
	$dhcrelaycfg = $config['dhcrelay'];
389

    
390
	/* DHCPRelay enabled on any interfaces? */
391
	$dhcrelayenable = false;
392
	if(is_array($dhcrelaycfg)) {
393
		foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
394
			if (isset($dhcrelayifconf['enable']) &&
395
				(($dhcrelayif == "lan") ||
396
				(isset($config['interfaces'][$dhcrelayif]['enable']) &&
397
				$config['interfaces'][$dhcrelayif]['if'] && (!$config['interfaces'][$dhcrelayif]['bridge']))))
398
				$dhcrelayenable = true;
399
		}
400
	}
401

    
402
	if (!$dhcrelayenable)
403
		return 0;
404

    
405
	if ($g['booting'])
406
		echo "Starting DHCP relay service...";
407
	else
408
		sleep(1);
409

    
410
	$dhcrelayifs = array();
411
	foreach ($dhcrelaycfg as $dhcrelayif => $dhcrelayifconf) {
412

    
413
		$ifcfg = $config['interfaces'][$dhcrelayif];
414

    
415
		if (!isset($dhcrelayifconf['enable']) ||
416
			(($dhcrelayif != "lan") &&
417
			(!isset($ifcfg['enable']) || !$ifcfg['if'] || $ifcfg['bridge'])))
418
			continue;
419

    
420
		$dhcrelayifs[] = $ifcfg['if'];
421
	}
422

    
423
	/* In order for the relay to work, it needs to be active on the
424
	   interface in which the destination server sits */
425
	foreach ($config['interfaces'] as $ifname) {
426
		$subnet = $ifname['ipaddr'] . "/" . $ifname['subnet'];
427
		if (ip_in_subnet($dhcrelaycfg['server'],$subnet))
428
			$destif = $ifname['if'];
429
	}
430

    
431
	if (!isset($destif))
432
		$destif = $config['interfaces']['wan']['if'];
433

    
434
	$dhcrelayifs[] = $destif;
435
	$dhcrelayifs = array_unique($dhcrelayifs);
436

    
437
	/* fire up dhcrelay */
438
	$cmd = "/usr/local/sbin/dhcrelay -i " .  join(" -i ", $dhcrelayifs);
439

    
440
	if (isset($dhcrelaycfg['agentoption']))
441
		$cmd .=  " -a -m replace";
442

    
443
	$cmd .= " {$dhcrelaycfg['server']}";
444
	mwexec($cmd);
445

    
446
	if (!$g['booting']) {
447
		/* set the reload filter dity flag */
448
		touch("{$g['tmp_path']}/filter_dirty");
449
	}
450

    
451
	return 0;
452
}
453

    
454
function services_dyndns_reset() {
455
	global $config, $g;
456
	if(isset($config['system']['developerspew'])) {
457
		$mt = microtime();
458
		echo "services_dyndns_reset() being called $mt\n";
459
	}
460

    
461
	if (file_exists("{$g['vardb_path']}/ez-ipupdate.cache")) {
462
		conf_mount_rw();
463
		unlink("{$g['vardb_path']}/ez-ipupdate.cache");
464
		conf_mount_ro();
465
	}
466

    
467
	if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
468
		conf_mount_rw();
469
		unlink("{$g['conf_path']}/ez-ipupdate.cache");
470
		conf_mount_ro();
471
	}
472
	
473
	if (file_exists("{$g['conf_path']}/dyndns.cache")) {
474
		conf_mount_rw();
475
		unlink("{$g['conf_path']}/dyndns.cache");
476
		conf_mount_ro();
477
	}
478

    
479
	return 0;
480
}
481

    
482
function services_dyndns_configure() {
483
	global $config, $g;
484
	if(isset($config['system']['developerspew'])) {
485
		$mt = microtime();
486
		echo "services_dyndns_configure() being called $mt\n";
487
	}
488

    
489
	$dyndnscfg = $config['dyndns'];
490
	$wancfg = $config['interfaces']['wan'];
491

    
492
	if (isset($dyndnscfg['enable'])) {
493

    
494
		if ($g['booting']) {
495
			echo "Starting DynDNS client...";
496
			if(isset($config['system']['use_old_dyndns'])) {
497
				echo " [Using ez-ipupdate] ";
498
				services_dyndns_configure_old();
499
				return;
500
			}
501
		} else {
502
			sleep(1);
503
			if(isset($config['system']['use_old_dyndns'])) {
504
				services_dyndns_configure_old();
505
				return;
506
			}
507
		}
508

    
509
		/* load up the dyndns.class */
510
		require_once("dyndns.class");
511

    
512
		log_error("DynDns: Running updatedns()");
513

    
514
		/* determine WAN interface name */
515
		$wanif = get_real_wan_interface();
516
		/* get ip */
517
		$ip = find_interface_ip($wanif);
518

    
519
		$dns = new updatedns($dnsService = $config['dyndns']['type'],
520
							 $dnsHost = $config['dyndns']['host'],
521
							 $dnsUser = $config['dyndns']['username'],
522
							 $dnsPass = $config['dyndns']['password'],
523
							 $dnsWilcard = $config['dyndns']['wildcard'],
524
							 $dnsMX = $config['dyndns']['mx']);
525

    
526
		if ($g['booting'])
527
			echo "done.\n";
528
	}
529

    
530
	return 0;
531
}
532

    
533
function services_dyndns_configure_old() {
534
	global $config, $g;
535
	if(isset($config['system']['developerspew'])) {
536
		$mt = microtime();
537
		echo "services_dyndns_configure_old() being called $mt\n";
538
	}
539

    
540
        /* kill any running ez-ipupdate */
541
        /* ez-ipupdate needs SIGQUIT instead of SIGTERM */
542
        sigkillbypid("{$g['varrun_path']}/ez-ipupdate.pid", "QUIT");
543

    
544
        $dyndnscfg = $config['dyndns'];
545
        $wancfg = $config['interfaces']['wan'];
546

    
547
        if (isset($dyndnscfg['enable'])) {
548

    
549
                if ($g['booting'])
550
                        echo "Starting DynDNS client...";
551
                else
552
                        sleep(1);
553

    
554
                /* determine WAN interface name */
555
                $wanif = get_real_wan_interface();
556

    
557
                /* write ez-ipupdate.conf */
558
                $fd = fopen("{$g['varetc_path']}/ez-ipupdate.conf", "w");
559
                if (!$fd) {
560
                        printf("Error: cannot open ez-ipupdate.conf in services_dyndns_configure().\n");
561
                        return 1;
562
                }
563

    
564
                $ezipupdateconf = <<<EOD
565
service-type={$dyndnscfg['type']}
566
user={$dyndnscfg['username']}:{$dyndnscfg['password']}
567
host={$dyndnscfg['host']}
568
interface={$wanif}
569
max-interval=2073600
570
pid-file={$g['varrun_path']}/ez-ipupdate.pid
571
cache-file={$g['vardb_path']}/ez-ipupdate.cache
572
execute=/etc/rc.dyndns.storecache
573
daemon
574

    
575
EOD;
576

    
577
                /* enable server[:port]? */
578
                if ($dyndnscfg['server']) {
579
                        if ($dyndnscfg['port'])
580
                                $ezipupdateconf .= "server={$dyndnscfg['server']}:{$dyndnscfg['port']}\n";
581
                        else
582
                                $ezipupdateconf .= "server={$dyndnscfg['server']}\n";
583
                }
584

    
585
                /* enable MX? */
586
                if ($dyndnscfg['mx']) {
587
                        $ezipupdateconf .= "mx={$dyndnscfg['mx']}\n";
588
                }
589

    
590
                /* enable wildcards? */
591
                if (isset($dyndnscfg['wildcard'])) {
592
                        $ezipupdateconf .= "wildcard\n";
593
                }
594

    
595
                fwrite($fd, $ezipupdateconf);
596
                fclose($fd);
597

    
598
                /* if we're booting, copy the cache file from /conf */
599
                if ($g['booting']) {
600
                        if (file_exists("{$g['conf_path']}/ez-ipupdate.cache")) {
601
                                copy("{$g['conf_path']}/ez-ipupdate.cache", "{$g['vardb_path']}/ez-ipupdate.cache");
602
                       }
603
                }
604

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

    
608
                if ($g['booting'])
609
                        echo "done\n";
610
        }
611

    
612
        return 0;
613
}
614

    
615
function services_dnsmasq_configure() {
616
	global $config, $g;
617
	$return = 0;
618
	
619
	if(isset($config['system']['developerspew'])) {
620
		$mt = microtime();
621
		echo "services_dnsmasq_configure() being called $mt\n";
622
	}
623

    
624
	/* kill any running dnsmasq */
625
	sigkillbypid("{$g['varrun_path']}/dnsmasq.pid", "TERM");
626

    
627
	if (isset($config['dnsmasq']['enable'])) {
628

    
629
		if ($g['booting'])
630
			echo "Starting DNS forwarder...";
631
		else
632
			sleep(1);
633

    
634
		/* generate hosts file */
635
		if(system_hosts_generate()!=0)
636
			$return = 1;
637

    
638
		$args = "";
639

    
640
		if (isset($config['dnsmasq']['regdhcp'])) {
641

    
642
			$args .= " -l {$g['dhcpd_chroot_path']}/var/db/dhcpd.leases" .
643
				" -s {$config['system']['domain']}";
644
		}
645

    
646
                if (isset($config['dnsmasq']['domainoverrides']) && is_array($config['dnsmasq']['domainoverrides'])) {
647
                        foreach($config['dnsmasq']['domainoverrides'] as $override) {
648
                                $args .= ' --server=/' . $override['domain'] . '/' . $override['ip'];
649
                        }
650
                }
651

    
652
		/* suppose that dnsmasq handles our domain and don't send
653
		requests for our local domain to upstream servers */
654
		//if (!empty($config['system']['domain'])) {
655
		//	$args .= sprintf(' --local=/%s/', $config['system']['domain']);
656
		//}
657

    
658
		/* run dnsmasq */
659
		mwexec("/usr/local/sbin/dnsmasq --cache-size=5000 {$args}");
660

    
661
		if ($g['booting'])
662
			echo "done.\n";
663
	}
664

    
665
	if (!$g['booting']) {
666
		if(services_dhcpd_configure()!=0)
667
			$return = 1;
668
	}
669

    
670
	return $return;
671
}
672

    
673
function services_snmpd_configure() {
674
	global $config, $g;
675
	if(isset($config['system']['developerspew'])) {
676
		$mt = microtime();
677
		echo "services_snmpd_configure() being called $mt\n";
678
	}
679

    
680
	/* kill any running snmpd */
681
	sigkillbypid("{$g['varrun_path']}/snmpd.pid", "TERM");
682
	if(is_process_running("bsnmpd")) 
683
		exec("/usr/bin/killall bsnmpd");
684

    
685
	if (isset($config['snmpd']['enable'])) {
686

    
687
		if ($g['booting'])
688
			echo "Starting SNMP daemon... ";
689

    
690
		/* generate snmpd.conf */
691
		$fd = fopen("{$g['varetc_path']}/snmpd.conf", "w");
692
		if (!$fd) {
693
			printf("Error: cannot open snmpd.conf in services_snmpd_configure().\n");
694
			return 1;
695
		}
696

    
697

    
698
		$snmpdconf = <<<EOD
699
location := "{$config['snmpd']['syslocation']}"
700
contact := "{$config['snmpd']['syscontact']}"
701
read := "{$config['snmpd']['rocommunity']}"
702

    
703
EOD;
704

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

    
711
EOD;
712
		}
713
*/
714

    
715

    
716
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
717
		    $snmpdconf .= <<<EOD
718
# SNMP Trap support.
719
traphost := {$config['snmpd']['trapserver']}
720
trapport := {$config['snmpd']['trapserverport']}
721
trap := "{$config['snmpd']['trapstring']}"
722

    
723

    
724
EOD;
725
		}
726

    
727

    
728
		$snmpdconf .= <<<EOD
729
system := 1     # pfSense
730
%snmpd
731
begemotSnmpdDebugDumpPdus       = 2
732
begemotSnmpdDebugSyslogPri      = 7
733
begemotSnmpdCommunityString.0.1 = $(read)
734

    
735
EOD;
736

    
737
/* No docs on what write strings do there for disable for now.
738
		if(isset($config['snmpd']['rwcommunity']) && preg_match('/^\S+$/', $config['snmpd']['rwcommunity'])){
739
		    $snmpdconf .= <<<EOD
740
begemotSnmpdCommunityString.0.2 = $(write)
741

    
742
EOD;
743
		}
744
*/
745

    
746

    
747
		if(isset($config['snmpd']['trapenable']) && preg_match('/^\S+$/', $config['snmpd']['trapserver'])){
748
		    $snmpdconf .= <<<EOD
749
begemotTrapSinkStatus.[$(traphost)].$(trapport) = 4
750
begemotTrapSinkVersion.[$(traphost)].$(trapport) = 2
751
begemotTrapSinkComm.[$(traphost)].$(trapport) = $(trap)
752

    
753
EOD;
754
		}
755

    
756

    
757
		$snmpdconf .= <<<EOD
758
begemotSnmpdCommunityDisable    = 1
759

    
760
EOD;
761

    
762
		if(isset($config['snmpd']['bindlan'])) {
763
			$bind_to_ip = $config['interfaces']['lan']['ipaddr'];
764
		} else {
765
			$bind_to_ip = "0.0.0.0";
766
		}
767

    
768
		if(is_port( $config['snmpd']['pollport'] )) {
769
		    $snmpdconf .= <<<EOD
770
begemotSnmpdPortStatus.{$bind_to_ip}.{$config['snmpd']['pollport']} = 1
771

    
772
EOD;
773

    
774
		}
775

    
776
		$snmpdconf .= <<<EOD
777
begemotSnmpdLocalPortStatus."/var/run/snmpd.sock" = 1
778
begemotSnmpdLocalPortType."/var/run/snmpd.sock" = 4
779

    
780
# These are bsnmp macros not php vars.
781
sysContact      = $(contact)
782
sysLocation     = $(location)
783
sysObjectId     = 1.3.6.1.4.1.12325.1.1.2.1.$(system)
784

    
785
snmpEnableAuthenTraps = 2
786

    
787
EOD;
788

    
789
		if (is_array( $config['snmpd']['modules'] )) {
790
		    if(isset($config['snmpd']['modules']['mibii'])) {
791
			$snmpdconf .= <<<EOD
792
begemotSnmpdModulePath."mibII"  = "/usr/lib/snmp_mibII.so"
793

    
794
EOD;
795
		    }
796

    
797
		    if(isset($config['snmpd']['modules']['netgraph'])) {
798
			$snmpdconf .= <<<EOD
799
begemotSnmpdModulePath."netgraph" = "/usr/lib/snmp_netgraph.so"
800
%netgraph
801
begemotNgControlNodeName = "snmpd"
802

    
803
EOD;
804
		    }
805

    
806
		    if(isset($config['snmpd']['modules']['pf'])) {
807
			$snmpdconf .= <<<EOD
808
begemotSnmpdModulePath."pf"     = "/usr/lib/snmp_pf.so"
809

    
810
EOD;
811
		    }
812

    
813
		    if(isset($config['snmpd']['modules']['hostres'])) {
814
			$snmpdconf .= <<<EOD
815
begemotSnmpdModulePath."hostres"     = "/usr/lib/snmp_hostres.so"
816

    
817
EOD;
818
		    }
819
		    if(isset($config['snmpd']['modules']['bridge'])) {
820
			$snmpdconf .= <<<EOD
821
begemotSnmpdModulePath."bridge"     = "/usr/lib/snmp_bridge.so"
822
# config must end with blank line
823

    
824

    
825
EOD;
826
		    }
827
		}
828

    
829
		fwrite($fd, $snmpdconf);
830
		fclose($fd);
831

    
832
		if (isset($config['snmpd']['bindlan'])) {
833
			$bindlan = "";
834
		}
835

    
836
		/* run bsnmpd */
837
		mwexec("/usr/sbin/bsnmpd -c {$g['varetc_path']}/snmpd.conf" .
838
			"{$bindlan} -p {$g['varrun_path']}/snmpd.pid");
839

    
840
		if ($g['booting'])
841
			echo "done.\n";
842
	}
843

    
844
	return 0;
845
}
846

    
847
function services_proxyarp_configure() {
848
	global $config, $g;
849
	if(isset($config['system']['developerspew'])) {
850
		$mt = microtime();
851
		echo "services_proxyarp_configure() being called $mt\n";
852
	}
853

    
854
	/* kill any running choparp */
855
	killbyname("choparp");
856

    
857
	if (isset($config['virtualip']) && is_array($config['virtualip']['vip'])) {
858
		$paa = array();
859

    
860
		/* group by interface */
861
		foreach ($config['virtualip']['vip'] as $vipent) {
862
			if ($vipent['mode'] === "proxyarp") {
863
				if ($vipent['interface'])
864
					$if = $vipent['interface'];
865
				else
866
					$if = "wan";
867

    
868
				if (!is_array($paa[$if]))
869
					$paa[$if] = array();
870

    
871
				$paa[$if][] = $vipent;
872
			}
873
		}
874

    
875
		if (count($paa))
876
		foreach ($paa as $paif => $paents) {
877
			if ($paif == "wan" && !(is_ipaddr($config['interfaces']['wan']['ipaddr']) ||
878
                                       ($config['interfaces']['wan']['ipaddr'] == "dhcp") ||
879
                                       ($config['interfaces']['wan']['ipaddr'] == "bigpond")))
880
                               continue;
881

    
882
			$args = $config['interfaces'][$paif]['if'] . " auto";
883

    
884
			foreach ($paents as $paent) {
885

    
886
				if (isset($paent['subnet']))
887
					$args .= " " . escapeshellarg("{$paent['subnet']}/{$paent['subnet_bits']}");
888
				else if (isset($paent['range']))
889
					$args .= " " . escapeshellarg($paent['range']['from'] . "-" .
890
						$paent['range']['to']);
891
			}
892

    
893
			mwexec_bg("/usr/local/sbin/choparp " . $args);
894
		}
895
	}
896
}
897

    
898
function services_dnsupdate_process() {
899
	global $config, $g;
900
	if(isset($config['system']['developerspew'])) {
901
		$mt = microtime();
902
		echo "services_dnsupdate_process() being called $mt\n";
903
	}
904

    
905
	/* Dynamic DNS updating active? */
906
	if (isset($config['dnsupdate']['enable'])) {
907

    
908
		$wanip = get_current_wan_address();
909
		if ($wanip) {
910

    
911
			$keyname = $config['dnsupdate']['keyname'];
912
			/* trailing dot */
913
			if (substr($keyname, -1) != ".")
914
				$keyname .= ".";
915

    
916
			$hostname = $config['dnsupdate']['host'];
917
			/* trailing dot */
918
			if (substr($hostname, -1) != ".")
919
				$hostname .= ".";
920

    
921
			/* write private key file
922
			   this is dumb - public and private keys are the same for HMAC-MD5,
923
			   but nsupdate insists on having both */
924
			$fd = fopen("{$g['varetc_path']}/K{$keyname}+157+00000.private", "w");
925
			$privkey .= <<<EOD
926
Private-key-format: v1.2
927
Algorithm: 157 (HMAC)
928
Key: {$config['dnsupdate']['keydata']}
929

    
930
EOD;
931
			fwrite($fd, $privkey);
932
			fclose($fd);
933

    
934
			/* write public key file */
935
			if ($config['dnsupdate']['keytype'] == "zone") {
936
				$flags = 257;
937
				$proto = 3;
938
			} else if ($config['dnsupdate']['keytype'] == "host") {
939
				$flags = 513;
940
				$proto = 3;
941
			} else if ($config['dnsupdate']['keytype'] == "user") {
942
				$flags = 0;
943
				$proto = 2;
944
			}
945

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

    
950
			/* generate update instructions */
951
			$upinst = "";
952
			if (!empty($config['dnsupdate']['server']))
953
				$upinst .= "server {$config['dnsupdate']['server']}\n";
954
			$upinst .= "update delete {$config['dnsupdate']['host']} A\n";
955
			$upinst .= "update add {$config['dnsupdate']['host']} {$config['dnsupdate']['ttl']} A {$wanip}\n";
956
			$upinst .= "\n";	/* mind that trailing newline! */
957

    
958
			$fd = fopen("{$g['varetc_path']}/nsupdatecmds", "w");
959
			fwrite($fd, $upinst);
960
			fclose($fd);
961

    
962
			/* invoke nsupdate */
963
			$cmd = "/usr/sbin/nsupdate -k {$g['varetc_path']}/K{$keyname}+157+00000.key";
964
			if (isset($config['dnsupdate']['usetcp']))
965
				$cmd .= " -v";
966
			$cmd .= " {$g['varetc_path']}/nsupdatecmds";
967

    
968
			mwexec_bg($cmd);
969
		}
970
	}
971

    
972
	return 0;
973
}
974

    
975
function setup_wireless_olsr() {
976
	global $config, $g;
977
	if(!$config['installedpackages']['olsrd'] || !$config['installedpackages'])
978
		return;
979
	if(isset($config['system']['developerspew'])) {
980
		$mt = microtime();
981
		echo "setup_wireless_olsr($interface) being called $mt\n";
982
	}
983
	conf_mount_rw();
984
	foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
985
		$olsr_enable = $olsrd['enable'];
986
		if($olsr_enable <> "on")
987
			return;
988
		$fd = fopen("{$g['varetc_path']}/olsr.conf", "w");
989

    
990
		if($olsrd['announcedynamicroute'] or $olsrd['enableannounce'] == "on") {
991
			$enableannounce .= "\nHna4\n";
992
			$enableannounce .= "{\n";
993
		if($olsrd['announcedynamicroute'])
994
			$enableannounce .= "\t{$olsrd['announcedynamicroute']}\n";
995
		if($olsrd['enableannounce'] == "on")
996
			$enableannounce .= "0.0.0.0 0.0.0.0";
997
			$enableannounce .= "\n}\n";
998
		} else {
999
			$enableannounce = "";
1000
		}
1001

    
1002
		$olsr .= <<<EODA
1003
#
1004
# olsr.org OLSR daemon config file
1005
#
1006
# Lines starting with a # are discarded
1007
#
1008
# This file was generated by setup_wireless_olsr() in services.inc
1009
#
1010

    
1011
# This file is an example of a typical
1012
# configuration for a mostly static
1013
# network(regarding mobility) using
1014
# the LQ extention
1015

    
1016
# Debug level(0-9)
1017
# If set to 0 the daemon runs in the background
1018

    
1019
DebugLevel	2
1020

    
1021
# IP version to use (4 or 6)
1022

    
1023
IpVersion	4
1024

    
1025
# Clear the screen each time the internal state changes
1026

    
1027
ClearScreen     yes
1028

    
1029
{$enableannounce}
1030

    
1031
# Should olsrd keep on running even if there are
1032
# no interfaces available? This is a good idea
1033
# for a PCMCIA/USB hotswap environment.
1034
# "yes" OR "no"
1035

    
1036
AllowNoInt	yes
1037

    
1038
# TOS(type of service) value for
1039
# the IP header of control traffic.
1040
# If not set it will default to 16
1041

    
1042
#TosValue	16
1043

    
1044
# The fixed willingness to use(0-7)
1045
# If not set willingness will be calculated
1046
# dynamically based on battery/power status
1047
# if such information is available
1048

    
1049
#Willingness    	4
1050

    
1051
# Allow processes like the GUI front-end
1052
# to connect to the daemon.
1053

    
1054
IpcConnect
1055
{
1056
     # Determines how many simultaneously
1057
     # IPC connections that will be allowed
1058
     # Setting this to 0 disables IPC
1059

    
1060
     MaxConnections  0
1061

    
1062
     # By default only 127.0.0.1 is allowed
1063
     # to connect. Here allowed hosts can
1064
     # be added
1065

    
1066
     Host            127.0.0.1
1067
     #Host            10.0.0.5
1068

    
1069
     # You can also specify entire net-ranges
1070
     # that are allowed to connect. Multiple
1071
     # entries are allowed
1072

    
1073
     #Net             192.168.1.0 255.255.255.0
1074
}
1075

    
1076
# Wether to use hysteresis or not
1077
# Hysteresis adds more robustness to the
1078
# link sensing but delays neighbor registration.
1079
# Used by default. 'yes' or 'no'
1080

    
1081
UseHysteresis	no
1082

    
1083
# Hysteresis parameters
1084
# Do not alter these unless you know
1085
# what you are doing!
1086
# Set to auto by default. Allowed
1087
# values are floating point values
1088
# in the interval 0,1
1089
# THR_LOW must always be lower than
1090
# THR_HIGH.
1091

    
1092
#HystScaling	0.50
1093
#HystThrHigh	0.80
1094
#HystThrLow	0.30
1095

    
1096

    
1097
# Link quality level
1098
# 0 = do not use link quality
1099
# 1 = use link quality for MPR selection
1100
# 2 = use link quality for MPR selection and routing
1101
# Defaults to 0
1102

    
1103
LinkQualityLevel	{$olsrd['enablelqe']}
1104

    
1105
# Link quality window size
1106
# Defaults to 10
1107

    
1108
LinkQualityWinSize	10
1109

    
1110
# Polling rate in seconds(float).
1111
# Default value 0.05 sec
1112

    
1113
Pollrate	0.05
1114

    
1115

    
1116
# TC redundancy
1117
# Specifies how much neighbor info should
1118
# be sent in TC messages
1119
# Possible values are:
1120
# 0 - only send MPR selectors
1121
# 1 - send MPR selectors and MPRs
1122
# 2 - send all neighbors
1123
#
1124
# defaults to 0
1125

    
1126
TcRedundancy	2
1127

    
1128
#
1129
# MPR coverage
1130
# Specifies how many MPRs a node should
1131
# try select to reach every 2 hop neighbor
1132
#
1133
# Can be set to any integer >0
1134
#
1135
# defaults to 1
1136

    
1137
MprCoverage	3
1138

    
1139
# Example plugin entry with parameters:
1140

    
1141
EODA;
1142

    
1143
if($olsrd['enablehttpinfo'] == "on") {
1144
	$olsr .= <<<EODB
1145

    
1146
LoadPlugin "/usr/local/lib/olsrd_httpinfo.so.0.1"
1147
{
1148
    PlParam     "port"   "{$olsrd['port']}"
1149
    PlParam     "Net"    "{$olsrd['allowedhttpinfohost']} {$olsrd['allowedhttpinfosubnet']}"
1150
}
1151

    
1152
EODB;
1153

    
1154
}
1155

    
1156
if($olsrd['enabledsecure'] == "on") {
1157
	$olsr .= <<<EODC
1158

    
1159
LoadPlugin "/usr/local/lib/olsrd_secure.so.0.5"
1160
{
1161
    PlParam     "Keyfile"   "/usr/local/etc/olsrkey.txt"
1162
}
1163

    
1164
EODC;
1165

    
1166
}
1167

    
1168
if($olsrd['enabledyngw'] == "on") {
1169

    
1170
	/* unset default route, olsr auto negotiates */
1171
	mwexec("/sbin/route delete default");
1172

    
1173
	$olsr .= <<<EODE
1174

    
1175
LoadPlugin "/usr/local/lib/olsrd_dyn_gw.so.0.4"
1176
{
1177
    # how often to look for a inet gw, in seconds
1178
    # defaults to 5 secs, if commented out
1179
    PlParam     "Interval"   "{$olsrd['polling']}"
1180

    
1181
    # if one or more IPv4 addresses are given, do a ping on these in
1182
    # descending order to validate that there is not only an entry in
1183
    # routing table, but also a real internet connection. If any of
1184
    # these addresses could be pinged successfully, the test was
1185
    # succesful, i.e. if the ping on the 1st address was successful,the
1186
    # 2nd won't be pinged
1187
    PlParam     "Ping"       "{$olsrd['ping']}"
1188
    #PlParam     "HNA"   "192.168.81.0 255.255.255.0"
1189
}
1190

    
1191
EODE;
1192

    
1193
}
1194

    
1195
foreach($config['installedpackages']['olsrd']['config'] as $conf) {
1196
	$interfaces = explode(',', $conf['iface_array']);
1197
	foreach($interfaces as $interface) {
1198
		$realinterface = convert_friendly_interface_to_real_interface_name($interface);
1199
$olsr .= <<<EODAD
1200
Interface "{$realinterface}"
1201
{
1202

    
1203
    # Hello interval in seconds(float)
1204
    HelloInterval    2.0
1205

    
1206
    # HELLO validity time
1207
    HelloValidityTime	20.0
1208

    
1209
    # TC interval in seconds(float)
1210
    TcInterval        5.0
1211

    
1212
    # TC validity time
1213
    TcValidityTime	30.0
1214

    
1215
    # MID interval in seconds(float)
1216
    MidInterval	5.0
1217

    
1218
    # MID validity time
1219
    MidValidityTime	30.0
1220

    
1221
    # HNA interval in seconds(float)
1222
    HnaInterval	5.0
1223

    
1224
    # HNA validity time
1225
    HnaValidityTime 	30.0
1226

    
1227
    # When multiple links exist between hosts
1228
    # the weight of interface is used to determine
1229
    # the link to use. Normally the weight is
1230
    # automatically calculated by olsrd based
1231
    # on the characteristics of the interface,
1232
    # but here you can specify a fixed value.
1233
    # Olsrd will choose links with the lowest value.
1234

    
1235
    # Weight 0
1236

    
1237

    
1238
}
1239

    
1240
EODAD;
1241

    
1242
	}
1243
	break;
1244
}
1245
		fwrite($fd, $olsr);
1246
		fclose($fd);
1247
	}
1248

    
1249
	if(is_process_running("olsrd"))
1250
		mwexec("/usr/bin/killall olsrd");
1251

    
1252
	sleep(2);
1253

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

    
1256
	conf_mount_ro();
1257
}
1258

    
1259
/* configure cron service */
1260
function configure_cron() {
1261
	global $g, $config;
1262
	conf_mount_rw();
1263
	/* preserve existing crontab entries */
1264
	$crontab_contents = file_get_contents("/etc/crontab");
1265
	$crontab_contents_a = split("\n", $crontab_contents);
1266
	
1267
	for ($i = 0; $i < count($crontab_contents_a); $i++) {
1268
		$item =& $crontab_contents_a[$i];
1269
		if (strpos($item, "# pfSense specific crontab entries") !== false) {
1270
			array_splice($crontab_contents_a, $i - 1);
1271
			break;
1272
		}
1273
	}
1274
	$crontab_contents = implode("\n", $crontab_contents_a) . "\n";
1275
	
1276
	
1277
	if (is_array($config['cron']['item'])) {
1278
		$crontab_contents .= "#\n";
1279
		$crontab_contents .= "# pfSense specific crontab entries\n";
1280
		$crontab_contents .= "# Created: " . date("F j, Y, g:i a") . "\n";
1281
		$crontab_contents .= "#\n";
1282

    
1283
		foreach ($config['cron']['item'] as $item) {
1284
			$crontab_contents .= "\n{$item['minute']}\t";
1285
			$crontab_contents .= "{$item['hour']}\t";
1286
			$crontab_contents .= "{$item['mday']}\t";
1287
			$crontab_contents .= "{$item['month']}\t";
1288
			$crontab_contents .= "{$item['wday']}\t";
1289
			$crontab_contents .= "{$item['who']}\t";
1290
			$crontab_contents .= "{$item['command']}";
1291
		}
1292
    
1293
		$crontab_contents .= "\n#\n";
1294
		$crontab_contents .= "# If possible do not add items to this file manually.\n";
1295
		$crontab_contents .= "# If you do so, this file must be terminated with a blank line (e.g. new line)\n";
1296
		$crontab_contents .= "#\n\n";
1297
	}
1298
	
1299
	/* please maintain the newline at the end of file */
1300
	file_put_contents("/etc/crontab", $crontab_contents);
1301
	
1302
	if (!$g['booting'])
1303
		conf_mount_ro();
1304
}
1305

    
1306
function upnp_action ($action) {
1307
	switch($action) {
1308
		case "start":
1309
			if(file_exists('/var/etc/miniupnpd.conf'))
1310
				mwexec_bg('/usr/local/sbin/miniupnpd -f /var/etc/miniupnpd.conf');
1311
			break;
1312
		case "stop":
1313
			while((int)exec("pgrep miniupnpd | wc -l") > 0)
1314
				mwexec('killall miniupnpd 2>/dev/null');
1315
			mwexec('/sbin/pfctl -aminiupnpd -Fr 2>&1 >/dev/null');
1316
			mwexec('/sbin/pfctl -aminiupnpd -Fn 2>&1 >/dev/null');
1317
			break;
1318
		case "restart":
1319
			upnp_action('stop');
1320
			upnp_action('start');
1321
			break;
1322
	}
1323
}
1324

    
1325
function upnp_start() {
1326
	global $config, $g;
1327
	if($config['installedpackages']['miniupnpd']['config'][0]['enable']) {
1328
		if($g['booting']) {
1329
			echo "Starting UPnP service... ";
1330
			include('/usr/local/pkg/miniupnpd.inc');
1331
			sync_package_miniupnpd();
1332
			echo "done.\n";
1333
		}
1334
		else {
1335
			upnp_action('start');
1336
		}
1337
	}
1338
}
1339

    
1340
?>
(19-19/29)