Project

General

Profile

Download (34.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
/* 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
        /* get system memory amount */
40
        $memory = get_memory();
41
        $avail = $memory[0];
42

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

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

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

    
73
EOFF;
74

    
75
        config_lock();
76
		conf_mount_rw();
77

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

    
83
		conf_mount_ro();
84
		exec("sync");
85
		config_unlock();
86

    
87
}
88

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

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

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

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

    
106
        $havedns = false;
107

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

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

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

    
136
        return 0;
137
}
138

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

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

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

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

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

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

    
185
EOD;
186

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

    
203
	return 0;
204
}
205

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

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

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

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

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

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

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

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

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

    
267
		fclose($fd);
268
	}
269

    
270
	return 0;
271
}
272

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

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

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

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

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

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

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

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

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

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

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

    
366
EOD;
367
		}
368

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

    
373
EOD;
374
		}
375

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

    
380
EOD;
381
		}
382

    
383

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

    
388
EOD;
389
		}
390

    
391

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

    
396
EOD;
397
		}
398

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

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

    
412
		$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
413

    
414
	} else {
415
		$retval = mwexec("/usr/sbin/syslogd -ss");
416
	}
417

    
418
	if ($g['booting'])
419
		echo "done.\n";
420

    
421
	return $retval;
422
}
423

    
424
function system_pccard_start() {
425
	global $config, $g;
426
	if(isset($config['system']['developerspew'])) {
427
		$mt = microtime();
428
		echo "system_pccard_start() being called $mt\n";
429
	}
430

    
431
	if ($g['booting'])
432
		echo "Initializing PCMCIA...";
433

    
434
	/* kill any running pccardd */
435
	killbypid("{$g['varrun_path']}/pccardd.pid");
436

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

    
440
	if ($g['booting']) {
441
		if ($res == 0)
442
			echo "done.\n";
443
		else
444
			echo "failed!\n";
445
	}
446

    
447
	return $res;
448
}
449

    
450

    
451
function system_webgui_start() {
452
	global $config, $g;
453

    
454
	if ($g['booting'])
455
		echo "Starting webConfigurator...";
456

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

    
460
	sleep(1);
461

    
462
	/* generate password file */
463
	system_password_configure();
464

    
465
	chdir($g['www_path']);
466

    
467
	/* non-standard port? */
468
	if ($config['system']['webgui']['port'])
469
		$portarg = "{$config['system']['webgui']['port']}";
470
	else
471
		$portarg = "";
472

    
473
	if ($config['system']['webgui']['protocol'] == "https") {
474

    
475
	if(!$config['system']['webgui']['port'])
476
		$portarg = "443";
477

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

    
504
EOD;
505

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

    
523
EOD;
524
		}
525
	} else {
526
		$cert = "";
527
		$key = "";
528
	}
529

    
530
	/* generate lighttpd configuration */
531
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
532
		$cert, $key, "lighty-webConfigurator.pid", $portarg, "/usr/local/www/");
533

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

    
537
	if ($g['booting']) {
538
		if ($res == 0)
539
			echo "done.\n";
540
		else
541
			echo "failed!\n";
542
	}
543

    
544
	return $res;
