Project

General

Profile

Download (24.3 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 system_resolvconf_generate($dynupdate = false) {
36
        global $config, $g;
37

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

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

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

    
48
        $havedns = false;
49

    
50
        if (isset($syscfg['dnsallowoverride'])) {
51
                /* get dynamically assigned DNS servers (if any) */
52
                $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
                $nfd = @fopen("{$g['varetc_path']}/defaultdomain.conf", "r");
64
                if ($nfd and $havedns) {
65
                        while (!feof($nfd)) {
66
                                $dnss = trim(fgets($nfd));
67
                                if ($dnss) {
68
                                        $resolvconf .= "search $dnss\n";
69
                                }
70
                        }
71
                        fclose($nfd);
72
                }
73
        }
74
        if (!$havedns && is_array($syscfg['dnsserver'])) {
75
                foreach ($syscfg['dnsserver'] as $ns) {
76
                        if ($ns) {
77
				
78
                                $resolvconf .= "nameserver $ns\n";
79
				$havedns = true;
80
			}
81
                }
82
        }
83

    
84
	$ns = str_replace("nameserver nameserver ", "nameserver ", $resolvconf);
85

    
86
        fwrite($fd, $resolvconf);
87
        fclose($fd);
88

    
89
        if (!$g['booting']) {
90
                /* restart dhcpd (nameservers may have changed) */
91
                if (!$dynupdate)
92
                        services_dhcpd_configure();
93
        }
94

    
95
        return 0;
96
}
97

    
98
function system_hosts_generate() {
99
	global $config, $g;
100

    
101
	$syscfg = $config['system'];
102
	$lancfg = $config['interfaces']['lan'];
103
	$dnsmasqcfg = $config['dnsmasq'];
104

    
105
	if (!is_array($dnsmasqcfg['hosts'])) {
106
		$dnsmasqcfg['hosts'] = array();
107
	}
108
	$hostscfg = $dnsmasqcfg['hosts'];
109

    
110
	$fd = fopen("{$g['varetc_path']}/hosts", "w");
111
	if (!$fd) {
112
		printf("Error: cannot open hosts file in system_hosts_generate().\n");
113
		return 1;
114
	}
115

    
116
	$hosts = <<<EOD
117
127.0.0.1	localhost localhost.{$syscfg['domain']}
118
{$lancfg['ipaddr']}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
119

    
120
EOD;
121

    
122
	foreach ($hostscfg as $host) {
123
		if ($host['host'])
124
			$hosts .= "{$host['ip']}	{$host['host']}.{$host['domain']} {$host['host']}\n";
125
		else
126
			$hosts .= "{$host['ip']}	{$host['domain']}\n";
127
	}
128
	fwrite($fd, $hosts);
129
	fclose($fd);
130

    
131
	return 0;
132
}
133

    
134
function system_hostname_configure() {
135
	global $config, $g;
136

    
137
	$syscfg = $config['system'];
138

    
139
	/* set hostname */
140
	return mwexec("/bin/hostname " .
141
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
142
}
143

    
144
function system_routing_configure() {
145
	global $config, $g;
146

    
147
	/* Enable fast routing, if enabled */
148
	if(isset($config['staticroutes']['enablefastrouting']))
149
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
150

    
151
	/* clear out old routes, if necessary */
152
	if (file_exists("{$g['vardb_path']}/routes.db")) {
153
		$fd = fopen("{$g['vardb_path']}/routes.db", "r");
154
		if (!$fd) {
155
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
156
			return 1;
157
		}
158
		while (!feof($fd)) {
159
			$oldrt = fgets($fd);
160
			if ($oldrt)
161
				mwexec("/sbin/route delete " . escapeshellarg($oldrt));
162
		}
163
		fclose($fd);
164
		unlink("{$g['vardb_path']}/routes.db");
165
	}
166

    
167
	if (is_array($config['staticroutes']['route'])) {
168

    
169
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
170
		if (!$fd) {
171
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
172
			return 1;
173
		}
174

    
175
		foreach ($config['staticroutes']['route'] as $rtent) {
176
			mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
177
				" " . escapeshellarg($rtent['gateway']));
178

    
179
			/* record route so it can be easily removed later (if necessary) */
180
			fwrite($fd, $rtent['network'] . "\n");
181
		}
182

    
183
		fclose($fd);
184
	}
185

    
186
	return 0;
187
}
188

    
189
function system_routing_enable() {
190
	global $config, $g;
191

    
192
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
193
}
194

    
195
function system_syslogd_start() {
196
	global $config, $g;
197

    
198
	$syslogcfg = $config['syslog'];
199

    
200
	if ($g['booting'])
201
		echo "Starting syslog... ";
202
	else
203
		killbypid("{$g['varrun_path']}/syslog.pid");
204

    
205
	if (isset($syslogcfg)) {
206
		if($config['installedpackages']['package']) {
207
                        foreach($config['installedpackages']['package'] as $package) {
208
                                if($package['logging']) {	
209
					$pkgfacilities[] = $package['logging']['facilityname'];
210
					$facilitylist = implode(',', $pkgfacilities);
211
					mwexec("clog -i -s 10000 {$g['varlog_path']}/{$package['logging']['logfilename']}");
212
                                	$syslogconf .= "!{$package['logging']['facilityname']}\n*.*\t\t\t\t\t\t%{$g['varlog_path']}/{$package['logging']['logfilename']}\n!-{$facilitylist}\n";
213
				}
214
                        }
215
                }
216
		/* write syslog.conf */
217
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
218
		if (!$fd) {
219
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
220
			return 1;
221
		}
222
		if (!isset($syslogcfg['disablelocallogging'])) {
223
		$syslogconf .= <<<EOD
224
!racoon
225
*.*						%{$g['varlog_path']}/ipsec.log
226
!-racoon,{$facilitylist}
227
local0.*					%{$g['varlog_path']}/filter.log
228
local3.*					%{$g['varlog_path']}/vpn.log
229
local4.*					%{$g['varlog_path']}/portalauth.log
230
local7.*					%{$g['varlog_path']}/dhcpd.log
231
*.notice;kern.debug;lpr.info;mail.crit;		%{$g['varlog_path']}/system.log
232
news.err;local0.none;local3.none;local4.none;	%{$g['varlog_path']}/system.log
233
local7.none					%{$g['varlog_path']}/system.log
234
security.*					%{$g['varlog_path']}/system.log
235
auth.info;authpriv.info;daemon.info		%{$g['varlog_path']}/system.log
236
local1.*					%{$g['varlog_path']}/slbd.log
237
*.emerg						*
238

    
239
EOD;
240
		}
241

    
242
		if (isset($syslogcfg['filter'])) {
243
			$syslogconf .= <<<EOD
244
local0.*					@{$syslogcfg['remoteserver']}
245

    
246
EOD;
247
		}
248

    
249
		if (isset($syslogcfg['vpn'])) {
250
			$syslogconf .= <<<EOD
251
local3.*					@{$syslogcfg['remoteserver']}
252

    
253
EOD;
254
		}
255

    
256

    
257
		if (isset($syslogcfg['portalauth'])) {
258
			$syslogconf .= <<<EOD
259
local4.*					@{$syslogcfg['remoteserver']}
260

    
261
EOD;
262
		}
263

    
264

    
265
		if (isset($syslogcfg['dhcp'])) {
266
			$syslogconf .= <<<EOD
267
local7.*					@{$syslogcfg['remoteserver']}
268

    
269
EOD;
270
		}
271

    
272
		if (isset($syslogcfg['system'])) {
273
			$syslogconf .= <<<EOD
274
*.notice;kern.debug;lpr.info;mail.crit;		@{$syslogcfg['remoteserver']}
275
news.err;local0.none;local3.none;local7.none	@{$syslogcfg['remoteserver']}
276
security.*					@{$syslogcfg['remoteserver']}
277
auth.info;authpriv.info;daemon.info		@{$syslogcfg['remoteserver']}
278
*.emerg						@{$syslogcfg['remoteserver']}
279
EOD;
280
		}
281
		fwrite($fd, $syslogconf);
282
		fclose($fd);
283

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

    
286
	} else {
287
		$retval = mwexec("/usr/sbin/syslogd -ss");
288
	}
289

    
290
	if ($g['booting'])
291
		echo "done.\n";
292

    
293
	return $retval;
294
}
295

    
296
function system_pccard_start() {
297
	global $config, $g;
298

    
299
	if ($g['booting'])
300
		echo "Initializing PCMCIA... ";
301

    
302
	/* kill any running pccardd */
303
	killbypid("{$g['varrun_path']}/pccardd.pid");
304

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

    
308
	if ($g['booting']) {
309
		if ($res == 0)
310
			echo "done.\n";
311
		else
312
			echo "failed!\n";
313
	}
314

    
315
	return $res;
316
}
317

    
318
function system_webgui_start() {
319
	global $config, $g;
320

    
321
	if ($g['booting'])
322
		echo "Starting webConfigurator... ";
323

    
324
	/* kill any running mini_httpd */
325
	killbypid("{$g['varrun_path']}/lighty-webConfigurator.pid");
326

    
327
	/* generate password file */
328
	system_password_configure();
329

    
330
	chdir($g['www_path']);
331

    
332
	/* non-standard port? */
333
	if ($config['system']['webgui']['port'])
334
		$portarg = "-p {$config['system']['webgui']['port']}";
335
	else
336
		$portarg = "";
337

    
338
	if ($config['system']['webgui']['protocol'] == "https") {
339

    
340
		if ($config['system']['webgui']['certificate'] && $config['system']['webgui']['private-key']) {
341
			$cert = base64_decode($config['system']['webgui']['certificate']);
342
			$key = base64_decode($config['system']['webgui']['private-key']);
343
		} else {
344
			/* default certificate/key */
345
			$cert = <<<EOD
346
-----BEGIN CERTIFICATE-----
347
MIIC4zCCAkygAwIBAgIBADANBgkqhkiG9w0BAQQFADBbMQswCQYDVQQGEwJOQTEL
348
MAkGA1UECBMCTkExCzAJBgNVBAcTAk5BMQswCQYDVQQKEwJOQTELMAkGA1UECxMC
349
TkExCzAJBgNVBAMTAk5BMQswCQYDVQQGEwJVUzAeFw0wNTAzMDYwMDE1NDJaFw0x
350
NTAzMDQwMDE1NDJaMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UE
351
BxMCTkExCzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJ
352
BgNVBAYTAlVTMIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDF7luuy70OvHrl
353
xnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KTgz4iSD+pxEOxxlY+bCH6HTkAy5Sa
354
zt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVkH0fEvBf1xqU7wpkOiWkw1RmfEvZI
355
6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQABo4G2MIGzMB0GA1UdDgQWBBTgvk9F
356
alPK6/OcZrkaE8BhBrRo2DCBgwYDVR0jBHwweoAU4L5PRWpTyuvznGa5GhPAYQa0
357
aNihX6RdMFsxCzAJBgNVBAYTAk5BMQswCQYDVQQIEwJOQTELMAkGA1UEBxMCTkEx
358
CzAJBgNVBAoTAk5BMQswCQYDVQQLEwJOQTELMAkGA1UEAxMCTkExCzAJBgNVBAYT
359
AlVTggEAMAwGA1UdEwQFMAMBAf8wDQYJKoZIhvcNAQEEBQADgYEAv9+GXdYIWs2R
360
8B0zI4jAbHcaRsfohuzpNHD5re7ZK8H4fYbHIfmPY2UM3yOU7J2rLP8KGfKztay1
361
Z3RNW7SKJI/CagbdQOuYdMrlEyA4ZImM6NNzUbH6rNKtmDIDo1kHL3cXjzXEjBE+
362
ZZYTREFcdhtzUH5lYzJz1uVFeCSwozk=
363
-----END CERTIFICATE-----
364
EOD;
365

    
366
			$key = <<<EOD
367
-----BEGIN RSA PRIVATE KEY-----
368
MIICXAIBAAKBgQDF7luuy70OvHrlxnW9ID6srsfxEFCF4d9LmlZ6XdW1rEUHQ6KT
369
gz4iSD+pxEOxxlY+bCH6HTkAy5Sazt3eT7javvF+ILZgarwoY2x+NbDctd0VBJVk
370
H0fEvBf1xqU7wpkOiWkw1RmfEvZI6XnGi6VSjSmkm0UoQMKg9R7niRtE4QIDAQAB
371
AoGAF9dMJ9PWo+3EB+VNzUgTBI3Q+5JxgI7ibKLcg8TFtypW7jcRYB9Q3qRBNtuz
372
I7i2LrKrrQrUEOp0rej5BIwpwcjtEE2NsZwgYwDyywptoqt3WO86nPXYz2KhkQmP
373
YCDmPrff4vXCv6zgefb/AIgrOkgD3ViEoePhCAg+0l3fEIECQQD7C68Nb6KAWUND
374
Q9B0RxYrlgXikQ8yVHhlyM433APe/NCJ9kl5dLXpyjuvrWB+ml6TlLrcroLGejbd
375
tYXvIiyJAkEAydZVHqB4MpMtuY7VJoHNgl06YBoeTI+BJptPaOUNl4SlUKIYJMhX
376
oOXIGk9uDjfSNS7HvunZBjgz092GShWvmQJAQ8NhmwTZHj/58fwqFljh2R4DtKZn
377
LbSzUvYjA9z1holDWRoLtycTu2mFNuRbuZC9mqR40/ye/CgdCzdmUagt0QJBAKq1
378
00ySINd10Cive+yTwMPQIj2CGbpbbbq/hYyMntBWapQmZRFHOYZmkrZeFBGGeQ5u
379
QJdipiIyivNY2+nxKZECQCvumJPfZYxCeCAEC+G2xezrP6bC6FhzUOw6410UARTM
380
fuFjHpSfOiG62lfRdZgCPAr1L/1pJF+8RqjGlFfAuFA=
381
-----END RSA PRIVATE KEY-----
382
EOD;
383
		}
384
	} else {
385
		$cert = "";
386
		$key = "";
387
	}
388

    
389
	/* non-standard port? */
390
	if ($config['system']['webgui']['port'] <> "") {
391
		$port = $config['system']['webgui']['port'];
392
	} else {
393
		if($config['system']['webgui']['protocol'] == "https")
394
			$port = "443";
395
		elseif($config['system']['webgui']['protocol'] == "http")
396
			$port = "80";
397
		else
398
			$port = "80";
399
	}
400

    
401
	/* if we are strapped for memory, do not use fast cgi */
402
	$memory = get_memory();
403
	$avail_mem = $memory[1];
404
	if($avail < 120) 
405
		$use_fastcgi = false;
406
	else
407
		$use_fastcgi = true;
408

    
409
	/* generate lighttpd configuration */
410
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
411
		$key, $cert, "lighty-webConfigurator.pid", $port, "/usr/local/www/",
412
			"cert.pem", "1", "1", $use_fastcgi);
413

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

    
417
	if ($g['booting']) {
418
		if ($res == 0)
419
			echo "done.\n";
420
		else
421
			echo "failed!\n";
422
	}
423

    
424
	return $res;
425
}
426

    
427
function system_generate_lighty_config($filename, $cert, $key, $pid_file,
428
	$port, $document_root = "/usr/local/www/",
429
	$cert_location = "cert.pem", $max_procs,
430
	$max_requests = "1", $fast_cgi_enable = true) {
431
	
432
	if($port <> "")
433
		$lighty_port = $port;
434
	else
435
		$lighty_port = "80";
436
	
437
	if($fast_cgi_enable == true) {
438
		$module = "\"mod_fastcgi\"";
439
		$cgi_config = "";
440
		$fastcgi_config = <<<EOD
441
#### fastcgi module
442
## read fastcgi.txt for more info
443
fastcgi.server             = ( ".php" =>
444
                               ( "localhost" =>
445
                                 (
446
                                   "socket" => "/tmp/php-fastcgi.socket",
447
				   "min-procs" => 1,
448
				   "max-procs" => {$max_procs},
449
				   "max-load-per-proc" => 1,
450
				   "idle-timeout" => 1,
451
				   "bin-environment" => ( 
452
				      "PHP_FCGI_CHILDREN" => "{$max_procs}",
453
				      "PHP_FCGI_MAX_REQUESTS" => "{$max_requests}"
454
				   ),				   
455
                                   "bin-path" => "/usr/local/bin/php"
456
                                 )
457
                               )
458
                            )		
459

    
460
EOD;
461
	} else {
462
		$fastcgi_config = "";
463
		$module = "\"mod_cgi\"";
464
		$cgi_config = <<<EOD
465
#### CGI module
466
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
467
                               ".cgi" => "/usr/bin/perl" )
468
		
469
EOD;
470
	}
