Project

General

Profile

Download (61.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
               "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 net.inet.carp.allow | cut -d" " -f2`;
324
    if(intval($status) == "0") return false;
325
    return true;
326
}
327

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

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

    
338
/*
339
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
340
 */
341
function get_carp_interface_status($carpinterface) {
342
	/* basically cache the contents of ifconfig statement
343
	to speed up this routine */
344
	global $carp_query;
345
	if($carp_query == "")
346
		$carp_query = split("\n", `/sbin/ifconfig $carpinterface | grep carp`);
347
	foreach($carp_query as $int) {
348
		if(stristr($int, "MASTER")) 
349
			return "MASTER";
350
		if(stristr($int, "BACKUP")) 
351
			return "BACKUP";
352
		if(stristr($int, "INIT")) 
353
			return "INIT";
354
	}
355
	return;
356
}
357

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

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

    
375
/*
376
 * remove_text_from_file
377
 * remove $text from file $file
378
 */
379
function remove_text_from_file($file, $text) {
380
	global $fd_log;
381
	if($fd_log)
382
		fwrite($fd_log, "Adding needed text items:\n");
383
	$filecontents = file_get_contents($file);
384
	$textTMP = str_replace($text, "", $filecontents);
385
	$text = $textTMP;
386
	if($fd_log)
387
		fwrite($fd_log, $text);
388
	$fd = fopen($file, "w");
389
	fwrite($fd, $text);
390
	fclose($fd);
391
}
392

    
393
/*
394
 * add_text_to_file($file, $text): adds $text to $file.
395
 * replaces the text if it already exists.
396
 */
397
function add_text_to_file($file, $text, $replace = false) {
398
	if(file_exists($file) and is_writable($file)) {
399
		$filecontents = file($file);
400
		$fout = fopen($file, "w");
401

    
402
		$filecontents = array_map('rtrim', $filecontents);
403
		array_push($filecontents, $text);
404
		if ($replace)
405
			$filecontents = array_unique($filecontents);
406

    
407
		$file_text = implode("\n", $filecontents);
408

    
409
		fwrite($fout, $file_text);
410
		fclose($fout);
411
		return true;
412
	} else {
413
		return false;
414
	}
415
}
416

    
417
/*
418
 *   after_sync_bump_adv_skew(): create skew values by 1S
419
 */
420
function after_sync_bump_adv_skew() {
421
	global $config, $g;
422
	$processed_skew = 1;
423
	$a_vip = &$config['virtualip']['vip'];
424
	foreach ($a_vip as $vipent) {
425
		if($vipent['advskew'] <> "") {
426
			$processed_skew = 1;
427
			$vipent['advskew'] = $vipent['advskew']+1;
428
		}
429
	}
430
	if($processed_skew == 1)
431
		write_config("After synch increase advertising skew");
432
}
433

    
434
/*
435
 * get_filename_from_url($url): converts a url to its filename.
436
 */
437
function get_filename_from_url($url) {
438
	return basename($url);
439
}
440

    
441
/*
442
 *   get_dir: return an array of $dir
443
 */
444
function get_dir($dir) {
445
	$dir_array = array();
446
	$d = dir($dir);
447
	while (false !== ($entry = $d->read())) {
448
		array_push($dir_array, $entry);
449
	}
450
	$d->close();
451
	return $dir_array;
452
}
453

    
454
/****f* pfsense-utils/WakeOnLan
455
 * NAME
456
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
457
 * RESULT
458
 *   true/false - true if the operation was successful
459
 ******/
460
function WakeOnLan($addr, $mac)
461
{
462
	$addr_byte = explode(':', $mac);
463
	$hw_addr = '';
464

    
465
	for ($a=0; $a < 6; $a++)
466
		$hw_addr .= chr(hexdec($addr_byte[$a]));
467

    
468
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
469

    
470
	for ($a = 1; $a <= 16; $a++)
471
		$msg .= $hw_addr;
472

    
473
	// send it to the broadcast address using UDP
474
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
475
	if ($s == false) {
476
		log_error("Error creating socket!");
477
		log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
478
	} else {
479
		// setting a broadcast option to socket:
480
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
481
		if($opt_ret < 0)
482
			log_error("setsockopt() failed, error: " . strerror($opt_ret));
483
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
484
		socket_close($s);
485
		log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}");
486
		return true;
487
	}
488

    
489
	return false;
490
}
491

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

    
522
/*
523
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
524
 *					 Useful for finding paths and stripping file extensions.
525
 */
526
function reverse_strrchr($haystack, $needle) {
527
	if (!is_string($haystack))
528
		return;
529
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
530
}
531

    
532
/*
533
 *  backup_config_section($section): returns as an xml file string of
534
 *                                   the configuration section
535
 */
536
function backup_config_section($section) {
537
	global $config;
538
	$new_section = &$config[$section];
539
	/* generate configuration XML */
540
	$xmlconfig = dump_xml_config($new_section, $section);
541
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
542
	return $xmlconfig;
543
}
544

    
545
/*
546
 *  restore_config_section($section, new_contents): restore a configuration section,
547
 *                                                  and write the configuration out
548
 *                                                  to disk/cf.
549
 */
