Project

General

Profile

Download (65.5 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
        );
121
        foreach($ip_private_list as $private) {
122
                if(ip_in_subnet($iptocheck,$private)==true)
123
                        $isprivate = true;
124
        }
125
        return $isprivate;
126
}
127

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

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

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

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

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

    
187
	return;
188
}
189

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

    
205
	return false;
206
}
207

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
474
	return false;
475
}
476

    
477
/*
478
 * gather_altq_queue_stats():  gather altq queue stats and return an array that
479
 *                             is queuename|qlength|measured_packets
480
 *                             NOTE: this command takes 5 seconds to run
481
 */
482
function gather_altq_queue_stats($dont_return_root_queues) {
483
	exec("/sbin/pfctl -vvsq", $stats_array);
484
	$queue_stats = array();
485
	foreach ($stats_array as $stats_line) {
486
		$match_array = "";
487
		if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
488
			$queue_name = $match_array[1][0];
489
		if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
490
			$speed = $match_array[1][0];
491
		if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
492
			$borrows = $match_array[1][0];
493
		if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
494
			$suspends = $match_array[1][0];
495
		if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
496
			$drops = $match_array[1][0];
497
		if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
498
			$measured = $match_array[1][0];
499
			if($dont_return_root_queues == true)
500
				if(stristr($queue_name,"root_") == false)
501
					array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
502
		}
503
	}
504
	return $queue_stats;
505
}
506

    
507
/*
508
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
509
 *					 Useful for finding paths and stripping file extensions.
510
 */
511
function reverse_strrchr($haystack, $needle) {
512
	if (!is_string($haystack))
513
		return;
514
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
515
}
516

    
517
/*
518
 *  backup_config_section($section): returns as an xml file string of
519
 *                                   the configuration section
520
 */
521
function backup_config_section($section) {
522
	global $config;
523
	$new_section = &$config[$section];
524
	/* generate configuration XML */
525
	$xmlconfig = dump_xml_config($new_section, $section);
526
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
527
	return $xmlconfig;
528
}
529

    
530
/*
531
 *  restore_config_section($section, new_contents): restore a configuration section,
532
 *                                                  and write the configuration out
533
 *                                                  to disk/cf.
534
 */
535
function restore_config_section($section, $new_contents) {
536
	global $config, $g;
537
	conf_mount_rw();
538
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
539
	fwrite($fout, $new_contents);
540
	fclose($fout);
541
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
542
	if ($section_xml != -1)
543
		$config[$section] = &$section_xml;
544
	@unlink($g['tmp_path'] . "/tmpxml");
545
	if(file_exists("{$g['tmp_path']}/config.cache"))
546
		unlink("{$g['tmp_path']}/config.cache");
547
	write_config("Restored {$section} of config file (maybe from CARP partner)");
548
	disable_security_checks();
549
	conf_mount_ro();
550
	return;
551
}
552

    
553
/*
554
 *  merge_config_section($section, new_contents):   restore a configuration section,
555
 *                                                  and write the configuration out
556
 *                                                  to disk/cf.  But preserve the prior
557
 * 													structure if needed
558
 */
559
function merge_config_section($section, $new_contents) {
560
	global $config;
561
	conf_mount_rw();
562
	$fname = get_tmp_filename();
563
	$fout = fopen($fname, "w");
564
	fwrite($fout, $new_contents);
565
	fclose($fout);
566
	$section_xml = parse_xml_config($fname, $section);
567
	$config[$section] = $section_xml;
568
	unlink($fname);
569
	write_config("Restored {$section} of config file (maybe from CARP partner)");
570
	disable_security_checks();
571
	conf_mount_ro();
572
	return;
573
}
574

    
575
/*
576
 * http_post($server, $port, $url, $vars): does an http post to a web server
577
 *                                         posting the vars array.
578
 * written by nf@bigpond.net.au
579
 */
580
function http_post($server, $port, $url, $vars) {
581
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
582
	$urlencoded = "";
583
	while (list($key,$value) = each($vars))
584
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
585
	$urlencoded = substr($urlencoded,0,-1);
586
	$content_length = strlen($urlencoded);
587
	$headers = "POST $url HTTP/1.1
588
Accept: */*
589
Accept-Language: en-au
590
Content-Type: application/x-www-form-urlencoded
591
User-Agent: $user_agent
592
Host: $server
593
Connection: Keep-Alive
594
Cache-Control: no-cache
595
Content-Length: $content_length
596

    
597
";
598

    
599
	$errno = "";
600
	$errstr = "";
601
	$fp = fsockopen($server, $port, $errno, $errstr);
602
	if (!$fp) {
603
		return false;
604
	}
605

    
606
	fputs($fp, $headers);
607
	fputs($fp, $urlencoded);
608

    
609
	$ret = "";
610
	while (!feof($fp))
611
		$ret.= fgets($fp, 1024);
612
	fclose($fp);
613

    
614
	return $ret;
615
}
616

    
617
/*
618
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
619
 */
620
if (!function_exists('php_check_syntax')){
621
	global $g;
622
	function php_check_syntax($code_to_check, &$errormessage){
623
		return false;
624
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
625
		$code = $_POST['content'];
626
		$code = str_replace("<?php", "", $code);
627
		$code = str_replace("?>", "", $code);
628
		fwrite($fout, "<?php\n\n");
629
		fwrite($fout, $code_to_check);
630
		fwrite($fout, "\n\n?>\n");
631
		fclose($fout);
632
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
633
		$output = exec_command($command);
634
		if (stristr($output, "Errors parsing") == false) {
635
			echo "false\n";
636
			$errormessage = '';
637
			return(false);
638
		} else {
639
			$errormessage = $output;
640
			return(true);
641
		}
642
	}
643
}
644

    
645
/*
646
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
647
 */
648
if (!function_exists('php_check_syntax')){
649
	function php_check_syntax($code_to_check, &$errormessage){
650
		return false;
651
		$command = "/usr/local/bin/php -l " . $code_to_check;
652
		$output = exec_command($command);
653
		if (stristr($output, "Errors parsing") == false) {
654
			echo "false\n";
655
			$errormessage = '';
656
			return(false);
657
		} else {
658
			$errormessage = $output;
659
			return(true);
660
		}
661
	}
662
}
663

    
664
/*
665
 * rmdir_recursive($path,$follow_links=false)
666
 * Recursively remove a directory tree (rm -rf path)
667
 * This is for directories _only_
668
 */
669
function rmdir_recursive($path,$follow_links=false) {
670
	$to_do = glob($path);
671
	if(!is_array($to_do)) $to_do = array($to_do);
672
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
673
		if(file_exists($workingdir)) {
674
			if(is_dir($workingdir)) {
675
				$dir = opendir($workingdir);
676
				while ($entry = readdir($dir)) {
677
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
678
						unlink("$workingdir/$entry");
679
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
680
						rmdir_recursive("$workingdir/$entry");
681
				}
682
				closedir($dir);
683
				rmdir($workingdir);
684
			} elseif (is_file($workingdir)) {
685
				unlink($workingdir);
686
			}
687
               	}
688
	}
689
	return;
690
}
691

    
692
/*
693
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
694
 */