471
	
472
	$lighty_config .= <<<EOD
473
#
474
# lighttpd configuration file
475
#
476
# use a it as base for lighttpd 1.0.0 and above
477
#
478
############ Options you really have to take care of ####################
479

    
480
## modules to load
481
server.modules              =   (
482
				  "mod_access",
483
                                  {$module}
484
				)
485

    
486
## Unused modules
487
#                               "mod_setenv",
488
#                               "mod_compress"
489
#				"mod_redirect",
490
#                               "mod_rewrite",
491
#                               "mod_ssi",
492
#                               "mod_usertrack",
493
#                               "mod_expire",
494
#                               "mod_secdownload",
495
#                               "mod_rrdtool",
496
#                               "mod_auth",
497
#                               "mod_status",
498
#                               "mod_alias",
499
#                               "mod_proxy",
500
#                               "mod_simple_vhost",
501
#                               "mod_evhost",
502
#                               "mod_userdir",
503
#                               "mod_cgi",
504
#                                "mod_accesslog"
505

    
506
## a static document-root, for virtual-hosting take look at the
507
## server.virtual-* options
508
server.document-root        = "{$document_root}"
509

    
510
## where to send error-messages to
511
#server.errorlog             = "/var/log/lighttpd.error.log"
512

    
513
# files to check for if .../ is requested
514
server.indexfiles           = ( "index.php", "index.html",
515
                                "index.htm", "default.htm" )
