Project

General

Profile

Download (14 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
	/* clear out old routes, if necessary */
133
	if (file_exists("{$g['vardb_path']}/routes.db")) {
134
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
135
		if (!$fd) {
136
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
137
			return 1;		
138
		}
139
		while (!feof($fd)) {
140
			$oldrt = fgets($fd);
141
			if ($oldrt)
142
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
143
		}
144
		fclose($fd);
145
		unlink("{$g['vardb_path']}/routes.db");
146
	}
147
	
148
	if (is_array($config['staticroutes']['route'])) {
149
		
150
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
151
		if (!$fd) {
152
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
153
			return 1;		
154
		}
155
		
156
		foreach ($config['staticroutes']['route'] as $rtent) {
157
			mwexec("/sbin/route add " . escapeshellarg($rtent['network']) . 
158
				" " . escapeshellarg($rtent['gateway']));
159
			
160
			/* record route so it can be easily removed later (if necessary) */
161
			fwrite($fd, $rtent['network'] . "\n");
162
		}
163
		
164
		fclose($fd); 
165
	}
166
	
167
	return 0;
168
}
169

    
170
function system_routing_enable() {
171
	global $config, $g;
172
	
173
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
174
}
175

    
176
function system_syslogd_start() {
177
	global $config, $g;
178
	
179
	$syslogcfg = $config['syslog'];
180

    
181
	if ($g['booting']) 
182
		echo "Starting syslog service... ";
183
	else
184
		killbypid("{$g['varrun_path']}/syslog.pid");
185
			
186
	if (isset($syslogcfg['enable'])) {
187

    
188
		/* write syslog.conf */
189
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
190
		if (!$fd) {
191
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
192
			return 1;
193
		}
194
		
195
		$syslogconf = <<<EOD
196
local0.*					%/var/log/filter.log
197
local3.*					%/var/log/vpn.log
198
local4.*					%/var/log/portalauth.log
199
local7.*					%/var/log/dhcpd.log
200
*.notice;kern.debug;lpr.info;mail.crit;news.err;local0.none;local3.none;local4.none;local7.none %/var/log/system.log
201
security.*					%/var/log/system.log
202
auth.info;authpriv.info;daemon.info		%/var/log/system.log
203
*.emerg						*
204

    
205
EOD;
206

    
207
		if (isset($syslogcfg['filter'])) {
208
			$syslogconf .= <<<EOD
209
local0.*					@{$syslogcfg['remoteserver']}
210

    
211
EOD;
212
		}
213
		
214
		if (isset($syslogcfg['vpn'])) {
215
			$syslogconf .= <<<EOD
216
local3.*					@{$syslogcfg['remoteserver']}
217
EOD;
218
		}
219

    
220

    
221
		if (isset($syslogcfg['portalauth'])) {
222
			$syslogconf .= <<<EOD
223
local4.*					@{$syslogcfg['remoteserver']}
224
EOD;
225
		}
226

    
227

    
228
		if (isset($syslogcfg['dhcp'])) {
229
			$syslogconf .= <<<EOD
230
local7.*					@{$syslogcfg['remoteserver']}
231
EOD;
232
		}
233

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

    
241
EOD;
242
		}
243

    
244
		fwrite($fd, $syslogconf);
245
		fclose($fd);
246
		
247
		$retval = mwexec("/usr/sbin/syslogd -s -f {$g['varetc_path']}/syslog.conf");
248

    
249
	} else {
250
		$retval = mwexec("/usr/sbin/syslogd -ss");
251
	}
252
	
253
	if ($g['booting'])
254
		echo "done\n";
255
		
256
	return $retval;
