Project

General

Profile

Download (80.4 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_natpfruleint_access
44
 * NAME
45
 *   have_natpfruleint_access
46
 * INPUTS
47
 *	none
48
 * RESULT
49
 *   returns true if user has access to edit a specific firewall nat port forward interface
50
 ******/
51
function have_natpfruleint_access($if) {
52
	$security_url = "firewall_nat_edit.php?if=". strtolower($if);
53
	if(isAllowedPage($security_url, $allowed))
54
		return true;
55
	return false;
56
}
57

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

    
73
/****f* pfsense-utils/does_url_exist
74
 * NAME
75
 *   does_url_exist
76
 * INPUTS
77
 *	none
78
 * RESULT
79
 *   returns true if a url is available
80
 ******/
81
function does_url_exist($url) {
82
	$fd = fopen("$url","r");
83
	if($fd) {
84
		fclose($fd);
85
		return true;
86
	} else {
87
		return false;
88
	}
89
}
90

    
91
/****f* pfsense-utils/is_private_ip
92
 * NAME
93
 *   is_private_ip
94
 * INPUTS
95
 *	none
96
 * RESULT
97
 *   returns true if an ip address is in a private range
98
 ******/
99
function is_private_ip($iptocheck) {
100
	$isprivate = false;
101
	$ip_private_list=array(
102
		"10.0.0.0/8",
103
		"100.64.0.0/10",
104
		"172.16.0.0/12",
105
		"192.168.0.0/16",
106
	);
107
	foreach($ip_private_list as $private) {
108
		if(ip_in_subnet($iptocheck,$private)==true)
109
			$isprivate = true;
110
	}
111
	return $isprivate;
112
}
113

    
114
/****f* pfsense-utils/get_tmp_file
115
 * NAME
116
 *   get_tmp_file
117
 * INPUTS
118
 *	none
119
 * RESULT
120
 *   returns a temporary filename
121
 ******/
122
function get_tmp_file() {
123
	global $g;
124
	return "{$g['tmp_path']}/tmp-" . time();
125
}
126

    
127
/****f* pfsense-utils/get_dns_servers
128
 * NAME
129
 *   get_dns_servres - get system dns servers
130
 * INPUTS
131
 *   $dns_servers - an array of the dns servers
132
 * RESULT
133
 *   null
134
 ******/
135
function get_dns_servers() {
136
	$dns_servers = array();
137
	$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
138
	foreach($dns_s as $dns) {
139
		$matches = "";
140
		if (preg_match("/nameserver (.*)/", $dns, $matches))
141
			$dns_servers[] = $matches[1];
142
	}
143
	return array_unique($dns_servers);
144
}
145

    
146
/****f* pfsense-utils/enable_hardware_offloading
147
 * NAME
148
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
149
 * INPUTS
150
 *   $interface	- string containing the physical interface to work on.
151
 * RESULT
152
 *   null
153
 * NOTES
154
 *   This function only supports the fxp driver's loadable microcode.
155
 ******/
156
function enable_hardware_offloading($interface) {
157
	global $g, $config;
158

    
159
	if(isset($config['system']['do_not_use_nic_microcode']))
160
		return;
161

    
162
	/* translate wan, lan, opt -> real interface if needed */
163
	$int = get_real_interface($interface);
164
	if(empty($int))
165
		return;
166
	$int_family = preg_split("/[0-9]+/", $int);
167
	$supported_ints = array('fxp');
168
	if (in_array($int_family, $supported_ints)) {
169
		if(does_interface_exist($int))
170
			pfSense_interface_flags($int, IFF_LINK0);
171
	}
172

    
173
	return;
174
}
175

    
176
/****f* pfsense-utils/interface_supports_polling
177
 * NAME
178
 *   checks to see if an interface supports polling according to man polling
179
 * INPUTS
180
 *
181
 * RESULT
182
 *   true or false
183
 * NOTES
184
 *
185
 ******/
186
function interface_supports_polling($iface) {
187
	$opts = pfSense_get_interface_addresses($iface);
188
	if (is_array($opts) && isset($opts['caps']['polling']))
189
		return true;
190

    
191
	return false;
192
}
193

    
194
/****f* pfsense-utils/is_alias_inuse
195
 * NAME
196
 *   checks to see if an alias is currently in use by a rule
197
 * INPUTS
198
 *
199
 * RESULT
200
 *   true or false
201
 * NOTES
202
 *
203
 ******/
204
function is_alias_inuse($alias) {
205
	global $g, $config;
206

    
207
	if($alias == "") return false;
208
	/* loop through firewall rules looking for alias in use */
209
	if(is_array($config['filter']['rule']))
210
		foreach($config['filter']['rule'] as $rule) {
211
			if($rule['source']['address'])
212
				if($rule['source']['address'] == $alias)
213
					return true;
214
			if($rule['destination']['address'])
215
				if($rule['destination']['address'] == $alias)
216
					return true;
217
		}
218
	/* loop through nat rules looking for alias in use */
219
	if(is_array($config['nat']['rule']))
220
		foreach($config['nat']['rule'] as $rule) {
221
			if($rule['target'] && $rule['target'] == $alias)
222
				return true;
223
			if($rule['source']['address'] && $rule['source']['address'] == $alias)
224
				return true;
225
			if($rule['destination']['address'] && $rule['destination']['address'] == $alias)
226
				return true;
227
		}
228
	return false;
229
}
230

    
231
/****f* pfsense-utils/is_schedule_inuse
232
 * NAME
233
 *   checks to see if a schedule is currently in use by a rule
234
 * INPUTS
235
 *
236
 * RESULT
237
 *   true or false
238
 * NOTES
239
 *
240
 ******/
241
function is_schedule_inuse($schedule) {
242
	global $g, $config;
243

    
244
	if($schedule == "") return false;
245
	/* loop through firewall rules looking for schedule in use */
246
	if(is_array($config['filter']['rule']))
247
		foreach($config['filter']['rule'] as $rule) {
248
			if($rule['sched'] == $schedule)
249
				return true;
250
		}
251
	return false;
252
}
253

    
254
/****f* pfsense-utils/setup_polling
255
 * NAME
256
 *   sets up polling
257
 * INPUTS
258
 *
259
 * RESULT
260
 *   null
261
 * NOTES
262
 *
263
 ******/
264
function setup_polling() {
265
	global $g, $config;
266

    
267
	if (isset($config['system']['polling']))
268
		mwexec("/sbin/sysctl kern.polling.idle_poll=1");
269
	else
270
		mwexec("/sbin/sysctl kern.polling.idle_poll=0");
271

    
272
	if($config['system']['polling_each_burst'])
273
		mwexec("/sbin/sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}");
274
	if($config['system']['polling_burst_max'])
275
		mwexec("/sbin/sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}");
276
	if($config['system']['polling_user_frac'])
277
		mwexec("/sbin/sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}");
278
}
279

    
280
/****f* pfsense-utils/setup_microcode
281
 * NAME
282
 *   enumerates all interfaces and calls enable_hardware_offloading which
283
 *   enables a NIC's supported hardware features.
284
 * INPUTS
285
 *
286
 * RESULT
287
 *   null
288
 * NOTES
289
 *   This function only supports the fxp driver's loadable microcode.
290
 ******/
291
function setup_microcode() {
292

    
293
	/* if list */
294
	$ifs = get_interface_arr();
295

    
296
	foreach($ifs as $if)
297
		enable_hardware_offloading($if);
298
}
299

    
300
/****f* pfsense-utils/get_carp_status
301
 * NAME
302
 *   get_carp_status - Return whether CARP is enabled or disabled.
303
 * RESULT
304
 *   boolean	- true if CARP is enabled, false if otherwise.
305
 ******/
306
function get_carp_status() {
307
	/* grab the current status of carp */
308
	$status = `/sbin/sysctl -n net.inet.carp.allow`;
309
	return (intval($status) > 0);
310
}
311

    
312
/*
313
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
314

    
315
 */
316
function convert_ip_to_network_format($ip, $subnet) {
317
	$ipsplit = explode('.', $ip);
318
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
319
	return $string;
320
}
321

    
322
/*
323
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
324
 */
325
function get_carp_interface_status($carpinterface) {
326
	$carp_query = "";
327
	exec("/sbin/ifconfig $carpinterface | /usr/bin/grep -v grep | /usr/bin/grep carp:", $carp_query);
328
	foreach($carp_query as $int) {
329
		if(stristr($int, "MASTER"))
330
			return gettext("MASTER");
331
		if(stristr($int, "BACKUP"))
332
			return gettext("BACKUP");
333
		if(stristr($int, "INIT"))
334
			return gettext("INIT");
335
	}
336
	return;
337
}
338

    
339
/*
340
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
341
 */
342
function get_pfsync_interface_status($pfsyncinterface) {
343
	$result = does_interface_exist($pfsyncinterface);
344
	if($result <> true) return;
345
	$status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
346
	return $status;
347
}
348

    
349
/*
350
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
351
 */
352
function add_rule_to_anchor($anchor, $rule, $label) {
353
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
354
}
355

    
356
/*
357
 * remove_text_from_file
358
 * remove $text from file $file
359
 */
360
function remove_text_from_file($file, $text) {
361
	if(!file_exists($file) && !is_writable($file))
362
		return;
363
	$filecontents = file_get_contents($file);
364
	$text = str_replace($text, "", $filecontents);
365
	@file_put_contents($file, $text);
366
}
367

    
368
/*
369
 * add_text_to_file($file, $text): adds $text to $file.
370
 * replaces the text if it already exists.
371
 */
372
function add_text_to_file($file, $text, $replace = false) {
373
	if(file_exists($file) and is_writable($file)) {
374
		$filecontents = file($file);
375
		$filecontents = array_map('rtrim', $filecontents);
376
		array_push($filecontents, $text);
377
		if ($replace)
378
			$filecontents = array_unique($filecontents);
379

    
380
		$file_text = implode("\n", $filecontents);
381

    
382
		@file_put_contents($file, $file_text);
383
		return true;
384
	}
385
	return false;
386
}
387

    
388
/*
389
 *   after_sync_bump_adv_skew(): create skew values by 1S
390
 */
391
function after_sync_bump_adv_skew() {
392
	global $config, $g;
393
	$processed_skew = 1;
394
	$a_vip = &$config['virtualip']['vip'];
395
	foreach ($a_vip as $vipent) {
396
		if($vipent['advskew'] <> "") {
397
			$processed_skew = 1;
398
			$vipent['advskew'] = $vipent['advskew']+1;
399
		}
400
	}
401
	if($processed_skew == 1)
402
		write_config(gettext("After synch increase advertising skew"));
403
}
404

    
405
/*
406
 * get_filename_from_url($url): converts a url to its filename.
407
 */
408
function get_filename_from_url($url) {
409
	return basename($url);
410
}
411

    
412
/*
413
 *   get_dir: return an array of $dir
414
 */
415
function get_dir($dir) {
416
	$dir_array = array();
417
	$d = dir($dir);
418
	while (false !== ($entry = $d->read())) {
419
		array_push($dir_array, $entry);
420
	}
421
	$d->close();
422
	return $dir_array;
423
}
424

    
425
/****f* pfsense-utils/WakeOnLan
426
 * NAME
427
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
428
 * RESULT
429
 *   true/false - true if the operation was successful
430
 ******/
431
function WakeOnLan($addr, $mac)
432
{
433
	$addr_byte = explode(':', $mac);
434
	$hw_addr = '';
435

    
436
	for ($a=0; $a < 6; $a++)
437
		$hw_addr .= chr(hexdec($addr_byte[$a]));
438

    
439
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
440

    
441
	for ($a = 1; $a <= 16; $a++)
442
		$msg .= $hw_addr;
443

    
444
	// send it to the broadcast address using UDP
445
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
446
	if ($s == false) {
447
		log_error(gettext("Error creating socket!"));
448
		log_error(sprintf(gettext("Error code is '%1\$s' - %2\$s"), socket_last_error($s), socket_strerror(socket_last_error($s))));
449
	} else {
450
		// setting a broadcast option to socket:
451
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
452
		if($opt_ret < 0)
453
			log_error(sprintf(gettext("setsockopt() failed, error: %s"), strerror($opt_ret)));
454
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
455
		socket_close($s);
456
		log_error(sprintf(gettext('Magic Packet sent (%1$s) to {%2$s} MAC=%3$s'), $e, $addr, $mac));
457
		return true;
458
	}
459

    
460
	return false;
461
}
462

    
463
/*
464
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
465
 *					 Useful for finding paths and stripping file extensions.
466
 */
467
function reverse_strrchr($haystack, $needle) {
468
	if (!is_string($haystack))
469
		return;
470
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
471
}
472

    
473
/*
474
 *  backup_config_section($section): returns as an xml file string of
475
 *                                   the configuration section
476
 */
477
function backup_config_section($section_name) {
478
	global $config;
479
	$new_section = &$config[$section_name];
480
	/* generate configuration XML */
481
	$xmlconfig = dump_xml_config($new_section, $section_name);
482
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
483
	return $xmlconfig;
484
}
485

    
486
/*
487
 *  restore_config_section($section_name, new_contents): restore a configuration section,
488
 *                                                  and write the configuration out
489
 *                                                  to disk/cf.
490
 */
491
function restore_config_section($section_name, $new_contents) {
492
	global $config, $g;
493
	conf_mount_rw();
494
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
495
	fwrite($fout, $new_contents);
496
	fclose($fout);
497

    
498
	$xml = parse_xml_config($g['tmp_path'] . "/tmpxml", null);
499
	if ($xml['pfsense']) {
500
		$xml = $xml['pfsense'];
501
	}
502
	else if ($xml['m0n0wall']) {
503
		$xml = $xml['m0n0wall'];
504
	}
505
	if ($xml[$section_name]) {
506
		$section_xml = $xml[$section_name];
507
	} else {
508
		$section_xml = -1;
509
	}
510

    
511
	@unlink($g['tmp_path'] . "/tmpxml");
512
	if ($section_xml === -1) {
513
		return false;
514
	}
515
	$config[$section_name] = &$section_xml;
516
	if(file_exists("{$g['tmp_path']}/config.cache"))
517
		unlink("{$g['tmp_path']}/config.cache");
518
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
519
	disable_security_checks();
520
	conf_mount_ro();
521
	return true;
522
}
523

    
524
/*
525
 *  merge_config_section($section_name, new_contents):   restore a configuration section,
526
 *                                                  and write the configuration out
527
 *                                                  to disk/cf.  But preserve the prior
528
 * 													structure if needed
529
 */
530
function merge_config_section($section_name, $new_contents) {
531
	global $config;
532
	conf_mount_rw();
533
	$fname = get_tmp_filename();
534
	$fout = fopen($fname, "w");
535
	fwrite($fout, $new_contents);
536
	fclose($fout);
537
	$section_xml = parse_xml_config($fname, $section_name);
538
	$config[$section_name] = $section_xml;
539
	unlink($fname);
540
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
541
	disable_security_checks();
542
	conf_mount_ro();
543
	return;
544
}
545

    
546
/*
547
 * http_post($server, $port, $url, $vars): does an http post to a web server
548
 *                                         posting the vars array.
549
 * written by nf@bigpond.net.au
550
 */
551
function http_post($server, $port, $url, $vars) {
552
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
553
	$urlencoded = "";
554
	while (list($key,$value) = each($vars))
555
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
556
	$urlencoded = substr($urlencoded,0,-1);
557
	$content_length = strlen($urlencoded);
558
	$headers = "POST $url HTTP/1.1
559
Accept: */*
560
Accept-Language: en-au
561
Content-Type: application/x-www-form-urlencoded
562
User-Agent: $user_agent
563
Host: $server
564
Connection: Keep-Alive
565
Cache-Control: no-cache
566
Content-Length: $content_length
567

    
568
";
569

    
570
	$errno = "";
571
	$errstr = "";
572
	$fp = fsockopen($server, $port, $errno, $errstr);
573
	if (!$fp) {
574
		return false;
575
	}
576

    
577
	fputs($fp, $headers);
578
	fputs($fp, $urlencoded);
579

    
580
	$ret = "";
581
	while (!feof($fp))
582
		$ret.= fgets($fp, 1024);
583
	fclose($fp);
584

    
585
	return $ret;
586
}
587

    
588
/*
589
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
590
 */
591
if (!function_exists('php_check_syntax')){
592
	global $g;
593
	function php_check_syntax($code_to_check, &$errormessage){
594
		return false;
595
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
596
		$code = $_POST['content'];
597
		$code = str_replace("<?php", "", $code);
598
		$code = str_replace("?>", "", $code);
599
		fwrite($fout, "<?php\n\n");
600
		fwrite($fout, $code_to_check);
601
		fwrite($fout, "\n\n?>\n");
602
		fclose($fout);
603
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
604
		$output = exec_command($command);
605
		if (stristr($output, "Errors parsing") == false) {
606
			echo "false\n";
607
			$errormessage = '';
608
			return(false);
609
		} else {
610
			$errormessage = $output;
611
			return(true);
612
		}
613
	}
614
}
615

    
616
/*
617
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
618
 */
619
if (!function_exists('php_check_syntax')){
620
	function php_check_syntax($code_to_check, &$errormessage){
621
		return false;
622
		$command = "/usr/local/bin/php -l " . $code_to_check;
623
		$output = exec_command($command);
624
		if (stristr($output, "Errors parsing") == false) {
625
			echo "false\n";
626
			$errormessage = '';
627
			return(false);
628
		} else {
629
			$errormessage = $output;
630
			return(true);
631
		}
632
	}
633
}
634

    
635
/*
636
 * rmdir_recursive($path,$follow_links=false)
637
 * Recursively remove a directory tree (rm -rf path)
638
 * This is for directories _only_
639
 */
640
function rmdir_recursive($path,$follow_links=false) {
641
	$to_do = glob($path);
642
	if(!is_array($to_do)) $to_do = array($to_do);
643
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
644
		if(file_exists($workingdir)) {
645
			if(is_dir($workingdir)) {
646
				$dir = opendir($workingdir);
647
				while ($entry = readdir($dir)) {
648
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
649
						unlink("$workingdir/$entry");
650
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
651
						rmdir_recursive("$workingdir/$entry");
652
				}
653
				closedir($dir);
654
				rmdir($workingdir);
655
			} elseif (is_file($workingdir)) {
656
				unlink($workingdir);
657
			}
658
		}
659
	}
660
	return;
661
}
662

    
663
/*
664
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
665
 */
666
function call_pfsense_method($method, $params, $timeout = 0) {
667
	global $g, $config;
668

    
669
	$ip = gethostbyname($g['product_website']);
670
	if($ip == $g['product_website'])
671
		return false;
672

    
673
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
674
	$xmlrpc_path = $g['xmlrpcpath'];
675
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
676
	$port = 0;
677
	$proxyurl = "";
678
	$proxyport = 0;
679
	$proxyuser = "";
680
	$proxypass = "";
681
	if (!empty($config['system']['proxyurl']))
682
		$proxyurl = $config['system']['proxyurl'];
683
	if (!empty($config['system']['proxyport']) && is_numeric($config['system']['proxyport']))
684
		$proxyport = $config['system']['proxyport'];
685
	if (!empty($config['system']['proxyuser']))
686
		$proxyuser = $config['system']['proxyuser'];
687
	if (!empty($config['system']['proxypass']))
688
		$proxypass = $config['system']['proxypass'];
689
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url, $port, $proxyurl, $proxyport, $proxyuser, $proxypass);
690
	// If the ALT PKG Repo has a username/password set, use it.
691
	if($config['system']['altpkgrepo']['username'] &&
692
	   $config['system']['altpkgrepo']['password']) {
693
		$username = $config['system']['altpkgrepo']['username'];
694
		$password = $config['system']['altpkgrepo']['password'];
695
		$cli->setCredentials($username, $password);
696
	}
697
	$resp = $cli->send($msg, $timeout);
698
	if(!is_object($resp)) {
699
		log_error(sprintf(gettext("XMLRPC communication error: %s"), $cli->errstr));
700
		return false;
701
	} elseif($resp->faultCode()) {
702
		log_error(sprintf(gettext('XMLRPC request failed with error %1$s: %2$s'), $resp->faultCode(), $resp->faultString()));
703
		return false;
704
	} else {
705
		return XML_RPC_Decode($resp->value());
706
	}
707
}
708

    
709
/*
710
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
711
 */