695
function call_pfsense_method($method, $params, $timeout = 0) {
696
	global $g, $config;
697

    
698
	$ip = gethostbyname($g['product_website']);
699
	if($ip == $g['product_website'])
700
		return false;
701

    
702
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
703
	$xmlrpc_path = $g['xmlrpcpath'];
704
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
705
	$port = 0;
706
	$proxyurl = "";
707
	$proxyport = 0;
708
	$proxyuser = "";
709
	$proxypass = "";
710
	if (!empty($config['system']['proxyurl']))
711
		$proxyurl = $config['system']['proxyurl'];
712
	if (!empty($config['system']['proxyport']) && is_numeric($config['system']['proxyport']))
713
		$proxyport = $config['system']['proxyport'];
714
	if (!empty($config['system']['proxyuser']))
715
		$proxyuser = $config['system']['proxyuser'];
716
	if (!empty($config['system']['proxypass']))
717
		$proxypass = $config['system']['proxypass'];
718
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url, $port, $proxyurl, $proxyport, $proxyuser, $proxypass);
719
	// If the ALT PKG Repo has a username/password set, use it.
720
	if($config['system']['altpkgrepo']['username'] && 
721
	   $config['system']['altpkgrepo']['password']) {
722
		$username = $config['system']['altpkgrepo']['username'];
723
		$password = $config['system']['altpkgrepo']['password'];
724
		$cli->setCredentials($username, $password);
725
	}
726
	elseif($g['xmlrpcauthuser'] && $g['xmlrpcauthpass']) {
727
		$username = $g['xmlrpcauthuser'];
728
		$password = $g['xmlrpcauthpass'];
729
		$cli->setCredentials($username, $password);
730
	}
731
	$resp = $cli->send($msg, $timeout);
732
	if(!is_object($resp)) {
733
		log_error("XMLRPC communication error: " . $cli->errstr);
734
		return false;
735
	} elseif($resp->faultCode()) {
736
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
737
		return false;
738
	} else {
739
		return XML_RPC_Decode($resp->value());
740
	}
741
}
742

    
743
/*
744
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
745
 */
746
function check_firmware_version($tocheck = "all", $return_php = true) {
747
	global $g, $config;
748

    
749
	$ip = gethostbyname($g['product_website']);
750
	if($ip == $g['product_website'])
751
		return false;
752

    
753
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
754
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
755
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
756
		"platform" => trim(file_get_contents('/etc/platform')),
757
		"config_version" => $config['version']
758
		);
759
	if($tocheck == "all") {
760
		$params = $rawparams;
761
	} else {
762
		foreach($tocheck as $check) {
763
			$params['check'] = $rawparams['check'];
764
			$params['platform'] = $rawparams['platform'];
765
		}
766
	}
767
	if($config['system']['firmware']['branch'])
768
		$params['branch'] = $config['system']['firmware']['branch'];
769

    
770
	/* XXX: What is this method? */
771
	if(!($versions = call_pfsense_method('pfsense.get_firmware_version', $params))) {
772
		return false;
773
	} else {
774
		$versions["current"] = $params;
775
	}
776

    
777
	return $versions;
778
}
779

    
780
/*
781
 * host_firmware_version(): Return the versions used in this install
782
 */
783
function host_firmware_version($tocheck = "") {
784
        global $g, $config;
785

    
786
        return array(
787
		"firmware" => array("version" => trim(file_get_contents('/etc/version', " \n"))),
788
                "kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel', " \n"))),
789
                "base"     => array("version" => trim(file_get_contents('/etc/version_base', " \n"))),
790
                "platform" => trim(file_get_contents('/etc/platform', " \n")),
791
                "config_version" => $config['version']
792
                );
793
}
794

    
795
function get_disk_info() {
796
	$diskout = "";
797
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
798
	return explode(' ', $diskout[0]);
799
}
800

    
801
/****f* pfsense-utils/strncpy
802
 * NAME
803
 *   strncpy - copy strings
804
 * INPUTS
805
 *   &$dst, $src, $length
806
 * RESULT
807
 *   none
808
 ******/
