Project

General

Profile

Download (14.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	system.inc
4
	part of m0n0wall (http://m0n0.ch/wall)
5

    
6
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
7
	All rights reserved.
8

    
9
	Redistribution and use in source and binary forms, with or without
10
	modification, are permitted provided that the following conditions are met:
11

    
12
	1. Redistributions of source code must retain the above copyright notice,
13
	   this list of conditions and the following disclaimer.
14

    
15
	2. Redistributions in binary form must reproduce the above copyright
16
	   notice, this list of conditions and the following disclaimer in the
17
	   documentation and/or other materials provided with the distribution.
18

    
19
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
20
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
21
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
22
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
	POSSIBILITY OF SUCH DAMAGE.
29
*/
30

    
31
/* include all configuration functions */
32
require_once("functions.inc");
33

    
34
function system_resolvconf_generate($dynupdate = false) {
35
	global $config, $g;
36

    
37
	$syscfg = $config['system'];
38

    
39
	$fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
40
	if (!$fd) {
41
		printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
42
		return 1;
43
	}
44

    
45
	$resolvconf = "domain {$syscfg['domain']}\n";
46

    
47
	$havedns = false;
48

    
49
	if (isset($syscfg['dnsallowoverride'])) {
50
		/* get dynamically assigned DNS servers (if any) */
51
		$nfd = @fopen("{$g['varetc_path']}/nameservers.conf", "r");
52
		if ($nfd) {
53
			while (!feof($nfd)) {
54
				$dnss = trim(fgets($nfd));
55
				if ($dnss) {
56
					$resolvconf .= "nameserver $dnss\n";
57
					$havedns = true;
58
				}
59
			}
60
			fclose($nfd);
61
		}
62
	}
63
	if (!$havedns && is_array($syscfg['dnsserver'])) {
64
		foreach ($syscfg['dnsserver'] as $ns) {
65
			if ($ns)
66
				$resolvconf .= "nameserver $ns\n";
67
			$havedns = true;
68
		}
69
	}
70

    
71
	fwrite($fd, $resolvconf);
72
	fclose($fd);
73

    
74
	if (!$g['booting']) {
75
		/* restart dhcpd (nameservers may have changed) */
76
		if (!$dynupdate)
77
			services_dhcpd_configure();
78
	}
79

    
80
	return 0;
81
}
82

    
83
function system_hosts_generate() {
84
	global $config, $g;
85

    
86
	$syscfg = $config['system'];
87
	$lancfg = $config['interfaces']['lan'];
88
	$dnsmasqcfg = $config['dnsmasq'];
89

    
90
	if (!is_array($dnsmasqcfg['hosts'])) {
91
		$dnsmasqcfg['hosts'] = array();
92
	}
93
	$hostscfg = $dnsmasqcfg['hosts'];
94

    
95
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
96
	if (!$fd) {
97
		printf("Error: cannot open hosts file in system_hosts_generate().\n");
98
		return 1;
99
	}
100

    
101
	$hosts = <<<EOD
102
127.0.0.1	localhost localhost.{$syscfg['domain']}
103
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
104

    
105
EOD;
106

    
107
	foreach ($hostscfg as $host) {
108
		if ($host['host'])
109
			$hosts .= "{$host['ip']}	{$host['host']}.{$host['domain']} {$host['host']}\n";
110
		else
111
			$hosts .= "{$host['ip']}	{$host['domain']}\n";
112
	}
113
	fwrite($fd, $hosts);
114
	fclose($fd);
115

    
116
	return 0;
117
}
118

    
119
function system_hostname_configure() {
120
	global $config, $g;
121

    
122
	$syscfg = $config['system'];
123

    
124
	/* set hostname */
125
	return mwexec("/bin/hostname " .
126
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
127
}
128

    
129
function system_routing_configure() {
130
	global $config, $g;
131

    
132
	/* Enable fast routing, if enabled */
133
	if(isset($config['staticroutes']['enablefastrouting']))
134
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
135

    
136
	/* clear out old routes, if necessary */
137
	if (file_exists("{$g['vardb_path']}/routes.db")) {
138
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
139
		if (!$fd) {
140
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
141
			return 1;
142
		}
143
		while (!feof($fd)) {
144
			$oldrt = fgets($fd);
145
			if ($oldrt)
146
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
147
		}
148
		fclose($fd);
149
		unlink("{$g['vardb_path']}/routes.db");
150
	}
151

    
152
	if (is_array($config['staticroutes']['route'])) {
153

    
154
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
155
		if (!$fd) {
156
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
157
			return 1;
158
		}
159

    
160
		foreach ($config['staticroutes']['route'] as $rtent) {
161
			mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
162
				" " . escapeshellarg($rtent['gateway']));
163

    
164
			/* record route so it can be easily removed later (if necessary) */
165
			fwrite($fd, $rtent['network'] . "\n");
166
		}
167

    
168
		fclose($fd);
169
	}
170

    
171
	return 0;
172
}
173

    
174
function system_routing_enable() {
175
	global $config, $g;
176

    
177
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
178
}
179

    
180
function system_syslogd_start() {
181
	global $config, $g;
182

    
183
	$syslogcfg = $config['syslog'];
184

    
185
	if ($g['booting'])
186
		echo "Starting syslog service... ";
187
	else
188
		killbypid("{$g['varrun_path']}/syslog.pid");
189

    
190
	if (isset($syslogcfg['enable'])) {
191

    
192
		/* write syslog.conf */
193
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
194
		if (!$fd) {
195
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
196
			return 1;
197
		}
198

    
199
		$syslogconf = <<<EOD
200
local0.*					%/var/log/filter.log
201
local3.*					%/var/log/vpn.log
202
local4.*					%/var/log/portalauth.log
203
local7.*					%/var/log/dhcpd.log
204
*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local4.none;local7.none %/var/log/system.log
205
security.*					%/var/log/system.log
206
auth.info;authpriv.info;daemon.info		%/var/log/system.log
207
*.emerg						*
208

    
209
EOD;
210

    
211
		if (isset($syslogcfg['filter'])) {
212
			$syslogconf .= <<<EOD
213
local0.*					@{$syslogcfg['remoteserver']}
214

    
215
EOD;
216
		}
217

    
218
		if (isset($syslogcfg['vpn'])) {
219
			$syslogconf .= <<<EOD
220
local3.*					@{$syslogcfg['remoteserver']}
221
EOD;
222
		}
223

    
224

    
225
		if (isset($syslogcfg['portalauth'])) {
226
			$syslogconf .= <<<EOD
227
local4.*					@{$syslogcfg['remoteserver']}
228
EOD;
229
		}
230

    
231

    
232
		if (isset($syslogcfg['dhcp'])) {
233
			$syslogconf .= <<<EOD
234
local7.*					@{$syslogcfg['remoteserver']}
235
EOD;
236
		}
237

    
238
		if (isset($syslogcfg['system'])) {
239
			$syslogconf .= <<<EOD
240
*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local7.none @{$syslogcfg['remoteserver']}
241
security.*					@{$syslogcfg['remoteserver']}
242
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
243
*.emerg						@{$syslogcfg['remoteserver']}
244

    
245
EOD;
246
		}
247

    
248
		fwrite($fd, $syslogconf);
249
		fclose($fd);
250

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

    
253
	} else {
254
		$retval = mwexec("/usr/sbin/syslogd -ss");
255
	}
256

    
257
	if ($g['booting'])
258
		echo "done\n";
259

    
260
	return $retval;
261
}
262

    
263
function system_pccard_start() {
264
	global $config, $g;
265

    
266
	if ($g['booting'])
267
		echo "Initializing PC cards... ";
268

    
269
	/* kill any running pccardd */
270
	killbypid("{$g['varrun_path']}/pccardd.pid");
271

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

    
275
	if ($g['booting']) {
276
		if ($res == 0)
277
			echo "done\n";
278
		else
279
			echo "failed (probably no PC card controller present)\n";
280
	}
281

    
282
	return $res;
283
}
284

    
285
function system_webgui_start() {
286
	global $config, $g;
287

    
288
	if ($g['booting'])
289
		echo "Starting webGUI... ";
290

    
291
	/* kill any running mini_httpd */
292
	killbypid("{$g['varrun_path']}/mini_httpd.pid");
293

    
294
	/* generate password file */
295
	system_password_configure();
296

    
297
	chdir($g['www_path']);
298

    
299
	/* non-standard port? */
300
	if ($config['system']['webgui']['port'])
301
		$portarg = "-p {$config['system']['webgui']['port']}";
302
	else
303
		$portarg = "";
304

    
305
	if ($config['system']['webgui']['protocol'] == "https") {
306

    
307
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
308
			$cert = base64_decode($config['system']['webgui']['certificate']);
309
			$key = base64_decode($config['system']['webgui']['private-key']);
310
		} else {
311
			/* default certificate/key */
312
			$cert = <<<EOD
313
-----BEGIN CERTIFICATE-----
314
MIIBlDCB/gIBADANBgkqhkiG9w0BAQQFADATMREwDwYDVQQKEwhtMG4wd2FsbDAe
315
Fw0wMzA5MDgxNzAzNDZaFw0wNDA5MDcxNzAzNDZaMBMxETAPBgNVBAoTCG0wbjB3
316
YWxsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAShszhFz+o8lsMWTGgTxs
317
TMPR+v4+qL5jXDyY97MLTGFK7aqQOtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+
318
83LPQmQoSPC0VqhfU3uYf3NzxiK8r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFP
319
C4jE2fvjkbzyVolPywBuewIDAQABMA0GCSqGSIb3DQEBBAUAA4GBAK2D8NqQSlUs
320
pFCe5J9ue1LrjfGHHy4HE9zA9avgrz3Qju+1JOshEwy/1BJjZ93tQUbiRS7RwvDO
321
4crGG4IejjhFczzA2CIX3rd2rYM2oGpojKgm5YuuhV5lYPwAHUOLbBaLOVqlLhzw
322
VqjD7R2DkXUIfhJ5ZekqK5ZwzqJXta8U
323
-----END CERTIFICATE-----
324

    
325
EOD;
326

    
327
			$key = <<<EOD
328
-----BEGIN RSA PRIVATE KEY-----
329
MIICXQIBAAKBgQDAShszhFz+o8lsMWTGgTxsTMPR+v4+qL5jXDyY97MLTGFK7aqQ
330
OtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+83LPQmQoSPC0VqhfU3uYf3NzxiK8
331
r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFPC4jE2fvjkbzyVolPywBuewIDAQAB
332
AoGAbJJrQW9fQrggJuLMz/hwsYW2m31oyOBmf5u463YQtjRuSuxe/gj87weZuNqY
333
H2rXq2k2K+ehl8hgW+egASyUL3L7kCkEAsVREujKTEyhSqqIRDPWTxo9S/YA9Gvn
334
2ZnJvkrcKjqCO9aHX3rvJOK/ErYI6akctgI3KmgkYw5XNmECQQDuZU97RTWH9rmP
335
aQr57ysNXxgFsyhetOOqeYkPtIVwpOiNbfwE1zi5RGdtO4Ku3fG1lV4J2UoWJ9yD
336
awdoyYIHAkEAzn0xJ90IjPsHk+8SODEj5JGdHSZPNu1tgtrbjEi9sfGWg4K7XTxr
337
QW90pWb1bKKU1uh5FzW6OhnFfuQXt1kC7QJAPSthqY+onKqCEnoxhtAHi/bKgyvl
338
P+fKQwPMV2tKkgy+XwvJjrRqqZ8TqsOKVLQ+QQmCh6RpjiXMPyxHSmvqIQJBAKLR
339
HF1ucDuaBROkwx0DwmWMW/KMLpIFDQDNSaiIAuu4rxHrl4mhBoGGPNffI04RtILw
340
s+qVNs5xW8T+XaT4ztECQQDFHPnZeoPWE5z+AX/UUQIUWaDExz3XRzmIxRbOrlFi
341
CsF1s0TdJLi/wzNQRAL37A8vqCeVFR/ng3Xpg96Yg+8Z
342
-----END RSA PRIVATE KEY-----
343

    
344
EOD;
345
		}
346

    
347
		$fd = fopen("{$g['varetc_path']}/cert.pem", "w");
348
		if (!$fd) {
349
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
350
			return 1;
351
		}
352
		chmod("{$g['varetc_path']}/cert.pem", 0600);
353
		fwrite($fd, $cert);
354
		fwrite($fd, "\n");
355
		fwrite($fd, $key);
356
		fclose($fd);
357

    
358
		$res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
359
			" -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
360
			" -i {$g['varrun_path']}/mini_httpd.pid");
361
	} else {
362
		$res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
363
			" -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
364
	}
365

    
366
	if ($g['booting']) {
367
		if ($res == 0)
368
			echo "done\n";
369
		else
370
			echo "failed\n";
371
	}
372

    
373
	return $res;
374
}
375

    
376
function system_password_configure() {
377
	global $config, $g;
378

    
379
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
380
	if (!$fd) {
381
		printf("Error: cannot open htpasswd in system_password_configure().\n");
382
		return 1;
383
	}
384

    
385
	if ($config['system']['username'])
386
		$username = $config['system']['username'];
387
	else
388
		$username = "admin";
389

    
390
	fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
391
	fclose($fd);
392
	chmod("{$g['varrun_path']}/htpasswd", 0600);
393

    
394
	return 0;
395
}
396

    
397
function system_timezone_configure() {
398
	global $config, $g;
399

    
400
	$syscfg = $config['system'];
401

    
402
	if ($g['booting'])
403
		echo "Initializing timezone... ";
404

    
405
	/* extract appropriate timezone file */
406
	$timezone = $syscfg['timezone'];
407
	if (!$timezone)
408
		$timezone = "Etc/UTC";
409

    
410
	exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
411
		escapeshellarg($timezone) . " > /etc/localtime");