712
function check_firmware_version($tocheck = "all", $return_php = true) {
713
	global $g, $config;
714

    
715
	$ip = gethostbyname($g['product_website']);
716
	if($ip == $g['product_website'])
717
		return false;
718

    
719
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
720
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
721
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
722
		"platform" => trim(file_get_contents('/etc/platform')),
723
		"config_version" => $config['version']
724
		);
725
	if($tocheck == "all") {
726
		$params = $rawparams;
727
	} else {
728
		foreach($tocheck as $check) {
729
			$params['check'] = $rawparams['check'];
730
			$params['platform'] = $rawparams['platform'];
731
		}
732
	}
733
	if($config['system']['firmware']['branch'])
734
		$params['branch'] = $config['system']['firmware']['branch'];
735

    
736
	/* XXX: What is this method? */
737
	if(!($versions = call_pfsense_method('pfsense.get_firmware_version', $params))) {
738
		return false;
739
	} else {
740
		$versions["current"] = $params;
741
	}
742

    
743
	return $versions;
744
}
745

    
746
/*
747
 * host_firmware_version(): Return the versions used in this install
748
 */
749
function host_firmware_version($tocheck = "") {
750
	global $g, $config;
751

    
752
	return array(
753
		"firmware" => array("version" => trim(file_get_contents('/etc/version', " \n"))),
754
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel', " \n"))),
755
		"base"     => array("version" => trim(file_get_contents('/etc/version_base', " \n"))),
756
		"platform" => trim(file_get_contents('/etc/platform', " \n")),
757
		"config_version" => $config['version']
758
	);
759
}
760

    
761
function get_disk_info() {
762
	$diskout = "";
763
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
764
	return explode(' ', $diskout[0]);
765
}
766

    
767
/****f* pfsense-utils/strncpy
768
 * NAME
769
 *   strncpy - copy strings
770
 * INPUTS
771
 *   &$dst, $src, $length
772
 * RESULT
773
 *   none
774
 ******/
775
function strncpy(&$dst, $src, $length) {
776
	if (strlen($src) > $length) {
777
		$dst = substr($src, 0, $length);
778
	} else {
779
		$dst = $src;
780
	}
781
}
782

    
783
/****f* pfsense-utils/reload_interfaces_sync
784
 * NAME
785
 *   reload_interfaces - reload all interfaces
786
 * INPUTS
787
 *   none
788
 * RESULT
789
 *   none
790
 ******/
791
function reload_interfaces_sync() {
792
	global $config, $g;
793

    
794
	if($g['debug'])
795
		log_error(gettext("reload_interfaces_sync() is starting."));
796

    
797
	/* parse config.xml again */
798
	$config = parse_config(true);
799

    
800
	/* enable routing */
801
	system_routing_enable();
802
	if($g['debug'])
803
		log_error(gettext("Enabling system routing"));
804

    
805
	if($g['debug'])
806
		log_error(gettext("Cleaning up Interfaces"));
807

    
808
	/* set up interfaces */
809
	interfaces_configure();
810
}
811

    
812
/****f* pfsense-utils/reload_all
813
 * NAME
814
 *   reload_all - triggers a reload of all settings
815
 *   * INPUTS
816
 *   none
817
 * RESULT
818
 *   none
819
 ******/
820
function reload_all() {
821
	send_event("service reload all");
822
}
823

    
824
/****f* pfsense-utils/reload_interfaces
825
 * NAME
826
 *   reload_interfaces - triggers a reload of all interfaces
827
 * INPUTS
828
 *   none
829
 * RESULT
830
 *   none
831
 ******/
832
function reload_interfaces() {
833
	send_event("interface all reload");
834
}
835

    
836
/****f* pfsense-utils/reload_all_sync
837
 * NAME
838
 *   reload_all - reload all settings
839
 *   * INPUTS
840
 *   none
841
 * RESULT
842
 *   none
843
 ******/
