Project

General

Profile

Download (36.8 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/* $Id$ */
3
/*
4
	system.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
/*
33
	pfSense_BUILDER_BINARIES:	/usr/sbin/powerd	/usr/bin/killall	/sbin/sysctl	/sbin/route
34
	pfSense_BUILDER_BINARIES:	/bin/hostname	/bin/ls	/usr/bin/netstat	/usr/sbin/syslogd	
35
	pfSense_BUILDER_BINARIES:	/usr/sbin/pccardd	/usr/local/sbin/lighttpd	/bin/chmod 	/bin/mkdir
36
	pfSense_BUILDER_BINARIES:	/usr/bin/tar	/bin/sync	/usr/local/sbin/ntpd	/usr/sbin/ntpdate
37
	pfSense_BUILDER_BINARIES:	/usr/bin/nohup	/sbin/dmesg	/usr/local/sbin/atareinit
38
	pfSense_MODULE:	utils
39
*/
40

    
41
function activate_powerd() {
42
	global $config, $g;
43
	if(isset($config['system']['powerd_enable'])) {
44
		exec("/usr/sbin/powerd -b adp -a adp");
45
	} else {
46
		exec("/usr/bin/killall powerd");
47
	}
48
}
49

    
50
function activate_sysctls() {
51
	global $config, $g;
52
	
53
	exec("/sbin/sysctl net.enc.out.ipsec_bpf_mask=0x00000001");
54
	exec("/sbin/sysctl net.enc.out.ipsec_filter_mask=0x00000001");
55
	exec("/sbin/sysctl net.enc.in.ipsec_bpf_mask=0x00000002");
56
	exec("/sbin/sysctl net.enc.in.ipsec_filter_mask=0x00000002");
57

    
58
	if (is_array($config['sysctl'])) 
59
		foreach ($config['sysctl']['item'] as $tunable) 
60
			mwexec("sysctl " . $tunable['tunable'] . "=\"" 
61
				. $tunable['value'] .  "\"");
62
}
63

    
64
function system_resolvconf_generate($dynupdate = false) {
65
	global $config, $g;
66

    
67
	if(isset($config['system']['developerspew'])) {
68
		$mt = microtime();
69
		echo "system_resolvconf_generate() being called $mt\n";
70
	}
71

    
72
        $syscfg = $config['system'];
73

    
74
        $fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
75
        if (!$fd) {
76
                printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
77
                return 1;
78
        }
79

    
80
        $resolvconf = "domain {$syscfg['domain']}\n";
81

    
82
        $havedns = false;
83

    
84
        if (isset($syscfg['dnsallowoverride'])) {
85
		/* get dynamically assigned DNS servers (if any) */
86
		$ns = array_unique(get_nameservers());
87
		foreach($ns as $nameserver) {
88
			if($nameserver) {
89
				$resolvconf .= "nameserver $nameserver\n";
90
				$havedns = true;
91
			}
92
		}
93
        }
94
        if (!$havedns && is_array($syscfg['dnsserver'])) {
95
		foreach ($syscfg['dnsserver'] as $ns) {
96
			if ($ns) {
97
				$resolvconf .= "nameserver $ns\n";
98
				$havedns = true;
99
			}
100
		}
101
	}
102

    
103
        fwrite($fd, $resolvconf);
104
        fclose($fd);
105

    
106
        if (!$g['booting']) {
107
		/* restart dhcpd (nameservers may have changed) */
108
		if (!$dynupdate)
109
			services_dhcpd_configure();
110
        }
111

    
112
	/* setup static routes for DNS servers. */
113
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
114
		/* setup static routes for dns servers */
115
		$dnsgw = "dns{$dnscounter}gwint";
116
		if (isset($config['system'][$dnsgw])) {
117
			$interface = $config['system'][$dnsgw];
118
			if (($interface <> "") && ($interface <> "none")) {
119
				$gatewayip = get_interface_gateway($interface);
120
				if(is_ipaddr($gatewayip)) {
121
					/* dns server array starts at 0 */
122
					$dnscountermo = $dnscounter - 1;
123
					mwexec("route delete -host {$syscfg['dnsserver'][$dnscountermo]}");
124
					mwexec("route add -host {$syscfg['dnsserver'][$dnscountermo]} {$gatewayip}");
125
				}
126
			}
127
		}
128
	}
129
	
130
	return 0;
131
}
132

    
133
function get_nameservers() {
134
	global $config, $g;
135
	$master_list = array();
136
	$dns_lists = split("\n", `ls /var/etc/nameserver_* 2>/dev/null`);
137
	if(is_array($dns_lists)) {
138
		foreach($dns_lists as $dns) {
139
			if(!$dns) 
140
				continue;
141
			$items = split("\n", file_get_contents($dns));
142
			foreach($items as $item)
143
				if($item <> "")
144
					$master_list[] = $item;
145
		}
146
	}
147
	if(!file_exists("/var/etc/nameservers.conf"))
148
		return $master_list;
149
	$dns = `cat /var/etc/nameservers.conf`;
150
	$dns_s = split("\n", $dns);
151
	if(is_array($dns_s))
152
		foreach($dns_s as $dns)
153
			$master_list[] = $dns;
154
	return $master_list;
155
}
156

    
157
function system_hosts_generate() {
158
	global $config, $g;
159
	if(isset($config['system']['developerspew'])) {
160
		$mt = microtime();
161
		echo "system_hosts_generate() being called $mt\n";
162
	}
163

    
164
	$syscfg = $config['system'];
165
	$lancfg = $config['interfaces']['lan'];
166
	$lancfgip = get_interface_ip("lan");
167
	$dnsmasqcfg = $config['dnsmasq'];
168

    
169
	if (!is_array($dnsmasqcfg['hosts'])) {
170
		$dnsmasqcfg['hosts'] = array();
171
	}
172
	$hostscfg = $dnsmasqcfg['hosts'];
173

    
174
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
175
	if (!$fd) {
176
		log_error("Error: cannot open hosts file in system_hosts_generate().\n");
177
		return 1;
178
	}
179

    
180
	$hosts = <<<EOD
181
127.0.0.1	localhost localhost.{$syscfg['domain']}
182

    
183
EOD;
184
	if (is_ipaddr($lancfgip))
185
		$hosts .= <<<EOD
186
{$lancfgip}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
187

    
188
EOD;
189

    
190
	foreach ($hostscfg as $host) {
191
		if ($host['host'])
192
			$hosts .= "{$host['ip']}	{$host['host']}.{$host['domain']} {$host['host']}\n";
193
		else
194
			$hosts .= "{$host['ip']}	{$host['domain']}\n";
195
	}
196
	if (isset($dnsmasqcfg['regdhcpstatic'])) {
197
		foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf)
198
			if(is_array($dhcpifconf['staticmap']) && isset($dhcpifconf['enable']))
199
					foreach ($dhcpifconf['staticmap'] as $host)
200
						if ($host['ipaddr'] && $host['hostname'])
201
							$hosts .= "{$host['ipaddr']}	{$host['hostname']}.{$syscfg['domain']} {$host['hostname']}\n";
202
	}