809
function strncpy(&$dst, $src, $length) {
810
	if (strlen($src) > $length) {
811
		$dst = substr($src, 0, $length);
812
	} else {
813
		$dst = $src;
814
	}
815
}
816

    
817
/****f* pfsense-utils/reload_interfaces_sync
818
 * NAME
819
 *   reload_interfaces - reload all interfaces
820
 * INPUTS
821
 *   none
822
 * RESULT
823
 *   none
824
 ******/
825
function reload_interfaces_sync() {
826
	global $config, $g;
827

    
828
	if($g['debug'])
829
		log_error("reload_interfaces_sync() is starting.");
830

    
831
	/* parse config.xml again */
832
	$config = parse_config(true);
833

    
834
	/* enable routing */
835
	system_routing_enable();
836
	if($g['debug'])
837
		log_error("Enabling system routing");
838

    
839
	if($g['debug'])
840
		log_error("Cleaning up Interfaces");
841

    
842
	/* set up interfaces */
843
	interfaces_configure();
844
}
845

    
846
/****f* pfsense-utils/reload_all
847
 * NAME
848
 *   reload_all - triggers a reload of all settings
849
 *   * INPUTS
850
 *   none
851
 * RESULT
852
 *   none
853
 ******/
854
function reload_all() {
855
	send_event("service reload all");
856
}
857

    
858
/****f* pfsense-utils/reload_interfaces
859
 * NAME
860
 *   reload_interfaces - triggers a reload of all interfaces
861
 * INPUTS
862
 *   none
863
 * RESULT
864
 *   none
865
 ******/
866
function reload_interfaces() {
867
	send_event("interface all reload");
868
}
869

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

    
881
	$g['booting'] = false;
882

    
883
	/* parse config.xml again */
884
	$config = parse_config(true);
885

    
886
	/* set up our timezone */
887
	system_timezone_configure();
888

    
889
	/* set up our hostname */
890
	system_hostname_configure();
891

    
892
	/* make hosts file */
893
	system_hosts_generate();
894

    
895
	/* generate resolv.conf */
896
	system_resolvconf_generate();
897

    
898
	/* enable routing */
899
	system_routing_enable();
900

    
901
	/* set up interfaces */
902
	interfaces_configure();
903

    
904
	/* start dyndns service */
905
	services_dyndns_configure();
906

    
907
	/* configure cron service */
908
	configure_cron();
909

    
910
	/* start the NTP client */
911
	system_ntp_configure();
912

    
913
	/* sync pw database */
914
	conf_mount_rw();
915
	unlink_if_exists("/etc/spwd.db.tmp");
916
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
917
	conf_mount_ro();
918

    
919
	/* restart sshd */
920
	send_event("service restart sshd");
921

    
922
	/* restart webConfigurator if needed */
923
	send_event("service restart webgui");
924
}
925

    
926
function auto_login() {
927
	global $config;
928

    
929
	if(isset($config['system']['disableconsolemenu']))
930
		$status = false;
931
	else
932
		$status = true;
933

    
934
	$gettytab = file_get_contents("/etc/gettytab");
935
	$getty_split = split("\n", $gettytab);
936
	conf_mount_rw();
937
	$fd = false;
938
	$tries = 0;
939
	while (!$fd && $tries < 100) {
940
		$fd = fopen("/etc/gettytab", "w");
941
		$tries++;
942
		
943
	}
944
	if (!$fd) {
945
		conf_mount_ro();
946
		log_error("Enabling auto login was not possible.");
947
		return;
948
	}
949
	foreach($getty_split as $gs) {
950
		if(stristr($gs, ":ht:np:sp#115200") ) {
951
			if($status == true) {
952
				fwrite($fd, "	:ht:np:sp#115200:al=root:\n");
953
			} else {
954
				fwrite($fd, "	:ht:np:sp#115200:\n");
955
			}
956
		} else {
957
			fwrite($fd, "{$gs}\n");
958
		}
959
	}
960
	fclose($fd);
961
	conf_mount_ro();
962
}
963

    
964
function setup_serial_port() {
965
	global $g, $config;
966
	conf_mount_rw();
967
	/* serial console - write out /boot.config */
968
	if(file_exists("/boot.config"))
969
		$boot_config = file_get_contents("/boot.config");
970
	else
971
		$boot_config = "";
972

    
973
	if($g['platform'] <> "cdrom") {
974
		$boot_config_split = split("\n", $boot_config);
975
		$fd = fopen("/boot.config","w");
976
		if($fd) {
977
			foreach($boot_config_split as $bcs) {
978
				if(stristr($bcs, "-D")) {
979
					/* DONT WRITE OUT, WE'LL DO IT LATER */
980
				} else {
981
					if($bcs <> "")
982
						fwrite($fd, "{$bcs}\n");
983
				}
984
			}
985
			if(isset($config['system']['enableserial'])) {
986
				fwrite($fd, "-D");
987
			}
988
			fclose($fd);
989
		}
990
		/* serial console - write out /boot/loader.conf */
991
		$boot_config = file_get_contents("/boot/loader.conf");
992
		$boot_config_split = explode("\n", $boot_config);
993
		if(count($boot_config_split) > 0) {
994
			$new_boot_config = array();
995
			// Loop through and only add lines that are not empty, and which
996
			//  do not contain a console directive.
997
			foreach($boot_config_split as $bcs)
998
				if(!empty($bcs) && (stripos($bcs, "console") === false))
999
					$new_boot_config[] = $bcs;
1000

    
1001
			if(isset($config['system']['enableserial']))
1002
				$new_boot_config[] = 'console="comconsole"';
1003
			file_put_contents("/boot/loader.conf", implode("\n", $new_boot_config));
1004
		}
1005
	}
1006
	$ttys = file_get_contents("/etc/ttys");
1007
	$ttys_split = split("\n", $ttys);
1008
	$fd = fopen("/etc/ttys", "w");
1009
	foreach($ttys_split as $tty) {
1010
		if(stristr($tty, "ttyd0") or stristr($tty, "ttyu0")) {
1011
			if(isset($config['system']['enableserial'])) {
1012
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	on	secure\n");
1013
			} else {
1014
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	off	secure\n");
1015
			}
1016
		} else {
1017
			fwrite($fd, $tty . "\n");
1018
		}
1019
	}
1020
	fclose($fd);
1021
	auto_login();
1022

    
1023
	conf_mount_ro();
1024
	return;
1025
}
1026

    
1027
function print_value_list($list, $count = 10, $separator = ",") {
1028
	$list = implode($separator, array_slice($list, 0, $count));
1029
	if(count($list) < $count) {
1030
		$list .= ".";
1031
	} else {
1032
		$list .= "...";
1033
	}
1034
	return $list;
1035
}
1036

    
1037
/* DHCP enabled on any interfaces? */
1038
function is_dhcp_server_enabled() 
1039
{
1040
	global $config;
1041

    
1042
	$dhcpdenable = false;
1043
	
1044
	if (!is_array($config['dhcpd']))
1045
		return false;
1046

    
1047
	$Iflist = get_configured_interface_list();
1048

    
1049
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1050
		if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
1051
			$dhcpdenable = true;
1052
			break;
1053
		}
