Project

General

Profile

Download (38.4 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
/*
33
	pfSense_BUILDER_BINARIES:	/usr/sbin/powerd	/usr/bin/killall	/sbin/sysctl	/sbin/route
34
	pfSense_BUILDER_BINARIES:	/bin/hostname	/bin/ls	/usr/bin/netstat	/usr/sbin/syslogd	
35
	pfSense_BUILDER_BINARIES:	/usr/sbin/pccardd	/usr/local/sbin/lighttpd	/bin/chmod 	/bin/mkdir
36
	pfSense_BUILDER_BINARIES:	/usr/bin/tar	/bin/sync	/usr/local/sbin/ntpd	/usr/sbin/ntpdate
37
	pfSense_BUILDER_BINARIES:	/usr/bin/nohup	/sbin/dmesg	/usr/local/sbin/atareinit
38
	pfSense_MODULE:	utils
39
*/
40

    
41
function activate_powerd() {
42
	global $config, $g;
43
	if(isset($config['system']['powerd_enable'])) {
44
		exec("/usr/sbin/powerd -b adp -a adp");
45
	} else {
46
		if(is_process_running("powerd"))
47
			exec("/usr/bin/killall powerd");
48
	}
49
}
50

    
51
function activate_sysctls() {
52
	global $config, $g;
53
	require("sysctl.inc");
54
	exec("/sbin/sysctl net.enc.out.ipsec_bpf_mask=0x00000001");
55
	exec("/sbin/sysctl net.enc.out.ipsec_filter_mask=0x00000001");
56
	exec("/sbin/sysctl net.enc.in.ipsec_bpf_mask=0x00000002");
57
	exec("/sbin/sysctl net.enc.in.ipsec_filter_mask=0x00000002");
58

    
59
	if (is_array($config['sysctl'])) {
60
		foreach ($config['sysctl']['item'] as $tunable) {
61
			if($tunable['value'] == "default")
62
				$tunable['value'] = get_default_sysctl_value($tunable['tunable']);
63
			mwexec("sysctl " . $tunable['tunable'] . "=\"" 
64
				. $tunable['value'] .  "\"");
65
		}
66
	}
67
}
68

    
69
function system_resolvconf_generate($dynupdate = false) {
70
	global $config, $g;
71

    
72
	if(isset($config['system']['developerspew'])) {
73
		$mt = microtime();
74
		echo "system_resolvconf_generate() being called $mt\n";
75
	}
76

    
77
	$syscfg = $config['system'];
78

    
79
	$fd = fopen("{$g['varetc_path']}/resolv.conf", "w");
80
	if (!$fd) {
81
		printf("Error: cannot open resolv.conf in system_resolvconf_generate().\n");
82
		return 1;
83
	}
84

    
85
	$resolvconf = "domain {$syscfg['domain']}\n";
86

    
87
	$havedns = false;
88

    
89
	if (isset($syscfg['dnsallowoverride'])) {
90
		/* get dynamically assigned DNS servers (if any) */
91
		$ns = array_unique(get_nameservers());
92
		foreach($ns as $nameserver) {
93
			if($nameserver) {
94
				$resolvconf .= "nameserver $nameserver\n";
95
				$havedns = true;
96
			}
97
		}
98
	}
99
	if (!$havedns && is_array($syscfg['dnsserver'])) {
100
		foreach ($syscfg['dnsserver'] as $ns) {
101
			if ($ns) {
102
				$resolvconf .= "nameserver $ns\n";
103
				$havedns = true;
104
			}
105
		}
106
	}
107

    
108
	fwrite($fd, $resolvconf);
109
	fclose($fd);
110

    
111
	if (!$g['booting']) {
112
		/* restart dhcpd (nameservers may have changed) */
113
		if (!$dynupdate)
114
			services_dhcpd_configure();
115
	}
116

    
117
	/* setup static routes for DNS servers. */
118
	for ($dnscounter=1; $dnscounter<5; $dnscounter++) {
119
		/* setup static routes for dns servers */
120
		$dnsgw = "dns{$dnscounter}gwint";
121
		if (isset($config['system'][$dnsgw])) {
122
			$interface = $config['system'][$dnsgw];
123
			if (($interface <> "") && ($interface <> "none")) {
124
				$gatewayip = get_interface_gateway($interface);
125
				if(is_ipaddr($gatewayip)) {
126
					/* dns server array starts at 0 */
127
					$dnscountermo = $dnscounter - 1;
128
					mwexec("route delete -host {$syscfg['dnsserver'][$dnscountermo]}");
129
					mwexec("route add -host {$syscfg['dnsserver'][$dnscountermo]} {$gatewayip}");
130
				}
131
			}
132
		}
133
	}
134
	
135
	return 0;
136
}
137

    
138
function get_nameservers() {
139
	global $config, $g;
140
	$master_list = array();
141
	
142
	// Read in dhclient nameservers
143
	$dns_lists = split("\n", `ls /var/etc/nameserver_* 2>/dev/null`);
144
	if(is_array($dns_lists)) {
145
		foreach($dns_lists as $dns) {
146
			if(!$dns) 
147
				continue;
148
			$items = split("\n", file_get_contents($dns));
149
			foreach($items as $item)
150
				if($item <> "")
151
					$master_list[] = $item;
152
		}
153
	}
154

    
155
	// Read in any extra nameservers
156
	if(file_exists("/var/etc/nameservers.conf")) {
157
		$dns = `cat /var/etc/nameservers.conf`;
158
		$dns_s = split("\n", $dns);
159
		if(is_array($dns_s))
160
			foreach($dns_s as $dns)
161
				$master_list[] = $dns;
162
	}
163

    
164
	return $master_list;
165
}
166

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

    
174
	$syscfg = $config['system'];
175
	$lancfg = $config['interfaces']['lan'];
176
	$lancfgip = get_interface_ip("lan");
177
	$dnsmasqcfg = $config['dnsmasq'];
178

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

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

    
190
	$hosts = <<<EOD
191
127.0.0.1	localhost localhost.{$syscfg['domain']}
192

    
193
EOD;
194
	if (is_ipaddr($lancfgip))
195
		$hosts .= <<<EOD
196
{$lancfgip}	{$syscfg['hostname']}.{$syscfg['domain']} {$syscfg['hostname']}
197

    
198
EOD;
199

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

    
216
	return 0;
217
}
218

    
219
function system_hostname_configure() {
220
	global $config, $g;
221
	if(isset($config['system']['developerspew'])) {
222
		$mt = microtime();
223
		echo "system_hostname_configure() being called $mt\n";
224
	}
225

    
226
	$syscfg = $config['system'];
227

    
228
	/* set hostname */
229
	$status = mwexec("/bin/hostname " .
230
		escapeshellarg("{$syscfg['hostname']}.{$syscfg['domain']}"));
231

    
232
    /* Setup host GUID ID.  This is used by ZFS. */
233
	mwexec("/etc/rc.d/hostid start");
234

    
235
	return $status;
236
}
237

    
238
function system_routing_configure() {
239
	global $config, $g;
240
	if(isset($config['system']['developerspew'])) {
241
		$mt = microtime();
242
		echo "system_routing_configure() being called $mt\n";
243
	}
244

    
245
	/* Enable fast routing, if enabled */
246
	if(isset($config['staticroutes']['enablefastrouting']))
247
		mwexec("/sbin/sysctl net.inet.ip.fastforwarding=1");
248

    
249
	$route_str = exec_command("/usr/bin/netstat -rn");
250

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

    
267
	if (false) {
268
	/* if list */
269
	$iflist = get_configured_interface_list();
270

    
271
	$dont_remove_route = false;
272
	foreach ($iflist as $ifent => $ifname) {
273
		/* 
274
		 * XXX: The value of this is really when this function can take
275
		 * 	an interface as parameter.
276
		 */
277
		/* do not process interfaces that will end up with gateways */
278
		if (interface_has_gateway($ifent) || 
279
			$config['interfaces'][$ifent]['ipaddr'] == "carpdev-dhcp") {
280
			$dont_remove_route = true;
281
			break;
282
		}
283
	}
284

    
285
	if ($dont_remove_route == false) {
286
		/* remove default route */
287
		mwexec("/sbin/route delete default", true);
288
	}
289
	}
290

    
291
	$dont_add_route = false;
292
	/* if OLSRD is enabled, allow WAN to house DHCP. */
293
	if($config['installedpackages']['olsrd']) {
294
		foreach($config['installedpackages']['olsrd']['config'] as $olsrd) {
295
			if($olsrd['enabledyngw'] == "on") {
296
				$dont_add_route = true;
297
				break;
298
			}
299
		}
300
	}
301

    
302
	if($dont_add_route == false) {
303
		if(is_array($config['gateways']['gateway_item'])) {
304
			foreach($config['gateways']['gateway_item'] as $gateway) {
305
		        	if(isset($gateway['defaultgw'])) {
306
					$gatewayip = $gateway['gateway'];
307
					$interfacegw = $gateway['interface'];
308
				}
309
			}
310
			if(($interfacegw <> "bgpd") && (is_ipaddr($gatewayip))) {
311
				preg_match("/default[ ]+([0-9].*?)[ ]+/i", $route_str, $elements);
312
				if(trim($elements[1]) != "$gatewayip") {
313
					mwexec("/sbin/route delete default " . escapeshellarg($gatewayip), false);
314
				}
315
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
316
			}
317
		} else {
318
			log_error("SYSTEM: We do not have a gateways array in our XML. Is this configuration damaged?");
319
			/* adding gateway for 1.2-style configs without the new
320
			  gateway setup configured.
321
			  Force WAN to be default gateway because that is the
322
			  1.2 behavior.
323
			*/
324
			if (is_ipaddr($config['interfaces']['wan']['gateway'])) {
325
				$gatewayip = $config['interfaces']['wan']['gateway'];
326
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
327
			}
328
		}
329
	}
330

    
331
	if (is_array($config['staticroutes']['route'])) {
332

    
333
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
334
		if (!$fd) {
335
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
336
			return 1;
337
		}
338

    
339
		foreach ($config['staticroutes']['route'] as $rtent) {
340
			unset($gatewayip);
341
			unset($interfacegw);
342
			if(is_array($config['gateways']['gateway_item'])) {
343
				foreach($config['gateways']['gateway_item'] as $gateway) {
344
					if($rtent['gateway'] == $gateway['name']) {
345
						$gatewayip = $gateway['gateway'];
346
						$interfacegw = $gateway['interface'];
347
					}
348
				}
349
			}
350
			if((is_ipaddr($rtent['gateway'])) && ($gatewayip == ""))  {
351
				$gatewayip = $rtent['gateway'];
352
				$interfacegw = $rtent['interface'];
353
			}			
354
			if((isset($rtent['interfacegateway'])) && (! is_ipaddr($gatewayip))){
355
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
356
					" -iface " . escapeshellarg(convert_friendly_interface_to_real_interface_name($interfacegw)));
357
			} else {
358
				mwexec("/sbin/route add " . escapeshellarg($rtent['network']) .
359
					" " . escapeshellarg($gatewayip));
360
			}
361
			/* record route so it can be easily removed later (if necessary) */
362
			fwrite($fd, $rtent['network'] . "\n");
363
		}
364
		fclose($fd);
365
	}