844
function reload_all_sync() {
845
	global $config, $g;
846

    
847
	$g['booting'] = false;
848

    
849
	/* parse config.xml again */
850
	$config = parse_config(true);
851

    
852
	/* set up our timezone */
853
	system_timezone_configure();
854

    
855
	/* set up our hostname */
856
	system_hostname_configure();
857

    
858
	/* make hosts file */
859
	system_hosts_generate();
860

    
861
	/* generate resolv.conf */
862
	system_resolvconf_generate();
863

    
864
	/* enable routing */
865
	system_routing_enable();
866

    
867
	/* set up interfaces */
868
	interfaces_configure();
869

    
870
	/* start dyndns service */
871
	services_dyndns_configure();
872

    
873
	/* configure cron service */
874
	configure_cron();
875

    
876
	/* start the NTP client */
877
	system_ntp_configure();
878

    
879
	/* sync pw database */
880
	conf_mount_rw();
881
	unlink_if_exists("/etc/spwd.db.tmp");
882
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
883
	conf_mount_ro();
884

    
885
	/* restart sshd */
886
	send_event("service restart sshd");
887

    
888
	/* restart webConfigurator if needed */
889
	send_event("service restart webgui");
890
}
891

    
892
function auto_login() {
893
	global $config;
894

    
895
	if(isset($config['system']['disableconsolemenu']))
896
		$status = false;
897
	else
898
		$status = true;
899

    
900
	$gettytab = file_get_contents("/etc/gettytab");
901
	$getty_split = explode("\n", $gettytab);
902
	$getty_update_needed = false;
903
	$getty_search_str = ":ht:np:sp#115200";
904
	$getty_al_str = ":al=root:";
905
	$getty_al_search_str = $getty_search_str . $getty_al_str;
906
	/* Check if gettytab is already OK, if so then do not rewrite it. */
907
	foreach($getty_split as $gs) {
908
		if(stristr($gs, $getty_search_str)) {
909
			if($status == true) {
910
				if(!stristr($gs, $getty_al_search_str)) {
911
					$getty_update_needed = true;
912
				}
913
			} else {
914
				if(stristr($gs, $getty_al_search_str)) {
915
					$getty_update_needed = true;
916
				}
917
			}
918
		}
919
	}
920

    
921
	if (!$getty_update_needed) {
922
		return;
923
	}
924

    
925
	conf_mount_rw();
926
	$fd = false;
927
	$tries = 0;
928
	while (!$fd && $tries < 100) {
929
		$fd = fopen("/etc/gettytab", "w");
930
		$tries++;
931

    
932
	}
933
	if (!$fd) {
934
		conf_mount_ro();
935
		if ($status) {
936
			log_error(gettext("Enabling auto login was not possible."));
937
		} else {
938
			log_error(gettext("Disabling auto login was not possible."));
939
		}
940
		return;
941
	}
942
	foreach($getty_split as $gs) {
943
		if(stristr($gs, $getty_search_str)) {
944
			if($status == true) {
945
				fwrite($fd, "	".$getty_al_search_str."\n");
946
			} else {
947
				fwrite($fd, "	".$getty_search_str."\n");
948
			}
949
		} else {
950
			fwrite($fd, "{$gs}\n");
951
		}
952
	}
953
	fclose($fd);
954

    
955
	if ($status) {
956
		log_error(gettext("Enabled console auto login, console menu is NOT password protected."));
957
	} else {
958
		log_error(gettext("Disabled console auto login, console menu is password protected."));
959
	}
960

    
961
	conf_mount_ro();
962
}
963

    
964
function setup_serial_port($when="save", $path="") {
965
	global $g, $config;
966
	conf_mount_rw();
967
	$prefix = "";
968
	if (($when == "upgrade") && (!empty($path)) && is_dir($path.'/boot/'))
969
		$prefix = "/tmp/{$path}";
970
	$boot_config_file = "{$path}/boot.config";
971
	$loader_conf_file = "{$path}/boot/loader.conf";
972
	/* serial console - write out /boot.config */
973
	if(file_exists($boot_config_file))
974
		$boot_config = file_get_contents($boot_config_file);
975
	else
976
		$boot_config = "";
977

    
978
	if(($g['platform'] != "cdrom") && ($g['platform'] != "nanobsd")) {
979
		$boot_config_split = explode("\n", $boot_config);
980
		$fd = fopen($boot_config_file,"w");
981
		if($fd) {
982
			foreach($boot_config_split as $bcs) {
983
				if(stristr($bcs, "-D")) {
984
					/* DONT WRITE OUT, WE'LL DO IT LATER */
985
				} else {
986
					if($bcs <> "")
987
						fwrite($fd, "{$bcs}\n");
988
				}
989
			}
990
			if(isset($config['system']['enableserial'])) {
991
				fwrite($fd, "-D");
992
			}
993
			fclose($fd);
994
		}
995
	}
996
	if($g['platform'] != "cdrom") {
997
		/* serial console - write out /boot/loader.conf */
998
		if ($when == "upgrade")
999
			system("echo \"Reading {$loader_conf_file}...\" >> /conf/upgrade_log.txt");
1000
		$boot_config = file_get_contents($loader_conf_file);
1001
		$boot_config_split = explode("\n", $boot_config);
1002
		if(count($boot_config_split) > 0) {
1003
			$new_boot_config = array();
1004
			// Loop through and only add lines that are not empty, and which
1005
			//  do not contain a console directive.
1006
			foreach($boot_config_split as $bcs)
1007
				if(!empty($bcs)
1008
					&& (stripos($bcs, "console") === false)
1009
					&& (stripos($bcs, "boot_multicons") === false)
1010
					&& (stripos($bcs, "boot_serial") === false)
1011
					&& (stripos($bcs, "hw.usb.no_pf") === false))
1012
					$new_boot_config[] = $bcs;
1013

    
1014
			$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : "9600";
1015
			if(isset($config['system']['enableserial'])) {
1016
				$new_boot_config[] = 'boot_multicons="YES"';
1017
				$new_boot_config[] = 'boot_serial="YES"';
1018
				$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
1019
				$new_boot_config[] = 'console="comconsole,vidconsole"';
1020
			} elseif ($g['platform'] == "nanobsd") {
1021
				$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
1022
			}
1023

    
1024
			$new_boot_config[] = 'hw.usb.no_pf="1"';
1025

    
1026
			file_put_contents($loader_conf_file, implode("\n", $new_boot_config) . "\n");
1027
		}
1028
	}
1029
	$ttys = file_get_contents("/etc/ttys");
1030
	$ttys_split = explode("\n", $ttys);
1031
	$fd = fopen("/etc/ttys", "w");
1032
	foreach($ttys_split as $tty) {
1033
		if(stristr($tty, "ttyd0") or stristr($tty, "ttyu0")) {
1034
			if(isset($config['system']['enableserial'])) {
1035
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	on	secure\n");
1036
			} else {
1037
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	off	secure\n");
1038
			}
1039
		} else {
1040
			fwrite($fd, $tty . "\n");
1041
		}
1042
	}
1043
	fclose($fd);
1044
	auto_login();
1045

    
1046
	conf_mount_ro();
1047
	return;
1048
}
1049

    
1050
function print_value_list($list, $count = 10, $separator = ",") {
1051
	$list = implode($separator, array_slice($list, 0, $count));
1052
	if(count($list) < $count) {
1053
		$list .= ".";
1054
	} else {
1055
		$list .= "...";
1056
	}
1057
	return $list;
1058
}
1059

    
1060
/* DHCP enabled on any interfaces? */
1061
function is_dhcp_server_enabled() {
1062
	global $config;
1063

    
1064
	if (!is_array($config['dhcpd']))
1065
		return false;
1066

    
1067
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1068
		if (isset($dhcpifconf['enable']) && !empty($config['interfaces'][$dhcpif]))
1069
			return true;
1070
	}
1071

    
1072
	return false;
1073
}
1074

    
1075
/* DHCP enabled on any interfaces? */
1076
function is_dhcpv6_server_enabled() {
1077
	global $config;
1078

    
1079
	if (is_array($config['interfaces'])) {
1080
		foreach ($config['interfaces'] as $ifcfg) {
1081
			if (isset($ifcfg['enable']) && !empty($ifcfg['track6-interface']))
1082
				return true;
1083
		}
1084
	}
1085

    
1086
	if (!is_array($config['dhcpdv6']))
1087
		return false;
1088

    
1089
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
1090
		if (isset($dhcpv6ifconf['enable']) && !empty($config['interfaces'][$dhcpv6if]))
1091
			return true;
1092
	}
1093

    
1094
	return false;
1095
}
1096

    
1097
/* radvd enabled on any interfaces? */
1098
function is_radvd_enabled() {
1099
	global $config;
1100

    
1101
	if (!is_array($config['dhcpdv6']))
1102
		$config['dhcpdv6'] = array();
1103

    
1104
	$dhcpdv6cfg = $config['dhcpdv6'];
1105
	$Iflist = get_configured_interface_list();
1106

    
1107
	/* handle manually configured DHCP6 server settings first */
1108
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1109
		if(!isset($config['interfaces'][$dhcpv6if]['enable']))
1110
			continue;
1111

    
1112
		if(!isset($dhcpv6ifconf['ramode']))
1113
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
1114

    
1115
		if($dhcpv6ifconf['ramode'] == "disabled")
1116
			continue;
1117

    
1118
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1119
		if(!is_ipaddrv6($ifcfgipv6))
1120
			continue;
1121

    
1122
		return true;
1123
	}
1124

    
1125
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
1126
	foreach ($Iflist as $if => $ifdescr) {
1127
		if(!isset($config['interfaces'][$if]['track6-interface']))
1128
			continue;
1129
		if(!isset($config['interfaces'][$if]['enable']))
1130
			continue;
1131

    
1132
		$ifcfgipv6 = get_interface_ipv6($if);
1133
		if(!is_ipaddrv6($ifcfgipv6))
1134
			continue;
1135

    
1136
		$ifcfgsnv6 = get_interface_subnetv6($if);
1137
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1138

    
1139
		if(!is_ipaddrv6($subnetv6))
1140
			continue;
1141

    
1142
		return true;
1143
	}
1144

    
1145
	return false;