550
function restore_config_section($section, $new_contents) {
551
	global $config, $g;
552
	conf_mount_rw();
553
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
554
	fwrite($fout, $new_contents);
555
	fclose($fout);
556
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
557
	if ($section_xml != -1)
558
		$config[$section] = &$section_xml;
559
	@unlink($g['tmp_path'] . "/tmpxml");
560
	if(file_exists("{$g['tmp_path']}/config.cache"))
561
		unlink("{$g['tmp_path']}/config.cache");
562
	write_config("Restored {$section} of config file (maybe from CARP partner)");
563
	conf_mount_ro();
564
	return;
565
}
566

    
567
/*
568
 *  merge_config_section($section, new_contents):   restore a configuration section,
569
 *                                                  and write the configuration out
570
 *                                                  to disk/cf.  But preserve the prior
571
 * 													structure if needed
572
 */
573
function merge_config_section($section, $new_contents) {
574
	global $config;
575
	conf_mount_rw();
576
	$fname = get_tmp_filename();
577
	$fout = fopen($fname, "w");
578
	fwrite($fout, $new_contents);
579
	fclose($fout);
580
	$section_xml = parse_xml_config($fname, $section);
581
	$config[$section] = $section_xml;
582
	unlink($fname);
583
	write_config("Restored {$section} of config file (maybe from CARP partner)");
584
	conf_mount_ro();
585
	return;
586
}
587

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

    
610
";
611

    
612
	$errno = "";
613
	$errstr = "";
614
	$fp = fsockopen($server, $port, $errno, $errstr);
615
	if (!$fp) {
616
		return false;
617
	}
618

    
619
	fputs($fp, $headers);
620
	fputs($fp, $urlencoded);
621

    
622
	$ret = "";
623
	while (!feof($fp))
624
		$ret.= fgets($fp, 1024);
625
	fclose($fp);
626

    
627
	return $ret;
628
}
629

    
630
/*
631
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
632
 */
633
if (!function_exists('php_check_syntax')){
634
	global $g;
635
	function php_check_syntax($code_to_check, &$errormessage){
636
		return false;
637
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
638
		$code = $_POST['content'];
639
		$code = str_replace("<?php", "", $code);
640
		$code = str_replace("?>", "", $code);
641
		fwrite($fout, "<?php\n\n");
642
		fwrite($fout, $code_to_check);
643
		fwrite($fout, "\n\n?>\n");
644
		fclose($fout);
645
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
646
		$output = exec_command($command);
647
		if (stristr($output, "Errors parsing") == false) {
648
			echo "false\n";
649
			$errormessage = '';
650
			return(false);
651
		} else {
652
			$errormessage = $output;
653
			return(true);
654
		}
655
	}
656
}
657

    
658
/*
659
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
660
 */
661
if (!function_exists('php_check_syntax')){
662
	function php_check_syntax($code_to_check, &$errormessage){
663
		return false;
664
		$command = "/usr/local/bin/php -l " . $code_to_check;
665
		$output = exec_command($command);
666
		if (stristr($output, "Errors parsing") == false) {
667
			echo "false\n";
668
			$errormessage = '';
669
			return(false);
670
		} else {
671
			$errormessage = $output;
672
			return(true);
673
		}
674
	}
675
}
676

    
677
/*
678
 * rmdir_recursive($path,$follow_links=false)
679
 * Recursively remove a directory tree (rm -rf path)
680
 * This is for directories _only_
681
 */
682
function rmdir_recursive($path,$follow_links=false) {
683
	$to_do = glob($path);
684
	if(!is_array($to_do)) $to_do = array($to_do);
685
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
686
		if(file_exists($workingdir)) {
687
			if(is_dir($workingdir)) {
688
				$dir = opendir($workingdir);
689
				while ($entry = readdir($dir)) {
690
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
691
						unlink("$workingdir/$entry");
692
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
693
						rmdir_recursive("$workingdir/$entry");
694
				}
695
				closedir($dir);
696
				rmdir($workingdir);
697
			} elseif (is_file($workingdir)) {
698
				unlink($workingdir);
699
			}
700
               	}
701
	}
702
	return;
703
}
704

    
705
/*
706
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
707
 */
708
function call_pfsense_method($method, $params, $timeout = 0) {
709
	global $g, $config;
710

    
711
	$ip = gethostbyname($g['product_website']);
712
	if($ip == $g['product_website'])
713
		return false;
714

    
715
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
716
	$xmlrpc_path = $g['xmlrpcpath'];
717
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
718
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
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
	$resp = $cli->send($msg, $timeout);
727
	if(!$resp) {
728
		log_error("XMLRPC communication error: " . $cli->errstr);
729
		return false;
730
	} elseif($resp->faultCode()) {
731
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
732
		return false;
733
	} else {
734
		return XML_RPC_Decode($resp->value());
735
	}
736
}
737

    
738
/*
739
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
740
 */
