Project

General

Profile

Download (60.7 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/pfsense-utils
3
 * NAME
4
 *   pfsense-utils.inc - Utilities specific to pfSense
5
 * DESCRIPTION
6
 *   This include contains various pfSense specific functions.
7
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11
 * Copyright (C) 2004-2007 Scott Ullrich (sullrich@gmail.com)
12
 * All rights reserved.
13
 * Redistribution and use in source and binary forms, with or without
14
 * modification, are permitted provided that the following conditions are met:
15
 *
16
 * 1. Redistributions of source code must retain the above copyright notice,
17
 * this list of conditions and the following disclaimer.
18
 *
19
 * 2. Redistributions in binary form must reproduce the above copyright
20
 * notice, this list of conditions and the following disclaimer in the
21
 * documentation and/or other materials provided with the distribution.
22
 *
23
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
25
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26
 * AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
27
 * OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31
 * RISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32
 * POSSIBILITY OF SUCH DAMAGE.
33
 *
34
 */
35

    
36
/*
37
	pfSense_BUILDER_BINARIES:	/sbin/sysctl	/sbin/ifconfig	/sbin/pfctl	/usr/local/bin/php /usr/bin/netstat
38
	pfSense_BUILDER_BINARIES:	/bin/df	/usr/bin/grep	/usr/bin/awk	/bin/rm	/usr/sbin/pwd_mkdb	/usr/bin/host
39
	pfSense_BUILDER_BINARIES:	/sbin/kldload
40
	pfSense_MODULE:	utils
41
*/
42

    
43
/****f* pfsense-utils/have_natonetooneruleint_access
44
 * NAME
45
 *   have_natonetooneruleint_access
46
 * INPUTS
47
 *	 none
48
 * RESULT
49
 *   returns true if user has access to edit a specific firewall nat one to one interface
50
 ******/
51
function have_natonetooneruleint_access($if) {
52
	$security_url = "firewall_nat_1to1_edit.php?if=". strtolower($if);
53
	if(isAllowedPage($security_url, $_SESSION['Username'])) 
54
		return true;
55
	return false;
56
}
57

    
58
/****f* pfsense-utils/have_natpfruleint_access
59
 * NAME
60
 *   have_natpfruleint_access
61
 * INPUTS
62
 *	 none
63
 * RESULT
64
 *   returns true if user has access to edit a specific firewall nat port forward interface
65
 ******/
66
function have_natpfruleint_access($if) {
67
	$security_url = "firewall_nat_edit.php?if=". strtolower($if);
68
	if(isAllowedPage($security_url, $allowed)) 
69
		return true;
70
	return false;
71
}
72

    
73
/****f* pfsense-utils/have_ruleint_access
74
 * NAME
75
 *   have_ruleint_access
76
 * INPUTS
77
 *	 none
78
 * RESULT
79
 *   returns true if user has access to edit a specific firewall interface
80
 ******/
81
function have_ruleint_access($if) {
82
	$security_url = "firewall_rules.php?if=". strtolower($if);
83
	if(isAllowedPage($security_url)) 
84
		return true;
85
	return false;
86
}
87

    
88
/****f* pfsense-utils/does_url_exist
89
 * NAME
90
 *   does_url_exist
91
 * INPUTS
92
 *	 none
93
 * RESULT
94
 *   returns true if a url is available
95
 ******/
96
function does_url_exist($url) {
97
	$fd = fopen("$url","r");
98
	if($fd) {
99
		fclose($fd);
100
   		return true;    
101
	} else {
102
        return false;
103
	}
104
}
105

    
106
/****f* pfsense-utils/is_private_ip
107
 * NAME
108
 *   is_private_ip
109
 * INPUTS
110
 *	 none
111
 * RESULT
112
 *   returns true if an ip address is in a private range
113
 ******/
114
function is_private_ip($iptocheck) {
115
        $isprivate = false;
116
        $ip_private_list=array(
117
               "10.0.0.0/8",
118
               "172.16.0.0/12",
119
               "192.168.0.0/16",
120
               "99.0.0.0/8"
121
        );
122
        foreach($ip_private_list as $private) {
123
                if(ip_in_subnet($iptocheck,$private)==true)
124
                        $isprivate = true;
125
        }
126
        return $isprivate;
127
}
128

    
129
/****f* pfsense-utils/get_tmp_file
130
 * NAME
131
 *   get_tmp_file
132
 * INPUTS
133
 *	 none
134
 * RESULT
135
 *   returns a temporary filename
136
 ******/
137
function get_tmp_file() {
138
	global $g;
139
	return "{$g['tmp_path']}/tmp-" . time();
140
}
141

    
142
/****f* pfsense-utils/get_dns_servers
143
 * NAME
144
 *   get_dns_servres - get system dns servers
145
 * INPUTS
146
 *   $dns_servers - an array of the dns servers
147
 * RESULT
148
 *   null
149
 ******/
150
function get_dns_servers() {
151
	$dns_servers = array();
152
	$dns = `cat /etc/resolv.conf`;
153
	$dns_s = split("\n", $dns);
154
	foreach($dns_s as $dns) {
155
		$matches = "";
156
		if (preg_match("/nameserver (.*)/", $dns, $matches))
157
			$dns_servers[] = $matches[1];
158
	}
159
	$dns_server_master = array();
160
	$lastseen = "";
161
	foreach($dns_servers as $t) {
162
		if($t <> $lastseen)
163
			if($t <> "")
164
				$dns_server_master[] = $t;
165
		$lastseen = $t;
166
	}
167
	return $dns_server_master;
168
}
169

    
170
/****f* pfsense-utils/enable_hardware_offloading
171
 * NAME
172
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
173
 * INPUTS
174
 *   $interface	- string containing the physical interface to work on.
175
 * RESULT
176
 *   null
177
 * NOTES
178
 *   This function only supports the fxp driver's loadable microcode.
179
 ******/
180
function enable_hardware_offloading($interface) {
181
	global $g, $config;
182

    
183
	if(isset($config['system']['do_not_use_nic_microcode']))
184
		return;
185

    
186
	/* translate wan, lan, opt -> real interface if needed */
187
	$int = get_real_interface($interface);
188
	if(empty($int)) 
189
		return;
190
	$int_family = preg_split("/[0-9]+/", $int);
191
	$supported_ints = array('fxp');
192
	if (in_array($int_family, $supported_ints)) {
193
		if(does_interface_exist($int)) 
194
			pfSense_interface_flags($int, IFF_LINK0);
195
	}
196

    
197
	return;
198
}
199

    
200
/****f* pfsense-utils/interface_supports_polling
201
 * NAME
202
 *   checks to see if an interface supports polling according to man polling
203
 * INPUTS
204
 *
205
 * RESULT
206
 *   true or false
207
 * NOTES
208
 *
209
 ******/
210
function interface_supports_polling($iface) {
211
	$opts = pfSense_get_interface_addresses($iface);
212
	if (is_array($opts) && isset($opts['caps']['polling']))
213
		return true;
214

    
215
	return false;
216
}
217

    
218
/****f* pfsense-utils/is_alias_inuse
219
 * NAME
220
 *   checks to see if an alias is currently in use by a rule
221
 * INPUTS
222
 *
223
 * RESULT
224
 *   true or false
225
 * NOTES
226
 *
227
 ******/
228
function is_alias_inuse($alias) {
229
	global $g, $config;
230

    
231
	if($alias == "") return false;
232
	/* loop through firewall rules looking for alias in use */
233
	if(is_array($config['filter']['rule']))
234
		foreach($config['filter']['rule'] as $rule) {
235
			if($rule['source']['address'])
236
				if($rule['source']['address'] == $alias)
237
					return true;
238
			if($rule['destination']['address'])
239
				if($rule['destination']['address'] == $alias)
240
					return true;
241
		}
242
	/* loop through nat rules looking for alias in use */
243
	if(is_array($config['nat']['rule']))
244
		foreach($config['nat']['rule'] as $rule) {
245
			if($rule['target'] && $rule['target'] == $alias)
246
				return true;
247
			if($rule['source']['address'] && $rule['source']['address'] == $alias)
248
				return true;
249
			if($rule['destination']['address'] && $rule['destination']['address'] == $alias)
250
				return true;
251
		}
252
	return false;
253
}
254

    
255
/****f* pfsense-utils/is_schedule_inuse
256
 * NAME
257
 *   checks to see if a schedule is currently in use by a rule
258
 * INPUTS
259
 *
260
 * RESULT
261
 *   true or false
262
 * NOTES
263
 *
264
 ******/
265
function is_schedule_inuse($schedule) {
266
	global $g, $config;
267

    
268
	if($schedule == "") return false;
269
	/* loop through firewall rules looking for schedule in use */
270
	if(is_array($config['filter']['rule']))
271
		foreach($config['filter']['rule'] as $rule) {
272
			if($rule['sched'] == $schedule)
273
				return true;
274
		}
275
	return false;
276
}
277

    
278
/****f* pfsense-utils/setup_polling
279
 * NAME
280
 *   sets up polling
281
 * INPUTS
282
 *
283
 * RESULT
284
 *   null
285
 * NOTES
286
 *
287
 ******/
288
function setup_polling() {
289
	global $g, $config;
290

    
291
	setup_polling_defaults();
292

    
293
	if (isset($config['system']['polling']))
294
		mwexec("/sbin/sysctl kern.polling.idle_poll=1");
295
	else
296
		mwexec("/sbin/sysctl kern.polling.idle_poll=0");
297

    
298
	if($config['system']['polling_each_burst'])
299
		mwexec("/sbin/sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}");
300
	if($config['system']['polling_burst_max'])
301
		mwexec("/sbin/sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}");
302
	if($config['system']['polling_user_frac'])
303
		mwexec("/sbin/sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}");
304
}
305

    
306
/****f* pfsense-utils/setup_microcode
307
 * NAME
308
 *   enumerates all interfaces and calls enable_hardware_offloading which
309
 *   enables a NIC's supported hardware features.
310
 * INPUTS
311
 *
312
 * RESULT
313
 *   null
314
 * NOTES
315
 *   This function only supports the fxp driver's loadable microcode.
316
 ******/
317
function setup_microcode() {
318

    
319
	/* if list */
320
	$ifs = get_interface_arr();
321

    
322
	foreach($ifs as $if)
323
		enable_hardware_offloading($if);
324
}
325

    
326
/****f* pfsense-utils/get_carp_status
327
 * NAME
328
 *   get_carp_status - Return whether CARP is enabled or disabled.
329
 * RESULT
330
 *   boolean	- true if CARP is enabled, false if otherwise.
331
 ******/
332
function get_carp_status() {
333
    /* grab the current status of carp */
334
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
335
    if(intval($status) == "0") return false;
336
    return true;
337
}
338

    
339
/*
340
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
341

    
342
 */
343
function convert_ip_to_network_format($ip, $subnet) {
344
	$ipsplit = split('[.]', $ip);
345
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
346
	return $string;
347
}
348

    
349
/*
350
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
351
 */
352
function get_carp_interface_status($carpinterface) {
353
	/* basically cache the contents of ifconfig statement
354
	to speed up this routine */
355
	global $carp_query;
356
	if($carp_query == "")
357
		$carp_query = split("\n", `/sbin/ifconfig $carpinterface | grep carp`);
358
	foreach($carp_query as $int) {
359
		if(stristr($int, "MASTER")) 
360
			return "MASTER";
361
		if(stristr($int, "BACKUP")) 
362
			return "BACKUP";
363
		if(stristr($int, "INIT")) 
364
			return "INIT";
365
	}
366
	return;
367
}
368

    
369
/*
370
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
371
 */
372
function get_pfsync_interface_status($pfsyncinterface) {
373
    $result = does_interface_exist($pfsyncinterface);
374
    if($result <> true) return;
375
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
376
    return $status;
377
}
378

    
379
/*
380
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
381
 */
382
function add_rule_to_anchor($anchor, $rule, $label) {
383
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
384
}
385

    
386
/*
387
 * remove_text_from_file
388
 * remove $text from file $file
389
 */
390
function remove_text_from_file($file, $text) {
391
	global $fd_log;
392
	if($fd_log)
393
		fwrite($fd_log, "Adding needed text items:\n");
394
	$filecontents = file_get_contents($file);
395
	$textTMP = str_replace($text, "", $filecontents);
396
	$text = $textTMP;
397
	if($fd_log)
398
		fwrite($fd_log, $text);
399
	$fd = fopen($file, "w");
400
	fwrite($fd, $text);
401
	fclose($fd);
402
}
403

    
404
/*
405
 * add_text_to_file($file, $text): adds $text to $file.
406
 * replaces the text if it already exists.
407
 */
408
function add_text_to_file($file, $text, $replace = false) {
409
	if(file_exists($file) and is_writable($file)) {
410
		$filecontents = file($file);
411
		$fout = fopen($file, "w");
412

    
413
		$filecontents = array_map('rtrim', $filecontents);
414
		array_push($filecontents, $text);
415
		if ($replace)
416
			$filecontents = array_unique($filecontents);
417

    
418
		$file_text = implode("\n", $filecontents);
419

    
420
		fwrite($fout, $file_text);
421
		fclose($fout);
422
		return true;
423
	} else {
424
		return false;
425
	}
426
}
427

    
428
/*
429
 *   after_sync_bump_adv_skew(): create skew values by 1S
430
 */
431
function after_sync_bump_adv_skew() {
432
	global $config, $g;
433
	$processed_skew = 1;
434
	$a_vip = &$config['virtualip']['vip'];
435
	foreach ($a_vip as $vipent) {
436
		if($vipent['advskew'] <> "") {
437
			$processed_skew = 1;
438
			$vipent['advskew'] = $vipent['advskew']+1;
439
		}
440
	}
441
	if($processed_skew == 1)
442
		write_config("After synch increase advertising skew");
443
}
444

    
445
/*
446
 * get_filename_from_url($url): converts a url to its filename.
447
 */
448
function get_filename_from_url($url) {
449
	return basename($url);
450
}
451

    
452
/*
453
 *   get_dir: return an array of $dir
454
 */
455
function get_dir($dir) {
456
	$dir_array = array();
457
	$d = dir($dir);
458
	while (false !== ($entry = $d->read())) {
459
		array_push($dir_array, $entry);
460
	}
461
	$d->close();
462
	return $dir_array;
463
}
464

    
465
/****f* pfsense-utils/WakeOnLan
466
 * NAME
467
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
468
 * RESULT
469
 *   true/false - true if the operation was successful
470
 ******/
471
function WakeOnLan($addr, $mac)
472
{
473
	$addr_byte = explode(':', $mac);
474
	$hw_addr = '';
475

    
476
	for ($a=0; $a < 6; $a++)
477
		$hw_addr .= chr(hexdec($addr_byte[$a]));
478

    
479
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
480

    
481
	for ($a = 1; $a <= 16; $a++)
482
		$msg .= $hw_addr;
483

    
484
	// send it to the broadcast address using UDP
485
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
486
	if ($s == false) {
487
		log_error("Error creating socket!");
488
		log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
489
	} else {
490
		// setting a broadcast option to socket:
491
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
492
		if($opt_ret < 0)
493
			log_error("setsockopt() failed, error: " . strerror($opt_ret));
494
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
495
		socket_close($s);
496
		log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}");
497
		return true;
498
	}
499

    
500
	return false;
501
}
502

    
503
/*
504
 * gather_altq_queue_stats():  gather altq queue stats and return an array that
505
 *                             is queuename|qlength|measured_packets
506
 *                             NOTE: this command takes 5 seconds to run
507
 */