203
	fwrite($fd, $hosts);
204
	fclose($fd);
205

    
206
	return 0;
207
}
208

    
209
function system_hostname_configure() {
210
	global $config, $g;
211
	if(isset($config['system']['developerspew'])) {
212
		$mt = microtime();
213
		echo "system_hostname_configure() being called $mt\n";
214
	}
215

    
216
	$syscfg = $config['system'];
217

    
218
	/* set hostname */
219
	$status = mwexec("/bin/hostname " .
220
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
221

    
222
    /* Setup host GUID ID.  This is used by ZFS. */
223
	mwexec("/etc/rc.d/hostid start");
224

    
225
	return $status;
226
}
227

    
228
function system_routing_configure() {
229
	global $config, $g;
230
	if(isset($config['system']['developerspew'])) {
231
		$mt = microtime();
232
		echo "system_routing_configure() being called $mt\n";
233
	}
234

    
235
	/* Enable fast routing, if enabled */
236
	if(isset($config['staticroutes']['enablefastrouting']))
237
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
238

    
239
	$route_str = exec_command("/usr/bin/netstat -rn");
240

    
241
	/* clear out old routes, if necessary */
242
	if (file_exists("{$g['vardb_path']}/routes.db")) {
243
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
244
		if (!$fd) {
245
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
246
			return 1;
247
		}
248
		while (!feof($fd)) {
249
			$oldrt = trim(fgets($fd));
250
			if (($oldrt) && (stristr($route_str, $oldrt)))
251
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
252
		}
253
		fclose($fd);
254
		unlink("{$g['vardb_path']}/routes.db");
255
	}
256

    
257
	/* if list */
258
	$iflist = get_configured_interface_list();
259

    
260
	$dont_remove_route = false;
261
	foreach ($iflist as $ifent => $ifname) {
262
		/* 
263
		 * XXX: The value of this is really when this function can take
264
		 * 	an interface as parameter.
265
		 */
266
		/* do not process interfaces that will end up with gateways */
267
		if (interface_has_gateway($ifent) || 
268
			$config['interfaces'][$ifent]['ipaddr'] == "carpdev-dhcp") {
269
			$dont_remove_route = true;
270
			break;
271
		}
272
	}
273

    
274
	if ($dont_remove_route == false) {
275
		/* remove default route */
276
		mwexec("/sbin/route delete default", true);
277
	}
278

    
279
	$dont_add_route = false;
280
	/* if OLSRD is enabled, allow WAN to house DHCP. */
281
	if($config['installedpackages']['olsrd']) {
282
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
283
			if($olsrd['enabledyngw'] == "on") {
284
				$dont_add_route = true;
285
				break;
286
			}
287
		}
288
	}
289

    
290
	if($dont_add_route == false) {
291
		if(is_array($config['gateways']['gateway_item'])) {
292
			foreach($config['gateways']['gateway_item'] as $gateway) {
293
		        	if(isset($gateway['defaultgw'])) {
294
					$gatewayip = $gateway['gateway'];
295
					$interfacegw = $gateway['interface'];
296
				}
297
			}
298
			if(($interfacegw <> "bgpd") && (is_ipaddr($gatewayip)))
299
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
300
		} else {
301
			/* FIXME */
302
			/* adding gateway for 1.2-style configs without the new
303
			  gateway setup configured.
304
			  Force WAN to be default gateway because that is the
305
			  1.2 behavior.
306
			*/
307
			if (is_ipaddr($config['interfaces']['wan']['gateway'])) {
308
				$gatewayip = $config['interfaces']['wan']['gateway'];
309
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
310
			}
311
		}
312
	}
313

    
314
	if (is_array($config['staticroutes']['route'])) {
315

    
316
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
317
		if (!$fd) {
318
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
319
			return 1;
320
		}
321

    
322
		foreach ($config['staticroutes']['route'] as $rtent) {
323
			unset($gatewayip);
324
			unset($interfacegw);
325
			if(is_array($config['gateways']['gateway_item'])) {
326
				foreach($config['gateways']['gateway_item'] as $gateway) {
327
					if($rtent['gateway'] == $gateway['name']) {
328
						$gatewayip = $gateway['gateway'];
329
						$interfacegw = $gateway['interface'];
330
					}
331
				}
332
			}
333
			if((is_ipaddr($rtent['gateway'])) && ($gatewayip == ""))  {
334
				$gatewayip = $rtent['gateway'];
335
				$interfacegw = $rtent['interface'];
336
			}			
337
			if((isset($rtent['interfacegateway'])) && (! is_ipaddr($gatewayip))){
338
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
339
					" -iface " . escapeshellarg(convert_friendly_interface_to_real_interface_name($interfacegw)));
340
			} else {
341
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
342
					" " . escapeshellarg($gatewayip));
343
			}
344
			/* record route so it can be easily removed later (if necessary) */
345
			fwrite($fd, $rtent['network'] . "\n");
346
		}