741
function check_firmware_version($tocheck = "all", $return_php = true) {
742
	global $g, $config;
743
	$ip = gethostbyname($g['product_website']);
744
	if($ip == $g['product_website'])
745
		return false;
746
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
747
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
748
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
749
		"platform" => trim(file_get_contents('/etc/platform'))
750
		);
751
	if($tocheck == "all") {
752
		$params = $rawparams;
753
	} else {
754
		foreach($tocheck as $check) {
755
			$params['check'] = $rawparams['check'];
756
			$params['platform'] = $rawparams['platform'];
757
		}
758
	}
759
	if($config['system']['firmware']['branch']) {
760
		$params['branch'] = $config['system']['firmware']['branch'];
761
	}
762
	if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) {
763
		return false;
764
	} else {
765
		$versions["current"] = $params;
766
	}
767
	return $versions;
768
}
769

    
770
function get_disk_info() {
771
	$diskout = "";
772
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
773
	return explode(' ', $diskout[0]);
774
	// $size, $used, $avail, $cap
775
}
776

    
777
/****f* pfsense-utils/strncpy
778
 * NAME
779
 *   strncpy - copy strings
780
 * INPUTS
781
 *   &$dst, $src, $length
782
 * RESULT
783
 *   none
784
 ******/
785
function strncpy(&$dst, $src, $length) {
786
	if (strlen($src) > $length) {
787
		$dst = substr($src, 0, $length);
788
	} else {
789
		$dst = $src;
790
	}
791
}
792

    
793
/****f* pfsense-utils/reload_interfaces_sync
794
 * NAME
795
 *   reload_interfaces - reload all interfaces
796
 * INPUTS
797
 *   none
798
 * RESULT
799
 *   none
800
 ******/
801
function reload_interfaces_sync() {
802
	global $config, $g;
803

    
804
	/* XXX: Use locks?! */
805
	if (file_exists("{$g['tmp_path']}/reloading_all")) {
806
		log_error("WARNING: Recursive call to interfaces sync!");
807
		return;
808
	}
809
	touch("{$g['tmp_path']}/reloading_all");
810

    
811
	if($g['debug'])
812
		log_error("reload_interfaces_sync() is starting.");
813

    
814
	/* parse config.xml again */
815
	$config = parse_config(true);
816

    
817
	/* enable routing */
818
	system_routing_enable();
819
	if($g['debug'])
820
		log_error("Enabling system routing");
821

    
822
	if($g['debug'])
823
		log_error("Cleaning up Interfaces");
824

    
825
	/* set up interfaces */
826
	interfaces_configure();
827

    
828
	/* remove reloading_all trigger */
829
	if($g['debug'])
830
		log_error("Removing {$g['tmp_path']}/reloading_all");
831

    
832
	/* start devd back up */
833
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
834
}
835

    
836
/****f* pfsense-utils/reload_all
837
 * NAME
838
 *   reload_all - triggers a reload of all settings
839
 *   * INPUTS
840
 *   none
841
 * RESULT
842
 *   none
843
 ******/
844
function reload_all() {
845
	global $g;
846
	send_event("service reload all");
847
}
848

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

    
862
/****f* pfsense-utils/reload_all_sync
863
 * NAME
864
 *   reload_all - reload all settings
865
 *   * INPUTS
866
 *   none
867
 * RESULT
868
 *   none
869
 ******/
870
function reload_all_sync() {
871
	global $config, $g;
872

    
873
	$g['booting'] = false;
874

    
875
	/* XXX: Use locks?! */
876
        if (file_exists("{$g['tmp_path']}/reloading_all")) {
877
                log_error("WARNING: Recursive call to reload all sync!");
878
                return;
879
        }
880
	touch("{$g['tmp_path']}/reloading_all");
881

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

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

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

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

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

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

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

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

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

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

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

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

    
921
	/* restart webConfigurator if needed */
922
	send_event("service restart webgui");
923

    
924
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
925
}
926

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

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

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

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

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

    
1027
	conf_mount_ro();
1028
	return;
1029
}
1030

    
1031
function print_value_list($list, $count = 10, $separator = ",") {
1032
	$list = implode($separator, array_slice($list, 0, $count));
1033
	if(count($list) < $count) {
1034
		$list .= ".";
1035
	} else {
1036
		$list .= "...";
1037
	}
1038
	return $list;
1039
}
1040

    
1041
/* DHCP enabled on any interfaces? */
1042
function is_dhcp_server_enabled() 
1043
{
1044
	global $config;
1045

    
1046
	$dhcpdenable = false;
1047
	
1048
	if (!is_array($config['dhcpd']))
1049
		return false;
1050

    
1051
	$Iflist = get_configured_interface_list();
1052

    
1053
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1054
		if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
1055
			$dhcpdenable = true;
1056
			break;
1057
		}
1058
	}
1059

    
1060
	return $dhcpdenable;
