Project

General

Profile

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

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

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

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

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

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

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

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

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

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

    
183
	if(stristr($interface,"lnc"))
184
		return;
185

    
186
	/* translate wan, lan, opt -> real interface if needed */
187
	$int = get_real_interface($interface);
188
	if($int <> "") 
189
		$interface = $int;
190
	$int_family = preg_split("/[0-9]+/", $interface);
191
	$options = pfSense_get_interface_addresses($interface);
192
	if (!is_array($options))
193
		return;
194
	$supported_ints = array('fxp');
195
	if (in_array($int_family, $supported_ints)) {
196
		if(isset($config['system']['do_not_use_nic_microcode']))
197
			continue;
198
		if(does_interface_exist($interface)) 
199
			pfSense_interface_flags($interface, IFF_LINK0);
200
	}
201

    
202
	/* skip vlans for checksumming and polling */
203
	if(stristr($interface, "vlan")) 
204
		return;
205

    
206
	if(isset($config['system']['disablechecksumoffloading'])) {
207
		if (isset($options['encaps']['txcsum']))
208
			pfSense_interface_capabilities($interface, -IFCAP_TXCSUM);
209
		if (isset($options['encaps']['rxcsum']))
210
			pfSense_interface_capabilities($interface, -IFCAP_RXCSUM);
211
	} else {
212
		if (isset($options['caps']['txcsum']))
213
			pfSense_interface_capabilities($interface, IFCAP_TXCSUM);
214
		if (isset($options['caps']['rxcsum']))
215
			pfSense_interface_capabilities($interface, IFCAP_RXCSUM);
216
	}
217

    
218
	if(isset($config['system']['disablesegmentationoffloading'])) {
219
		if (isset($options['encaps']['tso4']))
220
			pfSense_interface_capabilities($interface, -IFCAP_TSO);
221
		if (isset($options['encaps']['tso6']))
222
			pfSense_interface_capabilities($interface, -IFCAP_TSO);
223
	} else {
224
		if (isset($options['caps']['tso4']))
225
			pfSense_interface_capabilities($interface, IFCAP_TSO);
226
		if (isset($options['caps']['tso6']))
227
			pfSense_interface_capabilities($interface, IFCAP_TSO);
228
	}
229

    
230
	if(isset($config['system']['disablelargereceiveoffloading'])) {
231
		if (isset($options['encaps']['lro']))
232
			pfSense_interface_capabilities($interface, -IFCAP_LRO);
233
	} else {
234
		if (isset($options['caps']['lro']))
235
			pfSense_interface_capabilities($interface, IFCAP_LRO);
236
	}
237

    
238

    
239
	/* if the NIC supports polling *AND* it is enabled in the GUI */
240
	$polling = isset($config['system']['polling']);	
241
	if($polling && isset($options['caps']['polling']))
242
		pfSense_interface_capabilities($interface, IFCAP_POLLING);
243
	else
244
		pfSense_interface_capabilities($interface, -IFCAP_POLLING);
245

    
246
	return;
247
}
248

    
249
/****f* pfsense-utils/interface_supports_polling
250
 * NAME
251
 *   checks to see if an interface supports polling according to man polling
252
 * INPUTS
253
 *
254
 * RESULT
255
 *   true or false
256
 * NOTES
257
 *
258
 ******/
259
function interface_supports_polling($iface) {
260
	$opts = pfSense_get_interface_addresses($iface);
261
	if (is_array($opts) && isset($opts['caps']['polling']))
262
		return true;
263

    
264
	return false;
265
}
266

    
267
/****f* pfsense-utils/is_alias_inuse
268
 * NAME
269
 *   checks to see if an alias is currently in use by a rule
270
 * INPUTS
271
 *
272
 * RESULT
273
 *   true or false
274
 * NOTES
275
 *
276
 ******/
277
function is_alias_inuse($alias) {
278
	global $g, $config;
279

    
280
	if($alias == "") return false;
281
	/* loop through firewall rules looking for alias in use */
282
	if(is_array($config['filter']['rule']))
283
		foreach($config['filter']['rule'] as $rule) {
284
			if($rule['source']['address'])
285
				if($rule['source']['address'] == $alias)
286
					return true;
287
			if($rule['destination']['address'])
288
				if($rule['destination']['address'] == $alias)
289
					return true;
290
		}
291
	/* loop through nat rules looking for alias in use */
292
	if(is_array($config['nat']['rule']))
293
		foreach($config['nat']['rule'] as $rule) {
294
			if($rule['target'] && $rule['target'] == $alias)
295
				return true;
296
			if($rule['source']['address'] && $rule['source']['address'] == $alias)
297
				return true;
298
			if($rule['destination']['address'] && $rule['destination']['address'] == $alias)
299
				return true;
300
		}
301
	return false;
302
}
303

    
304
/****f* pfsense-utils/is_schedule_inuse
305
 * NAME
306
 *   checks to see if a schedule is currently in use by a rule
307
 * INPUTS
308
 *
309
 * RESULT
310
 *   true or false
311
 * NOTES
312
 *
313
 ******/
314
function is_schedule_inuse($schedule) {
315
	global $g, $config;
316

    
317
	if($schedule == "") return false;
318
	/* loop through firewall rules looking for schedule in use */
319
	if(is_array($config['filter']['rule']))
320
		foreach($config['filter']['rule'] as $rule) {
321
			if($rule['sched'] == $schedule)
322
				return true;
323
		}
324
	return false;
325
}
326

    
327
/****f* pfsense-utils/setup_polling_defaults
328
 * NAME
329
 *   sets up sysctls for polling
330
 * INPUTS
331
 *
332
 * RESULT
333
 *   null
334
 * NOTES
335
 *
336
 ******/
337
function setup_polling_defaults() {
338
	global $g, $config;
339
	if($config['system']['polling_each_burst'])
340
		mwexec("sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}");
341
	if($config['system']['polling_burst_max'])
342
		mwexec("sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}");
343
	if($config['system']['polling_user_frac'])
344
		mwexec("sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}");
345
}
346

    
347
/****f* pfsense-utils/setup_polling
348
 * NAME
349
 *   sets up polling
350
 * INPUTS
351
 *
352
 * RESULT
353
 *   null
354
 * NOTES
355
 *
356
 ******/
