Project

General

Profile

Download (35.3 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
/* include all configuration functions */
33
require_once("functions.inc");
34

    
35
function opcode_cache_configuration() {
36
		global $g;
37
        if($g['platform'] == "cdrom")
38
        	return;
39

    
40
        /* get system memory amount */
41
        $memory = get_memory();
42
        $avail = $memory[0];
43

    
44
		/* disable apc for platforms less than 90 megs of ram */
45
        if($memory > 90) {
46
        	$opcode_cacher = "extension=apc.so\n";
47
        	$opcode_cacher .= "apc.enabled=\"1\"\n";
48
			$opcode_cacher .= "apc.enable_cli=\"1\"\n";
49
			$opcode_cacher .= "apc.shm_size=\"8\"\n";
50
        } else {
51
			$opcode_cacher = "";
52
        }
53

    
54
		/* create a php.ini variable */
55
		$php_conf = file_get_contents("/usr/local/lib/php.ini");
56

    
57
$php_ini = <<<EOFF
58
output_buffering = "0"
59
implicit_flush = true
60
magic_quotes_gpc = Off
61
max_execution_time = 99999999
62
max_input_time = 99999999
63
register_argc_argv = On
64
file_uploads = On
65
extension_dir=/usr/local/lib/php/extensions/no-debug-non-zts-20020429/
66
upload_tmp_dir = /tmp
67
upload_max_filesize = 100M
68
post_max_size = 100M
69
html_errors = Off
70
include_path = ".:/etc/inc:/usr/local/www:/usr/local/captiveportal:/usr/local/pkg"
71
extension=radius.so
72
{$opcode_cacher}
73

    
74
EOFF;
75

    
76
        config_lock();
77
		conf_mount_rw();
78

    
79
		/* open up php.ini and write back out contents */
80
		$fd = fopen("/usr/local/lib/php.ini","w");
81
		fwrite($fd, $php_ini);
82
		fclose($fd);
83

    
84
		mwexec("sync");
85
		conf_mount_ro();
86
		exec("sync");
87
		config_unlock();
88
}
89

    
90
function system_resolvconf_generate($dynupdate = false) {
91
	global $config, $g;
92
	if(isset($config['system']['developerspew'])) {
93
		$mt = microtime();
94
		echo "system_resolvconf_generate() being called $mt\n";
95
	}
96

    
97
        $syscfg = $config['system'];
98

    
99
        $fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
100
        if (!$fd) {
101
                printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
102
                return 1;
103
        }
104

    
105
        $resolvconf = "domain {$syscfg['domain']}\n";
106

    
107
        $havedns = false;
108

    
109
        if (isset($syscfg['dnsallowoverride'])) {
110
                /* get dynamically assigned DNS servers (if any) */
111
		$ns = array_unique(get_nameservers());
112
		foreach($ns as $nameserver) {
113
			if($nameserver) {
114
				$resolvconf .= "nameserver $nameserver\n";
115
				$havedns = true;
116
			}
117
		}
118
        }
119
        if (!$havedns && is_array($syscfg['dnsserver'])) {
120
                foreach ($syscfg['dnsserver'] as $ns) {
121
                        if ($ns) {
122
                                $resolvconf .= "nameserver $ns\n";
123
				$havedns = true;
124
			}
125
                }
126
        }
127

    
128
        fwrite($fd, $resolvconf);
129
        fclose($fd);
130

    
131
        if (!$g['booting']) {
132
                /* restart dhcpd (nameservers may have changed) */
133
                if (!$dynupdate)
134
                        services_dhcpd_configure();
135
        }
136

    
137
        return 0;
138
}
139

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

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

    
167
	$syscfg = $config['system'];
168
	$lancfg = $config['interfaces']['lan'];
169
	$dnsmasqcfg = $config['dnsmasq'];
170

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

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

    
182
	$hosts = <<<EOD
183
127.0.0.1	localhost localhost.{$syscfg['domain']}
184
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
185

    
186
EOD;
187

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

    
204
	return 0;
205
}
206

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

    
214
	$syscfg = $config['system'];
215

    
216
	/* set hostname */
217
	return mwexec("/bin/hostname " .
218
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
219
}
220

    
221
function system_routing_configure() {
222
	global $config, $g;
223
	if(isset($config['system']['developerspew'])) {
224
		$mt = microtime();
225
		echo "system_routing_configure() being called $mt\n";
226
	}
227

    
228
	/* Enable fast routing, if enabled */
229
	if(isset($config['staticroutes']['enablefastrouting']))
230
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
231

    
232
	/* clear out old routes, if necessary */
233
	if (file_exists("{$g['vardb_path']}/routes.db")) {
234
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
235
		if (!$fd) {
236
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
237
			return 1;
238
		}
239
		while (!feof($fd)) {
240
			$oldrt = fgets($fd);
241
			if ($oldrt)
242
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
243
		}
244
		fclose($fd);
245
		unlink("{$g['vardb_path']}/routes.db");
246
	}
247

    
248
	if (is_array($config['staticroutes']['route'])) {
249

    
250
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
251
		if (!$fd) {
252
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
253
			return 1;
254
		}
255

    
256
		foreach ($config['staticroutes']['route'] as $rtent) {
257
			if(isset($rtent['interfacegateway'])) {
258
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
259
					" -iface " . escapeshellarg(convert_friendly_interface_to_real_interface_name($rtent['interface'])));
260
			} else {
261
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
262
					" " . escapeshellarg($rtent['gateway']));
263
			}
264
			/* record route so it can be easily removed later (if necessary) */
265
			fwrite($fd, $rtent['network'] . "\n");
266
		}
