Project

General

Profile

Download (38.2 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 list */
268
	$iflist = get_configured_interface_list();
269

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

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

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

    
300
	if($dont_add_route == false) {
301
		if(is_array($config['gateways']['gateway_item'])) {
302
			foreach($config['gateways']['gateway_item'] as $gateway) {
303
		        	if(isset($gateway['defaultgw'])) {
304
					$gatewayip = $gateway['gateway'];
305
					$interfacegw = $gateway['interface'];
306
				}
307
			}
308
			if(($interfacegw <> "bgpd") && (is_ipaddr($gatewayip)))
309
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
310
		} else {
311
			/* FIXME */
312
			/* adding gateway for 1.2-style configs without the new
313
			  gateway setup configured.
314
			  Force WAN to be default gateway because that is the
315
			  1.2 behavior.
316
			*/
317
			if (is_ipaddr($config['interfaces']['wan']['gateway'])) {
318
				$gatewayip = $config['interfaces']['wan']['gateway'];
319
				mwexec("/sbin/route add default " . escapeshellarg($gatewayip), true);
320
			}
321
		}
322
	}
323

    
324
	if (is_array($config['staticroutes']['route'])) {
325

    
326
		$fd = fopen("{$g['vardb_path']}/routes.db", "w");
327
		if (!$fd) {
328
			printf("Error: cannot open routes DB file in system_routing_configure().\n");
329
			return 1;
330
		}
331

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

    
360
	return 0;
361
}
362

    
363

    
364
function system_routing_enable() {
365
	global $config, $g;
366
	if(isset($config['system']['developerspew'])) {
367
		$mt = microtime();
368
		echo "system_routing_enable() being called $mt\n";
369
	}
370

    
371
	return mwexec("/sbin/sysctl net.inet.ip.forwarding=1");
372
}
373

    
374
function system_syslogd_start() {
375
	global $config, $g;
376
	if(isset($config['system']['developerspew'])) {
377
		$mt = microtime();
378
		echo "system_syslogd_start() being called $mt\n";
379
	}
380

    
381
	$syslogcfg = $config['syslog'];
382

    
383
	if ($g['booting'])
384
		echo "Starting syslog...";
385
	else
386
		killbypid("{$g['varrun_path']}/syslog.pid");
387

    
388
	if(is_process_running("syslogd"))
389
		mwexec("/usr/bin/killall -9 syslogd");
390
	if(is_process_running("fifolog_writer"))
391
		mwexec("/usr/bin/killall -9 fifolog_writer");
392
	
393
	// Define carious commands for logging
394
	$fifolog_create = "/usr/sbin/fifolog_create -s ";
395
	$fifolog_log = "|/usr/sbin/fifolog_writer ";
396
	$clog_create = "/usr/sbin/clog -i -s ";
397
	$clog_log = "%";
398

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

    
475
EOD;
476
		if (isset($syslogcfg['filter'])) {
477
			if($syslogcfg['remoteserver'])
478
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver']}\n";
479
			if($syslogcfg['remoteserver2'])
480
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver2']}\n";
481
			if($syslogcfg['remoteserver3'])
482
				$syslogconf .= "local0.*			 @{$syslogcfg['remoteserver3']}\n";
483

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

    
518
EOD;
519

    
520
		if (isset($syslogcfg['system'])) {
521
			if($syslogcfg['remoteserver2'])
522
				$syslogconf .= <<<EOD
523
*.notice;kern.debug;lpr.info;mail.crit;			 @{$syslogcfg['remoteserver2']}
524
news.err;local0.none;local3.none;local7.none	 @{$syslogcfg['remoteserver2']}
525
security.*										 @{$syslogcfg['remoteserver2']}
526
auth.info;authpriv.info;daemon.info				 @{$syslogcfg['remoteserver2']}
527
*.emerg											 @{$syslogcfg['remoteserver2']}
528

    
529
EOD;
530

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

    
540
EOD;
541

    
542
}
543
		if (isset($syslogcfg['logall'])) {
544
			if($syslogcfg['remoteserver'])
545
				$syslogconf .= <<<EOD
546
*.*								@{$syslogcfg['remoteserver']}
547

    
548
EOD;
549

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

    
555
EOD;
556

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

    
562
EOD;
563

    
564
}
565
		fwrite($fd, $syslogconf);
566
		fclose($fd);
567
		// Are we logging to a least one remote server ?
568
		if(strpos($syslogconf, "@") != false)
569
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
570
		else
571
			$retval = system("/usr/sbin/syslogd -c -f {$g['varetc_path']}/syslog.conf");
572

    
573
	} else {
574
		$retval = mwexec("/usr/sbin/syslogd -c");
575
	}
576

    
577
	if ($g['booting'])
578
		echo "done.\n";
579

    
580
	return $retval;
581
}
582

    
583
function system_pccard_start() {
584
	global $config, $g;
585
	if(isset($config['system']['developerspew'])) {
586
		$mt = microtime();
587
		echo "system_pccard_start() being called $mt\n";
588
	}
589

    
590
	if ($g['booting'])
591
		echo "Initializing PCMCIA...";
592

    
593
	/* kill any running pccardd */
594
	killbypid("{$g['varrun_path']}/pccardd.pid");
595

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

    
599
	if ($g['booting']) {
600
		if ($res == 0)
601
			echo "done.\n";
602
		else
603
			echo "failed!\n";
604
	}
605

    
606
	return $res;
607
}
608

    
609

    
610
function system_webgui_start() {
611
	global $config, $g;
612

    
613
	if ($g['booting'])
614
		echo "Starting webConfigurator...";
615

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

    
619
	sleep(1);
620

    
621
	chdir($g['www_path']);
622

    
623
	/* defaults */
624
	$portarg = "80";
625
	$crt = "";
626
	$key = "";
627
	$ca = "";
628

    
629
	/* non-standard port? */
630
	if (isset($config['system']['webgui']['port']) && $config['system']['webgui']['port'] <> "")
631
		$portarg = "{$config['system']['webgui']['port']}";
632

    
633
	if ($config['system']['webgui']['protocol'] == "https") {
634
		// Ensure that we have a webConfigurator CERT
635
		$cert =& lookup_cert($config['system']['webgui']['ssl-certref']);
636
		if(!is_array($cert) && !$cert['crt'] && !$cert['prv']) {
637
			if (!is_array($config['system']['ca']))
638
				$config['system']['ca'] = array();
639
			$a_ca =& $config['system']['ca'];
640
			if (!is_array($config['system']['cert']))
641
				$config['system']['cert'] = array();
642
			$a_cert =& $config['system']['cert'];
643
			echo "Importaing default SSL Certificate...";
644
			$cert_file = "/etc/default_ssl_certs/pfSense_webConfigurator_HTTPS_Certificate.crt";
645
			$key_file  = "/etc/default_ssl_certs/pfSense_webConfigurator_HTTPS_Certificate.key";
646
			if(file_exists($cert_file) && file_exists($key_file)) {
647
				$cert = array();
648
				$cert['refid'] = uniqid();
649
				$cert['name'] = "pfSense webConfigurator default";
650
				$crt = file_get_contents("/etc/default_ssl_certs/pfSense_webConfigurator_HTTPS_Certificate.crt");
651
				$key = file_get_contents("/etc/default_ssl_certs/pfSense_webConfigurator_HTTPS_Certificate.key");
652
				cert_import($cert, $crt, $key);
653
				$a_cert[] = $cert;
654
				$config['system']['webgui']['ssl-certref'] = $cert['refid'];
655
				write_config("Importing default HTTPS certificate from /etc/default_ssl_certs/");
656
				if(!$config['system']['webgui']['port'])
657
					$portarg = "443";
658
				$ca = ca_chain($cert);
659
			} else {
660
				log_error("ERROR: Could not locate a default certificate file in /etc/default_ssl/certs/ for import $cert_file - $key_file");
661
			}
662
		} else
663
			$crt = base64_decode($cert['crt']);
664
			$key = base64_decode($cert['prv']);
665
			if(!$config['system']['webgui']['port'])
666
				$portarg = "443";
667
			$ca = ca_chain($cert);
668
	}
669

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

    
674
	sleep(1);
675

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

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

    
682
	if ($g['booting']) {
683
		if ($res == 0)
684
			echo "done.\n";
685
		else
686
			echo "failed!\n";
687
	}
688

    
689
	return $res;
690
}
691

    
692
function system_generate_lighty_config($filename,
693
	$cert,
694
	$key,
695
	$ca,
696
	$pid_file,
697
	$port = 80,
698
	$document_root = "/usr/local/www/",
699
	$cert_location = "cert.pem",
700
	$ca_location = "ca.pem",
701
	$max_procs = 2,
702
	$max_requests = "1",
703
	$fast_cgi_enable = true,
704
	$captive_portal = false) {
705

    
706
	global $config, $g;
707

    
708
	if(!is_dir("/tmp/lighttpdcompress"))
709
		mkdir("/tmp/lighttpdcompress");
710

    
711
	if(isset($config['system']['developerspew'])) {
712
		$mt = microtime();
713
		echo "system_generate_lighty_config() being called $mt\n";
714
	}
715

    
716
	if($captive_portal == true)  {
717
		$captiveportal = ",\"mod_rewrite\"";
718
		$captive_portal_rewrite = "url.rewrite-once = ( \"(.*captiveportal.*)\" => \"$1\", \"(.*)\" => \"/index.php?redirurl=$1\" )\n";
719
		$captive_portal_module = "\"mod_accesslog\", ";
720
		$maxprocperip = $config['captiveportal']['maxprocperip'];
721
		if(!$maxprocperip and $maxprocperip > 0)
722
			$captive_portal_mod_evasive = "evasive.max-conns-per-ip = {$maxprocperip}";
723
		else
724
			$captive_portal_mod_evasive = "";
725
		$server_upload_dirs = "server.upload-dirs = ( \"/tmp/captiveportal/\" )\n";
726
		exec("mkdir -p /tmp/captiveportal");
727
		exec("chmod a-w /tmp/captiveportal");
728
		$server_max_request_size = "server.max-request-size    = 384";
729
	} else {
730
		$captive_portal_module = "";
731
		$captive_portal_mod_evasive = "";
732
		$server_upload_dirs = "server.upload-dirs = ( \"{$g['upload_path']}/\", \"/tmp/\", \"/var/\" )\n";
733
		$server_max_request_size = "server.max-request-size    = 2097152";
734
	}
735
	
736
	if($port <> "")
737
		$lighty_port = $port;
738
	else
739
		$lighty_port = "80";
740

    
741
	$memory = get_memory();
742
	$avail = $memory[0];
743

    
744
	if($avail > 0 and $avail < 65) {
745
		$fast_cgi_enable = false;
746
	}
747

    
748
	if($avail > 65 and $avail < 98) {
749
		$max_procs = 1;
750
	}
751

    
752
	if($avail > 97 and $avail < 128) {
753
		$max_procs = 1;
754
	}
755

    
756
	if($avail > 127 and $avail < 256) {
757
		$max_procs = 1;
758
	}
759

    
760
	if($avail > 255 and $avail < 384) {
761
		$max_procs = 3;
762
	}
763

    
764
	if($avail > 383) {
765
		$max_procs = 4;
766
	}
767

    
768
	if($captive_portal == true)  {	
769
		$bin_environment =  <<<EOC
770
        "bin-environment" => (
771
           "PHP_FCGI_CHILDREN" => "$max_procs",
772
           "PHP_FCGI_MAX_REQUESTS" => "500"
773
        ), 
774
EOC;
775

    
776
	} else if ($avail > 0 and $avail < 128) {
777
		$bin_environment = <<<EOC
778
		"bin-environment" => (
779
			"PHP_FCGI_CHILDREN" => "$max_procs",
780
			"PHP_FCGI_MAX_REQUESTS" => "2",
781
	),
782

    
783
EOC;
784
	} else
785
		$bin_environment =  <<<EOC
786
        "bin-environment" => (
787
           "PHP_FCGI_CHILDREN" => "$max_procs",
788
           "PHP_FCGI_MAX_REQUESTS" => "500"
789
        ), 
790
EOC;
791

    
792
	if($fast_cgi_enable == true) {
793
		$module = "\"mod_fastcgi\", \"mod_cgi\"";
794
		$cgi_config = "";
795
		$fastcgi_config = <<<EOD
796
#### fastcgi module
797
## read fastcgi.txt for more info
798
fastcgi.server = ( ".php" =>
799
	( "localhost" =>
800
		(
801
			"socket" => "/tmp/php-fastcgi.socket",
802
			"min-procs" => 0,
803
			"max-procs" => {$max_procs},
804
			{$bin_environment}			
805
			"bin-path" => "/usr/local/bin/php"
806
		)
807
	)
808
)
809

    
810
#### CGI module
811
cgi.assign                 = ( ".cgi" => "" )
812

    
813
EOD;
814
	} else {
815
		$fastcgi_config = "";
816
		$module = "\"mod_cgi\"";
817
		$cgi_config = <<<EOD
818
#### CGI module
819
cgi.assign                 = ( ".php"  => "/usr/local/bin/php",
820
                               ".cgi" => "" )
821

    
822
EOD;
823
	}
824

    
825
	$lighty_config .= <<<EOD
826
#
827
# lighttpd configuration file
828
#
829
# use a it as base for lighttpd 1.0.0 and above
830
#
831
############ Options you really have to take care of ####################
832

    
833
## FreeBSD!
834
server.event-handler	= "freebsd-kqueue"
835
server.network-backend 	= "writev"
836

    
837
## modules to load
838
server.modules              =   (
839
									{$captive_portal_module}
840
									"mod_access", "mod_accesslog", "mod_expire", "mod_compress", 
841
									{$module}{$captiveportal}
842
								)
843

    
844
## Unused modules
845
#                               "mod_setenv",
846

    
847
#				"mod_redirect",
848
#                               "mod_rewrite",
849
#                               "mod_ssi",
850
#                               "mod_usertrack",
851
#                               "mod_expire",
852
#                               "mod_secdownload",
853
#                               "mod_rrdtool",
854
#                               "mod_auth",
855
#                               "mod_status",
856
#                               "mod_alias",
857
#                               "mod_proxy",
858
#                               "mod_simple_vhost",
859
#                               "mod_evhost",
860
#                               "mod_userdir",
861
#                               "mod_cgi",
862
#                                "mod_accesslog"
863

    
864
server.max-keep-alive-requests = 15
865
server.max-keep-alive-idle = 30
866

    
867
## a static document-root, for virtual-hosting take look at the
868
## server.virtual-* options
869
server.document-root        = "{$document_root}"
870
{$captive_portal_rewrite}
871

    
872
# Maximum idle time with nothing being written (php downloading)
873
server.max-write-idle = 999
874

    
875
## where to send error-messages to
876
server.errorlog             = "/var/log/lighttpd.error.log"
877

    
878
# files to check for if .../ is requested
879
server.indexfiles           = ( "index.php", "index.html",
880
                                "index.htm", "default.htm" )
881

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

    
935
# Use the "Content-Type" extended attribute to obtain mime type if possible
936
#mimetypes.use-xattr        = "enable"
937

    
938
#### accesslog module
939
#accesslog.filename          = "/dev/null"
940

    
941
## deny access the file-extensions
942
#
943
# ~    is for backupfiles from vi, emacs, joe, ...
944
# .inc is often used for code includes which should in general not be part
945
#      of the document-root
946
url.access-deny             = ( "~", ".inc" )
947

    
948

    
949
######### Options that are good to be but not neccesary to be changed #######
950

    
951
## bind to port (default: 80)
952
server.port                = {$lighty_port}
953

    
954
## error-handler for status 404
955
#server.error-handler-404   = "/error-handler.html"
956
#server.error-handler-404   = "/error-handler.php"
957

    
958
## to help the rc.scripts
959
server.pid-file            = "/var/run/{$pid_file}"
960

    
961
## virtual directory listings
962
server.dir-listing         = "disable"
963

    
964
## enable debugging
965
debug.log-request-header   = "disable"
966
debug.log-response-header  = "disable"
967
debug.log-request-handling = "disable"
968
debug.log-file-not-found   = "disable"
969

    
970
# gzip compression
971
compress.cache-dir = "/tmp/lighttpdcompress/"
972
compress.filetype  = ("text/plain","text/css", "text/xml", "text/javascript" )
973

    
974
{$server_upload_dirs}
975

    
976
{$server_max_request_size}
977

    
978
{$fastcgi_config}
979

    
980
{$cgi_config}
981

    
982
{$captive_portal_mod_evasive}
983

    
984
expire.url = (
985
				"" => "access 50 hours",	
986
        )
987

    
988
EOD;
989

    
990
	$cert = str_replace("\r", "", $cert);
991
	$key = str_replace("\r", "", $key);
992
	$ca = str_replace("\r", "", $ca);
993

    
994
	$cert = str_replace("\n\n", "\n", $cert);
995
	$key = str_replace("\n\n", "\n", $key);
996
	$ca = str_replace("\n\n", "\n", $ca);
997

    
998
	if($cert <> "" and $key <> "") {
999
		$fd = fopen("{$g['varetc_path']}/{$cert_location}", "w");
1000
		if (!$fd) {
1001
			printf("Error: cannot open cert.pem in system_webgui_start().\n");
1002
			return 1;
1003
		}
1004
		chmod("{$g['varetc_path']}/{$cert_location}", 0600);
1005
		fwrite($fd, $cert);
1006
		fwrite($fd, "\n");
1007
		fwrite($fd, $key);
1008
		fclose($fd);
1009
		if($ca <> "") {
1010
			$fd = fopen("{$g['varetc_path']}/{$ca_location}", "w");
1011
			if (!$fd) {
1012
				printf("Error: cannot open ca.pem in system_webgui_start().\n");
1013
				return 1;
1014
			}
1015
			chmod("{$g['varetc_path']}/{$ca_location}", 0600);
1016
			fwrite($fd, $ca);
1017
			fclose($fd);
1018
		}
1019
		$lighty_config .= "\n";
1020
		$lighty_config .= "## ssl configuration\n";
1021
		$lighty_config .= "ssl.engine = \"enable\"\n";
1022
		$lighty_config .= "ssl.pemfile = \"{$g['varetc_path']}/{$cert_location}\"\n\n";
1023
		if($ca <> "")
1024
			$lighty_config .= "ssl.ca-file = \"{$g['varetc_path']}/{$ca_location}\"\n\n";
1025
	}
1026

    
1027
	$fd = fopen("{$filename}", "w");
1028
	if (!$fd) {
1029
		printf("Error: cannot open {$filename} in system_generate_lighty_config().\n");
1030
		return 1;
1031
	}
1032
	fwrite($fd, $lighty_config);
1033
	fclose($fd);
1034

    
1035
	return 0;
1036

    
1037
}
1038

    
1039
function system_timezone_configure() {
1040
	global $config, $g;
1041
	if(isset($config['system']['developerspew'])) {
1042
		$mt = microtime();
1043
		echo "system_timezone_configure() being called $mt\n";
1044
	}
1045

    
1046
	$syscfg = $config['system'];
1047

    
1048
	if ($g['booting'])
1049
		echo "Setting timezone...";
1050

    
1051
	/* extract appropriate timezone file */
1052
	$timezone = $syscfg['timezone'];
1053
	if (!$timezone)
1054
		$timezone = "Etc/UTC";
1055

    
1056
	conf_mount_rw();
1057

    
1058
	exec("LANG=C /usr/bin/tar xzfO /usr/share/zoneinfo.tgz " .
1059
		escapeshellarg($timezone) . " > /etc/localtime");
1060

    
1061
	mwexec("sync");
1062
	conf_mount_ro();
1063

    
1064
	if ($g['booting'])
1065
		echo "done.\n";
1066
}
1067

    
1068
function system_ntp_configure() {
1069
	global $config, $g;
1070

    
1071
	$syscfg = $config['system'];
1072

    
1073
	/* open configuration for wrting or bail */
1074
	$fd = fopen("{$g['varetc_path']}/ntpd.conf","w");
1075
	if(!$fd) {
1076
		log_error("Could not open {$g['varetc_path']}/ntpd.conf for writing");
1077
		return;
1078
	}
1079

    
1080
	fwrite($fd, "# \n");
1081
	fwrite($fd, "# pfSense OpenNTPD configuration file \n");
1082
	fwrite($fd, "# \n\n");
1083

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

    
1088
	/* Setup listener(s) if the user has configured one */
1089
        if ($config['installedpackages']['openntpd']) {
1090
    		/* server config is in coregui1 */
1091
		$xmlsettings = $config['installedpackages']['openntpd']['config'][0];
1092
		if ($xmlsettings['enable'] == 'on') {
1093
			$ifaces = explode(',', $xmlsettings['interface']);
1094
			$ifaces = array_map('convert_friendly_interface_to_real_interface_name', $ifaces);
1095
			$ifaces = array_filter($ifaces, 'does_interface_exist');
1096
			$ips = array_map('find_interface_ip', $ifaces);
1097
			foreach ($ips as $ip) {
1098
				if (is_ipaddr($ip))
1099
					fwrite($fd, "listen on $ip\n");
1100
			}
1101
		}
1102
	}
1103

    
1104
	fwrite($fd, "\n");
1105

    
1106
	/* slurp! */
1107
	fclose($fd);
1108

    
1109
	/* if openntpd is running, kill it */
1110
	while(is_process_running("ntpd")) {
1111
		mwexec("/usr/bin/killall ntpd", true);
1112
	}
1113

    
1114
	/* if /var/empty does not exist, create it */
1115
	if(!is_dir("/var/empty"))
1116
		exec("/bin/mkdir -p /var/empty && chmod ug+rw /var/empty/.");
1117

    
1118
	if($g['booting'])
1119
		return;
1120
	
1121
	/* start opentpd, set time now and use /var/etc/ntpd.conf */
1122
	exec("/usr/local/sbin/ntpd -s -f {$g['varetc_path']}/ntpd.conf");
1123
	
1124
	// Note that we are starting up
1125
	exec("echo 'OpenNTPD is starting up' >> {$g['varlog_path']}/ntpd.log");
1126

    
1127
}
1128

    
1129
function sync_system_time() {
1130
	global $config, $g;
1131

    
1132
	$syscfg = $config['system'];
1133

    
1134
	if ($g['booting'])
1135
		echo "Syncing system time before startup...";
1136

    
1137
	/* foreach through servers and write out to ntpd.conf */
1138
	foreach (explode(' ', $syscfg['timeservers']) as $ts) {
1139
		mwexec("/usr/sbin/ntpdate -s $ts");
1140
	}
1141
	
1142
	if ($g['booting'])
1143
		echo "done.\n";
1144
	
1145
}
1146

    
1147
function system_halt() {
1148
	global $g;
1149

    
1150
	system_reboot_cleanup();
1151

    
1152
	mwexec("/usr/bin/nohup /etc/rc.halt > /dev/null 2>&1 &");
1153
}
1154

    
1155
function system_reboot() {
1156
	global $g;
1157

    
1158
	system_reboot_cleanup();
1159

    
1160
	mwexec("nohup /etc/rc.reboot > /dev/null 2>&1 &");
1161
}
1162

    
1163
function system_reboot_sync() {
1164
	global $g;
1165

    
1166
	system_reboot_cleanup();
1167

    
1168
	mwexec("/etc/rc.reboot > /dev/null 2>&1");
1169
}
1170

    
1171
function system_reboot_cleanup() {
1172
	mwexec("/usr/local/bin/beep.sh stop");
1173
	require_once("captiveportal.inc");
1174
	captiveportal_radius_stop_all();
1175
	require_once("voucher.inc");
1176
	voucher_save_db_to_config();
1177
}
1178

    
1179
function system_do_shell_commands($early = 0) {
1180
	global $config, $g;
1181
	if(isset($config['system']['developerspew'])) {
1182
		$mt = microtime();
1183
		echo "system_do_shell_commands() being called $mt\n";
1184
	}
1185

    
1186
	if ($early)
1187
		$cmdn = "earlyshellcmd";
1188
	else
1189
		$cmdn = "shellcmd";
1190

    
1191
	if (is_array($config['system'][$cmdn])) {
1192

    
1193
		/* *cmd is an array, loop through */
1194
		foreach ($config['system'][$cmdn] as $cmd) {
1195
			exec($cmd);
1196
		}
1197

    
1198
	} elseif($config['system'][$cmdn] <> "") {
1199

    
1200
		/* execute single item */
1201
		exec($config['system'][$cmdn]);
1202

    
1203
	}
1204
}
1205

    
1206
function system_console_configure() {
1207
	global $config, $g;
1208
	if(isset($config['system']['developerspew'])) {
1209
		$mt = microtime();
1210
		echo "system_console_configure() being called $mt\n";
1211
	}
1212

    
1213
	if (isset($config['system']['disableconsolemenu'])) {
1214
		touch("{$g['varetc_path']}/disableconsole");
1215
	} else {
1216
		unlink_if_exists("{$g['varetc_path']}/disableconsole");
1217
	}
1218
}
1219

    
1220
function system_dmesg_save() {
1221
	global $g;
1222
	if(isset($config['system']['developerspew'])) {
1223
		$mt = microtime();
1224
		echo "system_dmesg_save() being called $mt\n";
1225
	}
1226

    
1227
	$dmesg = "";
1228
	exec("/sbin/dmesg", $dmesg);
1229

    
1230
	/* find last copyright line (output from previous boots may be present) */
1231
	$lastcpline = 0;
1232

    
1233
	for ($i = 0; $i < count($dmesg); $i++) {
1234
		if (strstr($dmesg[$i], "Copyright (c) 1992-"))
1235
			$lastcpline = $i;
1236
	}
1237

    
1238
	$fd = fopen("{$g['varlog_path']}/dmesg.boot", "w");
1239
	if (!$fd) {
1240
		printf("Error: cannot open dmesg.boot in system_dmesg_save().\n");
1241
		return 1;
1242
	}
1243

    
1244
	for ($i = $lastcpline; $i < count($dmesg); $i++)
1245
		fwrite($fd, $dmesg[$i] . "\n");
1246

    
1247
	fclose($fd);
1248

    
1249
	return 0;
1250
}
1251

    
1252
function system_set_harddisk_standby() {
1253
	global $g, $config;
1254
	if(isset($config['system']['developerspew'])) {
1255
		$mt = microtime();
1256
		echo "system_set_harddisk_standby() being called $mt\n";
1257
	}
1258

    
1259
	if (isset($config['system']['harddiskstandby'])) {
1260
		if ($g['booting']) {
1261
			echo 'Setting hard disk standby... ';
1262
		}
1263

    
1264
		$standby = $config['system']['harddiskstandby'];
1265
		// Check for a numeric value
1266
		if (is_numeric($standby)) {
1267
			// Sync the disk(s)
1268
			mwexec('/bin/sync');
1269
			if (!mwexec('/sbin/sysctl hw.ata.standby=' . ((int)$standby))) {
1270
				// Reinitialize ATA-drives
1271
				mwexec('/usr/local/sbin/atareinit');
1272
				if ($g['booting']) {
1273
					echo "done.\n";
1274
				}
1275
			} else if ($g['booting']) {
1276
				echo "failed!\n";
1277
			}
1278
		} else if ($g['booting']) {
1279
			echo "failed!\n";
1280
		}
1281
	}
1282
}
1283

    
1284
function system_setup_sysctl() {
1285
	global $config;
1286
	if(isset($config['system']['developerspew'])) {
1287
		$mt = microtime();
1288
		echo "system_setup_sysctl() being called $mt\n";
1289
	}
1290

    
1291
	activate_sysctls();	
1292

    
1293
	if (isset($config['system']['sharednet'])) {
1294
		system_disable_arp_wrong_if();
1295
	}
1296
}
1297

    
1298
function system_disable_arp_wrong_if() {
1299
	global $config;
1300
	if(isset($config['system']['developerspew'])) {
1301
		$mt = microtime();
1302
		echo "system_disable_arp_wrong_if() being called $mt\n";
1303
	}
1304
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_wrong_iface=0");
1305
	mwexec("/sbin/sysctl -n net.link.ether.inet.log_arp_movements=0");
1306
}
1307

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

    
1318
function enable_watchdog() {
1319
	global $config;
1320
	return;
1321
	$install_watchdog = false;
1322
	$supported_watchdogs = array("Geode");
1323
	$file = file_get_contents("/var/log/dmesg.boot");
1324
	foreach($supported_watchdogs as $sd) {
1325
		if(stristr($file, "Geode")) {
1326
			$install_watchdog = true;
1327
		}
1328
	}
1329
	if($install_watchdog == true) {
1330
		if(is_process_running("watchdogd"))
1331
			mwexec("/usr/bin/killall watchdogd", true);
1332
		exec("/usr/sbin/watchdogd");
1333
	}
1334
}
1335

    
1336
?>
(40-40/51)