1146
}
1147

    
1148
/* Any PPPoE servers enabled? */
1149
function is_pppoe_server_enabled() {
1150
	global $config;
1151

    
1152
	$pppoeenable = false;
1153

    
1154
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe']))
1155
		return false;
1156

    
1157
	foreach ($config['pppoes']['pppoe'] as $pppoes)
1158
		if ($pppoes['mode'] == 'server')
1159
			$pppoeenable = true;
1160

    
1161
	return $pppoeenable;
1162
}
1163

    
1164
function convert_seconds_to_hms($sec){
1165
	$min=$hrs=0;
1166
	if ($sec != 0){
1167
		$min = floor($sec/60);
1168
		$sec %= 60;
1169
	}
1170
	if ($min != 0){
1171
		$hrs = floor($min/60);
1172
		$min %= 60;
1173
	}
1174
	if ($sec < 10)
1175
		$sec = "0".$sec;
1176
	if ($min < 10)
1177
		$min = "0".$min;
1178
	if ($hrs < 10)
1179
		$hrs = "0".$hrs;
1180
	$result = $hrs.":".$min.":".$sec;
1181
	return $result;
1182
}
1183

    
1184
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1185

    
1186
function get_ppp_uptime($port){
1187
	if (file_exists("/conf/{$port}.log")){
1188
		$saved_time = file_get_contents("/conf/{$port}.log");
1189
		$uptime_data = explode("\n",$saved_time);
1190
		$sec=0;
1191
		foreach($uptime_data as $upt) {
1192
			$sec += substr($upt, 1 + strpos($upt, " "));
1193
		}
1194
		return convert_seconds_to_hms($sec);
1195
	} else {
1196
		$total_time = gettext("No history data found!");
1197
		return $total_time;
1198
	}
1199
}
1200

    
1201
//returns interface information
1202
function get_interface_info($ifdescr) {
1203
	global $config, $g;
1204

    
1205
	$ifinfo = array();
1206
	if (empty($config['interfaces'][$ifdescr]))
1207
		return;
1208
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1209
	$ifinfo['if'] = get_real_interface($ifdescr);
1210

    
1211
	$chkif = $ifinfo['if'];
1212
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1213
	$ifinfo['status'] = $ifinfotmp['status'];
1214
	if (empty($ifinfo['status']))
1215
		$ifinfo['status'] = "down";
1216
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1217
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1218
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1219
	$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
1220
	$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
1221
	$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
1222
	if (isset($ifinfotmp['link0']))
1223
		$link0 = "down";
1224
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1225
	// $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1226
	// $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1227
	$ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1228
	$ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1229
	$ifinfo['collisions'] = $ifinfotmp['collisions'];
1230

    
1231
	/* Use pfctl for non wrapping 64 bit counters */
1232
	/* Pass */
1233
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1234
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1235
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1236
	$pf_in6_pass = preg_split("/ +/ ", $pfctlstats[7]);
1237
	$pf_out6_pass = preg_split("/ +/", $pfctlstats[9]);
1238
	$in4_pass = $pf_in4_pass[5];
1239
	$out4_pass = $pf_out4_pass[5];
1240
	$in4_pass_packets = $pf_in4_pass[3];
1241
	$out4_pass_packets = $pf_out4_pass[3];
1242
	$in6_pass = $pf_in6_pass[5];
1243
	$out6_pass = $pf_out6_pass[5];
1244
	$in6_pass_packets = $pf_in6_pass[3];
1245
	$out6_pass_packets = $pf_out6_pass[3];
1246
	$ifinfo['inbytespass'] = $in4_pass + $in6_pass;
1247
	$ifinfo['outbytespass'] = $out4_pass + $out6_pass;
1248
	$ifinfo['inpktspass'] = $in4_pass_packets + $in6_pass_packets;
1249
	$ifinfo['outpktspass'] = $out4_pass_packets + $out6_pass_packets;
1250

    
1251
	/* Block */
1252
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1253
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1254
	$pf_in6_block = preg_split("/ +/", $pfctlstats[8]);
1255
	$pf_out6_block = preg_split("/ +/", $pfctlstats[10]);
1256
	$in4_block = $pf_in4_block[5];
1257
	$out4_block = $pf_out4_block[5];
1258
	$in4_block_packets = $pf_in4_block[3];
1259
	$out4_block_packets = $pf_out4_block[3];
1260
	$in6_block = $pf_in6_block[5];
1261
	$out6_block = $pf_out6_block[5];
1262
	$in6_block_packets = $pf_in6_block[3];
1263
	$out6_block_packets = $pf_out6_block[3];
1264
	$ifinfo['inbytesblock'] = $in4_block + $in6_block;
1265
	$ifinfo['outbytesblock'] = $out4_block + $out6_block;
1266
	$ifinfo['inpktsblock'] = $in4_block_packets + $in6_block_packets;
1267
	$ifinfo['outpktsblock'] = $out4_block_packets + $out6_block_packets;
1268

    
1269
	$ifinfo['inbytes'] = $in4_pass + $in6_pass;
1270
	$ifinfo['outbytes'] = $out4_pass + $out6_pass;
1271
	$ifinfo['inpkts'] = $in4_pass_packets + $in6_pass_packets;
1272
	$ifinfo['outpkts'] = $out4_pass_packets + $out6_pass_packets;
1273

    
1274
	$ifconfiginfo = "";
1275
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1276
	switch ($link_type) {
1277
	/* DHCP? -> see if dhclient is up */
1278
	case "dhcp":
1279
		/* see if dhclient is up */
1280
		if (find_dhclient_process($ifinfo['if']) <> "")
1281
			$ifinfo['dhcplink'] = "up";
1282
		else
1283
			$ifinfo['dhcplink'] = "down";
1284

    
1285
		break;
1286
	/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1287
	case "pppoe":
1288
	case "pptp":
1289
	case "l2tp":
1290
		if ($ifinfo['status'] == "up" && !isset($link0))
1291
			/* get PPPoE link status for dial on demand */
1292
			$ifinfo["{$link_type}link"] = "up";
1293
		else
1294
			$ifinfo["{$link_type}link"] = "down";
1295

    
1296
		break;
1297
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1298
	case "ppp":
1299
		if ($ifinfo['status'] == "up")
1300
			$ifinfo['ppplink'] = "up";
1301
		else
1302
			$ifinfo['ppplink'] = "down" ;
1303

    
1304
		if (empty($ifinfo['status']))
1305
			$ifinfo['status'] = "down";
1306

    
1307
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1308
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1309
				if ($config['interfaces'][$ifdescr]['if'] == $ppp['if'])
1310
					break;
1311
			}
1312
		}
1313
		$dev = $ppp['ports'];
1314
		if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev))
1315
			break;
1316
		if (!file_exists($dev)) {
1317
			$ifinfo['nodevice'] = 1;
1318
			$ifinfo['pppinfo'] = $dev . " " . gettext("device not present! Is the modem attached to the system?");
1319
		}
1320

    
1321
		$usbmodemoutput = array();
1322
		exec("usbconfig", $usbmodemoutput);
1323
		$mondev = "{$g['tmp_path']}/3gstats.{$ifdescr}";
1324
		if(file_exists($mondev)) {
1325
			$cellstats = file($mondev);
1326
			/* skip header */
1327
			$a_cellstats = explode(",", $cellstats[1]);
1328
			if(preg_match("/huawei/i", implode("\n", $usbmodemoutput))) {
1329
				$ifinfo['cell_rssi'] = huawei_rssi_to_string($a_cellstats[1]);
1330
				$ifinfo['cell_mode'] = huawei_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1331
				$ifinfo['cell_simstate'] = huawei_simstate_to_string($a_cellstats[10]);
1332
				$ifinfo['cell_service'] = huawei_service_to_string(trim($a_cellstats[11]));
1333
			}
1334
			if(preg_match("/zte/i", implode("\n", $usbmodemoutput))) {
1335
				$ifinfo['cell_rssi'] = zte_rssi_to_string($a_cellstats[1]);
1336
				$ifinfo['cell_mode'] = zte_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1337
				$ifinfo['cell_simstate'] = zte_simstate_to_string($a_cellstats[10]);
1338
				$ifinfo['cell_service'] = zte_service_to_string(trim($a_cellstats[11]));
1339
			}
1340
			$ifinfo['cell_upstream'] = $a_cellstats[4];
1341
			$ifinfo['cell_downstream'] = trim($a_cellstats[5]);
1342
			$ifinfo['cell_sent'] = $a_cellstats[6];
1343
			$ifinfo['cell_received'] = trim($a_cellstats[7]);
1344
			$ifinfo['cell_bwupstream'] = $a_cellstats[8];
1345
			$ifinfo['cell_bwdownstream'] = trim($a_cellstats[9]);
1346
		}
1347
		// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1348
		if (isset($ppp['uptime']))
1349
			$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1350
		break;
1351
	default:
1352
		break;
1353
	}
1354

    
1355
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1356
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1357
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1358
	}
1359

    
1360
	if ($ifinfo['status'] == "up") {
1361
		/* try to determine media with ifconfig */
1362
		unset($ifconfiginfo);
1363
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1364
		$wifconfiginfo = array();
1365
		if(is_interface_wireless($ifdescr)) {
1366
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1367
			array_shift($wifconfiginfo);
1368
		}
1369
		$matches = "";
1370
		foreach ($ifconfiginfo as $ici) {
1371

    
1372
			/* don't list media/speed for wireless cards, as it always
1373
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1374
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1375
				$ifinfo['media'] = $matches[1];
1376
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1377
				$ifinfo['media'] = $matches[1];
1378
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1379
				$ifinfo['media'] = $matches[1];
1380
			}
1381

    
1382
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1383
				if ($matches[1] != "active")
1384
					$ifinfo['status'] = $matches[1];
1385
				if($ifinfo['status'] == gettext("running"))
1386
					$ifinfo['status'] = gettext("up");
1387
			}
1388
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1389
				$ifinfo['channel'] = $matches[1];
1390
			}
1391
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1392
				if ($matches[1][0] == '"')
1393
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1394
				else
1395
					$ifinfo['ssid'] = $matches[1];
1396
			}
1397
			if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
1398
				$ifinfo['laggproto'] = $matches[1];
1399
			}
1400
			if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
1401
				$ifinfo['laggport'][] = $matches[1];
1402
			}
1403
		}
1404
		foreach($wifconfiginfo as $ici) {
1405
			$elements = preg_split("/[ ]+/i", $ici);
1406
			if ($elements[0] != "") {
1407
				$ifinfo['bssid'] = $elements[0];
1408
			}
1409
			if ($elements[3] != "") {
1410
				$ifinfo['rate'] = $elements[3];
1411
			}
1412
			if ($elements[4] != "") {
1413
				$ifinfo['rssi'] = $elements[4];
1414
			}
1415

    
1416
		}
1417
		/* lookup the gateway */
1418
		if (interface_has_gateway($ifdescr)) {
1419
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1420
			$ifinfo['gatewayv6'] = get_interface_gateway_v6($ifdescr);
1421
		}
1422
	}
1423

    
1424
	$bridge = "";
1425
	$bridge = link_interface_to_bridge($ifdescr);
1426
	if($bridge) {
1427
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1428
		if(stristr($bridge_text, "blocking") <> false) {
1429
			$ifinfo['bridge'] = "<b><font color='red'>" . gettext("blocking") . "</font></b> - " . gettext("check for ethernet loops");
1430
			$ifinfo['bridgeint'] = $bridge;
1431
		} else if(stristr($bridge_text, "learning") <> false) {
1432
			$ifinfo['bridge'] = gettext("learning");
1433
			$ifinfo['bridgeint'] = $bridge;
1434
		} else if(stristr($bridge_text, "forwarding") <> false) {
1435
			$ifinfo['bridge'] = gettext("forwarding");
1436
			$ifinfo['bridgeint'] = $bridge;
1437
		}
1438
	}
1439

    
1440
	return $ifinfo;
1441
}
1442

    
1443
//returns cpu speed of processor. Good for determining capabilities of machine
1444
function get_cpu_speed() {
1445
	return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1446
}
1447

    
1448
function get_uptime_sec() {
1449
	$boottime = "";
1450
	$matches = "";
1451
	exec("/sbin/sysctl -n kern.boottime", $boottime);
1452
	preg_match("/sec = (\d+)/", $boottime[0], $matches);
1453
	$boottime = $matches[1];
1454
	if(intval($boottime) == 0)
1455
		return 0;
1456

    
1457
	$uptime = time() - $boottime;
1458
	return $uptime;
1459
}
1460

    
1461
function add_hostname_to_watch($hostname) {
1462
	if(!is_dir("/var/db/dnscache")) {
1463
		mkdir("/var/db/dnscache");
1464
	}
1465
	$result = array();
1466
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1467
		$domrecords = array();
1468
		$domips = array();
1469
		exec("host -t A $hostname", $domrecords, $rethost);
1470
		if($rethost == 0) {
1471
			foreach($domrecords as $domr) {
1472
				$doml = explode(" ", $domr);
1473
				$domip = $doml[3];
1474
				/* fill array with domain ip addresses */
1475
				if(is_ipaddr($domip)) {
1476
					$domips[] = $domip;
1477
				}
1478
			}
1479
		}
1480
		sort($domips);
1481
		$contents = "";
1482
		if(! empty($domips)) {
1483
			foreach($domips as $ip) {
1484
				$contents .= "$ip\n";
1485
			}
1486
		}
1487
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1488
		/* Remove empty elements */
1489
		$result = array_filter(explode("\n", $contents), 'strlen');
1490
	}
1491
	return $result;
1492
}
1493

    
1494
function is_fqdn($fqdn) {
1495
	$hostname = false;
1496
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1497
		$hostname = true;
1498
	}
1499
	if(preg_match("/\.\./", $fqdn)) {
1500
		$hostname = false;
1501
	}
