Project

General

Profile

Download (35.9 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
	if($g['platform'] == "nanobsd" || $g['platform'] == "embedded")
41
		$upload_tmp_dir = "/root";
42
	else
43
		$upload_tmp_dir = "/tmp";
44

    
45
        /* get system memory amount */
46
        $memory = get_memory();
47
        $avail = $memory[0];
48

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

    
59
		/* create a php.ini variable */
60
		$php_conf = file_get_contents("/usr/local/lib/php.ini");
61

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

    
80
EOFF;
81

    
82
        config_lock();
83
		conf_mount_rw();
84

    
85
		/* open up php.ini and write back out contents */
86
		$fd = fopen("/usr/local/lib/php.ini","w");
87
		fwrite($fd, $php_ini);
88
		fclose($fd);
89

    
90
		mwexec("sync");
91
		conf_mount_ro();
92
		exec("sync");
93
		config_unlock();
94
}
95

    
96
function system_resolvconf_generate($dynupdate = false) {
97
	global $config, $g;
98
	if(isset($config['system']['developerspew'])) {
99
		$mt = microtime();
100
		echo "system_resolvconf_generate() being called $mt\n";
101
	}
102

    
103
        $syscfg = $config['system'];
104

    
105
        $fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
106
        if (!$fd) {
107
                printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
108
                return 1;
109
        }
110

    
111
        $resolvconf = "domain {$syscfg['domain']}\n";
112

    
113
        $havedns = false;
114

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

    
134
        fwrite($fd, $resolvconf);
135
        fclose($fd);
136

    
137
        if (!$g['booting']) {
138
                /* restart dhcpd (nameservers may have changed) */
139
                if (!$dynupdate)
140
                        services_dhcpd_configure();
141
        }
142

    
143
        return 0;
144
}
145

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

    
166
function system_hosts_generate() {
167
	global $config, $g;
168
	if(isset($config['system']['developerspew'])) {
169
		$mt = microtime();
170
		echo "system_hosts_generate() being called $mt\n";
171
	}
172

    
173
	$syscfg = $config['system'];
174
	$lancfg = $config['interfaces']['lan'];
175
	$dnsmasqcfg = $config['dnsmasq'];
176

    
177
	if (!is_array($dnsmasqcfg['hosts'])) {
178
		$dnsmasqcfg['hosts'] = array();
179
	}
180
	$hostscfg = $dnsmasqcfg['hosts'];
181

    
182
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
183
	if (!$fd) {
184
		log_error("Error: cannot open hosts file in system_hosts_generate().\n");
185
		return 1;
186
	}
187

    
188
	$hosts = <<<EOD
189
127.0.0.1	localhost localhost.{$syscfg['domain']}
190
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
191

    
192
EOD;
193

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

    
210
	return 0;
211
}
212

    
213
function system_hostname_configure() {
214
	global $config, $g;
215
	if(isset($config['system']['developerspew'])) {
216
		$mt = microtime();
217
		echo "system_hostname_configure() being called $mt\n";
218
	}
219

    
220
	$syscfg = $config['system'];
221

    
222
	/* set hostname */
223
	$status = mwexec("/bin/hostname " .
224
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
225
		
226
    /* Setup host GUID ID.  This is used by ZFS. */
227
	mwexec("/etc/rc.d/hostid start");
228

    
229
	return $status;
230
}
231

    
232
function system_routing_configure() {
233
	global $config, $g;
234
	if(isset($config['system']['developerspew'])) {
235
		$mt = microtime();
236
		echo "system_routing_configure() being called $mt\n";
237
	}
238

    
239
	/* Enable fast routing, if enabled */
240
	if(isset($config['staticroutes']['enablefastrouting']))
241
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
242

    
243
	/* clear out old routes, if necessary */
244
	exec("/usr/bin/netstat -rn", $route_arr, $retval);
245
	$route_str = implode("\n", $route_arr);
246

    
247
	if (file_exists("{$g['vardb_path']}/routes.db")) {
248
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
249
		if (!$fd) {
250
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
251
			return 1;
252
		}
253
		while (!feof($fd)) {
254
			$oldrt = trim(fgets($fd));
255
			if (($oldrt) && (stristr($route_str, $oldrt)))
256
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
257
		}
258
		fclose($fd);
259
		unlink("{$g['vardb_path']}/routes.db");
260
	}
261

    
262
	if (is_array($config['staticroutes']['route'])) {
263

    
264
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
265
		if (!$fd) {
266
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
267
			return 1;
268
		}
269

    
270
		foreach ($config['staticroutes']['route'] as $rtent) {
271
			if(isset($rtent['interfacegateway'])) {
272
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
273
					" -iface " . escapeshellarg(convert_friendly_interface_to_real_interface_name($rtent['interface'])));
274
			} else {
275
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
276
					" " . escapeshellarg($rtent['gateway']));
277
			}
278
			/* record route so it can be easily removed later (if necessary) */
279
			fwrite($fd, $rtent['network'] . "\n");
280
		}
281

    
282
		fclose($fd);
283
	}
284

    
285
	/* Make sure default gateway is present */
286
	$result = `/usr/bin/netstat -rn | grep default`;