267

    
268
		fclose($fd);
269
	}
270

    
271
	return 0;
272
}
273

    
274
function system_routing_enable() {
275
	global $config, $g;
276
	if(isset($config['system']['developerspew'])) {
277
		$mt = microtime();
278
		echo "system_routing_enable() being called $mt\n";
279
	}
280

    
281
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
282
}
283

    
284
function system_syslogd_start() {
285
	global $config, $g;
286
	if(isset($config['system']['developerspew'])) {
287
		$mt = microtime();
288
		echo "system_syslogd_start() being called $mt\n";
289
	}
290

    
291
	$syslogcfg = $config['syslog'];
292

    
293
	if ($g['booting'])
294
		echo "Starting syslog...";
295
	else
296
		killbypid("{$g['varrun_path']}/syslog.pid");
297

    
298
	if (isset($syslogcfg)) {
299
		$separatelogfacilities = array('ntpd','racoon','openvpn');
300
		if($config['installedpackages']['package']) {
301
                        foreach($config['installedpackages']['package'] as $package) {
302
                                if($package['logging']) {
303
					$pkgfacilities[] = $package['logging']['facilityname'];
304
					$separatelogfacilities = $separatelogfacilities + $pkgfacilities;
305
					$facilitylist = implode(',', $pkgfacilities);
306
					mwexec("clog -i -s 10000 {$g['varlog_path']}/{$package['logging']['logfilename']}");
307
                                	$syslogconf .= "!{$facilitylist}\n*.*\t\t\t\t\t\t%{$g['varlog_path']}/{$package['logging']['logfilename']}\n";
308
				}
309
                        }
310
                }
311
		$facilitylist = implode(',', array_unique($separatelogfacilities));
312
		/* write syslog.conf */
313
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
314
		if (!$fd) {
315
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
316
			return 1;
317
		}
318
		$syslogconf .= "!ntpdate,!ntpd\n";
319
		if (!isset($syslogcfg['disablelocallogging'])) {
320
			$syslogconf .= <<<EOD
321
*.*						%{$g['varlog_path']}/ntpd.log
322

    
323
EOD;
324
		}
325
		$syslogconf .= "!racoon\n";
326
		if (!isset($syslogcfg['disablelocallogging'])) {
327
			$syslogconf .= <<<EOD
328
*.*						%{$g['varlog_path']}/ipsec.log
329

    
330
EOD;
331
		}
332
		if (isset($syslogcfg['vpn'])) {
333
			$syslogconf .= <<<EOD
334
*.*						@{$syslogcfg['remoteserver']}
335

    
336
EOD;
337
		}
338
		$syslogconf .= "!openvpn\n";
339
		if (!isset($syslogcfg['disablelocallogging'])) {
340
			$syslogconf .= <<<EOD
341
*.*						%{$g['varlog_path']}/openvpn.log
342

    
343
EOD;
344
		}
345
		if (isset($syslogcfg['vpn'])) {
346
			$syslogconf .= <<<EOD
347
*.*						@{$syslogcfg['remoteserver']}
348

    
349
EOD;
350
		}
351
		$syslogconf .= "!-{$facilitylist}\n";
352
		if (!isset($syslogcfg['disablelocallogging'])) {
353
		$syslogconf .= <<<EOD
354
local0.*					%{$g['varlog_path']}/filter.log
355
local3.*					%{$g['varlog_path']}/vpn.log
356
local4.*					%{$g['varlog_path']}/portalauth.log
357
local7.*					%{$g['varlog_path']}/dhcpd.log
358
*.notice;kern.debug;lpr.info;mail.crit; 	%{$g['varlog_path']}/system.log
359
news.err;local0.none;local3.none;local4.none; 	%{$g['varlog_path']}/system.log
360
local7.none					%{$g['varlog_path']}/system.log
361
security.*					%{$g['varlog_path']}/system.log
362
auth.info;authpriv.info;daemon.info		%{$g['varlog_path']}/system.log
363
local1.*					%{$g['varlog_path']}/slbd.log
364
auth.info;authpriv.info 			|exec /usr/local/sbin/sshlockout_pf
365
*.emerg						*
366

    
367
EOD;
368
		}
369

    
370
		if (isset($syslogcfg['filter'])) {
371
			$syslogconf .= <<<EOD
372
local0.*					@{$syslogcfg['remoteserver']}
373

    
374
EOD;
375
		}
376

    
377
		if (isset($syslogcfg['vpn'])) {
378
			$syslogconf .= <<<EOD
379
local3.*					@{$syslogcfg['remoteserver']}
380

    
381
EOD;
382
		}
383

    
384

    
385
		if (isset($syslogcfg['portalauth'])) {
386
			$syslogconf .= <<<EOD
387
local4.*					@{$syslogcfg['remoteserver']}
388

    
389
EOD;
390
		}
391

    
392

    
393
		if (isset($syslogcfg['dhcp'])) {
394
			$syslogconf .= <<<EOD
395
local7.*					@{$syslogcfg['remoteserver']}
396

    
397
EOD;
398
		}
399

    
400
		if (isset($syslogcfg['system'])) {
401
			$syslogconf .= <<<EOD
402
*.notice;kern.debug;lpr.info;mail.crit;		@{$syslogcfg['remoteserver']}
403
news.err;local0.none;local3.none;local7.none	@{$syslogcfg['remoteserver']}
404
security.*					@{$syslogcfg['remoteserver']}
405
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
406
*.emerg						@{$syslogcfg['remoteserver']}
407

    
408
EOD;
409
		}
410
		fwrite($fd, $syslogconf);
411
		fclose($fd);
412

    
413
		// Are we logging to a least one remote server ?
414
		if(strpos($syslogconf, "@") != false)
415
			$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
416
		else
417
			$retval = mwexec("/usr/sbin/syslogd -ss -f {$g['varetc_path']}/syslog.conf");
418

    
419
	} else {
420
		$retval = mwexec("/usr/sbin/syslogd -ss");
421
	}
422

    
423
	if ($g['booting'])
424
		echo "done.\n";
425

    
426
	return $retval;
427
}
428

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

    
436
	if ($g['booting'])