1502
	if(preg_match("/^\./i", $fqdn)) {
1503
		$hostname = false;
1504
	}
1505
	if(preg_match("/\//i", $fqdn)) {
1506
		$hostname = false;
1507
	}
1508
	return($hostname);
1509
}
1510

    
1511
function pfsense_default_state_size() {
1512
	/* get system memory amount */
1513
	$memory = get_memory();
1514
	$physmem = $memory[0];
1515
	/* Be cautious and only allocate 10% of system memory to the state table */
1516
	$max_states = (int) ($physmem/10)*1000;
1517
	return $max_states;
1518
}
1519

    
1520
function pfsense_default_tables_size() {
1521
	$current = `pfctl -sm | grep ^tables | awk '{print $4};'`;
1522
	return $current;
1523
}
1524

    
1525
function pfsense_default_table_entries_size() {
1526
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1527
	return $current;
1528
}
1529

    
1530
/* Compare the current hostname DNS to the DNS cache we made
1531
 * if it has changed we return the old records
1532
 * if no change we return false */
1533
function compare_hostname_to_dnscache($hostname) {
1534
	if(!is_dir("/var/db/dnscache")) {
1535
		mkdir("/var/db/dnscache");
1536
	}
1537
	$hostname = trim($hostname);
1538
	if(is_readable("/var/db/dnscache/{$hostname}")) {
1539
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1540
	} else {
1541
		$oldcontents = "";
1542
	}
1543
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1544
		$domrecords = array();
1545
		$domips = array();
1546
		exec("host -t A $hostname", $domrecords, $rethost);
1547
		if($rethost == 0) {
1548
			foreach($domrecords as $domr) {
1549
				$doml = explode(" ", $domr);
1550
				$domip = $doml[3];
1551
				/* fill array with domain ip addresses */
1552
				if(is_ipaddr($domip)) {
1553
					$domips[] = $domip;
1554
				}
1555
			}
1556
		}
1557
		sort($domips);
1558
		$contents = "";
1559
		if(! empty($domips)) {
1560
			foreach($domips as $ip) {
1561
				$contents .= "$ip\n";
1562
			}
1563
		}
1564
	}
1565

    
1566
	if(trim($oldcontents) != trim($contents)) {
1567
		if($g['debug']) {
1568
			log_error(sprintf(gettext('DNSCACHE: Found old IP %1$s and new IP %2$s'), $oldcontents, $contents));
1569
		}
1570
		return ($oldcontents);
1571
	} else {
1572
		return false;
1573
	}
1574
}
1575

    
1576
/*
1577
 * load_crypto() - Load crypto modules if enabled in config.
1578
 */
1579
function load_crypto() {
1580
	global $config, $g;
1581
	$crypto_modules = array('glxsb', 'aesni');
1582

    
1583
	if (!in_array($config['system']['crypto_hardware'], $crypto_modules))
1584
		return false;
1585

    
1586
	if (!empty($config['system']['crypto_hardware']) && !is_module_loaded($config['system']['crypto_hardware'])) {
1587
		log_error("Loading {$config['system']['crypto_hardware']} cryptographic accelerator module.");
1588
		mwexec("/sbin/kldload {$config['system']['crypto_hardware']}");
1589
	}
1590
}
1591

    
1592
/*
1593
 * load_thermal_hardware() - Load temperature monitor kernel module
1594
 */
1595
function load_thermal_hardware() {
1596
	global $config, $g;
1597
	$thermal_hardware_modules = array('coretemp', 'amdtemp');
1598

    
1599
	if (!in_array($config['system']['thermal_hardware'], $thermal_hardware_modules))
1600
		return false;
1601

    
1602
	if (!empty($config['system']['thermal_hardware']) && !is_module_loaded($config['system']['thermal_hardware'])) {
1603
		log_error("Loading {$config['system']['thermal_hardware']} thermal monitor module.");
1604
		mwexec("/sbin/kldload {$config['system']['thermal_hardware']}");
1605
	}
1606
}
1607

    
1608
/****f* pfsense-utils/isvm
1609
 * NAME
1610
 *   isvm
1611
 * INPUTS
1612
 *	none
1613
 * RESULT
1614
 *   returns true if machine is running under a virtual environment
1615
 ******/
1616
function isvm() {
1617
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1618
	$bios_product = trim(`/bin/kenv | /usr/bin/awk -F= '/smbios.system.product/ {print $2}'`);
1619
	foreach ($virtualenvs as $virtualenv)
1620
		if (stripos($bios_product, $virtualenv) !== false)
1621
			return true;
1622

    
1623
	return false;
1624
}
1625

    
1626
function get_freebsd_version() {
1627
	$version = php_uname("r");
1628
	return $version[0];
1629
}
1630

    
1631
function download_file($url, $destination, $verify_ssl = false, $connect_timeout = 60, $timeout = 0) {
1632
	global $config, $g;
1633

    
1634
	$fp = fopen($destination, "wb");
1635

    
1636
	if (!$fp)
1637
		return false;
1638

    
1639
	$ch = curl_init();
1640
	curl_setopt($ch, CURLOPT_URL, $url);
1641
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
1642
	curl_setopt($ch, CURLOPT_FILE, $fp);
1643
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1644
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1645
	curl_setopt($ch, CURLOPT_HEADER, false);
1646
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1647
	curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
1648

    
1649
	if (!empty($config['system']['proxyurl'])) {
1650
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1651
		if (!empty($config['system']['proxyport']))
1652
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1653
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1654
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1655
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1656
		}
1657
	}
1658

    
1659
	@curl_exec($ch);
1660
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1661
	fclose($fp);
1662
	curl_close($ch);
1663
	return ($http_code == 200) ? true : $http_code;
1664
}
1665

    
1666
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body', $connect_timeout=60, $timeout=0) {
1667
	global $ch, $fout, $file_size, $downloaded, $config, $first_progress_update;
1668
	$file_size  = 1;
1669
	$downloaded = 1;
1670
	$first_progress_update = TRUE;
1671
	/* open destination file */
1672
	$fout = fopen($destination_file, "wb");
1673

    
1674
	/*
1675
	 *      Originally by Author: Keyvan Minoukadeh
1676
	 *      Modified by Scott Ullrich to return Content-Length size
1677
	 */
1678

    
1679
	$ch = curl_init();
1680
	curl_setopt($ch, CURLOPT_URL, $url_file);
1681
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1682
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1683
	/* Don't verify SSL peers since we don't have the certificates to do so. */
1684
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1685
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1686
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1687
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1688
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1689

    
1690
	if (!empty($config['system']['proxyurl'])) {
1691
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1692
		if (!empty($config['system']['proxyport']))
1693
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1694
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1695
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1696
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1697
		}
1698
	}
1699

    
1700
	@curl_exec($ch);
1701
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1702
	if($fout)
1703
		fclose($fout);
1704
	curl_close($ch);
1705
	return ($http_code == 200) ? true : $http_code;
