Project

General

Profile

Download (15.3 KB) Statistics
| Branch: | Tag: | Revision:
1 5b237745 Scott Ullrich
<?php
2 307cd525 Bill Marquette
/* $Id$ */
3 5b237745 Scott Ullrich
/*
4
	system.inc
5
	part of m0n0wall (http://m0n0.ch/wall)
6 0f282d7a Scott Ullrich
7 5b237745 Scott Ullrich
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
8
	All rights reserved.
9 0f282d7a Scott Ullrich
10 5b237745 Scott Ullrich
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12 0f282d7a Scott Ullrich
13 5b237745 Scott Ullrich
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15 0f282d7a Scott Ullrich
16 5b237745 Scott Ullrich
	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 0f282d7a Scott Ullrich
20 5b237745 Scott Ullrich
	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 0f282d7a Scott Ullrich
35 5b237745 Scott Ullrich
function system_resolvconf_generate($dynupdate = false) {
36
	global $config, $g;
37 0f282d7a Scott Ullrich
38 5b237745 Scott Ullrich
	$syscfg = $config['system'];
39 0f282d7a Scott Ullrich
40 186a42b0 Bill Marquette
	$fd = fopen("{$g['varetc_path']}/resolv.conf.new", "w");
41 5b237745 Scott Ullrich
	if (!$fd) {
42 186a42b0 Bill Marquette
		printf("Error: cannot open resolv.conf.new in system_resolvconf_generate().\n");
43 5b237745 Scott Ullrich
		return 1;
44
	}
45 0f282d7a Scott Ullrich
46 5b237745 Scott Ullrich
	$resolvconf = "domain {$syscfg['domain']}\n";
47 0f282d7a Scott Ullrich
48 5b237745 Scott Ullrich
	$havedns = false;
49 0f282d7a Scott Ullrich
50 5b237745 Scott Ullrich
	if (isset($syscfg['dnsallowoverride'])) {
51 186a42b0 Bill Marquette
		/* get dynamically assigned DNS servers for ppp (if any) */
52 5b237745 Scott Ullrich
		$nfd = @fopen("{$g['varetc_path']}/nameservers.conf", "r");
53
		if ($nfd) {
54
			while (!feof($nfd)) {
55
				$dnss = trim(fgets($nfd));
56
				if ($dnss) {
57
					$resolvconf .= "nameserver $dnss\n";
58
					$havedns = true;
59
				}
60
			}
61
			fclose($nfd);
62
		}
63
	}
64 186a42b0 Bill Marquette
65
	/* if we didn't get assigned DNS servers and have some set add 'em */
66 5b237745 Scott Ullrich
	if (!$havedns && is_array($syscfg['dnsserver'])) {
67
		foreach ($syscfg['dnsserver'] as $ns) {
68
			if ($ns)
69
				$resolvconf .= "nameserver $ns\n";
70
			$havedns = true;
71
		}
72
	}
73 0f282d7a Scott Ullrich
74 5b237745 Scott Ullrich
	fwrite($fd, $resolvconf);
75
	fclose($fd);
76 0f282d7a Scott Ullrich
77 186a42b0 Bill Marquette
	/* If we now have DNS servers, overwrite resolv.conf */
78
	if ($havedns) {
79
		if (file_exists("{$g['varetc_path']}/resolv.conf"))
80
			unlink("{$g['varetc_path']}/resolv.conf");
81
		rename("{$g['varetc_path']}/resolv.conf.new", "{$g['varetc_path']}/resolv.conf");
82
	} else {
83
		unlink("{$g['varetc_path']}/resolv.conf.new");
84
	}
85 27150275 Scott Ullrich
86 186a42b0 Bill Marquette
87 5b237745 Scott Ullrich
	if (!$g['booting']) {
88
		/* restart dhcpd (nameservers may have changed) */
89
		if (!$dynupdate)
90
			services_dhcpd_configure();
91
	}
92 0f282d7a Scott Ullrich
93 5b237745 Scott Ullrich
	return 0;