545
}
546

    
547
function system_webgui_start_old() {
548
	global $config, $g;
549
	if(isset($config['system']['developerspew'])) {
550
		$mt = microtime();
551
		echo "system_webgui_start() being called $mt\n";
552
	}
553

    
554
        if ($g['booting'])
555
                echo "Starting webConfigurator...";
556

    
557
        /* kill any running mini_httpd */
558
        killbypid("{$g['varrun_path']}/mini_httpd.pid");
559

    
560
        /* generate password file */
561
        system_password_configure();
562

    
563
        chdir($g['www_path']);
564

    
565
        /* non-standard port? */
566
        if ($config['system']['webgui']['port'])
567
                $portarg = "-p {$config['system']['webgui']['port']}";
568
        else
569
                $portarg = "";
570

    
571
        if ($config['system']['webgui']['protocol'] == "https") {
572

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

    
591
EOD;
592

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

    
610
EOD;
611
                }
612

    
613
				$cert = str_replace("\r", "", $cert);
614
				$key = str_replace("\r", "", $key);
615

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

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

    
635
        if ($g['booting']) {
636
                if ($res == 0)
637
                        echo "done\n";
638
                else
639
                        echo "failed\n";
640
        }
641

    
642
        return $res;
643
}
644

    
645
function system_generate_lighty_config($filename,
646
	$cert,
647
	$key,
648
	$pid_file,
649
	$port = 80,
650
	$document_root = "/usr/local/www/",
651
	$cert_location = "cert.pem",
652
	$max_procs = 2,
653
	$max_requests = "1",
654
	$fast_cgi_enable = true,
655
	$captive_portal = false) {
656

    
657
	global $config, $g;
658

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

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

    
672
	if($captive_portal == true)  {
673
		$captiveportal = ",\"mod_rewrite\"";
674
		$captive_portal_rewrite = "url.rewrite-once = ( \"(.*captiveportal.*)\" => \"$1\", \"(.*)\" => \"/index.php?redirurl=$1\" )\n";
675
		$captive_portal_module = "\"mod_accesslog\", ";
676
		$maxprocperip = $config['captiveportal']['maxprocperip'];
677
		if(!$maxprocperip and $maxprocperip > 0)
678
			$captive_portal_mod_evasive = "evasive.max-conns-per-ip = {$maxprocperip}";
679
		else
680
			$captive_portal_mod_evasive = "";
681
	} else {
682
		$captive_portal_module = "";
683
		$captive_portal_mod_evasive = "";
684
	}
685

    
686
	if($port <> "")
687
		$lighty_port = $port;
688
	else
689
		$lighty_port = "80";
690

    
691
	$memory = get_memory();
692
	$avail = $memory[0];
693

    
694
	if($avail > 0 and $avail < 98) {
695
		$max_procs = 1;
696
		$max_requests = 1;
697
	}
698

    
699
	if($avail > 97 and $avail < 128) {
700
		$max_procs = 2;
701
		$max_requests = 1;
702
	}
703

    
704
	if($avail > 127 and $avail < 256) {
705
		$max_procs = 2;
706
		$max_requests = 1;
707
	}
708

    
709
	if($avail > 255 and $avail < 384) {
710
		$max_procs = 3;
711
		$max_requests = 1;
712
	}
713

    
714
	if($avail > 383 and $avail < 512) {
715
		$max_procs = 4;
716
		$max_requests = 1;
717
	}
718

    
719
	/* we told you that 64 megs would be slow */
720
	if ($avail > 0 and $avail < 65)
721
		$fast_cgi_enable = false;
722

    
723
	if($fast_cgi_enable == true) {
724
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
725
		$cgi_config = "";
726
		$fastcgi_config = <<<EOD
727
#### fastcgi module
728
## read fastcgi.txt for more info
729
fastcgi.server = ( ".php" =>
730
	( "localhost" =>
731
		(
732
			"socket" => "/tmp/php-fastcgi.socket",
733
			"min-procs" => 1,
734
			"max-procs" => {$max_procs},
735
			"bin-path" => "/usr/local/bin/php"
736
		)
737
	)
738
)
739

    
740
#### CGI module
741
cgi.assign                 = ( ".cgi" => "" )
742

    
743
EOD;
744
	} else {
745
		$fastcgi_config = "";
746
		$module = "\"mod_cgi\"";
747
		$cgi_config = <<<EOD
748
#### CGI module
749
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
750
                               ".cgi" => "" )
751

    
752
EOD;
753
	}
754

    
755
	$lighty_config .= <<<EOD