257
}
258

    
259
function system_pccard_start() {
260
	global $config, $g;
261
	
262
	if ($g['booting'])
263
		echo "Initializing PC cards... ";
264
	
265
	/* kill any running pccardd */
266
	killbypid("{$g['varrun_path']}/pccardd.pid");
267
	
268
	/* fire up pccardd */
269
	$res = mwexec("/usr/sbin/pccardd -z -f {$g['etc_path']}/pccard.conf");
270
	
271
	if ($g['booting']) {
272
		if ($res == 0)
273
			echo "done\n";
274
		else
275
			echo "failed (probably no PC card controller present)\n";
276
	}
277
		
278
	return $res;
279
}
280

    
281
function system_webgui_start() {
282
	global $config, $g;
283
	
284
	if ($g['booting'])
285
		echo "Starting webGUI... ";
286
	
287
	/* kill any running mini_httpd */
288
	killbypid("{$g['varrun_path']}/mini_httpd.pid");
289
	
290
	/* generate password file */
291
	system_password_configure();
292
	
293
	chdir($g['www_path']);
294
	
295
	/* non-standard port? */
296
	if ($config['system']['webgui']['port'])
297
		$portarg = "-p {$config['system']['webgui']['port']}";
298
	else
299
		$portarg = "";
300
	
301
	if ($config['system']['webgui']['protocol'] == "https") {
302
	
303
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
304
			$cert = base64_decode($config['system']['webgui']['certificate']);
305
			$key = base64_decode($config['system']['webgui']['private-key']);
306
		} else {
307
			/* default certificate/key */
308
			$cert = <<<EOD
309
-----BEGIN CERTIFICATE-----
310
MIIBlDCB/gIBADANBgkqhkiG9w0BAQQFADATMREwDwYDVQQKEwhtMG4wd2FsbDAe
311
Fw0wMzA5MDgxNzAzNDZaFw0wNDA5MDcxNzAzNDZaMBMxETAPBgNVBAoTCG0wbjB3
312
YWxsMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDAShszhFz+o8lsMWTGgTxs
313
TMPR+v4+qL5jXDyY97MLTGFK7aqQOtpIQc+TcTc4jklgOVlHoR7oBXrsi8YrbCd+
314
83LPQmQoSPC0VqhfU3uYf3NzxiK8r97aPCsmWgwT2pQ6TcESTm6sF7nLprOf/zFP
315
C4jE2fvjkbzyVolPywBuewIDAQABMA0GCSqGSIb3DQEBBAUAA4GBAK2D8NqQSlUs
316
pFCe5J9ue1LrjfGHHy4HE9zA9avgrz3Qju+1JOshEwy/1BJjZ93tQUbiRS7RwvDO
317
4crGG4IejjhFczzA2CIX3rd2rYM2oGpojKgm5YuuhV5lYPwAHUOLbBaLOVqlLhzw
318
VqjD7R2DkXUIfhJ5ZekqK5ZwzqJXta8U
319
-----END CERTIFICATE-----
320

    
321
EOD;
322

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

    
340
EOD;
341
		}
342
		
343
		$fd = fopen("{$g['varetc_path']}/cert.pem", "w");
344
		if (!$fd) {
345
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
346
			return 1;
347
		}
348
		chmod("{$g['varetc_path']}/cert.pem", 0600);
349
		fwrite($fd, $cert);
350
		fwrite($fd, "\n");
351
		fwrite($fd, $key);
352
		fclose($fd);
353
	
354
		$res = mwexec("/usr/local/sbin/mini_httpd -S -E {$g['varetc_path']}/cert.pem" .
355
			" -c \"**.php|**.cgi\" -u root -maxproc 16 $portarg" .
356
			" -i {$g['varrun_path']}/mini_httpd.pid");
357
	} else {
358
		$res = mwexec("/usr/local/sbin/mini_httpd -c \"**.php|**.cgi\" -u root" .
359
			" -maxproc 16 $portarg -i {$g['varrun_path']}/mini_httpd.pid");
360
	}
361
	
362
	if ($g['booting']) {
363
		if ($res == 0)
364
			echo "done\n";
365
		else
366
			echo "failed\n";
367
	}
368
	
369
	return $res;
370
}
371

    
372
function system_password_configure() {
373
	global $config, $g;
374
	
375
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
376
	if (!$fd) {
377
		printf("Error: cannot open htpasswd in system_password_configure().\n");
378
		return 1;
379
	}
380
	
381
	if ($config['system']['username'])
382
		$username = $config['system']['username'];
383
	else
384
		$username = "admin";
385
	
386
	fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
387
	fclose($fd);
388
	chmod("{$g['varrun_path']}/htpasswd", 0600);
389
	
390
	return 0;
391
}
392

    
393
function system_timezone_configure() {
394
	global $config, $g;
395

    
396
	$syscfg = $config['system'];
397

    
398
	if ($g['booting'])
399
		echo "Initializing timezone... ";
400

    
401
	/* extract appropriate timezone file */
402
	$timezone = $syscfg['timezone'];
403
	if (!$timezone)
404
		$timezone = "Etc/UTC";
405
		
406
	exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " . 
407
		escapeshellarg($timezone) . " > /etc/localtime");