412

    
413
	if ($g['booting'])
414
		echo "done\n";
415
}
416

    
417
function system_ntp_configure() {
418
	global $config, $g;
419

    
420
	$syscfg = $config['system'];
421

    
422
	if ($g['booting'])
423
		echo "Starting NTP client... ";
424
	else {
425
		killbypid("{$g['varrun_path']}/runmsntp.pid");
426
		killbypid("{$g['varrun_path']}/msntp.pid");
427
	}
428

    
429
	/* start ntp client if needed - needs to be forced into background */
430
	$updateinterval = $syscfg['time-update-interval'];
431

    
432
	if ($updateinterval > 0) {
433
		if ($updateinterval < 6)
434
			$updateinterval = 6;
435

    
436
		$timeservers = "";
437
		foreach (explode(' ', $syscfg['timeservers']) as $ts)
438
			$timeservers .= " " . $ts;
439

    
440
		mwexec_bg("/usr/local/bin/runmsntp.sh " .
441
			escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
442
			escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
443
			escapeshellarg($updateinterval) . " " .
444
			escapeshellarg($timeservers));
445
	}
446

    
447
	if ($g['booting'])
448
		echo "done\n";
449
}
450

    
451
function system_reboot() {
452
	global $g;
453

    
454
	system_reboot_cleanup();
455

    
456
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
457
}
458

    
459
function system_reboot_sync() {
460
	global $g;
461

    
462
	system_reboot_cleanup();
463

    
464
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
465
}
466

    
467
function system_reboot_cleanup() {
468
	captiveportal_radius_stop_all();
469
}
470

    
471
function system_do_shell_commands($early = 0) {
472
	global $config, $g;
473

    
474
	if ($early)
475
		$cmdn = "earlyshellcmd";
476
	else
477
		$cmdn = "shellcmd";
478

    
479
	if (is_array($config['system'][$cmdn])) {
480

    
481
		foreach ($config['system'][$cmdn] as $cmd) {
482
			exec($cmd);
483
		}
484
	}
485
}
486

    
487
function system_do_extensions($early = false) {
488
	global $config, $g;
489

    
490
	if (!is_dir("{$g['etc_path']}/inc/ext"))
491
		return;
492

    
493
	$dh = @opendir("{$g['etc_path']}/inc/ext");
494
	if ($dh) {
495
		while (($extd = readdir($dh)) !== false) {
496
			if (($extd === ".") || ($extd === ".."))
497
				continue;
498
			$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
499
			if (file_exists($rcfile))
500
				passthru($rcfile);
501
		}
502
		closedir($dh);
503
	}
504
}
505

    
506
function system_console_configure() {
507
	global $config, $g;
508

    
509
	if (isset($config['system']['disableconsolemenu'])) {
510
		touch("{$g['varetc_path']}/disableconsole");
511
	} else {
512
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
513
	}
514
}
515

    
516
function system_dmesg_save() {
517
	global $g;
518

    
519
	exec("/sbin/dmesg", $dmesg);
520

    
521
	/* find last copyright line (output from previous boots may be present) */
522
	$lastcpline = 0;
523

    
524
	for ($i = 0; $i < count($dmesg); $i++) {
525
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
526
			$lastcpline = $i;
527
	}
528

    
529
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
530
	if (!$fd) {
531
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
532
		return 1;
533
	}
534

    
535
	for ($i = $lastcpline; $i < count($dmesg); $i++)
536
		fwrite($fd, $dmesg[$i] . "\n");
537

    
538
	fclose($fd);
539

    
540
	return 0;
541
}
542

    
543
function system_set_harddisk_standby() {
544
	global $g, $config;
545

    
546
	if ($g['platform'] != "generic-pc")
547
		return;
548

    
549
	if (isset($config['system']['harddiskstandby'])) {
550
		if ($g['booting']) {
551
			echo 'Setting harddisk standby time... ';
552
		}
553

    
554
		$standby = $config['system']['harddiskstandby'];
555
		// Check for a numeric value
556
		if (is_numeric($standby)) {
557
			// Sync the disk(s)
558
			mwexec('/bin/sync');
559
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
560
				// Reinitialize ATA-drives
561
				mwexec('/usr/local/sbin/atareinit');
562
				if ($g['booting']) {
563
					echo "done\n";
564
				}
565
			} else if ($g['booting']) {
566
				echo "failed\n";
567
			}
568
		} else if ($g['booting']) {
569
			echo "failed\n";
570
		}
571
	}
572
}
573

    
574
?>
(9-9/12)