1061
}
1062

    
1063
function convert_seconds_to_hms($sec){
1064
	$min=$hrs=0;
1065
	if ($sec != 0){
1066
		$min = floor($sec/60);
1067
		$sec %= 60;
1068
	}
1069
	if ($min != 0){
1070
		$hrs = floor($min/60);
1071
		$min %= 60;
1072
	}
1073
	if ($sec < 10)
1074
		$sec = "0".$sec;
1075
	if ($min < 10)
1076
		$min = "0".$min;
1077
	if ($hrs < 10)
1078
		$hrs = "0".$hrs;
1079
	$result = $hrs.":".$min.":".$sec;
1080
	return $result;
1081
}
1082

    
1083
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1084

    
1085
function get_ppp_uptime($port){
1086
	if (file_exists("/conf/{$port}.log")){
1087
    	$saved_time = file_get_contents("/conf/{$port}.log");
1088
    	$uptime_data = explode("\n",$saved_time);
1089
		$sec=0;
1090
		foreach($uptime_data as $upt) {
1091
			$sec += substr($upt, 1 + strpos($upt, " "));
1092
 		}
1093
		return convert_seconds_to_hms($sec);
1094
	} else {
1095
		$total_time = "No history data found!";
1096
		return $total_time;
1097
	}
1098
}
1099

    
1100
//returns interface information
1101
function get_interface_info($ifdescr) {
1102
	global $config, $g;
1103

    
1104
	$ifinfo = array();
1105
	if (empty($config['interfaces'][$ifdescr]))
1106
		return;
1107
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1108
	$ifinfo['if'] = get_real_interface($ifdescr);
1109

    
1110
	$chkif = $ifinfo['if'];
1111
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1112
	$ifinfo['status'] = $ifinfotmp['status'];
1113
	if (empty($ifinfo['status']))
1114
                $ifinfo['status'] = "down";
1115
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1116
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1117
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1118
	if (isset($ifinfotmp['link0']))
1119
		$link0 = "down";
1120
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1121
        $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1122
        $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1123
        $ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1124
        $ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1125
        $ifinfo['collisions'] = $ifinfotmp['collisions'];
1126

    
1127
	/* Use pfctl for non wrapping 64 bit counters */
1128
	/* Pass */
1129
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1130
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1131
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1132
	$in4_pass = $pf_in4_pass[5];
1133
	$out4_pass = $pf_out4_pass[5];
1134
	$in4_pass_packets = $pf_in4_pass[3];
1135
	$out4_pass_packets = $pf_out4_pass[3];
1136
	$ifinfo['inbytespass'] = $in4_pass;
1137
	$ifinfo['outbytespass'] = $out4_pass;
1138
	$ifinfo['inpktspass'] = $in4_pass_packets;
1139
	$ifinfo['outpktspass'] = $out4_pass_packets;
1140

    
1141
	/* Block */
1142
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1143
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1144
	$in4_block = $pf_in4_block[5];
1145
	$out4_block = $pf_out4_block[5];
1146
	$in4_block_packets = $pf_in4_block[3];
1147
	$out4_block_packets = $pf_out4_block[3];
1148
	$ifinfo['inbytesblock'] = $in4_block;
1149
	$ifinfo['outbytesblock'] = $out4_block;
1150
	$ifinfo['inpktsblock'] = $in4_block_packets;
1151
	$ifinfo['outpktsblock'] = $out4_block_packets;
1152

    
1153
	$ifinfo['inbytes'] = $in4_pass + $in4_block;
1154
	$ifinfo['outbytes'] = $out4_pass + $out4_block;
1155
	$ifinfo['inpkts'] = $in4_pass_packets + $in4_block_packets;
1156
	$ifinfo['outpkts'] = $in4_pass_packets + $out4_block_packets;
1157
		
1158
	$ifconfiginfo = "";
1159
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1160
	switch ($link_type) {
1161
	 /* DHCP? -> see if dhclient is up */
1162
	case "dhcp":
1163
	case "carpdev-dhcp":
1164
		/* see if dhclient is up */
1165
		if (find_dhclient_process($ifinfo['if']) <> "")
1166
			$ifinfo['dhcplink'] = "up";
1167
		else
1168
			$ifinfo['dhcplink'] = "down";
1169

    
1170
		break;
1171
	/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1172
	case "pppoe":
1173
	case "pptp":
1174
	case "l2tp":
1175
		if ($ifinfo['status'] == "up" && !isset($link0))
1176
			/* get PPPoE link status for dial on demand */
1177
			$ifinfo["{$link_type}link"] = "up";
1178
		else
1179
			$ifinfo["{$link_type}link"] = "down";
1180

    
1181
		break;
1182
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1183
	case "ppp":
1184
		if ($ifinfo['status'] == "up")
1185
			$ifinfo['ppplink'] = "up";
1186
		else
1187
			$ifinfo['ppplink'] = "down" ;
1188

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

    
1230
			/* don't list media/speed for wireless cards, as it always
1231
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1232
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1233
				$ifinfo['media'] = $matches[1];
1234
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1235
				$ifinfo['media'] = $matches[1];
1236
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1237
				$ifinfo['media'] = $matches[1];
1238
			}
1239

    
1240
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1241
				if ($matches[1] != "active")
1242
					$ifinfo['status'] = $matches[1];
1243
				if($ifinfo['status'] == "running")
1244
					$ifinfo['status'] = "up";
1245
			}
1246
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1247
				$ifinfo['channel'] = $matches[1];
1248
			}
1249
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1250
				if ($matches[1][0] == '"')
1251
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1252
				else
1253
					$ifinfo['ssid'] = $matches[1];
1254
			}
1255
		}
1256
		foreach($wifconfiginfo as $ici) {
1257
			$elements = preg_split("/[ ]+/i", $ici);
1258
			if ($elements[0] != "") {
1259
				$ifinfo['bssid'] = $elements[0];
1260
			}
1261
			if ($elements[3] != "") {
1262
				$ifinfo['rate'] = $elements[3];
1263
			}
1264
			if ($elements[4] != "") {
1265
				$ifinfo['rssi'] = $elements[4];
1266
			}
1267

    
1268
		}
1269
		/* lookup the gateway */
1270
		if (interface_has_gateway($ifdescr)) 
1271
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1272
	}
1273

    
1274
	$bridge = "";
1275
	$bridge = link_interface_to_bridge($ifdescr);
1276
	if($bridge) {
1277
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1278
		if(stristr($bridge_text, "blocking") <> false) {
1279
			$ifinfo['bridge'] = "<b><font color='red'>blocking</font></b> - check for ethernet loops";
1280
			$ifinfo['bridgeint'] = $bridge;
1281
		} else if(stristr($bridge_text, "learning") <> false) {
1282
			$ifinfo['bridge'] = "learning";
1283
			$ifinfo['bridgeint'] = $bridge;
1284
		} else if(stristr($bridge_text, "forwarding") <> false) {
1285
			$ifinfo['bridge'] = "forwarding";
1286
			$ifinfo['bridgeint'] = $bridge;
1287
		}
1288
	}
1289

    
1290
	return $ifinfo;
1291
}
1292

    
1293
//returns cpu speed of processor. Good for determining capabilities of machine
1294
function get_cpu_speed() {
1295
	 return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1296
}
1297

    
1298
function add_hostname_to_watch($hostname) {
1299
	if(!is_dir("/var/db/dnscache")) {
1300
		mkdir("/var/db/dnscache");
1301
	}
1302
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1303
		$domrecords = array();
1304
		$domips = array();
1305
		exec("host -t A $hostname", $domrecords, $rethost);
1306
		if($rethost == 0) {
1307
			foreach($domrecords as $domr) {
1308
				$doml = explode(" ", $domr);
1309
				$domip = $doml[3];
1310
				/* fill array with domain ip addresses */
1311
				if(is_ipaddr($domip)) {
1312
					$domips[] = $domip;
1313
				}
1314
			}
1315
		}
1316
		sort($domips);
1317
		$contents = "";
1318
		if(! empty($domips)) {
1319
			foreach($domips as $ip) {
1320
				$contents .= "$ip\n";
1321
			}
1322
		}
1323
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1324
	}