366

    
367
	return 0;
368
}
369

    
370

    
371
function system_routing_enable() {
372
	global $config, $g;
373
	if(isset($config['system']['developerspew'])) {
374
		$mt = microtime();
375
		echo "system_routing_enable() being called $mt\n";
376
	}
377

    
378
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
379
}
380

    
381
function system_syslogd_start() {
382
	global $config, $g;
383
	if(isset($config['system']['developerspew'])) {
384
		$mt = microtime();
385
		echo "system_syslogd_start() being called $mt\n";
386
	}
387

    
388
	$syslogcfg = $config['syslog'];
389

    
390
	if ($g['booting'])
391
		echo "Starting syslog...";
392
	else
393
		killbypid("{$g['varrun_path']}/syslog.pid");
394

    
395
	if(is_process_running("syslogd"))
396
		mwexec("/usr/bin/killall -9 syslogd");
397
	if(is_process_running("fifolog_writer"))
398
		mwexec("/usr/bin/killall -9 fifolog_writer");
399
	
400
	// Define carious commands for logging
401
	$fifolog_create = "/usr/sbin/fifolog_create -s ";
402
	$fifolog_log = "|/usr/sbin/fifolog_writer ";
403
	$clog_create = "/usr/sbin/clog -i -s ";
404
	$clog_log = "%";
405

    
406
	// Which logging type are we using this week??
407
	if(isset($config['system']['usefifolog'])) {
408
		$log_directive = $fifolog_log;
409
		$log_create_directive = $fifolog_create;		
410
	} else { // Defaults to CLOG
411
		$log_directive = $clog_log;
412
		$log_create_directive = $clog_create;
413
	}
414
	
415
	if (isset($syslogcfg)) {
416
		$separatelogfacilities = array('ntpd','racoon','openvpn');
417
		if($config['installedpackages']['package']) {
418
			foreach($config['installedpackages']['package'] as $package) {
419
				if($package['logging']) {
420
					$pkgfacilities[] = $package['logging']['facilityname'];
421
					$separatelogfacilities = $separatelogfacilities + $pkgfacilities;
422
					$facilitylist = implode(',', $pkgfacilities);
423
					mwexec("{$log_create_directive} 10240 {$g['varlog_path']}/{$package['logging']['logfilename']}");
424
					$syslogconf .= "!{$facilitylist}\n*.*\t\t\t\t\t\t {$log_directive}{$g['varlog_path']}/{$package['logging']['logfilename']}\n";
425
				}
426
			}
427
		}
428
		$facilitylist = implode(',', array_unique($separatelogfacilities));
429
		/* write syslog.conf */		
430
		$fd = fopen("{$g['varetc_path']}/syslog.conf", "w");
431
		if (!$fd) {
432
			printf("Error: cannot open syslog.conf in system_syslogd_start().\n");
433
			return 1;
434
		}
435
		$syslogconf .= "!ntpdate,!ntpd\n";
436
		if (!isset($syslogcfg['disablelocallogging'])) 
437
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/ntpd.log\n";
438
		$syslogconf .= "!ppp\n";
439
		if (!isset($syslogcfg['disablelocallogging'])) 
440
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/ppp.log\n";
441
		$syslogconf .= "!racoon\n";
442
		if (!isset($syslogcfg['disablelocallogging'])) 
443
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/ipsec.log\n";
444
		$syslogconf .= "!apinger\n";
445
		if (!isset($syslogcfg['disablelocallogging'])) 
446
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/slbd.log\n";
447
		if (isset($syslogcfg['vpn'])) {
448
			if($syslogcfg['remoteserver'])
449
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver']}\n";
450
			if($syslogcfg['remoteserver2'])
451
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver2']}\n";
452
			if($syslogcfg['remoteserver3'])
453
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
454
		}
455
		$syslogconf .= "!openvpn\n";
456
		if (!isset($syslogcfg['disablelocallogging'])) 
457
			$syslogconf .= "*.*					 {$log_directive}{$g['varlog_path']}/openvpn.log\n";
458
		if (isset($syslogcfg['vpn'])) {
459
			if($syslogcfg['remoteserver'])
460
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver']}\n";
461
			if($syslogcfg['remoteserver2'])
462
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
463
			if($syslogcfg['remoteserver3'])
464
				$syslogconf .= "*.*					 @{$syslogcfg['remoteserver3']}\n";
465
		}
466
		$syslogconf .= "!-{$facilitylist}\n";
467
		if (!isset($syslogcfg['disablelocallogging'])) 
468
			$syslogconf .= <<<EOD
469
local0.*										 {$log_directive}{$g['varlog_path']}/filter.log
470
local3.*										 {$log_directive}{$g['varlog_path']}/vpn.log
471
local4.*										 {$log_directive}{$g['varlog_path']}/portalauth.log
472
local7.*										 {$log_directive}{$g['varlog_path']}/dhcpd.log
473
*.notice;kern.debug;lpr.info;mail.crit; 		 {$log_directive}{$g['varlog_path']}/system.log
474
news.err;local0.none;local3.none;local4.none; 	 {$log_directive}{$g['varlog_path']}/system.log
475
local7.none										 {$log_directive}{$g['varlog_path']}/system.log
476
security.*										 {$log_directive}{$g['varlog_path']}/system.log
477
auth.info;authpriv.info;daemon.info				 {$log_directive}{$g['varlog_path']}/system.log
478
local1.*										 {$log_directive}{$g['varlog_path']}/relayd.log
479
auth.info;authpriv.info 						 |exec /usr/local/sbin/sshlockout_pf
480
*.emerg											 *
481

    
482
EOD;
483
		if (isset($syslogcfg['filter'])) {
484
			if($syslogcfg['remoteserver'])
485
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver']}\n";
486
			if($syslogcfg['remoteserver2'])
487
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver2']}\n";
488
			if($syslogcfg['remoteserver3'])
489
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver3']}\n";
490

    
491
		}
492
		if (isset($syslogcfg['vpn'])) {
493
			if($syslogcfg['remoteserver'])
494
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver']}\n";
495
			if($syslogcfg['remoteserver2'])
496
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver2']}\n";
497
			if($syslogcfg['remoteserver3'])
498
				$syslogconf .= "local3.*			 @{$syslogcfg['remoteserver3']}\n";
499
		}
500
		if (isset($syslogcfg['portalauth'])) {
501
			if($syslogcfg['remoteserver'])
502
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver']}\n";
503
			if($syslogcfg['remoteserver2'])
504
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver2']}\n";
505
			if($syslogcfg['remoteserver3'])
506
				$syslogconf .= "local4.*			 @{$syslogcfg['remoteserver3']}\n";
507
		}
508
		if (isset($syslogcfg['dhcp'])) {
509
			if($syslogcfg['remoteserver'])
510
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver']}\n";
511
			if($syslogcfg['remoteserver2'])
512
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver2']}\n";
513
			if($syslogcfg['remoteserver3'])
514
				$syslogconf .= "local7.*			 @{$syslogcfg['remoteserver3']}\n";
515
		}
516
		if (isset($syslogcfg['system'])) {
517
			if($syslogcfg['remoteserver'])
518
				$syslogconf .= <<<EOD
519
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver']}
520
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver']}
521
security.*										 @{$syslogcfg['remoteserver']}
522
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver']}
523
*.emerg											 @{$syslogcfg['remoteserver']}
524

    
525
EOD;
526

    
527
		if (isset($syslogcfg['system'])) {
528
			if($syslogcfg['remoteserver2'])
529
				$syslogconf .= <<<EOD
530
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver2']}
531
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver2']}
532
security.*										 @{$syslogcfg['remoteserver2']}
533
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver2']}
534
*.emerg											 @{$syslogcfg['remoteserver2']}
535

    
536
EOD;
537

    
538
		if (isset($syslogcfg['system'])) {
539
			if($syslogcfg['remoteserver3'])
540
				$syslogconf .= <<<EOD
541
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver3']}
542
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver3']}
543
security.*										 @{$syslogcfg['remoteserver3']}
544
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver3']}
545
*.emerg											 @{$syslogcfg['remoteserver3']}
546

    
547
EOD;
548

    
549
}
550
		if (isset($syslogcfg['logall'])) {
551
			if($syslogcfg['remoteserver'])
552
				$syslogconf .= <<<EOD
553
*.*								@{$syslogcfg['remoteserver']}
554

    
555
EOD;
556

    
557
}
558
			if($syslogcfg['remoteserver2'])
559
				$syslogconf .= <<<EOD
560
*.*								@{$syslogcfg['remoteserver2']}
561

    
562
EOD;
563

    
564
}
565
			if($syslogcfg['remoteserver3'])
566
				$syslogconf .= <<<EOD
567
*.*								@{$syslogcfg['remoteserver3']}
568

    
569
EOD;
570

    
571
}
572
		fwrite($fd, $syslogconf);
573
		fclose($fd);
574
		// Are we logging to a least one remote server ?
575
		if(strpos($syslogconf, "@") != false)
576
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
577
		else
578
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
579

    
580
	} else {
581
		$retval = mwexec("/usr/sbin/syslogd -c");
582
	}
583

    
584
	if ($g['booting'])
585
		echo "done.\n";
586

    
587
	return $retval;
588
}
589

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

    
597
	if ($g['booting'])