357
function setup_polling() {
358
	global $g, $config;
359

    
360
	setup_polling_defaults();
361

    
362
	$supported_ints = array('bge', 'dc', 'em', 'fwe', 'fwip', 'fxp', 'ixgb', 'ste', 'nge', 're', 'rl', 'sf', 'sis', 'ste', 'vge', 'vr', 'xl');
363

    
364
	/* if list */
365
	$iflist = get_configured_interface_list();
366

    
367
	foreach ($iflist as $ifent => $ifname) {
368
		$real_interface = convert_friendly_interface_to_real_interface_name($ifname);
369
		$ifdevice = substr($real_interface, 0, -1);
370
		if(!in_array($ifdevice, $supported_ints)) {
371
			continue;
372
        }
373
		if(isset($config['system']['polling'])) {
374
			mwexec("/sbin/ifconfig {$real_interface} polling");
375
			mwexec("/sbin/sysctl kern.polling.idle_poll=1");
376
		} else {
377
			mwexec("/sbin/ifconfig {$real_interface} -polling");
378
		}
379
	}
380
}
381

    
382
/****f* pfsense-utils/setup_microcode
383
 * NAME
384
 *   enumerates all interfaces and calls enable_hardware_offloading which
385
 *   enables a NIC's supported hardware features.
386
 * INPUTS
387
 *
388
 * RESULT
389
 *   null
390
 * NOTES
391
 *   This function only supports the fxp driver's loadable microcode.
392
 ******/
393
function setup_microcode() {
394

    
395
	/* if list */
396
	$ifs = get_interface_arr();
397

    
398
	foreach($ifs as $if)
399
		enable_hardware_offloading($if);
400
}
401

    
402
/****f* pfsense-utils/get_carp_status
403
 * NAME
404
 *   get_carp_status - Return whether CARP is enabled or disabled.
405
 * RESULT
406
 *   boolean	- true if CARP is enabled, false if otherwise.
407
 ******/
408
function get_carp_status() {
409
    /* grab the current status of carp */
410
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
411
    if(intval($status) == "0") return false;
412
    return true;
413
}
414

    
415
/*
416
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
417

    
418
 */
419
function convert_ip_to_network_format($ip, $subnet) {
420
	$ipsplit = split('[.]', $ip);
421
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
422
	return $string;
423
}
424

    
425
/*
426
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
427
 */
428
function get_carp_interface_status($carpinterface) {
429
	/* basically cache the contents of ifconfig statement
430
	to speed up this routine */
431
	global $carp_query;
432
	if($carp_query == "")
433
		$carp_query = split("\n", `/sbin/ifconfig $carpinterface | grep carp`);
434
	foreach($carp_query as $int) {
435
		if(stristr($int, "MASTER")) 
436
			return "MASTER";
437
		if(stristr($int, "BACKUP")) 
438
			return "BACKUP";
439
		if(stristr($int, "INIT")) 
440
			return "INIT";
441
	}
442
	return;
443
}
444

    
445
/*
446
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
447
 */
448
function get_pfsync_interface_status($pfsyncinterface) {
449
    $result = does_interface_exist($pfsyncinterface);
450
    if($result <> true) return;
451
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
452
    return $status;
453
}
454

    
455
/*
456
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
457
 */
458
function add_rule_to_anchor($anchor, $rule, $label) {
459
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
460
}
461

    
462
/*
463
 * remove_text_from_file
464
 * remove $text from file $file
465
 */
466
function remove_text_from_file($file, $text) {
467
	global $fd_log;
468
	if($fd_log)
469
		fwrite($fd_log, "Adding needed text items:\n");
470
	$filecontents = file_get_contents($file);
471
	$textTMP = str_replace($text, "", $filecontents);
472
	$text = $textTMP;
473
	if($fd_log)
474
		fwrite($fd_log, $text);
475
	$fd = fopen($file, "w");
476
	fwrite($fd, $text);
477
	fclose($fd);
478
}
479

    
480
/*
481
 * add_text_to_file($file, $text): adds $text to $file.
482
 * replaces the text if it already exists.
483
 */
484
function add_text_to_file($file, $text, $replace = false) {
485
	if(file_exists($file) and is_writable($file)) {
486
		$filecontents = file($file);
487
		$fout = fopen($file, "w");
488

    
489
		$filecontents = array_map('rtrim', $filecontents);
490
		array_push($filecontents, $text);
491
		if ($replace)
492
			$filecontents = array_unique($filecontents);
493

    
494
		$file_text = implode("\n", $filecontents);
495

    
496
		fwrite($fout, $file_text);
497
		fclose($fout);
498
		return true;
499
	} else {
500
		return false;
501
	}
502
}
503

    
504
/*
505
 *   after_sync_bump_adv_skew(): create skew values by 1S
506
 */
507
function after_sync_bump_adv_skew() {
508
	global $config, $g;
509
	$processed_skew = 1;
510
	$a_vip = &$config['virtualip']['vip'];
511
	foreach ($a_vip as $vipent) {
512
		if($vipent['advskew'] <> "") {
513
			$processed_skew = 1;
514
			$vipent['advskew'] = $vipent['advskew']+1;
515
		}
516
	}
517
	if($processed_skew == 1)
518
		write_config("After synch increase advertising skew");
519
}
520

    
521
/*
522
 * get_filename_from_url($url): converts a url to its filename.
523
 */
524
function get_filename_from_url($url) {
525
	return basename($url);
526
}
527

    
528
/*
529
 *   get_dir: return an array of $dir
530
 */
531
function get_dir($dir) {
532
	$dir_array = array();
533
	$d = dir($dir);
534
	while (false !== ($entry = $d->read())) {
535
		array_push($dir_array, $entry);
536
	}
537
	$d->close();
538
	return $dir_array;
539
}
540

    
541
/****f* pfsense-utils/WakeOnLan
542
 * NAME
543
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
544
 * RESULT
545
 *   true/false - true if the operation was successful
546
 ******/
547
function WakeOnLan($addr, $mac)
548
{
549
	$addr_byte = explode(':', $mac);
550
	$hw_addr = '';
551

    
552
	for ($a=0; $a < 6; $a++)
553
		$hw_addr .= chr(hexdec($addr_byte[$a]));
554

    
555
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
556

    
557
	for ($a = 1; $a <= 16; $a++)
558
		$msg .= $hw_addr;
559

    
560
	// send it to the broadcast address using UDP
561
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
562
	if ($s == false) {
563
		log_error("Error creating socket!");
564
		log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
565
	} else {
566
		// setting a broadcast option to socket:
567
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
568
		if($opt_ret < 0)
569
			log_error("setsockopt() failed, error: " . strerror($opt_ret));
570
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
571
		socket_close($s);
572
		log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}");
573
		return true;
574
	}
575

    
576
	return false;
577
}
578

    
579
/*
580
 * gather_altq_queue_stats():  gather altq queue stats and return an array that
581
 *                             is queuename|qlength|measured_packets
582
 *                             NOTE: this command takes 5 seconds to run
583
 */