437
		echo "Initializing PCMCIA...";
438

    
439
	/* kill any running pccardd */
440
	killbypid("{$g['varrun_path']}/pccardd.pid");
441

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

    
445
	if ($g['booting']) {
446
		if ($res == 0)
447
			echo "done.\n";
448
		else
449
			echo "failed!\n";
450
	}
451

    
452
	return $res;
453
}
454

    
455

    
456
function system_webgui_start() {
457
	global $config, $g;
458

    
459
	if ($g['booting'])
460
		echo "Starting webConfigurator...";
461

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

    
465
	sleep(1);
466

    
467
	/* generate password file */
468
	system_password_configure();
469

    
470
	chdir($g['www_path']);
471

    
472
	/* non-standard port? */
473
	if ($config['system']['webgui']['port'])
474
		$portarg = "{$config['system']['webgui']['port']}";
475
	else
476
		$portarg = "";
477

    
478
	if ($config['system']['webgui']['protocol'] == "https") {
479

    
480
	if(!$config['system']['webgui']['port'])
481
		$portarg = "443";
482

    
483
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
484
			$cert = base64_decode($config['system']['webgui']['certificate']);
485
			$key = base64_decode($config['system']['webgui']['private-key']);
486
		} else {
487
			/* default certificate/key */
488
			$cert = <<<EOD
489
-----BEGIN CERTIFICATE-----
490
MIIDEzCCAnygAwIBAgIJAJM91W+s6qptMA0GCSqGSIb3DQEBBAUAMGUxCzAJBgNV
491
BAYTAlVTMQswCQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UE
492
ChMHcGZTZW5zZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZTAe
493
Fw0wNjAzMTAyMzQ1MTlaFw0xNjAzMDcyMzQ1MTlaMGUxCzAJBgNVBAYTAlVTMQsw
494
CQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UEChMHcGZTZW5z
495
ZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZTCBnzANBgkqhkiG
496
9w0BAQEFAAOBjQAwgYkCgYEA3lPNTFH6qge/ygaqe/BS4oH59O6KvAesWcRzSu5N
497
21lyVE5tBbL0zqOSXmlLyReMSbtAMZqt1P8EPYFoOcaEQHIWm2VQF80Z18+8Gh4O
498
UQGjHq88OeaLqyk3OLpSKzSpXuCFrSN7q9Kez8zp5dQEu7sIW30da3pAbdqYOimA
499
1VsCAwEAAaOByjCBxzAdBgNVHQ4EFgQUAnx+ggC4SzJ0CK+rhPhJ2ZpyunEwgZcG
500
A1UdIwSBjzCBjIAUAnx+ggC4SzJ0CK+rhPhJ2ZpyunGhaaRnMGUxCzAJBgNVBAYT
501
AlVTMQswCQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UEChMH
502
cGZTZW5zZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZYIJAJM9
503
1W+s6qptMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAAviQpdoeabL8
504
1HSZiD7Yjx82pdLpyQOdXvAu3jEAYz53ckx0zSMrzsQ5r7Vae6AE7Xd7Pj+1Yihs
505
AJZzOQujnmsuim7qu6YSxzP34xonKwd1C9tZUlyNRNnEmtXOEDupn05bih1ugtLG
506
kqfPIgDbDLXuPtEAA6QDUypaunI6+1E=
507
-----END CERTIFICATE-----
508

    
509
EOD;
510

    
511
			$key = <<<EOD
512
-----BEGIN RSA PRIVATE KEY-----
513
MIICXgIBAAKBgQDeU81MUfqqB7/KBqp78FLigfn07oq8B6xZxHNK7k3bWXJUTm0F
514
svTOo5JeaUvJF4xJu0Axmq3U/wQ9gWg5xoRAchabZVAXzRnXz7waHg5RAaMerzw5
515
5ourKTc4ulIrNKle4IWtI3ur0p7PzOnl1AS7uwhbfR1rekBt2pg6KYDVWwIDAQAB
516
AoGAP7E0VFP8Aq/7os3sE1uS8y8XQ7L+7cUo/AKKoQHKLjfeyAY7t3FALt6vdPqn
517
anGjkA/j4RIWELoKJfCnwj17703NDCPwB7klcmZvmTx5Om1ZrRyZdQ6RJs0pOOO1
518
r2wOnZNaNWStXE9Afpw3dj20Gh0V/Ioo5HXn3sHfxZm8dnkCQQDwv8OaUdp2Hl8t
519
FDfXB1CMvUG1hEAvbQvZK1ODkE7na2/ChKjVPddEI3DvfzG+nLrNuTrAyVWgRLte
520
r8qX5PQHAkEA7GlKx0S18LdiKo6wy2QeGu6HYkPncaHNFOWX8cTpvGGtQoWYSh0J
521
tjCt1/mz4/XkvZWuZyTNx2FdkVlNF5nHDQJBAIRWVTZqEjVlwpmsCHnp6mxCyHD4
522
DrRDNAUfnNuwIr9xPlDlzUzSnpc1CCqOd5C45LKbRGGfCrN7tKd66FmQoFcCQQCy
523
Kvw3R1pTCvHJnvYwoshphaC0dvaDVeyINiwYAk4hMf/wpVxLZqz+CJvLrB1dzOBR
524
3O+uPjdzbrakpweJpNQ1AkEA3ZtlgEj9eWsLAJP8aKlwB8VqD+EtG9OJSUMnCDiQ
525
WFFNj/t3Ze3IVuAyL/yMpiv3JNEnZhIxCta42eDFpIZAKw==
526
-----END RSA PRIVATE KEY-----
527

    
528
EOD;
529
		}
530
	} else {
531
		$cert = "";
532
		$key = "";
533
	}