598
		echo "Initializing PCMCIA...";
599

    
600
	/* kill any running pccardd */
601
	killbypid("{$g['varrun_path']}/pccardd.pid");
602

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

    
606
	if ($g['booting']) {
607
		if ($res == 0)
608
			echo "done.\n";
609
		else
610
			echo "failed!\n";
611
	}
612

    
613
	return $res;
614
}
615

    
616

    
617
function system_webgui_start() {
618
	global $config, $g;
619

    
620
	if ($g['booting'])
621
		echo "Starting webConfigurator...";
622

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

    
626
	sleep(1);
627

    
628
	chdir($g['www_path']);
629

    
630
	/* defaults */
631
	$portarg = "80";
632
	$crt = "";
633
	$key = "";
634
	$ca = "";
635

    
636
	/* non-standard port? */
637
	if (isset($config['system']['webgui']['port']) && $config['system']['webgui']['port'] <> "")
638
		$portarg = "{$config['system']['webgui']['port']}";
639

    
640
	if ($config['system']['webgui']['protocol'] == "https") {
641
		// Ensure that we have a webConfigurator CERT
642
		$cert =& lookup_cert($config['system']['webgui']['ssl-certref']);
643
		if(!is_array($cert) && !$cert['crt'] && !$cert['prv']) {
644
			if (!is_array($config['system']['ca']))
645
				$config['system']['ca'] = array();
646
			$a_ca =& $config['system']['ca'];
647
			if (!is_array($config['system']['cert']))
648
				$config['system']['cert'] = array();
649
			$a_cert =& $config['system']['cert'];
650
			echo "Creating SSL Certificate... ";
651
			mwexec("openssl genrsa 1024 > /etc/ssl.key");
652
			mwexec("chmod 400 /etc/ssl.key");
653
			mwexec("openssl req -new -x509 -nodes -sha1 -days 365 -key /etc/ssl.key > /etc/ssl.crt");
654
			mwexec("chmod 400 /etc/ssl.crt");
655
			$cert_file = "/etc/ssl.crt";
656
			$key_file  = "/etc/ssl.key";
657
			if(file_exists($cert_file) && file_exists($key_file)) {
658
				$cert = array();
659
				$cert['refid'] = uniqid();
660
				$cert['name'] = "webConfigurator default";
661
				$crt = file_get_contents($cert_file);
662
				$key = file_get_contents($key_file);
663
				cert_import($cert, $crt, $key);
664
				$a_cert[] = $cert;
665
				$config['system']['webgui']['ssl-certref'] = $cert['refid'];
666
				write_config("Importing HTTPS certificate");
667
				if(!$config['system']['webgui']['port'])
668
					$portarg = "443";
669
				$ca = ca_chain($cert);
670
			} else {
671
				log_error("ERROR: Could not locate a certificate file for import $cert_file - $key_file");
672
			}
673
		} else
674
			$crt = base64_decode($cert['crt']);
675
			$key = base64_decode($cert['prv']);
676
			if(!$config['system']['webgui']['port'])
677
				$portarg = "443";
678
			$ca = ca_chain($cert);
679
	}
680

    
681
	/* generate lighttpd configuration */
682
	system_generate_lighty_config("{$g['varetc_path']}/lighty-webConfigurator.conf",
683
		$crt, $key, $ca, "lighty-webConfigurator.pid", $portarg, "/usr/local/www/");
684

    
685
	sleep(1);
686

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

    
690
	/* fetch page to preload apc cache */
691
	mwexec("fetch -o /dev/null -q {$config['system']['webgui']['protocol']}://localhost:{$portarg}/preload.php");
692

    
693
	if ($g['booting']) {
694
		if ($res == 0)
695
			echo "done.\n";
696
		else
697
			echo "failed!\n";
698
	}
699

    
700
	return $res;
701
}
702

    
703
function system_generate_lighty_config($filename,
704
	$cert,
705
	$key,
706
	$ca,
707
	$pid_file,
708
	$port = 80,
709
	$document_root = "/usr/local/www/",
710
	$cert_location = "cert.pem",
711
	$ca_location = "ca.pem",
712
	$max_procs = 2,
713
	$max_requests = "1",
714
	$fast_cgi_enable = true,
715
	$captive_portal = false) {
716

    
717
	global $config, $g;
718

    
719
	if(!is_dir("/tmp/lighttpdcompress"))
720
		mkdir("/tmp/lighttpdcompress");
721

    
722
	if(isset($config['system']['developerspew'])) {
723
		$mt = microtime();
724
		echo "system_generate_lighty_config() being called $mt\n";
725
	}
726

    
727
	if($captive_portal == true)  {
728
		$captiveportal = ",\"mod_rewrite\"";
729
		$captive_portal_rewrite = "url.rewrite-once = ( \"(.*captiveportal.*)\" => \"$1\", \"(.*)\" => \"/index.php?redirurl=$1\" )\n";
730
		$captive_portal_module = "\"mod_accesslog\", ";
731
		$maxprocperip = $config['captiveportal']['maxprocperip'];
732
		if(!$maxprocperip and $maxprocperip > 0)
733
			$captive_portal_mod_evasive = "evasive.max-conns-per-ip = {$maxprocperip}";
734
		else
735
			$captive_portal_mod_evasive = "";
736
		$server_upload_dirs = "server.upload-dirs = ( \"/tmp/captiveportal/\" )\n";
737
		exec("mkdir -p /tmp/captiveportal");
738
		exec("chmod a-w /tmp/captiveportal");
739
		$server_max_request_size = "server.max-request-size    = 384";
740
	} else {
741
		$captive_portal_module = "";
742
		$captive_portal_mod_evasive = "";
743
		$server_upload_dirs = "server.upload-dirs = ( \"{$g['upload_path']}/\", \"/tmp/\", \"/var/\" )\n";
744
		$server_max_request_size = "server.max-request-size    = 2097152";
745
	}
746
	
747
	if($port <> "")
748
		$lighty_port = $port;
749
	else
750
		$lighty_port = "80";
751

    
752
	$memory = get_memory();
753
	$avail = $memory[0];
754

    
755
	if($avail > 0 and $avail < 65) {
756
		$fast_cgi_enable = false;
757
	}
758

    
759
	if($avail > 65 and $avail < 98) {
760
		$max_procs = 1;
761
	}
762

    
763
	if($avail > 97 and $avail < 128) {
764
		$max_procs = 1;
765
	}
766

    
767
	if($avail > 127 and $avail < 256) {
768
		$max_procs = 1;
769
	}
770

    
771
	if($avail > 255 and $avail < 384) {
772
		$max_procs = 3;
773
	}
774

    
775
	if($avail > 383) {
776
		$max_procs = 4;
777
	}
778

    
779
	if($captive_portal == true)  {	
780
		$bin_environment =  <<<EOC
781
        "bin-environment" => (
782
           "PHP_FCGI_CHILDREN" => "$max_procs",
783
           "PHP_FCGI_MAX_REQUESTS" => "500"
784
        ), 
785
EOC;
786

    
787
	} else if ($avail > 0 and $avail < 128) {
788
		$bin_environment = <<<EOC
789
		"bin-environment" => (
790
			"PHP_FCGI_CHILDREN" => "$max_procs",
791
			"PHP_FCGI_MAX_REQUESTS" => "2",
792
	),
793

    
794
EOC;
795
	} else
796
		$bin_environment =  <<<EOC
797
        "bin-environment" => (
798
           "PHP_FCGI_CHILDREN" => "$max_procs",
799
           "PHP_FCGI_MAX_REQUESTS" => "500"
800
        ), 
801
EOC;
802

    
803
	if($fast_cgi_enable == true) {
804
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
805
		$cgi_config = "";
806
		$fastcgi_config = <<<EOD
807
#### fastcgi module
808
## read fastcgi.txt for more info
809
fastcgi.server = ( ".php" =>
810
	( "localhost" =>
811
		(
812
			"socket" => "/tmp/php-fastcgi.socket",
813
			"min-procs" => 0,
814
			"max-procs" => {$max_procs},
815
			{$bin_environment}			
816
			"bin-path" => "/usr/local/bin/php"
817
		)
818
	)
819
)
820

    
821
#### CGI module
822
cgi.assign                 = ( ".cgi" => "" )
823

    
824
EOD;
825
	} else {
826
		$fastcgi_config = "";
827
		$module = "\"mod_cgi\"";
828
		$cgi_config = <<<EOD
829
#### CGI module
830
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
831
                               ".cgi" => "" )
832

    
833
EOD;
834
	}