287
	if(!$result) {
288
		if(is_ipaddr($config['interfaces']['wan']['gateway'])) {
289
			log_error("No default gateway detected, adding {$config['interfaces']['wan']['gateway']}");
290
			mwexec("/sbin/route add default " . escapeshellarg($config['interfaces']['wan']['gateway']));
291
		}
292
	}
293
	return 0;
294
}
295

    
296
function system_routing_enable() {
297
	global $config, $g;
298
	if(isset($config['system']['developerspew'])) {
299
		$mt = microtime();
300
		echo "system_routing_enable() being called $mt\n";
301
	}
302

    
303
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
304
}
305

    
306
function system_syslogd_start() {
307
	global $config, $g;
308
	if(isset($config['system']['developerspew'])) {
309
		$mt = microtime();
310
		echo "system_syslogd_start() being called $mt\n";
311
	}
312

    
313
	$syslogcfg = $config['syslog'];
314

    
315
	if ($g['booting'])
316
		echo "Starting syslog...";
317
	else
318
		killbypid("{$g['varrun_path']}/syslog.pid");
319

    
320
	if (isset($syslogcfg)) {
321
		$separatelogfacilities = array('ntpd','racoon','openvpn');
322
		if($config['installedpackages']['package']) {
323
                        foreach($config['installedpackages']['package'] as $package) {
324
                                if($package['logging']) {
325
					$pkgfacilities[] = $package['logging']['facilityname'];
326
					$separatelogfacilities = $separatelogfacilities + $pkgfacilities;
327
					$facilitylist = implode(',', $pkgfacilities);
328
					mwexec("clog -i -s 10000 {$g['varlog_path']}/{$package['logging']['logfilename']}");
329
                                	$syslogconf .= "!{$facilitylist}\n*.*\t\t\t\t\t\t%{$g['varlog_path']}/{$package['logging']['logfilename']}\n";
330
				}
331
                        }
332
                }
333
		$facilitylist = implode(',', array_unique($separatelogfacilities));
334
		/* write syslog.conf */
335
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
336
		if (!$fd) {
337
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
338
			return 1;
339
		}
340

    
341
                if (isset($syslogcfg['logall'])) {
342
			$syslogconf .= <<<EOD
343
*.*		                                @{$syslogcfg['remoteserver']}
344

    
345
EOD;
346
		}
347

    
348
		$syslogconf .= "!ntpdate,!ntpd\n";
349
		if (!isset($syslogcfg['disablelocallogging'])) {
350
			$syslogconf .= <<<EOD
351
*.*						%{$g['varlog_path']}/ntpd.log
352

    
353
EOD;
354
		}
355
		$syslogconf .= "!apinger\n";
356
		if (!isset($syslogcfg['disablelocallogging'])) {
357
			$syslogconf .= <<<EOD
358
*.*						%{$g['varlog_path']}/slbd.log
359

    
360
EOD;
361
		}
362
		$syslogconf .= "!racoon\n";
363
		if (!isset($syslogcfg['disablelocallogging'])) {
364
			$syslogconf .= <<<EOD
365
*.*						%{$g['varlog_path']}/ipsec.log
366

    
367
EOD;
368
		}
369
		if (isset($syslogcfg['vpn'])) {
370
			$syslogconf .= <<<EOD
371
*.*						@{$syslogcfg['remoteserver']}
372

    
373
EOD;
374
		}
375
		$syslogconf .= "!openvpn\n";
376
		if (!isset($syslogcfg['disablelocallogging'])) {
377
			$syslogconf .= <<<EOD
378
*.*						%{$g['varlog_path']}/openvpn.log
379

    
380
EOD;
381
		}
382
		if (isset($syslogcfg['vpn'])) {
383
			$syslogconf .= <<<EOD
384
*.*						@{$syslogcfg['remoteserver']}
385

    
386
EOD;
387
		}
388
		$syslogconf .= "!-{$facilitylist}\n";
389
		if (!isset($syslogcfg['disablelocallogging'])) {
390
		$syslogconf .= <<<EOD
391
local0.*					%{$g['varlog_path']}/filter.log
392
local3.*					%{$g['varlog_path']}/vpn.log
393
local4.*					%{$g['varlog_path']}/portalauth.log
394
local7.*					%{$g['varlog_path']}/dhcpd.log
395
*.notice;kern.debug;lpr.info;mail.crit; 	%{$g['varlog_path']}/system.log
396
news.err;local0.none;local3.none;local4.none; 	%{$g['varlog_path']}/system.log
397
local7.none					%{$g['varlog_path']}/system.log
398
security.*					%{$g['varlog_path']}/system.log
399
auth.info;authpriv.info;daemon.info		%{$g['varlog_path']}/system.log
400
local1.*					%{$g['varlog_path']}/slbd.log
401
*.emerg						*
402

    
403
EOD;
404
		}
405

    
406
		if (isset($syslogcfg['filter'])) {
407
			$syslogconf .= <<<EOD
408
local0.*					@{$syslogcfg['remoteserver']}
409

    
410
EOD;
411
		}
412

    
413
		if (isset($syslogcfg['vpn'])) {
414
			$syslogconf .= <<<EOD
415
local3.*					@{$syslogcfg['remoteserver']}
416

    
417
EOD;
418
		}
419

    
420

    
421
		if (isset($syslogcfg['portalauth'])) {
422
			$syslogconf .= <<<EOD
423
local4.*					@{$syslogcfg['remoteserver']}
424

    
425
EOD;
426
		}
427

    
428

    
429
		if (isset($syslogcfg['dhcp'])) {
430
			$syslogconf .= <<<EOD
431
local7.*					@{$syslogcfg['remoteserver']}
432

    
433
EOD;
434
		}
435

    
436
		if (isset($syslogcfg['system'])) {
437
			$syslogconf .= <<<EOD
438
*.notice;kern.debug;lpr.info;mail.crit;		@{$syslogcfg['remoteserver']}
439
news.err;local0.none;local3.none;local7.none	@{$syslogcfg['remoteserver']}
440
security.*					@{$syslogcfg['remoteserver']}
441
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
442
*.emerg						@{$syslogcfg['remoteserver']}
443

    
444
EOD;
445
		}
446
		fwrite($fd, $syslogconf);
447
		fclose($fd);
448

    
449
		// Are we logging to a least one remote server ?
450
		if(strpos($syslogconf, "@") != false)
451
			$retval = mwexec("/usr/sbin/syslogd -c -s -f {$g['varetc_path']}/syslog.conf");
452
		else
453
			$retval = mwexec("/usr/sbin/syslogd -c -ss -f {$g['varetc_path']}/syslog.conf");
454

    
455
	} else {
456
		$retval = mwexec("/usr/sbin/syslogd -c -ss");
457
	}
458

    
459
	if ($g['booting'])
460
		echo "done.\n";
461

    
462
	return $retval;
463
}
464

    
465
function system_pccard_start() {
466
	global $config, $g;
467
	if(isset($config['system']['developerspew'])) {
468
		$mt = microtime();
469
		echo "system_pccard_start() being called $mt\n";
470
	}
471

    
472
	if ($g['booting'])
473
		echo "Initializing PCMCIA...";
474

    
475
	/* kill any running pccardd */
476
	killbypid("{$g['varrun_path']}/pccardd.pid");
477

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

    
481
	if ($g['booting']) {
482
		if ($res == 0)
483
			echo "done.\n";
484
		else
485
			echo "failed!\n";
486
	}
487

    
488
	return $res;
489
}
490

    
491

    
492
function system_webgui_start() {
493
	global $config, $g;
494

    
495
	if ($g['booting'])
496
		echo "Starting webConfigurator...";
497

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

    
501
	sleep(1);
502

    
503
	/* generate password file */
504
	system_password_configure();
505

    
506
	chdir($g['www_path']);
507

    
508
	/* non-standard port? */
509
	if ($config['system']['webgui']['port'])
510
		$portarg = "{$config['system']['webgui']['port']}";
511
	else
512
		$portarg = "";
513

    
514
	if ($config['system']['webgui']['protocol'] == "https") {
515

    
516
	if(!$config['system']['webgui']['port'])
517
		$portarg = "443";
518

    
519
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
520
			$cert = base64_decode($config['system']['webgui']['certificate']);
521
			$key = base64_decode($config['system']['webgui']['private-key']);
522
		} else {
523
			/* default certificate/key */
524
			$cert = <<<EOD
525
-----BEGIN CERTIFICATE-----
526
MIIDEzCCAnygAwIBAgIJAJM91W+s6qptMA0GCSqGSIb3DQEBBAUAMGUxCzAJBgNV
527
BAYTAlVTMQswCQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UE
528
ChMHcGZTZW5zZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZTAe
529
Fw0wNjAzMTAyMzQ1MTlaFw0xNjAzMDcyMzQ1MTlaMGUxCzAJBgNVBAYTAlVTMQsw
530
CQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UEChMHcGZTZW5z
531
ZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZTCBnzANBgkqhkiG
532
9w0BAQEFAAOBjQAwgYkCgYEA3lPNTFH6qge/ygaqe/BS4oH59O6KvAesWcRzSu5N
533
21lyVE5tBbL0zqOSXmlLyReMSbtAMZqt1P8EPYFoOcaEQHIWm2VQF80Z18+8Gh4O
534
UQGjHq88OeaLqyk3OLpSKzSpXuCFrSN7q9Kez8zp5dQEu7sIW30da3pAbdqYOimA
535
1VsCAwEAAaOByjCBxzAdBgNVHQ4EFgQUAnx+ggC4SzJ0CK+rhPhJ2ZpyunEwgZcG
536
A1UdIwSBjzCBjIAUAnx+ggC4SzJ0CK+rhPhJ2ZpyunGhaaRnMGUxCzAJBgNVBAYT
537
AlVTMQswCQYDVQQIEwJLWTETMBEGA1UEBxMKTG91aXN2aWxsZTEQMA4GA1UEChMH
538
cGZTZW5zZTEQMA4GA1UECxMHcGZTZW5zZTEQMA4GA1UEAxMHcGZTZW5zZYIJAJM9
539
1W+s6qptMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAAviQpdoeabL8
540
1HSZiD7Yjx82pdLpyQOdXvAu3jEAYz53ckx0zSMrzsQ5r7Vae6AE7Xd7Pj+1Yihs
541
AJZzOQujnmsuim7qu6YSxzP34xonKwd1C9tZUlyNRNnEmtXOEDupn05bih1ugtLG
542
kqfPIgDbDLXuPtEAA6QDUypaunI6+1E=
543
-----END CERTIFICATE-----
544

    
545
EOD;
546

    
547
			$key = <<<EOD
548
-----BEGIN RSA PRIVATE KEY-----
549
MIICXgIBAAKBgQDeU81MUfqqB7/KBqp78FLigfn07oq8B6xZxHNK7k3bWXJUTm0F
550
svTOo5JeaUvJF4xJu0Axmq3U/wQ9gWg5xoRAchabZVAXzRnXz7waHg5RAaMerzw5
551
5ourKTc4ulIrNKle4IWtI3ur0p7PzOnl1AS7uwhbfR1rekBt2pg6KYDVWwIDAQAB
552
AoGAP7E0VFP8Aq/7os3sE1uS8y8XQ7L+7cUo/AKKoQHKLjfeyAY7t3FALt6vdPqn
553
anGjkA/j4RIWELoKJfCnwj17703NDCPwB7klcmZvmTx5Om1ZrRyZdQ6RJs0pOOO1
554
r2wOnZNaNWStXE9Afpw3dj20Gh0V/Ioo5HXn3sHfxZm8dnkCQQDwv8OaUdp2Hl8t
555
FDfXB1CMvUG1hEAvbQvZK1ODkE7na2/ChKjVPddEI3DvfzG+nLrNuTrAyVWgRLte
556
r8qX5PQHAkEA7GlKx0S18LdiKo6wy2QeGu6HYkPncaHNFOWX8cTpvGGtQoWYSh0J
557
tjCt1/mz4/XkvZWuZyTNx2FdkVlNF5nHDQJBAIRWVTZqEjVlwpmsCHnp6mxCyHD4
558
DrRDNAUfnNuwIr9xPlDlzUzSnpc1CCqOd5C45LKbRGGfCrN7tKd66FmQoFcCQQCy
559
Kvw3R1pTCvHJnvYwoshphaC0dvaDVeyINiwYAk4hMf/wpVxLZqz+CJvLrB1dzOBR
560
3O+uPjdzbrakpweJpNQ1AkEA3ZtlgEj9eWsLAJP8aKlwB8VqD+EtG9OJSUMnCDiQ
561
WFFNj/t3Ze3IVuAyL/yMpiv3JNEnZhIxCta42eDFpIZAKw==
562
-----END RSA PRIVATE KEY-----
563

    
564
EOD;
565
		}
566
	} else {
567
		$cert = "";
568
		$key = "";
569
	}