584
function gather_altq_queue_stats($dont_return_root_queues) {
585
	exec("/sbin/pfctl -vvsq", $stats_array);
586
	$queue_stats = array();
587
	foreach ($stats_array as $stats_line) {
588
		$match_array = "";
589
		if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
590
			$queue_name = $match_array[1][0];
591
		if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
592
			$speed = $match_array[1][0];
593
		if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
594
			$borrows = $match_array[1][0];
595
		if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
596
			$suspends = $match_array[1][0];
597
		if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
598
			$drops = $match_array[1][0];
599
		if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
600
			$measured = $match_array[1][0];
601
			if($dont_return_root_queues == true)
602
				if(stristr($queue_name,"root_") == false)
603
					array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
604
		}
605
	}
606
	return $queue_stats;
607
}
608

    
609
/*
610
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
611
 *					 Useful for finding paths and stripping file extensions.
612
 */
613
function reverse_strrchr($haystack, $needle) {
614
	if (!is_string($haystack))
615
		return;
616
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
617
}
618

    
619
/*
620
 *  backup_config_section($section): returns as an xml file string of
621
 *                                   the configuration section
622
 */
623
function backup_config_section($section) {
624
	global $config;
625
	$new_section = &$config[$section];
626
	/* generate configuration XML */
627
	$xmlconfig = dump_xml_config($new_section, $section);
628
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
629
	return $xmlconfig;
630
}
631

    
632
/*
633
 *  restore_config_section($section, new_contents): restore a configuration section,
634
 *                                                  and write the configuration out
635
 *                                                  to disk/cf.
636
 */
637
function restore_config_section($section, $new_contents) {
638
	global $config, $g;
639
	conf_mount_rw();
640
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
641
	fwrite($fout, $new_contents);
642
	fclose($fout);
643
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
644
	if ($section_xml != -1)
645
		$config[$section] = &$section_xml;
646
	@unlink($g['tmp_path'] . "/tmpxml");
647
	if(file_exists("{$g['tmp_path']}/config.cache"))
648
		unlink("{$g['tmp_path']}/config.cache");
649
	write_config("Restored {$section} of config file (maybe from CARP partner)");
650
	conf_mount_ro();
651
	return;
652
}
653

    
654
/*
655
 *  merge_config_section($section, new_contents):   restore a configuration section,
656
 *                                                  and write the configuration out
657
 *                                                  to disk/cf.  But preserve the prior
658
 * 													structure if needed
659
 */
660
function merge_config_section($section, $new_contents) {
661
	global $config;
662
	conf_mount_rw();
663
	$fname = get_tmp_filename();
664
	$fout = fopen($fname, "w");
665
	fwrite($fout, $new_contents);
666
	fclose($fout);
667
	$section_xml = parse_xml_config($fname, $section);
668
	$config[$section] = $section_xml;
669
	unlink($fname);
670
	write_config("Restored {$section} of config file (maybe from CARP partner)");
671
	conf_mount_ro();
672
	return;
673
}
674

    
675
/*
676
 * http_post($server, $port, $url, $vars): does an http post to a web server
677
 *                                         posting the vars array.
678
 * written by nf@bigpond.net.au
679
 */
680
function http_post($server, $port, $url, $vars) {
681
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
682
	$urlencoded = "";
683
	while (list($key,$value) = each($vars))
684
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
685
	$urlencoded = substr($urlencoded,0,-1);
686
	$content_length = strlen($urlencoded);
687
	$headers = "POST $url HTTP/1.1
688
Accept: */*
689
Accept-Language: en-au
690
Content-Type: application/x-www-form-urlencoded
691
User-Agent: $user_agent
692
Host: $server
693
Connection: Keep-Alive
694
Cache-Control: no-cache
695
Content-Length: $content_length
696

    
697
";
698

    
699
	$errno = "";
700
	$errstr = "";
701
	$fp = fsockopen($server, $port, $errno, $errstr);
702
	if (!$fp) {
703
		return false;
704
	}
705

    
706
	fputs($fp, $headers);
707
	fputs($fp, $urlencoded);
708

    
709
	$ret = "";
710
	while (!feof($fp))
711
		$ret.= fgets($fp, 1024);
712
	fclose($fp);
713

    
714
	return $ret;
715
}
716

    
717
/*
718
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
719
 */
720
if (!function_exists('php_check_syntax')){
721
	global $g;
722
	function php_check_syntax($code_to_check, &$errormessage){
723
		return false;
724
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
725
		$code = $_POST['content'];
726
		$code = str_replace("<?php", "", $code);
727
		$code = str_replace("?>", "", $code);
728
		fwrite($fout, "<?php\n\n");
729
		fwrite($fout, $code_to_check);
730
		fwrite($fout, "\n\n?>\n");
731
		fclose($fout);
732
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
733
		$output = exec_command($command);
734
		if (stristr($output, "Errors parsing") == false) {
735
			echo "false\n";
736
			$errormessage = '';
737
			return(false);
738
		} else {
739
			$errormessage = $output;
740
			return(true);
741
		}
742
	}
743
}
744

    
745
/*
746
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
747
 */
748
if (!function_exists('php_check_syntax')){
749
	function php_check_syntax($code_to_check, &$errormessage){
750
		return false;
751
		$command = "/usr/local/bin/php -l " . $code_to_check;
752
		$output = exec_command($command);
753
		if (stristr($output, "Errors parsing") == false) {
754
			echo "false\n";
755
			$errormessage = '';
756
			return(false);
757
		} else {
758
			$errormessage = $output;
759
			return(true);
760
		}
761
	}
762
}
763

    
764
/*
765
 * rmdir_recursive($path,$follow_links=false)
766
 * Recursively remove a directory tree (rm -rf path)
767
 * This is for directories _only_
768
 */
769
function rmdir_recursive($path,$follow_links=false) {
770
	$to_do = glob($path);
771
	if(!is_array($to_do)) $to_do = array($to_do);
772
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
773
		if(file_exists($workingdir)) {
774
			if(is_dir($workingdir)) {
775
				$dir = opendir($workingdir);
776
				while ($entry = readdir($dir)) {
777
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
778
						unlink("$workingdir/$entry");
779
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
780
						rmdir_recursive("$workingdir/$entry");
781
				}
782
				closedir($dir);
783
				rmdir($workingdir);
784
			} elseif (is_file($workingdir)) {
785
				unlink($workingdir);
786
			}
787
               	}
788
	}
789
	return;
790
}
791

    
792
/*
793
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
794
 */