835

    
836
	$lighty_config .= <<<EOD
837
#
838
# lighttpd configuration file
839
#
840
# use a it as base for lighttpd 1.0.0 and above
841
#
842
############ Options you really have to take care of ####################
843

    
844
## FreeBSD!
845
server.event-handler	= "freebsd-kqueue"
846
server.network-backend 	= "writev"
847

    
848
## modules to load
849
server.modules              =   (
850
									{$captive_portal_module}
851
									"mod_access", "mod_accesslog", "mod_expire", "mod_compress", 
852
									{$module}{$captiveportal}
853
								)
854

    
855
## Unused modules
856
#                               "mod_setenv",
857

    
858
#				"mod_redirect",
859
#                               "mod_rewrite",
860
#                               "mod_ssi",
861
#                               "mod_usertrack",
862
#                               "mod_expire",
863
#                               "mod_secdownload",
864
#                               "mod_rrdtool",
865
#                               "mod_auth",
866
#                               "mod_status",
867
#                               "mod_alias",
868
#                               "mod_proxy",
869
#                               "mod_simple_vhost",
870
#                               "mod_evhost",
871
#                               "mod_userdir",
872
#                               "mod_cgi",
873
#                                "mod_accesslog"
874

    
875
server.max-keep-alive-requests = 15
876
server.max-keep-alive-idle = 30
877

    
878
## a static document-root, for virtual-hosting take look at the
879
## server.virtual-* options
880
server.document-root        = "{$document_root}"
881
{$captive_portal_rewrite}
882

    
883
# Maximum idle time with nothing being written (php downloading)
884
server.max-write-idle = 999
885

    
886
## where to send error-messages to
887
server.errorlog             = "/var/log/lighttpd.error.log"
888

    
889
# files to check for if .../ is requested
890
server.indexfiles           = ( "index.php", "index.html",
891
                                "index.htm", "default.htm" )