508
function gather_altq_queue_stats($dont_return_root_queues) {
509
	exec("/sbin/pfctl -vvsq", $stats_array);
510
	$queue_stats = array();
511
	foreach ($stats_array as $stats_line) {
512
		$match_array = "";
513
		if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
514
			$queue_name = $match_array[1][0];
515
		if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
516
			$speed = $match_array[1][0];
517
		if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
518
			$borrows = $match_array[1][0];
519
		if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
520
			$suspends = $match_array[1][0];
521
		if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
522
			$drops = $match_array[1][0];
523
		if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
524
			$measured = $match_array[1][0];
525
			if($dont_return_root_queues == true)
526
				if(stristr($queue_name,"root_") == false)
527
					array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
528
		}
529
	}
530
	return $queue_stats;
531
}
532

    
533
/*
534
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
535
 *					 Useful for finding paths and stripping file extensions.
536
 */
537
function reverse_strrchr($haystack, $needle) {
538
	if (!is_string($haystack))
539
		return;
540
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
541
}
542

    
543
/*
544
 *  backup_config_section($section): returns as an xml file string of
545
 *                                   the configuration section
546
 */
547
function backup_config_section($section) {
548
	global $config;
549
	$new_section = &$config[$section];
550
	/* generate configuration XML */
551
	$xmlconfig = dump_xml_config($new_section, $section);
552
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
553
	return $xmlconfig;
554
}
555

    
556
/*
557
 *  restore_config_section($section, new_contents): restore a configuration section,
558
 *                                                  and write the configuration out
559
 *                                                  to disk/cf.
560
 */