94
}
95
96
function system_hosts_generate() {
97
	global $config, $g;
98 0f282d7a Scott Ullrich
99 5b237745 Scott Ullrich
	$syscfg = $config['system'];
100
	$lancfg = $config['interfaces']['lan'];
101
	$dnsmasqcfg = $config['dnsmasq'];
102
103
	if (!is_array($dnsmasqcfg['hosts'])) {
104
		$dnsmasqcfg['hosts'] = array();
105
	}
106
	$hostscfg = $dnsmasqcfg['hosts'];
107 0f282d7a Scott Ullrich
108 5b237745 Scott Ullrich
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
109
	if (!$fd) {
110
		printf("Error: cannot open hosts file in system_hosts_generate().\n");
111
		return 1;
112
	}
113 0f282d7a Scott Ullrich
114 5b237745 Scott Ullrich
	$hosts = <<<EOD
115
127.0.0.1	localhost localhost.{$syscfg['domain']}
116
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
117
118
EOD;
119 0f282d7a Scott Ullrich
120 5b237745 Scott Ullrich
	foreach ($hostscfg as $host) {
121
		if ($host['host'])
122
			$hosts .= "{$host['ip']}	{$host['host']}.{$host['domain']} {$host['host']}\n";
123
		else
124
			$hosts .= "{$host['ip']}	{$host['domain']}\n";
125
	}
126
	fwrite($fd, $hosts);
127
	fclose($fd);
128 0f282d7a Scott Ullrich
129 5b237745 Scott Ullrich
	return 0;
130
}
131
132
function system_hostname_configure() {
133
	global $config, $g;
134 0f282d7a Scott Ullrich
135 5b237745 Scott Ullrich
	$syscfg = $config['system'];
136 0f282d7a Scott Ullrich
137 5b237745 Scott Ullrich
	/* set hostname */
138
	return mwexec("/bin/hostname " .
139
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
140
}
141
142
function system_routing_configure() {
143
	global $config, $g;
144 0f282d7a Scott Ullrich
145
	/* Enable fast routing, if enabled */
146
	if(isset($config['staticroutes']['enablefastrouting']))
147
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
148
149 5b237745 Scott Ullrich
	/* clear out old routes, if necessary */
150
	if (file_exists("{$g['vardb_path']}/routes.db")) {
151
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
152
		if (!$fd) {
153
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
154 0f282d7a Scott Ullrich
			return 1;
155 5b237745 Scott Ullrich
		}
156
		while (!feof($fd)) {
157
			$oldrt = fgets($fd);
158
			if ($oldrt)
159
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
160
		}
161
		fclose($fd);
162
		unlink("{$g['vardb_path']}/routes.db");
163
	}
164 0f282d7a Scott Ullrich
165 5b237745 Scott Ullrich
	if (is_array($config['staticroutes']['route'])) {
166 0f282d7a Scott Ullrich
167 5b237745 Scott Ullrich
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
168
		if (!$fd) {
169
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
170 0f282d7a Scott Ullrich
			return 1;
171 5b237745 Scott Ullrich
		}
172 0f282d7a Scott Ullrich
173 5b237745 Scott Ullrich
		foreach ($config['staticroutes']['route'] as $rtent) {
174 0f282d7a Scott Ullrich
			mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
175 5b237745 Scott Ullrich
				" " . escapeshellarg($rtent['gateway']));
176 0f282d7a Scott Ullrich
177 5b237745 Scott Ullrich
			/* record route so it can be easily removed later (if necessary) */
178
			fwrite($fd, $rtent['network'] . "\n");
179
		}
180 0f282d7a Scott Ullrich
181
		fclose($fd);
182 5b237745 Scott Ullrich
	}
183 0f282d7a Scott Ullrich
184 5b237745 Scott Ullrich
	return 0;
185
}
186
187
function system_routing_enable() {
188
	global $config, $g;
189 0f282d7a Scott Ullrich
190 5b237745 Scott Ullrich
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
191
}
192
193
function system_syslogd_start() {
194
	global $config, $g;
195 0f282d7a Scott Ullrich
196 5b237745 Scott Ullrich
	$syslogcfg = $config['syslog'];
197
198 0f282d7a Scott Ullrich
	if ($g['booting'])
199 5b237745 Scott Ullrich
		echo "Starting syslog service... ";
200
	else
201
		killbypid("{$g['varrun_path']}/syslog.pid");
202 0f282d7a Scott Ullrich
203 5b237745 Scott Ullrich
	if (isset($syslogcfg['enable'])) {
204
205
		/* write syslog.conf */
206
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
207
		if (!$fd) {
208
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
209
			return 1;
210
		}
211 0f282d7a Scott Ullrich
212 5b237745 Scott Ullrich
		$syslogconf = <<<EOD
213 bc328042 Bill Marquette
local0.*					%{$g['varlog_path']}/filter.log
214
local3.*					%{$g['varlog_path']}/vpn.log
215
local4.*					%{$g['varlog_path']}/portalauth.log
216
local7.*					%{$g['varlog_path']}/dhcpd.log
217
*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local4.none;local7.none %{$g['varlog_path']}/system.log
218
security.*					%{$g['varlog_path']}/system.log
219
auth.info;authpriv.info;daemon.info		%{$g['varlog_path']}/system.log
220 5b237745 Scott Ullrich
*.emerg						*
221
222
EOD;
223
224
		if (isset($syslogcfg['filter'])) {
225
			$syslogconf .= <<<EOD
226
local0.*					@{$syslogcfg['remoteserver']}
227
228
EOD;
229
		}
230 0f282d7a Scott Ullrich
231 5b237745 Scott Ullrich
		if (isset($syslogcfg['vpn'])) {
232
			$syslogconf .= <<<EOD
233
local3.*					@{$syslogcfg['remoteserver']}
234 3f2b92d2 Scott Ullrich
EOD;
235
		}
236
237 5b237745 Scott Ullrich
238 3f2b92d2 Scott Ullrich
		if (isset($syslogcfg['portalauth'])) {
239
			$syslogconf .= <<<EOD
240
local4.*					@{$syslogcfg['remoteserver']}
241 5b237745 Scott Ullrich
EOD;
242
		}
243
244 3f2b92d2 Scott Ullrich
245 5b237745 Scott Ullrich
		if (isset($syslogcfg['dhcp'])) {
246
			$syslogconf .= <<<EOD
247
local7.*					@{$syslogcfg['remoteserver']}
248
EOD;
249
		}
250
251
		if (isset($syslogcfg['system'])) {
252
			$syslogconf .= <<<EOD
253 a23d7248 Scott Ullrich
*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local7.none @{$syslogcfg['remoteserver']}
254 5b237745 Scott Ullrich
security.*					@{$syslogcfg['remoteserver']}
255
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
256
*.emerg						@{$syslogcfg['remoteserver']}
257
258
EOD;
259
		}
260
261
		fwrite($fd, $syslogconf);
262
		fclose($fd);
263 0f282d7a Scott Ullrich
264 5b237745 Scott Ullrich
		$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
265
266
	} else {
267
		$retval = mwexec("/usr/sbin/syslogd -ss");
268
	}
269 0f282d7a Scott Ullrich
270 5b237745 Scott Ullrich
	if ($g['booting'])
271
		echo "done\n";
272 0f282d7a Scott Ullrich
273 5b237745 Scott Ullrich
	return $retval;
274
}
275
276
function system_pccard_start() {
277
	global $config, $g;
278 0f282d7a Scott Ullrich
279 5b237745 Scott Ullrich
	if ($g['booting'])
280
		echo "Initializing PC cards... ";
281 0f282d7a Scott Ullrich
282 5b237745 Scott Ullrich
	/* kill any running pccardd */
283
	killbypid("{$g['varrun_path']}/pccardd.pid");
284 0f282d7a Scott Ullrich
285 5b237745 Scott Ullrich
	/* fire up pccardd */
286
	$res = mwexec("/usr/sbin/pccardd -z -f {$g['etc_path']}/pccard.conf");
287 0f282d7a Scott Ullrich
288 5b237745 Scott Ullrich
	if ($g['booting']) {
289
		if ($res == 0)
290
			echo "done\n";
291
		else
292
			echo "failed (probably no PC card controller present)\n";
293
	}
294 0f282d7a Scott Ullrich
295 5b237745 Scott Ullrich
	return $res;
296
}
297
298
function system_webgui_start() {
299
	global $config, $g;
300 0f282d7a Scott Ullrich
301 5b237745 Scott Ullrich
	if ($g['booting'])
302
		echo "Starting webGUI... ";
303 0f282d7a Scott Ullrich
304 5b237745 Scott Ullrich
	/* kill any running mini_httpd */
305
	killbypid("{$g['varrun_path']}/mini_httpd.pid");
306 0f282d7a Scott Ullrich
307 5b237745 Scott Ullrich
	/* generate password file */
308
	system_password_configure();
309 0f282d7a Scott Ullrich
310 5b237745 Scott Ullrich
	chdir($g['www_path']);
311 0f282d7a Scott Ullrich
312 5b237745 Scott Ullrich
	/* non-standard port? */
313
	if ($config['system']['webgui']['port'])
314
		$portarg = "-p {$config['system']['webgui']['port']}";
315
	else
316
		$portarg = "";
317 0f282d7a Scott Ullrich
318 5b237745 Scott Ullrich
	if ($config['system']['webgui']['protocol'] == "https") {
319 0f282d7a Scott Ullrich
320 5b237745 Scott Ullrich
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
321
			$cert = base64_decode($config['system']['webgui']['certificate']);
322
			$key = base64_decode($config['system']['webgui']['private-key']);
323
		} else {
324
			/* default certificate/key */
325
			$cert = <<<EOD
326
-----BEGIN CERTIFICATE-----
327 83a43b5f Scott Ullrich
MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJOQTEL
328
MAkGA1UECBMCTkExCzAJBgNVBAcTAk5BMQswCQYDVQQKEwJOQTELMAkGA1UECxMC
329
TkExCzAJBgNVBAMTAk5BMQswCQYDVQQGEwJVUzAeFw0wNTAzMDYwMDE1NDJaFw0x
330
NTAzMDQwMDE1NDJaMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UE
331
BxMCTkExCzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJ
332
BgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDF7luuy70OvHrl
333
xnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KTgz4iSD+pxEOxxlY+bCH6HTkAy5Sa
334
zt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVkH0fEvBf1xqU7wpkOiWkw1RmfEvZI
335
6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQABo4G2MIGzMB0GA1UdDgQWBBTgvk9F
336
alPK6/OcZrkaE8BhBrRo2DCBgwYDVR0jBHwweoAU4L5PRWpTyuvznGa5GhPAYQa0
337
aNihX6RdMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UEBxMCTkEx
338
CzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJBgNVBAYT
339
AlVTggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAv9+GXdYIWs2R
340
8B0zI4jAbHcaRsfohuzpNHD5re7ZK8H4fYbHIfmPY2UM3yOU7J2rLP8KGfKztay1
341
Z3RNW7SKJI/CagbdQOuYdMrlEyA4ZImM6NNzUbH6rNKtmDIDo1kHL3cXjzXEjBE+
342
ZZYTREFcdhtzUH5lYzJz1uVFeCSwozk=
343 5b237745 Scott Ullrich
-----END CERTIFICATE-----
344
EOD;
345
346
			$key = <<<EOD
347
-----BEGIN RSA PRIVATE KEY-----
348 83a43b5f Scott Ullrich
MIICXAIBAAKBgQDF7luuy70OvHrlxnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KT
349
gz4iSD+pxEOxxlY+bCH6HTkAy5Sazt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVk
350
H0fEvBf1xqU7wpkOiWkw1RmfEvZI6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQAB
351
AoGAF9dMJ9PWo+3EB+VNzUgTBI3Q+5JxgI7ibKLcg8TFtypW7jcRYB9Q3qRBNtuz
352
I7i2LrKrrQrUEOp0rej5BIwpwcjtEE2NsZwgYwDyywptoqt3WO86nPXYz2KhkQmP
353
YCDmPrff4vXCv6zgefb/AIgrOkgD3ViEoePhCAg+0l3fEIECQQD7C68Nb6KAWUND
354
Q9B0RxYrlgXikQ8yVHhlyM433APe/NCJ9kl5dLXpyjuvrWB+ml6TlLrcroLGejbd
355
tYXvIiyJAkEAydZVHqB4MpMtuY7VJoHNgl06YBoeTI+BJptPaOUNl4SlUKIYJMhX
356
oOXIGk9uDjfSNS7HvunZBjgz092GShWvmQJAQ8NhmwTZHj/58fwqFljh2R4DtKZn
357
LbSzUvYjA9z1holDWRoLtycTu2mFNuRbuZC9mqR40/ye/CgdCzdmUagt0QJBAKq1
358
00ySINd10Cive+yTwMPQIj2CGbpbbbq/hYyMntBWapQmZRFHOYZmkrZeFBGGeQ5u
359
QJdipiIyivNY2+nxKZECQCvumJPfZYxCeCAEC+G2xezrP6bC6FhzUOw6410UARTM
360
fuFjHpSfOiG62lfRdZgCPAr1L/1pJF+8RqjGlFfAuFA=
361 5b237745 Scott Ullrich
-----END RSA PRIVATE KEY-----
362
EOD;
363
		}
364 0f282d7a Scott Ullrich
365 5b237745 Scott Ullrich
		$fd = fopen("{$g['varetc_path']}/cert.pem", "w");
366
		if (!$fd) {
367
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
368
			return 1;
369
		}
370
		chmod("{$g['varetc_path']}/cert.pem", 0600);
371
		fwrite($fd, $cert);
372
		fwrite($fd, "\n");
373
		fwrite($fd, $key);
374
		fclose($fd);
375 0f282d7a Scott Ullrich
376 5b237745 Scott Ullrich
		$res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
377 c47f7673 Bill Marquette
			" -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
378 5b237745 Scott Ullrich
			" -i {$g['varrun_path']}/mini_httpd.pid");