516

    
517
# mimetype mapping
518
mimetype.assign             = (
519
  ".pdf"          =>      "application/pdf",
520
  ".sig"          =>      "application/pgp-signature",
521
  ".spl"          =>      "application/futuresplash",
522
  ".class"        =>      "application/octet-stream",
523
  ".ps"           =>      "application/postscript",
524
  ".torrent"      =>      "application/x-bittorrent",
525
  ".dvi"          =>      "application/x-dvi",
526
  ".gz"           =>      "application/x-gzip",
527
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
528
  ".swf"          =>      "application/x-shockwave-flash",
529
  ".tar.gz"       =>      "application/x-tgz",
530
  ".tgz"          =>      "application/x-tgz",
531
  ".tar"          =>      "application/x-tar",
532
  ".zip"          =>      "application/zip",
533
  ".mp3"          =>      "audio/mpeg",
534
  ".m3u"          =>      "audio/x-mpegurl",
535
  ".wma"          =>      "audio/x-ms-wma",
536
  ".wax"          =>      "audio/x-ms-wax",
537
  ".ogg"          =>      "audio/x-wav",
538
  ".wav"          =>      "audio/x-wav",
539
  ".gif"          =>      "image/gif",
540
  ".jpg"          =>      "image/jpeg",
541
  ".jpeg"         =>      "image/jpeg",
542
  ".png"          =>      "image/png",
543
  ".xbm"          =>      "image/x-xbitmap",
544
  ".xpm"          =>      "image/x-xpixmap",
545
  ".xwd"          =>      "image/x-xwindowdump",
546
  ".css"          =>      "text/css",
547
  ".html"         =>      "text/html",
548
  ".htm"          =>      "text/html",
549
  ".js"           =>      "text/javascript",
550
  ".asc"          =>      "text/plain",
551
  ".c"            =>      "text/plain",
552
  ".conf"         =>      "text/plain",
553
  ".text"         =>      "text/plain",
554
  ".txt"          =>      "text/plain",
555
  ".dtd"          =>      "text/xml",
556
  ".xml"          =>      "text/xml",
557
  ".mpeg"         =>      "video/mpeg",
558
  ".mpg"          =>      "video/mpeg",
559
  ".mov"          =>      "video/quicktime",
560
  ".qt"           =>      "video/quicktime",
561
  ".avi"          =>      "video/x-msvideo",
562
  ".asf"          =>      "video/x-ms-asf",
563
  ".asx"          =>      "video/x-ms-asf",
564
  ".wmv"          =>      "video/x-ms-wmv",
565
  ".bz2"          =>      "application/x-bzip",
566
  ".tbz"          =>      "application/x-bzip-compressed-tar",
567
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
568
 )