561
function restore_config_section($section, $new_contents) {
562
	global $config, $g;
563
	conf_mount_rw();
564
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
565
	fwrite($fout, $new_contents);
566
	fclose($fout);
567
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
568
	if ($section_xml != -1)
569
		$config[$section] = &$section_xml;
570
	@unlink($g['tmp_path'] . "/tmpxml");
571
	if(file_exists("{$g['tmp_path']}/config.cache"))
572
		unlink("{$g['tmp_path']}/config.cache");
573
	write_config("Restored {$section} of config file (maybe from CARP partner)");
574
	conf_mount_ro();
575
	return;
576
}
577

    
578
/*
579
 *  merge_config_section($section, new_contents):   restore a configuration section,
580
 *                                                  and write the configuration out
581
 *                                                  to disk/cf.  But preserve the prior
582
 * 													structure if needed
583
 */
584
function merge_config_section($section, $new_contents) {
585
	global $config;
586
	conf_mount_rw();
587
	$fname = get_tmp_filename();
588
	$fout = fopen($fname, "w");
589
	fwrite($fout, $new_contents);
590
	fclose($fout);
591
	$section_xml = parse_xml_config($fname, $section);
592
	$config[$section] = $section_xml;
593
	unlink($fname);
594
	write_config("Restored {$section} of config file (maybe from CARP partner)");
595
	conf_mount_ro();
596
	return;
597
}
598

    
599
/*
600
 * http_post($server, $port, $url, $vars): does an http post to a web server
601
 *                                         posting the vars array.
602
 * written by nf@bigpond.net.au
603
 */
604
function http_post($server, $port, $url, $vars) {
605
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
606
	$urlencoded = "";
607
	while (list($key,$value) = each($vars))
608
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
609
	$urlencoded = substr($urlencoded,0,-1);
610
	$content_length = strlen($urlencoded);
611
	$headers = "POST $url HTTP/1.1
612
Accept: */*
613
Accept-Language: en-au
614
Content-Type: application/x-www-form-urlencoded
615
User-Agent: $user_agent
616
Host: $server
617
Connection: Keep-Alive
618
Cache-Control: no-cache
619
Content-Length: $content_length
620

    
621
";
622

    
623
	$errno = "";
624
	$errstr = "";
625
	$fp = fsockopen($server, $port, $errno, $errstr);
626
	if (!$fp) {
627
		return false;
628
	}
629

    
630
	fputs($fp, $headers);
631
	fputs($fp, $urlencoded);
632

    
633
	$ret = "";
634
	while (!feof($fp))
635
		$ret.= fgets($fp, 1024);
636
	fclose($fp);
637

    
638
	return $ret;
639
}
640

    
641
/*
642
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
643
 */
644
if (!function_exists('php_check_syntax')){
645
	global $g;
646
	function php_check_syntax($code_to_check, &$errormessage){
647
		return false;
648
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
649
		$code = $_POST['content'];
650
		$code = str_replace("<?php", "", $code);
651
		$code = str_replace("?>", "", $code);
652
		fwrite($fout, "<?php\n\n");
653
		fwrite($fout, $code_to_check);
654
		fwrite($fout, "\n\n?>\n");
655
		fclose($fout);
656
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
657
		$output = exec_command($command);
658
		if (stristr($output, "Errors parsing") == false) {
659
			echo "false\n";
660
			$errormessage = '';
661
			return(false);
662
		} else {
663
			$errormessage = $output;
664
			return(true);
665
		}
666
	}
667
}
668

    
669
/*
670
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
671
 */
672
if (!function_exists('php_check_syntax')){
673
	function php_check_syntax($code_to_check, &$errormessage){
674
		return false;
675
		$command = "/usr/local/bin/php -l " . $code_to_check;
676
		$output = exec_command($command);
677
		if (stristr($output, "Errors parsing") == false) {
678
			echo "false\n";
679
			$errormessage = '';
680
			return(false);
681
		} else {
682
			$errormessage = $output;
683
			return(true);
684
		}
685
	}
686
}
687

    
688
/*
689
 * rmdir_recursive($path,$follow_links=false)
690
 * Recursively remove a directory tree (rm -rf path)
691
 * This is for directories _only_
692
 */
693
function rmdir_recursive($path,$follow_links=false) {
694
	$to_do = glob($path);
695
	if(!is_array($to_do)) $to_do = array($to_do);
696
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
697
		if(file_exists($workingdir)) {
698
			if(is_dir($workingdir)) {
699
				$dir = opendir($workingdir);
700
				while ($entry = readdir($dir)) {
701
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
702
						unlink("$workingdir/$entry");
703
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
704
						rmdir_recursive("$workingdir/$entry");
705
				}
706
				closedir($dir);
707
				rmdir($workingdir);
708
			} elseif (is_file($workingdir)) {
709
				unlink($workingdir);
710
			}
711
               	}
712
	}
713
	return;
714
}
715

    
716
/*
717
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
718
 */
719
function call_pfsense_method($method, $params, $timeout = 0) {
720
	global $g, $config;
721

    
722
	$ip = gethostbyname($g['product_website']);
723
	if($ip == $g['product_website'])
724
		return false;
725

    
726
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
727
	$xmlrpc_path = $g['xmlrpcpath'];
728
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
729
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
730
	// If the ALT PKG Repo has a username/password set, use it.
731
	if($config['system']['altpkgrepo']['username'] && 
732
	   $config['system']['altpkgrepo']['password']) {
733
		$username = $config['system']['altpkgrepo']['username'];
734
		$password = $config['system']['altpkgrepo']['password'];
735
		$cli->setCredentials($username, $password);
736
	}
737
	$resp = $cli->send($msg, $timeout);
738
	if(!$resp) {
739
		log_error("XMLRPC communication error: " . $cli->errstr);
740
		return false;
741
	} elseif($resp->faultCode()) {
742
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
743
		return false;
744
	} else {
745
		return XML_RPC_Decode($resp->value());
746
	}
747
}
748

    
749
/*
750
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
751
 */
752
function check_firmware_version($tocheck = "all", $return_php = true) {
753
	global $g, $config;
754
	$ip = gethostbyname($g['product_website']);
755
	if($ip == $g['product_website'])
756
		return false;
757
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
758
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
759
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
760
		"platform" => trim(file_get_contents('/etc/platform'))
761
		);
762
	if($tocheck == "all") {
763
		$params = $rawparams;
764
	} else {
765
		foreach($tocheck as $check) {
766
			$params['check'] = $rawparams['check'];
767
			$params['platform'] = $rawparams['platform'];
768
		}
769
	}
770
	if($config['system']['firmware']['branch']) {
771
		$params['branch'] = $config['system']['firmware']['branch'];
772
	}
773
	if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) {
774
		return false;
775
	} else {
776
		$versions["current"] = $params;
777
	}
778
	return $versions;
779
}
780

    
781
function get_disk_info() {
782
	$diskout = "";
783
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
784
	return explode(' ', $diskout[0]);
785
	// $size, $used, $avail, $cap
786
}
787

    
788
/****f* pfsense-utils/strncpy
789
 * NAME
790
 *   strncpy - copy strings
791
 * INPUTS
792
 *   &$dst, $src, $length
793
 * RESULT
794
 *   none
795
 ******/
796
function strncpy(&$dst, $src, $length) {
797
	if (strlen($src) > $length) {
798
		$dst = substr($src, 0, $length);
799
	} else {
800
		$dst = $src;
801
	}
802
}
803

    
804
/****f* pfsense-utils/reload_interfaces_sync
805
 * NAME
806
 *   reload_interfaces - reload all interfaces
807
 * INPUTS
808
 *   none
809
 * RESULT
810
 *   none
811
 ******/
812
function reload_interfaces_sync() {
813
	global $config, $g;
814

    
815
	/* XXX: Use locks?! */
816
	if (file_exists("{$g['tmp_path']}/reloading_all")) {
817
		log_error("WARNING: Recursive call to interfaces sync!");
818
		return;
819
	}
820
	touch("{$g['tmp_path']}/reloading_all");
821

    
822
	if($g['debug'])
823
		log_error("reload_interfaces_sync() is starting.");
824

    
825
	/* parse config.xml again */
826
	$config = parse_config(true);
827

    
828
	/* enable routing */
829
	system_routing_enable();
830
	if($g['debug'])
831
		log_error("Enabling system routing");
832

    
833
	if($g['debug'])
834
		log_error("Cleaning up Interfaces");
835

    
836
	/* set up interfaces */
837
	interfaces_configure();
838

    
839
	/* remove reloading_all trigger */
840
	if($g['debug'])
841
		log_error("Removing {$g['tmp_path']}/reloading_all");
842

    
843
	/* start devd back up */
844
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
845
}
846

    
847
/****f* pfsense-utils/reload_all
848
 * NAME
849
 *   reload_all - triggers a reload of all settings
850
 *   * INPUTS
851
 *   none
852
 * RESULT
853
 *   none
854
 ******/