534

    
535
	/* generate lighttpd configuration */
536
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
537
		$cert, $key, "lighty-webConfigurator.pid", $portarg, "/usr/local/www/");
538

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

    
542
	if ($g['booting']) {
543
		if ($res == 0)
544
			echo "done.\n";
545
		else
546
			echo "failed!\n";
547
	}
548

    
549
	return $res;
550
}
551

    
552
function system_webgui_start_old() {
553
	global $config, $g;
554
	if(isset($config['system']['developerspew'])) {
555
		$mt = microtime();
556
		echo "system_webgui_start() being called $mt\n";
557
	}
558

    
559
        if ($g['booting'])
560
                echo "Starting webConfigurator...";
561

    
562
        /* kill any running mini_httpd */
563
        killbypid("{$g['varrun_path']}/mini_httpd.pid");
564

    
565
        /* generate password file */
566
        system_password_configure();
567

    
568
        chdir($g['www_path']);
569

    
570
        /* non-standard port? */
571
        if ($config['system']['webgui']['port'])
572
                $portarg = "-p {$config['system']['webgui']['port']}";
573
        else
574
                $portarg = "";
575

    
576
        if ($config['system']['webgui']['protocol'] == "https") {
577

    
578
                if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
579
                        $cert = base64_decode($config['system']['webgui']['certificate']);
580
                        $key  = base64_decode($config['system']['webgui']['private-key']);
581
                } else {
582
                        /* default certificate/key */
583
                        $cert = <<<EOD
584
-----BEGIN CERTIFICATE-----
585
MIIBlDCB/gIBADANBgkqhkiG9w0BAQQFADATMREwDwYDVQQKEwhtMG4wd2FsbDAe
586
Fw0wNTA1MTAxMjI0NDRaFw0wNzA1MTAxMjI0NDRaMBMxETAPBgNVBAoTCG0wbjB3
587
YWxsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAShszhFz+o8lsMWTGgTxs
588
TMPR+v4+qL5jXDyY97MLTGFK7aqQOtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+
589
83LPQmQoSPC0VqhfU3uYf3NzxiK8r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFP
590
C4jE2fvjkbzyVolPywBuewIDAQABMA0GCSqGSIb3DQEBBAUAA4GBAFR962c4R5tV
591
cTn0OQcszYoW6WC+ini9tQQh5ku5jYDAiC+00atawJEVLnL3lwAcpSKTIWlTkD20
592
tl3lz5br1qFgYky+Rd0kwS2nk9jRbkxSXxd6KJVnNRCKre28aw3ENzZfCSurPQsX
593
UPp5er+NtwMT1g7s/JDmKTC4w1rGr5/c
594
-----END CERTIFICATE-----
595

    
596
EOD;
597

    
598
                        $key = <<<EOD
599
-----BEGIN RSA PRIVATE KEY-----
600
MIICXQIBAAKBgQDAShszhFz+o8lsMWTGgTxsTMPR+v4+qL5jXDyY97MLTGFK7aqQ
601
OtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+83LPQmQoSPC0VqhfU3uYf3NzxiK8
602
r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFPC4jE2fvjkbzyVolPywBuewIDAQAB
603
AoGAbJJrQW9fQrggJuLMz/hwsYW2m31oyOBmf5u463YQtjRuSuxe/gj87weZuNqY
604
H2rXq2k2K+ehl8hgW+egASyUL3L7kCkEAsVREujKTEyhSqqIRDPWTxo9S/YA9Gvn
605
2ZnJvkrcKjqCO9aHX3rvJOK/ErYI6akctgI3KmgkYw5XNmECQQDuZU97RTWH9rmP
606
aQr57ysNXxgFsyhetOOqeYkPtIVwpOiNbfwE1zi5RGdtO4Ku3fG1lV4J2UoWJ9yD
607
awdoyYIHAkEAzn0xJ90IjPsHk+8SODEj5JGdHSZPNu1tgtrbjEi9sfGWg4K7XTxr
608
QW90pWb1bKKU1uh5FzW6OhnFfuQXt1kC7QJAPSthqY+onKqCEnoxhtAHi/bKgyvl
609
P+fKQwPMV2tKkgy+XwvJjrRqqZ8TqsOKVLQ+QQmCh6RpjiXMPyxHSmvqIQJBAKLR
610
HF1ucDuaBROkwx0DwmWMW/KMLpIFDQDNSaiIAuu4rxHrl4mhBoGGPNffI04RtILw
611
s+qVNs5xW8T+XaT4ztECQQDFHPnZeoPWE5z+AX/UUQIUWaDExz3XRzmIxRbOrlFi
612
CsF1s0TdJLi/wzNQRAL37A8vqCeVFR/ng3Xpg96Yg+8Z
613
-----END RSA PRIVATE KEY-----
614

    
615
EOD;
616
                }
617

    
618
				$cert = str_replace("\r", "", $cert);
619
				$key = str_replace("\r", "", $key);
620

    
621
                $fd = fopen("{$g['varetc_path']}/cert.pem", "w");
622
                if (!$fd) {
623
                        printf("Error: cannot open cert.pem in system_webgui_start().\n");
624
                        return 1;
625
                }
626
                chmod("{$g['varetc_path']}/cert.pem", 0600);
627
                fwrite($fd, $cert);
628
                fwrite($fd, "\n");
629
                fwrite($fd, $key);
630
                fclose($fd);
631

    
632
                $res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
633
                        " -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
634
                        " -i {$g['varrun_path']}/mini_httpd.pid");