569

    
570
# Use the "Content-Type" extended attribute to obtain mime type if possible
571
#mimetypes.use-xattr        = "enable"
572

    
573
#### accesslog module
574
#accesslog.filename          = "/dev/null"
575

    
576
## deny access the file-extensions
577
#
578
# ~    is for backupfiles from vi, emacs, joe, ...
579
# .inc is often used for code includes which should in general not be part
580
#      of the document-root
581
url.access-deny             = ( "~", ".inc" )
582

    
583

    
584
######### Options that are good to be but not neccesary to be changed #######
585

    
586
## bind to port (default: 80)
587
server.port                = {$lighty_port}
588

    
589
## error-handler for status 404
590
#server.error-handler-404   = "/error-handler.html"
591
#server.error-handler-404   = "/error-handler.php"
592

    
593
## to help the rc.scripts
594
server.pid-file            = "/var/run/{$pid_file}"
595

    
596
## virtual directory listings
597
server.dir-listing         = "disable"
598

    
599
## enable debugging
600
debug.log-request-header   = "disable"
601
debug.log-response-header  = "disable"
602
debug.log-request-handling = "disable"
603
debug.log-file-not-found   = "disable"
604

    
605
#### compress module
606
#compress.cache-dir         = "/tmp/lighttpd/cache/compress/"
607
#compress.filetype          = ("text/plain", "text/html")
608

    
609
{$fastcgi_config}
610

    
611
{$cgi_config}
612

    
613
EOD;
614

    
615
	if($cert <> "" and $key <> "") {
616
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
617
		if (!$fd) {
618
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
619
			return 1;
620
		}
621
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
622
		fwrite($fd, $cert);
623
		fwrite($fd, "\n");
624
		fwrite($fd, $key);
625
		fclose($fd);
626
		$lighty_config .= "\n";
627
		$lighty_config .= "## ssl configuration\n";
628
		$lighty_config .= "ssl.engine = \"enable\"\n";
629
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_location}\"\n\n";	
630
	}