855
function reload_all() {
856
	global $g;
857
	touch("{$g['tmp_path']}/reload_all");
858
}
859

    
860
/****f* pfsense-utils/reload_interfaces
861
 * NAME
862
 *   reload_interfaces - triggers a reload of all interfaces
863
 * INPUTS
864
 *   none
865
 * RESULT
866
 *   none
867
 ******/
868
function reload_interfaces() {
869
	global $g;
870
	touch("{$g['tmp_path']}/reload_interfaces");
871
}
872

    
873
/****f* pfsense-utils/reload_all_sync
874
 * NAME
875
 *   reload_all - reload all settings
876
 *   * INPUTS
877
 *   none
878
 * RESULT
879
 *   none
880
 ******/
881
function reload_all_sync() {
882
	global $config, $g;
883

    
884
	$g['booting'] = false;
885

    
886
	/* XXX: Use locks?! */
887
        if (file_exists("{$g['tmp_path']}/reloading_all")) {
888
                log_error("WARNING: Recursive call to reload all sync!");
889
                return;
890
        }
891
	touch("{$g['tmp_path']}/reloading_all");
892

    
893
	/* parse config.xml again */
894
	$config = parse_config(true);
895

    
896
	/* set up our timezone */
897
	system_timezone_configure();
898

    
899
	/* set up our hostname */
900
	system_hostname_configure();
901

    
902
	/* make hosts file */
903
	system_hosts_generate();
904

    
905
	/* generate resolv.conf */
906
	system_resolvconf_generate();
907

    
908
	/* enable routing */
909
	system_routing_enable();
910

    
911
	/* set up interfaces */
912
	interfaces_configure();
913

    
914
	/* start dyndns service */
915
	services_dyndns_configure();
916

    
917
	/* configure cron service */
918
	configure_cron();
919

    
920
	/* start the NTP client */
921
	system_ntp_configure();
922

    
923
	/* sync pw database */
924
	conf_mount_rw();
925
	unlink_if_exists("/etc/spwd.db.tmp");
926
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
927
	conf_mount_ro();
928

    
929
	/* restart sshd */
930
	@touch("{$g['tmp_path']}/start_sshd");
931

    
932
	/* restart webConfigurator if needed */
933
	touch("{$g['tmp_path']}/restart_webgui");
934

    
935
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
936
}
937

    
938
function auto_login() {
939
	global $config;
940

    
941
	if(isset($config['system']['disableconsolemenu']))
942
		$status = false;
943
	else
944
		$status = true;
945

    
946
	$gettytab = file_get_contents("/etc/gettytab");
947
	$getty_split = split("\n", $gettytab);
948
	conf_mount_rw();
949
	$fd = false;
950
	$tries = 0;
951
	while (!$fd && $tries < 100) {
952
		$fd = fopen("/etc/gettytab", "w");
953
		$tries++;
954
		
955
	}
956
	if (!$fd) {
957
		conf_mount_ro();
958
		log_error("Enabling auto login was not possible.");
959
		return;
960
	}
961
	foreach($getty_split as $gs) {
962
		if(stristr($gs, ":ht:np:sp#115200") ) {
963
			if($status == true) {
964
				fwrite($fd, "	:ht:np:sp#115200:al=root:\n");
965
			} else {
966
				fwrite($fd, "	:ht:np:sp#115200:\n");
967
			}
968
		} else {
969
			fwrite($fd, "{$gs}\n");
970
		}
971
	}
972
	fclose($fd);
973
	conf_mount_ro();
974
}
975

    
976
function setup_serial_port() {
977
	global $g, $config;
978
	conf_mount_rw();
979
	/* serial console - write out /boot.config */
980
	if(file_exists("/boot.config"))
981
		$boot_config = file_get_contents("/boot.config");
982
	else
983
		$boot_config = "";
984

    
985
	if($g['platform'] <> "cdrom") {
986
		$boot_config_split = split("\n", $boot_config);
987
		$fd = fopen("/boot.config","w");
988
		if($fd) {
989
			foreach($boot_config_split as $bcs) {
990
				if(stristr($bcs, "-D")) {
991
					/* DONT WRITE OUT, WE'LL DO IT LATER */
992
				} else {
993
					if($bcs <> "")
994
						fwrite($fd, "{$bcs}\n");
995
				}
996
			}
997
			if(isset($config['system']['enableserial'])) {
998
				fwrite($fd, "-D");
999
			}
1000
			fclose($fd);
1001
		}
1002
		/* serial console - write out /boot/loader.conf */
1003
		$boot_config = file_get_contents("/boot/loader.conf");
1004
		$boot_config_split = split("\n", $boot_config);
1005
		$fd = fopen("/boot/loader.conf","w");
1006
		if($fd) {
1007
			foreach($boot_config_split as $bcs) {
1008
				if(stristr($bcs, "console")) {
1009
					/* DONT WRITE OUT, WE'LL DO IT LATER */
1010
				} else {
1011
					if($bcs <> "")
1012
						fwrite($fd, "{$bcs}\n");
1013
				}
1014
			}
1015
			if(isset($config['system']['enableserial'])) {
1016
				fwrite($fd, "console=\"comconsole\"\n");
1017
			}
1018
			fclose($fd);
1019
		}
1020
	}
1021
	$ttys = file_get_contents("/etc/ttys");
1022
	$ttys_split = split("\n", $ttys);
1023
	$fd = fopen("/etc/ttys", "w");
1024
	foreach($ttys_split as $tty) {
1025
		if(stristr($tty, "ttyd0") or stristr($tty, "ttyu0")) {
1026
			if(isset($config['system']['enableserial'])) {
1027
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	on	secure\n");
1028
			} else {
1029
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	off	secure\n");
1030
			}
1031
		} else {
1032
			fwrite($fd, $tty . "\n");
1033
		}
1034
	}
1035
	fclose($fd);
1036
	auto_login();
1037

    
1038
	conf_mount_ro();
1039
	return;
1040
}
1041

    
1042
function print_value_list($list, $count = 10, $separator = ",") {
1043
	$list = implode($separator, array_slice($list, 0, $count));
1044
	if(count($list) < $count) {
1045
		$list .= ".";
1046
	} else {
1047
		$list .= "...";
1048
	}
1049
	return $list;
1050
}
1051

    
1052
/* DHCP enabled on any interfaces? */
1053
function is_dhcp_server_enabled() 
1054
{
1055
	global $config;
1056

    
1057
	$dhcpdenable = false;
1058
	
1059
	if (!is_array($config['dhcpd']))
1060
		return false;
1061

    
1062
	$Iflist = get_configured_interface_list();
1063

    
1064
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1065
		if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
1066
			$dhcpdenable = true;
1067
			break;
1068
		}
1069
	}
1070

    
1071
	return $dhcpdenable;