570

    
571
	/* generate lighttpd configuration */
572
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
573
		$cert, $key, "lighty-webConfigurator.pid", $portarg, "/usr/local/www/");
574

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

    
578
	if ($g['booting']) {
579
		if ($res == 0)
580
			echo "done.\n";
581
		else
582
			echo "failed!\n";
583
	}
584

    
585
	return $res;
586
}
587

    
588
function system_webgui_start_old() {
589
	global $config, $g;
590
	if(isset($config['system']['developerspew'])) {
591
		$mt = microtime();
592
		echo "system_webgui_start() being called $mt\n";
593
	}
594

    
595
        if ($g['booting'])
596
                echo "Starting webConfigurator...";
597

    
598
        /* kill any running mini_httpd */
599
        killbypid("{$g['varrun_path']}/mini_httpd.pid");
600

    
601
        /* generate password file */
602
        system_password_configure();
603

    
604
        chdir($g['www_path']);
605

    
606
        /* non-standard port? */
607
        if ($config['system']['webgui']['port'])
608
                $portarg = "-p {$config['system']['webgui']['port']}";
609
        else
610
                $portarg = "";
611

    
612
        if ($config['system']['webgui']['protocol'] == "https") {
613

    
614
                if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
615
                        $cert = base64_decode($config['system']['webgui']['certificate']);
616
                        $key  = base64_decode($config['system']['webgui']['private-key']);
617
                } else {
618
                        /* default certificate/key */
619
                        $cert = <<<EOD
620
-----BEGIN CERTIFICATE-----
621
MIIBlDCB/gIBADANBgkqhkiG9w0BAQQFADATMREwDwYDVQQKEwhtMG4wd2FsbDAe
622
Fw0wNTA1MTAxMjI0NDRaFw0wNzA1MTAxMjI0NDRaMBMxETAPBgNVBAoTCG0wbjB3
623
YWxsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAShszhFz+o8lsMWTGgTxs
624
TMPR+v4+qL5jXDyY97MLTGFK7aqQOtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+
625
83LPQmQoSPC0VqhfU3uYf3NzxiK8r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFP
626
C4jE2fvjkbzyVolPywBuewIDAQABMA0GCSqGSIb3DQEBBAUAA4GBAFR962c4R5tV
627
cTn0OQcszYoW6WC+ini9tQQh5ku5jYDAiC+00atawJEVLnL3lwAcpSKTIWlTkD20
628
tl3lz5br1qFgYky+Rd0kwS2nk9jRbkxSXxd6KJVnNRCKre28aw3ENzZfCSurPQsX
629
UPp5er+NtwMT1g7s/JDmKTC4w1rGr5/c
630
-----END CERTIFICATE-----
631

    
632
EOD;
633

    
634
                        $key = <<<EOD
635
-----BEGIN RSA PRIVATE KEY-----
636
MIICXQIBAAKBgQDAShszhFz+o8lsMWTGgTxsTMPR+v4+qL5jXDyY97MLTGFK7aqQ
637
OtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+83LPQmQoSPC0VqhfU3uYf3NzxiK8
638
r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFPC4jE2fvjkbzyVolPywBuewIDAQAB
639
AoGAbJJrQW9fQrggJuLMz/hwsYW2m31oyOBmf5u463YQtjRuSuxe/gj87weZuNqY
640
H2rXq2k2K+ehl8hgW+egASyUL3L7kCkEAsVREujKTEyhSqqIRDPWTxo9S/YA9Gvn
641
2ZnJvkrcKjqCO9aHX3rvJOK/ErYI6akctgI3KmgkYw5XNmECQQDuZU97RTWH9rmP
642
aQr57ysNXxgFsyhetOOqeYkPtIVwpOiNbfwE1zi5RGdtO4Ku3fG1lV4J2UoWJ9yD
643
awdoyYIHAkEAzn0xJ90IjPsHk+8SODEj5JGdHSZPNu1tgtrbjEi9sfGWg4K7XTxr
644
QW90pWb1bKKU1uh5FzW6OhnFfuQXt1kC7QJAPSthqY+onKqCEnoxhtAHi/bKgyvl
645
P+fKQwPMV2tKkgy+XwvJjrRqqZ8TqsOKVLQ+QQmCh6RpjiXMPyxHSmvqIQJBAKLR
646
HF1ucDuaBROkwx0DwmWMW/KMLpIFDQDNSaiIAuu4rxHrl4mhBoGGPNffI04RtILw
647
s+qVNs5xW8T+XaT4ztECQQDFHPnZeoPWE5z+AX/UUQIUWaDExz3XRzmIxRbOrlFi
648
CsF1s0TdJLi/wzNQRAL37A8vqCeVFR/ng3Xpg96Yg+8Z
649
-----END RSA PRIVATE KEY-----
650

    
651
EOD;
652
                }
653

    
654
				$cert = str_replace("\r", "", $cert);
655
				$key = str_replace("\r", "", $key);
656

    
657
                $fd = fopen("{$g['varetc_path']}/cert.pem", "w");
658
                if (!$fd) {
659
                        printf("Error: cannot open cert.pem in system_webgui_start().\n");
660
                        return 1;
661
                }
662
                chmod("{$g['varetc_path']}/cert.pem", 0600);
663
                fwrite($fd, $cert);
664
                fwrite($fd, "\n");
665
                fwrite($fd, $key);
666
                fclose($fd);
667

    
668
                $res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
669
                        " -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
670
                        " -i {$g['varrun_path']}/mini_httpd.pid");
