Project

General

Profile

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

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

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

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

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

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

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

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

    
142
/****f* pfsense-utils/get_dns_servers
143
 * NAME
144
 *   get_dns_servres - get system dns servers
145
 * INPUTS
146
 *   $dns_servers - an array of the dns servers
147
 * RESULT
148
 *   null
149
 ******/
150
function get_dns_servers() {
151
	$dns_servers = array();
152
	$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
153
	foreach($dns_s as $dns) {
154
		$matches = "";
155
		if (preg_match("/nameserver (.*)/", $dns, $matches))
156
			$dns_servers[] = $matches[1];
157
	}
158
	return array_unique($dns_servers);
159
}
160

    
161
/****f* pfsense-utils/enable_hardware_offloading
162
 * NAME
163
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
164
 * INPUTS
165
 *   $interface	- string containing the physical interface to work on.
166
 * RESULT
167
 *   null
168
 * NOTES
169
 *   This function only supports the fxp driver's loadable microcode.
170
 ******/
171
function enable_hardware_offloading($interface) {
172
	global $g, $config;
173

    
174
	if(isset($config['system']['do_not_use_nic_microcode']))
175
		return;
176

    
177
	/* translate wan, lan, opt -> real interface if needed */
178
	$int = get_real_interface($interface);
179
	if(empty($int)) 
180
		return;
181
	$int_family = preg_split("/[0-9]+/", $int);
182
	$supported_ints = array('fxp');
183
	if (in_array($int_family, $supported_ints)) {
184
		if(does_interface_exist($int)) 
185
			pfSense_interface_flags($int, IFF_LINK0);
186
	}
187

    
188
	return;
189
}
190

    
191
/****f* pfsense-utils/interface_supports_polling
192
 * NAME
193
 *   checks to see if an interface supports polling according to man polling
194
 * INPUTS
195
 *
196
 * RESULT
197
 *   true or false
198
 * NOTES
199
 *
200
 ******/
201
function interface_supports_polling($iface) {
202
	$opts = pfSense_get_interface_addresses($iface);
203
	if (is_array($opts) && isset($opts['caps']['polling']))
204
		return true;
205

    
206
	return false;
207
}
208

    
209
/****f* pfsense-utils/is_alias_inuse
210
 * NAME
211
 *   checks to see if an alias is currently in use by a rule
212
 * INPUTS
213
 *
214
 * RESULT
215
 *   true or false
216
 * NOTES
217
 *
218
 ******/
219
function is_alias_inuse($alias) {
220
	global $g, $config;
221

    
222
	if($alias == "") return false;
223
	/* loop through firewall rules looking for alias in use */
224
	if(is_array($config['filter']['rule']))
225
		foreach($config['filter']['rule'] as $rule) {
226
			if($rule['source']['address'])
227
				if($rule['source']['address'] == $alias)
228
					return true;
229
			if($rule['destination']['address'])
230
				if($rule['destination']['address'] == $alias)
231
					return true;
232
		}
233
	/* loop through nat rules looking for alias in use */
234
	if(is_array($config['nat']['rule']))
235
		foreach($config['nat']['rule'] as $rule) {
236
			if($rule['target'] && $rule['target'] == $alias)
237
				return true;
238
			if($rule['source']['address'] && $rule['source']['address'] == $alias)
239
				return true;
240
			if($rule['destination']['address'] && $rule['destination']['address'] == $alias)
241
				return true;
242
		}
243
	return false;
244
}
245

    
246
/****f* pfsense-utils/is_schedule_inuse
247
 * NAME
248
 *   checks to see if a schedule is currently in use by a rule
249
 * INPUTS
250
 *
251
 * RESULT
252
 *   true or false
253
 * NOTES
254
 *
255
 ******/
256
function is_schedule_inuse($schedule) {
257
	global $g, $config;
258

    
259
	if($schedule == "") return false;
260
	/* loop through firewall rules looking for schedule in use */
261
	if(is_array($config['filter']['rule']))
262
		foreach($config['filter']['rule'] as $rule) {
263
			if($rule['sched'] == $schedule)
264
				return true;
265
		}
266
	return false;
267
}
268

    
269
/****f* pfsense-utils/setup_polling
270
 * NAME
271
 *   sets up polling
272
 * INPUTS
273
 *
274
 * RESULT
275
 *   null
276
 * NOTES
277
 *
278
 ******/
279
function setup_polling() {
280
	global $g, $config;
281

    
282
	if (isset($config['system']['polling']))
283
		mwexec("/sbin/sysctl kern.polling.idle_poll=1");
284
	else
285
		mwexec("/sbin/sysctl kern.polling.idle_poll=0");
286

    
287
	if($config['system']['polling_each_burst'])
288
		mwexec("/sbin/sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}");
289
	if($config['system']['polling_burst_max'])
290
		mwexec("/sbin/sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}");
291
	if($config['system']['polling_user_frac'])
292
		mwexec("/sbin/sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}");
293
}
294

    
295
/****f* pfsense-utils/setup_microcode
296
 * NAME
297
 *   enumerates all interfaces and calls enable_hardware_offloading which
298
 *   enables a NIC's supported hardware features.
299
 * INPUTS
300
 *
301
 * RESULT
302
 *   null
303
 * NOTES
304
 *   This function only supports the fxp driver's loadable microcode.
305
 ******/
306
function setup_microcode() {
307

    
308
	/* if list */
309
	$ifs = get_interface_arr();
310

    
311
	foreach($ifs as $if)
312
		enable_hardware_offloading($if);
313
}
314

    
315
/****f* pfsense-utils/get_carp_status
316
 * NAME
317
 *   get_carp_status - Return whether CARP is enabled or disabled.
318
 * RESULT
319
 *   boolean	- true if CARP is enabled, false if otherwise.
320
 ******/
321
function get_carp_status() {
322
    /* grab the current status of carp */
323
    $status = `/sbin/sysctl -n net.inet.carp.allow`;
324
    return (intval($status) > 0);
325
}
326

    
327
/*
328
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
329

    
330
 */
331
function convert_ip_to_network_format($ip, $subnet) {
332
	$ipsplit = explode('.', $ip);
333
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
334
	return $string;
335
}
336

    
337
/*
338
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
339
 */
340
function get_carp_interface_status($carpinterface) {
341
	$carp_query = "";
342
	exec("/sbin/ifconfig $carpinterface | /usr/bin/grep -v grep | /usr/bin/grep carp:", $carp_query);
343
	foreach($carp_query as $int) {
344
		if(stristr($int, "MASTER")) 
345
			return gettext("MASTER");
346
		if(stristr($int, "BACKUP")) 
347
			return gettext("BACKUP");
348
		if(stristr($int, "INIT")) 
349
			return gettext("INIT");
350
	}
351
	return;
352
}
353

    
354
/*
355
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
356
 */
357
function get_pfsync_interface_status($pfsyncinterface) {
358
    $result = does_interface_exist($pfsyncinterface);
359
    if($result <> true) return;
360
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
361
    return $status;
362
}
363

    
364
/*
365
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
366
 */
367
function add_rule_to_anchor($anchor, $rule, $label) {
368
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
369
}
370

    
371
/*
372
 * remove_text_from_file
373
 * remove $text from file $file
374
 */
375
function remove_text_from_file($file, $text) {
376
	if(!file_exists($file) && !is_writable($file))
377
		return;
378
	$filecontents = file_get_contents($file);
379
	$text = str_replace($text, "", $filecontents);
380
	@file_put_contents($file, $text); 
381
}
382

    
383
/*
384
 * add_text_to_file($file, $text): adds $text to $file.
385
 * replaces the text if it already exists.
386
 */
387
function add_text_to_file($file, $text, $replace = false) {
388
	if(file_exists($file) and is_writable($file)) {
389
		$filecontents = file($file);
390
		$filecontents = array_map('rtrim', $filecontents);
391
		array_push($filecontents, $text);
392
		if ($replace)
393
			$filecontents = array_unique($filecontents);
394

    
395
		$file_text = implode("\n", $filecontents);
396

    
397
		@file_put_contents($file, $file_text); 
398
		return true;
399
	}
400
	return false;
401
}
402

    
403
/*
404
 *   after_sync_bump_adv_skew(): create skew values by 1S
405
 */