892

    
893
# mimetype mapping
894
mimetype.assign             = (
895
  ".pdf"          =>      "application/pdf",
896
  ".sig"          =>      "application/pgp-signature",
897
  ".spl"          =>      "application/futuresplash",
898
  ".class"        =>      "application/octet-stream",
899
  ".ps"           =>      "application/postscript",
900
  ".torrent"      =>      "application/x-bittorrent",
901
  ".dvi"          =>      "application/x-dvi",
902
  ".gz"           =>      "application/x-gzip",
903
  ".pac"          =>      "application/x-ns-proxy-autoconfig",
904
  ".swf"          =>      "application/x-shockwave-flash",
905
  ".tar.gz"       =>      "application/x-tgz",
906
  ".tgz"          =>      "application/x-tgz",
907
  ".tar"          =>      "application/x-tar",
908
  ".zip"          =>      "application/zip",
909
  ".mp3"          =>      "audio/mpeg",
910
  ".m3u"          =>      "audio/x-mpegurl",
911
  ".wma"          =>      "audio/x-ms-wma",
912
  ".wax"          =>      "audio/x-ms-wax",
913
  ".ogg"          =>      "audio/x-wav",
914
  ".wav"          =>      "audio/x-wav",
915
  ".gif"          =>      "image/gif",
916
  ".jpg"          =>      "image/jpeg",
917
  ".jpeg"         =>      "image/jpeg",
918
  ".png"          =>      "image/png",
919
  ".xbm"          =>      "image/x-xbitmap",
920
  ".xpm"          =>      "image/x-xpixmap",
921
  ".xwd"          =>      "image/x-xwindowdump",
922
  ".css"          =>      "text/css",
923
  ".html"         =>      "text/html",
924
  ".htm"          =>      "text/html",
925
  ".js"           =>      "text/javascript",
926
  ".asc"          =>      "text/plain",
927
  ".c"            =>      "text/plain",
928
  ".conf"         =>      "text/plain",
929
  ".text"         =>      "text/plain",
930
  ".txt"          =>      "text/plain",
931
  ".dtd"          =>      "text/xml",
932
  ".xml"          =>      "text/xml",
933
  ".mpeg"         =>      "video/mpeg",
934
  ".mpg"          =>      "video/mpeg",
935
  ".mov"          =>      "video/quicktime",
936
  ".qt"           =>      "video/quicktime",
937
  ".avi"          =>      "video/x-msvideo",
938
  ".asf"          =>      "video/x-ms-asf",
939
  ".asx"          =>      "video/x-ms-asf",
940
  ".wmv"          =>      "video/x-ms-wmv",
941
  ".bz2"          =>      "application/x-bzip",
942
  ".tbz"          =>      "application/x-bzip-compressed-tar",
943
  ".tar.bz2"      =>      "application/x-bzip-compressed-tar"
944
 )