671
        } else {
672
                $res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
673
                        " -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
674
        }
675

    
676
        if ($g['booting']) {
677
                if ($res == 0)
678
                        echo "done\n";
679
                else
680
                        echo "failed\n";
681
        }
682

    
683
        return $res;
684
}
685

    
686
function system_generate_lighty_config($filename,
687
	$cert,
688
	$key,
689
	$pid_file,
690
	$port = 80,
691
	$document_root = "/usr/local/www/",
692
	$cert_location = "cert.pem",
693
	$max_procs = 4,
694
	$max_requests = "1",
695
	$fast_cgi_enable = true,
696
	$captive_portal = false) {
697

    
698
	global $config, $g;
699

    
700
	if(isset($config['system']['developerspew'])) {
701
		$mt = microtime();
702
		echo "system_generate_lighty_config() being called $mt\n";
703
	}
704

    
705
	if($captive_portal == true)  {
706
		$captiveportal = ",\"mod_rewrite\"";
707
		$captive_portal_rewrite = "url.rewrite-once = ( \"(.*captiveportal.*)\" => \"$1\", \"(.*)\" => \"/index.php?redirurl=$1\" )\n";
708
		$captive_portal_module = "\"mod_accesslog\", ";
709
		$maxprocperip = $config['captiveportal']['maxprocperip'];
710
		if(!$maxprocperip and $maxprocperip > 0)
711
			$captive_portal_mod_evasive = "evasive.max-conns-per-ip = {$maxprocperip}";
712
		else
713
			$captive_portal_mod_evasive = "";
714
		$server_upload_dirs = "server.upload-dirs = ( \"/tmp/captiveportal/\" )\n";
715
		exec("mkdir -p /tmp/captiveportal");
716
		exec("chmod a-w /tmp/captiveportal");
717
		$server_max_request_size = "server.max-request-size    = 384";
718
	} else {
719
		$captive_portal_module = "";
720
		$captive_portal_mod_evasive = "";
721
		$server_upload_dirs = "server.upload-dirs = ( \"{$g['upload_path']}/\", \"/tmp/\", \"/var/\" )\n";
722
		$server_max_request_size = "server.max-request-size    = 2097152";
723
	}
724

    
725
	if($port <> "")
726
		$lighty_port = $port;
727
	else
728
		$lighty_port = "80";
729

    
730
	$memory = get_memory();
731
	$avail = $memory[0];
732

    
733
	if($avail > 0 and $avail < 98) {
734
		$max_procs = 1;
735
	}
736

    
737
	if($avail > 97 and $avail < 128) {
738
		$max_procs = 2;
739
	}
740

    
741
	if($avail > 127 and $avail < 256) {
742
		$max_procs = 3;
743
	}
744

    
745
	if($avail > 255 and $avail < 384) {
746
		$max_procs = 4;
747
	}
748

    
749
	if($avail > 384) {
750
		$max_procs = 5;
751
	}
752

    
753
	if($captive_portal == true)  {	
754
		$bin_environment =  <<<EOC
755
        "bin-environment" => (
756
			"PHP_FCGI_MAX_REQUESTS" => "500",
757
			"PHP_FCGI_CHILDREN" => "$max_procs"
758
        ), 
759
EOC;
760

    
761
	} else {
762
		$bin_environment = "";
763
	}
764
		
765
	if($fast_cgi_enable == true) {
766
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
767
		$cgi_config = "";
768
		$fastcgi_config = <<<EOD
769
#### fastcgi module
770
## read fastcgi.txt for more info
771
fastcgi.server = ( ".php" =>
772
	( "localhost" =>
773
		(
774
			"socket" => "/tmp/php-fastcgi.socket",
775
			"min-procs" => 1,
776
			"max-procs" => {$max_procs},
777
			"idle-timeout" => 0,
778
			{$bin_environment}			
779
			"bin-path" => "/usr/local/bin/php"
780
		)
781
	)
782
)
783

    
784
#### CGI module
785
cgi.assign                 = ( ".cgi" => "" )
786

    
787
EOD;
788
	} else {
789
		$fastcgi_config = "";
790
		$module = "\"mod_cgi\"";
791
		$cgi_config = <<<EOD
792
#### CGI module
793
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
794
                               ".cgi" => "" )
795

    
796
EOD;
797
	}