406
function after_sync_bump_adv_skew() {
407
	global $config, $g;
408
	$processed_skew = 1;
409
	$a_vip = &$config['virtualip']['vip'];
410
	foreach ($a_vip as $vipent) {
411
		if($vipent['advskew'] <> "") {
412
			$processed_skew = 1;
413
			$vipent['advskew'] = $vipent['advskew']+1;
414
		}
415
	}
416
	if($processed_skew == 1)
417
		write_config(gettext("After synch increase advertising skew"));
418
}
419

    
420
/*
421
 * get_filename_from_url($url): converts a url to its filename.
422
 */
423
function get_filename_from_url($url) {
424
	return basename($url);
425
}
426

    
427
/*
428
 *   get_dir: return an array of $dir
429
 */
430
function get_dir($dir) {
431
	$dir_array = array();
432
	$d = dir($dir);
433
	while (false !== ($entry = $d->read())) {
434
		array_push($dir_array, $entry);
435
	}
436
	$d->close();
437
	return $dir_array;
438
}
439

    
440
/****f* pfsense-utils/WakeOnLan
441
 * NAME
442
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
443
 * RESULT
444
 *   true/false - true if the operation was successful
445
 ******/
446
function WakeOnLan($addr, $mac)
447
{
448
	$addr_byte = explode(':', $mac);
449
	$hw_addr = '';
450

    
451
	for ($a=0; $a < 6; $a++)
452
		$hw_addr .= chr(hexdec($addr_byte[$a]));
453

    
454
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
455

    
456
	for ($a = 1; $a <= 16; $a++)
457
		$msg .= $hw_addr;
458

    
459
	// send it to the broadcast address using UDP
460
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
461
	if ($s == false) {
462
		log_error(gettext("Error creating socket!"));
463
		log_error(sprintf(gettext("Error code is '%1\$s' - %2\$s"), socket_last_error($s), socket_strerror(socket_last_error($s))));
464
	} else {
465
		// setting a broadcast option to socket:
466
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
467
		if($opt_ret < 0)
468
			log_error(sprintf(gettext("setsockopt() failed, error: %s"), strerror($opt_ret)));
469
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
470
		socket_close($s);
471
		log_error(sprintf(gettext('Magic Packet sent (%1$s) to {%2$s} MAC=%3$s'), $e, $addr, $mac));
472
		return true;
473
	}
474

    
475
	return false;
476
}
477

    
478
/*
479
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
480
 *					 Useful for finding paths and stripping file extensions.
481
 */
482
function reverse_strrchr($haystack, $needle) {
483
	if (!is_string($haystack))
484
		return;
485
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
486
}
487

    
488
/*
489
 *  backup_config_section($section): returns as an xml file string of
490
 *                                   the configuration section
491
 */
492
function backup_config_section($section) {
493
	global $config;
494
	$new_section = &$config[$section];
495
	/* generate configuration XML */
496
	$xmlconfig = dump_xml_config($new_section, $section);
497
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
498
	return $xmlconfig;
499
}
500

    
501
/*
502
 *  restore_config_section($section, new_contents): restore a configuration section,
503
 *                                                  and write the configuration out
504
 *                                                  to disk/cf.
505
 */
506
function restore_config_section($section, $new_contents) {
507
	global $config, $g;
508
	conf_mount_rw();
509
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
510
	fwrite($fout, $new_contents);
511
	fclose($fout);
512
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
513
	if ($section_xml != -1)
514
		$config[$section] = &$section_xml;
515
	@unlink($g['tmp_path'] . "/tmpxml");
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));
519
	disable_security_checks();
520
	conf_mount_ro();
521
	return;
522
}
523

    
524
/*
525
 *  merge_config_section($section, 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, $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);
538
	$config[$section] = $section_xml;
539
	unlink($fname);
540
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section));
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
	conf_mount_rw();
903
	$fd = false;
904
	$tries = 0;
905
	while (!$fd && $tries < 100) {
906
		$fd = fopen("/etc/gettytab", "w");
907
		$tries++;
908
		
909
	}
910
	if (!$fd) {
911
		conf_mount_ro();
912
		log_error(gettext("Enabling auto login was not possible."));
913
		return;
914
	}
915
	foreach($getty_split as $gs) {
916
		if(stristr($gs, ":ht:np:sp#115200") ) {
917
			if($status == true) {
918
				fwrite($fd, "	:ht:np:sp#115200:al=root:\n");
919
			} else {
920
				fwrite($fd, "	:ht:np:sp#115200:\n");
921
			}
922
		} else {
923
			fwrite($fd, "{$gs}\n");
924
		}
925
	}
926
	fclose($fd);
927
	conf_mount_ro();
928
}
929

    
930
function setup_serial_port($when="save", $path="") {
931
	global $g, $config;
932
	conf_mount_rw();
933
	$prefix = "";
934
	if (($when == "upgrade") && (!empty($path)) && is_dir($path.'/boot/'))
935
		$prefix = "/tmp/{$path}";
936
	$boot_config_file = "{$path}/boot.config";
937
	$loader_conf_file = "{$path}/boot/loader.conf";
938
	/* serial console - write out /boot.config */
939
	if(file_exists($boot_config_file))
940
		$boot_config = file_get_contents($boot_config_file);
941
	else
942
		$boot_config = "";
943

    
944
	if($g['platform'] <> "cdrom") {
945
		$boot_config_split = explode("\n", $boot_config);
946
		$fd = fopen($boot_config_file,"w");
947
		if($fd) {
948
			foreach($boot_config_split as $bcs) {
949
				if(stristr($bcs, "-D")) {
950
					/* DONT WRITE OUT, WE'LL DO IT LATER */
951
				} else {
952
					if($bcs <> "")
953
						fwrite($fd, "{$bcs}\n");
954
				}
955
			}
956
			if(isset($config['system']['enableserial'])) {
957
				fwrite($fd, "-D");
958
			}
959
			fclose($fd);
960
		}
961
		/* serial console - write out /boot/loader.conf */
962
		$boot_config = file_get_contents($loader_conf_file);
963
		$boot_config_split = explode("\n", $boot_config);
964
		if(count($boot_config_split) > 0) {
965
			$new_boot_config = array();
966
			// Loop through and only add lines that are not empty, and which
967
			//  do not contain a console directive.
968
			foreach($boot_config_split as $bcs)
969
				if(!empty($bcs)
970
					&& (stripos($bcs, "console") === false)
971
					&& (stripos($bcs, "boot_multicons") === false)
972
					&& (stripos($bcs, "boot_serial") === false))
973
					$new_boot_config[] = $bcs;
974

    
975
			$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : "9600";
976
			if(isset($config['system']['enableserial'])) {
977
				$new_boot_config[] = 'boot_multicons="YES"';
978
				$new_boot_config[] = 'boot_serial="YES"';
979
				$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
980
				$new_boot_config[] = 'console="comconsole,vidconsole"';
981
			} elseif ($g['platform'] == "nanobsd") {
982
				$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
983
				$new_boot_config[] = 'console="comconsole"';
984
			}
985
			file_put_contents($loader_conf_file, implode("\n", $new_boot_config) . "\n");
986
		}
987
	}
988
	$ttys = file_get_contents("/etc/ttys");
989
	$ttys_split = explode("\n", $ttys);
990
	$fd = fopen("/etc/ttys", "w");
991
	foreach($ttys_split as $tty) {
992
		if(stristr($tty, "ttyd0") or stristr($tty, "ttyu0")) {
993
			if(isset($config['system']['enableserial'])) {
994
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	on	secure\n");
995
			} else {
996
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	off	secure\n");
997
			}
998
		} else {
999
			fwrite($fd, $tty . "\n");
1000
		}
1001
	}
1002
	fclose($fd);
1003
	auto_login();
1004

    
1005
	conf_mount_ro();
1006
	return;