795
function call_pfsense_method($method, $params, $timeout = 0) {
796
	global $g, $config;
797

    
798
	$ip = gethostbyname($g['product_website']);
799
	if($ip == $g['product_website'])
800
		return false;
801

    
802
	$xmlrpc_base_url = isset($config['system']['altpkgrepo']['enable']) ? $config['system']['altpkgrepo']['xmlrpcbaseurl'] : $g['xmlrpcbaseurl'];
803
	$xmlrpc_path = $g['xmlrpcpath'];
804
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
805
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
806
	// If the ALT PKG Repo has a username/password set, use it.
807
	if($config['system']['altpkgrepo']['username'] && 
808
	   $config['system']['altpkgrepo']['password']) {
809
		$username = $config['system']['altpkgrepo']['username'];
810
		$password = $config['system']['altpkgrepo']['password'];
811
		$cli->setCredentials($username, $password);
812
	}
813
	$resp = $cli->send($msg, $timeout);
814
	if(!$resp) {
815
		log_error("XMLRPC communication error: " . $cli->errstr);
816
		return false;
817
	} elseif($resp->faultCode()) {
818
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
819
		return false;
820
	} else {
821
		return XML_RPC_Decode($resp->value());
822
	}
823
}
824

    
825
/*
826
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
827
 */
828
function check_firmware_version($tocheck = "all", $return_php = true) {
829
	global $g, $config;
830
	$ip = gethostbyname($g['product_website']);
831
	if($ip == $g['product_website'])
832
		return false;
833
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
834
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
835
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
836
		"platform" => trim(file_get_contents('/etc/platform'))
837
		);
838
	if($tocheck == "all") {
839
		$params = $rawparams;
840
	} else {
841
		foreach($tocheck as $check) {
842
			$params['check'] = $rawparams['check'];
843
			$params['platform'] = $rawparams['platform'];
844
		}
845
	}
846
	if($config['system']['firmware']['branch']) {
847
		$params['branch'] = $config['system']['firmware']['branch'];
848
	}
849
	if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) {
850
		return false;
851
	} else {
852
		$versions["current"] = $params;
853
	}
854
	return $versions;
855
}
856

    
857
function get_disk_info() {
858
	$diskout = "";
859
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
860
	return explode(' ', $diskout[0]);
861
	// $size, $used, $avail, $cap
862
}
863

    
864
/****f* pfsense-utils/strncpy
865
 * NAME
866
 *   strncpy - copy strings
867
 * INPUTS
868
 *   &$dst, $src, $length
869
 * RESULT
870
 *   none
871
 ******/
872
function strncpy(&$dst, $src, $length) {
873
	if (strlen($src) > $length) {
874
		$dst = substr($src, 0, $length);
875
	} else {
876
		$dst = $src;
877
	}
878
}
879

    
880
/****f* pfsense-utils/reload_interfaces_sync
881
 * NAME
882
 *   reload_interfaces - reload all interfaces
883
 * INPUTS
884
 *   none
885
 * RESULT
886
 *   none
887
 ******/
888
function reload_interfaces_sync() {
889
	global $config, $g;
890

    
891
	/* XXX: Use locks?! */
892
	if (file_exists("{$g['tmp_path']}/reloading_all")) {
893
		log_error("WARNING: Recursive call to interfaces sync!");
894
		return;
895
	}
896
	touch("{$g['tmp_path']}/reloading_all");
897

    
898
	if($g['debug'])
899
		log_error("reload_interfaces_sync() is starting.");
900

    
901
	/* parse config.xml again */
902
	$config = parse_config(true);
903

    
904
	/* enable routing */
905
	system_routing_enable();
906
	if($g['debug'])
907
		log_error("Enabling system routing");
908

    
909
	if($g['debug'])
910
		log_error("Cleaning up Interfaces");
911

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

    
915
	/* remove reloading_all trigger */
916
	if($g['debug'])
917
		log_error("Removing {$g['tmp_path']}/reloading_all");
918

    
919
	/* start devd back up */
920
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
921
}
922

    
923
/****f* pfsense-utils/reload_all
924
 * NAME
925
 *   reload_all - triggers a reload of all settings
926
 *   * INPUTS
927
 *   none
928
 * RESULT
929
 *   none
930
 ******/
931
function reload_all() {
932
	global $g;
933
	touch("{$g['tmp_path']}/reload_all");
934
}
935

    
936
/****f* pfsense-utils/reload_interfaces
937
 * NAME
938
 *   reload_interfaces - triggers a reload of all interfaces
939
 * INPUTS
940
 *   none
941
 * RESULT
942
 *   none
943
 ******/
944
function reload_interfaces() {
945
	global $g;
946
	touch("{$g['tmp_path']}/reload_interfaces");
947
}
948

    
949
/****f* pfsense-utils/reload_all_sync
950
 * NAME
951
 *   reload_all - reload all settings
952
 *   * INPUTS
953
 *   none
954
 * RESULT
955
 *   none
956
 ******/