379
	} else {
380
		$res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
381 c47f7673 Bill Marquette
			" -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
382 5b237745 Scott Ullrich
	}
383 0f282d7a Scott Ullrich
384 5b237745 Scott Ullrich
	if ($g['booting']) {
385
		if ($res == 0)
386
			echo "done\n";
387
		else
388
			echo "failed\n";
389
	}
390 0f282d7a Scott Ullrich
391 5b237745 Scott Ullrich
	return $res;
392
}
393
394
function system_password_configure() {
395
	global $config, $g;
396 0f282d7a Scott Ullrich
397 5b237745 Scott Ullrich
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
398
	if (!$fd) {
399
		printf("Error: cannot open htpasswd in system_password_configure().\n");
400
		return 1;
401
	}
402 0f282d7a Scott Ullrich
403 5b237745 Scott Ullrich
	if ($config['system']['username'])
404
		$username = $config['system']['username'];
405
	else
406
		$username = "admin";
407 0f282d7a Scott Ullrich
408 5b237745 Scott Ullrich
	fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
409
	fclose($fd);
410
	chmod("{$g['varrun_path']}/htpasswd", 0600);
411 0f282d7a Scott Ullrich
412 5b237745 Scott Ullrich
	return 0;
413
}
414
415
function system_timezone_configure() {
416
	global $config, $g;
417
418
	$syscfg = $config['system'];
419
420
	if ($g['booting'])
421
		echo "Initializing timezone... ";
422
423
	/* extract appropriate timezone file */
424
	$timezone = $syscfg['timezone'];
425
	if (!$timezone)
426
		$timezone = "Etc/UTC";
427 0f282d7a Scott Ullrich
428 34febcde Scott Ullrich
	conf_mount_rw();
429
430 0f282d7a Scott Ullrich
	exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
431 5b237745 Scott Ullrich
		escapeshellarg($timezone) . " > /etc/localtime");