1007
}
1008

    
1009
function print_value_list($list, $count = 10, $separator = ",") {
1010
	$list = implode($separator, array_slice($list, 0, $count));
1011
	if(count($list) < $count) {
1012
		$list .= ".";
1013
	} else {
1014
		$list .= "...";
1015
	}
1016
	return $list;
1017
}
1018

    
1019
/* DHCP enabled on any interfaces? */
1020
function is_dhcp_server_enabled() 
1021
{
1022
	global $config;
1023

    
1024
	$dhcpdenable = false;
1025
	
1026
	if (!is_array($config['dhcpd']))
1027
		return false;
1028

    
1029
	$Iflist = get_configured_interface_list();
1030

    
1031
	if(is_array($config['dhcpd'])) {
1032
		foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1033
			if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
1034
				$dhcpdenable = true;
1035
				break;
1036
			}
1037
		}
1038
	}
1039

    
1040
	return $dhcpdenable;
1041
}
1042

    
1043
/* DHCP enabled on any interfaces? */
1044
function is_dhcpv6_server_enabled() 
1045
{
1046
	global $config;
1047

    
1048
	$dhcpdenable = false;
1049
	
1050
	$Iflist = get_configured_interface_list();
1051

    
1052
	foreach($Iflist as $ifname) {
1053
		if($config['interfaces'][$ifname]['track6-interface'] <> "") {
1054
			return true;
1055
		}
1056
	}
1057

    
1058
	if (!is_array($config['dhcpdv6']))
1059
		return false;
1060

    
1061

    
1062
	if(is_array($config['dhcpdv6'])) {
1063
		foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
1064
			if (isset($dhcpv6ifconf['enable']) && isset($Iflist[$dhcpv6if])) {
1065
				$dhcpdenable = true;
1066
				break;
1067
			}
1068
		}
1069
	}
1070

    
1071
	return $dhcpdenable;
1072
}
1073

    
1074
/* Any PPPoE servers enabled? */
1075
function is_pppoe_server_enabled() {
1076
	global $config;
1077

    
1078
	$pppoeenable = false;
1079

    
1080
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe']))
1081
		return false;
1082

    
1083
	foreach ($config['pppoes']['pppoe'] as $pppoes)
1084
		if ($pppoes['mode'] == 'server')
1085
			$pppoeenable = true;
1086

    
1087
	return $pppoeenable;
1088
}
1089

    
1090
function convert_seconds_to_hms($sec){
1091
	$min=$hrs=0;
1092
	if ($sec != 0){
1093
		$min = floor($sec/60);
1094
		$sec %= 60;
1095
	}
1096
	if ($min != 0){
1097
		$hrs = floor($min/60);
1098
		$min %= 60;
1099
	}
1100
	if ($sec < 10)
1101
		$sec = "0".$sec;
1102
	if ($min < 10)
1103
		$min = "0".$min;
1104
	if ($hrs < 10)
1105
		$hrs = "0".$hrs;
1106
	$result = $hrs.":".$min.":".$sec;
1107
	return $result;
1108
}
1109

    
1110
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1111

    
1112
function get_ppp_uptime($port){
1113
	if (file_exists("/conf/{$port}.log")){
1114
    	$saved_time = file_get_contents("/conf/{$port}.log");
1115
    	$uptime_data = explode("\n",$saved_time);
1116
		$sec=0;
1117
		foreach($uptime_data as $upt) {
1118
			$sec += substr($upt, 1 + strpos($upt, " "));
1119
 		}
1120
		return convert_seconds_to_hms($sec);
1121
	} else {
1122
		$total_time = gettext("No history data found!");
1123
		return $total_time;
1124
	}
1125
}
1126

    
1127
//returns interface information
1128
function get_interface_info($ifdescr) {
1129
	global $config, $g;
1130

    
1131
	$ifinfo = array();
1132
	if (empty($config['interfaces'][$ifdescr]))
1133
		return;
1134
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1135
	$ifinfo['if'] = get_real_interface($ifdescr);
1136

    
1137
	$chkif = $ifinfo['if'];
1138
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1139
	$ifinfo['status'] = $ifinfotmp['status'];
1140
	if (empty($ifinfo['status']))
1141
                $ifinfo['status'] = "down";
1142
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1143
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1144
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1145
	$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
1146
	$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
1147
	$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
1148
	if (isset($ifinfotmp['link0']))
1149
		$link0 = "down";
1150
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1151
        // $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1152
        // $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1153
        $ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1154
        $ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1155
        $ifinfo['collisions'] = $ifinfotmp['collisions'];
1156

    
1157
	/* Use pfctl for non wrapping 64 bit counters */
1158
	/* Pass */
1159
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1160
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1161
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1162
	$pf_in6_pass = preg_split("/ +/ ", $pfctlstats[7]);
1163
	$pf_out6_pass = preg_split("/ +/", $pfctlstats[9]);
1164
	$in4_pass = $pf_in4_pass[5];
1165
	$out4_pass = $pf_out4_pass[5];
1166
	$in4_pass_packets = $pf_in4_pass[3];
1167
	$out4_pass_packets = $pf_out4_pass[3];
1168
	$in6_pass = $pf_in6_pass[5];
1169
	$out6_pass = $pf_out6_pass[5];
1170
	$in6_pass_packets = $pf_in6_pass[3];
1171
	$out6_pass_packets = $pf_out6_pass[3];
1172
	$ifinfo['inbytespass'] = $in4_pass + $in6_pass;
1173
	$ifinfo['outbytespass'] = $out4_pass + $out6_pass;
1174
	$ifinfo['inpktspass'] = $in4_pass_packets + $in6_pass_packets;
1175
	$ifinfo['outpktspass'] = $out4_pass_packets + $in6_pass_packets;
1176

    
1177
	/* Block */
1178
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1179
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1180
	$pf_in6_block = preg_split("/ +/", $pfctlstats[8]);
1181
	$pf_out6_block = preg_split("/ +/", $pfctlstats[10]);
1182
	$in4_block = $pf_in4_block[5];
1183
	$out4_block = $pf_out4_block[5];
1184
	$in4_block_packets = $pf_in4_block[3];
1185
	$out4_block_packets = $pf_out4_block[3];
1186
	$in6_block = $pf_in6_block[5];
1187
	$out6_block = $pf_out6_block[5];
1188
	$in6_block_packets = $pf_in6_block[3];
1189
	$out6_block_packets = $pf_out6_block[3];
1190
	$ifinfo['inbytesblock'] = $in4_block + $in6_block;
1191
	$ifinfo['outbytesblock'] = $out4_block + $out6_block;
1192
	$ifinfo['inpktsblock'] = $in4_block_packets + $in6_block_packets;
1193
	$ifinfo['outpktsblock'] = $out4_block_packets + $out6_block_packets;
1194

    
1195
	$ifinfo['inbytes'] = $in4_pass + $in6_pass;
1196
	$ifinfo['outbytes'] = $out4_pass + $out6_pass;
1197
	$ifinfo['inpkts'] = $in4_pass_packets + $in6_pass_packets;
1198
	$ifinfo['outpkts'] = $in4_pass_packets + $out6_pass_packets;
1199
		
1200
	$ifconfiginfo = "";
1201
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1202
	switch ($link_type) {
1203
	 /* DHCP? -> see if dhclient is up */
1204
	case "dhcp":
1205
	case "carpdev-dhcp":
1206
		/* see if dhclient is up */
1207
		if (find_dhclient_process($ifinfo['if']) <> "")
1208
			$ifinfo['dhcplink'] = "up";
1209
		else
1210
			$ifinfo['dhcplink'] = "down";
1211

    
1212
		break;
1213
	/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1214
	case "pppoe":
1215
	case "pptp":
1216
	case "l2tp":
1217
		if ($ifinfo['status'] == "up" && !isset($link0))
1218
			/* get PPPoE link status for dial on demand */
1219
			$ifinfo["{$link_type}link"] = "up";
1220
		else
1221
			$ifinfo["{$link_type}link"] = "down";
1222

    
1223
		break;
1224
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1225
	case "ppp":
1226
		if ($ifinfo['status'] == "up")
1227
			$ifinfo['ppplink'] = "up";
1228
		else
1229
			$ifinfo['ppplink'] = "down" ;
1230

    
1231
		if (empty($ifinfo['status']))
1232
			$ifinfo['status'] = "down";
1233
			
1234
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1235
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1236
				if ($config['interfaces'][$ifdescr]['if'] == $ppp['if'])
1237
					break;
1238
			}
1239
		}