1054
	}
1055

    
1056
	return $dhcpdenable;
1057
}
1058

    
1059
/* Any PPPoE servers enabled? */
1060
function is_pppoe_server_enabled() {
1061
	global $config;
1062

    
1063
	$pppoeenable = false;
1064

    
1065
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe']))
1066
		return false;
1067

    
1068
	foreach ($config['pppoes']['pppoe'] as $pppoes)
1069
		if ($pppoes['mode'] == 'server')
1070
			$pppoeenable = true;
1071

    
1072
	return $pppoeenable;
1073
}
1074

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
1286
		}
1287
		/* lookup the gateway */
1288
		if (interface_has_gateway($ifdescr)) 
1289
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1290
	}
1291

    
1292
	$bridge = "";
1293
	$bridge = link_interface_to_bridge($ifdescr);
1294
	if($bridge) {
1295
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1296
		if(stristr($bridge_text, "blocking") <> false) {
1297
			$ifinfo['bridge'] = "<b><font color='red'>blocking</font></b> - check for ethernet loops";
1298
			$ifinfo['bridgeint'] = $bridge;
1299
		} else if(stristr($bridge_text, "learning") <> false) {
1300
			$ifinfo['bridge'] = "learning";
1301
			$ifinfo['bridgeint'] = $bridge;
1302
		} else if(stristr($bridge_text, "forwarding") <> false) {
1303
			$ifinfo['bridge'] = "forwarding";
1304
			$ifinfo['bridgeint'] = $bridge;
1305
		}
1306
	}
1307

    
1308
	return $ifinfo;
1309
}
1310

    
1311
//returns cpu speed of processor. Good for determining capabilities of machine
1312
function get_cpu_speed() {
1313
	 return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1314
}
1315

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

    
1345
function is_fqdn($fqdn) {
1346
	$hostname = false;
1347
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1348
		$hostname = true;
1349
	}
1350
	if(preg_match("/\.\./", $fqdn)) {
1351
		$hostname = false;
1352
	}
1353
	if(preg_match("/^\./i", $fqdn)) { 
1354
		$hostname = false;
1355
	}
1356
	if(preg_match("/\//i", $fqdn)) {
1357
		$hostname = false;
1358
	}
1359
	return($hostname);
1360
}
1361

    
1362
function pfsense_default_state_size() {
1363
  /* get system memory amount */
1364
  $memory = get_memory();
1365
  $avail = $memory[0];
1366
  /* Be cautious and only allocate 10% of system memory to the state table */
1367
  $max_states = (int) ($avail/10)*1000;
1368
  return $max_states;
1369
}
1370

    
1371
function pfsense_default_tables_size() {
1372
	$current = `pfctl -sm | grep ^tables | awk '{print $4};'`;
1373
	return $current;
1374
}
1375

    
1376
function pfsense_default_table_entries_size() {
1377
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1378
	return $current;
1379
}
1380

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

    
1417
	if(trim($oldcontents) != trim($contents)) {
1418
		if($g['debug']) {
1419
			log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
1420
		}
1421
		return ($oldcontents);
1422
	} else {
1423
		return false;
1424
	}
1425
}
1426

    
1427
/*
1428
 * load_glxsb() - Load the glxsb crypto module if enabled in config.
1429
 */
1430
function load_glxsb() {
1431
	global $config, $g;
1432
	$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
1433
	if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
1434
		mwexec("/sbin/kldload glxsb");
1435
	}
1436
}
1437

    
1438
/****f* pfsense-utils/isvm
1439
 * NAME
1440
 *   isvm
1441
 * INPUTS
1442
 *	 none
1443
 * RESULT
1444
 *   returns true if machine is running under a virtual environment
1445
 ******/
1446
function isvm() {
1447
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1448
	$bios_vendor = strtolower(`/bin/kenv | /usr/bin/awk -F= '/smbios.bios.vendor/ {print $2}'`);
1449
	if(in_array($bios_vendor, $virtualenvs)) 
1450
		return true;
1451
	else
1452
		return false;
1453
}
1454

    
1455
function get_freebsd_version() {
1456
	$version = php_uname("r");
1457
	return $version[0];
1458
}
1459

    
1460
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body', $connect_timeout=60, $timeout=0) {
1461
        global $ch, $fout, $file_size, $downloaded, $config;