347
		fclose($fd);
348
	}
349

    
350
	return 0;
351
}
352

    
353

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

    
361
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
362
}
363

    
364
function system_syslogd_start() {
365
	global $config, $g;
366
	if(isset($config['system']['developerspew'])) {
367
		$mt = microtime();
368
		echo "system_syslogd_start() being called $mt\n";
369
	}
370

    
371
	$syslogcfg = $config['syslog'];
372

    
373
	if ($g['booting'])
374
		echo "Starting syslog...";
375
	else
376
		killbypid("{$g['varrun_path']}/syslog.pid");
377

    
378
	mwexec("/usr/bin/killall -9 syslogd");
379
	mwexec("/usr/bin/killall -9 fifolog_writer");
380
	
381
	// Define carious commands for logging
382
	$fifolog_create = "/usr/sbin/fifolog_create -s ";
383
	$fifolog_log = "|/usr/sbin/fifolog_writer ";
384
	$clog_create = "/usr/sbin/clog -i -s ";
385
	$clog_log = "%";
386

    
387
	// Which logging type are we using this week??
388
	if(isset($config['system']['usefifolog'])) {
389
		$log_directive = $fifolog_log;
390
		$log_create_directive = $fifolog_create;		
391
	} else { // Defaults to CLOG
392
		$log_directive = $clog_log;
393
		$log_create_directive = $clog_create;
394
	}
395
	
396
	if (isset($syslogcfg)) {
397
		$separatelogfacilities = array('ntpd','racoon','openvpn');
398
		if($config['installedpackages']['package']) {
399
			foreach($config['installedpackages']['package'] as $package) {
400
				if($package['logging']) {
401
					$pkgfacilities[] = $package['logging']['facilityname'];
402
					$separatelogfacilities = $separatelogfacilities + $pkgfacilities;
403
					$facilitylist = implode(',', $pkgfacilities);
404
					mwexec("{$log_create_directive} 10240 {$g['varlog_path']}/{$package['logging']['logfilename']}");
405
					$syslogconf .= "!{$facilitylist}\n*.*\t\t\t\t\t\t {$log_directive}{$g['varlog_path']}/{$package['logging']['logfilename']}\n";
406
				}
407
			}
408
		}
409
		$facilitylist = implode(',', array_unique($separatelogfacilities));
410
		/* write syslog.conf */		
411
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
412
		if (!$fd) {
413
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
414
			return 1;
415
		}
416
		$syslogconf .= "!ntpdate,!ntpd\n";
417
		if (!isset($syslogcfg['disablelocallogging'])) 
418
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/ntpd.log\n";
419
		$syslogconf .= "!racoon\n";
420
		if (!isset($syslogcfg['disablelocallogging'])) 
421
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/ipsec.log\n";
422
		$syslogconf .= "!apinger\n";
423
		if (!isset($syslogcfg['disablelocallogging'])) 
424
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/slbd.log\n";
425
		if (isset($syslogcfg['vpn'])) {
426
			if($syslogcfg['remoteserver'])
427
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver']}\n";
428
			if($syslogcfg['remoteserver2'])
429
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver2']}\n";
430
			if($syslogcfg['remoteserver3'])
431
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
432
		}
433
		$syslogconf .= "!openvpn\n";
434
		if (!isset($syslogcfg['disablelocallogging'])) 
435
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/openvpn.log\n";
436
		if (isset($syslogcfg['vpn'])) {
437
			if($syslogcfg['remoteserver'])
438
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver']}\n";
439
			if($syslogcfg['remoteserver2'])
440
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
441
			if($syslogcfg['remoteserver3'])
442
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
443
		}
444
		$syslogconf .= "!-{$facilitylist}\n";
445
		if (!isset($syslogcfg['disablelocallogging'])) 
446
			$syslogconf .= <<<EOD
447
local0.*										 {$log_directive}{$g['varlog_path']}/filter.log
448
!mpd
449
*.*										 {$log_directive}{$g['varlog_path']}/vpn.log
450
local4.*										 {$log_directive}{$g['varlog_path']}/portalauth.log
451
local7.*										 {$log_directive}{$g['varlog_path']}/dhcpd.log
452
*.notice;kern.debug;lpr.info;mail.crit; 		 {$log_directive}{$g['varlog_path']}/system.log
453
news.err;local0.none;local3.none;local4.none; 	 {$log_directive}{$g['varlog_path']}/system.log
454
local7.none										 {$log_directive}{$g['varlog_path']}/system.log
455
security.*										 {$log_directive}{$g['varlog_path']}/system.log
456
auth.info;authpriv.info;daemon.info				 {$log_directive}{$g['varlog_path']}/system.log
457
local1.*										 {$log_directive}{$g['varlog_path']}/relayd.log
458
auth.info;authpriv.info 						 |exec /usr/local/sbin/sshlockout_pf
459
*.emerg											 *
460

    
461
EOD;
462
		if (isset($syslogcfg['filter'])) {
463
			if($syslogcfg['remoteserver'])
464
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver']}\n";
465
			if($syslogcfg['remoteserver2'])
466
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver2']}\n";
467
			if($syslogcfg['remoteserver3'])
468
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver3']}\n";
469

    
470
		}
471
		if (isset($syslogcfg['vpn'])) {
472
			if($syslogcfg['remoteserver'])
473
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver']}\n";
474
			if($syslogcfg['remoteserver2'])
475
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver2']}\n";
476
			if($syslogcfg['remoteserver3'])
477
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver3']}\n";
478
		}
479
		if (isset($syslogcfg['portalauth'])) {
480
			if($syslogcfg['remoteserver'])
481
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver']}\n";
482
			if($syslogcfg['remoteserver2'])
483
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver2']}\n";
484
			if($syslogcfg['remoteserver3'])
485
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver3']}\n";
486
		}
487
		if (isset($syslogcfg['dhcp'])) {
488
			if($syslogcfg['remoteserver'])
489
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver']}\n";
490
			if($syslogcfg['remoteserver2'])
491
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver2']}\n";
492
			if($syslogcfg['remoteserver3'])
493
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver3']}\n";
494
		}
495
		if (isset($syslogcfg['system'])) {
496
			if($syslogcfg['remoteserver'])
497
				$syslogconf .= <<<EOD
498
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver']}
499
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver']}
500
security.*										 @{$syslogcfg['remoteserver']}
501
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver']}
502
*.emerg											 @{$syslogcfg['remoteserver']}
503

    
504
EOD;
505

    
506
		if (isset($syslogcfg['system'])) {
507
			if($syslogcfg['remoteserver2'])
508
				$syslogconf .= <<<EOD
509
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver2']}
510
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver2']}
511
security.*										 @{$syslogcfg['remoteserver2']}
512
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver2']}
513
*.emerg											 @{$syslogcfg['remoteserver2']}
514

    
515
EOD;
516

    
517
		if (isset($syslogcfg['system'])) {
518
			if($syslogcfg['remoteserver3'])
519
				$syslogconf .= <<<EOD
520
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver3']}
521
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver3']}
522
security.*										 @{$syslogcfg['remoteserver3']}
523
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver3']}
524
*.emerg											 @{$syslogcfg['remoteserver3']}
525

    
526
EOD;
527

    
528
}
529
		if (isset($syslogcfg['logall'])) {
530
			if($syslogcfg['remoteserver'])
531
				$syslogconf .= <<<EOD
532
*.*								@{$syslogcfg['remoteserver']}
533

    
534
EOD;
535

    
536
}
537
			if($syslogcfg['remoteserver2'])
538
				$syslogconf .= <<<EOD
539
*.*								@{$syslogcfg['remoteserver2']}
540

    
541
EOD;
542

    
543
}
544
			if($syslogcfg['remoteserver3'])
545
				$syslogconf .= <<<EOD
546
*.*								@{$syslogcfg['remoteserver3']}
547

    
548
EOD;
549

    
550
}
551
		fwrite($fd, $syslogconf);
552
		fclose($fd);
553
		// Are we logging to a least one remote server ?
554
		if(strpos($syslogconf, "@") != false)
555
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
556
		else
557
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
558

    
559
	} else {
560
		$retval = mwexec("/usr/sbin/syslogd -c");
561
	}
562

    
563
	if ($g['booting'])
564
		echo "done.\n";
565

    
566
	return $retval;
567
}
568

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

    
576
	if ($g['booting'])