945

    
946
# Use the "Content-Type" extended attribute to obtain mime type if possible
947
#mimetypes.use-xattr        = "enable"
948

    
949
#### accesslog module
950
#accesslog.filename          = "/dev/null"
951

    
952
## deny access the file-extensions
953
#
954
# ~    is for backupfiles from vi, emacs, joe, ...
955
# .inc is often used for code includes which should in general not be part
956
#      of the document-root
957
url.access-deny             = ( "~", ".inc" )
958

    
959

    
960
######### Options that are good to be but not neccesary to be changed #######
961

    
962
## bind to port (default: 80)
963
server.port                = {$lighty_port}
964

    
965
## error-handler for status 404
966
#server.error-handler-404   = "/error-handler.html"
967
#server.error-handler-404   = "/error-handler.php"
968

    
969
## to help the rc.scripts
970
server.pid-file            = "/var/run/{$pid_file}"
971

    
972
## virtual directory listings
973
server.dir-listing         = "disable"
974

    
975
## enable debugging
976
debug.log-request-header   = "disable"
977
debug.log-response-header  = "disable"
978
debug.log-request-handling = "disable"
979
debug.log-file-not-found   = "disable"
980

    
981
# gzip compression
982
compress.cache-dir = "/tmp/lighttpdcompress/"
983
compress.filetype  = ("text/plain","text/css", "text/xml", "text/javascript" )
984

    
985
{$server_upload_dirs}
986

    
987
{$server_max_request_size}
988

    
989
{$fastcgi_config}
990

    
991
{$cgi_config}
992

    
993
{$captive_portal_mod_evasive}
994

    
995
expire.url = (
996
				"" => "access 50 hours",	
997
        )