957
function reload_all_sync() {
958
	global $config, $g;
959

    
960
	$g['booting'] = false;
961

    
962
	/* XXX: Use locks?! */
963
        if (file_exists("{$g['tmp_path']}/reloading_all")) {
964
                log_error("WARNING: Recursive call to reload all sync!");
965
                return;
966
        }
967
	touch("{$g['tmp_path']}/reloading_all");
968

    
969
	/* parse config.xml again */
970
	$config = parse_config(true);
971

    
972
	/* set up our timezone */
973
	system_timezone_configure();
974

    
975
	/* set up our hostname */
976
	system_hostname_configure();
977

    
978
	/* make hosts file */
979
	system_hosts_generate();
980

    
981
	/* generate resolv.conf */
982
	system_resolvconf_generate();
983

    
984
	/* enable routing */
985
	system_routing_enable();
986

    
987
	/* set up interfaces */
988
	interfaces_configure();
989

    
990
	/* start dyndns service */
991
	services_dyndns_configure();
992

    
993
	/* configure cron service */
994
	configure_cron();
995

    
996
	/* start the NTP client */
997
	system_ntp_configure();
998

    
999
	/* sync pw database */
1000
	conf_mount_rw();
1001
	unlink_if_exists("/etc/spwd.db.tmp");
1002
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
1003
	conf_mount_ro();
1004

    
1005
	/* restart sshd */
1006
	@touch("{$g['tmp_path']}/start_sshd");
1007

    
1008
	/* restart webConfigurator if needed */
1009
	touch("{$g['tmp_path']}/restart_webgui");
1010

    
1011
	mwexec("/bin/rm {$g['tmp_path']}/reload*");
1012
}
1013

    
1014
function auto_login() {
1015
	global $config;
1016

    
1017
	if(isset($config['system']['disableconsolemenu']))
1018
		$status = false;
1019
	else
1020
		$status = true;
1021

    
1022
	$gettytab = file_get_contents("/etc/gettytab");
1023
	$getty_split = split("\n", $gettytab);
1024
	conf_mount_rw();
1025
	$fd = false;
1026
	$tries = 0;
1027
	while (!$fd && $tries < 100) {
1028
		$fd = fopen("/etc/gettytab", "w");
1029
		$tries++;
1030
		
1031
	}
1032
	if (!$fd) {
1033
		conf_mount_ro();
1034
		log_error("Enabling auto login was not possible.");
1035
		return;
1036
	}
1037
	foreach($getty_split as $gs) {
1038
		if(stristr($gs, ":ht:np:sp#115200") ) {
1039
			if($status == true) {
1040
				fwrite($fd, "	:ht:np:sp#115200:al=root:\n");
1041
			} else {
1042
				fwrite($fd, "	:ht:np:sp#115200:\n");
1043
			}
1044
		} else {
1045
			fwrite($fd, "{$gs}\n");
1046
		}
1047
	}
1048
	fclose($fd);
1049
	conf_mount_ro();
1050
}
1051

    
1052
function setup_serial_port() {
1053
	global $g, $config;
1054
	conf_mount_rw();
1055
	/* serial console - write out /boot.config */
1056
	if(file_exists("/boot.config"))
1057
		$boot_config = file_get_contents("/boot.config");
1058
	else
1059
		$boot_config = "";
1060

    
1061
	if($g['platform'] <> "cdrom") {
1062
		$boot_config_split = split("\n", $boot_config);
1063
		$fd = fopen("/boot.config","w");
1064
		if($fd) {
1065
			foreach($boot_config_split as $bcs) {
1066
				if(stristr($bcs, "-D")) {
1067
					/* DONT WRITE OUT, WE'LL DO IT LATER */
1068
				} else {
1069
					if($bcs <> "")
1070
						fwrite($fd, "{$bcs}\n");
1071
				}
1072
			}
1073
			if(isset($config['system']['enableserial'])) {
1074
				fwrite($fd, "-D");
1075
			}
1076
			fclose($fd);
1077
		}
1078
		/* serial console - write out /boot/loader.conf */
1079
		$boot_config = file_get_contents("/boot/loader.conf");
1080
		$boot_config_split = split("\n", $boot_config);
1081
		$fd = fopen("/boot/loader.conf","w");
1082
		if($fd) {
1083
			foreach($boot_config_split as $bcs) {
1084
				if(stristr($bcs, "console")) {
1085
					/* DONT WRITE OUT, WE'LL DO IT LATER */
1086
				} else {
1087
					if($bcs <> "")
1088
						fwrite($fd, "{$bcs}\n");
1089
				}
1090
			}
1091
			if(isset($config['system']['enableserial'])) {
1092
				fwrite($fd, "console=\"comconsole\"\n");
1093
			}
1094
			fclose($fd);
1095
		}
1096
	}
1097
	$ttys = file_get_contents("/etc/ttys");
1098
	$ttys_split = split("\n", $ttys);
1099
	$fd = fopen("/etc/ttys", "w");
1100
	foreach($ttys_split as $tty) {
1101
		if(stristr($tty, "ttyd0") or stristr($tty, "ttyu0")) {
1102
			if(isset($config['system']['enableserial'])) {
1103
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	on	secure\n");
1104
			} else {
1105
				fwrite($fd, "ttyu0	\"/usr/libexec/getty bootupcli\"	cons25	off	secure\n");
1106
			}
1107
		} else {
1108
			fwrite($fd, $tty . "\n");
1109
		}
1110
	}
1111
	fclose($fd);
1112
	auto_login();
1113

    
1114
	conf_mount_ro();
1115
	return;
1116
}
1117

    
1118
function print_value_list($list, $count = 10, $separator = ",") {
1119
	$list = implode($separator, array_slice($list, 0, $count));
1120
	if(count($list) < $count) {
1121
		$list .= ".";
1122
	} else {
1123
		$list .= "...";
1124
	}
1125
	return $list;
1126
}
1127

    
1128
/* DHCP enabled on any interfaces? */
1129
function is_dhcp_server_enabled() 
1130
{
1131
	global $config;
1132

    
1133
	$dhcpdenable = false;
1134
	
1135
	if (!is_array($config['dhcpd']))
1136
		return false;
1137

    
1138
	$Iflist = get_configured_interface_list();
1139

    
1140
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1141
		if (isset($dhcpifconf['enable']) && isset($Iflist[$dhcpif])) {
1142
			$dhcpdenable = true;
1143
			break;
1144
		}
1145
	}
1146

    
1147
	return $dhcpdenable;