1072
}
1073

    
1074
function convert_seconds_to_hms($sec){
1075
	$min=$hrs=0;
1076
	if ($sec != 0){
1077
		$min = floor($sec/60);
1078
		$sec %= 60;
1079
	}
1080
	if ($min != 0){
1081
		$hrs = floor($min/60);
1082
		$min %= 60;
1083
	}
1084
	if ($sec < 10)
1085
		$sec = "0".$sec;
1086
	if ($min < 10)
1087
		$min = "0".$min;
1088
	if ($hrs < 10)
1089
		$hrs = "0".$hrs;
1090
	$result = $hrs.":".$min.":".$sec;
1091
	return $result;
1092
}
1093

    
1094
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1095

    
1096
function get_ppp_uptime($port){
1097
	if (file_exists("/conf/{$port}.log")){
1098
    	$saved_time = file_get_contents("/conf/{$port}.log");
1099
    	$uptime_data = explode("\n",$saved_time);
1100
		$sec=0;
1101
		foreach($uptime_data as $upt) {
1102
			$sec += substr($upt, 1 + strpos($upt, " "));
1103
 		}
1104
		return convert_seconds_to_hms($sec);
1105
	} else {
1106
		$total_time = "No history data found!";
1107
		return $total_time;
1108
	}
1109
}
1110

    
1111
//returns interface information
1112
function get_interface_info($ifdescr) {
1113
	global $config, $g;
1114

    
1115
	$ifinfo = array();
1116
	if (empty($config['interfaces'][$ifdescr]))
1117
		return;
1118
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1119
	$ifinfo['if'] = get_real_interface($ifdescr);
1120

    
1121
	$chkif = $ifinfo['if'];
1122
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1123
	$ifinfo['status'] = $ifinfotmp['status'];
1124
	if (empty($ifinfo['status']))
1125
                $ifinfo['status'] = "down";
1126
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1127
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1128
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1129
	if (isset($ifinfotmp['link0']))
1130
		$link0 = "down";
1131
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1132
        $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1133
        $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1134
        $ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1135
        $ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1136
        $ifinfo['collisions'] = $ifinfotmp['collisions'];
1137

    
1138
	/* Use pfctl for non wrapping 64 bit counters */
1139
	/* Pass */
1140
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1141
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1142
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1143
	$in4_pass = $pf_in4_pass[5];
1144
	$out4_pass = $pf_out4_pass[5];
1145
	$in4_pass_packets = $pf_in4_pass[3];
1146
	$out4_pass_packets = $pf_out4_pass[3];
1147
	$ifinfo['inbytespass'] = $in4_pass;
1148
	$ifinfo['outbytespass'] = $out4_pass;
1149
	$ifinfo['inpktspass'] = $in4_pass_packets;
1150
	$ifinfo['outpktspass'] = $out4_pass_packets;
1151

    
1152
	/* Block */
1153
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1154
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1155
	$in4_block = $pf_in4_block[5];
1156
	$out4_block = $pf_out4_block[5];
1157
	$in4_block_packets = $pf_in4_block[3];
1158
	$out4_block_packets = $pf_out4_block[3];
1159
	$ifinfo['inbytesblock'] = $in4_block;
1160
	$ifinfo['outbytesblock'] = $out4_block;
1161
	$ifinfo['inpktsblock'] = $in4_block_packets;
1162
	$ifinfo['outpktsblock'] = $out4_block_packets;
1163

    
1164
	$ifinfo['inbytes'] = $in4_pass + $in4_block;
1165
	$ifinfo['outbytes'] = $out4_pass + $out4_block;
1166
	$ifinfo['inpkts'] = $in4_pass_packets + $in4_block_packets;
1167
	$ifinfo['outpkts'] = $in4_pass_packets + $out4_block_packets;
1168
		
1169
	$ifconfiginfo = "";
1170
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1171
	switch ($link_type) {
1172
	 /* DHCP? -> see if dhclient is up */
1173
	case "dhcp":
1174
	case "carpdev-dhcp":
1175
		/* see if dhclient is up */
1176
		if (find_dhclient_process($ifinfo['if']) <> "")
1177
			$ifinfo['dhcplink'] = "up";
1178
		else
1179
			$ifinfo['dhcplink'] = "down";
1180

    
1181
		break;
1182
	/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1183
	case "pppoe":
1184
	case "pptp":
1185
	case "l2tp":
1186
		if ($ifinfo['status'] == "up" && !isset($link0))
1187
			/* get PPPoE link status for dial on demand */
1188
			$ifinfo["{$link_type}link"] = "up";
1189
		else
1190
			$ifinfo["{$link_type}link"] = "down";
1191

    
1192
		break;
1193
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1194
	case "ppp":
1195
		if ($ifinfo['status'] == "up")
1196
			$ifinfo['ppplink'] = "up";
1197
		else
1198
			$ifinfo['ppplink'] = "down" ;
1199

    
1200
		if (empty($ifinfo['status']))
1201
			$ifinfo['status'] = "down";
1202
			
1203
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1204
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1205
				if ($config['interfaces'][$ifdescr]['if'] == $ppp['if'])
1206
					break;
1207
			}
1208
		}
1209
		$dev = $ppp['ports'];
1210
		if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev))
1211
			break;
1212
		if (!file_exists($dev)) {
1213
			$ifinfo['nodevice'] = 1;
1214
			$ifinfo['pppinfo'] = $dev . " device not present! Is the modem attached to the system?";	
1215
		}
1216
		// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1217
		if (isset($ppp['uptime']))
1218
			$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1219
		break;
1220
	default:
1221
		break;
1222
	}
1223
	
1224
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1225
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1226
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1227
	}
1228
	
1229
	if ($ifinfo['status'] == "up") {
1230
		/* try to determine media with ifconfig */
1231
		unset($ifconfiginfo);
1232
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1233
		$wifconfiginfo = array();
1234
		if(is_interface_wireless($ifdescr)) {
1235
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1236
			array_shift($wifconfiginfo);
1237
		}
1238
		$matches = "";
1239
		foreach ($ifconfiginfo as $ici) {
1240

    
1241
			/* don't list media/speed for wireless cards, as it always
1242
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1243
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1244
				$ifinfo['media'] = $matches[1];
1245
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1246
				$ifinfo['media'] = $matches[1];
1247
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1248
				$ifinfo['media'] = $matches[1];
1249
			}
1250

    
1251
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1252
				if ($matches[1] != "active")
1253
					$ifinfo['status'] = $matches[1];
1254
				if($ifinfo['status'] == "running")
1255
					$ifinfo['status'] = "up";
1256
			}
1257
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1258
				$ifinfo['channel'] = $matches[1];
1259
			}
1260
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1261
				if ($matches[1][0] == '"')
1262
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1263
				else
1264
					$ifinfo['ssid'] = $matches[1];
1265
			}
1266
		}
1267
		foreach($wifconfiginfo as $ici) {
1268
			$elements = preg_split("/[ ]+/i", $ici);
1269
			if ($elements[0] != "") {
1270
				$ifinfo['bssid'] = $elements[0];
1271
			}
1272
			if ($elements[3] != "") {
1273
				$ifinfo['rate'] = $elements[3];
1274
			}
1275
			if ($elements[4] != "") {
1276
				$ifinfo['rssi'] = $elements[4];
1277
			}
1278

    
1279
		}
1280
		/* lookup the gateway */
1281
		if (interface_has_gateway($ifdescr)) 
1282
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1283
	}
1284

    
1285
	$bridge = "";
1286
	$bridge = link_interface_to_bridge($ifdescr);
1287
	if($bridge) {
1288
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1289
		if(stristr($bridge_text, "blocking") <> false) {
1290
			$ifinfo['bridge'] = "<b><font color='red'>blocking</font></b> - check for ethernet loops";
1291
			$ifinfo['bridgeint'] = $bridge;
1292
		} else if(stristr($bridge_text, "learning") <> false) {
1293
			$ifinfo['bridge'] = "learning";
1294
			$ifinfo['bridgeint'] = $bridge;
1295
		} else if(stristr($bridge_text, "forwarding") <> false) {
1296
			$ifinfo['bridge'] = "forwarding";
1297
			$ifinfo['bridgeint'] = $bridge;
1298
		}
1299
	}
1300

    
1301
	return $ifinfo;
1302
}
1303

    
1304
//returns cpu speed of processor. Good for determining capabilities of machine
1305
function get_cpu_speed() {
1306
	 return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1307
}
1308

    
1309
function add_hostname_to_watch($hostname) {
1310
	if(!is_dir("/var/db/dnscache")) {
1311
		mkdir("/var/db/dnscache");
1312
	}
1313
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1314
		$domrecords = array();
1315
		$domips = array();
1316
		exec("host -t A $hostname", $domrecords, $rethost);
1317
		if($rethost == 0) {
1318
			foreach($domrecords as $domr) {
1319
				$doml = explode(" ", $domr);
1320
				$domip = $doml[3];
1321
				/* fill array with domain ip addresses */
1322
				if(is_ipaddr($domip)) {
1323
					$domips[] = $domip;
1324
				}
1325
			}
1326
		}
1327
		sort($domips);
1328
		$contents = "";
1329
		if(! empty($domips)) {
1330
			foreach($domips as $ip) {
1331
				$contents .= "$ip\n";
1332
			}
1333
		}
1334
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1335
	}
1336
}
1337

    
1338
function is_fqdn($fqdn) {
1339
	$hostname = false;
1340
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1341
		$hostname = true;
1342
	}
1343
	if(preg_match("/\.\./", $fqdn)) {
1344
		$hostname = false;
1345
	}
1346
	if(preg_match("/^\./i", $fqdn)) { 
1347
		$hostname = false;
1348
	}