1706
}
1707

    
1708
function read_header($ch, $string) {
1709
	global $file_size, $fout;
1710
	$length = strlen($string);
1711
	$regs = "";
1712
	preg_match("/(Content-Length:) (.*)/", $string, $regs);
1713
	if($regs[2] <> "") {
1714
		$file_size = intval($regs[2]);
1715
	}
1716
	ob_flush();
1717
	return $length;
1718
}
1719

    
1720
function read_body($ch, $string) {
1721
	global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $first_progress_update;
1722
	global $pkg_interface;
1723
	$length = strlen($string);
1724
	$downloaded += intval($length);
1725
	if($file_size > 0) {
1726
		$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1727
		$downloadProgress = 100 - $downloadProgress;
1728
	} else
1729
		$downloadProgress = 0;
1730
	if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1731
		if($sendto == "status") {
1732
			if($pkg_interface == "console") {
1733
				if(($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1734
					$tostatus = $static_status . $downloadProgress . "%";
1735
					if ($downloadProgress == 100) {
1736
						$tostatus = $tostatus . "\r";
1737
					}
1738
					update_status($tostatus);
1739
				}
1740
			} else {
1741
				$tostatus = $static_status . $downloadProgress . "%";
1742
				update_status($tostatus);
1743
			}
1744
		} else {
1745
			if($pkg_interface == "console") {
1746
				if(($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1747
					$tooutput = $static_output . $downloadProgress . "%";
1748
					if ($downloadProgress == 100) {
1749
						$tooutput = $tooutput . "\r";
1750
					}
1751
					update_output_window($tooutput);
1752
				}
1753
			} else {
1754
				$tooutput = $static_output . $downloadProgress . "%";
1755
				update_output_window($tooutput);
1756
			}
1757
		}
1758
				if(($pkg_interface != "console") || (($downloadProgress % 10) == 0) || ($downloadProgress < 10)) {
1759
					update_progress_bar($downloadProgress, $first_progress_update);
1760
					$first_progress_update = FALSE;
1761
				}
1762
		$lastseen = $downloadProgress;
1763
	}
1764
	if($fout)
1765
		fwrite($fout, $string);
1766
	ob_flush();
1767
	return $length;
1768
}
1769

    
1770
/*
1771
 *   update_output_window: update bottom textarea dynamically.
1772
 */
1773
function update_output_window($text) {
1774
	global $pkg_interface;
1775
	$log = preg_replace("/\n/", "\\n", $text);
1776
	if($pkg_interface != "console") {
1777
		echo "\n<script type=\"text/javascript\">";
1778
		echo "\n//<![CDATA[";
1779
		echo "\nthis.document.forms[0].output.value = \"" . $log . "\";";
1780
		echo "\nthis.document.forms[0].output.scrollTop = this.document.forms[0].output.scrollHeight;";
1781
		echo "\n//]]>";
1782
		echo "\n</script>";
1783
	}
1784
	/* ensure that contents are written out */
1785
	ob_flush();
1786
}
1787

    
1788
/*
1789
 *   update_status: update top textarea dynamically.
1790
 */
1791
function update_status($status) {
1792
	global $pkg_interface;
1793
	if($pkg_interface == "console") {
1794
		echo "\r{$status}";
1795
	} else {
1796
		echo "\n<script type=\"text/javascript\">";
1797
		echo "\n//<![CDATA[";
1798
		echo "\nthis.document.forms[0].status.value=\"" . $status . "\";";
1799
		echo "\n//]]>";
1800
		echo "\n</script>";
1801
	}
1802
	/* ensure that contents are written out */
1803
	ob_flush();
1804
}
1805

    
1806
/*
1807
 * update_progress_bar($percent, $first_time): updates the javascript driven progress bar.
1808
 */
1809
function update_progress_bar($percent, $first_time) {
1810
	global $pkg_interface;
1811
	if($percent > 100) $percent = 1;
1812
	if($pkg_interface <> "console") {
1813
		echo "\n<script type=\"text/javascript\">";
1814
		echo "\n//<![CDATA[";
1815
		echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1816
		echo "\n//]]>";
1817
		echo "\n</script>";
1818
	} else {
1819
		if(!($first_time))
1820
			echo "\x08\x08\x08\x08\x08";
1821
		echo sprintf("%4d%%", $percent);
1822
	}
1823
}
1824

    
1825
/* 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. */
1826
if(!function_exists("split")) {
1827
	function split($separator, $haystack, $limit = null) {
1828
		log_error("deprecated split() call with separator '{$separator}'");
1829
		return preg_split($separator, $haystack, $limit);
1830
	}
1831
}
1832

    
1833
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
1834
	global $g, $config, $pconfig, $debug;
1835
	if(!$origname)
1836
		return;
1837

    
1838
	$sectionref = &$config;
1839
	foreach($section as $sectionname) {
1840
		if(is_array($sectionref) && isset($sectionref[$sectionname]))
1841
			$sectionref = &$sectionref[$sectionname];
1842
		else
1843
			return;
1844
	}
1845

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

    
1849
	if(is_array($sectionref)) {
1850
		foreach($sectionref as $itemkey => $item) {
1851
			if($debug) fwrite($fd, "$itemkey\n");
1852

    
1853
			$fieldfound = true;
1854
			$fieldref = &$sectionref[$itemkey];
1855
			foreach($field as $fieldname) {
1856
				if(is_array($fieldref) && isset($fieldref[$fieldname]))
1857
					$fieldref = &$fieldref[$fieldname];
1858
				else {
1859
					$fieldfound = false;
1860
					break;
1861
				}
1862
			}
1863
			if($fieldfound && $fieldref == $origname) {
1864
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1865
				$fieldref = $new_alias_name;
1866
			}
1867
		}
1868
	}
1869

    
1870
	if($debug) fclose($fd);
1871

    
1872
}
1873

    
1874
function update_alias_url_data() {
1875
	global $config, $g;
1876

    
1877
	/* item is a url type */
1878
	$lockkey = lock('config');
1879
	if (is_array($config['aliases']['alias'])) {
1880
		foreach ($config['aliases']['alias'] as $x => $alias) {
1881
			if (empty($alias['aliasurl']))
1882
				continue;
1883

    
1884
			$address = "";
1885
			$isfirst = 0;
1886
			foreach ($alias['aliasurl'] as $alias_url) {
1887
				/* fetch down and add in */
1888
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
1889
				unlink($temp_filename);
1890
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
1891
				mwexec("/bin/mkdir -p {$temp_filename}");
1892
				download_file($alias_url, $temp_filename . "/aliases", $verify_ssl);
1893

    
1894
				/* if the item is tar gzipped then extract */
1895
				if (stristr($alias_url, ".tgz"))
1896
					process_alias_tgz($temp_filename);
1897
				else if (stristr($alias_url, ".zip"))
1898
					process_alias_unzip($temp_filename);
1899
				if (file_exists("{$temp_filename}/aliases")) {
1900
					$file_contents = file_get_contents("{$temp_filename}/aliases");
1901
					$file_contents = str_replace("#", "\n#", $file_contents);
1902
					$file_contents_split = explode("\n", $file_contents);
1903
					foreach ($file_contents_split as $fc) {
1904
						$tmp = trim($fc);
1905
						if (stristr($fc, "#")) {
1906
							$tmp_split = explode("#", $tmp);
1907
							$tmp = trim($tmp_split[0]);
1908
						}
1909
						if (trim($tmp) <> "") {
1910
							if ($isfirst == 1)
1911
								$address .= " ";
1912
							$address .= $tmp;
1913
							$isfirst = 1;
1914
						}
1915
					}
1916
					mwexec("/bin/rm -rf {$temp_filename}");
1917
				}
1918
			}
1919
			if($isfirst > 0) {
1920
				$config['aliases']['alias'][$x]['address'] = $address;
1921
				$updated = true;
1922
			}
1923
		}
1924
	}
1925
	if ($updated)
1926
		write_config();
1927
	unlock($lockkey);
1928
}
1929

    
1930
function process_alias_unzip($temp_filename) {
1931
	if(!file_exists("/usr/local/bin/unzip"))
1932
		return;
1933
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.zip");
1934
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
1935
	unlink("{$temp_filename}/aliases.zip");
1936
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1937
	/* foreach through all extracted files and build up aliases file */
1938
	$fd = fopen("{$temp_filename}/aliases", "w");
1939
	foreach($files_to_process as $f2p) {
1940
		$file_contents = file_get_contents($f2p);
1941
		fwrite($fd, $file_contents);
1942
		unlink($f2p);
1943
	}
1944
	fclose($fd);
1945
}
1946

    
1947
function process_alias_tgz($temp_filename) {
1948
	if(!file_exists("/usr/bin/tar"))
1949
		return;
1950
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
1951
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
1952
	unlink("{$temp_filename}/aliases.tgz");
1953
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1954
	/* foreach through all extracted files and build up aliases file */
1955
	$fd = fopen("{$temp_filename}/aliases", "w");
1956
	foreach($files_to_process as $f2p) {
1957
		$file_contents = file_get_contents($f2p);
1958
		fwrite($fd, $file_contents);
1959
		unlink($f2p);
1960
	}
1961
	fclose($fd);
1962
}
1963

    
1964
function version_compare_dates($a, $b) {
1965
	$a_time = strtotime($a);
1966
	$b_time = strtotime($b);
1967

    
1968
	if ((!$a_time) || (!$b_time)) {
1969
		return FALSE;
1970
	} else {
1971
		if ($a_time < $b_time)
1972
			return -1;
1973
		elseif ($a_time == $b_time)
1974
			return 0;
1975
		else
1976
			return 1;
1977
	}
1978
}
1979
function version_get_string_value($a) {
1980
	$strs = array(
1981
		0 => "ALPHA-ALPHA",
1982
		2 => "ALPHA",
1983
		3 => "BETA",
1984
		4 => "B",
1985
		5 => "C",
1986
		6 => "D",
1987
		7 => "RC",
1988
		8 => "RELEASE",
1989
		9 => "*"			// Matches all release levels
1990
	);
1991
	$major = 0;
1992
	$minor = 0;
1993
	foreach ($strs as $num => $str) {
1994
		if (substr($a, 0, strlen($str)) == $str) {
1995
			$major = $num;
1996
			$n = substr($a, strlen($str));
1997
			if (is_numeric($n))
1998
				$minor = $n;
1999
			break;
2000
		}
2001
	}
2002
	return "{$major}.{$minor}";
2003
}
2004
function version_compare_string($a, $b) {
2005
	// Only compare string parts if both versions give a specific release
2006
	// (If either version lacks a string part, assume intended to match all release levels)
2007
	if (isset($a) && isset($b))
2008
		return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
2009
	else
2010
		return 0;
2011
}
2012
function version_compare_numeric($a, $b) {
2013
	$a_arr = explode('.', rtrim($a, '.0'));
2014
	$b_arr = explode('.', rtrim($b, '.0'));
2015

    
2016
	foreach ($a_arr as $n => $val) {
2017
		if (array_key_exists($n, $b_arr)) {
2018
			// So far so good, both have values at this minor version level. Compare.
2019
			if ($val > $b_arr[$n])
2020
				return 1;
2021
			elseif ($val < $b_arr[$n])
2022
				return -1;
2023
		} else {
2024
			// a is greater, since b doesn't have any minor version here.
2025
			return 1;
2026
		}
2027
	}
2028
	if (count($b_arr) > count($a_arr)) {
2029
		// b is longer than a, so it must be greater.
2030
		return -1;
2031
	} else {
2032
		// Both a and b are of equal length and value.
2033
		return 0;
2034
	}
2035
}
2036
function pfs_version_compare($cur_time, $cur_text, $remote) {
2037
	// First try date compare
2038
	$v = version_compare_dates($cur_time, $remote);
2039
	if ($v === FALSE) {
2040
		// If that fails, try to compare by string
2041
		// Before anything else, simply test if the strings are equal
2042
		if (($cur_text == $remote) || ($cur_time == $remote))
2043
			return 0;
2044
		list($cur_num, $cur_str) = explode('-', $cur_text);
2045
		list($rem_num, $rem_str) = explode('-', $remote);
2046

    
2047
		// First try to compare the numeric parts of the version string.
2048
		$v = version_compare_numeric($cur_num, $rem_num);
2049

    
2050
		// If the numeric parts are the same, compare the string parts.
2051
		if ($v == 0)
2052
			return version_compare_string($cur_str, $rem_str);
2053
	}
2054
	return $v;
2055
}
2056
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
2057
	global $config;
2058

    
2059
	$urltable_prefix = "/var/db/aliastables/";
2060
	$urltable_filename = $urltable_prefix . $name . ".txt";
2061

    
2062
	// Make the aliases directory if it doesn't exist
2063
	if (!file_exists($urltable_prefix)) {
2064
		mkdir($urltable_prefix);
2065
	} elseif (!is_dir($urltable_prefix)) {
2066
		unlink($urltable_prefix);
2067
		mkdir($urltable_prefix);
2068
	}