577
		echo "Initializing PCMCIA...";
578

    
579
	/* kill any running pccardd */
580
	killbypid("{$g['varrun_path']}/pccardd.pid");
581

    
582
	/* fire up pccardd */
583
	$res = mwexec("/usr/sbin/pccardd -z -f {$g['etc_path']}/pccard.conf");
584

    
585
	if ($g['booting']) {
586
		if ($res == 0)
587
			echo "done.\n";
588
		else
589
			echo "failed!\n";
590
	}
591

    
592
	return $res;
593
}
594

    
595

    
596
function system_webgui_start() {
597
	global $config, $g;
598

    
599
	if ($g['booting'])
600
		echo "Starting webConfigurator...";
601

    
602
	/* kill any running lighttpd */
603
	killbypid("{$g['varrun_path']}/lighty-webConfigurator.pid");
604

    
605
	sleep(1);
606

    
607
	chdir($g['www_path']);
608

    
609
	/* defaults */
610
	$portarg = "80";
611
	$crt = "";
612
	$key = "";
613
	$ca = "";
614

    
615
	/* non-standard port? */
616
	if (isset($config['system']['webgui']['port']) && $config['system']['webgui']['port'] <> "")
617
		$portarg = "{$config['system']['webgui']['port']}";
618

    
619
	if ($config['system']['webgui']['protocol'] == "https") {
620

    
621
		$cert =& lookup_cert($config['system']['webgui']['ssl-certref']);
622
		if(is_array($cert) && $cert['crt'] && $cert['prv']) {
623
			$crt = base64_decode($cert['crt']);
624
			$key = base64_decode($cert['prv']);
625
			if(!$config['system']['webgui']['port'])
626
				$portarg = "443";
627
			$ca = ca_chain($cert);
628
		} else
629
			log_error("Invalid webConfigurator https certificate, defaulting to http");
630
	}
631

    
632
	/* generate lighttpd configuration */
633
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
634
		$crt, $key, $ca, "lighty-webConfigurator.pid", $portarg, "/usr/local/www/");
635

    
636
	/* attempt to start lighthttpd */
637
	$res = mwexec("/usr/local/sbin/lighttpd -f {$g['varetc_path']}/lighty-webConfigurator.conf");