1148
}
1149

    
1150
function convert_seconds_to_hms($sec){
1151
	$min=$hrs=0;
1152
	if ($sec != 0){
1153
		$min = floor($sec/60);
1154
		$sec %= 60;
1155
	}
1156
	if ($min != 0){
1157
		$hrs = floor($min/60);
1158
		$min %= 60;
1159
	}
1160
	if ($sec < 10)
1161
		$sec = "0".$sec;
1162
	if ($min < 10)
1163
		$min = "0".$min;
1164
	if ($hrs < 10)
1165
		$hrs = "0".$hrs;
1166
	$result = $hrs.":".$min.":".$sec;
1167
	return $result;
1168
}
1169

    
1170
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1171

    
1172
function get_ppp_uptime($port){
1173
	if (file_exists("/conf/{$port}.log")){
1174
    	$saved_time = file_get_contents("/conf/{$port}.log");
1175
    	$uptime_data = explode("\n",$saved_time);
1176
		$sec=0;
1177
		foreach($uptime_data as $upt) {
1178
			$sec += substr($upt, 1 + strpos($upt, " "));
1179
 		}
1180
		return convert_seconds_to_hms($sec);
1181
	} else {
1182
		$total_time = "No history data found!";
1183
		return $total_time;
1184
	}
1185
}
1186

    
1187
//returns interface information
1188
function get_interface_info($ifdescr) {
1189
	global $config, $g;
1190

    
1191
	$ifinfo = array();
1192
	if (empty($config['interfaces'][$ifdescr]))
1193
		return;
1194
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1195
	$ifinfo['if'] = get_real_interface($ifdescr);
1196

    
1197
	$chkif = $ifinfo['if'];
1198
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1199
	$ifinfo['status'] = $ifinfotmp['status'];
1200
	if (empty($ifinfo['status']))
1201
                $ifinfo['status'] = "down";
1202
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1203
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1204
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1205
	if (isset($ifinfotmp['link0']))
1206
		$link0 = "down";
1207
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1208
        $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1209
        $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1210
        $ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1211
        $ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1212
        $ifinfo['collisions'] = $ifinfotmp['collisions'];
1213

    
1214
	/* Use pfctl for non wrapping 64 bit counters */
1215
	/* Pass */
1216
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1217
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1218
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1219
	$in4_pass = $pf_in4_pass[5];
1220
	$out4_pass = $pf_out4_pass[5];
1221
	$in4_pass_packets = $pf_in4_pass[3];
1222
	$out4_pass_packets = $pf_out4_pass[3];
1223
	$ifinfo['inbytespass'] = $in4_pass;
1224
	$ifinfo['outbytespass'] = $out4_pass;
1225
	$ifinfo['inpktspass'] = $in4_pass_packets;
1226
	$ifinfo['outpktspass'] = $out4_pass_packets;
1227

    
1228
	/* Block */
1229
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1230
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1231
	$in4_block = $pf_in4_block[5];
1232
	$out4_block = $pf_out4_block[5];
1233
	$in4_block_packets = $pf_in4_block[3];
1234
	$out4_block_packets = $pf_out4_block[3];
1235
	$ifinfo['inbytesblock'] = $in4_block;
1236
	$ifinfo['outbytesblock'] = $out4_block;
1237
	$ifinfo['inpktsblock'] = $in4_block_packets;
1238
	$ifinfo['outpktsblock'] = $out4_block_packets;
1239

    
1240
	$ifinfo['inbytes'] = $in4_pass + $in4_block;
1241
	$ifinfo['outbytes'] = $out4_pass + $out4_block;
1242
	$ifinfo['inpkts'] = $in4_pass_packets + $in4_block_packets;
1243
	$ifinfo['outpkts'] = $in4_pass_packets + $out4_block_packets;
1244
		
1245
	$ifconfiginfo = "";
1246
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1247
	switch ($link_type) {
1248
	 /* DHCP? -> see if dhclient is up */
1249
	case "dhcp":
1250
	case "carpdev-dhcp":
1251
		/* see if dhclient is up */
1252
		if (find_dhclient_process($ifinfo['if']) <> "")
1253
			$ifinfo['dhcplink'] = "up";
1254
		else
1255
			$ifinfo['dhcplink'] = "down";
1256

    
1257
		break;
1258
	/* PPPoE interface? -> get status from virtual interface */
1259
	case "pppoe":
1260
		if ($ifinfo['status'] == "up" && !isset($link0))
1261
			/* get PPPoE link status for dial on demand */
1262
			$ifinfo['pppoelink'] = "up";
1263
		else
1264
			$ifinfo['pppoelink'] = "down";
1265

    
1266
		break;
1267
	/* PPTP interface? -> get status from virtual interface */
1268
	case "pptp":
1269
		if ($ifinfo['status'] == "up" && !isset($link0))
1270
			/* get PPTP link status for dial on demand */
1271
			$ifinfo['pptplink'] = "up";
1272
		else
1273
			$ifinfo['pptplink'] = "down";
1274
		break;
1275
	/* PPP interface? -> get uptime for this session and cumulative uptime from the persistant log file in conf */
1276
	case "ppp":
1277
		if ($ifinfo['status'] == "up")
1278
			$ifinfo['ppplink'] = "up";
1279
		else
1280
			$ifinfo['ppplink'] = "down" ;
1281

    
1282
		if (empty($ifinfo['status']))
1283
			$ifinfo['status'] = "down";
1284
			
1285
		if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1286
			foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1287
				if ($config['interfaces'][$ifdescr]['if'] == $ppp['if'])
1288
					break;
1289
			}
1290
		}
1291
		$dev = $ppp['ports'];
1292
		if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev))
1293
			break;
1294
		if (!file_exists($dev)) {
1295
			$ifinfo['nodevice'] = 1;
1296
			$ifinfo['pppinfo'] = $dev . " device not present! Is the modem attached to the system?";	
1297
		}
1298
		// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1299
		if (isset($ppp['uptime']))
1300
			$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1301
		break;
1302
	default:
1303
		break;
1304
	}
1305
	
1306
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1307
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1308
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1309
	}
1310
	
1311
	if ($ifinfo['status'] == "up") {
1312
		/* try to determine media with ifconfig */
1313
		unset($ifconfiginfo);
1314
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1315
		$wifconfiginfo = array();
1316
		if(is_interface_wireless($ifdescr)) {
1317
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1318
			array_shift($wifconfiginfo);
1319
		}
1320
		$matches = "";
1321
		foreach ($ifconfiginfo as $ici) {
1322

    
1323
			/* don't list media/speed for wireless cards, as it always
1324
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1325
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1326
				$ifinfo['media'] = $matches[1];
1327
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1328
				$ifinfo['media'] = $matches[1];
1329
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1330
				$ifinfo['media'] = $matches[1];
1331
			}
1332

    
1333
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1334
				if ($matches[1] != "active")
1335
					$ifinfo['status'] = $matches[1];
1336
				if($ifinfo['status'] == "running")
1337
					$ifinfo['status'] = "up";
1338
			}
1339
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1340
				$ifinfo['channel'] = $matches[1];
1341
			}
1342
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1343
				if ($matches[1][0] == '"')
1344
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1345
				else
1346
					$ifinfo['ssid'] = $matches[1];
1347
			}
1348
		}
1349
		foreach($wifconfiginfo as $ici) {
1350
			$elements = preg_split("/[ ]+/i", $ici);
1351
			if ($elements[0] != "") {
1352
				$ifinfo['bssid'] = $elements[0];
1353
			}
1354
			if ($elements[3] != "") {
1355
				$ifinfo['rate'] = $elements[3];
1356
			}
1357
			if ($elements[4] != "") {
1358
				$ifinfo['rssi'] = $elements[4];
1359
			}
1360

    
1361
		}
1362
		/* lookup the gateway */
1363
		if (interface_has_gateway($ifdescr)) 
1364
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1365
	}
1366

    
1367
	$bridge = "";
1368
	$bridge = link_interface_to_bridge($ifdescr);
1369
	if($bridge) {
1370
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1371
		if(stristr($bridge_text, "blocking") <> false) {
1372
			$ifinfo['bridge'] = "<b><font color='red'>blocking</font></b> - check for ethernet loops";
1373
			$ifinfo['bridgeint'] = $bridge;
1374
		} else if(stristr($bridge_text, "learning") <> false) {
1375
			$ifinfo['bridge'] = "learning";
1376
			$ifinfo['bridgeint'] = $bridge;
1377
		} else if(stristr($bridge_text, "forwarding") <> false) {
1378
			$ifinfo['bridge'] = "forwarding";
1379
			$ifinfo['bridgeint'] = $bridge;
1380
		}
1381
	}
1382

    
1383
	return $ifinfo;
1384
}
1385

    
1386
//returns cpu speed of processor. Good for determining capabilities of machine
1387
function get_cpu_speed() {
1388
	 return exec("sysctl hw.clockrate | awk '{ print $2 }'");
1389
}
1390

    
1391
function add_hostname_to_watch($hostname) {
1392
	if(!is_dir("/var/db/dnscache")) {
1393
		mkdir("/var/db/dnscache");
1394
	}
1395
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1396
		$domrecords = array();
1397
		$domips = array();
1398
		exec("host -t A $hostname", $domrecords, $rethost);
1399
		if($rethost == 0) {
1400
			foreach($domrecords as $domr) {
1401
				$doml = explode(" ", $domr);
1402
				$domip = $doml[3];
1403
				/* fill array with domain ip addresses */
1404
				if(is_ipaddr($domip)) {
1405
					$domips[] = $domip;
1406
				}
1407
			}
1408
		}
1409
		sort($domips);
1410
		$contents = "";
1411
		if(! empty($domips)) {
1412
			foreach($domips as $ip) {
1413
				$contents .= "$ip\n";
1414
			}
1415
		}
1416
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1417
	}