1240
		$dev = $ppp['ports'];
1241
		if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev))
1242
			break;
1243
		if (!file_exists($dev)) {
1244
			$ifinfo['nodevice'] = 1;
1245
			$ifinfo['pppinfo'] = $dev . " " . gettext("device not present! Is the modem attached to the system?");	
1246
		}
1247
		// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1248
		if (isset($ppp['uptime']))
1249
			$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1250
		break;
1251
	default:
1252
		break;
1253
	}
1254
	
1255
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1256
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1257
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1258
	}
1259
	
1260
	if ($ifinfo['status'] == "up") {
1261
		/* try to determine media with ifconfig */
1262
		unset($ifconfiginfo);
1263
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1264
		$wifconfiginfo = array();
1265
		if(is_interface_wireless($ifdescr)) {
1266
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1267
			array_shift($wifconfiginfo);
1268
		}
1269
		$matches = "";
1270
		foreach ($ifconfiginfo as $ici) {
1271

    
1272
			/* don't list media/speed for wireless cards, as it always
1273
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1274
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1275
				$ifinfo['media'] = $matches[1];
1276
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1277
				$ifinfo['media'] = $matches[1];
1278
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1279
				$ifinfo['media'] = $matches[1];
1280
			}
1281

    
1282
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1283
				if ($matches[1] != "active")
1284
					$ifinfo['status'] = $matches[1];
1285
				if($ifinfo['status'] == gettext("running"))
1286
					$ifinfo['status'] = gettext("up");
1287
			}
1288
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1289
				$ifinfo['channel'] = $matches[1];
1290
			}
1291
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1292
				if ($matches[1][0] == '"')
1293
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1294
				else
1295
					$ifinfo['ssid'] = $matches[1];
1296
			}
1297
			if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
1298
				$ifinfo['laggproto'] = $matches[1];
1299
			}
1300
			if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
1301
				$ifinfo['laggport'][] = $matches[1];
1302
			}
1303
		}
1304
		foreach($wifconfiginfo as $ici) {
1305
			$elements = preg_split("/[ ]+/i", $ici);
1306
			if ($elements[0] != "") {
1307
				$ifinfo['bssid'] = $elements[0];
1308
			}
1309
			if ($elements[3] != "") {
1310
				$ifinfo['rate'] = $elements[3];
1311
			}
1312
			if ($elements[4] != "") {
1313
				$ifinfo['rssi'] = $elements[4];
1314
			}
1315

    
1316
		}
1317
		/* lookup the gateway */
1318
		if (interface_has_gateway($ifdescr)) {
1319
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1320
			$ifinfo['gatewayv6'] = get_interface_gateway_v6($ifdescr);
1321
		}
1322
	}
1323

    
1324
	$bridge = "";
1325
	$bridge = link_interface_to_bridge($ifdescr);
1326
	if($bridge) {
1327
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1328
		if(stristr($bridge_text, "blocking") <> false) {
1329
			$ifinfo['bridge'] = "<b><font color='red'>" . gettext("blocking") . "</font></b> - " . gettext("check for ethernet loops");
1330
			$ifinfo['bridgeint'] = $bridge;
1331
		} else if(stristr($bridge_text, "learning") <> false) {
1332
			$ifinfo['bridge'] = gettext("learning");
1333
			$ifinfo['bridgeint'] = $bridge;
1334
		} else if(stristr($bridge_text, "forwarding") <> false) {
1335
			$ifinfo['bridge'] = gettext("forwarding");
1336
			$ifinfo['bridgeint'] = $bridge;
1337
		}
1338
	}
1339

    
1340
	return $ifinfo;
1341
}
1342

    
1343
//returns cpu speed of processor. Good for determining capabilities of machine
1344
function get_cpu_speed() {
1345
	 return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1346
}
1347

    
1348
function add_hostname_to_watch($hostname) {
1349
	if(!is_dir("/var/db/dnscache")) {
1350
		mkdir("/var/db/dnscache");
1351
	}
1352
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1353
		$domrecords = array();
1354
		$domips = array();
1355
		exec("host -t A $hostname", $domrecords, $rethost);
1356
		if($rethost == 0) {
1357
			foreach($domrecords as $domr) {
1358
				$doml = explode(" ", $domr);
1359
				$domip = $doml[3];
1360
				/* fill array with domain ip addresses */
1361
				if(is_ipaddr($domip)) {
1362
					$domips[] = $domip;
1363
				}
1364
			}
1365
		}
1366
		sort($domips);
1367
		$contents = "";
1368
		if(! empty($domips)) {
1369
			foreach($domips as $ip) {
1370
				$contents .= "$ip\n";
1371
			}
1372
		}
1373
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1374
	}
1375
}
1376

    
1377
function is_fqdn($fqdn) {
1378
	$hostname = false;
1379
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1380
		$hostname = true;
1381
	}
1382
	if(preg_match("/\.\./", $fqdn)) {
1383
		$hostname = false;
1384
	}
1385
	if(preg_match("/^\./i", $fqdn)) { 
1386
		$hostname = false;
1387
	}
1388
	if(preg_match("/\//i", $fqdn)) {
1389
		$hostname = false;
1390
	}
1391
	return($hostname);
1392
}
1393

    
1394
function pfsense_default_state_size() {
1395
  /* get system memory amount */
1396
  $memory = get_memory();
1397
  $avail = $memory[0];
1398
  /* Be cautious and only allocate 10% of system memory to the state table */
1399
  $max_states = (int) ($avail/10)*1000;
1400
  return $max_states;
1401
}
1402

    
1403
function pfsense_default_table_entries_size() {
1404
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1405
	return $current;
1406
}
1407

    
1408
/* Compare the current hostname DNS to the DNS cache we made
1409
 * if it has changed we return the old records
1410
 * if no change we return true */
1411
function compare_hostname_to_dnscache($hostname) {
1412
	if(!is_dir("/var/db/dnscache")) {
1413
		mkdir("/var/db/dnscache");
1414
	}
1415
	$hostname = trim($hostname);
1416
	if(is_readable("/var/db/dnscache/{$hostname}")) {
1417
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1418
	} else {
1419
		$oldcontents = "";
1420
	}
1421
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1422
		$domrecords = array();
1423
		$domips = array();
1424
		exec("host -t A $hostname", $domrecords, $rethost);
1425
		if($rethost == 0) {
1426
			foreach($domrecords as $domr) {
1427
				$doml = explode(" ", $domr);
1428
				$domip = $doml[3];
1429
				/* fill array with domain ip addresses */
1430
				if(is_ipaddr($domip)) {
1431
					$domips[] = $domip;
1432
				}
1433
			}
1434
		}
1435
		sort($domips);
1436
		$contents = "";
1437
		if(! empty($domips)) {
1438
			foreach($domips as $ip) {
1439
				$contents .= "$ip\n";
1440
			}
1441
		}
1442
	}
1443

    
1444
	if(trim($oldcontents) != trim($contents)) {
1445
		if($g['debug']) {
1446
			log_error(sprintf(gettext('DNSCACHE: Found old IP %1$s and new IP %2$s'), $oldcontents, $contents));
1447
		}
1448
		return ($oldcontents);
1449
	} else {
1450
		return false;
1451
	}
1452
}
1453

    
1454
/*
1455
 * load_glxsb() - Load the glxsb crypto module if enabled in config.
1456
 */
1457
function load_glxsb() {
1458
	global $config, $g;
1459
	$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
1460
	if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
1461
		mwexec("/sbin/kldload glxsb");
1462
	}