756
#
757
# lighttpd configuration file
758
#
759
# use a it as base for lighttpd 1.0.0 and above
760
#
761
############ Options you really have to take care of ####################
762

    
763
## FreeBSD!
764
server.event-handler		= "freebsd-kqueue"
765

    
766
{$network_handler}
767

    
768
## modules to load
769
server.modules              =   (
770
				  {$captive_portal_module}
771
				  "mod_access", "mod_accesslog",
772
                                  {$module}{$captiveportal}
773
				)
774

    
775
## Unused modules
776
#                               "mod_setenv",
777
#                               "mod_compress"
778
#				"mod_redirect",
779
#                               "mod_rewrite",
780
#                               "mod_ssi",
781
#                               "mod_usertrack",
782
#                               "mod_expire",
783
#                               "mod_secdownload",
784
#                               "mod_rrdtool",
785
#                               "mod_auth",
786
#                               "mod_status",
787
#                               "mod_alias",
788
#                               "mod_proxy",
789
#                               "mod_simple_vhost",
790
#                               "mod_evhost",
791
#                               "mod_userdir",
792
#                               "mod_cgi",
793
#                                "mod_accesslog"
794

    
795
## a static document-root, for virtual-hosting take look at the
796
## server.virtual-* options
797
server.document-root        = "{$document_root}"
798
{$captive_portal_rewrite}
799

    
800
## where to send error-messages to
801
server.errorlog             = "/var/log/lighttpd.error.log"
802

    
803
# files to check for if .../ is requested
804
server.indexfiles           = ( "index.php", "index.html",
805
                                "index.htm", "default.htm" )
806

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

    
860
# Use the "Content-Type" extended attribute to obtain mime type if possible
861
#mimetypes.use-xattr        = "enable"
862

    
863
#### accesslog module
864
#accesslog.filename          = "/dev/null"
865

    
866
## deny access the file-extensions
867
#
868
# ~    is for backupfiles from vi, emacs, joe, ...
869
# .inc is often used for code includes which should in general not be part
870
#      of the document-root
871
url.access-deny             = ( "~", ".inc" )
872

    
873

    
874
######### Options that are good to be but not neccesary to be changed #######
875

    
876
## bind to port (default: 80)
877
server.port                = {$lighty_port}
878

    
879
## error-handler for status 404
880
#server.error-handler-404   = "/error-handler.html"
881
#server.error-handler-404   = "/error-handler.php"
882

    
883
## to help the rc.scripts
884
server.pid-file            = "/var/run/{$pid_file}"
885

    
886
## virtual directory listings
887
server.dir-listing         = "disable"
888

    
889
## enable debugging
890
debug.log-request-header   = "disable"
891
debug.log-response-header  = "disable"
892
debug.log-request-handling = "disable"
893
debug.log-file-not-found   = "disable"
894

    
895
#### compress module
896
#compress.cache-dir         = "/tmp/lighttpd/cache/compress/"
897
#compress.filetype          = ("text/plain", "text/html")
898

    
899
#server.network-backend = "writev"
900

    
901
server.upload-dirs = ( "{$g['upload_path']}/", "/tmp/", "/var/" )
902

    
903
server.max-request-size = 2097152
904

    
905
{$fastcgi_config}
906

    
907
{$cgi_config}
908

    
909
{$captive_portal_mod_evasive}
910

    
911
EOD;
912

    
913
	$cert = str_replace("\r", "", $cert);
914
	$key = str_replace("\r", "", $key);
915

    
916
	$cert = str_replace("\n\n", "\n", $cert);
917
	$key = str_replace("\n\n", "\n", $key);
918

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

    
936
	$fd = fopen("{$filename}", "w");
937
	if (!$fd) {
938
		printf("Error: cannot open {$filename} in system_generate_lighty_config().\n");
939
		return 1;
940
	}
941
	fwrite($fd, $lighty_config);
942
	fclose($fd);
943

    
944
	return 0;