1418
}
1419

    
1420
function is_fqdn($fqdn) {
1421
	$hostname = false;
1422
	if(preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1423
		$hostname = true;
1424
	}
1425
	if(preg_match("/\.\./", $fqdn)) {
1426
		$hostname = false;
1427
	}
1428
	if(preg_match("/^\./i", $fqdn)) { 
1429
		$hostname = false;
1430
	}
1431
	if(preg_match("/\//i", $fqdn)) {
1432
		$hostname = false;
1433
	}
1434
	return($hostname);
1435
}
1436

    
1437
function pfsense_default_state_size() {
1438
  /* get system memory amount */
1439
  $memory = get_memory();
1440
  $avail = $memory[0];
1441
  /* Be cautious and only allocate 10% of system memory to the state table */
1442
  $max_states = (int) ($avail/10)*1000;
1443
  return $max_states;
1444
}
1445

    
1446
function pfsense_default_table_entries_size() {
1447
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1448
	return $current;
1449
}
1450

    
1451
/* Compare the current hostname DNS to the DNS cache we made
1452
 * if it has changed we return the old records
1453
 * if no change we return true */
1454
function compare_hostname_to_dnscache($hostname) {
1455
	if(!is_dir("/var/db/dnscache")) {
1456
		mkdir("/var/db/dnscache");
1457
	}
1458
	$hostname = trim($hostname);
1459
	if(is_readable("/var/db/dnscache/{$hostname}")) {
1460
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1461
	} else {
1462
		$oldcontents = "";
1463
	}
1464
	if((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1465
		$domrecords = array();
1466
		$domips = array();
1467
		exec("host -t A $hostname", $domrecords, $rethost);
1468
		if($rethost == 0) {
1469
			foreach($domrecords as $domr) {
1470
				$doml = explode(" ", $domr);
1471
				$domip = $doml[3];
1472
				/* fill array with domain ip addresses */
1473
				if(is_ipaddr($domip)) {
1474
					$domips[] = $domip;
1475
				}
1476
			}
1477
		}
1478
		sort($domips);
1479
		$contents = "";
1480
		if(! empty($domips)) {
1481
			foreach($domips as $ip) {
1482
				$contents .= "$ip\n";
1483
			}
1484
		}
1485
	}
1486

    
1487
	if(trim($oldcontents) != trim($contents)) {
1488
		if($g['debug']) {
1489
			log_error("DNSCACHE: Found old IP {$oldcontents} and new IP {$contents}");
1490
		}
1491
		return ($oldcontents);
1492
	} else {
1493
		return false;
1494
	}
1495
}
1496

    
1497
/*
1498
 * load_glxsb() - Load the glxsb crypto module if enabled in config.
1499
 */
1500
function load_glxsb() {
1501
	global $config, $g;
1502
	$is_loaded = `/sbin/kldstat | /usr/bin/grep -c glxsb`;
1503
	if (isset($config['system']['glxsb_enable']) && ($is_loaded == 0)) {
1504
		mwexec("/sbin/kldload glxsb");
1505
	}
1506
}
1507

    
1508
/****f* pfsense-utils/isvm
1509
 * NAME
1510
 *   isvm
1511
 * INPUTS
1512
 *	 none
1513
 * RESULT
1514
 *   returns true if machine is running under a virtual environment
1515
 ******/
1516
function isvm() {
1517
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86");
1518
	$bios_vendor = strtolower(`/bin/kenv | /usr/bin/awk -F= '/smbios.bios.vendor/ {print $2}'`);
1519
	if(in_array($bios_vendor, $virtualenvs)) 
1520
		return true;
1521
	else
1522
		return false;
1523
}
1524

    
1525
function get_freebsd_version() {
1526
	$version = trim(`/usr/bin/uname -r | /usr/bin/cut  -d'.' -f1`);
1527
	return $version;
1528
}
1529

    
1530
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body') {
1531
        global $ch, $fout, $file_size, $downloaded;
1532
        $file_size  = 1;
1533
        $downloaded = 1;
1534
        /* open destination file */
1535
        $fout = fopen($destination_file, "wb");
1536

    
1537
        /*
1538
         *      Originally by Author: Keyvan Minoukadeh
1539
         *      Modified by Scott Ullrich to return Content-Length size
1540
         */
1541

    
1542
        $ch = curl_init();
1543
        curl_setopt($ch, CURLOPT_URL, $url_file);
1544
        curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1545
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1546
        /* Don't verify SSL peers since we don't have the certificates to do so. */
1547
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
1548
        curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1549
        curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1550
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, '5');
1551
        curl_setopt($ch, CURLOPT_TIMEOUT, 0);
1552

    
1553
        curl_exec($ch);
1554
        $http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1555
        if($fout)
1556
                fclose($fout);
1557
        curl_close($ch);
1558
        return ($http_code == 200) ? true : $http_code;