1325
}
1326

    
1327
function is_fqdn($fqdn) {
1328
	$hostname = false;
1329
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1330
		$hostname = true;
1331
	}
1332
	if(preg_match("/\.\./", $fqdn)) {
1333
		$hostname = false;
1334
	}
1335
	if(preg_match("/^\./i", $fqdn)) { 
1336
		$hostname = false;
1337
	}
1338
	if(preg_match("/\//i", $fqdn)) {
1339
		$hostname = false;
1340
	}
1341
	return($hostname);
1342
}
1343

    
1344
function pfsense_default_state_size() {
1345
  /* get system memory amount */
1346
  $memory = get_memory();
1347
  $avail = $memory[0];
1348
  /* Be cautious and only allocate 10% of system memory to the state table */
1349
  $max_states = (int) ($avail/10)*1000;
1350
  return $max_states;
1351
}
1352

    
1353
function pfsense_default_table_entries_size() {
1354
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1355
	return $current;
1356
}
1357

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

    
1394
	if(trim($oldcontents) != trim($contents)) {
1395
		if($g['debug']) {
1396
			log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
1397
		}
1398
		return ($oldcontents);
1399
	} else {
1400
		return false;
1401
	}
1402
}
1403

    
1404
/*
1405
 * load_glxsb() - Load the glxsb crypto module if enabled in config.
1406
 */
1407
function load_glxsb() {
1408
	global $config, $g;
1409
	$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
1410
	if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
1411
		mwexec("/sbin/kldload glxsb");
1412
	}
1413
}
1414

    
1415
/****f* pfsense-utils/isvm
1416
 * NAME
1417
 *   isvm
1418
 * INPUTS
1419
 *	 none
1420
 * RESULT
1421
 *   returns true if machine is running under a virtual environment
1422
 ******/