798

    
799
	$lighty_config .= <<<EOD
800
#
801
# lighttpd configuration file
802
#
803
# use a it as base for lighttpd 1.0.0 and above
804
#
805
############ Options you really have to take care of ####################
806

    
807
## FreeBSD!
808
server.event-handler		= "freebsd-kqueue"
809
server.network-backend		= "writev"  ## Fixes 7.x upload issues
810

    
811
{$network_handler}
812

    
813
## modules to load
814
server.modules              =   (
815
				  {$captive_portal_module}
816
				  "mod_access", "mod_accesslog",
817
                                  {$module}{$captiveportal}
818
				)
819

    
820
## Unused modules
821
#                               "mod_setenv",
822
#                               "mod_compress"
823
#				"mod_redirect",
824
#                               "mod_rewrite",
825
#                               "mod_ssi",
826
#                               "mod_usertrack",
827
#                               "mod_expire",
828
#                               "mod_secdownload",
829
#                               "mod_rrdtool",
830
#                               "mod_auth",
831
#                               "mod_status",
832
#                               "mod_alias",
833
#                               "mod_proxy",
834
#                               "mod_simple_vhost",
835
#                               "mod_evhost",
836
#                               "mod_userdir",
837
#                               "mod_cgi",
838
#                                "mod_accesslog"
839

    
840
## a static document-root, for virtual-hosting take look at the
841
## server.virtual-* options
842
server.document-root        = "{$document_root}"
843
{$captive_portal_rewrite}
844

    
845
# Maximum idle time with nothing being written (php downloading)
846
server.max-write-idle = 999
847

    
848
## where to send error-messages to
849
server.errorlog             = "/var/log/lighttpd.error.log"
850

    
851
# files to check for if .../ is requested
852
server.indexfiles           = ( "index.php", "index.html",
853
                                "index.htm", "default.htm" )