635
        } else {
636
                $res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
637
                        " -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
638
        }
639

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

    
647
        return $res;
648
}
649

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

    
662
	global $config, $g;
663

    
664
	/* only use freebsd-sendfile network handler on full installations
665
	 * tests have shown that it is actually slower on embedded.
666
	 */
667
	if(stristr("pfSense", $g['platform']))
668
		$network_handler = "server.network-backend		= \"freebsd-sendfile\"\n";
669
	else
670
		$network_handler = "";
671

    
672
	if(isset($config['system']['developerspew'])) {
673
		$mt = microtime();
674
		echo "system_generate_lighty_config() being called $mt\n";
675
	}
676

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

    
697
	if($port <> "")
698
		$lighty_port = $port;
699
	else
700
		$lighty_port = "80";
701

    
702
	$memory = get_memory();
703
	$avail = $memory[0];
704

    
705
	if($avail > 0 and $avail < 98) {
706
		$max_procs = 1;
707
		$max_requests = 1;
708
	}
709

    
710
	if($avail > 97 and $avail < 128) {
711
		$max_procs = 2;
712
		$max_requests = 1;
713
	}
714

    
715
	if($avail > 127 and $avail < 256) {
716
		$max_procs = 2;
717
		$max_requests = 1;
718
	}
719

    
720
	if($avail > 255 and $avail < 384) {
721
		$max_procs = 3;
722
		$max_requests = 1;
723
	}
724

    
725
	if($avail > 383 and $avail < 512) {
726
		$max_procs = 4;
727
		$max_requests = 1;
728
	}
729

    
730
	if($fast_cgi_enable == true) {
731
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
732
		$cgi_config = "";
733
		$fastcgi_config = <<<EOD
734
#### fastcgi module
735
## read fastcgi.txt for more info
736
fastcgi.server = ( ".php" =>
737
	( "localhost" =>
738
		(
739
			"socket" => "/tmp/php-fastcgi.socket",
740
			"min-procs" => 1,
741
			"max-procs" => {$max_procs},
742
			"bin-path" => "/usr/local/bin/php"
743
		)
744
	)
745
)
746

    
747
#### CGI module
748
cgi.assign                 = ( ".cgi" => "" )
749

    
750
EOD;
751
	} else {
752
		$fastcgi_config = "";
753
		$module = "\"mod_cgi\"";
754
		$cgi_config = <<<EOD
755
#### CGI module
756
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
757
                               ".cgi" => "" )
758

    
759
EOD;
760
	}
761

    
762
	$lighty_config .= <<<EOD
763
#
764
# lighttpd configuration file
765
#
766
# use a it as base for lighttpd 1.0.0 and above
767
#
768
############ Options you really have to take care of ####################
769

    
770
## FreeBSD!
771
server.event-handler		= "freebsd-kqueue"
772

    
773
{$network_handler}
774

    
775
## modules to load
776
server.modules              =   (
777
				  {$captive_portal_module}
778
				  "mod_access", "mod_accesslog",
779
                                  {$module}{$captiveportal}
780
				)