638

    
639
	if ($g['booting']) {
640
		if ($res == 0)
641
			echo "done.\n";
642
		else
643
			echo "failed!\n";
644
	}
645

    
646
	return $res;
647
}
648

    
649
function system_generate_lighty_config($filename,
650
	$cert,
651
	$key,
652
	$ca,
653
	$pid_file,
654
	$port = 80,
655
	$document_root = "/usr/local/www/",
656
	$cert_location = "cert.pem",
657
	$ca_location = "ca.pem",
658
	$max_procs = 2,
659
	$max_requests = "1",
660
	$fast_cgi_enable = true,
661
	$captive_portal = false) {
662

    
663
	global $config, $g;
664

    
665
	if(isset($config['system']['developerspew'])) {
666
		$mt = microtime();
667
		echo "system_generate_lighty_config() being called $mt\n";
668
	}
669

    
670
	if($captive_portal == true)  {
671
		$captiveportal = ",\"mod_rewrite\"";
672
		$captive_portal_rewrite = "url.rewrite-once = ( \"(.*captiveportal.*)\" => \"$1\", \"(.*)\" => \"/index.php?redirurl=$1\" )\n";
673
		$captive_portal_module = "\"mod_accesslog\", ";
674
		$maxprocperip = $config['captiveportal']['maxprocperip'];
675
		if(!$maxprocperip and $maxprocperip > 0)
676
			$captive_portal_mod_evasive = "evasive.max-conns-per-ip = {$maxprocperip}";
677
		else
678
			$captive_portal_mod_evasive = "";
679
		$server_upload_dirs = "server.upload-dirs = ( \"/tmp/captiveportal/\" )\n";
680
		exec("mkdir -p /tmp/captiveportal");
681
		exec("chmod a-w /tmp/captiveportal");
682
		$server_max_request_size = "server.max-request-size    = 384";
683
	} else {
684
		$captive_portal_module = "";
685
		$captive_portal_mod_evasive = "";
686
		$server_upload_dirs = "server.upload-dirs = ( \"{$g['upload_path']}/\", \"/tmp/\", \"/var/\" )\n";
687
		$server_max_request_size = "server.max-request-size    = 2097152";
688
	}
689
	
690
	if($port <> "")
691
		$lighty_port = $port;
692
	else
693
		$lighty_port = "80";
694

    
695
	$memory = get_memory();
696
	$avail = $memory[0];
697

    
698
	if($avail > 0 and $avail < 65) {
699
		$fast_cgi_enable = false;
700
	}
701

    
702
	if($avail > 65 and $avail < 98) {
703
		$max_procs = 1;
704
	}
705

    
706
	if($avail > 97 and $avail < 128) {
707
		$max_procs = 1;
708
	}
709

    
710
	if($avail > 127 and $avail < 256) {
711
		$max_procs = 1;
712
	}
713

    
714
	if($avail > 255 and $avail < 384) {
715
		$max_procs = 3;
716
	}
717

    
718
	if($avail > 383) {
719
		$max_procs = 4;
720
	}
721

    
722
	if($captive_portal == true)  {	
723
		$bin_environment =  <<<EOC
724
        "bin-environment" => (
725
           "PHP_FCGI_CHILDREN" => "16",
726
           "PHP_FCGI_MAX_REQUESTS" => "{$max_requests}"
727
        ), 
728
EOC;
729

    
730
	} else if ($avail > 0 and $avail < 128) {
731
		$bin_environment = <<<EOC
732
	"bin-environment" => (
733
		"PHP_FCGI_CHILDREN" => "1",
734
			/*   This problem seems to stem from a little-known issue with PHP: 
735
			 *   PHP stops accepting new FastCGI connections after handling 500 requests; 
736
			 *   unfortunately, there is a potential race condition during the PHP cleanup
737
			 *   code in which PHP can be shutting down but still have the socket open, so
738
			 *   lighty can send request number 501 to PHP and have it "accepted", but then 
739
			 *   PHP appears to simply exit, causing a 500 return from lighty. 
740
			 */
741
			"PHP_FCGI_MAX_REQUESTS" => "500",
742
			"PHP_FCGI_CHILDREN" => "50"
743
	),
744

    
745
EOC;
746
	} else
747
		$bin_environment = "";
748
		
749
	if($fast_cgi_enable == true) {
750
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
751
		$cgi_config = "";
752
		$fastcgi_config = <<<EOD
753
#### fastcgi module
754
## read fastcgi.txt for more info
755
fastcgi.server = ( ".php" =>
756
	( "localhost" =>
757
		(
758
			"socket" => "/tmp/php-fastcgi.socket",
759
			"min-procs" => 1,
760
			"max-procs" => {$max_procs},
761
			{$bin_environment}			
762
			"bin-path" => "/usr/local/bin/php"
763
		)
764
	)
765
)
766

    
767
#### CGI module
768
cgi.assign                 = ( ".cgi" => "" )
769

    
770
EOD;
771
	} else {
772
		$fastcgi_config = "";
773
		$module = "\"mod_cgi\"";
774
		$cgi_config = <<<EOD
775
#### CGI module
776
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
777
                               ".cgi" => "" )
778

    
779
EOD;
780
	}
781

    
782
	$lighty_config .= <<<EOD
783
#
784
# lighttpd configuration file
785
#
786
# use a it as base for lighttpd 1.0.0 and above
787
#
788
############ Options you really have to take care of ####################
789

    
790
## FreeBSD!
791
server.event-handler	= "freebsd-kqueue"
792
server.network-backend 	= "writev"
793

    
794
## modules to load
795
server.modules              =   (
796
									{$captive_portal_module}
797
									"mod_access", "mod_accesslog", "mod_expire", "mod_compress",
798
									{$module}{$captiveportal}
799
								)