854

    
855
# mimetype mapping
856
mimetype.assign             = (
857
  ".pdf"          =>      "application/pdf",
858
  ".sig"          =>      "application/pgp-signature",
859
  ".spl"          =>      "application/futuresplash",
860
  ".class"        =>      "application/octet-stream",
861
  ".ps"           =>      "application/postscript",
862
  ".torrent"      =>      "application/x-bittorrent",
863
  ".dvi"          =>      "application/x-dvi",
864
  ".gz"           =>      "application/x-gzip",
865
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
866
  ".swf"          =>      "application/x-shockwave-flash",
867
  ".tar.gz"       =>      "application/x-tgz",
868
  ".tgz"          =>      "application/x-tgz",
869
  ".tar"          =>      "application/x-tar",
870
  ".zip"          =>      "application/zip",
871
  ".mp3"          =>      "audio/mpeg",
872
  ".m3u"          =>      "audio/x-mpegurl",
873
  ".wma"          =>      "audio/x-ms-wma",
874
  ".wax"          =>      "audio/x-ms-wax",
875
  ".ogg"          =>      "audio/x-wav",
876
  ".wav"          =>      "audio/x-wav",
877
  ".gif"          =>      "image/gif",
878
  ".jpg"          =>      "image/jpeg",
879
  ".jpeg"         =>      "image/jpeg",
880
  ".png"          =>      "image/png",
881
  ".xbm"          =>      "image/x-xbitmap",
882
  ".xpm"          =>      "image/x-xpixmap",
883
  ".xwd"          =>      "image/x-xwindowdump",
884
  ".css"          =>      "text/css",
885
  ".html"         =>      "text/html",
886
  ".htm"          =>      "text/html",
887
  ".js"           =>      "text/javascript",
888
  ".asc"          =>      "text/plain",
889
  ".c"            =>      "text/plain",
890
  ".conf"         =>      "text/plain",
891
  ".text"         =>      "text/plain",
892
  ".txt"          =>      "text/plain",
893
  ".dtd"          =>      "text/xml",
894
  ".xml"          =>      "text/xml",
895
  ".mpeg"         =>      "video/mpeg",
896
  ".mpg"          =>      "video/mpeg",
897
  ".mov"          =>      "video/quicktime",
898
  ".qt"           =>      "video/quicktime",
899
  ".avi"          =>      "video/x-msvideo",
900
  ".asf"          =>      "video/x-ms-asf",
901
  ".asx"          =>      "video/x-ms-asf",
902
  ".wmv"          =>      "video/x-ms-wmv",
903
  ".bz2"          =>      "application/x-bzip",
904
  ".tbz"          =>      "application/x-bzip-compressed-tar",
905
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
906
 )