631

    
632
	$fd = fopen("{$g['varetc_path']}/{$filename}", "w");
633
	if (!$fd) {
634
		printf("Error: cannot open {$g['varetc_path']}/{$filename} in system_generate_lighty_config().\n");
635
		return 1;
636
	}
637
	fwrite($fd, $lighty_config);
638
	fclose($fd);
639

    
640
	return 0;
641

    
642
}
643

    
644
function system_password_configure() {
645
	global $config, $g;
646

    
647
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
648
	if (!$fd) {
649
		printf("Error: cannot open htpasswd in system_password_configure().\n");
650
		return 1;
651
	}
652

    
653
	if ($config['system']['username'])
654
		$username = $config['system']['username'];
655
	else
656
		$username = "admin";
657

    
658
	fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
659
	fclose($fd);
660
	chmod("{$g['varrun_path']}/htpasswd", 0600);
661

    
662
	return 0;
663
}
664

    
665
function system_timezone_configure() {
666
	global $config, $g;
667

    
668
	$syscfg = $config['system'];
669

    
670
	if ($g['booting'])
671
		echo "Setting timezone... ";
672

    
673
	/* extract appropriate timezone file */
674
	$timezone = $syscfg['timezone'];
675
	if (!$timezone)
676
		$timezone = "Etc/UTC";
677

    
678
	conf_mount_rw();
679

    
680
	exec("/usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
681
		escapeshellarg($timezone) . " > /etc/localtime");
682

    
683
	conf_mount_ro();
684

    
685
	if ($g['booting'])
686
		echo "done.\n";
687
}
688

    
689
function system_ntp_configure() {
690
	global $config, $g;
691

    
692
	$syscfg = $config['system'];
693

    
694
	if ($g['booting'])
695
		echo "Starting NTP client... ";
696
	else {
697
		killbypid("{$g['varrun_path']}/runmsntp.pid");
698
		killbypid("{$g['varrun_path']}/msntp.pid");
699
	}
700

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

    
704
	if ($updateinterval > 0) {
705
		if ($updateinterval < 6)
706
			$updateinterval = 6;
707

    
708
		$timeservers = "";
709
		foreach (explode(' ', $syscfg['timeservers']) as $ts)
710
			$timeservers .= " " . $ts;
711

    
712
		mwexec_bg("/usr/local/bin/runmsntp.sh " .
713
			escapeshellarg("{$g['varrun_path']}/runmsntp.pid") . " " .
714
			escapeshellarg("{$g['varrun_path']}/msntp.pid") . " " .
715
			escapeshellarg($updateinterval) . " " .
716
			escapeshellarg($timeservers));
717
	}
718

    
719
	if ($g['booting'])
720
		echo "done.\n";
721
}
722

    
723
function system_halt() {
724
	global $g;
725

    
726
	system_reboot_cleanup();
727

    
728
	mwexec("nohup /etc/rc.halt > /dev/null 2>&1 &");
729
}
730

    
731
function system_reboot() {
732
	global $g;
733

    
734
	system_reboot_cleanup();
735

    
736
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
737
}
738

    
739
function system_reboot_sync() {
740
	global $g;
741

    
742
	system_reboot_cleanup();
743

    
744
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
745
}
746

    
747
function system_reboot_cleanup() {
748
	captiveportal_radius_stop_all();
749
}
750

    
751
function system_do_shell_commands($early = 0) {
752
	global $config, $g;
753

    
754
	if ($early)
755
		$cmdn = "earlyshellcmd";
756
	else
757
		$cmdn = "shellcmd";
758

    
759
	if (is_array($config['system'][$cmdn])) {
760

    
761
		foreach ($config['system'][$cmdn] as $cmd) {
762
			exec($cmd);
763
		}
764
	}
765
}
766

    
767
function system_do_extensions($early = false) {
768
	global $config, $g;
769

    
770
	if (!is_dir("{$g['etc_path']}/inc/ext"))
771
		return;
772

    
773
	$dh = @opendir("{$g['etc_path']}/inc/ext");
774
	if ($dh) {
775
		while (($extd = readdir($dh)) !== false) {
776
			if (($extd === ".") || ($extd === ".."))
777
				continue;
778
			$rcfile = "{$g['etc_path']}/inc/ext/" . $extd . "/" . ($early ? "rc.early" : "rc");
779
			if (file_exists($rcfile))
780
				passthru($rcfile);
781
		}
782
		closedir($dh);
783
	}
784
}
785

    
786
function system_console_configure() {
787
	global $config, $g;
788

    
789
	if (isset($config['system']['disableconsolemenu'])) {
790
		touch("{$g['varetc_path']}/disableconsole");
791
	} else {
792
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
793
	}
794
}
795

    
796
function system_dmesg_save() {
797
	global $g;
798

    
799
	exec("/sbin/dmesg", $dmesg);
800

    
801
	/* find last copyright line (output from previous boots may be present) */
802
	$lastcpline = 0;
803

    
804
	for ($i = 0; $i < count($dmesg); $i++) {
805
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
806
			$lastcpline = $i;
807
	}
808

    
809
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
810
	if (!$fd) {
811
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
812
		return 1;
813
	}
814

    
815
	for ($i = $lastcpline; $i < count($dmesg); $i++)
816
		fwrite($fd, $dmesg[$i] . "\n");
817

    
818
	fclose($fd);
819

    
820
	return 0;
821
}
822

    
823
function system_set_harddisk_standby() {
824
	global $g, $config;
825

    
826
	if (isset($config['system']['harddiskstandby'])) {
827
		if ($g['booting']) {
828
			echo 'Setting hard disk standby... ';
829
		}
830

    
831
		$standby = $config['system']['harddiskstandby'];
832
		// Check for a numeric value
833
		if (is_numeric($standby)) {
834
			// Sync the disk(s)
835
			mwexec('/bin/sync');
836
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
837
				// Reinitialize ATA-drives
838
				mwexec('/usr/local/sbin/atareinit');
839
				if ($g['booting']) {
840
					echo "done.\n";
841
				}
842
			} else if ($g['booting']) {
843
				echo "failed!\n";
844
			}
845
		} else if ($g['booting']) {
846
			echo "failed!\n";
847
		}
848
	}
849
}
850

    
851
function system_setup_sysctl() {
852
	global $config;
853

    
854
	$sysctl = return_filename_as_array("/etc/sysctl.conf");
855
	foreach($sysctl as $sysc) {
856
		if($sysc <> "")
857
			mwexec("sysctl {$sysc}");
858
	}
859
	if (isset($config['system']['sharednet'])) {
860
		system_disable_arp_wrong_if();
861
	}
862
}
863

    
864
function system_disable_arp_wrong_if() {
865
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
866
}
867

    
868
function system_enable_arp_wrong_if() {
869
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=1");
870
}
871

    
872

    
873
?>
(16-16/23)