1349
	if(preg_match("/\//i", $fqdn)) {
1350
		$hostname = false;
1351
	}
1352
	return($hostname);
1353
}
1354

    
1355
function pfsense_default_state_size() {
1356
  /* get system memory amount */
1357
  $memory = get_memory();
1358
  $avail = $memory[0];
1359
  /* Be cautious and only allocate 10% of system memory to the state table */
1360
  $max_states = (int) ($avail/10)*1000;
1361
  return $max_states;
1362
}
1363

    
1364
function pfsense_default_table_entries_size() {
1365
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1366
	return $current;
1367
}
1368

    
1369
/* Compare the current hostname DNS to the DNS cache we made
1370
 * if it has changed we return the old records
1371
 * if no change we return true */
1372
function compare_hostname_to_dnscache($hostname) {
1373
	if(!is_dir("/var/db/dnscache")) {
1374
		mkdir("/var/db/dnscache");
1375
	}
1376
	$hostname = trim($hostname);
1377
	if(is_readable("/var/db/dnscache/{$hostname}")) {
1378
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1379
	} else {
1380
		$oldcontents = "";
1381
	}
1382
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1383
		$domrecords = array();
1384
		$domips = array();
1385
		exec("host -t A $hostname", $domrecords, $rethost);
1386
		if($rethost == 0) {
1387
			foreach($domrecords as $domr) {
1388
				$doml = explode(" ", $domr);
1389
				$domip = $doml[3];
1390
				/* fill array with domain ip addresses */
1391
				if(is_ipaddr($domip)) {
1392
					$domips[] = $domip;
1393
				}
1394
			}
1395
		}
1396
		sort($domips);
1397
		$contents = "";
1398
		if(! empty($domips)) {
1399
			foreach($domips as $ip) {
1400
				$contents .= "$ip\n";
1401
			}
1402
		}
1403
	}
1404

    
1405
	if(trim($oldcontents) != trim($contents)) {
1406
		if($g['debug']) {
1407
			log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
1408
		}
1409
		return ($oldcontents);
1410
	} else {
1411
		return false;
1412
	}
1413
}
1414

    
1415
/*
1416
 * load_glxsb() - Load the glxsb crypto module if enabled in config.
1417
 */
1418
function load_glxsb() {
1419
	global $config, $g;
1420
	$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
1421
	if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
1422
		mwexec("/sbin/kldload glxsb");
1423
	}
1424
}
1425

    
1426
/****f* pfsense-utils/isvm
1427
 * NAME
1428
 *   isvm
1429
 * INPUTS
1430
 *	 none
1431
 * RESULT
1432
 *   returns true if machine is running under a virtual environment
1433
 ******/
1434
function isvm() {
1435
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1436
	$bios_vendor = strtolower(`/bin/kenv | /usr/bin/awk -F= '/smbios.bios.vendor/ {print $2}'`);
1437
	if(in_array($bios_vendor, $virtualenvs)) 
1438
		return true;
1439
	else
1440
		return false;
1441
}
1442

    
1443
function get_freebsd_version() {
1444
	$version = trim(`/usr/bin/uname -r | /usr/bin/cut  -d'.' -f1`);
1445
	return $version;
1446
}
1447

    
1448
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
1449
        global $ch, $fout, $file_size, $downloaded;
1450
        $file_size  = 1;
1451
        $downloaded = 1;
1452
        /* open destination file */
1453
        $fout = fopen($destination_file, "wb");
1454

    
1455
        /*
1456
         *      Originally by Author: Keyvan Minoukadeh
1457
         *      Modified by Scott Ullrich to return Content-Length size
1458
         */
1459

    
1460
        $ch = curl_init();
1461
        curl_setopt($ch, CURLOPT_URL, $url_file);
1462
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1463
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1464
        /* Don't verify SSL peers since we don't have the certificates to do so. */
1465
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1466
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1467
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1468
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
1469
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
1470

    
1471
        curl_exec($ch);
1472
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1473
        if($fout)
1474
                fclose($fout);
1475
        curl_close($ch);
1476
        return ($http_code == 200) ? true : $http_code;
1477
}
1478

    
1479
function read_header($ch, $string) {
1480
        global $file_size, $fout;
1481
        $length = strlen($string);
1482
        $regs = "";
1483
        ereg("(Content-Length:) (.*)", $string, $regs);
1484
        if($regs[2] <> "") {
1485
                $file_size = intval($regs[2]);
1486
        }
1487
        ob_flush();
1488
        return $length;
1489
}
1490

    
1491
function read_body($ch, $string) {
1492
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
1493
        $length = strlen($string);
1494
        $downloaded += intval($length);
1495
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1496
        $downloadProgress = 100 - $downloadProgress;
1497
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1498
                if($sendto == "status") {
1499
                        $tostatus = $static_status . $downloadProgress . "%";
1500
                        update_status($tostatus);
1501
                } else {
1502
                        $tooutput = $static_output . $downloadProgress . "%";
1503
                        update_output_window($tooutput);
1504
                }
1505
                update_progress_bar($downloadProgress);
1506
                $lastseen = $downloadProgress;
1507
        }
1508
        if($fout)
1509
                fwrite($fout, $string);
1510
        ob_flush();
1511
        return $length;
1512
}
1513

    
1514
/*
1515
 *   update_output_window: update bottom textarea dynamically.
1516
 */
1517
function update_output_window($text) {
1518
        global $pkg_interface;
1519
        $log = ereg_replace("\n", "\\n", $text);
1520
        if($pkg_interface == "console") {
1521
                /* too chatty */
1522
        } else {
1523
                echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
1524
        }
1525
        /* ensure that contents are written out */
1526
        ob_flush();
1527
}
1528

    
1529
/*
1530
 *   update_output_window: update top textarea dynamically.
1531
 */
1532
function update_status($status) {
1533
        global $pkg_interface;
1534
        if($pkg_interface == "console") {
1535
                echo $status . "\n";
1536
        } else {
1537
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
1538
        }
1539
        /* ensure that contents are written out */
1540
        ob_flush();
1541
}
1542

    
1543
/*
1544
 * update_progress_bar($percent): updates the javascript driven progress bar.
1545
 */
1546
function update_progress_bar($percent) {
1547
        global $pkg_interface;
1548
        if($percent > 100) $percent = 1;
1549
        if($pkg_interface <> "console") {
1550
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
1551
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1552
                echo "\n</script>";
1553
        } else {
1554
                echo " {$percent}%";
1555
        }
1556
}
1557

    
1558
/* Split() is being DEPRECATED as of PHP 5.3.0 and REMOVED as of PHP 6.0.0. Relying on this feature is highly discouraged. */
1559
if(!function_exists("split")) {
1560
	function split($seperator, $haystack, $limit = null) {
1561
		return preg_split($seperator, $haystack, $limit);
1562
	}
1563
}
1564

    
1565
function update_alias_names_upon_change($section, $subsection, $fielda, $fieldb, $new_alias_name, $origname) {
1566
	global $g, $config, $pconfig, $debug;
1567
	if(!$origname) 
1568
		return;
1569

    
1570
	if($debug) $fd = fopen("{$g['tmp_path']}/print_r", "a");
1571
	if($debug) fwrite($fd, print_r($pconfig, true));
1572

    
1573
	if($fieldb) {
1574
		if($debug) fwrite($fd, "fieldb exists\n");
1575
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1576
			if($debug) fwrite($fd, "$i\n");
1577
			if($config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] == $origname) {
1578
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1579
				$config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] = $new_alias_name;
1580
			}
1581
		}	
1582
	} else {
1583
		if($debug) fwrite($fd, "fieldb does not exist\n");
1584
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1585
			if($config["$section"]["$subsection"][$i]["$fielda"] == $origname) {
1586
				$config["$section"]["$subsection"][$i]["$fielda"] = $new_alias_name;
1587
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1588
			}
1589
		}
1590
	}
1591

    
1592
	if($debug) fclose($fd);