781

    
782
## Unused modules
783
#                               "mod_setenv",
784
#                               "mod_compress"
785
#				"mod_redirect",
786
#                               "mod_rewrite",
787
#                               "mod_ssi",
788
#                               "mod_usertrack",
789
#                               "mod_expire",
790
#                               "mod_secdownload",
791
#                               "mod_rrdtool",
792
#                               "mod_auth",
793
#                               "mod_status",
794
#                               "mod_alias",
795
#                               "mod_proxy",
796
#                               "mod_simple_vhost",
797
#                               "mod_evhost",
798
#                               "mod_userdir",
799
#                               "mod_cgi",
800
#                                "mod_accesslog"
801

    
802
## a static document-root, for virtual-hosting take look at the
803
## server.virtual-* options
804
server.document-root        = "{$document_root}"
805
{$captive_portal_rewrite}
806

    
807
## where to send error-messages to
808
server.errorlog             = "/var/log/lighttpd.error.log"
809

    
810
# files to check for if .../ is requested
811
server.indexfiles           = ( "index.php", "index.html",
812
                                "index.htm", "default.htm" )
813

    
814
# mimetype mapping
815
mimetype.assign             = (
816
  ".pdf"          =>      "application/pdf",
817
  ".sig"          =>      "application/pgp-signature",
818
  ".spl"          =>      "application/futuresplash",
819
  ".class"        =>      "application/octet-stream",
820
  ".ps"           =>      "application/postscript",
821
  ".torrent"      =>      "application/x-bittorrent",
822
  ".dvi"          =>      "application/x-dvi",
823
  ".gz"           =>      "application/x-gzip",
824
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
825
  ".swf"          =>      "application/x-shockwave-flash",
826
  ".tar.gz"       =>      "application/x-tgz",
827
  ".tgz"          =>      "application/x-tgz",
828
  ".tar"          =>      "application/x-tar",
829
  ".zip"          =>      "application/zip",
830
  ".mp3"          =>      "audio/mpeg",
831
  ".m3u"          =>      "audio/x-mpegurl",
832
  ".wma"          =>      "audio/x-ms-wma",
833
  ".wax"          =>      "audio/x-ms-wax",
834
  ".ogg"          =>      "audio/x-wav",
835
  ".wav"          =>      "audio/x-wav",
836
  ".gif"          =>      "image/gif",
837
  ".jpg"          =>      "image/jpeg",
838
  ".jpeg"         =>      "image/jpeg",
839
  ".png"          =>      "image/png",
840
  ".xbm"          =>      "image/x-xbitmap",
841
  ".xpm"          =>      "image/x-xpixmap",
842
  ".xwd"          =>      "image/x-xwindowdump",
843
  ".css"          =>      "text/css",
844
  ".html"         =>      "text/html",
845
  ".htm"          =>      "text/html",
846
  ".js"           =>      "text/javascript",
847
  ".asc"          =>      "text/plain",
848
  ".c"            =>      "text/plain",
849
  ".conf"         =>      "text/plain",
850
  ".text"         =>      "text/plain",
851
  ".txt"          =>      "text/plain",
852
  ".dtd"          =>      "text/xml",
853
  ".xml"          =>      "text/xml",
854
  ".mpeg"         =>      "video/mpeg",
855
  ".mpg"          =>      "video/mpeg",
856
  ".mov"          =>      "video/quicktime",
857
  ".qt"           =>      "video/quicktime",
858
  ".avi"          =>      "video/x-msvideo",
859
  ".asf"          =>      "video/x-ms-asf",
860
  ".asx"          =>      "video/x-ms-asf",
861
  ".wmv"          =>      "video/x-ms-wmv",
862
  ".bz2"          =>      "application/x-bzip",
863
  ".tbz"          =>      "application/x-bzip-compressed-tar",
864
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
865
 )
866

    
867
# Use the "Content-Type" extended attribute to obtain mime type if possible
868
#mimetypes.use-xattr        = "enable"
869

    
870
#### accesslog module
871
#accesslog.filename          = "/dev/null"
872

    
873
## deny access the file-extensions
874
#
875
# ~    is for backupfiles from vi, emacs, joe, ...
876
# .inc is often used for code includes which should in general not be part
877
#      of the document-root
878
url.access-deny             = ( "~", ".inc" )
879

    
880

    
881
######### Options that are good to be but not neccesary to be changed #######
882

    
883
## bind to port (default: 80)
884
server.port                = {$lighty_port}
885

    
886
## error-handler for status 404
887
#server.error-handler-404   = "/error-handler.html"
888
#server.error-handler-404   = "/error-handler.php"
889

    
890
## to help the rc.scripts
891
server.pid-file            = "/var/run/{$pid_file}"
892

    
893
## virtual directory listings
894
server.dir-listing         = "disable"
895

    
896
## enable debugging
897
debug.log-request-header   = "disable"
898
debug.log-response-header  = "disable"
899
debug.log-request-handling = "disable"
900
debug.log-file-not-found   = "disable"
901

    
902
#### compress module
903
#compress.cache-dir         = "/tmp/lighttpd/cache/compress/"
904
#compress.filetype          = ("text/plain", "text/html")
905

    
906
#server.network-backend = "writev"
907

    
908
{$server_upload_dirs}
909

    
910
{$server_max_request_size}
911

    
912
{$fastcgi_config}
913

    
914
{$cgi_config}
915

    
916
{$captive_portal_mod_evasive}
917

    
918
EOD;
919

    
920
	$cert = str_replace("\r", "", $cert);