945

    
946
}
947

    
948
function system_password_configure() {
949
	global $config, $g;
950
	if(isset($config['system']['developerspew'])) {
951
		$mt = microtime();
952
		echo "system_password_configure() being called $mt\n";
953
	}
954

    
955
	/* sync passwords */
956
	sync_webgui_passwords();
957

    
958
	/* !NOTE! conf_mount_ro is done by sync_webgui_passwords() */
959

    
960
	return 0;
961
}
962

    
963
function system_timezone_configure() {
964
	global $config, $g;
965
	if(isset($config['system']['developerspew'])) {
966
		$mt = microtime();
967
		echo "system_timezone_configure() being called $mt\n";
968
	}
969

    
970
	$syscfg = $config['system'];
971

    
972
	if ($g['booting'])
973
		echo "Setting timezone...";
974

    
975
	/* extract appropriate timezone file */
976
	$timezone = $syscfg['timezone'];
977
	if (!$timezone)
978
		$timezone = "Etc/UTC";
979

    
980
	conf_mount_rw();
981

    
982
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
983
		escapeshellarg($timezone) . " > /etc/localtime");
984

    
985
	conf_mount_ro();
986

    
987
	if ($g['booting'])
988
		echo "done.\n";
989
}
990

    
991
function system_ntp_configure() {
992
	global $config, $g;
993

    
994
	$syscfg = $config['system'];
995

    
996
	/* open configuration for wrting or bail */
997
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
998
	if(!$fd) {
999
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1000
		return;
1001
	}
1002

    
1003
	fwrite($fd, "# \n");
1004
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1005
	fwrite($fd, "# \n\n");
1006

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

    
1011
    /* server config is in coregui1 */
1012
	$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1013
	if ($xmlsettings['enable'] == 'on') {
1014
		$ifaces = explode(',', $xmlsettings['interface']);
1015
		$ifaces = array_map('convert_friendly_interface_to_real_interface_name', $ifaces);
1016
		$ifaces = array_filter($ifaces, 'does_interface_exist');
1017
		$ips = array_map('find_interface_ip', $ifaces);
1018
		foreach ($ips as $ip) {
1019
			if (is_ipaddr($ip))
1020
				fwrite($fd, "listen on $ip\n");
1021
		}
1022
	}
1023

    
1024
	fwrite($fd, "\n");
1025

    
1026
	/* slurp! */
1027
	fclose($fd);
1028

    
1029
	/* if openntpd is running, kill it */
1030
	while(is_process_running("ntpd")) {
1031
		exec("/usr/bin/killall ntpd");
1032
		sleep(3);
1033
	}
1034

    
1035
	/* if /var/empty does not exist, create it */
1036
	if(!is_dir("/var/empty"))
1037
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1038

    
1039
	sleep(1);
1040

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

    
1044
}
1045

    
1046
function sync_system_time() {
1047
	global $config, $g;
1048

    
1049
	$syscfg = $config['system'];
1050

    
1051
	if ($g['booting'])
1052
		echo "Syncing system time before startup...";
1053

    
1054
	/* foreach through servers and write out to ntpd.conf */
1055
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1056
		mwexec("/usr/sbin/ntpdate -s $ts");
1057
	}
1058
	
1059
	if ($g['booting'])
1060
		echo "done.\n";
1061
	
1062
}
1063

    
1064
function system_halt() {
1065
	global $g;
1066

    
1067
	system_reboot_cleanup();
1068

    
1069
	mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
1070
}
1071

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

    
1075
	system_reboot_cleanup();
1076

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

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

    
1083
	system_reboot_cleanup();