800

    
801
## Unused modules
802
#                               "mod_setenv",
803
#                               "mod_compress"
804
#				"mod_redirect",
805
#                               "mod_rewrite",
806
#                               "mod_ssi",
807
#                               "mod_usertrack",
808
#                               "mod_expire",
809
#                               "mod_secdownload",
810
#                               "mod_rrdtool",
811
#                               "mod_auth",
812
#                               "mod_status",
813
#                               "mod_alias",
814
#                               "mod_proxy",
815
#                               "mod_simple_vhost",
816
#                               "mod_evhost",
817
#                               "mod_userdir",
818
#                               "mod_cgi",
819
#                                "mod_accesslog"
820

    
821
server.max-keep-alive-requests = 15
822
server.max-keep-alive-idle = 30
823

    
824
## a static document-root, for virtual-hosting take look at the
825
## server.virtual-* options
826
server.document-root        = "{$document_root}"
827
{$captive_portal_rewrite}
828

    
829
# Maximum idle time with nothing being written (php downloading)
830
server.max-write-idle = 999
831

    
832
## where to send error-messages to
833
server.errorlog             = "/var/log/lighttpd.error.log"
834

    
835
# files to check for if .../ is requested
836
server.indexfiles           = ( "index.php", "index.html",
837
                                "index.htm", "default.htm" )
838

    
839
# mimetype mapping
840
mimetype.assign             = (
841
  ".pdf"          =>      "application/pdf",
842
  ".sig"          =>      "application/pgp-signature",
843
  ".spl"          =>      "application/futuresplash",
844
  ".class"        =>      "application/octet-stream",
845
  ".ps"           =>      "application/postscript",
846
  ".torrent"      =>      "application/x-bittorrent",
847
  ".dvi"          =>      "application/x-dvi",
848
  ".gz"           =>      "application/x-gzip",
849
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
850
  ".swf"          =>      "application/x-shockwave-flash",
851
  ".tar.gz"       =>      "application/x-tgz",
852
  ".tgz"          =>      "application/x-tgz",
853
  ".tar"          =>      "application/x-tar",
854
  ".zip"          =>      "application/zip",
855
  ".mp3"          =>      "audio/mpeg",
856
  ".m3u"          =>      "audio/x-mpegurl",
857
  ".wma"          =>      "audio/x-ms-wma",
858
  ".wax"          =>      "audio/x-ms-wax",
859
  ".ogg"          =>      "audio/x-wav",
860
  ".wav"          =>      "audio/x-wav",
861
  ".gif"          =>      "image/gif",
862
  ".jpg"          =>      "image/jpeg",
863
  ".jpeg"         =>      "image/jpeg",
864
  ".png"          =>      "image/png",
865
  ".xbm"          =>      "image/x-xbitmap",
866
  ".xpm"          =>      "image/x-xpixmap",
867
  ".xwd"          =>      "image/x-xwindowdump",
868
  ".css"          =>      "text/css",
869
  ".html"         =>      "text/html",
870
  ".htm"          =>      "text/html",
871
  ".js"           =>      "text/javascript",
872
  ".asc"          =>      "text/plain",
873
  ".c"            =>      "text/plain",
874
  ".conf"         =>      "text/plain",
875
  ".text"         =>      "text/plain",
876
  ".txt"          =>      "text/plain",
877
  ".dtd"          =>      "text/xml",
878
  ".xml"          =>      "text/xml",
879
  ".mpeg"         =>      "video/mpeg",
880
  ".mpg"          =>      "video/mpeg",
881
  ".mov"          =>      "video/quicktime",
882
  ".qt"           =>      "video/quicktime",
883
  ".avi"          =>      "video/x-msvideo",
884
  ".asf"          =>      "video/x-ms-asf",
885
  ".asx"          =>      "video/x-ms-asf",
886
  ".wmv"          =>      "video/x-ms-wmv",
887
  ".bz2"          =>      "application/x-bzip",
888
  ".tbz"          =>      "application/x-bzip-compressed-tar",
889
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
890
 )
891

    
892
# Use the "Content-Type" extended attribute to obtain mime type if possible
893
#mimetypes.use-xattr        = "enable"
894

    
895
#### accesslog module
896
#accesslog.filename          = "/dev/null"
897

    
898
## deny access the file-extensions
899
#
900
# ~    is for backupfiles from vi, emacs, joe, ...
901
# .inc is often used for code includes which should in general not be part
902
#      of the document-root
903
url.access-deny             = ( "~", ".inc" )
904

    
905

    
906
######### Options that are good to be but not neccesary to be changed #######
907

    
908
## bind to port (default: 80)
909
server.port                = {$lighty_port}
910

    
911
## error-handler for status 404
912
#server.error-handler-404   = "/error-handler.html"
913
#server.error-handler-404   = "/error-handler.php"
914

    
915
## to help the rc.scripts
916
server.pid-file            = "/var/run/{$pid_file}"
917

    
918
## virtual directory listings
919
server.dir-listing         = "disable"
920

    
921
## enable debugging
922
debug.log-request-header   = "disable"
923
debug.log-response-header  = "disable"
924
debug.log-request-handling = "disable"
925
debug.log-file-not-found   = "disable"
926

    
927
{$server_upload_dirs}
928

    
929
{$server_max_request_size}
930

    
931
{$fastcgi_config}
932

    
933
{$cgi_config}
934

    
935
{$captive_portal_mod_evasive}
936

    
937
# Turn on Lighty caching directives
938
compress.cache-dir         = "/tmp/"
939
compress.filetype          = ("text/plain", "text/html", "text/javascript", "text/css")
940

    
941
expire.url = (
942
				"" => "access 50 hours",	
943
        )