921
	$key = str_replace("\r", "", $key);
922

    
923
	$cert = str_replace("\n\n", "\n", $cert);
924
	$key = str_replace("\n\n", "\n", $key);
925

    
926
	if($cert <> "" and $key <> "") {
927
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
928
		if (!$fd) {
929
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
930
			return 1;
931
		}
932
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
933
		fwrite($fd, $cert);
934
		fwrite($fd, "\n");
935
		fwrite($fd, $key);
936
		fclose($fd);
937
		$lighty_config .= "\n";
938
		$lighty_config .= "## ssl configuration\n";
939
		$lighty_config .= "ssl.engine = \"enable\"\n";
940
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_location}\"\n\n";
941
	}
942

    
943
	$fd = fopen("{$filename}", "w");
944
	if (!$fd) {
945
		printf("Error: cannot open {$filename} in system_generate_lighty_config().\n");
946
		return 1;
947
	}
948
	fwrite($fd, $lighty_config);
949
	fclose($fd);
950

    
951
	return 0;
952

    
953
}
954

    
955
function system_password_configure() {
956
	global $config, $g;
957
	if(isset($config['system']['developerspew'])) {
958
		$mt = microtime();
959
		echo "system_password_configure() being called $mt\n";
960
	}
961

    
962
	/* sync passwords */
963
	sync_webgui_passwords();
964

    
965
	/* !NOTE! conf_mount_ro is done by sync_webgui_passwords() */
966

    
967
	return 0;
968
}
969

    
970
function system_timezone_configure() {
971
	global $config, $g;
972
	if(isset($config['system']['developerspew'])) {
973
		$mt = microtime();
974
		echo "system_timezone_configure() being called $mt\n";
975
	}
976

    
977
	$syscfg = $config['system'];
978

    
979
	if ($g['booting'])
980
		echo "Setting timezone...";
981

    
982
	/* extract appropriate timezone file */
983
	$timezone = $syscfg['timezone'];
984
	if (!$timezone)
985
		$timezone = "Etc/UTC";
986

    
987
	conf_mount_rw();
988

    
989
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
990
		escapeshellarg($timezone) . " > /etc/localtime");
991

    
992
	mwexec("sync");
993
	conf_mount_ro();
994

    
995
	if ($g['booting'])
996
		echo "done.\n";
997
}
998

    
999
function system_ntp_configure() {
1000
	global $config, $g;
1001

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

    
1004
	/* open configuration for wrting or bail */
1005
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
1006
	if(!$fd) {
1007
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1008
		return;
1009
	}
1010

    
1011
	fwrite($fd, "# \n");
1012
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1013
	fwrite($fd, "# \n\n");
1014

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

    
1019
    /* server config is in coregui1 */
1020
	$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1021
	if ($xmlsettings['enable'] == 'on') {
1022
		$ifaces = explode(',', $xmlsettings['interface']);
1023
		$ifaces = array_map('convert_friendly_interface_to_real_interface_name', $ifaces);
1024
		$ifaces = array_filter($ifaces, 'does_interface_exist');
1025
		$ips = array_map('find_interface_ip', $ifaces);
1026
		foreach ($ips as $ip) {
1027
			if (is_ipaddr($ip))
1028
				fwrite($fd, "listen on $ip\n");
1029
		}
1030
	}
1031

    
1032
	fwrite($fd, "\n");
1033

    
1034
	/* slurp! */
1035
	fclose($fd);
1036

    
1037
	/* if openntpd is running, kill it */
1038
	while(is_process_running("ntpd")) {
1039
		exec("/usr/bin/killall ntpd");
1040
		sleep(3);
1041
	}
1042

    
1043
	/* if /var/empty does not exist, create it */
1044
	if(!is_dir("/var/empty"))
1045
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1046

    
1047
	sleep(1);
1048

    
1049
	/* start opentpd, set time now and use /var/etc/ntpd.conf */
1050
	exec("/usr/local/sbin/ntpd -s -f {$g['varetc_path']}/ntpd.conf");
1051

    
1052
}
1053

    
1054
function sync_system_time() {
1055
	global $config, $g;
1056

    
1057
	$syscfg = $config['system'];
1058

    
1059
	if ($g['booting'])
1060
		echo "Syncing system time before startup...";
1061

    
1062
	/* foreach through servers and write out to ntpd.conf */
1063
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1064
		mwexec("/usr/sbin/ntpdate -s $ts");
1065
	}
1066
	
1067
	if ($g['booting'])
1068
		echo "done.\n";
1069
	