1559
}
1560

    
1561
function read_header($ch, $string) {
1562
        global $file_size, $fout;
1563
        $length = strlen($string);
1564
        $regs = "";
1565
        ereg("(Content-Length:) (.*)", $string, $regs);
1566
        if($regs[2] <> "") {
1567
                $file_size = intval($regs[2]);
1568
        }
1569
        ob_flush();
1570
        return $length;
1571
}
1572

    
1573
function read_body($ch, $string) {
1574
        global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen;
1575
        $length = strlen($string);
1576
        $downloaded += intval($length);
1577
        $downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1578
        $downloadProgress = 100 - $downloadProgress;
1579
        if($lastseen <> $downloadProgress and $downloadProgress < 101) {
1580
                if($sendto == "status") {
1581
                        $tostatus = $static_status . $downloadProgress . "%";
1582
                        update_status($tostatus);
1583
                } else {
1584
                        $tooutput = $static_output . $downloadProgress . "%";
1585
                        update_output_window($tooutput);
1586
                }
1587
                update_progress_bar($downloadProgress);
1588
                $lastseen = $downloadProgress;
1589
        }
1590
        if($fout)
1591
                fwrite($fout, $string);
1592
        ob_flush();
1593
        return $length;
1594
}
1595

    
1596
/*
1597
 *   update_output_window: update bottom textarea dynamically.
1598
 */
1599
function update_output_window($text) {
1600
        global $pkg_interface;
1601
        $log = ereg_replace("\n", "\\n", $text);
1602
        if($pkg_interface == "console") {
1603
                /* too chatty */
1604
        } else {
1605
                echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
1606
        }
1607
        /* ensure that contents are written out */
1608
        ob_flush();
1609
}
1610

    
1611
/*
1612
 *   update_output_window: update top textarea dynamically.
1613
 */
1614
function update_status($status) {
1615
        global $pkg_interface;
1616
        if($pkg_interface == "console") {
1617
                echo $status . "\n";
1618
        } else {
1619
                echo "\n<script type=\"text/javascript\">this.document.forms[0].status.value=\"" . $status . "\";</script>";
1620
        }
1621
        /* ensure that contents are written out */
1622
        ob_flush();
1623
}
1624

    
1625
/*
1626
 * update_progress_bar($percent): updates the javascript driven progress bar.
1627
 */
1628
function update_progress_bar($percent) {
1629
        global $pkg_interface;
1630
        if($percent > 100) $percent = 1;
1631
        if($pkg_interface <> "console") {
1632
                echo "\n<script type=\"text/javascript\" language=\"javascript\">";
1633
                echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1634
                echo "\n</script>";
1635
        } else {
1636
                echo " {$percent}%";
1637
        }
1638
}
1639

    
1640
/* 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. */
1641
if(!function_exists("split")) {
1642
	function split($seperator, $haystack, $limit = null) {
1643
		return preg_split($seperator, $haystack, $limit);
1644
	}
1645
}
1646

    
1647
function update_alias_names_upon_change($section, $subsection, $fielda, $fieldb, $new_alias_name, $origname) {
1648
	global $g, $config, $pconfig, $debug;
1649
	if(!$origname) 
1650
		return;
1651

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

    
1655
	if($fieldb) {
1656
		if($debug) fwrite($fd, "fieldb exists\n");
1657
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1658
			if($debug) fwrite($fd, "$i\n");
1659
			if($config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] == $origname) {
1660
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1661
				$config["$section"]["$subsection"][$i]["$fielda"]["$fieldb"] = $new_alias_name;
1662
			}
1663
		}	
1664
	} else {
1665
		if($debug) fwrite($fd, "fieldb does not exist\n");
1666
		for ($i = 0; isset($config["$section"]["$subsection"][$i]["$fielda"]); $i++) {
1667
			if($config["$section"]["$subsection"][$i]["$fielda"] == $origname) {
1668
				$config["$section"]["$subsection"][$i]["$fielda"] = $new_alias_name;
1669
				if($debug) fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1670
			}
1671
		}
1672
	}
1673

    
1674
	if($debug) fclose($fd);
1675

    
1676
}
1677

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

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

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

    
1767
function version_compare_dates($a, $b) {
1768
	$a_time = strtotime($a);
1769
	$b_time = strtotime($b);
1770

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

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

    
1842
		// First try to compare the numeric parts of the version string.
1843
		$v = version_compare_numeric($cur_num, $rem_num);
1844

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

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

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

    
1868
		// Try to fetch the URL supplied
1869
		conf_mount_rw();
1870
		unlink_if_exists($urltable_filename . ".tmp");
1871
		// 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.
1872
		mwexec("/usr/bin/fetch -q -o " . escapeshellarg($urltable_filename . ".tmp") . " " . escapeshellarg($url));
1873
		// Remove comments. Might need some grep-fu to only allow lines that look like IPs/subnets
1874
		mwexec("/usr/bin/grep -v '^#' " . escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
1875
		unlink_if_exists($urltable_filename . ".tmp");
1876
		conf_mount_ro();
1877
		if (filesize($urltable_filename)) {
1878
			return true;
1879
		} else {
1880
			// If it's unfetchable or an empty file, bail
1881
			return false;
1882
		}
1883
	} else {
1884
		// File exists, and it doesn't need updated.
1885
		return -1;
1886
	}
1887
}
1888
function get_real_slice_from_glabel($label) {
1889
	$label = escapeshellarg($label);
1890
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
1891
}
1892
function nanobsd_get_boot_slice() {
1893
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
1894
}
1895
function nanobsd_get_boot_drive() {
1896
	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`);
1897
}
1898
function nanobsd_get_active_slice() {
1899
	$boot_drive = nanobsd_get_boot_drive();
1900
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
1901

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

    
1913
	if ($BOOTFLASH == $ACTIVE_SLICE) {
1914
		$slice = $TOFLASH;
1915
	} else {
1916
		$slice = $BOOTFLASH;
1917
	}
1918

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

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

    
1973
	exec("/bin/mkdir {$tmppath}");
1974
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
1975
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
1976
	exec("/bin/cp /etc/fstab {$fstabpath}");
1977

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

    
1993
	return $status;
1994
}
1995
function nanobsd_detect_slice_info() {
1996
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
1997
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
1998
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
1999

    
2000
	$BOOT_DEVICE=nanobsd_get_boot_slice();
2001
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
2002
	$BOOT_DRIVE=nanobsd_get_boot_drive();
2003
	$ACTIVE_SLICE=nanobsd_get_active_slice();
2004

    
2005
	// Detect which slice is active and set information.
2006
	if(strstr($REAL_BOOT_DEVICE, "s1")) {
2007
		$SLICE="2";
2008
		$OLDSLICE="1";
2009
		$GLABEL_SLICE="pfsense1";
2010
		$UFS_ID="1";
2011
		$OLD_UFS_ID="0";
2012

    
2013
	} else {
2014
		$SLICE="1";
2015
		$OLDSLICE="2";
2016
		$GLABEL_SLICE="pfsense0";
2017
		$UFS_ID="0";
2018
		$OLD_UFS_ID="1";
2019
	}
2020
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2021
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2022
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2023
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2024
}
2025

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

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

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

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

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

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

    
2197
?>
(30-30/52)