1423
function isvm() {
1424
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1425
	$bios_vendor = strtolower(`/bin/kenv | /usr/bin/awk -F= '/smbios.bios.vendor/ {print $2}'`);
1426
	if(in_array($bios_vendor, $virtualenvs)) 
1427
		return true;
1428
	else
1429
		return false;
1430
}
1431

    
1432
function get_freebsd_version() {
1433
	$version = trim(`/usr/bin/uname -r | /usr/bin/cut  -d'.' -f1`);
1434
	return $version;
1435
}
1436

    
1437
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
1438
        global $ch, $fout, $file_size, $downloaded;
1439
        $file_size  = 1;
1440
        $downloaded = 1;
1441
        /* open destination file */
1442
        $fout = fopen($destination_file, "wb");
1443

    
1444
        /*
1445
         *      Originally by Author: Keyvan Minoukadeh
1446
         *      Modified by Scott Ullrich to return Content-Length size
1447
         */
1448

    
1449
        $ch = curl_init();
1450
        curl_setopt($ch, CURLOPT_URL, $url_file);
1451
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1452
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1453
        /* Don't verify SSL peers since we don't have the certificates to do so. */
1454
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1455
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1456
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1457
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
1458
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
1459

    
1460
        curl_exec($ch);
1461
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1462
        if($fout)
1463
                fclose($fout);
1464
        curl_close($ch);
1465
        return ($http_code == 200) ? true : $http_code;
1466
}
1467

    
1468
function read_header($ch, $string) {
1469
        global $file_size, $fout;
1470
        $length = strlen($string);
1471
        $regs = "";
1472
        ereg("(Content-Length:) (.*)", $string, $regs);
1473
        if($regs[2] <> "") {
1474
                $file_size = intval($regs[2]);
1475
        }
1476
        ob_flush();
1477
        return $length;
1478
}
1479

    
1480
function read_body($ch, $string) {
1481
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
1482
        $length = strlen($string);
1483
        $downloaded += intval($length);
1484
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1485
        $downloadProgress = 100 - $downloadProgress;
1486
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1487
                if($sendto == "status") {
1488
                        $tostatus = $static_status . $downloadProgress . "%";
1489
                        update_status($tostatus);
1490
                } else {
1491
                        $tooutput = $static_output . $downloadProgress . "%";
1492
                        update_output_window($tooutput);
1493
                }
1494
                update_progress_bar($downloadProgress);
1495
                $lastseen = $downloadProgress;
1496
        }
1497
        if($fout)
1498
                fwrite($fout, $string);
1499
        ob_flush();
1500
        return $length;
1501
}
1502

    
1503
/*
1504
 *   update_output_window: update bottom textarea dynamically.
1505
 */
1506
function update_output_window($text) {
1507
        global $pkg_interface;
1508
        $log = ereg_replace("\n", "\\n", $text);
1509
        if($pkg_interface == "console") {
1510
                /* too chatty */
1511
        } else {
1512
                echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
1513
        }
1514
        /* ensure that contents are written out */
1515
        ob_flush();
1516
}
1517

    
1518
/*
1519
 *   update_output_window: update top textarea dynamically.
1520
 */
1521
function update_status($status) {
1522
        global $pkg_interface;
1523
        if($pkg_interface == "console") {
1524
                echo $status . "\n";
1525
        } else {
1526
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
1527
        }
1528
        /* ensure that contents are written out */
1529
        ob_flush();
1530
}
1531

    
1532
/*
1533
 * update_progress_bar($percent): updates the javascript driven progress bar.
1534
 */
1535
function update_progress_bar($percent) {
1536
        global $pkg_interface;
1537
        if($percent > 100) $percent = 1;
1538
        if($pkg_interface <> "console") {
1539
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
1540
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1541
                echo "\n</script>";
1542
        } else {
1543
                echo " {$percent}%";
1544
        }
1545
}
1546

    
1547
/* 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. */
1548
if(!function_exists("split")) {
1549
	function split($seperator, $haystack, $limit = null) {
1550
		return preg_split($seperator, $haystack, $limit);
1551
	}
1552
}
1553

    
1554
function update_alias_names_upon_change($section, $subsection, $fielda, $fieldb, $new_alias_name, $origname) {
1555
	global $g, $config, $pconfig, $debug;
1556
	if(!$origname) 
1557
		return;
1558

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

    
1562
	if($fieldb) {
1563
		if($debug) fwrite($fd, "fieldb exists\n");
1564
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1565
			if($debug) fwrite($fd, "$i\n");
1566
			if($config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] == $origname) {
1567
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1568
				$config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] = $new_alias_name;
1569
			}
1570
		}	
1571
	} else {
1572
		if($debug) fwrite($fd, "fieldb does not exist\n");
1573
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1574
			if($config["$section"]["$subsection"][$i]["$fielda"] == $origname) {
1575
				$config["$section"]["$subsection"][$i]["$fielda"] = $new_alias_name;
1576
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1577
			}
1578
		}
1579
	}
1580

    
1581
	if($debug) fclose($fd);