1084

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

    
1088
function system_reboot_cleanup() {
1089
	mwexec("/usr/local/bin/beep.sh stop");
1090
	captiveportal_radius_stop_all();
1091
}
1092

    
1093
function system_do_shell_commands($early = 0) {
1094
	global $config, $g;
1095
	if(isset($config['system']['developerspew'])) {
1096
		$mt = microtime();
1097
		echo "system_do_shell_commands() being called $mt\n";
1098
	}
1099

    
1100
	if ($early)
1101
		$cmdn = "earlyshellcmd";
1102
	else
1103
		$cmdn = "shellcmd";
1104

    
1105
	if (is_array($config['system'][$cmdn])) {
1106

    
1107
		/* *cmd is an array, loop through */
1108
		foreach ($config['system'][$cmdn] as $cmd) {
1109
			exec($cmd);
1110
		}
1111

    
1112
	} elseif($config['system'][$cmdn] <> "") {
1113

    
1114
		/* execute single item */
1115
		exec($config['system'][$cmdn]);
1116

    
1117
	}
1118
}
1119

    
1120
function system_console_configure() {
1121
	global $config, $g;
1122
	if(isset($config['system']['developerspew'])) {
1123
		$mt = microtime();
1124
		echo "system_console_configure() being called $mt\n";
1125
	}
1126

    
1127
	if (isset($config['system']['disableconsolemenu'])) {
1128
		touch("{$g['varetc_path']}/disableconsole");
1129
	} else {
1130
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1131
	}
1132
}
1133

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

    
1141
	$dmesg = "";
1142
	exec("/sbin/dmesg", $dmesg);
1143

    
1144
	/* find last copyright line (output from previous boots may be present) */
1145
	$lastcpline = 0;
1146

    
1147
	for ($i = 0; $i < count($dmesg); $i++) {
1148
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1149
			$lastcpline = $i;
1150
	}
1151

    
1152
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1153
	if (!$fd) {
1154
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1155
		return 1;
1156
	}
1157

    
1158
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1159
		fwrite($fd, $dmesg[$i] . "\n");
1160

    
1161
	fclose($fd);
1162

    
1163
	return 0;
1164
}
1165

    
1166
function system_set_harddisk_standby() {
1167
	global $g, $config;
1168
	if(isset($config['system']['developerspew'])) {
1169
		$mt = microtime();
1170
		echo "system_set_harddisk_standby() being called $mt\n";
1171
	}
1172

    
1173
	if (isset($config['system']['harddiskstandby'])) {
1174
		if ($g['booting']) {
1175
			echo 'Setting hard disk standby... ';
1176
		}
1177

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

    
1198
function system_setup_sysctl() {
1199
	global $config;
1200
	if(isset($config['system']['developerspew'])) {
1201
		$mt = microtime();
1202
		echo "system_setup_sysctl() being called $mt\n";
1203
	}
1204

    
1205
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
1206
	foreach($sysctl as $sysc) {
1207
		if($sysc <> "")
1208
			mwexec("sysctl {$sysc} 2>/dev/null");
1209
	}
1210
	if (isset($config['system']['sharednet'])) {
1211
		system_disable_arp_wrong_if();
1212
	}
1213
}
1214

    
1215
function system_disable_arp_wrong_if() {
1216
	global $config;
1217
	if(isset($config['system']['developerspew'])) {
1218
		$mt = microtime();
1219
		echo "system_disable_arp_wrong_if() being called $mt\n";
1220
	}
1221
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
1222
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=0");
1223
}
1224

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

    
1235
function enable_watchdog() {
1236
	global $config;
1237
	$install_watchdog = false;
1238
	$supported_watchdogs = array("Geode");
1239
	$file = file_get_contents("/var/log/dmesg.boot");
1240
	foreach($supported_watchdogs as $sd) {
1241
		if(stristr($file, "Geode")) {
1242
			$install_watchdog = true;
1243
		}
1244
	}
1245
	if($install_watchdog == true) {
1246
		if(is_process_running("watchdogd"))
1247
			exec("/usr/bin/killall watchdogd");
1248
		exec("/usr/sbin/watchdogd");
1249
	}
1250
}
1251

    
1252
?>
(21-21/28)