1070
}
1071

    
1072
function system_halt() {
1073
	global $g;
1074

    
1075
	system_reboot_cleanup();
1076

    
1077
	mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
1078
}
1079

    
1080
function system_reboot() {
1081
	global $g;
1082

    
1083
	system_reboot_cleanup();
1084

    
1085
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
1086
}
1087

    
1088
function system_reboot_sync() {
1089
	global $g;
1090

    
1091
	system_reboot_cleanup();
1092

    
1093
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
1094
}
1095

    
1096
function system_reboot_cleanup() {
1097
	mwexec("/usr/local/bin/beep.sh stop");
1098
	captiveportal_radius_stop_all();
1099
}
1100

    
1101
function system_do_shell_commands($early = 0) {
1102
	global $config, $g;
1103
	if(isset($config['system']['developerspew'])) {
1104
		$mt = microtime();
1105
		echo "system_do_shell_commands() being called $mt\n";
1106
	}
1107

    
1108
	if ($early)
1109
		$cmdn = "earlyshellcmd";
1110
	else
1111
		$cmdn = "shellcmd";
1112

    
1113
	if (is_array($config['system'][$cmdn])) {
1114

    
1115
		/* *cmd is an array, loop through */
1116
		foreach ($config['system'][$cmdn] as $cmd) {
1117
			exec($cmd);
1118
		}
1119

    
1120
	} elseif($config['system'][$cmdn] <> "") {
1121

    
1122
		/* execute single item */
1123
		exec($config['system'][$cmdn]);
1124

    
1125
	}
1126
}
1127

    
1128
function system_console_configure() {
1129
	global $config, $g;
1130
	if(isset($config['system']['developerspew'])) {
1131
		$mt = microtime();
1132
		echo "system_console_configure() being called $mt\n";
1133
	}
1134

    
1135
	if (isset($config['system']['disableconsolemenu'])) {
1136
		touch("{$g['varetc_path']}/disableconsole");
1137
	} else {
1138
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1139
	}
1140
}
1141

    
1142
function system_dmesg_save() {
1143
	global $g;
1144
	if(isset($config['system']['developerspew'])) {
1145
		$mt = microtime();
1146
		echo "system_dmesg_save() being called $mt\n";
1147
	}
1148

    
1149
	$dmesg = "";
1150
	exec("/sbin/dmesg", $dmesg);
1151

    
1152
	/* find last copyright line (output from previous boots may be present) */
1153
	$lastcpline = 0;
1154

    
1155
	for ($i = 0; $i < count($dmesg); $i++) {
1156
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1157
			$lastcpline = $i;
1158
	}
1159

    
1160
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1161
	if (!$fd) {
1162
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1163
		return 1;
1164
	}
1165

    
1166
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1167
		fwrite($fd, $dmesg[$i] . "\n");
1168

    
1169
	fclose($fd);
1170

    
1171
	return 0;
1172
}
1173

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

    
1181
	if (isset($config['system']['harddiskstandby'])) {
1182
		if ($g['booting']) {
1183
			echo 'Setting hard disk standby... ';
1184
		}
1185

    
1186
		$standby = $config['system']['harddiskstandby'];
1187
		// Check for a numeric value
1188
		if (is_numeric($standby)) {
1189
			// Sync the disk(s)
1190
			mwexec('/bin/sync');
1191
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
1192
				// Reinitialize ATA-drives
1193
				mwexec('/usr/local/sbin/atareinit');
1194
				if ($g['booting']) {
1195
					echo "done.\n";
1196
				}
1197
			} else if ($g['booting']) {
1198
				echo "failed!\n";
1199
			}
1200
		} else if ($g['booting']) {
1201
			echo "failed!\n";
1202
		}
1203
	}
1204
}
1205

    
1206
function system_setup_sysctl() {
1207
	global $config;
1208
	if(isset($config['system']['developerspew'])) {
1209
		$mt = microtime();
1210
		echo "system_setup_sysctl() being called $mt\n";
1211
	}
1212

    
1213
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
1214
	foreach($sysctl as $sysc) {
1215
		$sysc = rtrim($sysc);
1216
		if($sysc <> "")
1217
			mwexec("sysctl {$sysc} 2>/dev/null");
1218
	}
1219
	if (isset($config['system']['sharednet'])) {
1220
		system_disable_arp_wrong_if();
1221
	}
1222
}
1223

    
1224
function system_disable_arp_wrong_if() {
1225
	global $config;
1226
	if(isset($config['system']['developerspew'])) {
1227
		$mt = microtime();
1228
		echo "system_disable_arp_wrong_if() being called $mt\n";
1229
	}
1230
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
1231
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=0");
1232
}
1233

    
1234
function system_enable_arp_wrong_if() {
1235
	global $config;
1236
	if(isset($config['system']['developerspew'])) {
1237
		$mt = microtime();
1238
		echo "system_enable_arp_wrong_if() being called $mt\n";
1239
	}
1240
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=1");
1241
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=1");
1242
}
1243

    
1244
function enable_watchdog() {
1245
	global $config;
1246
	$install_watchdog = false;
1247
	$supported_watchdogs = array("Geode");
1248
	$file = file_get_contents("/var/log/dmesg.boot");
1249
	foreach($supported_watchdogs as $sd) {
1250
		if(stristr($file, "Geode")) {
1251
			$install_watchdog = true;
1252
		}
1253
	}
1254
	if($install_watchdog == true) {
1255
		if(is_process_running("watchdogd"))
1256
			exec("/usr/bin/killall watchdogd");
1257
		exec("/usr/sbin/watchdogd");
1258
	}
1259
}
1260

    
1261
?>
(20-20/27)