2069

    
2070
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
2071
	if (!file_exists($urltable_filename)
2072
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400))
2073
		|| $forceupdate) {
2074

    
2075
		// Try to fetch the URL supplied
2076
		conf_mount_rw();
2077
		unlink_if_exists($urltable_filename . ".tmp");
2078
		$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2079
		if (download_file($url, $urltable_filename . ".tmp", $verify_ssl)) {
2080
			mwexec("/usr/bin/sed -E 's/\;.*//g; /^[[:space:]]*($|#)/d' ". escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
2081
			if (alias_get_type($name) == "urltable_ports") {
2082
				$ports = explode("\n", file_get_contents($urltable_filename));
2083
				$ports = group_ports($ports);
2084
				file_put_contents($urltable_filename, implode("\n", $ports));
2085
			}
2086
			unlink_if_exists($urltable_filename . ".tmp");
2087
		} else
2088
			mwexec("/usr/bin/touch {$urltable_filename}");
2089
		conf_mount_ro();
2090
		return true;
2091
	} else {
2092
		// File exists, and it doesn't need updated.
2093
		return -1;
2094
	}
2095
}
2096
function get_real_slice_from_glabel($label) {
2097
	$label = escapeshellarg($label);
2098
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
2099
}
2100
function nanobsd_get_boot_slice() {
2101
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
2102
}
2103
function nanobsd_get_boot_drive() {
2104
	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`);
2105
}
2106
function nanobsd_get_active_slice() {
2107
	$boot_drive = nanobsd_get_boot_drive();
2108
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
2109

    
2110
	return "{$boot_drive}s{$active}";
2111
}
2112
function nanobsd_get_size() {
2113
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
2114
}
2115
function nanobsd_switch_boot_slice() {
2116
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2117
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2118
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2119
	nanobsd_detect_slice_info();
2120

    
2121
	if ($BOOTFLASH == $ACTIVE_SLICE) {
2122
		$slice = $TOFLASH;
2123
	} else {
2124
		$slice = $BOOTFLASH;
2125
	}
2126

    
2127
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
2128
	ob_implicit_flush(1);
2129
	if(strstr($slice, "s2")) {
2130
		$ASLICE="2";
2131
		$AOLDSLICE="1";
2132
		$AGLABEL_SLICE="pfsense1";
2133
		$AUFS_ID="1";
2134
		$AOLD_UFS_ID="0";
2135
	} else {
2136
		$ASLICE="1";
2137
		$AOLDSLICE="2";
2138
		$AGLABEL_SLICE="pfsense0";
2139
		$AUFS_ID="0";
2140
		$AOLD_UFS_ID="1";
2141
	}
2142
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
2143
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
2144
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
2145
	conf_mount_rw();
2146
	exec("sysctl kern.geom.debugflags=16");
2147
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
2148
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
2149
	// We can't update these if they are mounted now.
2150
	if ($BOOTFLASH != $slice) {
2151
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
2152
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
2153
	}
2154
	exec("/sbin/sysctl kern.geom.debugflags=0");
2155
	conf_mount_ro();
2156
}
2157
function nanobsd_clone_slice() {
2158
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2159
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2160
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2161
	nanobsd_detect_slice_info();
2162

    
2163
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
2164
	ob_implicit_flush(1);
2165
	exec("/sbin/sysctl kern.geom.debugflags=16");
2166
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
2167
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
2168
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
2169
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
2170
	exec("/sbin/sysctl kern.geom.debugflags=0");
2171
	if($status) {
2172
		return false;
2173
	} else {
2174
		return true;
2175
	}
2176
}
2177
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
2178
	$tmppath = "/tmp/{$gslice}";
2179
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
2180

    
2181
	exec("/bin/mkdir {$tmppath}");
2182
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
2183
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
2184
	exec("/bin/cp /etc/fstab {$fstabpath}");
2185

    
2186
	if (!file_exists($fstabpath)) {
2187
		$fstab = <<<EOF
2188
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
2189
/dev/ufs/cf /cf ufs ro,noatime 1 1
2190
EOF;
2191
		if (file_put_contents($fstabpath, $fstab))
2192
			$status = true;
2193
		else
2194
			$status = false;
2195
	} else {
2196
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
2197
	}
2198
	exec("/sbin/umount {$tmppath}");
2199
	exec("/bin/rmdir {$tmppath}");
2200

    
2201
	return $status;
2202
}
2203
function nanobsd_detect_slice_info() {
2204
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2205
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2206
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2207

    
2208
	$BOOT_DEVICE=nanobsd_get_boot_slice();
2209
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
2210
	$BOOT_DRIVE=nanobsd_get_boot_drive();
2211
	$ACTIVE_SLICE=nanobsd_get_active_slice();
2212

    
2213
	// Detect which slice is active and set information.
2214
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
2215
		$SLICE="2";
2216
		$OLDSLICE="1";
2217
		$GLABEL_SLICE="pfsense1";
2218
		$UFS_ID="1";
2219
		$OLD_UFS_ID="0";
2220

    
2221
	} else {
2222
		$SLICE="1";
2223
		$OLDSLICE="2";
2224
		$GLABEL_SLICE="pfsense0";
2225
		$UFS_ID="0";
2226
		$OLD_UFS_ID="1";
2227
	}
2228
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2229
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2230
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2231
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2232
}
2233

    
2234
function nanobsd_friendly_slice_name($slicename) {
2235
	global $g;
2236
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
2237
}
2238

    
2239
function get_include_contents($filename) {
2240
	if (is_file($filename)) {
2241
		ob_start();
2242
		include $filename;
2243
		$contents = ob_get_contents();
2244
		ob_end_clean();
2245
		return $contents;
2246
	}
2247
	return false;
2248
}
2249

    
2250
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
2251
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
2252
 * size of the RRD xml dumps this is required.
2253
 * The reason we do not use it for pfSense is that it does not know about array fields
2254
 * which causes it to fail on array fields with single items. Possible Todo?
2255
 */
2256
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
2257
{
2258
	if (!function_exists('xml_parser_create'))
2259
	{
2260
		return array ();
2261
	}
2262
	$parser = xml_parser_create('');
2263
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
2264
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
2265
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
2266
	xml_parse_into_struct($parser, trim($contents), $xml_values);
2267
	xml_parser_free($parser);
2268
	if (!$xml_values)
2269
		return; //Hmm...
2270
	$xml_array = array ();
2271
	$parents = array ();
2272
	$opened_tags = array ();
2273
	$arr = array ();
2274
	$current = & $xml_array;
2275
	$repeated_tag_index = array ();
2276
	foreach ($xml_values as $data)
2277
	{
2278
		unset ($attributes, $value);
2279
		extract($data);
2280
		$result = array ();
2281
		$attributes_data = array ();
2282
		if (isset ($value))
2283
		{
2284
			if ($priority == 'tag')
2285
				$result = $value;
2286
			else
2287
				$result['value'] = $value;
2288
		}
2289
		if (isset ($attributes) and $get_attributes)
2290
		{
2291
			foreach ($attributes as $attr => $val)
2292
			{
2293
				if ($priority == 'tag')
2294
					$attributes_data[$attr] = $val;
2295
				else
2296
					$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2297
			}
2298
		}
2299
		if ($type == "open")
2300
		{
2301
			$parent[$level -1] = & $current;
2302
			if (!is_array($current) or (!in_array($tag, array_keys($current))))
2303
			{
2304
				$current[$tag] = $result;
2305
				if ($attributes_data)
2306
					$current[$tag . '_attr'] = $attributes_data;
2307
				$repeated_tag_index[$tag . '_' . $level] = 1;
2308
				$current = & $current[$tag];
2309
			}
2310
			else
2311
			{
2312
				if (isset ($current[$tag][0]))
2313
				{
2314
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2315
					$repeated_tag_index[$tag . '_' . $level]++;
2316
				}
2317
				else
2318
				{
2319
					$current[$tag] = array (
2320
						$current[$tag],
2321
						$result
2322
						);
2323
					$repeated_tag_index[$tag . '_' . $level] = 2;
2324
					if (isset ($current[$tag . '_attr']))
2325
					{
2326
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2327
						unset ($current[$tag . '_attr']);
2328
					}
2329
				}
2330
				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2331
				$current = & $current[$tag][$last_item_index];
2332
			}
2333
		}
2334
		elseif ($type == "complete")
2335
		{
2336
			if (!isset ($current[$tag]))
2337
			{
2338
				$current[$tag] = $result;
2339
				$repeated_tag_index[$tag . '_' . $level] = 1;
2340
				if ($priority == 'tag' and $attributes_data)
2341
					$current[$tag . '_attr'] = $attributes_data;
2342
			}
2343
			else
2344
			{
2345
				if (isset ($current[$tag][0]) and is_array($current[$tag]))
2346
				{
2347
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2348
					if ($priority == 'tag' and $get_attributes and $attributes_data)
2349
					{
2350
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2351
					}
2352
					$repeated_tag_index[$tag . '_' . $level]++;
2353
				}
2354
				else
2355
				{
2356
					$current[$tag] = array (
2357
						$current[$tag],
2358
						$result
2359
						);
2360
					$repeated_tag_index[$tag . '_' . $level] = 1;
2361
					if ($priority == 'tag' and $get_attributes)
2362
					{
2363
						if (isset ($current[$tag . '_attr']))
2364
						{
2365
							$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2366
							unset ($current[$tag . '_attr']);
2367
						}
2368
						if ($attributes_data)
2369
						{
2370
							$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2371
						}
2372
					}
2373
					$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2374
				}
2375
			}
2376
		}
2377
		elseif ($type == 'close')
2378
		{
2379
			$current = & $parent[$level -1];
2380
		}
2381
	}
2382
	return ($xml_array);
2383
}
2384

    
2385
function get_country_name($country_code) {
2386
	if ($country_code != "ALL" && strlen($country_code) != 2)
2387
		return "";
2388

    
2389
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2390
	$country_names_contents = file_get_contents($country_names_xml);
2391
	$country_names = xml2array($country_names_contents);
2392

    
2393
	if($country_code == "ALL") {
2394
		$country_list = array();
2395
		foreach($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2396
			$country_list[] = array("code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2397
						"name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])) );
2398
		}
2399
		return $country_list;
2400
	}
2401

    
2402
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2403
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2404
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2405
		}
2406
	}
2407
	return "";
2408
}
2409

    
2410
/* sort by interface only, retain the original order of rules that apply to
2411
   the same interface */
2412
function filter_rules_sort() {
2413
	global $config;
2414

    
2415
	/* mark each rule with the sequence number (to retain the order while sorting) */
2416
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2417
		$config['filter']['rule'][$i]['seq'] = $i;
2418

    
2419
	usort($config['filter']['rule'], "filter_rules_compare");
2420

    
2421
	/* strip the sequence numbers again */
2422
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2423
		unset($config['filter']['rule'][$i]['seq']);
2424
}
2425
function filter_rules_compare($a, $b) {
2426
	if (isset($a['floating']) && isset($b['floating']))
2427
		return $a['seq'] - $b['seq'];
2428
	else if (isset($a['floating']))
2429
		return -1;
2430
	else if (isset($b['floating']))
2431
		return 1;
2432
	else if ($a['interface'] == $b['interface'])
2433
		return $a['seq'] - $b['seq'];
2434
	else
2435
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2436
}
2437

    
2438
function generate_ipv6_from_mac($mac) {
2439
	$elements = explode(":", $mac);
2440
	if(count($elements) <> 6)
2441
		return false;
2442

    
2443
	$i = 0;
2444
	$ipv6 = "fe80::";
2445
	foreach($elements as $byte) {
2446
		if($i == 0) {
2447
			$hexadecimal =  substr($byte, 1, 2);
2448
			$bitmap = base_convert($hexadecimal, 16, 2);
2449
			$bitmap = str_pad($bitmap, 4, "0", STR_PAD_LEFT);
2450
			$bitmap = substr($bitmap, 0, 2) ."1". substr($bitmap, 3,4);
2451
			$byte = substr($byte, 0, 1) . base_convert($bitmap, 2, 16);
2452
		}
2453
		$ipv6 .= $byte;
2454
		if($i == 1) {
2455
			$ipv6 .= ":";
2456
		}
2457
		if($i == 3) {
2458
			$ipv6 .= ":";
2459
		}
2460
		if($i == 2) {
2461
			$ipv6 .= "ff:fe";
2462
		}
2463

    
2464
		$i++;
2465
	}
2466
	return $ipv6;
2467
}
2468

    
2469
/****f* pfsense-utils/load_mac_manufacturer_table
2470
 * NAME
2471
 *   load_mac_manufacturer_table
2472
 * INPUTS
2473
 *   none
2474
 * RESULT
2475
 *   returns associative array with MAC-Manufacturer pairs
2476
 ******/
2477
function load_mac_manufacturer_table() {
2478
	/* load MAC-Manufacture data from the file */
2479
	$macs = false;
2480
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes"))
2481
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2482
	if ($macs){
2483
		foreach ($macs as $line){
2484
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)){
2485
				/* store values like this $mac_man['000C29']='VMware' */
2486
				$mac_man["$matches[1]"]=$matches[2];
2487
			}
2488
		}
2489
		return $mac_man;
2490
	} else
2491
		return -1;
2492

    
2493
}
2494

    
2495
/****f* pfsense-utils/is_ipaddr_configured
2496
 * NAME
2497
 *   is_ipaddr_configured
2498
 * INPUTS
2499
 *   IP Address to check.
2500
 * RESULT
2501
 *   returns true if the IP Address is
2502
 *   configured and present on this device.
2503
*/
2504
function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false) {
2505
	global $config;
2506

    
2507
	$isipv6 = is_ipaddrv6($ipaddr);
2508

    
2509
	if ($check_subnets) {
2510
		$iflist = get_configured_interface_list();
2511
		foreach ($iflist as $if => $ifname) {
2512
			if ($ignore_if == $if)
2513
				continue;
2514

    
2515
			if ($isipv6 === true) {
2516
				$bitmask = get_interface_subnetv6($if);
2517
				$subnet = gen_subnetv6(get_interface_ipv6($if), $bitmask);
2518
			} else {
2519
				$bitmask = get_interface_subnet($if);
2520
				$subnet = gen_subnet(get_interface_ip($if), $bitmask);
2521
			}
2522

    
2523
			if (ip_in_subnet($ipaddr, $subnet . '/' . $bitmask))
2524
				return true;
2525
		}
2526
	} else {
2527
		if ($isipv6 === true)
2528
			$interface_list_ips = get_configured_ipv6_addresses();
2529
		else
2530
			$interface_list_ips = get_configured_ip_addresses();
2531

    
2532
		foreach($interface_list_ips as $if => $ilips) {
2533
			/* Also ignore CARP interfaces, it'll be checked below */
2534
			if ($ignore_if == $if)
2535
				continue;
2536
			if (strcasecmp($ipaddr, $ilips) == 0)
2537
				return true;
2538
		}
2539
	}
2540

    
2541
	/* XXX: Need to correct this! */
2542
	$interface_list_vips = get_configured_vips_list(true);
2543
	foreach ($interface_list_vips as $id => $vip) {
2544
		if ($ignore_if == "vip_{$id}")
2545
			continue;
2546
		if (strcasecmp($ipaddr, $vip['ipaddr']) == 0)
2547
			return true;
2548
	}
2549

    
2550
	if ($check_localip) {
2551
		if (is_array($config['pptpd']) && !empty($config['pptpd']['localip']) && (strcasecmp($ipaddr, $config['pptpd']['localip']) == 0))
2552
			return true;
2553

    
2554
		if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, $config['l2tp']['localip']) == 0))
2555
			return true;
2556
	}
2557

    
2558
	return false;
2559
}
2560

    
2561
/****f* pfsense-utils/pfSense_handle_custom_code
2562
 * NAME
2563
 *   pfSense_handle_custom_code
2564
 * INPUTS
2565
 *   directory name to process
2566
 * RESULT
2567
 *   globs the directory and includes the files
2568
 */
2569
function pfSense_handle_custom_code($src_dir) {
2570
	// Allow extending of the nat edit page and include custom input validation
2571
	if(is_dir("$src_dir")) {
2572
		$cf = glob($src_dir . "/*.inc");
2573
		foreach($cf as $nf) {
2574
			if($nf == "." || $nf == "..")
2575
				continue;
2576
			// Include the extra handler
2577
			include("$nf");
2578
		}
2579
	}
2580
}
2581

    
2582
function set_language($lang = 'en_US', $encoding = "ISO8859-1") {
2583
	putenv("LANG={$lang}.{$encoding}");
2584
	setlocale(LC_ALL, "{$lang}.{$encoding}");
2585
	textdomain("pfSense");
2586
	bindtextdomain("pfSense","/usr/local/share/locale");
2587
	bind_textdomain_codeset("pfSense","{$lang}.{$encoding}");
2588
}
2589

    
2590
function get_locale_list() {
2591
	$locales = array(
2592
		"en_US" => gettext("English"),
2593
		"pt_BR" => gettext("Portuguese (Brazil)"),
2594
		"tr" => gettext("Turkish"),
2595
	);
2596
	asort($locales);
2597
	return $locales;
2598
}
2599

    
2600
function system_get_language_code() {
2601
	global $config, $g_languages;
2602

    
2603
	// a language code, as per [RFC3066]
2604
	$language = $config['system']['language'];
2605
	//$code = $g_languages[$language]['code'];
2606
	$code = str_replace("_", "-", $language);
2607

    
2608
	if (empty($code))
2609
		$code = "en-US"; // Set default code.
2610

    
2611
	return $code;
2612
}
2613

    
2614
function system_get_language_codeset() {
2615
	global $config, $g_languages;
2616

    
2617
	$language = $config['system']['language'];
2618
	$codeset = $g_languages[$language]['codeset'];
2619

    
2620
	if (empty($codeset))
2621
		$codeset = "UTF-8"; // Set default codeset.
2622

    
2623
	return $codeset;
2624
}
2625

    
2626
/* Available languages/locales */
2627
$g_languages = array (
2628
	"sq"    => array("codeset" => "UTF-8", "desc" => gettext("Albanian")),
2629
	"bg"    => array("codeset" => "UTF-8", "desc" => gettext("Bulgarian")),
2630
	"zh_CN" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Simplified)")),
2631
	"zh_TW" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Traditional)")),
2632
	"nl"    => array("codeset" => "UTF-8", "desc" => gettext("Dutch")),
2633
	"da"    => array("codeset" => "UTF-8", "desc" => gettext("Danish")),
2634
	"en_US" => array("codeset" => "ISO-8859-1", "desc" => gettext("English")),
2635
	"fi"    => array("codeset" => "UTF-8", "desc" => gettext("Finnish")),
2636
	"fr"    => array("codeset" => "UTF-8", "desc" => gettext("French")),
2637
	"de"    => array("codeset" => "UTF-8", "desc" => gettext("German")),
2638
	"el"    => array("codeset" => "UTF-8", "desc" => gettext("Greek")),
2639
	"hu"    => array("codeset" => "UTF-8", "desc" => gettext("Hungarian")),
2640
	"it"    => array("codeset" => "UTF-8", "desc" => gettext("Italian")),
2641
	"ja"    => array("codeset" => "UTF-8", "desc" => gettext("Japanese")),
2642
	"ko"    => array("codeset" => "UTF-8", "desc" => gettext("Korean")),
2643
	"lv"    => array("codeset" => "UTF-8", "desc" => gettext("Latvian")),
2644
	"nb"    => array("codeset" => "UTF-8", "desc" => gettext("Norwegian (Bokmal)")),
2645
	"pl"    => array("codeset" => "UTF-8", "desc" => gettext("Polish")),
2646
	"pt_BR" => array("codeset" => "ISO-8859-1", "desc" => gettext("Portuguese (Brazil)")),
2647
	"pt"    => array("codeset" => "UTF-8", "desc" => gettext("Portuguese (Portugal)")),
2648
	"ro"    => array("codeset" => "UTF-8", "desc" => gettext("Romanian")),
2649
	"ru"    => array("codeset" => "UTF-8", "desc" => gettext("Russian")),
2650
	"sl"    => array("codeset" => "UTF-8", "desc" => gettext("Slovenian")),
2651
	"tr"    => array("codeset" => "UTF-8", "desc" => gettext("Turkish")),
2652
	"es"    => array("codeset" => "UTF-8", "desc" => gettext("Spanish")),
2653
	"sv"    => array("codeset" => "UTF-8", "desc" => gettext("Swedish")),
2654
	"sk"    => array("codeset" => "UTF-8", "desc" => gettext("Slovak")),
2655
	"cs"    => array("codeset" => "UTF-8", "desc" => gettext("Czech"))
2656
);
2657

    
2658
function return_hex_ipv4($ipv4) {
2659
	if(!is_ipaddrv4($ipv4))
2660
		return(false);
2661

    
2662
	/* we need the hex form of the interface IPv4 address */
2663
	$ip4arr = explode(".", $ipv4);
2664
	return (sprintf("%02x%02x%02x%02x", $ip4arr[0], $ip4arr[1], $ip4arr[2], $ip4arr[3]));
2665
}
2666

    
2667
function convert_ipv6_to_128bit($ipv6) {
2668
	if(!is_ipaddrv6($ipv6))
2669
		return(false);
2670

    
2671
	$ip6arr = array();
2672
	$ip6prefix = Net_IPv6::uncompress($ipv6);
2673
	$ip6arr = explode(":", $ip6prefix);
2674
	/* binary presentation of the prefix for all 128 bits. */
2675
	$ip6prefixbin = "";
2676
	foreach($ip6arr as $element) {
2677
		$ip6prefixbin .= sprintf("%016b", hexdec($element));
2678
	}
2679
	return($ip6prefixbin);
2680
}
2681

    
2682
function convert_128bit_to_ipv6($ip6bin) {
2683
	if(strlen($ip6bin) <> 128)
2684
		return(false);
2685

    
2686
	$ip6arr = array();
2687
	$ip6binarr = array();
2688
	$ip6binarr = str_split($ip6bin, 16);
2689
	foreach($ip6binarr as $binpart)
2690
		$ip6arr[] = dechex(bindec($binpart));
2691
	$ip6addr = Net_IPv6::compress(implode(":", $ip6arr));
2692

    
2693
	return($ip6addr);
2694
}
2695

    
2696

    
2697
/* Returns the calculated bit length of the prefix delegation from the WAN interface */
2698
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
2699
/* 6rd is variable, calculate from 64 - (v6 prefixlen - (32 - v4 prefixlen)) */
2700
/* 6to4 is 16 bits, e.g. 65535 */
2701
function calculate_ipv6_delegation_length($if) {
2702
	global $config;
2703

    
2704
	if(!is_array($config['interfaces'][$if]))
2705
		return false;
2706

    
2707
	switch($config['interfaces'][$if]['ipaddrv6']) {
2708
		case "6to4":
2709
			$pdlen = 16;
2710
			break;
2711
		case "6rd":
2712
			$rd6cfg = $config['interfaces'][$if];
2713
			$rd6plen = explode("/", $rd6cfg['prefix-6rd']);
2714
			$pdlen = (64 - ($rd6plen[1] + (32 - $rd6cfg['prefix-6rd-v4plen'])));
2715
			break;
2716
		case "dhcp6":
2717
			$dhcp6cfg = $config['interfaces'][$if];
2718
			$pdlen = $dhcp6cfg['dhcp6-ia-pd-len'];
2719
			break;
2720
		default:
2721
			$pdlen = 0;
2722
			break;
2723
	}
2724
	return($pdlen);
2725
}
2726

    
2727
function huawei_rssi_to_string($rssi) {
2728
	$dbm = array();
2729
	$i = 0;
2730
	$dbstart = -113;
2731
	while($i < 32) {
2732
		$dbm[$i] = $dbstart + ($i * 2);
2733
		$i++;
2734
	}
2735
	$percent = round(($rssi / 31) * 100);
2736
	$string = "rssi:{$rssi} level:{$dbm[$rssi]}dBm percent:{$percent}%";
2737
	return $string;
2738
}
2739

    
2740
function huawei_mode_to_string($mode, $submode) {
2741
	$modes[0] = "None";
2742
	$modes[1] = "AMPS";
2743
	$modes[2] = "CDMA";
2744
	$modes[3] = "GSM/GPRS";
2745
	$modes[4] = "HDR";
2746
	$modes[5] = "WCDMA";
2747
	$modes[6] = "GPS";
2748

    
2749
	$submodes[0] = "No Service";
2750
	$submodes[1] = "GSM";
2751
	$submodes[2] = "GPRS";
2752
	$submodes[3] = "EDGE";
2753
	$submodes[4] = "WCDMA";
2754
	$submodes[5] = "HSDPA";
2755
	$submodes[6] = "HSUPA";
2756
	$submodes[7] = "HSDPA+HSUPA";
2757
	$submodes[8] = "TD-SCDMA";
2758
	$submodes[9] = "HSPA+";
2759
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
2760
	return $string;
2761
}
2762

    
2763
function huawei_service_to_string($state) {
2764
	$modes[0] = "No";
2765
	$modes[1] = "Restricted";
2766
	$modes[2] = "Valid";
2767
	$modes[3] = "Restricted Regional";
2768
	$modes[4] = "Powersaving";
2769
	$string = "{$modes[$state]} Service";
2770
	return $string;
2771
}
2772

    
2773
function huawei_simstate_to_string($state) {
2774
	$modes[0] = "Invalid SIM/locked";
2775
	$modes[1] = "Valid SIM";
2776
	$modes[2] = "Invalid SIM CS";
2777
	$modes[3] = "Invalid SIM PS";
2778
	$modes[4] = "Invalid SIM CS/PS";
2779
	$modes[255] = "Missing SIM";
2780
	$string = "{$modes[$state]} State";
2781
	return $string;
2782
}
2783

    
2784
function zte_rssi_to_string($rssi) {
2785
	return huawei_rssi_to_string($rssi);
2786
}
2787

    
2788
function zte_mode_to_string($mode, $submode) {
2789
	$modes[0] = "No Service";
2790
	$modes[1] = "Limited Service";
2791
	$modes[2] = "GPRS";
2792
	$modes[3] = "GSM";
2793
	$modes[4] = "UMTS";
2794
	$modes[5] = "EDGE";
2795
	$modes[6] = "HSDPA";
2796

    
2797
	$submodes[0] = "CS_ONLY";
2798
	$submodes[1] = "PS_ONLY";
2799
	$submodes[2] = "CS_PS";
2800
	$submodes[3] = "CAMPED";
2801
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
2802
	return $string;
2803
}
2804

    
2805
function zte_service_to_string($state) {
2806
	$modes[0] = "Initializing";
2807
	$modes[1] = "Network Lock error";
2808
	$modes[2] = "Network Locked";
2809
	$modes[3] = "Unlocked or correct MCC/MNC";
2810
	$string = "{$modes[$state]} Service";
2811
	return $string;
2812
}
2813

    
2814
function zte_simstate_to_string($state) {
2815
	$modes[0] = "No action";
2816
	$modes[1] = "Network lock";
2817
	$modes[2] = "(U)SIM card lock";
2818
	$modes[3] = "Network Lock and (U)SIM card Lock";
2819
	$string = "{$modes[$state]} State";
2820
	return $string;
2821
}
2822

    
2823
function get_configured_pppoe_server_interfaces() {
2824
	global $config;
2825
	$iflist = array();
2826
	if (is_array($config['pppoes']['pppoe'])) {
2827
		foreach($config['pppoes']['pppoe'] as $pppoe) {
2828
			if ($pppoe['mode'] == "server") {
2829
				$int = "poes". $pppoe['pppoeid'];
2830
				$iflist[$int] = strtoupper($int);
2831
			}
2832
		}
2833
	}
2834
	return $iflist;
2835
}
2836

    
2837
function get_pppoes_child_interfaces($ifpattern) {
2838
	$if_arr = array();
2839
	if($ifpattern == "")
2840
		return;
2841

    
2842
	exec("ifconfig", $out, $ret);
2843
	foreach($out as $line) {
2844
		if(preg_match("/^({$ifpattern}[0-9]+):/i", $line, $match)) {
2845
			$if_arr[] = $match[1];
2846
		}
2847
	}
2848
	return $if_arr;
2849

    
2850
}
2851

    
2852
?>
(39-39/66)