1462
        $file_size  = 1;
1463
        $downloaded = 1;
1464
        /* open destination file */
1465
        $fout = fopen($destination_file, "wb");
1466

    
1467
        /*
1468
         *      Originally by Author: Keyvan Minoukadeh
1469
         *      Modified by Scott Ullrich to return Content-Length size
1470
         */
1471

    
1472
        $ch = curl_init();
1473
        curl_setopt($ch, CURLOPT_URL, $url_file);
1474
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1475
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1476
        /* Don't verify SSL peers since we don't have the certificates to do so. */
1477
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1478
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1479
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1480
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1481
        curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1482

    
1483
	if (!empty($config['system']['proxyurl'])) {
1484
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1485
		if (!empty($config['system']['proxyport']))
1486
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1487
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1488
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1489
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1490
		}
1491
	}
1492

    
1493
        @curl_exec($ch);
1494
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1495
        if($fout)
1496
                fclose($fout);
1497
        curl_close($ch);
1498
        return ($http_code == 200) ? true : $http_code;
1499
}
1500

    
1501
function read_header($ch, $string) {
1502
        global $file_size, $fout;
1503
        $length = strlen($string);
1504
        $regs = "";
1505
        ereg("(Content-Length:) (.*)", $string, $regs);
1506
        if($regs[2] <> "") {
1507
                $file_size = intval($regs[2]);
1508
        }
1509
        ob_flush();
1510
        return $length;
1511
}
1512

    
1513
function read_body($ch, $string) {
1514
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
1515
		global $pkg_interface;
1516
        $length = strlen($string);
1517
        $downloaded += intval($length);
1518
        if($file_size > 0) {
1519
                $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1520
                $downloadProgress = 100 - $downloadProgress;
1521
        } else
1522
                $downloadProgress = 0;
1523
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1524
                if($sendto == "status") {
1525
					if($pkg_interface == "console") {
1526
						if(substr($downloadProgress,2,1) == "0" || count($downloadProgress) < 2) {
1527
                        	$tostatus = $static_status . $downloadProgress . "%";
1528
                        	update_status($tostatus);
1529
						}
1530
					} else {
1531
                        $tostatus = $static_status . $downloadProgress . "%";
1532
                        update_status($tostatus);						
1533
					}
1534
                } else {
1535
					if($pkg_interface == "console") {
1536
						if(substr($downloadProgress,2,1) == "0" || count($downloadProgress) < 2) {
1537
                        	$tooutput = $static_output . $downloadProgress . "%";
1538
                        	update_output_window($tooutput);
1539
						}
1540
					} else {
1541
                        $tooutput = $static_output . $downloadProgress . "%";
1542
                        update_output_window($tooutput);
1543
					}
1544
                }
1545
                update_progress_bar($downloadProgress);
1546
                $lastseen = $downloadProgress;
1547
        }
1548
        if($fout)
1549
                fwrite($fout, $string);
1550
        ob_flush();
1551
        return $length;
1552
}
1553

    
1554
/*
1555
 *   update_output_window: update bottom textarea dynamically.
1556
 */
1557
function update_output_window($text) {
1558
        global $pkg_interface;
1559
        $log = ereg_replace("\n", "\\n", $text);
1560
        if($pkg_interface != "console") {
1561
                echo "\n<script language=\"JavaScript\">\nthis.document.forms[0].output.value = \"" . $log . "\";\n";
1562
				echo "this.document.forms[0].output.scrollTop = this.document.forms[0].output.scrollHeight;\n";	
1563
				echo "</script>";
1564
        } else
1565
		echo $text;
1566
        /* ensure that contents are written out */
1567
        ob_flush();
1568
}
1569

    
1570
/*
1571
 *   update_output_window: update top textarea dynamically.
1572
 */
1573
function update_status($status) {
1574
        global $pkg_interface;
1575

    
1576
        if($pkg_interface != "console") {
1577
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
1578
        } else {
1579
                echo $status . "\n";
1580
        }
1581
        /* ensure that contents are written out */
1582
        ob_flush();
1583
}
1584

    
1585
/*
1586
 * update_progress_bar($percent): updates the javascript driven progress bar.
1587
 */
1588
function update_progress_bar($percent) {
1589
        global $pkg_interface;
1590
        if($percent > 100) $percent = 1;
1591
        if($pkg_interface <> "console") {
1592
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
1593
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1594
                echo "\n</script>";
1595
        } else {
1596
                echo " {$percent}%";
1597
        }
1598
}
1599

    
1600
/* 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. */
1601
if(!function_exists("split")) {
1602
	function split($seperator, $haystack, $limit = null) {
1603
		return preg_split($seperator, $haystack, $limit);
1604
	}
1605
}
1606

    
1607
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
1608
	global $g, $config, $pconfig, $debug;
1609
	if(!$origname) 
1610
		return;
1611

    
1612
	$sectionref = &$config;
1613
	foreach($section as $sectionname) {
1614
		if(is_array($sectionref) && isset($sectionref[$sectionname]))
1615
			$sectionref = &$sectionref[$sectionname];
1616
		else
1617
			return;
1618
	}
1619

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

    
1623
	if(is_array($sectionref)) {
1624
		foreach($sectionref as $itemkey => $item) {
1625
			if($debug) fwrite($fd, "$itemkey\n");
1626

    
1627
			$fieldfound = true;
1628
			$fieldref = &$sectionref[$itemkey];
1629
			foreach($field as $fieldname) {
1630
				if(is_array($fieldref) && isset($fieldref[$fieldname]))
1631
					$fieldref = &$fieldref[$fieldname];
1632
				else {
1633
					$fieldfound = false;
1634
					break;
1635
				}
1636
			}
1637
			if($fieldfound && $fieldref == $origname) {
1638
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1639
				$fieldref = $new_alias_name;
1640
			}
1641
		}
1642
	}