432
433 27150275 Scott Ullrich
	conf_mount_ro();
434 34febcde Scott Ullrich
435 5b237745 Scott Ullrich
	if ($g['booting'])
436
		echo "done\n";
437
}
438
439
function system_ntp_configure() {
440
	global $config, $g;
441
442
	$syscfg = $config['system'];
443
444
	if ($g['booting'])
445
		echo "Starting NTP client... ";
446
	else {
447
		killbypid("{$g['varrun_path']}/runmsntp.pid");
448
		killbypid("{$g['varrun_path']}/msntp.pid");
449
	}
450
451
	/* start ntp client if needed - needs to be forced into background */
452
	$updateinterval = $syscfg['time-update-interval'];
453 0f282d7a Scott Ullrich
454 5b237745 Scott Ullrich
	if ($updateinterval > 0) {
455
		if ($updateinterval < 6)
456
			$updateinterval = 6;
457 0f282d7a Scott Ullrich
458 5b237745 Scott Ullrich
		$timeservers = "";
459
		foreach (explode(' ', $syscfg['timeservers']) as $ts)
460
			$timeservers .= " " . $ts;
461 0f282d7a Scott Ullrich
462 5b237745 Scott Ullrich
		mwexec_bg("/usr/local/bin/runmsntp.sh " .
463
			escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
464
			escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
465
			escapeshellarg($updateinterval) . " " .
466
			escapeshellarg($timeservers));
467
	}
468 0f282d7a Scott Ullrich
469 5b237745 Scott Ullrich
	if ($g['booting'])
470
		echo "done\n";
471
}
472
473 405e5de0 Scott Ullrich
function system_halt() {
474
	global $g;
475
476
	system_reboot_cleanup();
477
478
	mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
479
}
480
481 5b237745 Scott Ullrich
function system_reboot() {
482
	global $g;
483 0f282d7a Scott Ullrich
484 5b237745 Scott Ullrich
	system_reboot_cleanup();
485 0f282d7a Scott Ullrich
486 5b237745 Scott Ullrich
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
487
}
488
489
function system_reboot_sync() {
490
	global $g;
491 0f282d7a Scott Ullrich
492 5b237745 Scott Ullrich
	system_reboot_cleanup();
493 0f282d7a Scott Ullrich
494 5b237745 Scott Ullrich
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
495
}
496
497
function system_reboot_cleanup() {
498
	captiveportal_radius_stop_all();
499
}
500
501
function system_do_shell_commands($early = 0) {
502
	global $config, $g;
503 0f282d7a Scott Ullrich
504 5b237745 Scott Ullrich
	if ($early)
505
		$cmdn = "earlyshellcmd";
506
	else
507
		$cmdn = "shellcmd";
508 0f282d7a Scott Ullrich
509 5b237745 Scott Ullrich
	if (is_array($config['system'][$cmdn])) {
510 0f282d7a Scott Ullrich
511 5b237745 Scott Ullrich
		foreach ($config['system'][$cmdn] as $cmd) {
512
			exec($cmd);
513
		}
514
	}
515
}
516
517 a23d7248 Scott Ullrich
function system_do_extensions($early = false) {
518 5b237745 Scott Ullrich
	global $config, $g;
519 0f282d7a Scott Ullrich
520 5b237745 Scott Ullrich
	if (!is_dir("{$g['etc_path']}/inc/ext"))
521
		return;
522 0f282d7a Scott Ullrich
523 5b237745 Scott Ullrich
	$dh = @opendir("{$g['etc_path']}/inc/ext");
524
	if ($dh) {
525
		while (($extd = readdir($dh)) !== false) {
526
			if (($extd === ".") || ($extd === ".."))
527
				continue;
528 a23d7248 Scott Ullrich
			$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
529 5b237745 Scott Ullrich
			if (file_exists($rcfile))
530
				passthru($rcfile);
531
		}
532
		closedir($dh);
533
	}
534
}
535
536
function system_console_configure() {
537
	global $config, $g;
538 0f282d7a Scott Ullrich
539 5b237745 Scott Ullrich
	if (isset($config['system']['disableconsolemenu'])) {
540
		touch("{$g['varetc_path']}/disableconsole");
541
	} else {
542
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
543
	}
544
}
545
546
function system_dmesg_save() {
547
	global $g;
548 0f282d7a Scott Ullrich
549 5b237745 Scott Ullrich
	exec("/sbin/dmesg", $dmesg);
550 0f282d7a Scott Ullrich
551 5b237745 Scott Ullrich
	/* find last copyright line (output from previous boots may be present) */
552
	$lastcpline = 0;
553 0f282d7a Scott Ullrich
554 5b237745 Scott Ullrich
	for ($i = 0; $i < count($dmesg); $i++) {
555
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
556
			$lastcpline = $i;
557
	}
558 0f282d7a Scott Ullrich
559 5b237745 Scott Ullrich
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
560
	if (!$fd) {
561
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
562
		return 1;
563
	}
564 0f282d7a Scott Ullrich
565 5b237745 Scott Ullrich
	for ($i = $lastcpline; $i < count($dmesg); $i++)
566
		fwrite($fd, $dmesg[$i] . "\n");
567 0f282d7a Scott Ullrich
568 5b237745 Scott Ullrich
	fclose($fd);
569 0f282d7a Scott Ullrich
570 5b237745 Scott Ullrich
	return 0;
571
}
572
573
function system_set_harddisk_standby() {
574
	global $g, $config;
575
576
	if ($g['platform'] != "generic-pc")
577
		return;
578
579
	if (isset($config['system']['harddiskstandby'])) {
580
		if ($g['booting']) {
581
			echo 'Setting harddisk standby time... ';
582
		}
583
584
		$standby = $config['system']['harddiskstandby'];
585
		// Check for a numeric value
586
		if (is_numeric($standby)) {
587
			// Sync the disk(s)
588
			mwexec('/bin/sync');
589
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
590
				// Reinitialize ATA-drives
591
				mwexec('/usr/local/sbin/atareinit');
592
				if ($g['booting']) {
593
					echo "done\n";
594
				}
595
			} else if ($g['booting']) {
596
				echo "failed\n";
597
			}
598
		} else if ($g['booting']) {
599
			echo "failed\n";
600
		}
601
	}
602
}
603
604 3ff9d424 Scott Ullrich
function system_setup_sysctl() {
605
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
606
	foreach($sysctl as $sysc) {
607 89f7e23c Scott Ullrich
		if($sysc <> "")
608
			mwexec("sysctl {$sysc}");
609 3ff9d424 Scott Ullrich
	}
610
}
611
612 5b237745 Scott Ullrich
?>