944

    
945
EOD;
946

    
947
	$cert = str_replace("\r", "", $cert);
948
	$key = str_replace("\r", "", $key);
949
	$ca = str_replace("\r", "", $ca);
950

    
951
	$cert = str_replace("\n\n", "\n", $cert);
952
	$key = str_replace("\n\n", "\n", $key);
953
	$ca = str_replace("\n\n", "\n", $ca);
954

    
955
	if($cert <> "" and $key <> "") {
956
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
957
		if (!$fd) {
958
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
959
			return 1;
960
		}
961
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
962
		fwrite($fd, $cert);
963
		fwrite($fd, "\n");
964
		fwrite($fd, $key);
965
		fclose($fd);
966
		if($ca <> "") {
967
			$fd = fopen("{$g['varetc_path']}/{$ca_location}", "w");
968
			if (!$fd) {
969
				printf("Error: cannot open ca.pem in system_webgui_start().\n");
970
				return 1;
971
			}
972
			chmod("{$g['varetc_path']}/{$ca_location}", 0600);
973
			fwrite($fd, $ca);
974
			fclose($fd);
975
		}
976
		$lighty_config .= "\n";
977
		$lighty_config .= "## ssl configuration\n";
978
		$lighty_config .= "ssl.engine = \"enable\"\n";
979
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_location}\"\n\n";
980
		if($ca <> "")
981
			$lighty_config .= "ssl.ca-file = \"{$g['varetc_path']}/{$ca_location}\"\n\n";
982
	}
983

    
984
	$fd = fopen("{$filename}", "w");
985
	if (!$fd) {
986
		printf("Error: cannot open {$filename} in system_generate_lighty_config().\n");
987
		return 1;
988
	}
989
	fwrite($fd, $lighty_config);
990
	fclose($fd);
991

    
992
	return 0;
993

    
994
}
995

    
996
function system_timezone_configure() {
997
	global $config, $g;
998
	if(isset($config['system']['developerspew'])) {
999
		$mt = microtime();
1000
		echo "system_timezone_configure() being called $mt\n";
1001
	}
1002

    
1003
	$syscfg = $config['system'];
1004

    
1005
	if ($g['booting'])
1006
		echo "Setting timezone...";
1007

    
1008
	/* extract appropriate timezone file */
1009
	$timezone = $syscfg['timezone'];
1010
	if (!$timezone)
1011
		$timezone = "Etc/UTC";
1012

    
1013
	conf_mount_rw();
1014

    
1015
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
1016
		escapeshellarg($timezone) . " > /etc/localtime");
1017

    
1018
	mwexec("sync");
1019
	conf_mount_ro();
1020

    
1021
	if ($g['booting'])
1022
		echo "done.\n";
1023
}
1024

    
1025
function system_ntp_configure() {
1026
	global $config, $g;
1027

    
1028
	$syscfg = $config['system'];
1029

    
1030
	/* open configuration for wrting or bail */
1031
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
1032
	if(!$fd) {
1033
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1034
		return;
1035
	}
1036

    
1037
	fwrite($fd, "# \n");
1038
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1039
	fwrite($fd, "# \n\n");
1040

    
1041
	/* foreach through servers and write out to ntpd.conf */
1042
	foreach (explode(' ', $syscfg['timeservers']) as $ts)
1043
		fwrite($fd, "servers {$ts}\n");
1044

    
1045
	/* Setup listener(s) if the user has configured one */
1046
        if ($config['installedpackages']['openntpd']) {
1047
    		/* server config is in coregui1 */
1048
		$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1049
		if ($xmlsettings['enable'] == 'on') {
1050
			$ifaces = explode(',', $xmlsettings['interface']);
1051
			$ifaces = array_map('convert_friendly_interface_to_real_interface_name', $ifaces);
1052
			$ifaces = array_filter($ifaces, 'does_interface_exist');
1053
			$ips = array_map('find_interface_ip', $ifaces);
1054
			foreach ($ips as $ip) {
1055
				if (is_ipaddr($ip))
1056
					fwrite($fd, "listen on $ip\n");
1057
			}
1058
		}
1059
	}
1060

    
1061
	fwrite($fd, "\n");
1062

    
1063
	/* slurp! */
1064
	fclose($fd);
1065

    
1066
	/* if openntpd is running, kill it */
1067
	while(is_process_running("ntpd")) {
1068
		mwexec("/usr/bin/killall ntpd", true);
1069
	}
1070

    
1071
	/* if /var/empty does not exist, create it */
1072
	if(!is_dir("/var/empty"))
1073
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1074

    
1075
	if($g['booting'])
1076
		return;
1077
	
1078
	/* start opentpd, set time now and use /var/etc/ntpd.conf */
1079
	exec("/usr/local/sbin/ntpd -s -f {$g['varetc_path']}/ntpd.conf");
1080
	
1081
	// Note that we are starting up
1082
	exec("echo 'OpenNTPD is starting up' >> {$g['varlog_path']}/ntpd.log");
1083

    
1084
}
1085

    
1086
function sync_system_time() {
1087
	global $config, $g;
1088

    
1089
	$syscfg = $config['system'];
1090

    
1091
	if ($g['booting'])
1092
		echo "Syncing system time before startup...";
1093

    
1094
	/* foreach through servers and write out to ntpd.conf */
1095
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1096
		mwexec("/usr/sbin/ntpdate -s $ts");
1097
	}
1098
	
1099
	if ($g['booting'])
1100
		echo "done.\n";
1101
	