1593

    
1594
}
1595

    
1596
function update_alias_url_data() {
1597
	global $config, $g;
1598
	/* item is a url type */
1599
	$lockkey = lock('config');
1600
	for($x=0; $x<count($config['aliases']['alias']); $x++) {
1601
		if($config['aliases']['alias'][$x]['aliasurl']) {
1602
			/* fetch down and add in */
1603
			$isfirst = 0;
1604
			$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
1605
			unlink($temp_filename);
1606
			$fda = fopen("{$g['tmp_path']}/tmpfetch","w");
1607
			fwrite($fda, "/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1608
			fclose($fda);
1609
			mwexec("/bin/mkdir -p {$temp_filename}");
1610
			mwexec("/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1611
			/* if the item is tar gzipped then extract */
1612
			if(stristr($config['aliases']['alias'][$x]['aliasurl'], ".tgz"))
1613
				process_alias_tgz($temp_filename);
1614
			if(stristr($config['aliases']['alias'][$x]['aliasurl'], ".zip"))
1615
				process_alias_unzip($temp_filename);
1616
			if(file_exists("{$temp_filename}/aliases")) {
1617
				$file_contents = file_get_contents("{$temp_filename}/aliases");
1618
				$file_contents = str_replace("#", "\n#", $file_contents);
1619
				$file_contents_split = split("\n", $file_contents);
1620
				foreach($file_contents_split as $fc) {
1621
					$tmp = trim($fc);
1622
					if(stristr($fc, "#")) {
1623
						$tmp_split = split("#", $tmp);
1624
						$tmp = trim($tmp_split[0]);
1625
					}
1626
					if(trim($tmp) <> "") {
1627
						if($isfirst == 1)
1628
							$address .= " ";
1629
						$address .= $tmp;
1630
						$isfirst = 1;
1631
					}
1632
				}
1633
				if($isfirst == 0) {
1634
					/* nothing was found */
1635
					$dont_update = true;
1636
					break;
1637
				}
1638
				if(!$dont_update) {
1639
					$config['aliases']['alias'][$x]['address'] = $address;
1640
					$updated = true;
1641
				}
1642
				mwexec("/bin/rm -rf {$temp_filename}");
1643
			}
1644
		}
1645
	}
1646
	if($updated)
1647
		write_config();
1648
	unlock($lockkey);
1649
}
1650

    
1651
function process_alias_unzip($temp_filename) {
1652
	if(!file_exists("/usr/local/bin/unzip"))
1653
		return;
1654
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.zip");
1655
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
1656
	unlink("{$temp_filename}/aliases.zip");
1657
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1658
	/* foreach through all extracted files and build up aliases file */
1659
	$fd = fopen("{$temp_filename}/aliases", "w");
1660
	foreach($files_to_process as $f2p) {
1661
		$file_contents = file_get_contents($f2p);
1662
		fwrite($fd, $file_contents);
1663
		unlink($f2p);
1664
	}
1665
	fclose($fd);
1666
}
1667

    
1668
function process_alias_tgz($temp_filename) {
1669
	if(!file_exists("/usr/bin/tar"))
1670
		return;
1671
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
1672
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
1673
	unlink("{$temp_filename}/aliases.tgz");
1674
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1675
	/* foreach through all extracted files and build up aliases file */
1676
	$fd = fopen("{$temp_filename}/aliases", "w");
1677
	foreach($files_to_process as $f2p) {
1678
		$file_contents = file_get_contents($f2p);
1679
		fwrite($fd, $file_contents);
1680
		unlink($f2p);
1681
	}
1682
	fclose($fd);
1683
}
1684

    
1685
function version_compare_dates($a, $b) {
1686
	$a_time = strtotime($a);
1687
	$b_time = strtotime($b);
1688

    
1689
	if ((!$a_time) || (!$b_time)) {
1690
		return FALSE;
1691
	} else {
1692
		if ($a < $b)
1693
			return -1;
1694
		elseif ($a == $b)
1695
			return 0;
1696
		else
1697
			return 1;
1698
	}
1699
}
1700
function version_get_string_value($a) {
1701
	$strs = array(
1702
		0 => "ALPHA-ALPHA",
1703
		2 => "ALPHA",
1704
		3 => "BETA",
1705
		4 => "B",
1706
		5 => "RC",
1707
		6 => "RELEASE"
1708
	);
1709
	$major = 0;
1710
	$minor = 0;
1711
	foreach ($strs as $num => $str) {
1712
		if (substr($a, 0, strlen($str)) == $str) {
1713
			$major = $num;
1714
			$n = substr($a, strlen($str));
1715
			if (is_numeric($n))
1716
				$minor = $n;
1717
			break;
1718
		}
1719
	}
1720
	return "{$major}.{$minor}";
1721
}
1722
function version_compare_string($a, $b) {
1723
	return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
1724
}
1725
function version_compare_numeric($a, $b) {
1726
	$a_arr = explode('.', rtrim($a, '.0'));
1727
	$b_arr = explode('.', rtrim($b, '.0'));
1728

    
1729
	foreach ($a_arr as $n => $val) {
1730
		if (array_key_exists($n, $b_arr)) {
1731
			// So far so good, both have values at this minor version level. Compare.
1732
			if ($val > $b_arr[$n])
1733
				return 1;
1734
			elseif ($val < $b_arr[$n])
1735
				return -1;
1736
		} else {
1737
			// a is greater, since b doesn't have any minor version here.
1738
			return 1;
1739
		}
1740
	}
1741
	if (count($b_arr) > count($a_arr)) {
1742
		// b is longer than a, so it must be greater.
1743
		return -1;
1744
	} else {
1745
		// Both a and b are of equal length and value.
1746
		return 0;
1747
	}
1748
}
1749
function pfs_version_compare($cur_time, $cur_text, $remote) {
1750
	// First try date compare
1751
	$v = version_compare_dates($cur_time, $b);
1752
	if ($v === FALSE) {
1753
		// If that fails, try to compare by string
1754
		// Before anything else, simply test if the strings are equal
1755
		if (($cur_text == $remote) || ($cur_time == $remote))
1756
			return 0;
1757
		list($cur_num, $cur_str) = explode('-', $cur_text);
1758
		list($rem_num, $rem_str) = explode('-', $remote);
1759

    
1760
		// First try to compare the numeric parts of the version string.
1761
		$v = version_compare_numeric($cur_num, $rem_num);
1762

    
1763
		// If the numeric parts are the same, compare the string parts.
1764
		if ($v == 0)
1765
			return version_compare_string($cur_str, $rem_str);
1766
	}
1767
	return $v;
1768
}
1769
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
1770
	$urltable_prefix = "/var/db/aliastables/";
1771
	$urltable_filename = $urltable_prefix . $name . ".txt";
1772

    
1773
	// Make the aliases directory if it doesn't exist
1774
	if (!file_exists($urltable_prefix)) {
1775
		mkdir($urltable_prefix);
1776
	} elseif (!is_dir($urltable_prefix)) {
1777
		unlink($urltable_prefix);
1778
		mkdir($urltable_prefix);
1779
	}
1780

    
1781
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
1782
	if (!file_exists($urltable_filename)
1783
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400))
1784
		|| $forceupdate) {
1785

    
1786
		// Try to fetch the URL supplied
1787
		conf_mount_rw();
1788
		unlink_if_exists($urltable_filename . ".tmp");
1789
		// Use fetch to grab data since these may be large files, we don't want to process them through PHP if we can help it.
1790
		mwexec("/usr/bin/fetch -q -o " . escapeshellarg($urltable_filename . ".tmp") . " " . escapeshellarg($url));
1791
		// Remove comments. Might need some grep-fu to only allow lines that look like IPs/subnets
1792
		mwexec("/usr/bin/grep -v '^#' " . escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
1793
		unlink_if_exists($urltable_filename . ".tmp");
1794
		conf_mount_ro();
1795
		if (filesize($urltable_filename)) {
1796
			return true;
1797
		} else {
1798
			// If it's unfetchable or an empty file, bail
1799
			return false;
1800
		}
1801
	} else {
1802
		// File exists, and it doesn't need updated.
1803
		return -1;
1804
	}
1805
}
1806
function get_real_slice_from_glabel($label) {
1807
	$label = escapeshellarg($label);
1808
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1809
}
1810
function nanobsd_get_boot_slice() {
1811
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1812
}
1813
function nanobsd_get_boot_drive() {
1814
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/pfsense | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' ' | /usr/bin/cut -d's' -f1`);
1815
}
1816
function nanobsd_get_active_slice() {
1817
	$boot_drive = nanobsd_get_boot_drive();
1818
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1819

    
1820
	return "{$boot_drive}s{$active}";
1821
}
1822
function nanobsd_get_size() {
1823
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
1824
}
1825
function nanobsd_switch_boot_slice() {
1826
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1827
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1828
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1829
	nanobsd_detect_slice_info();
1830

    
1831
	if ($BOOTFLASH == $ACTIVE_SLICE) {
1832
		$slice = $TOFLASH;
1833
	} else {
1834
		$slice = $BOOTFLASH;
1835
	}
1836

    
1837
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1838
	ob_implicit_flush(1);
1839
	if(strstr($slice, "s2")) {
1840
		$ASLICE="2";
1841
		$AOLDSLICE="1";
1842
		$AGLABEL_SLICE="pfsense1";
1843
		$AUFS_ID="1";
1844
		$AOLD_UFS_ID="0";
1845
	} else {
1846
		$ASLICE="1";
1847
		$AOLDSLICE="2";
1848
		$AGLABEL_SLICE="pfsense0";
1849
		$AUFS_ID="0";
1850
		$AOLD_UFS_ID="1";
1851
	}
1852
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
1853
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
1854
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
1855
	conf_mount_rw();
1856
	exec("sysctl kern.geom.debugflags=16");
1857
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
1858
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
1859
	// We can't update these if they are mounted now.
1860
	if ($BOOTFLASH != $slice) {
1861
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
1862
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
1863
	}
1864
	exec("/sbin/sysctl kern.geom.debugflags=0");
1865
	conf_mount_ro();
1866
}
1867
function nanobsd_clone_slice() {
1868
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1869
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1870
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1871
	nanobsd_detect_slice_info();
1872

    
1873
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1874
	ob_implicit_flush(1);
1875
	exec("/sbin/sysctl kern.geom.debugflags=16");
1876
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
1877
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
1878
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
1879
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
1880
	exec("/sbin/sysctl kern.geom.debugflags=0");
1881
	if($status) {
1882
		return false;
1883
	} else {
1884
		return true;
1885
	}
1886
}
1887
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
1888
	$tmppath = "/tmp/{$gslice}";
1889
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
1890

    
1891
	exec("/bin/mkdir {$tmppath}");
1892
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
1893
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
1894
	exec("/bin/cp /etc/fstab {$fstabpath}");
1895

    
1896
	if (!file_exists($fstabpath)) {
1897
		$fstab = <<<EOF
1898
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
1899
/dev/ufs/cf /cf ufs ro,noatime 1 1
1900
EOF;
1901
		if (file_put_contents($fstabpath, $fstab))
1902
			$status = true;
1903
		else
1904
			$status = false;
1905
	} else {
1906
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
1907
	}
1908
	exec("/sbin/umount {$tmppath}");
1909
	exec("/bin/rmdir {$tmppath}");
1910

    
1911
	return $status;
1912
}
1913
function nanobsd_detect_slice_info() {
1914
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1915
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1916
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1917

    
1918
	$BOOT_DEVICE=nanobsd_get_boot_slice();
1919
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
1920
	$BOOT_DRIVE=nanobsd_get_boot_drive();
1921
	$ACTIVE_SLICE=nanobsd_get_active_slice();
1922

    
1923
	// Detect which slice is active and set information.
1924
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
1925
		$SLICE="2";
1926
		$OLDSLICE="1";
1927
		$GLABEL_SLICE="pfsense1";
1928
		$UFS_ID="1";
1929
		$OLD_UFS_ID="0";
1930

    
1931
	} else {
1932
		$SLICE="1";
1933
		$OLDSLICE="2";
1934
		$GLABEL_SLICE="pfsense0";
1935
		$UFS_ID="0";
1936
		$OLD_UFS_ID="1";
1937
	}
1938
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
1939
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
1940
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
1941
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
1942
}
1943

    
1944
function get_include_contents($filename) {
1945
    if (is_file($filename)) {
1946
        ob_start();
1947
        include $filename;
1948
        $contents = ob_get_contents();
1949
        ob_end_clean();
1950
        return $contents;
1951
    }
1952
    return false;
1953
}
1954

    
1955
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
1956
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
1957
 * size of the RRD xml dumps this is required.
1958
 * The reason we do not use it for pfSense is that it does not know about array fields
1959
 * which causes it to fail on array fields with single items. Possible Todo?
1960
 */
1961
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
1962
{
1963
    if (!function_exists('xml_parser_create'))
1964
    {
1965
        return array ();
1966
    }
1967
    $parser = xml_parser_create('');
1968
    xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
1969
    xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
1970
    xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
1971
    xml_parse_into_struct($parser, trim($contents), $xml_values);
1972
    xml_parser_free($parser);
1973
    if (!$xml_values)
1974
        return; //Hmm...
1975
    $xml_array = array ();
1976
    $parents = array ();
1977
    $opened_tags = array ();
1978
    $arr = array ();
1979
    $current = & $xml_array;
1980
    $repeated_tag_index = array ();
1981
    foreach ($xml_values as $data)
1982
    {
1983
        unset ($attributes, $value);
1984
        extract($data);
1985
        $result = array ();
1986
        $attributes_data = array ();
1987
        if (isset ($value))
1988
        {
1989
            if ($priority == 'tag')
1990
                $result = $value;
1991
            else
1992
                $result['value'] = $value;
1993
        }
1994
        if (isset ($attributes) and $get_attributes)
1995
        {
1996
            foreach ($attributes as $attr => $val)
1997
            {
1998
                if ($priority == 'tag')
1999
                    $attributes_data[$attr] = $val;
2000
                else
2001
                    $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2002
            }
2003
        }
2004
        if ($type == "open")
2005
        {
2006
            $parent[$level -1] = & $current;
2007
            if (!is_array($current) or (!in_array($tag, array_keys($current))))
2008
            {
2009
                $current[$tag] = $result;
2010
                if ($attributes_data)
2011
                    $current[$tag . '_attr'] = $attributes_data;
2012
                $repeated_tag_index[$tag . '_' . $level] = 1;
2013
                $current = & $current[$tag];
2014
            }
2015
            else
2016
            {
2017
                if (isset ($current[$tag][0]))
2018
                {
2019
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2020
                    $repeated_tag_index[$tag . '_' . $level]++;
2021
                }
2022
                else
2023
                {
2024
                    $current[$tag] = array (
2025
                        $current[$tag],
2026
                        $result
2027
                    );
2028
                    $repeated_tag_index[$tag . '_' . $level] = 2;
2029
                    if (isset ($current[$tag . '_attr']))
2030
                    {
2031
                        $current[$tag]['0_attr'] = $current[$tag . '_attr'];
2032
                        unset ($current[$tag . '_attr']);
2033
                    }
2034
                }
2035
                $last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2036
                $current = & $current[$tag][$last_item_index];
2037
            }
2038
        }
2039
        elseif ($type == "complete")
2040
        {
2041
            if (!isset ($current[$tag]))
2042
            {
2043
                $current[$tag] = $result;
2044
                $repeated_tag_index[$tag . '_' . $level] = 1;
2045
                if ($priority == 'tag' and $attributes_data)
2046
                    $current[$tag . '_attr'] = $attributes_data;
2047
            }
2048
            else
2049
            {
2050
                if (isset ($current[$tag][0]) and is_array($current[$tag]))
2051
                {
2052
                    $current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2053
                    if ($priority == 'tag' and $get_attributes and $attributes_data)
2054
                    {
2055
                        $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2056
                    }
2057
                    $repeated_tag_index[$tag . '_' . $level]++;
2058
                }
2059
                else
2060
                {
2061
                    $current[$tag] = array (
2062
                        $current[$tag],
2063
                        $result
2064
                    );
2065
                    $repeated_tag_index[$tag . '_' . $level] = 1;
2066
                    if ($priority == 'tag' and $get_attributes)
2067
                    {
2068
                        if (isset ($current[$tag . '_attr']))
2069
                        {
2070
                            $current[$tag]['0_attr'] = $current[$tag . '_attr'];
2071
                            unset ($current[$tag . '_attr']);
2072
                        }
2073
                        if ($attributes_data)
2074
                        {
2075
                            $current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2076
                        }
2077
                    }
2078
                    $repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2079
                }
2080
            }
2081
        }
2082
        elseif ($type == 'close')
2083
        {
2084
            $current = & $parent[$level -1];
2085
        }
2086
    }
2087
    return ($xml_array);
2088
}
2089

    
2090
function get_country_name($country_code) {
2091
	if ($country_code != "ALL" && strlen($country_code) != 2)
2092
		return "";
2093

    
2094
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2095
	$country_names_contents = file_get_contents($country_names_xml);
2096
	$country_names = xml2array($country_names_contents);
2097

    
2098
	if($country_code == "ALL") {
2099
		$country_list = array();
2100
		foreach($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2101
			$country_list[] = array( "code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2102
						 "name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])) );
2103
		}
2104
		return $country_list;
2105
	}
2106

    
2107
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2108
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2109
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2110
		}
2111
	}
2112
	return "";
2113
}
2114

    
2115
?>
(30-30/54)