907

    
908
# Use the "Content-Type" extended attribute to obtain mime type if possible
909
#mimetypes.use-xattr        = "enable"
910

    
911
#### accesslog module
912
#accesslog.filename          = "/dev/null"
913

    
914
## deny access the file-extensions
915
#
916
# ~    is for backupfiles from vi, emacs, joe, ...
917
# .inc is often used for code includes which should in general not be part
918
#      of the document-root
919
url.access-deny             = ( "~", ".inc" )
920

    
921

    
922
######### Options that are good to be but not neccesary to be changed #######
923

    
924
## bind to port (default: 80)
925
server.port                = {$lighty_port}
926

    
927
## error-handler for status 404
928
#server.error-handler-404   = "/error-handler.html"
929
#server.error-handler-404   = "/error-handler.php"
930

    
931
## to help the rc.scripts
932
server.pid-file            = "/var/run/{$pid_file}"
933

    
934
## virtual directory listings
935
server.dir-listing         = "disable"
936

    
937
## enable debugging
938
debug.log-request-header   = "disable"
939
debug.log-response-header  = "disable"
940
debug.log-request-handling = "disable"
941
debug.log-file-not-found   = "disable"
942

    
943
#### compress module
944
#compress.cache-dir         = "/tmp/lighttpd/cache/compress/"
945
#compress.filetype          = ("text/plain", "text/html")
946

    
947
#server.network-backend = "writev"
948

    
949
{$server_upload_dirs}
950

    
951
{$server_max_request_size}
952

    
953
{$fastcgi_config}
954

    
955
{$cgi_config}
956

    
957
{$captive_portal_mod_evasive}
958

    
959
EOD;
960

    
961
	$cert = str_replace("\r", "", $cert);
962
	$key = str_replace("\r", "", $key);
963

    
964
	$cert = str_replace("\n\n", "\n", $cert);
965
	$key = str_replace("\n\n", "\n", $key);
966

    
967
	if($cert <> "" and $key <> "") {
968
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
969
		if (!$fd) {
970
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
971
			return 1;
972
		}
973
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
974
		fwrite($fd, $cert);
975
		fwrite($fd, "\n");
976
		fwrite($fd, $key);
977
		fclose($fd);
978
		$lighty_config .= "\n";
979
		$lighty_config .= "## ssl configuration\n";
980
		$lighty_config .= "ssl.engine = \"enable\"\n";
981
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_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_password_configure() {
997
	global $config, $g;
998
	if(isset($config['system']['developerspew'])) {
999
		$mt = microtime();
1000
		echo "system_password_configure() being called $mt\n";
1001
	}
1002

    
1003
	/* sync passwords */
1004
	sync_webgui_passwords();
1005

    
1006
	/* !NOTE! conf_mount_ro is done by sync_webgui_passwords() */
1007

    
1008
	return 0;
1009
}
1010

    
1011
function system_timezone_configure() {
1012
	global $config, $g;
1013
	if(isset($config['system']['developerspew'])) {
1014
		$mt = microtime();
1015
		echo "system_timezone_configure() being called $mt\n";
1016
	}
1017

    
1018
	$syscfg = $config['system'];
1019

    
1020
	if ($g['booting'])
1021
		echo "Setting timezone...";
1022

    
1023
	/* extract appropriate timezone file */
1024
	$timezone = $syscfg['timezone'];
1025
	if (!$timezone)
1026
		$timezone = "Etc/UTC";
1027

    
1028
	conf_mount_rw();
1029

    
1030
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
1031
		escapeshellarg($timezone) . " > /etc/localtime");
1032

    
1033
	mwexec("sync");
1034
	conf_mount_ro();
1035

    
1036
	if ($g['booting'])
1037
		echo "done.\n";
1038
}
1039

    
1040
function system_ntp_configure() {
1041
	global $config, $g;
1042

    
1043
	$syscfg = $config['system'];
1044

    
1045
	/* open configuration for wrting or bail */
1046
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
1047
	if(!$fd) {
1048
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1049
		return;
1050
	}
1051

    
1052
	fwrite($fd, "# \n");
1053
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1054
	fwrite($fd, "# \n\n");
1055

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

    
1060
    /* server config is in coregui1 */
1061
	$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1062
	if ($xmlsettings['enable'] == 'on') {
1063
		$ifaces = explode(',', $xmlsettings['interface']);
1064
		$ifaces = array_map('convert_friendly_interface_to_real_interface_name', $ifaces);
1065
		$ifaces = array_filter($ifaces, 'does_interface_exist');
1066
		$ips = array_map('find_interface_ip', $ifaces);
1067
		foreach ($ips as $ip) {
1068
			if (is_ipaddr($ip))
1069
				fwrite($fd, "listen on $ip\n");
1070
		}
1071
	}
1072

    
1073
	fwrite($fd, "\n");
1074

    
1075
	/* slurp! */
1076
	fclose($fd);
1077

    
1078
	/* if openntpd is running, kill it */
1079
	while(is_process_running("ntpd")) {
1080
		mwexec("/usr/bin/killall ntpd", true);
1081
	}
1082

    
1083
	/* if /var/empty does not exist, create it */
1084
	if(!is_dir("/var/empty"))
1085
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1086

    
1087
	if($g['booting'])
1088
		return;
1089

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

    
1093
}
1094

    
1095
function sync_system_time() {
1096
	global $config, $g;
1097

    
1098
	$syscfg = $config['system'];
1099

    
1100
	if ($g['booting'])
1101
		echo "Syncing system time before startup...";
1102

    
1103
	/* foreach through servers and write out to ntpd.conf */
1104
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1105
		mwexec("/usr/sbin/ntpdate -s $ts");
1106
	}