1643

    
1644
	if($debug) fclose($fd);
1645

    
1646
}
1647

    
1648
function update_alias_url_data() {
1649
	global $config, $g;
1650

    
1651
	/* item is a url type */
1652
	$lockkey = lock('config');
1653
	if (is_array($config['aliases']['alias'])) {
1654
		foreach ($config['aliases']['alias'] as $x => $alias) {
1655
			if (empty($alias['aliasurl']))
1656
				continue;
1657

    
1658
			/* fetch down and add in */
1659
			$isfirst = 0;
1660
			$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
1661
			unlink($temp_filename);
1662
			$fda = fopen("{$g['tmp_path']}/tmpfetch","w");
1663
			fwrite($fda, "/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1664
			fclose($fda);
1665
			mwexec("/bin/mkdir -p {$temp_filename}");
1666
			mwexec("/usr/bin/fetch -q -o \"{$temp_filename}/aliases\" \"" . $config['aliases']['alias'][$x]['aliasurl'] . "\"");
1667
			/* if the item is tar gzipped then extract */
1668
			if(stristr($alias['aliasurl'], ".tgz"))
1669
				process_alias_tgz($temp_filename);
1670
			else if(stristr($alias['aliasurl'], ".zip"))
1671
				process_alias_unzip($temp_filename);
1672
			if(file_exists("{$temp_filename}/aliases")) {
1673
				$file_contents = file_get_contents("{$temp_filename}/aliases");
1674
				$file_contents = str_replace("#", "\n#", $file_contents);
1675
				$file_contents_split = split("\n", $file_contents);
1676
				foreach($file_contents_split as $fc) {
1677
					$tmp = trim($fc);
1678
					if(stristr($fc, "#")) {
1679
						$tmp_split = split("#", $tmp);
1680
						$tmp = trim($tmp_split[0]);
1681
					}
1682
					if(trim($tmp) <> "") {
1683
						if($isfirst == 1)
1684
							$address .= " ";
1685
						$address .= $tmp;
1686
						$isfirst = 1;
1687
					}
1688
				}
1689
				if($isfirst > 0) {
1690
					$config['aliases']['alias'][$x]['address'] = $address;
1691
					$updated = true;
1692
				}
1693
				mwexec("/bin/rm -rf {$temp_filename}");
1694
			}
1695
		}
1696
	}
1697
	if($updated)
1698
		write_config();
1699
	unlock($lockkey);
1700
}
1701

    
1702
function process_alias_unzip($temp_filename) {
1703
	if(!file_exists("/usr/local/bin/unzip"))
1704
		return;
1705
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.zip");
1706
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
1707
	unlink("{$temp_filename}/aliases.zip");
1708
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1709
	/* foreach through all extracted files and build up aliases file */
1710
	$fd = fopen("{$temp_filename}/aliases", "w");
1711
	foreach($files_to_process as $f2p) {
1712
		$file_contents = file_get_contents($f2p);
1713
		fwrite($fd, $file_contents);
1714
		unlink($f2p);
1715
	}
1716
	fclose($fd);
1717
}
1718

    
1719
function process_alias_tgz($temp_filename) {
1720
	if(!file_exists("/usr/bin/tar"))
1721
		return;
1722
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
1723
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
1724
	unlink("{$temp_filename}/aliases.tgz");
1725
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1726
	/* foreach through all extracted files and build up aliases file */
1727
	$fd = fopen("{$temp_filename}/aliases", "w");
1728
	foreach($files_to_process as $f2p) {
1729
		$file_contents = file_get_contents($f2p);
1730
		fwrite($fd, $file_contents);
1731
		unlink($f2p);
1732
	}
1733
	fclose($fd);
1734
}
1735

    
1736
function version_compare_dates($a, $b) {
1737
	$a_time = strtotime($a);
1738
	$b_time = strtotime($b);
1739

    
1740
	if ((!$a_time) || (!$b_time)) {
1741
		return FALSE;
1742
	} else {
1743
		if ($a_time < $b_time)
1744
			return -1;
1745
		elseif ($$a_time == $b_time)
1746
			return 0;
1747
		else
1748
			return 1;
1749
	}
1750
}
1751
function version_get_string_value($a) {
1752
	$strs = array(
1753
		0 => "ALPHA-ALPHA",
1754
		2 => "ALPHA",
1755
		3 => "BETA",
1756
		4 => "B",
1757
		5 => "C",
1758
		6 => "D",
1759
		7 => "RC",
1760
		8 => "RELEASE"
1761
	);
1762
	$major = 0;
1763
	$minor = 0;
1764
	foreach ($strs as $num => $str) {
1765
		if (substr($a, 0, strlen($str)) == $str) {
1766
			$major = $num;
1767
			$n = substr($a, strlen($str));
1768
			if (is_numeric($n))
1769
				$minor = $n;
1770
			break;
1771
		}
1772
	}
1773
	return "{$major}.{$minor}";
1774
}
1775
function version_compare_string($a, $b) {
1776
	return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
1777
}
1778
function version_compare_numeric($a, $b) {
1779
	$a_arr = explode('.', rtrim($a, '.0'));
1780
	$b_arr = explode('.', rtrim($b, '.0'));
1781

    
1782
	foreach ($a_arr as $n => $val) {
1783
		if (array_key_exists($n, $b_arr)) {
1784
			// So far so good, both have values at this minor version level. Compare.
1785
			if ($val > $b_arr[$n])
1786
				return 1;
1787
			elseif ($val < $b_arr[$n])
1788
				return -1;
1789
		} else {
1790
			// a is greater, since b doesn't have any minor version here.
1791
			return 1;
1792
		}
1793
	}
1794
	if (count($b_arr) > count($a_arr)) {
1795
		// b is longer than a, so it must be greater.
1796
		return -1;
1797
	} else {
1798
		// Both a and b are of equal length and value.
1799
		return 0;
1800
	}
1801
}
1802
function pfs_version_compare($cur_time, $cur_text, $remote) {
1803
	// First try date compare
1804
	$v = version_compare_dates($cur_time, $remote);
1805
	if ($v === FALSE) {
1806
		// If that fails, try to compare by string
1807
		// Before anything else, simply test if the strings are equal
1808
		if (($cur_text == $remote) || ($cur_time == $remote))
1809
			return 0;
1810
		list($cur_num, $cur_str) = explode('-', $cur_text);
1811
		list($rem_num, $rem_str) = explode('-', $remote);
1812

    
1813
		// First try to compare the numeric parts of the version string.
1814
		$v = version_compare_numeric($cur_num, $rem_num);
1815

    
1816
		// If the numeric parts are the same, compare the string parts.
1817
		if ($v == 0)
1818
			return version_compare_string($cur_str, $rem_str);
1819
	}
1820
	return $v;
1821
}
1822
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
1823
	$urltable_prefix = "/var/db/aliastables/";
1824
	$urltable_filename = $urltable_prefix . $name . ".txt";
1825

    
1826
	// Make the aliases directory if it doesn't exist
1827
	if (!file_exists($urltable_prefix)) {
1828
		mkdir($urltable_prefix);
1829
	} elseif (!is_dir($urltable_prefix)) {
1830
		unlink($urltable_prefix);
1831
		mkdir($urltable_prefix);
1832
	}
1833

    
1834
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
1835
	if (!file_exists($urltable_filename)
1836
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400))
1837
		|| $forceupdate) {
1838

    
1839
		// Try to fetch the URL supplied
1840
		conf_mount_rw();
1841
		unlink_if_exists($urltable_filename . ".tmp");
1842
		// 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.
1843
		mwexec("/usr/bin/fetch -q -o " . escapeshellarg($urltable_filename . ".tmp") . " " . escapeshellarg($url));
1844
		// Remove comments. Might need some grep-fu to only allow lines that look like IPs/subnets
1845
		if (file_exists($urltable_filename . ".tmp")) {
1846
			mwexec("/usr/bin/grep -v '^#' " . escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
1847
			unlink_if_exists($urltable_filename . ".tmp");
1848
		} else
1849
			mwexec("/usr/bin/touch {$urltable_filename}");
1850
		conf_mount_ro();
1851
		return true;
1852
	} else {
1853
		// File exists, and it doesn't need updated.
1854
		return -1;
1855
	}
1856
}
1857
function get_real_slice_from_glabel($label) {
1858
	$label = escapeshellarg($label);
1859
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1860
}
1861
function nanobsd_get_boot_slice() {
1862
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1863
}
1864
function nanobsd_get_boot_drive() {
1865
	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`);
1866
}
1867
function nanobsd_get_active_slice() {
1868
	$boot_drive = nanobsd_get_boot_drive();
1869
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1870

    
1871
	return "{$boot_drive}s{$active}";
1872
}
1873
function nanobsd_get_size() {
1874
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
1875
}
1876
function nanobsd_switch_boot_slice() {
1877
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1878
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1879
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1880
	nanobsd_detect_slice_info();
1881

    
1882
	if ($BOOTFLASH == $ACTIVE_SLICE) {
1883
		$slice = $TOFLASH;
1884
	} else {
1885
		$slice = $BOOTFLASH;
1886
	}
1887

    
1888
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1889
	ob_implicit_flush(1);
1890
	if(strstr($slice, "s2")) {
1891
		$ASLICE="2";
1892
		$AOLDSLICE="1";
1893
		$AGLABEL_SLICE="pfsense1";
1894
		$AUFS_ID="1";
1895
		$AOLD_UFS_ID="0";
1896
	} else {
1897
		$ASLICE="1";
1898
		$AOLDSLICE="2";
1899
		$AGLABEL_SLICE="pfsense0";
1900
		$AUFS_ID="0";
1901
		$AOLD_UFS_ID="1";
1902
	}
1903
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
1904
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
1905
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
1906
	conf_mount_rw();
1907
	exec("sysctl kern.geom.debugflags=16");
1908
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
1909
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
1910
	// We can't update these if they are mounted now.
1911
	if ($BOOTFLASH != $slice) {
1912
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
1913
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
1914
	}
1915
	exec("/sbin/sysctl kern.geom.debugflags=0");
1916
	conf_mount_ro();
1917
}
1918
function nanobsd_clone_slice() {
1919
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1920
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1921
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1922
	nanobsd_detect_slice_info();
1923

    
1924
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1925
	ob_implicit_flush(1);
1926
	exec("/sbin/sysctl kern.geom.debugflags=16");
1927
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
1928
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
1929
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
1930
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
1931
	exec("/sbin/sysctl kern.geom.debugflags=0");
1932
	if($status) {
1933
		return false;
1934
	} else {
1935
		return true;
1936
	}
1937
}
1938
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
1939
	$tmppath = "/tmp/{$gslice}";
1940
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
1941

    
1942
	exec("/bin/mkdir {$tmppath}");
1943
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
1944
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
1945
	exec("/bin/cp /etc/fstab {$fstabpath}");
1946

    
1947
	if (!file_exists($fstabpath)) {
1948
		$fstab = <<<EOF
1949
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
1950
/dev/ufs/cf /cf ufs ro,noatime 1 1
1951
EOF;
1952
		if (file_put_contents($fstabpath, $fstab))
1953
			$status = true;
1954
		else
1955
			$status = false;
1956
	} else {
1957
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
1958
	}
1959
	exec("/sbin/umount {$tmppath}");
1960
	exec("/bin/rmdir {$tmppath}");
1961

    
1962
	return $status;
1963
}
1964
function nanobsd_detect_slice_info() {
1965
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1966
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1967
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1968

    
1969
	$BOOT_DEVICE=nanobsd_get_boot_slice();
1970
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
1971
	$BOOT_DRIVE=nanobsd_get_boot_drive();
1972
	$ACTIVE_SLICE=nanobsd_get_active_slice();
1973

    
1974
	// Detect which slice is active and set information.
1975
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
1976
		$SLICE="2";
1977
		$OLDSLICE="1";
1978
		$GLABEL_SLICE="pfsense1";
1979
		$UFS_ID="1";
1980
		$OLD_UFS_ID="0";
1981

    
1982
	} else {
1983
		$SLICE="1";
1984
		$OLDSLICE="2";
1985
		$GLABEL_SLICE="pfsense0";
1986
		$UFS_ID="0";
1987
		$OLD_UFS_ID="1";
1988
	}
1989
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
1990
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
1991
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
1992
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
1993
}
1994

    
1995
function nanobsd_friendly_slice_name($slicename) {
1996
	global $g;
1997
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
1998
}
1999

    
2000
function get_include_contents($filename) {
2001
    if (is_file($filename)) {
2002
        ob_start();
2003
        include $filename;
2004
        $contents = ob_get_contents();
2005
        ob_end_clean();
2006
        return $contents;
2007
    }
2008
    return false;
2009
}
2010

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

    
2146
function get_country_name($country_code) {
2147
	if ($country_code != "ALL" && strlen($country_code) != 2)
2148
		return "";
2149

    
2150
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2151
	$country_names_contents = file_get_contents($country_names_xml);
2152
	$country_names = xml2array($country_names_contents);
2153

    
2154
	if($country_code == "ALL") {
2155
		$country_list = array();
2156
		foreach($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2157
			$country_list[] = array( "code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2158
						 "name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])) );
2159
		}
2160
		return $country_list;
2161
	}
2162

    
2163
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2164
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2165
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2166
		}
2167
	}
2168
	return "";
2169
}
2170

    
2171
/* sort by interface only, retain the original order of rules that apply to
2172
   the same interface */
2173
function filter_rules_sort() {
2174
	global $config;
2175

    
2176
	/* mark each rule with the sequence number (to retain the order while sorting) */
2177
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2178
		$config['filter']['rule'][$i]['seq'] = $i;
2179

    
2180
	usort($config['filter']['rule'], "filter_rules_compare");
2181

    
2182
	/* strip the sequence numbers again */
2183
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2184
		unset($config['filter']['rule'][$i]['seq']);
2185
}
2186
function filter_rules_compare($a, $b) {
2187
	if (isset($a['floating']) && isset($b['floating']))
2188
		return $a['seq'] - $b['seq'];
2189
	else if (isset($a['floating']))
2190
		return -1;
2191
	else if (isset($b['floating']))
2192
		return 1;
2193
	else if ($a['interface'] == $b['interface'])
2194
		return $a['seq'] - $b['seq'];
2195
	else
2196
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2197
}
2198

    
2199
/****f* pfsense-utils/load_mac_manufacturer_table
2200
 * NAME
2201
 *   load_mac_manufacturer_table
2202
 * INPUTS
2203
 *   none
2204
 * RESULT
2205
 *   returns associative array with MAC-Manufacturer pairs
2206
 ******/
2207
function load_mac_manufacturer_table() {
2208
	/* load MAC-Manufacture data from the file */
2209
	$macs = false;
2210
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes"))
2211
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2212
	if ($macs){
2213
		foreach ($macs as $line){
2214
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)){
2215
				/* store values like this $mac_man['000C29']='VMware' */
2216
				$mac_man["$matches[1]"]=$matches[2];
2217
			}
2218
		}
2219
 		return $mac_man;
2220
	} else
2221
		return -1;
2222

    
2223
}
2224

    
2225
/****f* pfsense-utils/is_ipaddr_configured
2226
 * NAME
2227
 *   is_ipaddr_configured
2228
 * INPUTS
2229
 *   IP Address to check.
2230
 * RESULT
2231
 *   returns true if the IP Address is
2232
 *   configured and present on this device.
2233
*/
2234
function is_ipaddr_configured($ipaddr) {
2235
	$interface_list_ips = get_configured_ip_addresses();
2236
	foreach($interface_list_ips as $ilips) {
2237
		if(strcasecmp($ipaddr, $ilips) == 0) 
2238
				return true;
2239
	}	
2240
}
2241

    
2242
/****f* pfsense-utils/pfSense_handle_custom_code
2243
 * NAME
2244
 *   pfSense_handle_custom_code
2245
 * INPUTS
2246
 *   directory name to process
2247
 * RESULT
2248
 *   globs the directory and includes the files
2249
 */
2250
function pfSense_handle_custom_code($src_dir) {
2251
	// Allow extending of the nat edit page and include custom input validation 
2252
	if(is_dir("$src_dir")) {
2253
		$cf = glob($src_dir . "/*.inc");
2254
		foreach($cf as $nf) {
2255
			if($nf == "." || $nf == "..") 
2256
				continue;
2257
			// Include the extra handler
2258
			include("$nf");
2259
		}
2260
	}
2261
}
2262

    
2263
?>
(36-36/62)