998

    
999
EOD;
1000

    
1001
	$cert = str_replace("\r", "", $cert);
1002
	$key = str_replace("\r", "", $key);
1003
	$ca = str_replace("\r", "", $ca);
1004

    
1005
	$cert = str_replace("\n\n", "\n", $cert);
1006
	$key = str_replace("\n\n", "\n", $key);
1007
	$ca = str_replace("\n\n", "\n", $ca);
1008

    
1009
	if($cert <> "" and $key <> "") {
1010
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
1011
		if (!$fd) {
1012
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
1013
			return 1;
1014
		}
1015
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
1016
		fwrite($fd, $cert);
1017
		fwrite($fd, "\n");
1018
		fwrite($fd, $key);
1019
		fclose($fd);
1020
		if($ca <> "") {
1021
			$fd = fopen("{$g['varetc_path']}/{$ca_location}", "w");
1022
			if (!$fd) {
1023
				printf("Error: cannot open ca.pem in system_webgui_start().\n");
1024
				return 1;
1025
			}
1026
			chmod("{$g['varetc_path']}/{$ca_location}", 0600);
1027
			fwrite($fd, $ca);
1028
			fclose($fd);
1029
		}
1030
		$lighty_config .= "\n";
1031
		$lighty_config .= "## ssl configuration\n";
1032
		$lighty_config .= "ssl.engine = \"enable\"\n";
1033
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_location}\"\n\n";
1034
		if($ca <> "")
1035
			$lighty_config .= "ssl.ca-file = \"{$g['varetc_path']}/{$ca_location}\"\n\n";
1036
	}
1037

    
1038
	$fd = fopen("{$filename}", "w");
1039
	if (!$fd) {
1040
		printf("Error: cannot open {$filename} in system_generate_lighty_config().\n");
1041
		return 1;
1042
	}
1043
	fwrite($fd, $lighty_config);
1044
	fclose($fd);
1045

    
1046
	return 0;
1047

    
1048
}
1049

    
1050
function system_timezone_configure() {
1051
	global $config, $g;
1052
	if(isset($config['system']['developerspew'])) {
1053
		$mt = microtime();
1054
		echo "system_timezone_configure() being called $mt\n";
1055
	}
1056

    
1057
	$syscfg = $config['system'];
1058

    
1059
	if ($g['booting'])
1060
		echo "Setting timezone...";
1061

    
1062
	/* extract appropriate timezone file */
1063
	$timezone = $syscfg['timezone'];
1064
	if (!$timezone)
1065
		$timezone = "Etc/UTC";
1066

    
1067
	conf_mount_rw();
1068

    
1069
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
1070
		escapeshellarg($timezone) . " > /etc/localtime");
1071

    
1072
	mwexec("sync");
1073
	conf_mount_ro();
1074

    
1075
	if ($g['booting'])
1076
		echo "done.\n";
1077
}
1078

    
1079
function system_ntp_configure() {
1080
	global $config, $g;
1081

    
1082
	$syscfg = $config['system'];
1083

    
1084
	/* open configuration for wrting or bail */
1085
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
1086
	if(!$fd) {
1087
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1088
		return;
1089
	}
1090

    
1091
	fwrite($fd, "# \n");
1092
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1093
	fwrite($fd, "# \n\n");
1094

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

    
1099
	/* Setup listener(s) if the user has configured one */
1100
        if ($config['installedpackages']['openntpd']) {
1101
    		/* server config is in coregui1 */
1102
		$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1103
		if ($xmlsettings['enable'] == 'on') {
1104
			$ifaces = explode(',', $xmlsettings['interface']);
1105
			$ifaces = array_map('get_real_interface', $ifaces);
1106
			$ifaces = array_filter($ifaces, 'does_interface_exist');
1107
			$ips = array_map('find_interface_ip', $ifaces);
1108
			foreach ($ips as $ip) {
1109
				if (is_ipaddr($ip))
1110
					fwrite($fd, "listen on $ip\n");
1111
			}
1112
		}
1113
	}
1114

    
1115
	fwrite($fd, "\n");
1116

    
1117
	/* slurp! */
1118
	fclose($fd);
1119

    
1120
	/* if openntpd is running, kill it */
1121
	while(is_process_running("ntpd")) {
1122
		mwexec("/usr/bin/killall ntpd", true);
1123
	}
1124

    
1125
	/* if /var/empty does not exist, create it */
1126
	if(!is_dir("/var/empty"))
1127
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1128

    
1129
	if($g['booting'])
1130
		return;
1131
	
1132
	/* start opentpd, set time now and use /var/etc/ntpd.conf */
1133
	exec("/usr/local/sbin/ntpd -s -f {$g['varetc_path']}/ntpd.conf");
1134
	
1135
	// Note that we are starting up
1136
	exec("echo 'OpenNTPD is starting up' >> {$g['varlog_path']}/ntpd.log");
1137

    
1138
}
1139

    
1140
function sync_system_time() {
1141
	global $config, $g;
1142

    
1143
	$syscfg = $config['system'];
1144

    
1145
	if ($g['booting'])
1146
		echo "Syncing system time before startup...";
1147

    
1148
	/* foreach through servers and write out to ntpd.conf */
1149
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1150
		mwexec("/usr/sbin/ntpdate -s $ts");
1151
	}