1107
	
1108
	if ($g['booting'])
1109
		echo "done.\n";
1110
	
1111
}
1112

    
1113
function system_halt() {
1114
	global $g;
1115

    
1116
	system_reboot_cleanup();
1117

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

    
1121
function system_reboot() {
1122
	global $g;
1123

    
1124
	system_reboot_cleanup();
1125

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

    
1129
function system_reboot_sync() {
1130
	global $g;
1131

    
1132
	system_reboot_cleanup();
1133

    
1134
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
1135
}
1136

    
1137
function system_reboot_cleanup() {
1138
	mwexec("/usr/local/bin/beep.sh stop");
1139
	captiveportal_radius_stop_all();
1140
}
1141

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

    
1149
	if ($early)
1150
		$cmdn = "earlyshellcmd";
1151
	else
1152
		$cmdn = "shellcmd";
1153

    
1154
	if (is_array($config['system'][$cmdn])) {
1155

    
1156
		/* *cmd is an array, loop through */
1157
		foreach ($config['system'][$cmdn] as $cmd) {
1158
			exec($cmd);
1159
		}
1160

    
1161
	} elseif($config['system'][$cmdn] <> "") {
1162

    
1163
		/* execute single item */
1164
		exec($config['system'][$cmdn]);
1165

    
1166
	}
1167
}
1168

    
1169
function system_console_configure() {
1170
	global $config, $g;
1171
	if(isset($config['system']['developerspew'])) {
1172
		$mt = microtime();
1173
		echo "system_console_configure() being called $mt\n";
1174
	}
1175

    
1176
	if (isset($config['system']['disableconsolemenu'])) {
1177
		touch("{$g['varetc_path']}/disableconsole");
1178
	} else {
1179
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1180
	}
1181
}
1182

    
1183
function system_dmesg_save() {
1184
	global $g;
1185
	if(isset($config['system']['developerspew'])) {
1186
		$mt = microtime();
1187
		echo "system_dmesg_save() being called $mt\n";
1188
	}
1189

    
1190
	$dmesg = "";
1191
	exec("/sbin/dmesg", $dmesg);
1192

    
1193
	/* find last copyright line (output from previous boots may be present) */
1194
	$lastcpline = 0;
1195

    
1196
	for ($i = 0; $i < count($dmesg); $i++) {
1197
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1198
			$lastcpline = $i;
1199
	}
1200

    
1201
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1202
	if (!$fd) {
1203
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1204
		return 1;
1205
	}
1206

    
1207
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1208
		fwrite($fd, $dmesg[$i] . "\n");
1209

    
1210
	fclose($fd);
1211

    
1212
	return 0;
1213
}
1214

    
1215
function system_set_harddisk_standby() {
1216
	global $g, $config;
1217
	if(isset($config['system']['developerspew'])) {
1218
		$mt = microtime();
1219
		echo "system_set_harddisk_standby() being called $mt\n";
1220
	}
1221

    
1222
	if (isset($config['system']['harddiskstandby'])) {
1223
		if ($g['booting']) {
1224
			echo 'Setting hard disk standby... ';
1225
		}
1226

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

    
1247
function system_setup_sysctl() {
1248
	global $config;
1249
	if(isset($config['system']['developerspew'])) {
1250
		$mt = microtime();
1251
		echo "system_setup_sysctl() being called $mt\n";
1252
	}
1253

    
1254
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
1255
	foreach($sysctl as $sysc) {
1256
		$sysc = rtrim($sysc);
1257
		if($sysc <> "")
1258
			mwexec("sysctl {$sysc} 2>/dev/null");
1259
	}
1260
	if (isset($config['system']['sharednet'])) {
1261
		system_disable_arp_wrong_if();
1262
	}
1263
}
1264

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

    
1275
function system_enable_arp_wrong_if() {
1276
	global $config;
1277
	if(isset($config['system']['developerspew'])) {
1278
		$mt = microtime();
1279
		echo "system_enable_arp_wrong_if() being called $mt\n";
1280
	}
1281
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=1");
1282
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=1");
1283
}
1284

    
1285
?>
(20-20/27)