1102
}
1103

    
1104
function system_halt() {
1105
	global $g;
1106

    
1107
	system_reboot_cleanup();
1108

    
1109
	mwexec("/usr/bin/nohup /etc/rc.halt > /dev/null 2>&1 &");
1110
}
1111

    
1112
function system_reboot() {
1113
	global $g;
1114

    
1115
	system_reboot_cleanup();
1116

    
1117
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
1118
}
1119

    
1120
function system_reboot_sync() {
1121
	global $g;
1122

    
1123
	system_reboot_cleanup();
1124

    
1125
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
1126
}
1127

    
1128
function system_reboot_cleanup() {
1129
	mwexec("/usr/local/bin/beep.sh stop");
1130
	captiveportal_radius_stop_all();
1131
	require_once("voucher.inc");
1132
	voucher_save_db_to_config();
1133
}
1134

    
1135
function system_do_shell_commands($early = 0) {
1136
	global $config, $g;
1137
	if(isset($config['system']['developerspew'])) {
1138
		$mt = microtime();
1139
		echo "system_do_shell_commands() being called $mt\n";
1140
	}
1141

    
1142
	if ($early)
1143
		$cmdn = "earlyshellcmd";
1144
	else
1145
		$cmdn = "shellcmd";
1146

    
1147
	if (is_array($config['system'][$cmdn])) {
1148

    
1149
		/* *cmd is an array, loop through */
1150
		foreach ($config['system'][$cmdn] as $cmd) {
1151
			exec($cmd);
1152
		}
1153

    
1154
	} elseif($config['system'][$cmdn] <> "") {
1155

    
1156
		/* execute single item */
1157
		exec($config['system'][$cmdn]);
1158

    
1159
	}
1160
}
1161

    
1162
function system_console_configure() {
1163
	global $config, $g;
1164
	if(isset($config['system']['developerspew'])) {
1165
		$mt = microtime();
1166
		echo "system_console_configure() being called $mt\n";
1167
	}
1168

    
1169
	if (isset($config['system']['disableconsolemenu'])) {
1170
		touch("{$g['varetc_path']}/disableconsole");
1171
	} else {
1172
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1173
	}
1174
}
1175

    
1176
function system_dmesg_save() {
1177
	global $g;
1178
	if(isset($config['system']['developerspew'])) {
1179
		$mt = microtime();
1180
		echo "system_dmesg_save() being called $mt\n";
1181
	}
1182

    
1183
	$dmesg = "";
1184
	exec("/sbin/dmesg", $dmesg);
1185

    
1186
	/* find last copyright line (output from previous boots may be present) */
1187
	$lastcpline = 0;
1188

    
1189
	for ($i = 0; $i < count($dmesg); $i++) {
1190
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1191
			$lastcpline = $i;
1192
	}
1193

    
1194
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1195
	if (!$fd) {
1196
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1197
		return 1;
1198
	}
1199

    
1200
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1201
		fwrite($fd, $dmesg[$i] . "\n");
1202

    
1203
	fclose($fd);
1204

    
1205
	return 0;
1206
}
1207

    
1208
function system_set_harddisk_standby() {
1209
	global $g, $config;
1210
	if(isset($config['system']['developerspew'])) {
1211
		$mt = microtime();
1212
		echo "system_set_harddisk_standby() being called $mt\n";
1213
	}
1214

    
1215
	if (isset($config['system']['harddiskstandby'])) {
1216
		if ($g['booting']) {
1217
			echo 'Setting hard disk standby... ';
1218
		}
1219

    
1220
		$standby = $config['system']['harddiskstandby'];
1221
		// Check for a numeric value
1222
		if (is_numeric($standby)) {
1223
			// Sync the disk(s)
1224
			mwexec('/bin/sync');
1225
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
1226
				// Reinitialize ATA-drives
1227
				mwexec('/usr/local/sbin/atareinit');
1228
				if ($g['booting']) {
1229
					echo "done.\n";
1230
				}
1231
			} else if ($g['booting']) {
1232
				echo "failed!\n";
1233
			}
1234
		} else if ($g['booting']) {
1235
			echo "failed!\n";
1236
		}
1237
	}
1238
}
1239

    
1240
function system_setup_sysctl() {
1241
	global $config;
1242
	if(isset($config['system']['developerspew'])) {
1243
		$mt = microtime();
1244
		echo "system_setup_sysctl() being called $mt\n";
1245
	}
1246

    
1247
	activate_sysctls();	
1248

    
1249
	if (isset($config['system']['sharednet'])) {
1250
		system_disable_arp_wrong_if();
1251
	}
1252
}
1253

    
1254
function system_disable_arp_wrong_if() {
1255
	global $config;
1256
	if(isset($config['system']['developerspew'])) {
1257
		$mt = microtime();
1258
		echo "system_disable_arp_wrong_if() being called $mt\n";
1259
	}
1260
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
1261
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=0");
1262
}
1263

    
1264
function system_enable_arp_wrong_if() {
1265
	global $config;
1266
	if(isset($config['system']['developerspew'])) {
1267
		$mt = microtime();
1268
		echo "system_enable_arp_wrong_if() being called $mt\n";
1269
	}
1270
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=1");
1271
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=1");
1272
}
1273

    
1274
function enable_watchdog() {
1275
	global $config;
1276
	return;
1277
	$install_watchdog = false;
1278
	$supported_watchdogs = array("Geode");
1279
	$file = file_get_contents("/var/log/dmesg.boot");
1280
	foreach($supported_watchdogs as $sd) {
1281
		if(stristr($file, "Geode")) {
1282
			$install_watchdog = true;
1283
		}
1284
	}
1285
	if($install_watchdog == true) {
1286
		if(is_process_running("watchdogd"))
1287
			mwexec("/usr/bin/killall watchdogd", true);
1288
		exec("/usr/sbin/watchdogd");
1289
	}
1290
}
1291

    
1292
?>
(35-35/44)