1463
}
1464

    
1465
/****f* pfsense-utils/isvm
1466
 * NAME
1467
 *   isvm
1468
 * INPUTS
1469
 *	 none
1470
 * RESULT
1471
 *   returns true if machine is running under a virtual environment
1472
 ******/
1473
function isvm() {
1474
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1475
	$bios_vendor = strtolower(`/bin/kenv | /usr/bin/awk -F= '/smbios.bios.vendor/ {print $2}'`);
1476
	if(in_array($bios_vendor, $virtualenvs)) 
1477
		return true;
1478
	else
1479
		return false;
1480
}
1481

    
1482
function get_freebsd_version() {
1483
	$version = php_uname("r");
1484
	return $version[0];
1485
}
1486

    
1487
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body', $connect_timeout=60, $timeout=0) {
1488
        global $ch, $fout, $file_size, $downloaded, $config;
1489
        $file_size  = 1;
1490
        $downloaded = 1;
1491
        /* open destination file */
1492
        $fout = fopen($destination_file, "wb");
1493

    
1494
        /*
1495
         *      Originally by Author: Keyvan Minoukadeh
1496
         *      Modified by Scott Ullrich to return Content-Length size
1497
         */
1498

    
1499
        $ch = curl_init();
1500
        curl_setopt($ch, CURLOPT_URL, $url_file);
1501
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1502
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1503
        /* Don't verify SSL peers since we don't have the certificates to do so. */
1504
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1505
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1506
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1507
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1508
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1509

    
1510
	if (!empty($config['system']['proxyurl'])) {
1511
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1512
		if (!empty($config['system']['proxyport']))
1513
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1514
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1515
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1516
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1517
		}
1518
	}
1519

    
1520
        @curl_exec($ch);
1521
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1522
        if($fout)
1523
                fclose($fout);
1524
        curl_close($ch);
1525
        return ($http_code == 200) ? true : $http_code;
1526
}
1527

    
1528
function read_header($ch, $string) {
1529
        global $file_size, $fout;
1530
        $length = strlen($string);
1531
        $regs = "";
1532
        ereg("(Content-Length:) (.*)", $string, $regs);
1533
        if($regs[2] <> "") {
1534
                $file_size = intval($regs[2]);
1535
        }
1536
        ob_flush();
1537
        return $length;
1538
}
1539

    
1540
function read_body($ch, $string) {
1541
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
1542
		global $pkg_interface;
1543
        $length = strlen($string);
1544
        $downloaded += intval($length);
1545
        if($file_size > 0) {
1546
                $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1547
                $downloadProgress = 100 - $downloadProgress;
1548
        } else
1549
                $downloadProgress = 0;
1550
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1551
                if($sendto == "status") {
1552
					if($pkg_interface == "console") {
1553
						if(substr($downloadProgress,2,1) == "0" || count($downloadProgress) < 2) {
1554
                        	$tostatus = $static_status . $downloadProgress . "%";
1555
                        	update_status($tostatus);
1556
						}
1557
					} else {
1558
                        $tostatus = $static_status . $downloadProgress . "%";
1559
                        update_status($tostatus);						
1560
					}
1561
                } else {
1562
					if($pkg_interface == "console") {
1563
						if(substr($downloadProgress,2,1) == "0" || count($downloadProgress) < 2) {
1564
                        	$tooutput = $static_output . $downloadProgress . "%";
1565
                        	update_output_window($tooutput);
1566
						}
1567
					} else {
1568
                        $tooutput = $static_output . $downloadProgress . "%";
1569
                        update_output_window($tooutput);
1570
					}
1571
                }
1572
                update_progress_bar($downloadProgress);
1573
                $lastseen = $downloadProgress;
1574
        }
1575
        if($fout)
1576
                fwrite($fout, $string);
1577
        ob_flush();
1578
        return $length;
1579
}
1580

    
1581
/*
1582
 *   update_output_window: update bottom textarea dynamically.
1583
 */
1584
function update_output_window($text) {
1585
        global $pkg_interface;
1586
        $log = ereg_replace("\n", "\\n", $text);
1587
        if($pkg_interface != "console") {
1588
                echo "\n<script language=\"JavaScript\">\nthis.document.forms[0].output.value = \"" . $log . "\";\n";
1589
				echo "this.document.forms[0].output.scrollTop = this.document.forms[0].output.scrollHeight;\n";	
1590
				echo "</script>";
1591
        }
1592
        /* ensure that contents are written out */
1593
        ob_flush();
1594
}
1595

    
1596
/*
1597
 *   update_output_window: update top textarea dynamically.
1598
 */
1599
function update_status($status) {
1600
        global $pkg_interface;
1601
        if($pkg_interface == "console") {
1602
                echo $status . "\n";
1603
        } else {
1604
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
1605
        }
1606
        /* ensure that contents are written out */
1607
        ob_flush();
1608
}
1609

    
1610
/*
1611
 * update_progress_bar($percent): updates the javascript driven progress bar.
1612
 */
1613
function update_progress_bar($percent) {
1614
        global $pkg_interface;
1615
        if($percent > 100) $percent = 1;
1616
        if($pkg_interface <> "console") {
1617
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
1618
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1619
                echo "\n</script>";
1620
        } else {
1621
                echo " {$percent}%";
1622
        }
1623
}
1624

    
1625
/* 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. */
1626
if(!function_exists("split")) {
1627
	function split($seperator, $haystack, $limit = null) {
1628
		log_error("deprecated split() call with seperator '{$seperator}'");
1629
		return preg_split($seperator, $haystack, $limit);
1630
	}
1631
}
1632

    
1633
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
1634
	global $g, $config, $pconfig, $debug;
1635
	if(!$origname) 
1636
		return;
1637

    
1638
	$sectionref = &$config;
1639
	foreach($section as $sectionname) {
1640
		if(is_array($sectionref) && isset($sectionref[$sectionname]))
1641
			$sectionref = &$sectionref[$sectionname];
1642
		else
1643
			return;
1644
	}
1645

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

    
1649
	if(is_array($sectionref)) {
1650
		foreach($sectionref as $itemkey => $item) {
1651
			if($debug) fwrite($fd, "$itemkey\n");
1652

    
1653
			$fieldfound = true;
1654
			$fieldref = &$sectionref[$itemkey];
1655
			foreach($field as $fieldname) {
1656
				if(is_array($fieldref) && isset($fieldref[$fieldname]))
1657
					$fieldref = &$fieldref[$fieldname];
1658
				else {
1659
					$fieldfound = false;
1660
					break;
1661
				}
1662
			}
1663
			if($fieldfound && $fieldref == $origname) {
1664
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1665
				$fieldref = $new_alias_name;
1666
			}
1667
		}
1668
	}
1669

    
1670
	if($debug) fclose($fd);