1152
	
1153
	if ($g['booting'])
1154
		echo "done.\n";
1155
	
1156
}
1157

    
1158
function system_halt() {
1159
	global $g;
1160

    
1161
	system_reboot_cleanup();
1162

    
1163
	mwexec("/usr/bin/nohup /etc/rc.halt > /dev/null 2>&1 &");
1164
}
1165

    
1166
function system_reboot() {
1167
	global $g;
1168

    
1169
	system_reboot_cleanup();
1170

    
1171
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
1172
}
1173

    
1174
function system_reboot_sync() {
1175
	global $g;
1176

    
1177
	system_reboot_cleanup();
1178

    
1179
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
1180
}
1181

    
1182
function system_reboot_cleanup() {
1183
	mwexec("/usr/local/bin/beep.sh stop");
1184
	require_once("captiveportal.inc");
1185
	captiveportal_radius_stop_all();
1186
	require_once("voucher.inc");
1187
	voucher_save_db_to_config();
1188
}
1189

    
1190
function system_do_shell_commands($early = 0) {
1191
	global $config, $g;
1192
	if(isset($config['system']['developerspew'])) {
1193
		$mt = microtime();
1194
		echo "system_do_shell_commands() being called $mt\n";
1195
	}
1196

    
1197
	if ($early)
1198
		$cmdn = "earlyshellcmd";
1199
	else
1200
		$cmdn = "shellcmd";
1201

    
1202
	if (is_array($config['system'][$cmdn])) {
1203

    
1204
		/* *cmd is an array, loop through */
1205
		foreach ($config['system'][$cmdn] as $cmd) {
1206
			exec($cmd);
1207
		}
1208

    
1209
	} elseif($config['system'][$cmdn] <> "") {
1210

    
1211
		/* execute single item */
1212
		exec($config['system'][$cmdn]);
1213

    
1214
	}
1215
}
1216

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

    
1224
	if (isset($config['system']['disableconsolemenu'])) {
1225
		touch("{$g['varetc_path']}/disableconsole");
1226
	} else {
1227
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1228
	}
1229
}
1230

    
1231
function system_dmesg_save() {
1232
	global $g;
1233
	if(isset($config['system']['developerspew'])) {
1234
		$mt = microtime();
1235
		echo "system_dmesg_save() being called $mt\n";
1236
	}
1237

    
1238
	$dmesg = "";
1239
	exec("/sbin/dmesg", $dmesg);
1240

    
1241
	/* find last copyright line (output from previous boots may be present) */
1242
	$lastcpline = 0;
1243

    
1244
	for ($i = 0; $i < count($dmesg); $i++) {
1245
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1246
			$lastcpline = $i;
1247
	}
1248

    
1249
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1250
	if (!$fd) {
1251
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1252
		return 1;
1253
	}
1254

    
1255
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1256
		fwrite($fd, $dmesg[$i] . "\n");
1257

    
1258
	fclose($fd);
1259

    
1260
	return 0;
1261
}
1262

    
1263
function system_set_harddisk_standby() {
1264
	global $g, $config;
1265
	if(isset($config['system']['developerspew'])) {
1266
		$mt = microtime();
1267
		echo "system_set_harddisk_standby() being called $mt\n";
1268
	}
1269

    
1270
	if (isset($config['system']['harddiskstandby'])) {
1271
		if ($g['booting']) {
1272
			echo 'Setting hard disk standby... ';
1273
		}
1274

    
1275
		$standby = $config['system']['harddiskstandby'];
1276
		// Check for a numeric value
1277
		if (is_numeric($standby)) {
1278
			// Sync the disk(s)
1279
			mwexec('/bin/sync');
1280
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
1281
				// Reinitialize ATA-drives
1282
				mwexec('/usr/local/sbin/atareinit');
1283
				if ($g['booting']) {
1284
					echo "done.\n";
1285
				}
1286
			} else if ($g['booting']) {
1287
				echo "failed!\n";
1288
			}
1289
		} else if ($g['booting']) {
1290
			echo "failed!\n";
1291
		}
1292
	}
1293
}
1294

    
1295
function system_setup_sysctl() {
1296
	global $config;
1297
	if(isset($config['system']['developerspew'])) {
1298
		$mt = microtime();
1299
		echo "system_setup_sysctl() being called $mt\n";
1300
	}
1301

    
1302
	activate_sysctls();	
1303

    
1304
	if (isset($config['system']['sharednet'])) {
1305
		system_disable_arp_wrong_if();
1306
	}
1307
}
1308

    
1309
function system_disable_arp_wrong_if() {
1310
	global $config;
1311
	if(isset($config['system']['developerspew'])) {
1312
		$mt = microtime();
1313
		echo "system_disable_arp_wrong_if() being called $mt\n";
1314
	}
1315
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
1316
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=0");
1317
}
1318

    
1319
function system_enable_arp_wrong_if() {
1320
	global $config;
1321
	if(isset($config['system']['developerspew'])) {
1322
		$mt = microtime();
1323
		echo "system_enable_arp_wrong_if() being called $mt\n";
1324
	}
1325
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=1");
1326
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=1");
1327
}
1328

    
1329
function enable_watchdog() {
1330
	global $config;
1331
	return;
1332
	$install_watchdog = false;
1333
	$supported_watchdogs = array("Geode");
1334
	$file = file_get_contents("/var/log/dmesg.boot");
1335
	foreach($supported_watchdogs as $sd) {
1336
		if(stristr($file, "Geode")) {
1337
			$install_watchdog = true;
1338
		}
1339
	}
1340
	if($install_watchdog == true) {
1341
		if(is_process_running("watchdogd"))
1342
			mwexec("/usr/bin/killall watchdogd", true);
1343
		exec("/usr/sbin/watchdogd");
1344
	}
1345
}
1346

    
1347
?>
(40-40/51)