1582

    
1583
}
1584

    
1585
function update_alias_url_data() {
1586
	global $config, $g;
1587

    
1588
	/* item is a url type */
1589
	$lockkey = lock('config');
1590
	if (is_array($config['aliases']['alias'])) {
1591
		foreach ($config['aliases']['alias'] as $x => $alias) {
1592
			if (empty($alias['aliasurl']))
1593
				continue;
1594

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

    
1639
function process_alias_unzip($temp_filename) {
1640
	if(!file_exists("/usr/local/bin/unzip"))
1641
		return;
1642
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.zip");
1643
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
1644
	unlink("{$temp_filename}/aliases.zip");
1645
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1646
	/* foreach through all extracted files and build up aliases file */
1647
	$fd = fopen("{$temp_filename}/aliases", "w");
1648
	foreach($files_to_process as $f2p) {
1649
		$file_contents = file_get_contents($f2p);
1650
		fwrite($fd, $file_contents);
1651
		unlink($f2p);
1652
	}
1653
	fclose($fd);
1654
}
1655

    
1656
function process_alias_tgz($temp_filename) {
1657
	if(!file_exists("/usr/bin/tar"))
1658
		return;
1659
	mwexec("/bin/mv {$temp_filename}/aliases {$temp_filename}/aliases.tgz");
1660
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
1661
	unlink("{$temp_filename}/aliases.tgz");
1662
	$files_to_process = return_dir_as_array("{$temp_filename}/");
1663
	/* foreach through all extracted files and build up aliases file */
1664
	$fd = fopen("{$temp_filename}/aliases", "w");
1665
	foreach($files_to_process as $f2p) {
1666
		$file_contents = file_get_contents($f2p);
1667
		fwrite($fd, $file_contents);
1668
		unlink($f2p);
1669
	}
1670
	fclose($fd);
1671
}
1672

    
1673
function version_compare_dates($a, $b) {
1674
	$a_time = strtotime($a);
1675
	$b_time = strtotime($b);
1676

    
1677
	if ((!$a_time) || (!$b_time)) {
1678
		return FALSE;
1679
	} else {
1680
		if ($a < $b)
1681
			return -1;
1682
		elseif ($a == $b)
1683
			return 0;
1684
		else
1685
			return 1;
1686
	}
1687
}
1688
function version_get_string_value($a) {
1689
	$strs = array(
1690
		0 => "ALPHA-ALPHA",
1691
		2 => "ALPHA",
1692
		3 => "BETA",
1693
		4 => "B",
1694
		5 => "C",
1695
		6 => "D",
1696
		7 => "RC",
1697
		8 => "RELEASE"
1698
	);
1699
	$major = 0;
1700
	$minor = 0;
1701
	foreach ($strs as $num => $str) {
1702
		if (substr($a, 0, strlen($str)) == $str) {
1703
			$major = $num;
1704
			$n = substr($a, strlen($str));
1705
			if (is_numeric($n))
1706
				$minor = $n;
1707
			break;
1708
		}
1709
	}
1710
	return "{$major}.{$minor}";
1711
}
1712
function version_compare_string($a, $b) {
1713
	return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
1714
}
1715
function version_compare_numeric($a, $b) {
1716
	$a_arr = explode('.', rtrim($a, '.0'));
1717
	$b_arr = explode('.', rtrim($b, '.0'));
1718

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

    
1750
		// First try to compare the numeric parts of the version string.
1751
		$v = version_compare_numeric($cur_num, $rem_num);
1752

    
1753
		// If the numeric parts are the same, compare the string parts.
1754
		if ($v == 0)
1755
			return version_compare_string($cur_str, $rem_str);
1756
	}
1757
	return $v;
1758
}
1759
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
1760
	$urltable_prefix = "/var/db/aliastables/";
1761
	$urltable_filename = $urltable_prefix . $name . ".txt";
1762

    
1763
	// Make the aliases directory if it doesn't exist
1764
	if (!file_exists($urltable_prefix)) {
1765
		mkdir($urltable_prefix);
1766
	} elseif (!is_dir($urltable_prefix)) {
1767
		unlink($urltable_prefix);
1768
		mkdir($urltable_prefix);
1769
	}
1770

    
1771
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
1772
	if (!file_exists($urltable_filename)
1773
		|| ((time() - filemtime($urltable_filename)) > ($freq * 86400))
1774
		|| $forceupdate) {
1775

    
1776
		// Try to fetch the URL supplied
1777
		conf_mount_rw();
1778
		unlink_if_exists($urltable_filename . ".tmp");
1779
		// 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.
1780
		mwexec("/usr/bin/fetch -q -o " . escapeshellarg($urltable_filename . ".tmp") . " " . escapeshellarg($url));
1781
		// Remove comments. Might need some grep-fu to only allow lines that look like IPs/subnets
1782
		mwexec("/usr/bin/grep -v '^#' " . escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
1783
		unlink_if_exists($urltable_filename . ".tmp");
1784
		conf_mount_ro();
1785
		if (filesize($urltable_filename)) {
1786
			return true;
1787
		} else {
1788
			// If it's unfetchable or an empty file, bail
1789
			return false;
1790
		}
1791
	} else {
1792
		// File exists, and it doesn't need updated.
1793
		return -1;
1794
	}
1795
}
1796
function get_real_slice_from_glabel($label) {
1797
	$label = escapeshellarg($label);
1798
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1799
}
1800
function nanobsd_get_boot_slice() {
1801
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1802
}
1803
function nanobsd_get_boot_drive() {
1804
	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`);
1805
}
1806
function nanobsd_get_active_slice() {
1807
	$boot_drive = nanobsd_get_boot_drive();
1808
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1809

    
1810
	return "{$boot_drive}s{$active}";
1811
}
1812
function nanobsd_get_size() {
1813
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
1814
}
1815
function nanobsd_switch_boot_slice() {
1816
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1817
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1818
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1819
	nanobsd_detect_slice_info();
1820

    
1821
	if ($BOOTFLASH == $ACTIVE_SLICE) {
1822
		$slice = $TOFLASH;
1823
	} else {
1824
		$slice = $BOOTFLASH;
1825
	}
1826

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

    
1863
	for ($i = 0; $i < ob_get_level(); $i++) { ob_end_flush(); }
1864
	ob_implicit_flush(1);
1865
	exec("/sbin/sysctl kern.geom.debugflags=16");
1866
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
1867
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
1868
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
1869
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
1870
	exec("/sbin/sysctl kern.geom.debugflags=0");
1871
	if($status) {
1872
		return false;
1873
	} else {
1874
		return true;
1875
	}
1876
}
1877
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
1878
	$tmppath = "/tmp/{$gslice}";
1879
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
1880

    
1881
	exec("/bin/mkdir {$tmppath}");
1882
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
1883
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
1884
	exec("/bin/cp /etc/fstab {$fstabpath}");
1885

    
1886
	if (!file_exists($fstabpath)) {
1887
		$fstab = <<<EOF
1888
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
1889
/dev/ufs/cf /cf ufs ro,noatime 1 1
1890
EOF;
1891
		if (file_put_contents($fstabpath, $fstab))
1892
			$status = true;
1893
		else
1894
			$status = false;
1895
	} else {
1896
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
1897
	}
1898
	exec("/sbin/umount {$tmppath}");
1899
	exec("/bin/rmdir {$tmppath}");
1900

    
1901
	return $status;
1902
}
1903
function nanobsd_detect_slice_info() {
1904
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1905
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1906
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1907

    
1908
	$BOOT_DEVICE=nanobsd_get_boot_slice();
1909
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
1910
	$BOOT_DRIVE=nanobsd_get_boot_drive();
1911
	$ACTIVE_SLICE=nanobsd_get_active_slice();
1912

    
1913
	// Detect which slice is active and set information.
1914
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
1915
		$SLICE="2";
1916
		$OLDSLICE="1";
1917
		$GLABEL_SLICE="pfsense1";
1918
		$UFS_ID="1";
1919
		$OLD_UFS_ID="0";
1920

    
1921
	} else {
1922
		$SLICE="1";
1923
		$OLDSLICE="2";
1924
		$GLABEL_SLICE="pfsense0";
1925
		$UFS_ID="0";
1926
		$OLD_UFS_ID="1";
1927
	}
1928
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
1929
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
1930
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
1931
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
1932
}
1933

    
1934
function nanobsd_friendly_slice_name($slicename) {
1935
	global $g;
1936
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
1937
}
1938

    
1939
function get_include_contents($filename) {
1940
    if (is_file($filename)) {
1941
        ob_start();
1942
        include $filename;
1943
        $contents = ob_get_contents();
1944
        ob_end_clean();
1945
        return $contents;
1946
    }
1947
    return false;
1948
}
1949

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

    
2085
function get_country_name($country_code) {
2086
	if ($country_code != "ALL" && strlen($country_code) != 2)
2087
		return "";
2088

    
2089
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2090
	$country_names_contents = file_get_contents($country_names_xml);
2091
	$country_names = xml2array($country_names_contents);
2092

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

    
2102
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2103
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2104
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2105
		}
2106
	}
2107
	return "";
2108
}
2109

    
2110
/* sort by interface only, retain the original order of rules that apply to
2111
   the same interface */
2112
function filter_rules_sort() {
2113
	global $config;
2114

    
2115
	/* mark each rule with the sequence number (to retain the order while sorting) */
2116
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2117
		$config['filter']['rule'][$i]['seq'] = $i;
2118

    
2119
	usort($config['filter']['rule'], "filter_rules_compare");
2120

    
2121
	/* strip the sequence numbers again */
2122
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++)
2123
		unset($config['filter']['rule'][$i]['seq']);
2124
}
2125
function filter_rules_compare($a, $b) {
2126
	if ($a['interface'] == $b['interface'] || ( isset($a['floating']) && isset($b['floating']) ))
2127
		return $a['seq'] - $b['seq'];
2128
	else if (isset($a['floating']))
2129
		return -1;
2130
	else if (isset($b['floating']))
2131
		return 1;
2132
	else
2133
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2134
}
2135

    
2136
?>
(30-30/54)