1671

    
1672
}
1673

    
1674
function update_alias_url_data() {
1675
	global $config, $g;
1676

    
1677
	/* item is a url type */
1678
	$lockkey = lock('config');
1679
	if (is_array($config['aliases']['alias'])) {
1680
		foreach ($config['aliases']['alias'] as $x => $alias) {
1681
			if (empty($alias['aliasurl']))
1682
				continue;
1683

    
1684
			/* fetch down and add in */
1685
			$isfirst = 0;
1686
			$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
1687
			unlink($temp_filename);
1688
			$fda = fopen("{$g['tmp_path']}/tmpfetch","w");
1689
			fwrite($fda, "/usr/bin/fetch -T 5 -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1690
			fclose($fda);
1691
			mwexec("/bin/mkdir -p {$temp_filename}");
1692
			mwexec("/usr/bin/fetch -T 5 -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1693
			/* if the item is tar gzipped then extract */
1694
			if(stristr($alias['aliasurl'], ".tgz"))
1695
				process_alias_tgz($temp_filename);
1696
			else if(stristr($alias['aliasurl'], ".zip"))
1697
				process_alias_unzip($temp_filename);
1698
			if(file_exists("{$temp_filename}/aliases")) {
1699
				$file_contents = file_get_contents("{$temp_filename}/aliases");
1700
				$file_contents = str_replace("#", "\n#", $file_contents);
1701
				$file_contents_split = explode("\n", $file_contents);
1702
				foreach($file_contents_split as $fc) {
1703
					$tmp = trim($fc);
1704
					if(stristr($fc, "#")) {
1705
						$tmp_split = explode("#", $tmp);
1706
						$tmp = trim($tmp_split[0]);
1707
					}
1708
					if(trim($tmp) <> "") {
1709
						if($isfirst == 1)
1710
							$address .= " ";
1711
						$address .= $tmp;
1712
						$isfirst = 1;
1713
					}
1714
				}
1715
				if($isfirst > 0) {
1716
					$config['aliases']['alias'][$x]['address'] = $address;
1717
					$updated = true;
1718
				}
1719
				mwexec("/bin/rm -rf {$temp_filename}");
1720
			}
1721
		}
1722
	}
1723
	if($updated)
1724
		write_config();
1725
	unlock($lockkey);
1726
}
1727

    
1728
function process_alias_unzip($temp_filename) {
1729
	if(!file_exists("/usr/local/bin/unzip"))
1730
		return;
1731
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.zip");
1732
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
1733
	unlink("{$temp_filename}/aliases.zip");
1734
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1735
	/* foreach through all extracted files and build up aliases file */
1736
	$fd = fopen("{$temp_filename}/aliases", "w");
1737
	foreach($files_to_process as $f2p) {
1738
		$file_contents = file_get_contents($f2p);
1739
		fwrite($fd, $file_contents);
1740
		unlink($f2p);
1741
	}
1742
	fclose($fd);
1743
}
1744

    
1745
function process_alias_tgz($temp_filename) {
1746
	if(!file_exists("/usr/bin/tar"))
1747
		return;
1748
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
1749
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
1750
	unlink("{$temp_filename}/aliases.tgz");
1751
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1752
	/* foreach through all extracted files and build up aliases file */
1753
	$fd = fopen("{$temp_filename}/aliases", "w");
1754
	foreach($files_to_process as $f2p) {
1755
		$file_contents = file_get_contents($f2p);
1756
		fwrite($fd, $file_contents);
1757
		unlink($f2p);
1758
	}
1759
	fclose($fd);
1760
}
1761

    
1762
function version_compare_dates($a, $b) {
1763
	$a_time = strtotime($a);
1764
	$b_time = strtotime($b);
1765

    
1766
	if ((!$a_time) || (!$b_time)) {
1767
		return FALSE;
1768
	} else {
1769
		if ($a_time < $b_time)
1770
			return -1;
1771
		elseif ($$a_time == $b_time)
1772
			return 0;
1773
		else
1774
			return 1;
1775
	}
1776
}
1777
function version_get_string_value($a) {
1778
	$strs = array(
1779
		0 => "ALPHA-ALPHA",
1780
		2 => "ALPHA",
1781
		3 => "BETA",
1782
		4 => "B",
1783
		5 => "C",
1784
		6 => "D",
1785
		7 => "RC",
1786
		8 => "RELEASE"
1787
	);
1788
	$major = 0;
1789
	$minor = 0;
1790
	foreach ($strs as $num => $str) {
1791
		if (substr($a, 0, strlen($str)) == $str) {
1792
			$major = $num;
1793
			$n = substr($a, strlen($str));
1794
			if (is_numeric($n))
1795
				$minor = $n;
1796
			break;
1797
		}
1798
	}
1799
	return "{$major}.{$minor}";
1800
}
1801
function version_compare_string($a, $b) {
1802
	return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
1803
}
1804
function version_compare_numeric($a, $b) {
1805
	$a_arr = explode('.', rtrim($a, '.0'));
1806
	$b_arr = explode('.', rtrim($b, '.0'));
1807

    
1808
	foreach ($a_arr as $n => $val) {
1809
		if (array_key_exists($n, $b_arr)) {
1810
			// So far so good, both have values at this minor version level. Compare.
1811
			if ($val > $b_arr[$n])
1812
				return 1;
1813
			elseif ($val < $b_arr[$n])
1814
				return -1;
1815
		} else {
1816
			// a is greater, since b doesn't have any minor version here.
1817
			return 1;
1818
		}
1819
	}
1820
	if (count($b_arr) > count($a_arr)) {
1821
		// b is longer than a, so it must be greater.
1822
		return -1;
1823
	} else {
1824
		// Both a and b are of equal length and value.
1825
		return 0;
1826
	}
1827
}
1828
function pfs_version_compare($cur_time, $cur_text, $remote) {
1829
	// First try date compare
1830
	$v = version_compare_dates($cur_time, $remote);
1831
	if ($v === FALSE) {
1832
		// If that fails, try to compare by string
1833
		// Before anything else, simply test if the strings are equal
1834
		if (($cur_text == $remote) || ($cur_time == $remote))
1835
			return 0;
1836
		list($cur_num, $cur_str) = explode('-', $cur_text);
1837
		list($rem_num, $rem_str) = explode('-', $remote);
1838

    
1839
		// First try to compare the numeric parts of the version string.
1840
		$v = version_compare_numeric($cur_num, $rem_num);
1841

    
1842
		// If the numeric parts are the same, compare the string parts.
1843
		if ($v == 0)
1844
			return version_compare_string($cur_str, $rem_str);
1845
	}
1846
	return $v;
1847
}
1848
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
1849
	$urltable_prefix = "/var/db/aliastables/";
1850
	$urltable_filename = $urltable_prefix . $name . ".txt";
1851

    
1852
	// Make the aliases directory if it doesn't exist
1853
	if (!file_exists($urltable_prefix)) {
1854
		mkdir($urltable_prefix);
1855
	} elseif (!is_dir($urltable_prefix)) {
1856
		unlink($urltable_prefix);
1857
		mkdir($urltable_prefix);
1858
	}
1859

    
1860
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
1861
	if (!file_exists($urltable_filename)
1862
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400))
1863
		|| $forceupdate) {
1864

    
1865
		// Try to fetch the URL supplied
1866
		conf_mount_rw();
1867
		unlink_if_exists($urltable_filename . ".tmp");
1868
		// Use fetch to grab data since these may be large files, we don't want to process them through PHP if we can help it.
1869
		mwexec("/usr/bin/fetch -T 5 -q -o " . escapeshellarg($urltable_filename . ".tmp") . " " . escapeshellarg($url));
1870
		// Remove comments. Might need some grep-fu to only allow lines that look like IPs/subnets
1871
		if (file_exists($urltable_filename . ".tmp")) {
1872
			mwexec("/usr/bin/grep -v '^#' " . escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
1873
			unlink_if_exists($urltable_filename . ".tmp");
1874
		} else
1875
			mwexec("/usr/bin/touch {$urltable_filename}");
1876
		conf_mount_ro();
1877
		return true;
1878
	} else {
1879
		// File exists, and it doesn't need updated.
1880
		return -1;
1881
	}
1882
}
1883
function get_real_slice_from_glabel($label) {
1884
	$label = escapeshellarg($label);
1885
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1886
}
1887
function nanobsd_get_boot_slice() {
1888
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1889
}
1890
function nanobsd_get_boot_drive() {
1891
	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`);
1892
}
1893
function nanobsd_get_active_slice() {
1894
	$boot_drive = nanobsd_get_boot_drive();
1895
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1896

    
1897
	return "{$boot_drive}s{$active}";
1898
}
1899
function nanobsd_get_size() {
1900
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
1901
}
1902
function nanobsd_switch_boot_slice() {
1903
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1904
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1905
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1906
	nanobsd_detect_slice_info();
1907

    
1908
	if ($BOOTFLASH == $ACTIVE_SLICE) {
1909
		$slice = $TOFLASH;
1910
	} else {
1911
		$slice = $BOOTFLASH;
1912
	}
1913

    
1914
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1915
	ob_implicit_flush(1);
1916
	if(strstr($slice, "s2")) {
1917
		$ASLICE="2";
1918
		$AOLDSLICE="1";
1919
		$AGLABEL_SLICE="pfsense1";
1920
		$AUFS_ID="1";
1921
		$AOLD_UFS_ID="0";
1922
	} else {
1923
		$ASLICE="1";
1924
		$AOLDSLICE="2";
1925
		$AGLABEL_SLICE="pfsense0";
1926
		$AUFS_ID="0";
1927
		$AOLD_UFS_ID="1";
1928
	}
1929
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
1930
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
1931
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
1932
	conf_mount_rw();
1933
	exec("sysctl kern.geom.debugflags=16");
1934
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
1935
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
1936
	// We can't update these if they are mounted now.
1937
	if ($BOOTFLASH != $slice) {
1938
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
1939
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
1940
	}
1941
	exec("/sbin/sysctl kern.geom.debugflags=0");
1942
	conf_mount_ro();
1943
}
1944
function nanobsd_clone_slice() {
1945
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1946
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1947
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1948
	nanobsd_detect_slice_info();
1949

    
1950
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1951
	ob_implicit_flush(1);
1952
	exec("/sbin/sysctl kern.geom.debugflags=16");
1953
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
1954
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
1955
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
1956
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
1957
	exec("/sbin/sysctl kern.geom.debugflags=0");
1958
	if($status) {
1959
		return false;
1960
	} else {
1961
		return true;
1962
	}
1963
}
1964
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
1965
	$tmppath = "/tmp/{$gslice}";
1966
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
1967

    
1968
	exec("/bin/mkdir {$tmppath}");
1969
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
1970
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
1971
	exec("/bin/cp /etc/fstab {$fstabpath}");
1972

    
1973
	if (!file_exists($fstabpath)) {
1974
		$fstab = <<<EOF
1975
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
1976
/dev/ufs/cf /cf ufs ro,noatime 1 1
1977
EOF;
1978
		if (file_put_contents($fstabpath, $fstab))
1979
			$status = true;
1980
		else
1981
			$status = false;
1982
	} else {
1983
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
1984
	}
1985
	exec("/sbin/umount {$tmppath}");
1986
	exec("/bin/rmdir {$tmppath}");
1987

    
1988
	return $status;
1989
}
1990
function nanobsd_detect_slice_info() {
1991
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1992
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1993
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1994

    
1995
	$BOOT_DEVICE=nanobsd_get_boot_slice();
1996
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
1997
	$BOOT_DRIVE=nanobsd_get_boot_drive();
1998
	$ACTIVE_SLICE=nanobsd_get_active_slice();
1999

    
2000
	// Detect which slice is active and set information.
2001
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
2002
		$SLICE="2";
2003
		$OLDSLICE="1";
2004
		$GLABEL_SLICE="pfsense1";
2005
		$UFS_ID="1";
2006
		$OLD_UFS_ID="0";
2007

    
2008
	} else {
2009
		$SLICE="1";
2010
		$OLDSLICE="2";
2011
		$GLABEL_SLICE="pfsense0";
2012
		$UFS_ID="0";
2013
		$OLD_UFS_ID="1";
2014
	}
2015
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2016
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2017
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2018
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2019
}
2020

    
2021
function nanobsd_friendly_slice_name($slicename) {
2022
	global $g;
2023
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
2024
}
2025

    
2026
function get_include_contents($filename) {
2027
    if (is_file($filename)) {
2028
        ob_start();
2029
        include $filename;
2030
        $contents = ob_get_contents();
2031
        ob_end_clean();
2032
        return $contents;
2033
    }
2034
    return false;
2035
}
2036

    
2037
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
2038
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
2039
 * size of the RRD xml dumps this is required.
2040
 * The reason we do not use it for pfSense is that it does not know about array fields
2041
 * which causes it to fail on array fields with single items. Possible Todo?
2042
 */
2043
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
2044
{
2045
	if (!function_exists('xml_parser_create'))
2046
	{
2047
		return array ();
2048
	}
2049
	$parser = xml_parser_create('');
2050
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
2051
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
2052
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
2053
	xml_parse_into_struct($parser, trim($contents), $xml_values);
2054
	xml_parser_free($parser);
2055
	if (!$xml_values)
2056
		return; //Hmm...
2057
	$xml_array = array ();
2058
	$parents = array ();
2059
	$opened_tags = array ();
2060
	$arr = array ();
2061
	$current = & $xml_array;
2062
	$repeated_tag_index = array ();
2063
	foreach ($xml_values as $data)
2064
	{
2065
		unset ($attributes, $value);
2066
		extract($data);
2067
		$result = array ();
2068
		$attributes_data = array ();
2069
		if (isset ($value))
2070
		{
2071
			if ($priority == 'tag')
2072
				$result = $value;
2073
			else
2074
				$result['value'] = $value;
2075
		}
2076
		if (isset ($attributes) and $get_attributes)
2077
		{
2078
			foreach ($attributes as $attr => $val)
2079
			{
2080
				if ($priority == 'tag')
2081
					$attributes_data[$attr] = $val;
2082
				else
2083
					$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2084
			}
2085
		}
2086
		if ($type == "open")
2087
		{
2088
			$parent[$level -1] = & $current;
2089
			if (!is_array($current) or (!in_array($tag, array_keys($current))))
2090
			{
2091
				$current[$tag] = $result;
2092
				if ($attributes_data)
2093
					$current[$tag . '_attr'] = $attributes_data;
2094
				$repeated_tag_index[$tag . '_' . $level] = 1;
2095
				$current = & $current[$tag];
2096
			}
2097
			else
2098
			{
2099
				if (isset ($current[$tag][0]))
2100
				{
2101
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2102
					$repeated_tag_index[$tag . '_' . $level]++;
2103
				}
2104
				else
2105
				{
2106
					$current[$tag] = array (
2107
						$current[$tag],
2108
						$result
2109
						);
2110
					$repeated_tag_index[$tag . '_' . $level] = 2;
2111
					if (isset ($current[$tag . '_attr']))
2112
					{
2113
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2114
						unset ($current[$tag . '_attr']);
2115
					}
2116
				}
2117
				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2118
				$current = & $current[$tag][$last_item_index];
2119
			}
2120
		}
2121
		elseif ($type == "complete")
2122
		{
2123
			if (!isset ($current[$tag]))
2124
			{
2125
				$current[$tag] = $result;
2126
				$repeated_tag_index[$tag . '_' . $level] = 1;
2127
				if ($priority == 'tag' and $attributes_data)
2128
					$current[$tag . '_attr'] = $attributes_data;
2129
			}
2130
			else
2131
			{
2132
				if (isset ($current[$tag][0]) and is_array($current[$tag]))
2133
				{
2134
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2135
					if ($priority == 'tag' and $get_attributes and $attributes_data)
2136
					{
2137
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2138
					}
2139
					$repeated_tag_index[$tag . '_' . $level]++;
2140
				}
2141
				else
2142
				{
2143
					$current[$tag] = array (
2144
						$current[$tag],
2145
						$result
2146
						);
2147
					$repeated_tag_index[$tag . '_' . $level] = 1;
2148
					if ($priority == 'tag' and $get_attributes)
2149
					{
2150
						if (isset ($current[$tag . '_attr']))
2151
						{
2152
							$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2153
							unset ($current[$tag . '_attr']);
2154
						}
2155
						if ($attributes_data)
2156
						{
2157
							$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2158
						}
2159
					}
2160
					$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2161
				}
2162
			}
2163
		}
2164
		elseif ($type == 'close')
2165
		{
2166
			$current = & $parent[$level -1];
2167
		}
2168
	}
2169
	return ($xml_array);
2170
}
2171

    
2172
function get_country_name($country_code) {
2173
	if ($country_code != "ALL" && strlen($country_code) != 2)
2174
		return "";
2175

    
2176
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2177
	$country_names_contents = file_get_contents($country_names_xml);
2178
	$country_names = xml2array($country_names_contents);
2179

    
2180
	if($country_code == "ALL") {
2181
		$country_list = array();
2182
		foreach($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2183
			$country_list[] = array( "code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2184
						 "name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])) );
2185
		}
2186
		return $country_list;
2187
	}
2188

    
2189
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2190
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2191
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2192
		}
2193
	}
2194
	return "";
2195
}
2196

    
2197
/* sort by interface only, retain the original order of rules that apply to
2198
   the same interface */
2199
function filter_rules_sort() {
2200
	global $config;
2201

    
2202
	/* mark each rule with the sequence number (to retain the order while sorting) */
2203
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2204
		$config['filter']['rule'][$i]['seq'] = $i;
2205

    
2206
	usort($config['filter']['rule'], "filter_rules_compare");
2207

    
2208
	/* strip the sequence numbers again */
2209
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2210
		unset($config['filter']['rule'][$i]['seq']);
2211
}
2212
function filter_rules_compare($a, $b) {
2213
	if (isset($a['floating']) && isset($b['floating']))
2214
		return $a['seq'] - $b['seq'];
2215
	else if (isset($a['floating']))
2216
		return -1;
2217
	else if (isset($b['floating']))
2218
		return 1;
2219
	else if ($a['interface'] == $b['interface'])
2220
		return $a['seq'] - $b['seq'];
2221
	else
2222
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2223
}
2224

    
2225
function generate_ipv6_from_mac($mac) {
2226
	$elements = explode(":", $mac);
2227
	if(count($elements) <> 6)
2228
		return false;
2229

    
2230
	$i = 0;
2231
	$ipv6 = "fe80::";
2232
	foreach($elements as $byte) {
2233
		if($i == 0) {
2234
			$hexadecimal =  substr($byte, 1, 2);
2235
			$bitmap = base_convert($hexadecimal, 16, 2);
2236
			$bitmap = str_pad($bitmap, 4, "0", STR_PAD_LEFT);
2237
			$bitmap = substr($bitmap, 0, 2) ."1". substr($bitmap, 3,4);
2238
			$byte = substr($byte, 0, 1) . base_convert($bitmap, 2, 16);
2239
		}
2240
		$ipv6 .= $byte;
2241
		if($i == 1) {
2242
			$ipv6 .= ":";
2243
		}
2244
		if($i == 3) {
2245
			$ipv6 .= ":";
2246
		}
2247
		if($i == 2) {
2248
			$ipv6 .= "ff:fe";
2249
		}
2250
		
2251
		$i++;
2252
	}	
2253
	return $ipv6;
2254
}
2255

    
2256
/****f* pfsense-utils/load_mac_manufacturer_table
2257
 * NAME
2258
 *   load_mac_manufacturer_table
2259
 * INPUTS
2260
 *   none
2261
 * RESULT
2262
 *   returns associative array with MAC-Manufacturer pairs
2263
 ******/
2264
function load_mac_manufacturer_table() {
2265
	/* load MAC-Manufacture data from the file */
2266
	$macs = false;
2267
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes"))
2268
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2269
	if ($macs){
2270
		foreach ($macs as $line){
2271
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)){
2272
				/* store values like this $mac_man['000C29']='VMware' */
2273
				$mac_man["$matches[1]"]=$matches[2];
2274
			}
2275
		}
2276
 		return $mac_man;
2277
	} else
2278
		return -1;
2279

    
2280
}
2281

    
2282
/****f* pfsense-utils/is_ipaddr_configured
2283
 * NAME
2284
 *   is_ipaddr_configured
2285
 * INPUTS
2286
 *   IP Address to check.
2287
 * RESULT
2288
 *   returns true if the IP Address is
2289
 *   configured and present on this device.
2290
*/
2291
function is_ipaddr_configured($ipaddr) {
2292
	$interface_list_ips = get_configured_ip_addresses();
2293
	foreach($interface_list_ips as $ilips) {
2294
		if(strcasecmp($ipaddr, $ilips) == 0) 
2295
				return true;
2296
	}	
2297
}
2298

    
2299
/****f* pfsense-utils/pfSense_handle_custom_code
2300
 * NAME
2301
 *   pfSense_handle_custom_code
2302
 * INPUTS
2303
 *   directory name to process
2304
 * RESULT
2305
 *   globs the directory and includes the files
2306
 */
2307
function pfSense_handle_custom_code($src_dir) {
2308
	// Allow extending of the nat edit page and include custom input validation 
2309
	if(is_dir("$src_dir")) {
2310
		$cf = glob($src_dir . "/*.inc");
2311
		foreach($cf as $nf) {
2312
			if($nf == "." || $nf == "..") 
2313
				continue;
2314
			// Include the extra handler
2315
			include("$nf");
2316
		}
2317
	}
2318
}
2319

    
2320
function set_language($lang = 'en_US', $encoding = "ISO8859-1") {
2321
	putenv("LANG={$lang}.{$encoding}");
2322
	setlocale(LC_ALL, "{$lang}.{$encoding}");
2323
	textdomain("pfSense");
2324
	bindtextdomain("pfSense","/usr/share/locale");
2325
	bind_textdomain_codeset("pfSense","{$lang}.{$encoding}");
2326
}
2327

    
2328
function get_locale_list() {
2329
	$locales = array(
2330
		"en_US" => gettext("English"),
2331
	);
2332
	asort($locales);
2333
	return $locales;
2334
}
2335

    
2336
function return_hex_ipv4($ipv4) {
2337
	if(!is_ipaddrv4($ipv4))
2338
		return(false);
2339
	
2340
	/* we need the hex form of the interface IPv4 address */
2341
	$ip4arr = explode(".", $ipv4);
2342
	$hexwanv4 = "";
2343
	foreach($ip4arr as $octet)
2344
		$hexwanv4 .= sprintf("%02x", $octet);
2345

    
2346
	return($hexwanv4);
2347
}
2348

    
2349
function convert_ipv6_to_128bit($ipv6) {
2350
	if(!is_ipaddrv6($ipv6))
2351
		return(false);
2352

    
2353
	$ip6arr = array();
2354
	$ip6prefix = Net_IPv6::uncompress($ipv6);
2355
	$ip6arr = explode(":", $ip6prefix);
2356
	/* binary presentation of the prefix for all 128 bits. */
2357
	$ip6prefixbin = "";
2358
	foreach($ip6arr as $element) {
2359
		$ip6prefixbin .= sprintf("%016b", hexdec($element));
2360
	}
2361
	return($ip6prefixbin);
2362
}
2363

    
2364
function convert_128bit_to_ipv6($ip6bin) {
2365
	if(strlen($ip6bin) <> 128)
2366
		return(false);
2367

    
2368
	$ip6arr = array();
2369
	$ip6binarr = array();
2370
	$ip6binarr = str_split($ip6bin, 16);
2371
	foreach($ip6binarr as $binpart)
2372
		$ip6arr[] = dechex(bindec($binpart));
2373
	$ip6addr = Net_IPv6::compress(implode(":", $ip6arr));
2374

    
2375
	return($ip6addr);
2376
}
2377

    
2378

    
2379
/* Returns the calculated bit length of the prefix delegation from the WAN interface */
2380
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
2381
/* 6rd is variable, calculate from 64 - (v6 prefixlen - (32 - v4 prefixlen)) */
2382
/* 6to4 is 16 bits, e.g. 65535 */
2383
function calculate_ipv6_delegation_length($if) {
2384
	global $config;
2385

    
2386
	if(!is_array($config['interfaces'][$if]))
2387
		return false;
2388

    
2389
	switch($config['interfaces'][$if]['ipaddrv6']) {
2390
		case "6to4":
2391
			$pdlen = 16;
2392
			break;
2393
		case "6rd":
2394
			$rd6cfg = $config['interfaces'][$if];
2395
			$rd6plen = explode("/", $rd6cfg['prefix-6rd']);
2396
			$pdlen = (64 - ($rd6plen[1] + (32 - $rd6cfg['prefix-6rd-v4plen'])));
2397
			break;
2398
		case "dhcp6":
2399
			$dhcp6cfg = $config['interfaces'][$if];
2400
			$pdlen = $dhcp6cfg['dhcp6-ia-pd-len'];
2401
			break;
2402
		default:
2403
			$pdlen = 0;
2404
			break;
2405
	}
2406
	return($pdlen);
2407
}
2408
?>
(37-37/65)