408

    
409
	if ($g['booting'])
410
		echo "done\n";
411
}
412

    
413
function system_ntp_configure() {
414
	global $config, $g;
415

    
416
	$syscfg = $config['system'];
417

    
418
	if ($g['booting'])
419
		echo "Starting NTP client... ";
420
	else {
421
		killbypid("{$g['varrun_path']}/runmsntp.pid");
422
		killbypid("{$g['varrun_path']}/msntp.pid");
423
	}
424

    
425
	/* start ntp client if needed - needs to be forced into background */
426
	$updateinterval = $syscfg['time-update-interval'];
427
	
428
	if ($updateinterval > 0) {
429
		if ($updateinterval < 6)
430
			$updateinterval = 6;
431
		
432
		$timeservers = "";
433
		foreach (explode(' ', $syscfg['timeservers']) as $ts)
434
			$timeservers .= " " . $ts;
435
		
436
		mwexec_bg("/usr/local/bin/runmsntp.sh " .
437
			escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
438
			escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
439
			escapeshellarg($updateinterval) . " " .
440
			escapeshellarg($timeservers));
441
	}
442
		
443
	if ($g['booting'])
444
		echo "done\n";
445
}
446

    
447
function system_reboot() {
448
	global $g;
449
	
450
	system_reboot_cleanup();
451
	
452
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
453
}
454

    
455
function system_reboot_sync() {
456
	global $g;
457
	
458
	system_reboot_cleanup();
459
	
460
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
461
}
462

    
463
function system_reboot_cleanup() {
464
	captiveportal_radius_stop_all();
465
}
466

    
467
function system_do_shell_commands($early = 0) {
468
	global $config, $g;
469
	
470
	if ($early)
471
		$cmdn = "earlyshellcmd";
472
	else
473
		$cmdn = "shellcmd";
474
	
475
	if (is_array($config['system'][$cmdn])) {
476
		
477
		foreach ($config['system'][$cmdn] as $cmd) {
478
			exec($cmd);
479
		}
480
	}
481
}
482

    
483
function system_do_extensions() {
484
	global $config, $g;
485
	
486
	if (!is_dir("{$g['etc_path']}/inc/ext"))
487
		return;
488
	
489
	$dh = @opendir("{$g['etc_path']}/inc/ext");
490
	if ($dh) {
491
		while (($extd = readdir($dh)) !== false) {
492
			if (($extd === ".") || ($extd === ".."))
493
				continue;
494
			$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/rc";
495
			if (file_exists($rcfile))
496
				passthru($rcfile);
497
		}
498
		closedir($dh);
499
	}
500
}
501

    
502
function system_console_configure() {
503
	global $config, $g;
504
	
505
	if (isset($config['system']['disableconsolemenu'])) {
506
		touch("{$g['varetc_path']}/disableconsole");
507
	} else {
508
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
509
	}
510
}
511

    
512
function system_dmesg_save() {
513
	global $g;
514
	
515
	exec("/sbin/dmesg", $dmesg);
516
	
517
	/* find last copyright line (output from previous boots may be present) */
518
	$lastcpline = 0;
519
	
520
	for ($i = 0; $i < count($dmesg); $i++) {
521
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
522
			$lastcpline = $i;
523
	}
524
	
525
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
526
	if (!$fd) {
527
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
528
		return 1;
529
	}
530
	
531
	for ($i = $lastcpline; $i < count($dmesg); $i++)
532
		fwrite($fd, $dmesg[$i] . "\n");
533
	
534
	fclose($fd);
535
	
536
	return 0;
537
}
538

    
539
function system_set_harddisk_standby() {
540
	global $g, $config;
541

    
542
	if ($g['platform'] != "generic-pc")
543
		return;
544

    
545
	if (isset($config['system']['harddiskstandby'])) {
546
		if ($g['booting']) {
547
			echo 'Setting harddisk standby time... ';
548
		}
549

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

    
570
?>
(9-9/12)