Project

General

Profile

Download (89.2 KB) Statistics
| Branch: | Tag: | Revision:
1 14227c51 Scott Ullrich
<?php
2 3076becf Scott Ullrich
/****h* pfSense/pfsense-utils
3 23a193da Phil Davis
	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
	Copyright (C) 2004-2007 Scott Ullrich (sullrich@gmail.com)
11
	All rights reserved.
12
	Redistribution and use in source and binary forms, with or without
13
	modification, are permitted provided that the following conditions are met:
14
15
	1. Redistributions of source code must retain the above copyright notice,
16
	   this list of conditions and the following disclaimer.
17
18
	2. Redistributions in binary form must reproduce the above copyright
19
	   notice, this list of conditions and the following disclaimer in the
20
	   documentation and/or other materials provided with the distribution.
21
22
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
23
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
24
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
26
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31
	POSSIBILITY OF SUCH DAMAGE.
32
*/
33 3076becf Scott Ullrich
34 523855b0 Scott Ullrich
/*
35 971de1f9 Renato Botelho
	pfSense_BUILDER_BINARIES:	/sbin/ifconfig	/sbin/pfctl	/usr/local/bin/php /usr/bin/netstat
36 523855b0 Scott Ullrich
	pfSense_BUILDER_BINARIES:	/bin/df	/usr/bin/grep	/usr/bin/awk	/bin/rm	/usr/sbin/pwd_mkdb	/usr/bin/host
37
	pfSense_BUILDER_BINARIES:	/sbin/kldload
38
	pfSense_MODULE:	utils
39
*/
40
41 0397013a Scott Ullrich
/****f* pfsense-utils/have_natpfruleint_access
42
 * NAME
43
 *   have_natpfruleint_access
44
 * INPUTS
45 c96e71d1 Renato Botelho
 *	none
46 0397013a Scott Ullrich
 * RESULT
47
 *   returns true if user has access to edit a specific firewall nat port forward interface
48
 ******/
49
function have_natpfruleint_access($if) {
50
	$security_url = "firewall_nat_edit.php?if=". strtolower($if);
51 23a193da Phil Davis
	if (isAllowedPage($security_url, $allowed)) {
52 0397013a Scott Ullrich
		return true;
53 23a193da Phil Davis
	}
54 0397013a Scott Ullrich
	return false;
55
}
56
57 b6742927 Scott Ullrich
/****f* pfsense-utils/have_ruleint_access
58
 * NAME
59
 *   have_ruleint_access
60
 * INPUTS
61 c96e71d1 Renato Botelho
 *	none
62 b6742927 Scott Ullrich
 * RESULT
63
 *   returns true if user has access to edit a specific firewall interface
64
 ******/
65
function have_ruleint_access($if) {
66
	$security_url = "firewall_rules.php?if=". strtolower($if);
67 23a193da Phil Davis
	if (isAllowedPage($security_url)) {
68 45ee90ed Matthew Grooms
		return true;
69 23a193da Phil Davis
	}
70 b6742927 Scott Ullrich
	return false;
71
}
72
73 10387862 Scott Ullrich
/****f* pfsense-utils/does_url_exist
74
 * NAME
75
 *   does_url_exist
76
 * INPUTS
77 c96e71d1 Renato Botelho
 *	none
78 10387862 Scott Ullrich
 * RESULT
79
 *   returns true if a url is available
80
 ******/
81
function does_url_exist($url) {
82 3264c13b Scott Ullrich
	$fd = fopen("$url","r");
83 23a193da Phil Davis
	if ($fd) {
84 4cc6345e Scott Ullrich
		fclose($fd);
85 5fa78adc Renato Botelho
		return true;
86 10387862 Scott Ullrich
	} else {
87 5fa78adc Renato Botelho
		return false;
88 10387862 Scott Ullrich
	}
89
}
90
91 5928bd75 Scott Ullrich
/****f* pfsense-utils/is_private_ip
92
 * NAME
93
 *   is_private_ip
94
 * INPUTS
95 c96e71d1 Renato Botelho
 *	none
96 5928bd75 Scott Ullrich
 * RESULT
97
 *   returns true if an ip address is in a private range
98
 ******/
99
function is_private_ip($iptocheck) {
100 5fa78adc Renato Botelho
	$isprivate = false;
101
	$ip_private_list=array(
102
		"10.0.0.0/8",
103
		"100.64.0.0/10",
104
		"172.16.0.0/12",
105
		"192.168.0.0/16",
106
	);
107 23a193da Phil Davis
	foreach ($ip_private_list as $private) {
108
		if (ip_in_subnet($iptocheck,$private)==true) {
109 5fa78adc Renato Botelho
			$isprivate = true;
110 23a193da Phil Davis
		}
111 5fa78adc Renato Botelho
	}
112
	return $isprivate;
113 5928bd75 Scott Ullrich
}
114
115 8cb370b9 Scott Ullrich
/****f* pfsense-utils/get_tmp_file
116
 * NAME
117
 *   get_tmp_file
118
 * INPUTS
119 c96e71d1 Renato Botelho
 *	none
120 8cb370b9 Scott Ullrich
 * RESULT
121
 *   returns a temporary filename
122
 ******/
123 3076becf Scott Ullrich
function get_tmp_file() {
124 da17d77e Ermal Lu?i
	global $g;
125
	return "{$g['tmp_path']}/tmp-" . time();
126 3076becf Scott Ullrich
}
127
128
/****f* pfsense-utils/get_dns_servers
129
 * NAME
130
 *   get_dns_servres - get system dns servers
131
 * INPUTS
132
 *   $dns_servers - an array of the dns servers
133
 * RESULT
134
 *   null
135
 ******/
136
function get_dns_servers() {
137
	$dns_servers = array();
138 fa112436 Ermal
	$dns_s = file("/etc/resolv.conf", FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
139 23a193da Phil Davis
	foreach ($dns_s as $dns) {
140 3076becf Scott Ullrich
		$matches = "";
141 23a193da Phil Davis
		if (preg_match("/nameserver (.*)/", $dns, $matches)) {
142 3076becf Scott Ullrich
			$dns_servers[] = $matches[1];
143 23a193da Phil Davis
		}
144 3076becf Scott Ullrich
	}
145 fa112436 Ermal
	return array_unique($dns_servers);
146 3076becf Scott Ullrich
}
147
148 43517fcc Ermal LUÇI
function hardware_offloading_applyflags($iface) {
149
	global $config;
150
151
	$flags_on = 0;
152
	$flags_off = 0;
153
	$options = pfSense_get_interface_addresses($iface);
154
155 23a193da Phil Davis
	if (isset($config['system']['disablechecksumoffloading'])) {
156
		if (isset($options['encaps']['txcsum'])) {
157 43517fcc Ermal LUÇI
			$flags_off |= IFCAP_TXCSUM;
158 23a193da Phil Davis
		}
159
		if (isset($options['encaps']['rxcsum'])) {
160 43517fcc Ermal LUÇI
			$flags_off |= IFCAP_RXCSUM;
161 23a193da Phil Davis
		}
162 43517fcc Ermal LUÇI
	} else {
163 23a193da Phil Davis
		if (isset($options['caps']['txcsum'])) {
164 43517fcc Ermal LUÇI
			$flags_on |= IFCAP_TXCSUM;
165 23a193da Phil Davis
		}
166
		if (isset($options['caps']['rxcsum'])) {
167 43517fcc Ermal LUÇI
			$flags_on |= IFCAP_RXCSUM;
168 23a193da Phil Davis
		}
169 43517fcc Ermal LUÇI
	}
170
171 23a193da Phil Davis
	if (isset($config['system']['disablesegmentationoffloading'])) {
172 43517fcc Ermal LUÇI
		$flags_off |= IFCAP_TSO;
173 23a193da Phil Davis
	} else if (isset($options['caps']['tso']) || isset($options['caps']['tso4']) || isset($options['caps']['tso6'])) {
174 43517fcc Ermal LUÇI
		$flags_on |= IFCAP_TSO;
175 23a193da Phil Davis
	}
176 43517fcc Ermal LUÇI
177 23a193da Phil Davis
	if (isset($config['system']['disablelargereceiveoffloading'])) {
178 43517fcc Ermal LUÇI
		$flags_off |= IFCAP_LRO;
179 23a193da Phil Davis
	} else if (isset($options['caps']['lro'])) {
180 43517fcc Ermal LUÇI
		$flags_on |= IFCAP_LRO;
181 23a193da Phil Davis
	}
182 43517fcc Ermal LUÇI
183
	/* if the NIC supports polling *AND* it is enabled in the GUI */
184 23a193da Phil Davis
	if (!isset($config['system']['polling'])) {
185 43517fcc Ermal LUÇI
		$flags_off |= IFCAP_POLLING;
186 23a193da Phil Davis
	} else if (isset($options['caps']['polling'])) {
187 43517fcc Ermal LUÇI
		$flags_on |= IFCAP_POLLING;
188 23a193da Phil Davis
	}
189 43517fcc Ermal LUÇI
190
	pfSense_interface_capabilities($iface, -$flags_off);
191
	pfSense_interface_capabilities($iface, $flags_on);
192
}
193
194 3076becf Scott Ullrich
/****f* pfsense-utils/enable_hardware_offloading
195
 * NAME
196
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
197
 * INPUTS
198
 *   $interface	- string containing the physical interface to work on.
199
 * RESULT
200
 *   null
201
 * NOTES
202
 *   This function only supports the fxp driver's loadable microcode.
203
 ******/
204
function enable_hardware_offloading($interface) {
205
	global $g, $config;
206
207 a2934331 Scott Ullrich
	$int = get_real_interface($interface);
208 23a193da Phil Davis
	if (empty($int)) {
209 3d063391 Ermal
		return;
210 23a193da Phil Davis
	}
211 43517fcc Ermal LUÇI
212
	if (!isset($config['system']['do_not_use_nic_microcode'])) {
213
		/* translate wan, lan, opt -> real interface if needed */
214
		$int_family = preg_split("/[0-9]+/", $int);
215
		$supported_ints = array('fxp');
216
		if (in_array($int_family, $supported_ints)) {
217 23a193da Phil Davis
			if (does_interface_exist($int)) {
218 43517fcc Ermal LUÇI
				pfSense_interface_flags($int, IFF_LINK0);
219 23a193da Phil Davis
			}
220 43517fcc Ermal LUÇI
		}
221 a2934331 Scott Ullrich
	}
222 3076becf Scott Ullrich
223 43517fcc Ermal LUÇI
	/* This is mostly for vlans and ppp types */
224
	$realhwif = get_parent_interface($interface);
225 23a193da Phil Davis
	if ($realhwif[0] == $int) {
226 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($int);
227 23a193da Phil Davis
	} else {
228 43517fcc Ermal LUÇI
		hardware_offloading_applyflags($realhwif[0]);
229
		hardware_offloading_applyflags($int);
230
	}
231 3076becf Scott Ullrich
}
232
233 f7eb54e4 Scott Ullrich
/****f* pfsense-utils/interface_supports_polling
234
 * NAME
235
 *   checks to see if an interface supports polling according to man polling
236
 * INPUTS
237
 *
238
 * RESULT
239
 *   true or false
240
 * NOTES
241
 *
242
 ******/
243
function interface_supports_polling($iface) {
244 3d063391 Ermal
	$opts = pfSense_get_interface_addresses($iface);
245 23a193da Phil Davis
	if (is_array($opts) && isset($opts['caps']['polling'])) {
246 f7eb54e4 Scott Ullrich
		return true;
247 23a193da Phil Davis
	}
248 3d063391 Ermal
249 f7eb54e4 Scott Ullrich
	return false;
250
}
251
252 3076becf Scott Ullrich
/****f* pfsense-utils/is_alias_inuse
253
 * NAME
254
 *   checks to see if an alias is currently in use by a rule
255
 * INPUTS
256
 *
257
 * RESULT
258
 *   true or false
259
 * NOTES
260
 *
261
 ******/
262
function is_alias_inuse($alias) {
263
	global $g, $config;
264
265 23a193da Phil Davis
	if ($alias == "") {
266
		return false;
267
	}
268 3076becf Scott Ullrich
	/* loop through firewall rules looking for alias in use */
269 23a193da Phil Davis
	if (is_array($config['filter']['rule'])) {
270
		foreach ($config['filter']['rule'] as $rule) {
271
			if ($rule['source']['address']) {
272
				if ($rule['source']['address'] == $alias) {
273 0c8c496e Scott Ullrich
					return true;
274 23a193da Phil Davis
				}
275
			}
276
			if ($rule['destination']['address']) {
277
				if ($rule['destination']['address'] == $alias) {
278 0c8c496e Scott Ullrich
					return true;
279 23a193da Phil Davis
				}
280
			}
281 0c8c496e Scott Ullrich
		}
282 23a193da Phil Davis
	}
283 3076becf Scott Ullrich
	/* loop through nat rules looking for alias in use */
284 23a193da Phil Davis
	if (is_array($config['nat']['rule'])) {
285
		foreach ($config['nat']['rule'] as $rule) {
286
			if ($rule['target'] && $rule['target'] == $alias) {
287 3076becf Scott Ullrich
				return true;
288 23a193da Phil Davis
			}
289
			if ($rule['source']['address'] && $rule['source']['address'] == $alias) {
290 59ecde49 Renato Botelho
				return true;
291 23a193da Phil Davis
			}
292
			if ($rule['destination']['address'] && $rule['destination']['address'] == $alias) {
293 3076becf Scott Ullrich
				return true;
294 23a193da Phil Davis
			}
295 3076becf Scott Ullrich
		}
296 23a193da Phil Davis
	}
297 3076becf Scott Ullrich
	return false;
298
}
299
300 63724b02 Scott Dale
/****f* pfsense-utils/is_schedule_inuse
301
 * NAME
302
 *   checks to see if a schedule is currently in use by a rule
303
 * INPUTS
304
 *
305
 * RESULT
306
 *   true or false
307
 * NOTES
308
 *
309
 ******/
310
function is_schedule_inuse($schedule) {
311
	global $g, $config;
312
313 23a193da Phil Davis
	if ($schedule == "") {
314
		return false;
315
	}
316 63724b02 Scott Dale
	/* loop through firewall rules looking for schedule in use */
317 23a193da Phil Davis
	if (is_array($config['filter']['rule'])) {
318
		foreach ($config['filter']['rule'] as $rule) {
319
			if ($rule['sched'] == $schedule) {
320 591ceb32 Scott Dale
				return true;
321 23a193da Phil Davis
			}
322 63724b02 Scott Dale
		}
323 23a193da Phil Davis
	}
324 63724b02 Scott Dale
	return false;
325
}
326
327 3076becf Scott Ullrich
/****f* pfsense-utils/setup_polling
328
 * NAME
329
 *   sets up polling
330
 * INPUTS
331
 *
332
 * RESULT
333
 *   null
334
 * NOTES
335
 *
336
 ******/
337
function setup_polling() {
338
	global $g, $config;
339
340 23a193da Phil Davis
	if (isset($config['system']['polling'])) {
341 971de1f9 Renato Botelho
		set_single_sysctl("kern.polling.idle_poll", "1");
342 23a193da Phil Davis
	} else {
343 971de1f9 Renato Botelho
		set_single_sysctl("kern.polling.idle_poll", "0");
344 23a193da Phil Davis
	}
345 3076becf Scott Ullrich
346 23a193da Phil Davis
	if ($config['system']['polling_each_burst']) {
347 971de1f9 Renato Botelho
		set_single_sysctl("kern.polling.each_burst", $config['system']['polling_each_burst']);
348 23a193da Phil Davis
	}
349
	if ($config['system']['polling_burst_max']) {
350 971de1f9 Renato Botelho
		set_single_sysctl("kern.polling.burst_max", $config['system']['polling_burst_max']);
351 23a193da Phil Davis
	}
352
	if ($config['system']['polling_user_frac']) {
353 971de1f9 Renato Botelho
		set_single_sysctl("kern.polling.user_frac", $config['system']['polling_user_frac']);
354 23a193da Phil Davis
	}
355 3076becf Scott Ullrich
}
356
357
/****f* pfsense-utils/setup_microcode
358
 * NAME
359
 *   enumerates all interfaces and calls enable_hardware_offloading which
360
 *   enables a NIC's supported hardware features.
361
 * INPUTS
362
 *
363
 * RESULT
364
 *   null
365
 * NOTES
366
 *   This function only supports the fxp driver's loadable microcode.
367
 ******/
368
function setup_microcode() {
369
370 3a4ce87d Ermal Luçi
	/* if list */
371 43517fcc Ermal LUÇI
	$iflist = get_configured_interface_list(false, true);
372 23a193da Phil Davis
	foreach ($iflist as $if => $ifdescr) {
373 3076becf Scott Ullrich
		enable_hardware_offloading($if);
374 23a193da Phil Davis
	}
375 dced0dd0 Ermal LUÇI
	unset($iflist);
376 3076becf Scott Ullrich
}
377
378
/****f* pfsense-utils/get_carp_status
379
 * NAME
380
 *   get_carp_status - Return whether CARP is enabled or disabled.
381
 * RESULT
382
 *   boolean	- true if CARP is enabled, false if otherwise.
383
 ******/
384
function get_carp_status() {
385 5fa78adc Renato Botelho
	/* grab the current status of carp */
386 971de1f9 Renato Botelho
	$status = get_single_sysctl('net.inet.carp.allow');
387 5fa78adc Renato Botelho
	return (intval($status) > 0);
388 3076becf Scott Ullrich
}
389
390
/*
391
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
392 52947718 Ermal Lu?i
393 3076becf Scott Ullrich
 */
394
function convert_ip_to_network_format($ip, $subnet) {
395 2ce660ad smos
	$ipsplit = explode('.', $ip);
396 3076becf Scott Ullrich
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
397
	return $string;
398
}
399
400
/*
401
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
402
 */
403
function get_carp_interface_status($carpinterface) {
404 b6877e06 Ermal
405 0c21eb70 Ermal
	$interface = get_real_interface($interface);
406 89f171b0 Ermal LUÇI
	$carp_query = '';
407
	$_gb = exec("/sbin/ifconfig $interface | /usr/bin/grep -v grep | /usr/bin/grep carp: | /usr/bin/head -n 1", $carp_query);
408 23a193da Phil Davis
	foreach ($carp_query as $int) {
409
		if (stripos($int, "MASTER")) {
410 7b753c2b Renato Botelho
			return "MASTER";
411 23a193da Phil Davis
		}
412
		if (stripos($int, "BACKUP")) {
413 7b753c2b Renato Botelho
			return "BACKUP";
414 23a193da Phil Davis
		}
415
		if (stripos($int, "INIT")) {
416 7b753c2b Renato Botelho
			return "INIT";
417 23a193da Phil Davis
		}
418 3076becf Scott Ullrich
	}
419
	return;
420
}
421
422
/*
423
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
424
 */
425
function get_pfsync_interface_status($pfsyncinterface) {
426 23a193da Phil Davis
	if (!does_interface_exist($pfsyncinterface)) {
427 306f8556 Renato Botelho
		return;
428 23a193da Phil Davis
	}
429 306f8556 Renato Botelho
430
	return exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/awk '/pfsync:/ {print \$5}'");
431 3076becf Scott Ullrich
}
432
433
/*
434
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
435
 */
436
function add_rule_to_anchor($anchor, $rule, $label) {
437 873c1701 Renato Botelho
	mwexec("echo " . escapeshellarg($rule) . " | /sbin/pfctl -a " . escapeshellarg($anchor) . ":" . escapeshellarg($label) . " -f -");
438 3076becf Scott Ullrich
}
439
440
/*
441
 * remove_text_from_file
442
 * remove $text from file $file
443
 */
444
function remove_text_from_file($file, $text) {
445 23a193da Phil Davis
	if (!file_exists($file) && !is_writable($file)) {
446 2addd5b2 Ermal
		return;
447 23a193da Phil Davis
	}
448 3076becf Scott Ullrich
	$filecontents = file_get_contents($file);
449 2addd5b2 Ermal
	$text = str_replace($text, "", $filecontents);
450 5fa78adc Renato Botelho
	@file_put_contents($file, $text);
451 3076becf Scott Ullrich
}
452
453
/*
454
 *   after_sync_bump_adv_skew(): create skew values by 1S
455
 */
456
function after_sync_bump_adv_skew() {
457
	global $config, $g;
458
	$processed_skew = 1;
459
	$a_vip = &$config['virtualip']['vip'];
460
	foreach ($a_vip as $vipent) {
461 23a193da Phil Davis
		if ($vipent['advskew'] <> "") {
462 3076becf Scott Ullrich
			$processed_skew = 1;
463
			$vipent['advskew'] = $vipent['advskew']+1;
464
		}
465
	}
466 23a193da Phil Davis
	if ($processed_skew == 1) {
467 7d1b238c Carlos Eduardo Ramos
		write_config(gettext("After synch increase advertising skew"));
468 23a193da Phil Davis
	}
469 3076becf Scott Ullrich
}
470
471
/*
472
 * get_filename_from_url($url): converts a url to its filename.
473
 */
474
function get_filename_from_url($url) {
475
	return basename($url);
476
}
477
478
/*
479
 *   get_dir: return an array of $dir
480
 */
481
function get_dir($dir) {
482
	$dir_array = array();
483
	$d = dir($dir);
484
	while (false !== ($entry = $d->read())) {
485
		array_push($dir_array, $entry);
486
	}
487
	$d->close();
488
	return $dir_array;
489
}
490
491
/****f* pfsense-utils/WakeOnLan
492
 * NAME
493
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
494
 * RESULT
495
 *   true/false - true if the operation was successful
496
 ******/
497
function WakeOnLan($addr, $mac)
498
{
499
	$addr_byte = explode(':', $mac);
500
	$hw_addr = '';
501
502 23a193da Phil Davis
	for ($a=0; $a < 6; $a++) {
503 3076becf Scott Ullrich
		$hw_addr .= chr(hexdec($addr_byte[$a]));
504 23a193da Phil Davis
	}
505 3076becf Scott Ullrich
506
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
507
508 23a193da Phil Davis
	for ($a = 1; $a <= 16; $a++) {
509 3076becf Scott Ullrich
		$msg .= $hw_addr;
510 23a193da Phil Davis
	}
511 3076becf Scott Ullrich
512
	// send it to the broadcast address using UDP
513
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
514
	if ($s == false) {
515 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Error creating socket!"));
516 addc0439 Renato Botelho
		log_error(sprintf(gettext("Error code is '%1\$s' - %2\$s"), socket_last_error($s), socket_strerror(socket_last_error($s))));
517 3076becf Scott Ullrich
	} else {
518
		// setting a broadcast option to socket:
519
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
520 23a193da Phil Davis
		if ($opt_ret < 0) {
521 7d1b238c Carlos Eduardo Ramos
			log_error(sprintf(gettext("setsockopt() failed, error: %s"), strerror($opt_ret)));
522 23a193da Phil Davis
		}
523 3076becf Scott Ullrich
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
524
		socket_close($s);
525 addc0439 Renato Botelho
		log_error(sprintf(gettext('Magic Packet sent (%1$s) to {%2$s} MAC=%3$s'), $e, $addr, $mac));
526 3076becf Scott Ullrich
		return true;
527 0c8c496e Scott Ullrich
	}
528 3076becf Scott Ullrich
529
	return false;
530
}
531
532
/*
533
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
534
 *					 Useful for finding paths and stripping file extensions.
535
 */
536
function reverse_strrchr($haystack, $needle) {
537 23a193da Phil Davis
	if (!is_string($haystack)) {
538 4824d857 Ermal Lu?i
		return;
539 23a193da Phil Davis
	}
540
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1) : false;
541 3076becf Scott Ullrich
}
542
543
/*
544
 *  backup_config_section($section): returns as an xml file string of
545
 *                                   the configuration section
546
 */
547 8dcca9b5 Darren Embry
function backup_config_section($section_name) {
548 3076becf Scott Ullrich
	global $config;
549 8dcca9b5 Darren Embry
	$new_section = &$config[$section_name];
550 3076becf Scott Ullrich
	/* generate configuration XML */
551 8dcca9b5 Darren Embry
	$xmlconfig = dump_xml_config($new_section, $section_name);
552 3076becf Scott Ullrich
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
553
	return $xmlconfig;
554
}
555
556
/*
557 8dcca9b5 Darren Embry
 *  restore_config_section($section_name, new_contents): restore a configuration section,
558 3076becf Scott Ullrich
 *                                                  and write the configuration out
559
 *                                                  to disk/cf.
560
 */
561 8dcca9b5 Darren Embry
function restore_config_section($section_name, $new_contents) {
562 3076becf Scott Ullrich
	global $config, $g;
563
	conf_mount_rw();
564
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
565
	fwrite($fout, $new_contents);
566
	fclose($fout);
567 8dcca9b5 Darren Embry
568
	$xml = parse_xml_config($g['tmp_path'] . "/tmpxml", null);
569
	if ($xml['pfsense']) {
570
		$xml = $xml['pfsense'];
571
	}
572
	else if ($xml['m0n0wall']) {
573
		$xml = $xml['m0n0wall'];
574
	}
575
	if ($xml[$section_name]) {
576
		$section_xml = $xml[$section_name];
577
	} else {
578
		$section_xml = -1;
579
	}
580
581 541989d5 Ermal
	@unlink($g['tmp_path'] . "/tmpxml");
582 8dcca9b5 Darren Embry
	if ($section_xml === -1) {
583
		return false;
584
	}
585
	$config[$section_name] = &$section_xml;
586 23a193da Phil Davis
	if (file_exists("{$g['tmp_path']}/config.cache")) {
587 a57d6170 Scott Ullrich
		unlink("{$g['tmp_path']}/config.cache");
588 23a193da Phil Davis
	}
589 8dcca9b5 Darren Embry
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
590 0f806eca Erik Fonnesbeck
	disable_security_checks();
591 3076becf Scott Ullrich
	conf_mount_ro();
592 8dcca9b5 Darren Embry
	return true;
593 3076becf Scott Ullrich
}
594
595
/*
596 8dcca9b5 Darren Embry
 *  merge_config_section($section_name, new_contents):   restore a configuration section,
597 3076becf Scott Ullrich
 *                                                  and write the configuration out
598
 *                                                  to disk/cf.  But preserve the prior
599
 * 													structure if needed
600
 */
601 8dcca9b5 Darren Embry
function merge_config_section($section_name, $new_contents) {
602 3076becf Scott Ullrich
	global $config;
603
	conf_mount_rw();
604
	$fname = get_tmp_filename();
605
	$fout = fopen($fname, "w");
606
	fwrite($fout, $new_contents);
607
	fclose($fout);
608 8dcca9b5 Darren Embry
	$section_xml = parse_xml_config($fname, $section_name);
609
	$config[$section_name] = $section_xml;
610 3076becf Scott Ullrich
	unlink($fname);
611 8dcca9b5 Darren Embry
	write_config(sprintf(gettext("Restored %s of config file (maybe from CARP partner)"), $section_name));
612 0f806eca Erik Fonnesbeck
	disable_security_checks();
613 3076becf Scott Ullrich
	conf_mount_ro();
614
	return;
615
}
616
617
/*
618
 * http_post($server, $port, $url, $vars): does an http post to a web server
619
 *                                         posting the vars array.
620
 * written by nf@bigpond.net.au
621
 */
622
function http_post($server, $port, $url, $vars) {
623
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
624
	$urlencoded = "";
625 23a193da Phil Davis
	while (list($key,$value) = each($vars)) {
626 3076becf Scott Ullrich
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
627 23a193da Phil Davis
	}
628 3076becf Scott Ullrich
	$urlencoded = substr($urlencoded,0,-1);
629
	$content_length = strlen($urlencoded);
630
	$headers = "POST $url HTTP/1.1
631
Accept: */*
632
Accept-Language: en-au
633
Content-Type: application/x-www-form-urlencoded
634
User-Agent: $user_agent
635
Host: $server
636
Connection: Keep-Alive
637
Cache-Control: no-cache
638
Content-Length: $content_length
639
640
";
641
642
	$errno = "";
643
	$errstr = "";
644
	$fp = fsockopen($server, $port, $errno, $errstr);
645
	if (!$fp) {
646 0c8c496e Scott Ullrich
		return false;
647
	}
648 3076becf Scott Ullrich
649
	fputs($fp, $headers);
650
	fputs($fp, $urlencoded);
651
652
	$ret = "";
653 23a193da Phil Davis
	while (!feof($fp)) {
654 3076becf Scott Ullrich
		$ret.= fgets($fp, 1024);
655 23a193da Phil Davis
	}
656 3076becf Scott Ullrich
	fclose($fp);
657
658
	return $ret;
659
}
660
661
/*
662
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
663
 */
664 23a193da Phil Davis
if (!function_exists('php_check_syntax')) {
665 da17d77e Ermal Lu?i
	global $g;
666 23a193da Phil Davis
	function php_check_syntax($code_to_check, &$errormessage) {
667 3076becf Scott Ullrich
		return false;
668 da17d77e Ermal Lu?i
		$fout = fopen("{$g['tmp_path']}/codetocheck.php","w");
669 3076becf Scott Ullrich
		$code = $_POST['content'];
670
		$code = str_replace("<?php", "", $code);
671
		$code = str_replace("?>", "", $code);
672
		fwrite($fout, "<?php\n\n");
673
		fwrite($fout, $code_to_check);
674
		fwrite($fout, "\n\n?>\n");
675 0c8c496e Scott Ullrich
		fclose($fout);
676 da17d77e Ermal Lu?i
		$command = "/usr/local/bin/php -l {$g['tmp_path']}/codetocheck.php";
677 3076becf Scott Ullrich
		$output = exec_command($command);
678
		if (stristr($output, "Errors parsing") == false) {
679
			echo "false\n";
680
			$errormessage = '';
681
			return(false);
682
		} else {
683
			$errormessage = $output;
684
			return(true);
685 0c8c496e Scott Ullrich
		}
686
	}
687 3076becf Scott Ullrich
}
688
689
/*
690
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
691
 */
692 23a193da Phil Davis
if (!function_exists('php_check_syntax')) {
693
	function php_check_syntax($code_to_check, &$errormessage) {
694 3076becf Scott Ullrich
		return false;
695 873c1701 Renato Botelho
		$command = "/usr/local/bin/php -l " . escapeshellarg($code_to_check);
696 3076becf Scott Ullrich
		$output = exec_command($command);
697
		if (stristr($output, "Errors parsing") == false) {
698
			echo "false\n";
699
			$errormessage = '';
700
			return(false);
701
		} else {
702
			$errormessage = $output;
703
			return(true);
704
		}
705
	}
706
}
707
708
/*
709
 * rmdir_recursive($path,$follow_links=false)
710
 * Recursively remove a directory tree (rm -rf path)
711
 * This is for directories _only_
712
 */
713
function rmdir_recursive($path,$follow_links=false) {
714
	$to_do = glob($path);
715 23a193da Phil Davis
	if (!is_array($to_do)) {
716
		$to_do = array($to_do);
717
	}
718
	foreach ($to_do as $workingdir) { // Handle wildcards by foreaching.
719
		if (file_exists($workingdir)) {
720
			if (is_dir($workingdir)) {
721 3076becf Scott Ullrich
				$dir = opendir($workingdir);
722
				while ($entry = readdir($dir)) {
723 23a193da Phil Davis
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry"))) {
724 3076becf Scott Ullrich
						unlink("$workingdir/$entry");
725 23a193da Phil Davis
					} elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..') {
726 3076becf Scott Ullrich
						rmdir_recursive("$workingdir/$entry");
727 23a193da Phil Davis
					}
728 6613a031 Scott Ullrich
				}
729 3076becf Scott Ullrich
				closedir($dir);
730
				rmdir($workingdir);
731
			} elseif (is_file($workingdir)) {
732
				unlink($workingdir);
733
			}
734 5fa78adc Renato Botelho
		}
735 3076becf Scott Ullrich
	}
736
	return;
737
}
738
739
/*
740 f5e09d92 Chris Buechler
 * call_pfsense_method(): Call a method exposed by the pfsense.org XMLRPC server.
741 3076becf Scott Ullrich
 */
742
function call_pfsense_method($method, $params, $timeout = 0) {
743 cfceefc6 Scott Ullrich
	global $g, $config;
744
745 7c8f3711 jim-p
	$xmlrpc_base_url = get_active_xml_rpc_base_url();
746 3076becf Scott Ullrich
	$xmlrpc_path = $g['xmlrpcpath'];
747 23a193da Phil Davis
748 1b92cc61 Chris Buechler
	$xmlrpcfqdn = preg_replace("(https?://)", "", $xmlrpc_base_url);
749
	$ip = gethostbyname($xmlrpcfqdn);
750 23a193da Phil Davis
	if ($ip == $xmlrpcfqdn) {
751 1b92cc61 Chris Buechler
		return false;
752 23a193da Phil Davis
	}
753 1b92cc61 Chris Buechler
754 3076becf Scott Ullrich
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
755 42c07003 Ermal
	$port = 0;
756
	$proxyurl = "";
757
	$proxyport = 0;
758
	$proxyuser = "";
759
	$proxypass = "";
760 23a193da Phil Davis
	if (!empty($config['system']['proxyurl'])) {
761 aa3c4866 Ermal
		$proxyurl = $config['system']['proxyurl'];
762 23a193da Phil Davis
	}
763
	if (!empty($config['system']['proxyport']) && is_numeric($config['system']['proxyport'])) {
764 aa3c4866 Ermal
		$proxyport = $config['system']['proxyport'];
765 23a193da Phil Davis
	}
766
	if (!empty($config['system']['proxyuser'])) {
767 aa3c4866 Ermal
		$proxyuser = $config['system']['proxyuser'];
768 23a193da Phil Davis
	}
769
	if (!empty($config['system']['proxypass'])) {
770 aa3c4866 Ermal
		$proxypass = $config['system']['proxypass'];
771 23a193da Phil Davis
	}
772 42c07003 Ermal
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url, $port, $proxyurl, $proxyport, $proxyuser, $proxypass);
773 16348c36 Scott Ullrich
	// If the ALT PKG Repo has a username/password set, use it.
774 23a193da Phil Davis
	if ($config['system']['altpkgrepo']['username'] &&
775
	    $config['system']['altpkgrepo']['password']) {
776 16348c36 Scott Ullrich
		$username = $config['system']['altpkgrepo']['username'];
777
		$password = $config['system']['altpkgrepo']['password'];
778
		$cli->setCredentials($username, $password);
779
	}
780 3076becf Scott Ullrich
	$resp = $cli->send($msg, $timeout);
781 23a193da Phil Davis
	if (!is_object($resp)) {
782 7d1b238c Carlos Eduardo Ramos
		log_error(sprintf(gettext("XMLRPC communication error: %s"), $cli->errstr));
783 3076becf Scott Ullrich
		return false;
784 23a193da Phil Davis
	} elseif ($resp->faultCode()) {
785 addc0439 Renato Botelho
		log_error(sprintf(gettext('XMLRPC request failed with error %1$s: %2$s'), $resp->faultCode(), $resp->faultString()));
786 3076becf Scott Ullrich
		return false;
787
	} else {
788
		return XML_RPC_Decode($resp->value());
789
	}
790
}
791
792
/*
793
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
794
 */
795
function check_firmware_version($tocheck = "all", $return_php = true) {
796
	global $g, $config;
797 23a193da Phil Davis
798 7c8f3711 jim-p
	$xmlrpc_base_url = get_active_xml_rpc_base_url();
799 1b92cc61 Chris Buechler
	$xmlrpcfqdn = preg_replace("(https?://)", "", $xmlrpc_base_url);
800
	$ip = gethostbyname($xmlrpcfqdn);
801 23a193da Phil Davis
	if ($ip == $xmlrpcfqdn) {
802 3076becf Scott Ullrich
		return false;
803 23a193da Phil Davis
	}
804 a02fa5ec Ermal
	$version = php_uname('r');
805
	$version = explode('-', $version);
806 3076becf Scott Ullrich
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
807 a02fa5ec Ermal
		"kernel"   => array("version" => $version[0]),
808
		"base"     => array("version" => $version[0]),
809 d064a115 Ermal
		"platform" => trim(file_get_contents('/etc/platform')),
810
		"config_version" => $config['version']
811 3076becf Scott Ullrich
		);
812 a02fa5ec Ermal
	unset($version);
813
814 23a193da Phil Davis
	if ($tocheck == "all") {
815 3076becf Scott Ullrich
		$params = $rawparams;
816
	} else {
817 23a193da Phil Davis
		foreach ($tocheck as $check) {
818 3076becf Scott Ullrich
			$params['check'] = $rawparams['check'];
819
			$params['platform'] = $rawparams['platform'];
820
		}
821
	}
822 23a193da Phil Davis
	if ($config['system']['firmware']['branch']) {
823 3076becf Scott Ullrich
		$params['branch'] = $config['system']['firmware']['branch'];
824 23a193da Phil Davis
	}
825 d064a115 Ermal
826
	/* XXX: What is this method? */
827 23a193da Phil Davis
	if (!($versions = call_pfsense_method('pfsense.get_firmware_version', $params))) {
828 3076becf Scott Ullrich
		return false;
829
	} else {
830
		$versions["current"] = $params;
831
	}
832 d064a115 Ermal
833 3076becf Scott Ullrich
	return $versions;
834
}
835
836 e501de37 Ermal
/*
837
 * host_firmware_version(): Return the versions used in this install
838
 */
839 18be996d Ermal
function host_firmware_version($tocheck = "") {
840 5fa78adc Renato Botelho
	global $g, $config;
841 e501de37 Ermal
842 02406801 jim-p
	$os_version = trim(substr(php_uname("r"), 0, strpos(php_uname("r"), '-')));
843
844 5fa78adc Renato Botelho
	return array(
845 e501de37 Ermal
		"firmware" => array("version" => trim(file_get_contents('/etc/version', " \n"))),
846 02406801 jim-p
		"kernel"   => array("version" => $os_version),
847
		"base"     => array("version" => $os_version),
848 5fa78adc Renato Botelho
		"platform" => trim(file_get_contents('/etc/platform', " \n")),
849
		"config_version" => $config['version']
850
	);
851 e501de37 Ermal
}
852
853 3076becf Scott Ullrich
function get_disk_info() {
854
	$diskout = "";
855
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
856
	return explode(' ', $diskout[0]);
857
}
858
859
/****f* pfsense-utils/strncpy
860
 * NAME
861
 *   strncpy - copy strings
862
 * INPUTS
863
 *   &$dst, $src, $length
864
 * RESULT
865
 *   none
866
 ******/
867
function strncpy(&$dst, $src, $length) {
868
	if (strlen($src) > $length) {
869
		$dst = substr($src, 0, $length);
870
	} else {
871
		$dst = $src;
872
	}
873
}
874
875
/****f* pfsense-utils/reload_interfaces_sync
876
 * NAME
877
 *   reload_interfaces - reload all interfaces
878
 * INPUTS
879
 *   none
880
 * RESULT
881
 *   none
882
 ******/
883
function reload_interfaces_sync() {
884 c0836064 Ermal Luçi
	global $config, $g;
885 3076becf Scott Ullrich
886 23a193da Phil Davis
	if ($g['debug']) {
887 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("reload_interfaces_sync() is starting."));
888 23a193da Phil Davis
	}
889 3076becf Scott Ullrich
890
	/* parse config.xml again */
891
	$config = parse_config(true);
892
893 a5d6f60b Ermal Lu?i
	/* enable routing */
894
	system_routing_enable();
895 23a193da Phil Davis
	if ($g['debug']) {
896 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Enabling system routing"));
897 23a193da Phil Davis
	}
898 3076becf Scott Ullrich
899 23a193da Phil Davis
	if ($g['debug']) {
900 7d1b238c Carlos Eduardo Ramos
		log_error(gettext("Cleaning up Interfaces"));
901 23a193da Phil Davis
	}
902 3076becf Scott Ullrich
903 67ee1ec5 Ermal Luçi
	/* set up interfaces */
904
	interfaces_configure();
905 3076becf Scott Ullrich
}
906
907
/****f* pfsense-utils/reload_all
908
 * NAME
909
 *   reload_all - triggers a reload of all settings
910
 *   * INPUTS
911
 *   none
912
 * RESULT
913
 *   none
914
 ******/
915
function reload_all() {
916 0ae6daf8 Ermal
	send_event("service reload all");
917 3076becf Scott Ullrich
}
918
919
/****f* pfsense-utils/reload_interfaces
920
 * NAME
921
 *   reload_interfaces - triggers a reload of all interfaces
922
 * INPUTS
923
 *   none
924
 * RESULT
925
 *   none
926
 ******/
927
function reload_interfaces() {
928 5e3a84e2 Ermal
	send_event("interface all reload");
929 3076becf Scott Ullrich
}
930
931
/****f* pfsense-utils/reload_all_sync
932
 * NAME
933
 *   reload_all - reload all settings
934
 *   * INPUTS
935
 *   none
936
 * RESULT
937
 *   none
938
 ******/
939
function reload_all_sync() {
940
	global $config, $g;
941
942
	/* parse config.xml again */
943
	$config = parse_config(true);
944
945
	/* set up our timezone */
946
	system_timezone_configure();
947
948
	/* set up our hostname */
949
	system_hostname_configure();
950
951
	/* make hosts file */
952
	system_hosts_generate();
953
954
	/* generate resolv.conf */
955
	system_resolvconf_generate();
956
957
	/* enable routing */
958
	system_routing_enable();
959
960 a5d6f60b Ermal Lu?i
	/* set up interfaces */
961
	interfaces_configure();
962 3076becf Scott Ullrich
963
	/* start dyndns service */
964
	services_dyndns_configure();
965
966
	/* configure cron service */
967
	configure_cron();
968
969
	/* start the NTP client */
970
	system_ntp_configure();
971
972
	/* sync pw database */
973
	conf_mount_rw();
974 6b0c5879 Scott Ullrich
	unlink_if_exists("/etc/spwd.db.tmp");
975 3076becf Scott Ullrich
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
976
	conf_mount_ro();
977
978
	/* restart sshd */
979 0ae6daf8 Ermal
	send_event("service restart sshd");
980 3076becf Scott Ullrich
981
	/* restart webConfigurator if needed */
982 0ae6daf8 Ermal
	send_event("service restart webgui");
983 3076becf Scott Ullrich
}
984
985 196d0085 jim-p
function setup_serial_port($when="save", $path="") {
986 3076becf Scott Ullrich
	global $g, $config;
987
	conf_mount_rw();
988 02e4ee54 Renato Botelho
	$ttys_file = "{$path}/etc/ttys";
989 196d0085 jim-p
	$boot_config_file = "{$path}/boot.config";
990
	$loader_conf_file = "{$path}/boot/loader.conf";
991 3076becf Scott Ullrich
	/* serial console - write out /boot.config */
992 23a193da Phil Davis
	if (file_exists($boot_config_file)) {
993 196d0085 jim-p
		$boot_config = file_get_contents($boot_config_file);
994 23a193da Phil Davis
	} else {
995 3076becf Scott Ullrich
		$boot_config = "";
996 23a193da Phil Davis
	}
997 3076becf Scott Ullrich
998 4887afa1 Renato Botelho
	$serialspeed = (is_numeric($config['system']['serialspeed'])) ? $config['system']['serialspeed'] : "115200";
999 38c7d42e Renato Botelho
	if ($g['platform'] != "cdrom") {
1000 986e77a2 Renato Botelho
		$serial_only = false;
1001
1002
		if (($g['platform'] == "nanobsd") && !file_exists("/etc/nano_use_vga.txt")) {
1003
			$serial_only = true;
1004
		} else {
1005
			$specific_platform = system_identify_specific_platform();
1006
			if ($specific_platform['name'] == 'RCC-VE') {
1007
				$serial_only = true;
1008
			}
1009
		}
1010
1011 cfbfd941 smos
		$boot_config_split = explode("\n", $boot_config);
1012 196d0085 jim-p
		$fd = fopen($boot_config_file,"w");
1013 23a193da Phil Davis
		if ($fd) {
1014
			foreach ($boot_config_split as $bcs) {
1015
				if (stristr($bcs, "-D") || stristr($bcs, "-h")) {
1016 3076becf Scott Ullrich
					/* DONT WRITE OUT, WE'LL DO IT LATER */
1017
				} else {
1018 23a193da Phil Davis
					if ($bcs <> "") {
1019 3076becf Scott Ullrich
						fwrite($fd, "{$bcs}\n");
1020 23a193da Phil Davis
					}
1021 3076becf Scott Ullrich
				}
1022 0c8c496e Scott Ullrich
			}
1023 986e77a2 Renato Botelho
			if ($serial_only === true) {
1024 38c7d42e Renato Botelho
				fwrite($fd, "-S{$serialspeed} -h");
1025 23a193da Phil Davis
			} else if (is_serial_enabled()) {
1026 4887afa1 Renato Botelho
				fwrite($fd, "-S{$serialspeed} -D");
1027 23a193da Phil Davis
			}
1028 3076becf Scott Ullrich
			fclose($fd);
1029 0c8c496e Scott Ullrich
		}
1030 38c7d42e Renato Botelho
1031 3076becf Scott Ullrich
		/* serial console - write out /boot/loader.conf */
1032 23a193da Phil Davis
		if ($when == "upgrade") {
1033 baef6be8 jim-p
			system("echo \"Reading {$loader_conf_file}...\" >> /conf/upgrade_log.txt");
1034 23a193da Phil Davis
		}
1035 196d0085 jim-p
		$boot_config = file_get_contents($loader_conf_file);
1036 5f36c658 jim-p
		$boot_config_split = explode("\n", $boot_config);
1037 23a193da Phil Davis
		if (count($boot_config_split) > 0) {
1038 5f36c658 jim-p
			$new_boot_config = array();
1039
			// Loop through and only add lines that are not empty, and which
1040
			//  do not contain a console directive.
1041 23a193da Phil Davis
			foreach ($boot_config_split as $bcs) {
1042
				if (!empty($bcs) &&
1043
				    (stripos($bcs, "console") === false) &&
1044
				    (stripos($bcs, "boot_multicons") === false) &&
1045 b45e428c Renato Botelho
				    (stripos($bcs, "boot_serial") === false)) {
1046 5f36c658 jim-p
					$new_boot_config[] = $bcs;
1047 23a193da Phil Davis
				}
1048
			}
1049 5f36c658 jim-p
1050 986e77a2 Renato Botelho
			if ($serial_only === true) {
1051 eee053fe Renato Botelho
				$new_boot_config[] = 'boot_serial="YES"';
1052 38c7d42e Renato Botelho
				$new_boot_config[] = 'console="comconsole"';
1053
			} else if (is_serial_enabled()) {
1054 c1becc31 jim-p
				$new_boot_config[] = 'boot_multicons="YES"';
1055 eee053fe Renato Botelho
				$new_boot_config[] = 'boot_serial="YES"';
1056 bf4e62ac jim-p
				$primaryconsole = isset($g['primaryconsole_force']) ? $g['primaryconsole_force'] : $config['system']['primaryconsole'];
1057
				switch ($primaryconsole) {
1058
					case "video":
1059
						$new_boot_config[] = 'console="vidconsole,comconsole"';
1060
						break;
1061
					case "serial":
1062
					default:
1063
						$new_boot_config[] = 'console="comconsole,vidconsole"';
1064
				}
1065 c1becc31 jim-p
			}
1066 4887afa1 Renato Botelho
			$new_boot_config[] = 'comconsole_speed="' . $serialspeed . '"';
1067 25c088de Renato Botelho
1068 196d0085 jim-p
			file_put_contents($loader_conf_file, implode("\n", $new_boot_config) . "\n");
1069 0c8c496e Scott Ullrich
		}
1070
	}
1071 02e4ee54 Renato Botelho
	$ttys = file_get_contents($ttys_file);
1072 cfbfd941 smos
	$ttys_split = explode("\n", $ttys);
1073 02e4ee54 Renato Botelho
	$fd = fopen($ttys_file, "w");
1074 c5f9fb72 Renato Botelho
1075 4f009171 Renato Botelho
	$on_off = (is_serial_enabled() ? 'onifconsole' : 'off');
1076 c5f9fb72 Renato Botelho
1077 edb4b657 Renato Botelho
	if (isset($config['system']['disableconsolemenu'])) {
1078
		$console_type = 'Pc';
1079
		$serial_type = 'std.' . $serialspeed;
1080
	} else {
1081
		$console_type = 'al.Pc';
1082
		$serial_type = 'al.' . $serialspeed;
1083
	}
1084 23a193da Phil Davis
	foreach ($ttys_split as $tty) {
1085
		if (stristr($tty, "ttyv0")) {
1086 d4b1e549 Renato Botelho
			fwrite($fd, "ttyv0	\"/usr/libexec/getty {$console_type}\"	cons25	on	secure\n");
1087 23a193da Phil Davis
		} else if (stristr($tty, "ttyu")) {
1088 4f009171 Renato Botelho
			$ttyn = substr($tty, 0, 5);
1089
			fwrite($fd, "{$ttyn}	\"/usr/libexec/getty {$serial_type}\"	cons25	{$on_off}	secure\n");
1090 23a193da Phil Davis
		} else {
1091 3076becf Scott Ullrich
			fwrite($fd, $tty . "\n");
1092 23a193da Phil Davis
		}
1093 3076becf Scott Ullrich
	}
1094 edb4b657 Renato Botelho
	unset($on_off, $console_type, $serial_type);
1095 3076becf Scott Ullrich
	fclose($fd);
1096 23a193da Phil Davis
	if ($when != "upgrade") {
1097 02e4ee54 Renato Botelho
		reload_ttys();
1098 23a193da Phil Davis
	}
1099 a46e450c Ermal Lu?i
1100 3076becf Scott Ullrich
	conf_mount_ro();
1101
	return;
1102
}
1103
1104 38c7d42e Renato Botelho
function is_serial_enabled() {
1105
	global $g, $config;
1106
1107
	if (!isset($g['enableserial_force']) &&
1108
	    !isset($config['system']['enableserial']) &&
1109 23a193da Phil Davis
	    ($g['platform'] == "pfSense" || $g['platform'] == "cdrom" || file_exists("/etc/nano_use_vga.txt"))) {
1110 38c7d42e Renato Botelho
		return false;
1111 23a193da Phil Davis
	}
1112 38c7d42e Renato Botelho
1113
	return true;
1114
}
1115
1116 edb4b657 Renato Botelho
function reload_ttys() {
1117
	// Send a HUP signal to init will make it reload /etc/ttys
1118
	posix_kill(1, SIGHUP);
1119
}
1120
1121 3076becf Scott Ullrich
function print_value_list($list, $count = 10, $separator = ",") {
1122
	$list = implode($separator, array_slice($list, 0, $count));
1123 23a193da Phil Davis
	if (count($list) < $count) {
1124 3076becf Scott Ullrich
		$list .= ".";
1125
	} else {
1126
		$list .= "...";
1127
	}
1128
	return $list;
1129
}
1130
1131 bfe776f0 Ermal Luçi
/* DHCP enabled on any interfaces? */
1132 abdd01f5 Ermal
function is_dhcp_server_enabled() {
1133 db9fabf3 Ermal Luçi
	global $config;
1134 bfe776f0 Ermal Luçi
1135 23a193da Phil Davis
	if (!is_array($config['dhcpd'])) {
1136 bfe776f0 Ermal Luçi
		return false;
1137 23a193da Phil Davis
	}
1138 bfe776f0 Ermal Luçi
1139 abdd01f5 Ermal
	foreach ($config['dhcpd'] as $dhcpif => $dhcpifconf) {
1140 23a193da Phil Davis
		if (isset($dhcpifconf['enable']) && !empty($config['interfaces'][$dhcpif])) {
1141 abdd01f5 Ermal
			return true;
1142 23a193da Phil Davis
		}
1143 3076becf Scott Ullrich
	}
1144 bfe776f0 Ermal Luçi
1145 abdd01f5 Ermal
	return false;
1146 a6610d82 smos
}
1147
1148
/* DHCP enabled on any interfaces? */
1149 abdd01f5 Ermal
function is_dhcpv6_server_enabled() {
1150 a6610d82 smos
	global $config;
1151
1152 abdd01f5 Ermal
	if (is_array($config['interfaces'])) {
1153
		foreach ($config['interfaces'] as $ifcfg) {
1154 23a193da Phil Davis
			if (isset($ifcfg['enable']) && !empty($ifcfg['track6-interface'])) {
1155 abdd01f5 Ermal
				return true;
1156 23a193da Phil Davis
			}
1157 a6610d82 smos
		}
1158
	}
1159
1160 23a193da Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1161 a6610d82 smos
		return false;
1162 23a193da Phil Davis
	}
1163 a6610d82 smos
1164 abdd01f5 Ermal
	foreach ($config['dhcpdv6'] as $dhcpv6if => $dhcpv6ifconf) {
1165 23a193da Phil Davis
		if (isset($dhcpv6ifconf['enable']) && !empty($config['interfaces'][$dhcpv6if])) {
1166 abdd01f5 Ermal
			return true;
1167 23a193da Phil Davis
		}
1168 65b1e7d5 Seth Mos
	}
1169
1170 abdd01f5 Ermal
	return false;
1171 3076becf Scott Ullrich
}
1172
1173 0ed8d746 bcyrill
/* radvd enabled on any interfaces? */
1174
function is_radvd_enabled() {
1175
	global $config;
1176
1177 23a193da Phil Davis
	if (!is_array($config['dhcpdv6'])) {
1178 0ed8d746 bcyrill
		$config['dhcpdv6'] = array();
1179 23a193da Phil Davis
	}
1180 0ed8d746 bcyrill
1181
	$dhcpdv6cfg = $config['dhcpdv6'];
1182
	$Iflist = get_configured_interface_list();
1183
1184
	/* handle manually configured DHCP6 server settings first */
1185
	foreach ($dhcpdv6cfg as $dhcpv6if => $dhcpv6ifconf) {
1186 23a193da Phil Davis
		if (!isset($config['interfaces'][$dhcpv6if]['enable'])) {
1187 0ed8d746 bcyrill
			continue;
1188 23a193da Phil Davis
		}
1189 0ed8d746 bcyrill
1190 23a193da Phil Davis
		if (!isset($dhcpv6ifconf['ramode'])) {
1191 0ed8d746 bcyrill
			$dhcpv6ifconf['ramode'] = $dhcpv6ifconf['mode'];
1192 23a193da Phil Davis
		}
1193 0ed8d746 bcyrill
1194 23a193da Phil Davis
		if ($dhcpv6ifconf['ramode'] == "disabled") {
1195 0ed8d746 bcyrill
			continue;
1196 23a193da Phil Davis
		}
1197 0ed8d746 bcyrill
1198
		$ifcfgipv6 = get_interface_ipv6($dhcpv6if);
1199 23a193da Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
1200 0ed8d746 bcyrill
			continue;
1201 23a193da Phil Davis
		}
1202 0ed8d746 bcyrill
1203
		return true;
1204
	}
1205
1206
	/* handle DHCP-PD prefixes and 6RD dynamic interfaces */
1207
	foreach ($Iflist as $if => $ifdescr) {
1208 23a193da Phil Davis
		if (!isset($config['interfaces'][$if]['track6-interface'])) {
1209 0ed8d746 bcyrill
			continue;
1210 23a193da Phil Davis
		}
1211
		if (!isset($config['interfaces'][$if]['enable'])) {
1212 0ed8d746 bcyrill
			continue;
1213 23a193da Phil Davis
		}
1214 0ed8d746 bcyrill
1215
		$ifcfgipv6 = get_interface_ipv6($if);
1216 23a193da Phil Davis
		if (!is_ipaddrv6($ifcfgipv6)) {
1217 0ed8d746 bcyrill
			continue;
1218 23a193da Phil Davis
		}
1219 0ed8d746 bcyrill
1220
		$ifcfgsnv6 = get_interface_subnetv6($if);
1221
		$subnetv6 = gen_subnetv6($ifcfgipv6, $ifcfgsnv6);
1222
1223 23a193da Phil Davis
		if (!is_ipaddrv6($subnetv6)) {
1224 0ed8d746 bcyrill
			continue;
1225 23a193da Phil Davis
		}
1226 0ed8d746 bcyrill
1227
		return true;
1228
	}
1229
1230
	return false;
1231
}
1232
1233 93c2c1e6 jim-p
/* Any PPPoE servers enabled? */
1234
function is_pppoe_server_enabled() {
1235
	global $config;
1236
1237
	$pppoeenable = false;
1238
1239 23a193da Phil Davis
	if (!is_array($config['pppoes']) || !is_array($config['pppoes']['pppoe'])) {
1240 93c2c1e6 jim-p
		return false;
1241 23a193da Phil Davis
	}
1242 93c2c1e6 jim-p
1243 23a193da Phil Davis
	foreach ($config['pppoes']['pppoe'] as $pppoes) {
1244
		if ($pppoes['mode'] == 'server') {
1245 93c2c1e6 jim-p
			$pppoeenable = true;
1246 23a193da Phil Davis
		}
1247
	}
1248 93c2c1e6 jim-p
1249
	return $pppoeenable;
1250
}
1251
1252 23a193da Phil Davis
function convert_seconds_to_hms($sec) {
1253 63292199 gnhb
	$min=$hrs=0;
1254 23a193da Phil Davis
	if ($sec != 0) {
1255 9ebe7028 gnhb
		$min = floor($sec/60);
1256
		$sec %= 60;
1257
	}
1258 23a193da Phil Davis
	if ($min != 0) {
1259 9ebe7028 gnhb
		$hrs = floor($min/60);
1260
		$min %= 60;
1261
	}
1262 23a193da Phil Davis
	if ($sec < 10) {
1263 9ebe7028 gnhb
		$sec = "0".$sec;
1264 23a193da Phil Davis
	}
1265
	if ($min < 10) {
1266 9ebe7028 gnhb
		$min = "0".$min;
1267 23a193da Phil Davis
	}
1268
	if ($hrs < 10) {
1269 9ebe7028 gnhb
		$hrs = "0".$hrs;
1270 23a193da Phil Davis
	}
1271 9ebe7028 gnhb
	$result = $hrs.":".$min.":".$sec;
1272
	return $result;
1273
}
1274 8eb2f33a Scott Ullrich
1275 63292199 gnhb
/* Compute the total uptime from the ppp uptime log file in the conf directory */
1276
1277 23a193da Phil Davis
function get_ppp_uptime($port) {
1278
	if (file_exists("/conf/{$port}.log")) {
1279 5fa78adc Renato Botelho
		$saved_time = file_get_contents("/conf/{$port}.log");
1280
		$uptime_data = explode("\n",$saved_time);
1281 63292199 gnhb
		$sec=0;
1282 23a193da Phil Davis
		foreach ($uptime_data as $upt) {
1283 63292199 gnhb
			$sec += substr($upt, 1 + strpos($upt, " "));
1284 5fa78adc Renato Botelho
		}
1285 63292199 gnhb
		return convert_seconds_to_hms($sec);
1286
	} else {
1287 7d1b238c Carlos Eduardo Ramos
		$total_time = gettext("No history data found!");
1288 63292199 gnhb
		return $total_time;
1289
	}
1290
}
1291 8eb2f33a Scott Ullrich
1292 6189988d Scott Dale
//returns interface information
1293
function get_interface_info($ifdescr) {
1294 cffe41cb Ermal
	global $config, $g;
1295 6189988d Scott Dale
1296
	$ifinfo = array();
1297 23a193da Phil Davis
	if (empty($config['interfaces'][$ifdescr])) {
1298 67ee1ec5 Ermal Luçi
		return;
1299 23a193da Phil Davis
	}
1300 ebdbdbc2 gnhb
	$ifinfo['hwif'] = $config['interfaces'][$ifdescr]['if'];
1301 cffe41cb Ermal
	$ifinfo['if'] = get_real_interface($ifdescr);
1302 6189988d Scott Dale
1303 cb074893 Ermal Lu?i
	$chkif = $ifinfo['if'];
1304
	$ifinfotmp = pfSense_get_interface_addresses($chkif);
1305
	$ifinfo['status'] = $ifinfotmp['status'];
1306 23a193da Phil Davis
	if (empty($ifinfo['status'])) {
1307 5fa78adc Renato Botelho
		$ifinfo['status'] = "down";
1308 23a193da Phil Davis
	}
1309 cb074893 Ermal Lu?i
	$ifinfo['macaddr'] = $ifinfotmp['macaddr'];
1310 2d2e466c Ermal LUÇI
	$ifinfo['mtu'] = $ifinfotmp['mtu'];
1311 cb074893 Ermal Lu?i
	$ifinfo['ipaddr'] = $ifinfotmp['ipaddr'];
1312
	$ifinfo['subnet'] = $ifinfotmp['subnet'];
1313 58418355 smos
	$ifinfo['linklocal'] = get_interface_linklocal($ifdescr);
1314 15cc0894 Seth Mos
	$ifinfo['ipaddrv6'] = get_interface_ipv6($ifdescr);
1315
	$ifinfo['subnetv6'] = get_interface_subnetv6($ifdescr);
1316 23a193da Phil Davis
	if (isset($ifinfotmp['link0'])) {
1317 cb074893 Ermal Lu?i
		$link0 = "down";
1318 23a193da Phil Davis
	}
1319 cffe41cb Ermal
	$ifinfotmp = pfSense_get_interface_stats($chkif);
1320 5fa78adc Renato Botelho
	// $ifinfo['inpkts'] = $ifinfotmp['inpkts'];
1321
	// $ifinfo['outpkts'] = $ifinfotmp['outpkts'];
1322
	$ifinfo['inerrs'] = $ifinfotmp['inerrs'];
1323
	$ifinfo['outerrs'] = $ifinfotmp['outerrs'];
1324
	$ifinfo['collisions'] = $ifinfotmp['collisions'];
1325 6189988d Scott Dale
1326 01385b0c Scott Ullrich
	/* Use pfctl for non wrapping 64 bit counters */
1327 b5a8483c Seth Mos
	/* Pass */
1328 cb074893 Ermal Lu?i
	exec("/sbin/pfctl -vvsI -i {$chkif}", $pfctlstats);
1329 971eaab5 Seth Mos
	$pf_in4_pass = preg_split("/ +/ ", $pfctlstats[3]);
1330
	$pf_out4_pass = preg_split("/ +/", $pfctlstats[5]);
1331 15cc0894 Seth Mos
	$pf_in6_pass = preg_split("/ +/ ", $pfctlstats[7]);
1332
	$pf_out6_pass = preg_split("/ +/", $pfctlstats[9]);
1333 971eaab5 Seth Mos
	$in4_pass = $pf_in4_pass[5];
1334
	$out4_pass = $pf_out4_pass[5];
1335
	$in4_pass_packets = $pf_in4_pass[3];
1336
	$out4_pass_packets = $pf_out4_pass[3];
1337 15cc0894 Seth Mos
	$in6_pass = $pf_in6_pass[5];
1338
	$out6_pass = $pf_out6_pass[5];
1339
	$in6_pass_packets = $pf_in6_pass[3];
1340
	$out6_pass_packets = $pf_out6_pass[3];
1341
	$ifinfo['inbytespass'] = $in4_pass + $in6_pass;
1342
	$ifinfo['outbytespass'] = $out4_pass + $out6_pass;
1343
	$ifinfo['inpktspass'] = $in4_pass_packets + $in6_pass_packets;
1344 4bdfa5dd Phil Davis
	$ifinfo['outpktspass'] = $out4_pass_packets + $out6_pass_packets;
1345 01385b0c Scott Ullrich
1346 971eaab5 Seth Mos
	/* Block */
1347
	$pf_in4_block = preg_split("/ +/", $pfctlstats[4]);
1348
	$pf_out4_block = preg_split("/ +/", $pfctlstats[6]);
1349 15cc0894 Seth Mos
	$pf_in6_block = preg_split("/ +/", $pfctlstats[8]);
1350
	$pf_out6_block = preg_split("/ +/", $pfctlstats[10]);
1351 971eaab5 Seth Mos
	$in4_block = $pf_in4_block[5];
1352
	$out4_block = $pf_out4_block[5];
1353
	$in4_block_packets = $pf_in4_block[3];
1354
	$out4_block_packets = $pf_out4_block[3];
1355 15cc0894 Seth Mos
	$in6_block = $pf_in6_block[5];
1356
	$out6_block = $pf_out6_block[5];
1357
	$in6_block_packets = $pf_in6_block[3];
1358
	$out6_block_packets = $pf_out6_block[3];
1359
	$ifinfo['inbytesblock'] = $in4_block + $in6_block;
1360
	$ifinfo['outbytesblock'] = $out4_block + $out6_block;
1361
	$ifinfo['inpktsblock'] = $in4_block_packets + $in6_block_packets;
1362
	$ifinfo['outpktsblock'] = $out4_block_packets + $out6_block_packets;
1363
1364
	$ifinfo['inbytes'] = $in4_pass + $in6_pass;
1365
	$ifinfo['outbytes'] = $out4_pass + $out6_pass;
1366
	$ifinfo['inpkts'] = $in4_pass_packets + $in6_pass_packets;
1367 4bdfa5dd Phil Davis
	$ifinfo['outpkts'] = $out4_pass_packets + $out6_pass_packets;
1368 5fa78adc Renato Botelho
1369 63161b3f Ermal Luçi
	$ifconfiginfo = "";
1370 59db783a gnhb
	$link_type = $config['interfaces'][$ifdescr]['ipaddr'];
1371
	switch ($link_type) {
1372 23a193da Phil Davis
		/* DHCP? -> see if dhclient is up */
1373
		case "dhcp":
1374
			/* see if dhclient is up */
1375
			if (find_dhclient_process($ifinfo['if']) != 0) {
1376
				$ifinfo['dhcplink'] = "up";
1377
			} else {
1378
				$ifinfo['dhcplink'] = "down";
1379 badbe349 gnhb
			}
1380 23a193da Phil Davis
1381 611ae852 Ermal
			break;
1382 23a193da Phil Davis
		/* PPPoE/PPTP/L2TP interface? -> get status from virtual interface */
1383
		case "pppoe":
1384
		case "pptp":
1385
		case "l2tp":
1386
			if ($ifinfo['status'] == "up" && !isset($link0)) {
1387
				/* get PPPoE link status for dial on demand */
1388
				$ifinfo["{$link_type}link"] = "up";
1389
			} else {
1390
				$ifinfo["{$link_type}link"] = "down";
1391 4adf752c smos
			}
1392 23a193da Phil Davis
1393
			break;
1394
		/* PPP interface? -> get uptime for this session and cumulative uptime from the persistent log file in conf */
1395
		case "ppp":
1396
			if ($ifinfo['status'] == "up") {
1397
				$ifinfo['ppplink'] = "up";
1398
			} else {
1399
				$ifinfo['ppplink'] = "down" ;
1400 4adf752c smos
			}
1401 23a193da Phil Davis
1402
			if (empty($ifinfo['status'])) {
1403
				$ifinfo['status'] = "down";
1404
			}
1405
1406
			if (is_array($config['ppps']['ppp']) && count($config['ppps']['ppp'])) {
1407
				foreach ($config['ppps']['ppp'] as $pppid => $ppp) {
1408
					if ($config['interfaces'][$ifdescr]['if'] == $ppp['if']) {
1409
						break;
1410
					}
1411
				}
1412
			}
1413
			$dev = $ppp['ports'];
1414
			if ($config['interfaces'][$ifdescr]['if'] != $ppp['if'] || empty($dev)) {
1415
				break;
1416
			}
1417
			if (!file_exists($dev)) {
1418
				$ifinfo['nodevice'] = 1;
1419
				$ifinfo['pppinfo'] = $dev . " " . gettext("device not present! Is the modem attached to the system?");
1420
			}
1421
1422
			$usbmodemoutput = array();
1423
			exec("usbconfig", $usbmodemoutput);
1424
			$mondev = "{$g['tmp_path']}/3gstats.{$ifdescr}";
1425
			if (file_exists($mondev)) {
1426
				$cellstats = file($mondev);
1427
				/* skip header */
1428
				$a_cellstats = explode(",", $cellstats[1]);
1429
				if (preg_match("/huawei/i", implode("\n", $usbmodemoutput))) {
1430
					$ifinfo['cell_rssi'] = huawei_rssi_to_string($a_cellstats[1]);
1431
					$ifinfo['cell_mode'] = huawei_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1432
					$ifinfo['cell_simstate'] = huawei_simstate_to_string($a_cellstats[10]);
1433
					$ifinfo['cell_service'] = huawei_service_to_string(trim($a_cellstats[11]));
1434
				}
1435
				if (preg_match("/zte/i", implode("\n", $usbmodemoutput))) {
1436
					$ifinfo['cell_rssi'] = zte_rssi_to_string($a_cellstats[1]);
1437
					$ifinfo['cell_mode'] = zte_mode_to_string($a_cellstats[2], $a_cellstats[3]);
1438
					$ifinfo['cell_simstate'] = zte_simstate_to_string($a_cellstats[10]);
1439
					$ifinfo['cell_service'] = zte_service_to_string(trim($a_cellstats[11]));
1440
				}
1441
				$ifinfo['cell_upstream'] = $a_cellstats[4];
1442
				$ifinfo['cell_downstream'] = trim($a_cellstats[5]);
1443
				$ifinfo['cell_sent'] = $a_cellstats[6];
1444
				$ifinfo['cell_received'] = trim($a_cellstats[7]);
1445
				$ifinfo['cell_bwupstream'] = $a_cellstats[8];
1446
				$ifinfo['cell_bwdownstream'] = trim($a_cellstats[9]);
1447
			}
1448
			// Calculate cumulative uptime for PPP link. Useful for connections that have per minute/hour contracts so you don't go over!
1449
			if (isset($ppp['uptime'])) {
1450
				$ifinfo['ppp_uptime_accumulated'] = "(".get_ppp_uptime($ifinfo['if']).")";
1451
			}
1452
			break;
1453
		default:
1454
			break;
1455 6189988d Scott Dale
	}
1456 5fa78adc Renato Botelho
1457 59db783a gnhb
	if (file_exists("{$g['varrun_path']}/{$link_type}_{$ifdescr}.pid")) {
1458
		$sec = trim(`/usr/local/sbin/ppp-uptime.sh {$ifinfo['if']}`);
1459
		$ifinfo['ppp_uptime'] = convert_seconds_to_hms($sec);
1460
	}
1461 5fa78adc Renato Botelho
1462 6189988d Scott Dale
	if ($ifinfo['status'] == "up") {
1463
		/* try to determine media with ifconfig */
1464
		unset($ifconfiginfo);
1465 818a6b7d Seth Mos
		exec("/sbin/ifconfig " . $ifinfo['if'], $ifconfiginfo);
1466
		$wifconfiginfo = array();
1467 23a193da Phil Davis
		if (is_interface_wireless($ifdescr)) {
1468 818a6b7d Seth Mos
			exec("/sbin/ifconfig {$ifinfo['if']} list sta", $wifconfiginfo);
1469
			array_shift($wifconfiginfo);
1470
		}
1471 6189988d Scott Dale
		$matches = "";
1472
		foreach ($ifconfiginfo as $ici) {
1473
1474
			/* don't list media/speed for wireless cards, as it always
1475
			   displays 2 Mbps even though clients can connect at 11 Mbps */
1476
			if (preg_match("/media: .*? \((.*?)\)/", $ici, $matches)) {
1477
				$ifinfo['media'] = $matches[1];
1478
			} else if (preg_match("/media: Ethernet (.*)/", $ici, $matches)) {
1479
				$ifinfo['media'] = $matches[1];
1480
			} else if (preg_match("/media: IEEE 802.11 Wireless Ethernet (.*)/", $ici, $matches)) {
1481
				$ifinfo['media'] = $matches[1];
1482
			}
1483
1484
			if (preg_match("/status: (.*)$/", $ici, $matches)) {
1485 23a193da Phil Davis
				if ($matches[1] != "active") {
1486 6189988d Scott Dale
					$ifinfo['status'] = $matches[1];
1487 23a193da Phil Davis
				}
1488
				if ($ifinfo['status'] == gettext("running")) {
1489 7d1b238c Carlos Eduardo Ramos
					$ifinfo['status'] = gettext("up");
1490 23a193da Phil Davis
				}
1491 6189988d Scott Dale
			}
1492
			if (preg_match("/channel (\S*)/", $ici, $matches)) {
1493
				$ifinfo['channel'] = $matches[1];
1494
			}
1495
			if (preg_match("/ssid (\".*?\"|\S*)/", $ici, $matches)) {
1496 23a193da Phil Davis
				if ($matches[1][0] == '"') {
1497 6189988d Scott Dale
					$ifinfo['ssid'] = substr($matches[1], 1, -1);
1498 23a193da Phil Davis
				}
1499
				else {
1500 6189988d Scott Dale
					$ifinfo['ssid'] = $matches[1];
1501 23a193da Phil Davis
				}
1502 6189988d Scott Dale
			}
1503 0b29093b jim-p
			if (preg_match("/laggproto (.*)$/", $ici, $matches)) {
1504
				$ifinfo['laggproto'] = $matches[1];
1505
			}
1506
			if (preg_match("/laggport: (.*)$/", $ici, $matches)) {
1507
				$ifinfo['laggport'][] = $matches[1];
1508
			}
1509 6189988d Scott Dale
		}
1510 23a193da Phil Davis
		foreach ($wifconfiginfo as $ici) {
1511 818a6b7d Seth Mos
			$elements = preg_split("/[ ]+/i", $ici);
1512
			if ($elements[0] != "") {
1513
				$ifinfo['bssid'] = $elements[0];
1514
			}
1515
			if ($elements[3] != "") {
1516
				$ifinfo['rate'] = $elements[3];
1517
			}
1518
			if ($elements[4] != "") {
1519
				$ifinfo['rssi'] = $elements[4];
1520
			}
1521
		}
1522 67ee1ec5 Ermal Luçi
		/* lookup the gateway */
1523 2bbb79cb Seth Mos
		if (interface_has_gateway($ifdescr)) {
1524 ebdbdbc2 gnhb
			$ifinfo['gateway'] = get_interface_gateway($ifdescr);
1525 2bbb79cb Seth Mos
			$ifinfo['gatewayv6'] = get_interface_gateway_v6($ifdescr);
1526
		}
1527 6189988d Scott Dale
	}
1528
1529
	$bridge = "";
1530 7ec05d27 Ermal Luçi
	$bridge = link_interface_to_bridge($ifdescr);
1531 23a193da Phil Davis
	if ($bridge) {
1532 6189988d Scott Dale
		$bridge_text = `/sbin/ifconfig {$bridge}`;
1533 23a193da Phil Davis
		if (stristr($bridge_text, "blocking") <> false) {
1534 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = "<b><font color='red'>" . gettext("blocking") . "</font></b> - " . gettext("check for ethernet loops");
1535 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1536 23a193da Phil Davis
		} else if (stristr($bridge_text, "learning") <> false) {
1537 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = gettext("learning");
1538 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1539 23a193da Phil Davis
		} else if (stristr($bridge_text, "forwarding") <> false) {
1540 7d1b238c Carlos Eduardo Ramos
			$ifinfo['bridge'] = gettext("forwarding");
1541 6189988d Scott Dale
			$ifinfo['bridgeint'] = $bridge;
1542
		}
1543
	}
1544
1545
	return $ifinfo;
1546
}
1547
1548
//returns cpu speed of processor. Good for determining capabilities of machine
1549
function get_cpu_speed() {
1550 971de1f9 Renato Botelho
	return get_single_sysctl("hw.clockrate");
1551 6189988d Scott Dale
}
1552 fab7ff44 Bill Marquette
1553 df0cb10b Phil Davis
function get_uptime_sec() {
1554
	$boottime = "";
1555
	$matches = "";
1556 971de1f9 Renato Botelho
	$boottime = get_single_sysctl("kern.boottime");
1557
	preg_match("/sec = (\d+)/", $boottime, $matches);
1558 df0cb10b Phil Davis
	$boottime = $matches[1];
1559 23a193da Phil Davis
	if (intval($boottime) == 0) {
1560 df0cb10b Phil Davis
		return 0;
1561 23a193da Phil Davis
	}
1562 df0cb10b Phil Davis
1563
	$uptime = time() - $boottime;
1564
	return $uptime;
1565
}
1566
1567 a5f94f14 Scott Ullrich
function add_hostname_to_watch($hostname) {
1568 23a193da Phil Davis
	if (!is_dir("/var/db/dnscache")) {
1569 c941ea1c Seth Mos
		mkdir("/var/db/dnscache");
1570
	}
1571 2d0c5e3e Renato Botelho
	$result = array();
1572 23a193da Phil Davis
	if ((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1573 581e772e Seth Mos
		$domrecords = array();
1574
		$domips = array();
1575 873c1701 Renato Botelho
		exec("host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1576 23a193da Phil Davis
		if ($rethost == 0) {
1577
			foreach ($domrecords as $domr) {
1578 581e772e Seth Mos
				$doml = explode(" ", $domr);
1579
				$domip = $doml[3];
1580
				/* fill array with domain ip addresses */
1581 23a193da Phil Davis
				if (is_ipaddr($domip)) {
1582 581e772e Seth Mos
					$domips[] = $domip;
1583
				}
1584
			}
1585
		}
1586
		sort($domips);
1587
		$contents = "";
1588 23a193da Phil Davis
		if (!empty($domips)) {
1589
			foreach ($domips as $ip) {
1590 162c059e Seth Mos
				$contents .= "$ip\n";
1591
			}
1592 581e772e Seth Mos
		}
1593
		file_put_contents("/var/db/dnscache/$hostname", $contents);
1594 aa57f965 Renato Botelho
		/* Remove empty elements */
1595
		$result = array_filter(explode("\n", $contents), 'strlen');
1596 a5f94f14 Scott Ullrich
	}
1597 2d0c5e3e Renato Botelho
	return $result;
1598 a5f94f14 Scott Ullrich
}
1599
1600 5ed54b93 Seth Mos
function is_fqdn($fqdn) {
1601
	$hostname = false;
1602 23a193da Phil Davis
	if (preg_match("/[-A-Z0-9\.]+\.[-A-Z0-9\.]+/i", $fqdn)) {
1603 5ed54b93 Seth Mos
		$hostname = true;
1604
	}
1605 23a193da Phil Davis
	if (preg_match("/\.\./", $fqdn)) {
1606 5ed54b93 Seth Mos
		$hostname = false;
1607
	}
1608 23a193da Phil Davis
	if (preg_match("/^\./i", $fqdn)) {
1609 5ed54b93 Seth Mos
		$hostname = false;
1610
	}
1611 23a193da Phil Davis
	if (preg_match("/\//i", $fqdn)) {
1612 c941ea1c Seth Mos
		$hostname = false;
1613
	}
1614 5ed54b93 Seth Mos
	return($hostname);
1615
}
1616
1617 639aaa95 Bill Marquette
function pfsense_default_state_size() {
1618 5fa78adc Renato Botelho
	/* get system memory amount */
1619
	$memory = get_memory();
1620 386758bb Phil Davis
	$physmem = $memory[0];
1621 5fa78adc Renato Botelho
	/* Be cautious and only allocate 10% of system memory to the state table */
1622 386758bb Phil Davis
	$max_states = (int) ($physmem/10)*1000;
1623 5fa78adc Renato Botelho
	return $max_states;
1624 639aaa95 Bill Marquette
}
1625
1626 84aea606 jim-p
function pfsense_default_tables_size() {
1627
	$current = `pfctl -sm | grep ^tables | awk '{print $4};'`;
1628
	return $current;
1629
}
1630
1631 fb586a16 jim-p
function pfsense_default_table_entries_size() {
1632
	$current = `pfctl -sm | grep table-entries | awk '{print $4};'`;
1633
	return $current;
1634
}
1635
1636 7723c7e0 Seth Mos
/* Compare the current hostname DNS to the DNS cache we made
1637
 * if it has changed we return the old records
1638 046b8ba6 Renato Botelho
 * if no change we return false */
1639 7723c7e0 Seth Mos
function compare_hostname_to_dnscache($hostname) {
1640 23a193da Phil Davis
	if (!is_dir("/var/db/dnscache")) {
1641 7723c7e0 Seth Mos
		mkdir("/var/db/dnscache");
1642
	}
1643
	$hostname = trim($hostname);
1644 23a193da Phil Davis
	if (is_readable("/var/db/dnscache/{$hostname}")) {
1645 7723c7e0 Seth Mos
		$oldcontents = file_get_contents("/var/db/dnscache/{$hostname}");
1646
	} else {
1647
		$oldcontents = "";
1648
	}
1649 23a193da Phil Davis
	if ((is_fqdn($hostname)) && (!is_ipaddr($hostname))) {
1650 7723c7e0 Seth Mos
		$domrecords = array();
1651
		$domips = array();
1652 873c1701 Renato Botelho
		exec("host -t A " . escapeshellarg($hostname), $domrecords, $rethost);
1653 23a193da Phil Davis
		if ($rethost == 0) {
1654
			foreach ($domrecords as $domr) {
1655 7723c7e0 Seth Mos
				$doml = explode(" ", $domr);
1656
				$domip = $doml[3];
1657
				/* fill array with domain ip addresses */
1658 23a193da Phil Davis
				if (is_ipaddr($domip)) {
1659 7723c7e0 Seth Mos
					$domips[] = $domip;
1660
				}
1661
			}
1662
		}
1663
		sort($domips);
1664
		$contents = "";
1665 23a193da Phil Davis
		if (!empty($domips)) {
1666
			foreach ($domips as $ip) {
1667 7723c7e0 Seth Mos
				$contents .= "$ip\n";
1668
			}
1669
		}
1670
	}
1671
1672 23a193da Phil Davis
	if (trim($oldcontents) != trim($contents)) {
1673
		if ($g['debug']) {
1674 addc0439 Renato Botelho
			log_error(sprintf(gettext('DNSCACHE: Found old IP %1$s and new IP %2$s'), $oldcontents, $contents));
1675 a5f91ef4 Seth Mos
		}
1676 7723c7e0 Seth Mos
		return ($oldcontents);
1677
	} else {
1678
		return false;
1679
	}
1680
}
1681
1682 09f18f59 jim-p
/*
1683 7530177c jim-p
 * load_crypto() - Load crypto modules if enabled in config.
1684 09f18f59 jim-p
 */
1685 7530177c jim-p
function load_crypto() {
1686 09f18f59 jim-p
	global $config, $g;
1687 7530177c jim-p
	$crypto_modules = array('glxsb', 'aesni');
1688
1689 23a193da Phil Davis
	if (!in_array($config['system']['crypto_hardware'], $crypto_modules)) {
1690 7530177c jim-p
		return false;
1691 23a193da Phil Davis
	}
1692 7530177c jim-p
1693 3d74b803 jim-p
	if (!empty($config['system']['crypto_hardware']) && !is_module_loaded($config['system']['crypto_hardware'])) {
1694 7530177c jim-p
		log_error("Loading {$config['system']['crypto_hardware']} cryptographic accelerator module.");
1695
		mwexec("/sbin/kldload {$config['system']['crypto_hardware']}");
1696 09f18f59 jim-p
	}
1697
}
1698
1699 f60156f6 jim-p
/*
1700
 * load_thermal_hardware() - Load temperature monitor kernel module
1701
 */
1702
function load_thermal_hardware() {
1703
	global $config, $g;
1704
	$thermal_hardware_modules = array('coretemp', 'amdtemp');
1705
1706 23a193da Phil Davis
	if (!in_array($config['system']['thermal_hardware'], $thermal_hardware_modules)) {
1707 f60156f6 jim-p
		return false;
1708 23a193da Phil Davis
	}
1709 f60156f6 jim-p
1710 3d74b803 jim-p
	if (!empty($config['system']['thermal_hardware']) && !is_module_loaded($config['system']['thermal_hardware'])) {
1711 f60156f6 jim-p
		log_error("Loading {$config['system']['thermal_hardware']} thermal monitor module.");
1712
		mwexec("/sbin/kldload {$config['system']['thermal_hardware']}");
1713
	}
1714
}
1715
1716 cde4f5d3 Scott Ullrich
/****f* pfsense-utils/isvm
1717
 * NAME
1718
 *   isvm
1719
 * INPUTS
1720 c96e71d1 Renato Botelho
 *	none
1721 cde4f5d3 Scott Ullrich
 * RESULT
1722
 *   returns true if machine is running under a virtual environment
1723
 ******/
1724
function isvm() {
1725 7e36f71c Renato Botelho
	$virtualenvs = array("vmware", "parallels", "qemu", "bochs", "plex86", "VirtualBox");
1726
	$_gb = exec('/bin/kenv smbios.system.product 2>/dev/null', $output, $rc);
1727
1728 23a193da Phil Davis
	if ($rc != 0 || !isset($output[0])) {
1729 7e36f71c Renato Botelho
		return false;
1730 23a193da Phil Davis
	}
1731 7e36f71c Renato Botelho
1732 23a193da Phil Davis
	foreach ($virtualenvs as $virtualenv) {
1733
		if (stripos($output[0], $virtualenv) !== false) {
1734 58897b8c Warren Baker
			return true;
1735 23a193da Phil Davis
		}
1736
	}
1737 58897b8c Warren Baker
1738
	return false;
1739 cde4f5d3 Scott Ullrich
}
1740
1741 e0d0eb71 Scott Ullrich
function get_freebsd_version() {
1742 54597012 Renato Botelho
	$version = explode(".", php_uname("r"));
1743
	return $version[0];
1744 e0d0eb71 Scott Ullrich
}
1745
1746 91957c42 Chris Buechler
function download_file($url, $destination, $verify_ssl = true, $connect_timeout = 60, $timeout = 0) {
1747 ffd7802a Renato Botelho
	global $config, $g;
1748
1749
	$fp = fopen($destination, "wb");
1750
1751 23a193da Phil Davis
	if (!$fp) {
1752 ffd7802a Renato Botelho
		return false;
1753 23a193da Phil Davis
	}
1754 ffd7802a Renato Botelho
1755
	$ch = curl_init();
1756
	curl_setopt($ch, CURLOPT_URL, $url);
1757
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $verify_ssl);
1758
	curl_setopt($ch, CURLOPT_FILE, $fp);
1759
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1760
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1761
	curl_setopt($ch, CURLOPT_HEADER, false);
1762
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1763
	curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
1764
1765
	if (!empty($config['system']['proxyurl'])) {
1766
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1767 23a193da Phil Davis
		if (!empty($config['system']['proxyport'])) {
1768 ffd7802a Renato Botelho
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1769 23a193da Phil Davis
		}
1770 ffd7802a Renato Botelho
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1771
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1772
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1773
		}
1774
	}
1775
1776
	@curl_exec($ch);
1777
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1778
	fclose($fp);
1779
	curl_close($ch);
1780
	return ($http_code == 200) ? true : $http_code;
1781
}
1782
1783 d164643a jim-p
function download_file_with_progress_bar($url_file, $destination_file, $readbody = 'read_body', $connect_timeout=60, $timeout=0) {
1784 b051f148 Chris Buechler
	global $ch, $fout, $file_size, $downloaded, $config, $first_progress_update, $g;
1785 5fa78adc Renato Botelho
	$file_size  = 1;
1786
	$downloaded = 1;
1787 e961bd67 phildd
	$first_progress_update = TRUE;
1788 5fa78adc Renato Botelho
	/* open destination file */
1789
	$fout = fopen($destination_file, "wb");
1790
1791
	/*
1792
	 *      Originally by Author: Keyvan Minoukadeh
1793
	 *      Modified by Scott Ullrich to return Content-Length size
1794
	 */
1795
1796
	$ch = curl_init();
1797
	curl_setopt($ch, CURLOPT_URL, $url_file);
1798
	curl_setopt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
1799
	curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
1800
	curl_setopt($ch, CURLOPT_WRITEFUNCTION, $readbody);
1801
	curl_setopt($ch, CURLOPT_NOPROGRESS, '1');
1802
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $connect_timeout);
1803
	curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
1804 0ae4f3fa Manuel Silvoso
	curl_setopt($ch, CURLOPT_USERAGENT, $g['product_name'] . '/' . rtrim(file_get_contents("/etc/version")));
1805 b31da21e Scott Ullrich
1806 42c07003 Ermal
	if (!empty($config['system']['proxyurl'])) {
1807
		curl_setopt($ch, CURLOPT_PROXY, $config['system']['proxyurl']);
1808 23a193da Phil Davis
		if (!empty($config['system']['proxyport'])) {
1809 42c07003 Ermal
			curl_setopt($ch, CURLOPT_PROXYPORT, $config['system']['proxyport']);
1810 23a193da Phil Davis
		}
1811 42c07003 Ermal
		if (!empty($config['system']['proxyuser']) && !empty($config['system']['proxypass'])) {
1812
			@curl_setopt($ch, CURLOPT_PROXYAUTH, CURLAUTH_ANY | CURLAUTH_ANYSAFE);
1813 2a57a4d1 Ermal
			curl_setopt($ch, CURLOPT_PROXYUSERPWD, "{$config['system']['proxyuser']}:{$config['system']['proxypass']}");
1814 42c07003 Ermal
		}
1815
	}
1816
1817 5fa78adc Renato Botelho
	@curl_exec($ch);
1818
	$http_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
1819 23a193da Phil Davis
	if ($fout) {
1820 5fa78adc Renato Botelho
		fclose($fout);
1821 23a193da Phil Davis
	}
1822 5fa78adc Renato Botelho
	curl_close($ch);
1823
	return ($http_code == 200) ? true : $http_code;
1824 b31da21e Scott Ullrich
}
1825
1826
function read_header($ch, $string) {
1827 5fa78adc Renato Botelho
	global $file_size, $fout;
1828
	$length = strlen($string);
1829
	$regs = "";
1830
	preg_match("/(Content-Length:) (.*)/", $string, $regs);
1831 23a193da Phil Davis
	if ($regs[2] <> "") {
1832 5fa78adc Renato Botelho
		$file_size = intval($regs[2]);
1833
	}
1834
	ob_flush();
1835
	return $length;
1836 b31da21e Scott Ullrich
}
1837
1838
function read_body($ch, $string) {
1839 5fa78adc Renato Botelho
	global $fout, $file_size, $downloaded, $sendto, $static_status, $static_output, $lastseen, $first_progress_update;
1840
	global $pkg_interface;
1841
	$length = strlen($string);
1842
	$downloaded += intval($length);
1843 23a193da Phil Davis
	if ($file_size > 0) {
1844 5fa78adc Renato Botelho
		$downloadProgress = round(100 * (1 - $downloaded / $file_size), 0);
1845
		$downloadProgress = 100 - $downloadProgress;
1846 23a193da Phil Davis
	} else {
1847 5fa78adc Renato Botelho
		$downloadProgress = 0;
1848 23a193da Phil Davis
	}
1849
	if ($lastseen <> $downloadProgress and $downloadProgress < 101) {
1850
		if ($sendto == "status") {
1851
			if ($pkg_interface == "console") {
1852
				if (($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1853 03b2cab6 Ermal
					$tostatus = $static_status . $downloadProgress . "%";
1854 2a315bee Phil Davis
					if ($downloadProgress == 100) {
1855 a3da8f50 Ermal
						$tostatus = $tostatus . "\r";
1856 2a315bee Phil Davis
					}
1857 03b2cab6 Ermal
					update_status($tostatus);
1858
				}
1859
			} else {
1860
				$tostatus = $static_status . $downloadProgress . "%";
1861 5fa78adc Renato Botelho
				update_status($tostatus);
1862 03b2cab6 Ermal
			}
1863 5fa78adc Renato Botelho
		} else {
1864 23a193da Phil Davis
			if ($pkg_interface == "console") {
1865
				if (($downloadProgress % 10) == 0 || $downloadProgress < 10) {
1866 03b2cab6 Ermal
					$tooutput = $static_output . $downloadProgress . "%";
1867 2a315bee Phil Davis
					if ($downloadProgress == 100) {
1868 a3da8f50 Ermal
						$tooutput = $tooutput . "\r";
1869 2a315bee Phil Davis
					}
1870 03b2cab6 Ermal
					update_output_window($tooutput);
1871
				}
1872
			} else {
1873
				$tooutput = $static_output . $downloadProgress . "%";
1874
				update_output_window($tooutput);
1875
			}
1876 5fa78adc Renato Botelho
		}
1877 23a193da Phil Davis
		if (($pkg_interface != "console") || (($downloadProgress % 10) == 0) || ($downloadProgress < 10)) {
1878
			update_progress_bar($downloadProgress, $first_progress_update);
1879
			$first_progress_update = FALSE;
1880
		}
1881 5fa78adc Renato Botelho
		$lastseen = $downloadProgress;
1882
	}
1883 23a193da Phil Davis
	if ($fout) {
1884 5fa78adc Renato Botelho
		fwrite($fout, $string);
1885 23a193da Phil Davis
	}
1886 5fa78adc Renato Botelho
	ob_flush();
1887
	return $length;
1888 b31da21e Scott Ullrich
}
1889
1890 84677257 Scott Ullrich
/*
1891
 *   update_output_window: update bottom textarea dynamically.
1892
 */
1893
function update_output_window($text) {
1894 5fa78adc Renato Botelho
	global $pkg_interface;
1895
	$log = preg_replace("/\n/", "\\n", $text);
1896 23a193da Phil Davis
	if ($pkg_interface != "console") {
1897 6e4e6286 Colin Fleming
		echo "\n<script type=\"text/javascript\">";
1898
		echo "\n//<![CDATA[";
1899
		echo "\nthis.document.forms[0].output.value = \"" . $log . "\";";
1900
		echo "\nthis.document.forms[0].output.scrollTop = this.document.forms[0].output.scrollHeight;";
1901
		echo "\n//]]>";
1902
		echo "\n</script>";
1903 5fa78adc Renato Botelho
	}
1904
	/* ensure that contents are written out */
1905
	ob_flush();
1906 84677257 Scott Ullrich
}
1907
1908
/*
1909 82acb8b3 Phil Davis
 *   update_status: update top textarea dynamically.
1910 84677257 Scott Ullrich
 */
1911
function update_status($status) {
1912 5fa78adc Renato Botelho
	global $pkg_interface;
1913 23a193da Phil Davis
	if ($pkg_interface == "console") {
1914 5fa78adc Renato Botelho
		echo "\r{$status}";
1915
	} else {
1916 6e4e6286 Colin Fleming
		echo "\n<script type=\"text/javascript\">";
1917
		echo "\n//<![CDATA[";
1918
		echo "\nthis.document.forms[0].status.value=\"" . $status . "\";";
1919
		echo "\n//]]>";
1920
		echo "\n</script>";
1921 5fa78adc Renato Botelho
	}
1922
	/* ensure that contents are written out */
1923
	ob_flush();
1924 84677257 Scott Ullrich
}
1925
1926
/*
1927 e961bd67 phildd
 * update_progress_bar($percent, $first_time): updates the javascript driven progress bar.
1928 84677257 Scott Ullrich
 */
1929 e961bd67 phildd
function update_progress_bar($percent, $first_time) {
1930 5fa78adc Renato Botelho
	global $pkg_interface;
1931 23a193da Phil Davis
	if ($percent > 100) {
1932
		$percent = 1;
1933
	}
1934
	if ($pkg_interface <> "console") {
1935 6e4e6286 Colin Fleming
		echo "\n<script type=\"text/javascript\">";
1936
		echo "\n//<![CDATA[";
1937 5fa78adc Renato Botelho
		echo "\ndocument.progressbar.style.width='" . $percent . "%';";
1938 6e4e6286 Colin Fleming
		echo "\n//]]>";
1939 5fa78adc Renato Botelho
		echo "\n</script>";
1940
	} else {
1941 23a193da Phil Davis
		if (!($first_time)) {
1942 e961bd67 phildd
			echo "\x08\x08\x08\x08\x08";
1943 23a193da Phil Davis
		}
1944 e961bd67 phildd
		echo sprintf("%4d%%", $percent);
1945 5fa78adc Renato Botelho
	}
1946 84677257 Scott Ullrich
}
1947
1948 f5d637bc Scott Ullrich
/* 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. */
1949 23a193da Phil Davis
if (!function_exists("split")) {
1950 5aa68a55 Renato Botelho
	function split($separator, $haystack, $limit = null) {
1951
		log_error("deprecated split() call with separator '{$separator}'");
1952
		return preg_split($separator, $haystack, $limit);
1953 f5d637bc Scott Ullrich
	}
1954
}
1955
1956 f1ac1733 Erik Fonnesbeck
function update_alias_names_upon_change($section, $field, $new_alias_name, $origname) {
1957 978fd2e8 Scott Ullrich
	global $g, $config, $pconfig, $debug;
1958 23a193da Phil Davis
	if (!$origname) {
1959 b6db8ea3 sullrich
		return;
1960 23a193da Phil Davis
	}
1961 b6db8ea3 sullrich
1962 f1ac1733 Erik Fonnesbeck
	$sectionref = &$config;
1963 23a193da Phil Davis
	foreach ($section as $sectionname) {
1964
		if (is_array($sectionref) && isset($sectionref[$sectionname])) {
1965 f1ac1733 Erik Fonnesbeck
			$sectionref = &$sectionref[$sectionname];
1966 23a193da Phil Davis
		} else {
1967 f1ac1733 Erik Fonnesbeck
			return;
1968 23a193da Phil Davis
		}
1969 f1ac1733 Erik Fonnesbeck
	}
1970
1971 23a193da Phil Davis
	if ($debug) {
1972
		$fd = fopen("{$g['tmp_path']}/print_r", "a");
1973
		fwrite($fd, print_r($pconfig, true));
1974
	}
1975 b6db8ea3 sullrich
1976 23a193da Phil Davis
	if (is_array($sectionref)) {
1977
		foreach ($sectionref as $itemkey => $item) {
1978
			if ($debug) {
1979
				fwrite($fd, "$itemkey\n");
1980
			}
1981 f1ac1733 Erik Fonnesbeck
1982
			$fieldfound = true;
1983
			$fieldref = &$sectionref[$itemkey];
1984 23a193da Phil Davis
			foreach ($field as $fieldname) {
1985
				if (is_array($fieldref) && isset($fieldref[$fieldname])) {
1986 f1ac1733 Erik Fonnesbeck
					$fieldref = &$fieldref[$fieldname];
1987 23a193da Phil Davis
				} else {
1988 f1ac1733 Erik Fonnesbeck
					$fieldfound = false;
1989
					break;
1990
				}
1991 b6db8ea3 sullrich
			}
1992 23a193da Phil Davis
			if ($fieldfound && $fieldref == $origname) {
1993
				if ($debug) {
1994
					fwrite($fd, "Setting old alias value $origname to $new_alias_name\n");
1995
				}
1996 f1ac1733 Erik Fonnesbeck
				$fieldref = $new_alias_name;
1997 b6db8ea3 sullrich
			}
1998
		}
1999
	}
2000
2001 23a193da Phil Davis
	if ($debug) {
2002
		fclose($fd);
2003
	}
2004 b6db8ea3 sullrich
2005
}
2006 f6ba4bd1 Scott Ullrich
2007 6d1907a3 Renato Botelho
function parse_aliases_file($filename, $type = "url", $max_items = -1) {
2008
	/*
2009
	 * $filename = file to process for example blocklist like DROP:  http://www.spamhaus.org/drop/drop.txt
2010
	 * $type = if set to 'url' then subnets and ips will be returned,
2011
	 *         if set to 'url_ports' port-ranges and ports will be returned
2012
	 * $max_items = sets the maximum amount of valid items to load, -1 the default defines there is no limit.
2013
	 *
2014
	 * RETURNS an array of ip subnets and ip's or ports and port-ranges, returns NULL upon a error conditions (file not found)
2015
	 */
2016
2017
	$fd = @fopen($filename, 'r');
2018
	if (!$fd) {
2019
		log_error(gettext("Could not process aliases from alias: {$alias_url}"));
2020
		return null;
2021
	}
2022
	$items = array();
2023
	/* NOTE: fgetss() is not a typo RTFM before being smart */
2024
	while (($fc = fgetss($fd)) !== FALSE) {
2025
		$tmp = trim($fc, " \t\n\r");
2026 23a193da Phil Davis
		if (empty($tmp)) {
2027 6d1907a3 Renato Botelho
			continue;
2028 23a193da Phil Davis
		}
2029 6d1907a3 Renato Botelho
		$tmp_str = strstr($tmp, '#', true);
2030 23a193da Phil Davis
		if (!empty($tmp_str)) {
2031 6d1907a3 Renato Botelho
			$tmp = $tmp_str;
2032 23a193da Phil Davis
		}
2033 6d1907a3 Renato Botelho
		$tmp_str = strstr($tmp, ' ', true);
2034 23a193da Phil Davis
		if (!empty($tmp_str)) {
2035 6d1907a3 Renato Botelho
			$tmp = $tmp_str;
2036 23a193da Phil Davis
		}
2037 6d1907a3 Renato Botelho
		$valid = ($type == "url" && (is_ipaddr($tmp) || is_subnet($tmp))) ||
2038 23a193da Phil Davis
			 ($type == "url_ports" && (is_port($tmp) || is_portrange($tmp)));
2039 6d1907a3 Renato Botelho
		if ($valid) {
2040
			$items[] = $tmp;
2041 23a193da Phil Davis
			if (count($items) == $max_items) {
2042 6d1907a3 Renato Botelho
				break;
2043 23a193da Phil Davis
			}
2044 6d1907a3 Renato Botelho
		}
2045
	}
2046
	fclose($fd);
2047
	return $items;
2048
}
2049
2050 f6ba4bd1 Scott Ullrich
function update_alias_url_data() {
2051
	global $config, $g;
2052 e5953c68 Ermal
2053 8422cdd5 Ermal
	$updated = false;
2054
2055 f6ba4bd1 Scott Ullrich
	/* item is a url type */
2056 8422cdd5 Ermal
	$lockkey = lock('aliasurl');
2057 e5953c68 Ermal
	if (is_array($config['aliases']['alias'])) {
2058
		foreach ($config['aliases']['alias'] as $x => $alias) {
2059 23a193da Phil Davis
			if (empty($alias['aliasurl'])) {
2060 e5953c68 Ermal
				continue;
2061 23a193da Phil Davis
			}
2062 e5953c68 Ermal
2063 6d1907a3 Renato Botelho
			$address = null;
2064 2ef16014 bcyrill
			foreach ($alias['aliasurl'] as $alias_url) {
2065
				/* fetch down and add in */
2066
				$temp_filename = tempnam("{$g['tmp_path']}/", "alias_import");
2067
				unlink($temp_filename);
2068 76590ffe Renato Botelho
				$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2069 873c1701 Renato Botelho
				mkdir($temp_filename);
2070 76590ffe Renato Botelho
				download_file($alias_url, $temp_filename . "/aliases", $verify_ssl);
2071
2072 2ef16014 bcyrill
				/* if the item is tar gzipped then extract */
2073 e45bae34 Ermal
				if (stripos($alias_url, '.tgz')) {
2074 23a193da Phil Davis
					if (!process_alias_tgz($temp_filename)) {
2075 e45bae34 Ermal
						continue;
2076 23a193da Phil Davis
					}
2077 e45bae34 Ermal
				} else if (stripos($alias_url, '.zip')) {
2078 23a193da Phil Davis
					if (!process_alias_unzip($temp_filename)) {
2079 e45bae34 Ermal
						continue;
2080 23a193da Phil Davis
					}
2081 e45bae34 Ermal
				}
2082 2ef16014 bcyrill
				if (file_exists("{$temp_filename}/aliases")) {
2083 6d1907a3 Renato Botelho
					$address = parse_aliases_file("{$temp_filename}/aliases", $alias['type'], 3000);
2084 2ef16014 bcyrill
					mwexec("/bin/rm -rf {$temp_filename}");
2085 f6ba4bd1 Scott Ullrich
				}
2086 2ef16014 bcyrill
			}
2087 6d1907a3 Renato Botelho
			if ($address != null) {
2088
				$config['aliases']['alias'][$x]['address'] = implode(" ", $address);
2089 2ef16014 bcyrill
				$updated = true;
2090 f6ba4bd1 Scott Ullrich
			}
2091
		}
2092
	}
2093 26d060bc Ermal
	unlock($lockkey);
2094 8422cdd5 Ermal
2095
	/* Report status to callers as well */
2096
	return $updated;
2097 f6ba4bd1 Scott Ullrich
}
2098
2099 10189b2a Scott Ullrich
function process_alias_unzip($temp_filename) {
2100 23a193da Phil Davis
	if (!file_exists("/usr/local/bin/unzip")) {
2101 e45bae34 Ermal
		log_error(gettext("Alias archive is a .zip file which cannot be decompressed because utility is missing!"));
2102
		return false;
2103
	}
2104 873c1701 Renato Botelho
	rename("{$temp_filename}/aliases", "{$temp_filename}/aliases.zip");
2105 10189b2a Scott Ullrich
	mwexec("/usr/local/bin/unzip {$temp_filename}/aliases.tgz -d {$temp_filename}/aliases/");
2106
	unlink("{$temp_filename}/aliases.zip");
2107
	$files_to_process = return_dir_as_array("{$temp_filename}/");
2108
	/* foreach through all extracted files and build up aliases file */
2109 e45bae34 Ermal
	$fd = @fopen("{$temp_filename}/aliases", "w");
2110
	if (!$fd) {
2111
		log_error(gettext("Could not open {$temp_filename}/aliases for writing!"));
2112
		return false;
2113
	}
2114 23a193da Phil Davis
	foreach ($files_to_process as $f2p) {
2115 e45bae34 Ermal
		$tmpfd = @fopen($f2p, 'r');
2116
		if (!$tmpfd) {
2117
			log_error(gettext("The following file could not be read {$f2p} from {$temp_filename}"));
2118
			continue;
2119
		}
2120 23a193da Phil Davis
		while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE) {
2121 e45bae34 Ermal
			fwrite($fd, $tmpbuf);
2122 23a193da Phil Davis
		}
2123 e45bae34 Ermal
		fclose($tmpfd);
2124 10189b2a Scott Ullrich
		unlink($f2p);
2125
	}
2126
	fclose($fd);
2127 e45bae34 Ermal
	unset($tmpbuf);
2128
2129
	return true;
2130 10189b2a Scott Ullrich
}
2131
2132 f6ba4bd1 Scott Ullrich
function process_alias_tgz($temp_filename) {
2133 23a193da Phil Davis
	if (!file_exists('/usr/bin/tar')) {
2134 e45bae34 Ermal
		log_error(gettext("Alias archive is a .tar/tgz file which cannot be decompressed because utility is missing!"));
2135
		return false;
2136
	}
2137 873c1701 Renato Botelho
	rename("{$temp_filename}/aliases", "{$temp_filename}/aliases.tgz");
2138 f6ba4bd1 Scott Ullrich
	mwexec("/usr/bin/tar xzf {$temp_filename}/aliases.tgz -C {$temp_filename}/aliases/");
2139
	unlink("{$temp_filename}/aliases.tgz");
2140
	$files_to_process = return_dir_as_array("{$temp_filename}/");
2141
	/* foreach through all extracted files and build up aliases file */
2142 e45bae34 Ermal
	$fd = @fopen("{$temp_filename}/aliases", "w");
2143
	if (!$fd) {
2144
		log_error(gettext("Could not open {$temp_filename}/aliases for writing!"));
2145
		return false;
2146
	}
2147 23a193da Phil Davis
	foreach ($files_to_process as $f2p) {
2148 e45bae34 Ermal
		$tmpfd = @fopen($f2p, 'r');
2149
		if (!$tmpfd) {
2150
			log_error(gettext("The following file could not be read {$f2p} from {$temp_filename}"));
2151
			continue;
2152
		}
2153 23a193da Phil Davis
		while (($tmpbuf = fread($tmpfd, 65536)) !== FALSE) {
2154 e45bae34 Ermal
			fwrite($fd, $tmpbuf);
2155 23a193da Phil Davis
		}
2156 e45bae34 Ermal
		fclose($tmpfd);
2157 f6ba4bd1 Scott Ullrich
		unlink($f2p);
2158
	}
2159
	fclose($fd);
2160 e45bae34 Ermal
	unset($tmpbuf);
2161
2162
	return true;
2163 f6ba4bd1 Scott Ullrich
}
2164
2165 a76c1c45 jim-p
function version_compare_dates($a, $b) {
2166
	$a_time = strtotime($a);
2167
	$b_time = strtotime($b);
2168
2169
	if ((!$a_time) || (!$b_time)) {
2170
		return FALSE;
2171
	} else {
2172 23a193da Phil Davis
		if ($a_time < $b_time) {
2173 a76c1c45 jim-p
			return -1;
2174 23a193da Phil Davis
		} elseif ($a_time == $b_time) {
2175 a76c1c45 jim-p
			return 0;
2176 23a193da Phil Davis
		} else {
2177 a76c1c45 jim-p
			return 1;
2178 23a193da Phil Davis
		}
2179 a76c1c45 jim-p
	}
2180
}
2181
function version_get_string_value($a) {
2182
	$strs = array(
2183
		0 => "ALPHA-ALPHA",
2184
		2 => "ALPHA",
2185
		3 => "BETA",
2186
		4 => "B",
2187 5eb03383 jim-p
		5 => "C",
2188
		6 => "D",
2189
		7 => "RC",
2190 f8c8d65c Stilez
		8 => "RELEASE",
2191
		9 => "*"			// Matches all release levels
2192 a76c1c45 jim-p
	);
2193
	$major = 0;
2194
	$minor = 0;
2195
	foreach ($strs as $num => $str) {
2196
		if (substr($a, 0, strlen($str)) == $str) {
2197
			$major = $num;
2198
			$n = substr($a, strlen($str));
2199 23a193da Phil Davis
			if (is_numeric($n)) {
2200 a76c1c45 jim-p
				$minor = $n;
2201 23a193da Phil Davis
			}
2202 a76c1c45 jim-p
			break;
2203
		}
2204
	}
2205
	return "{$major}.{$minor}";
2206
}
2207
function version_compare_string($a, $b) {
2208 f8c8d65c Stilez
	// Only compare string parts if both versions give a specific release
2209
	// (If either version lacks a string part, assume intended to match all release levels)
2210 23a193da Phil Davis
	if (isset($a) && isset($b)) {
2211 c96e71d1 Renato Botelho
		return version_compare_numeric(version_get_string_value($a), version_get_string_value($b));
2212 23a193da Phil Davis
	} else {
2213 c96e71d1 Renato Botelho
		return 0;
2214 23a193da Phil Davis
	}
2215 a76c1c45 jim-p
}
2216
function version_compare_numeric($a, $b) {
2217
	$a_arr = explode('.', rtrim($a, '.0'));
2218
	$b_arr = explode('.', rtrim($b, '.0'));
2219
2220
	foreach ($a_arr as $n => $val) {
2221
		if (array_key_exists($n, $b_arr)) {
2222
			// So far so good, both have values at this minor version level. Compare.
2223 23a193da Phil Davis
			if ($val > $b_arr[$n]) {
2224 a76c1c45 jim-p
				return 1;
2225 23a193da Phil Davis
			} elseif ($val < $b_arr[$n]) {
2226 a76c1c45 jim-p
				return -1;
2227 23a193da Phil Davis
			}
2228 a76c1c45 jim-p
		} else {
2229
			// a is greater, since b doesn't have any minor version here.
2230
			return 1;
2231
		}
2232
	}
2233
	if (count($b_arr) > count($a_arr)) {
2234
		// b is longer than a, so it must be greater.
2235
		return -1;
2236
	} else {
2237
		// Both a and b are of equal length and value.
2238
		return 0;
2239
	}
2240
}
2241
function pfs_version_compare($cur_time, $cur_text, $remote) {
2242
	// First try date compare
2243 bda131b2 jim-p
	$v = version_compare_dates($cur_time, $remote);
2244 a76c1c45 jim-p
	if ($v === FALSE) {
2245
		// If that fails, try to compare by string
2246
		// Before anything else, simply test if the strings are equal
2247 23a193da Phil Davis
		if (($cur_text == $remote) || ($cur_time == $remote)) {
2248 a76c1c45 jim-p
			return 0;
2249 23a193da Phil Davis
		}
2250 a76c1c45 jim-p
		list($cur_num, $cur_str) = explode('-', $cur_text);
2251
		list($rem_num, $rem_str) = explode('-', $remote);
2252
2253
		// First try to compare the numeric parts of the version string.
2254
		$v = version_compare_numeric($cur_num, $rem_num);
2255
2256
		// If the numeric parts are the same, compare the string parts.
2257 23a193da Phil Davis
		if ($v == 0) {
2258 a76c1c45 jim-p
			return version_compare_string($cur_str, $rem_str);
2259 23a193da Phil Davis
		}
2260 a76c1c45 jim-p
	}
2261
	return $v;
2262
}
2263 c7de8be4 jim-p
function process_alias_urltable($name, $url, $freq, $forceupdate=false) {
2264 dd042c51 Renato Botelho
	global $config;
2265
2266 c7de8be4 jim-p
	$urltable_prefix = "/var/db/aliastables/";
2267
	$urltable_filename = $urltable_prefix . $name . ".txt";
2268
2269
	// Make the aliases directory if it doesn't exist
2270
	if (!file_exists($urltable_prefix)) {
2271
		mkdir($urltable_prefix);
2272
	} elseif (!is_dir($urltable_prefix)) {
2273
		unlink($urltable_prefix);
2274
		mkdir($urltable_prefix);
2275
	}
2276
2277
	// If the file doesn't exist or is older than update_freq days, fetch a new copy.
2278 23a193da Phil Davis
	if (!file_exists($urltable_filename) ||
2279
	    ((time() - filemtime($urltable_filename)) > ($freq * 86400 - 90)) ||
2280
	    $forceupdate) {
2281 c7de8be4 jim-p
2282
		// Try to fetch the URL supplied
2283
		conf_mount_rw();
2284
		unlink_if_exists($urltable_filename . ".tmp");
2285 dd042c51 Renato Botelho
		$verify_ssl = isset($config['system']['checkaliasesurlcert']);
2286
		if (download_file($url, $urltable_filename . ".tmp", $verify_ssl)) {
2287
			mwexec("/usr/bin/sed -E 's/\;.*//g; /^[[:space:]]*($|#)/d' ". escapeshellarg($urltable_filename . ".tmp") . " > " . escapeshellarg($urltable_filename));
2288
			if (alias_get_type($name) == "urltable_ports") {
2289
				$ports = explode("\n", file_get_contents($urltable_filename));
2290
				$ports = group_ports($ports);
2291
				file_put_contents($urltable_filename, implode("\n", $ports));
2292
			}
2293 4aa0979f Ermal
			unlink_if_exists($urltable_filename . ".tmp");
2294 23a193da Phil Davis
		} else {
2295 873c1701 Renato Botelho
			touch($urltable_filename);
2296 23a193da Phil Davis
		}
2297 c7de8be4 jim-p
		conf_mount_ro();
2298 966f359e Ermal
		return true;
2299 c7de8be4 jim-p
	} else {
2300 23a193da Phil Davis
		// File exists, and it doesn't need to be updated.
2301 c7de8be4 jim-p
		return -1;
2302
	}
2303
}
2304 08fd5444 jim-p
function get_real_slice_from_glabel($label) {
2305
	$label = escapeshellarg($label);
2306
	return trim(`/sbin/glabel list | /usr/bin/grep -B2 ufs/{$label} | /usr/bin/head -n 1 | /usr/bin/cut -f3 -d' '`);
2307
}
2308
function nanobsd_get_boot_slice() {
2309
	return trim(`/sbin/mount | /usr/bin/grep pfsense | /usr/bin/cut -d'/' -f4 | /usr/bin/cut -d' ' -f1`);
2310
}
2311
function nanobsd_get_boot_drive() {
2312
	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`);
2313
}
2314
function nanobsd_get_active_slice() {
2315
	$boot_drive = nanobsd_get_boot_drive();
2316
	$active = trim(`gpart show $boot_drive | grep '\[active\]' | awk '{print $3;}'`);
2317
2318
	return "{$boot_drive}s{$active}";
2319
}
2320
function nanobsd_get_size() {
2321
	return strtoupper(file_get_contents("/etc/nanosize.txt"));
2322
}
2323 2b5f276f jim-p
function nanobsd_switch_boot_slice() {
2324 08fd5444 jim-p
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2325
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2326
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2327
	nanobsd_detect_slice_info();
2328
2329 2b5f276f jim-p
	if ($BOOTFLASH == $ACTIVE_SLICE) {
2330
		$slice = $TOFLASH;
2331
	} else {
2332
		$slice = $BOOTFLASH;
2333
	}
2334
2335 23a193da Phil Davis
	for ($i = 0; $i < ob_get_level(); $i++) {
2336
		ob_end_flush();
2337
	}
2338 08fd5444 jim-p
	ob_implicit_flush(1);
2339 23a193da Phil Davis
	if (strstr($slice, "s2")) {
2340 08fd5444 jim-p
		$ASLICE="2";
2341
		$AOLDSLICE="1";
2342
		$AGLABEL_SLICE="pfsense1";
2343
		$AUFS_ID="1";
2344
		$AOLD_UFS_ID="0";
2345
	} else {
2346
		$ASLICE="1";
2347
		$AOLDSLICE="2";
2348
		$AGLABEL_SLICE="pfsense0";
2349
		$AUFS_ID="0";
2350
		$AOLD_UFS_ID="1";
2351
	}
2352
	$ATOFLASH="{$BOOT_DRIVE}s{$ASLICE}";
2353
	$ACOMPLETE_PATH="{$BOOT_DRIVE}s{$ASLICE}a";
2354
	$ABOOTFLASH="{$BOOT_DRIVE}s{$AOLDSLICE}";
2355
	conf_mount_rw();
2356 971de1f9 Renato Botelho
	set_single_sysctl("kern.geom.debugflags", "16");
2357 08fd5444 jim-p
	exec("gpart set -a active -i {$ASLICE} {$BOOT_DRIVE}");
2358
	exec("/usr/sbin/boot0cfg -s {$ASLICE} -v /dev/{$BOOT_DRIVE}");
2359 2b5f276f jim-p
	// We can't update these if they are mounted now.
2360
	if ($BOOTFLASH != $slice) {
2361
		exec("/sbin/tunefs -L ${AGLABEL_SLICE} /dev/$ACOMPLETE_PATH");
2362
		nanobsd_update_fstab($AGLABEL_SLICE, $ACOMPLETE_PATH, $AOLD_UFS_ID, $AUFS_ID);
2363
	}
2364 971de1f9 Renato Botelho
	set_single_sysctl("kern.geom.debugflags", "0");
2365 08fd5444 jim-p
	conf_mount_ro();
2366
}
2367 2b5f276f jim-p
function nanobsd_clone_slice() {
2368 08fd5444 jim-p
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2369
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2370
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2371
	nanobsd_detect_slice_info();
2372
2373 23a193da Phil Davis
	for ($i = 0; $i < ob_get_level(); $i++) {
2374
		ob_end_flush();
2375
	}
2376 08fd5444 jim-p
	ob_implicit_flush(1);
2377 971de1f9 Renato Botelho
	set_single_sysctl("kern.geom.debugflags", "16");
2378 08fd5444 jim-p
	exec("/bin/dd if=/dev/zero of=/dev/{$TOFLASH} bs=1m count=1");
2379
	exec("/bin/dd if=/dev/{$BOOTFLASH} of=/dev/{$TOFLASH} bs=64k");
2380
	exec("/sbin/tunefs -L {$GLABEL_SLICE} /dev/{$COMPLETE_PATH}");
2381 2b5f276f jim-p
	$status = nanobsd_update_fstab($GLABEL_SLICE, $COMPLETE_PATH, $OLD_UFS_ID, $UFS_ID);
2382 971de1f9 Renato Botelho
	set_single_sysctl("kern.geom.debugflags", "0");
2383 23a193da Phil Davis
	if ($status) {
2384 08fd5444 jim-p
		return false;
2385
	} else {
2386
		return true;
2387
	}
2388
}
2389 2b5f276f jim-p
function nanobsd_update_fstab($gslice, $complete_path, $oldufs, $newufs) {
2390
	$tmppath = "/tmp/{$gslice}";
2391
	$fstabpath = "/tmp/{$gslice}/etc/fstab";
2392
2393 873c1701 Renato Botelho
	mkdir($tmppath);
2394 2b5f276f jim-p
	exec("/sbin/fsck_ufs -y /dev/{$complete_path}");
2395
	exec("/sbin/mount /dev/ufs/{$gslice} {$tmppath}");
2396 873c1701 Renato Botelho
	copy("/etc/fstab", $fstabpath);
2397 2b5f276f jim-p
2398
	if (!file_exists($fstabpath)) {
2399
		$fstab = <<<EOF
2400 9b1a8d98 Ermal
/dev/ufs/{$gslice} / ufs ro,noatime 1 1
2401
/dev/ufs/cf /cf ufs ro,noatime 1 1
2402 2b5f276f jim-p
EOF;
2403 23a193da Phil Davis
		if (file_put_contents($fstabpath, $fstab)) {
2404 2b5f276f jim-p
			$status = true;
2405 23a193da Phil Davis
		} else {
2406 2b5f276f jim-p
			$status = false;
2407 23a193da Phil Davis
		}
2408 2b5f276f jim-p
	} else {
2409
		$status = exec("sed -i \"\" \"s/pfsense{$oldufs}/pfsense{$newufs}/g\" {$fstabpath}");
2410
	}
2411
	exec("/sbin/umount {$tmppath}");
2412 873c1701 Renato Botelho
	rmdir($tmppath);
2413 2b5f276f jim-p
2414
	return $status;
2415
}
2416 08fd5444 jim-p
function nanobsd_detect_slice_info() {
2417
	global $SLICE, $OLDSLICE, $TOFLASH, $COMPLETE_PATH, $COMPLETE_BOOT_PATH;
2418
	global $GLABEL_SLICE, $UFS_ID, $OLD_UFS_ID, $BOOTFLASH;
2419
	global $BOOT_DEVICE, $REAL_BOOT_DEVICE, $BOOT_DRIVE, $ACTIVE_SLICE;
2420 a76c1c45 jim-p
2421 08fd5444 jim-p
	$BOOT_DEVICE=nanobsd_get_boot_slice();
2422
	$REAL_BOOT_DEVICE=get_real_slice_from_glabel($BOOT_DEVICE);
2423
	$BOOT_DRIVE=nanobsd_get_boot_drive();
2424
	$ACTIVE_SLICE=nanobsd_get_active_slice();
2425
2426
	// Detect which slice is active and set information.
2427 23a193da Phil Davis
	if (strstr($REAL_BOOT_DEVICE, "s1")) {
2428 08fd5444 jim-p
		$SLICE="2";
2429
		$OLDSLICE="1";
2430
		$GLABEL_SLICE="pfsense1";
2431
		$UFS_ID="1";
2432
		$OLD_UFS_ID="0";
2433 a76c1c45 jim-p
2434 08fd5444 jim-p
	} else {
2435
		$SLICE="1";
2436
		$OLDSLICE="2";
2437
		$GLABEL_SLICE="pfsense0";
2438
		$UFS_ID="0";
2439
		$OLD_UFS_ID="1";
2440
	}
2441
	$TOFLASH="{$BOOT_DRIVE}s{$SLICE}";
2442
	$COMPLETE_PATH="{$BOOT_DRIVE}s{$SLICE}a";
2443
	$COMPLETE_BOOT_PATH="{$BOOT_DRIVE}s{$OLDSLICE}";
2444
	$BOOTFLASH="{$BOOT_DRIVE}s{$OLDSLICE}";
2445
}
2446 38080cc1 Scott Ullrich
2447 26c8cc72 jim-p
function nanobsd_friendly_slice_name($slicename) {
2448
	global $g;
2449
	return strtolower(str_ireplace('pfsense', $g['product_name'], $slicename));
2450
}
2451
2452 38080cc1 Scott Ullrich
function get_include_contents($filename) {
2453 5fa78adc Renato Botelho
	if (is_file($filename)) {
2454
		ob_start();
2455
		include $filename;
2456
		$contents = ob_get_contents();
2457
		ob_end_clean();
2458
		return $contents;
2459
	}
2460
	return false;
2461 38080cc1 Scott Ullrich
}
2462
2463 3ffa8318 Renato Botelho
/* This xml 2 array function is courtesy of the php.net comment section on xml_parse.
2464
 * it is roughly 4 times faster then our existing pfSense parser but due to the large
2465
 * size of the RRD xml dumps this is required.
2466
 * The reason we do not use it for pfSense is that it does not know about array fields
2467
 * which causes it to fail on array fields with single items. Possible Todo?
2468
 */
2469
function xml2array($contents, $get_attributes = 1, $priority = 'tag')
2470
{
2471 23a193da Phil Davis
	if (!function_exists('xml_parser_create')) {
2472 86c707f3 Darren Embry
		return array ();
2473
	}
2474
	$parser = xml_parser_create('');
2475
	xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8");
2476
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
2477
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
2478
	xml_parse_into_struct($parser, trim($contents), $xml_values);
2479
	xml_parser_free($parser);
2480 23a193da Phil Davis
	if (!$xml_values) {
2481 86c707f3 Darren Embry
		return; //Hmm...
2482 23a193da Phil Davis
	}
2483 86c707f3 Darren Embry
	$xml_array = array ();
2484
	$parents = array ();
2485
	$opened_tags = array ();
2486
	$arr = array ();
2487
	$current = & $xml_array;
2488
	$repeated_tag_index = array ();
2489 23a193da Phil Davis
	foreach ($xml_values as $data) {
2490 86c707f3 Darren Embry
		unset ($attributes, $value);
2491
		extract($data);
2492
		$result = array ();
2493
		$attributes_data = array ();
2494 23a193da Phil Davis
		if (isset ($value)) {
2495
			if ($priority == 'tag') {
2496 86c707f3 Darren Embry
				$result = $value;
2497 23a193da Phil Davis
			} else {
2498 86c707f3 Darren Embry
				$result['value'] = $value;
2499 23a193da Phil Davis
			}
2500 86c707f3 Darren Embry
		}
2501 23a193da Phil Davis
		if (isset ($attributes) and $get_attributes) {
2502
			foreach ($attributes as $attr => $val) {
2503
				if ($priority == 'tag') {
2504 86c707f3 Darren Embry
					$attributes_data[$attr] = $val;
2505 23a193da Phil Davis
				} else {
2506 86c707f3 Darren Embry
					$result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
2507 23a193da Phil Davis
				}
2508 86c707f3 Darren Embry
			}
2509
		}
2510 23a193da Phil Davis
		if ($type == "open") {
2511 86c707f3 Darren Embry
			$parent[$level -1] = & $current;
2512 23a193da Phil Davis
			if (!is_array($current) or (!in_array($tag, array_keys($current)))) {
2513 86c707f3 Darren Embry
				$current[$tag] = $result;
2514 23a193da Phil Davis
				if ($attributes_data) {
2515 86c707f3 Darren Embry
					$current[$tag . '_attr'] = $attributes_data;
2516 23a193da Phil Davis
				}
2517 86c707f3 Darren Embry
				$repeated_tag_index[$tag . '_' . $level] = 1;
2518
				$current = & $current[$tag];
2519 23a193da Phil Davis
			} else {
2520
				if (isset ($current[$tag][0])) {
2521 86c707f3 Darren Embry
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2522
					$repeated_tag_index[$tag . '_' . $level]++;
2523 23a193da Phil Davis
				} else {
2524 86c707f3 Darren Embry
					$current[$tag] = array (
2525
						$current[$tag],
2526
						$result
2527
						);
2528
					$repeated_tag_index[$tag . '_' . $level] = 2;
2529 23a193da Phil Davis
					if (isset ($current[$tag . '_attr'])) {
2530 86c707f3 Darren Embry
						$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2531
						unset ($current[$tag . '_attr']);
2532
					}
2533
				}
2534
				$last_item_index = $repeated_tag_index[$tag . '_' . $level] - 1;
2535
				$current = & $current[$tag][$last_item_index];
2536
			}
2537 23a193da Phil Davis
		} elseif ($type == "complete") {
2538
			if (!isset ($current[$tag])) {
2539 86c707f3 Darren Embry
				$current[$tag] = $result;
2540
				$repeated_tag_index[$tag . '_' . $level] = 1;
2541 23a193da Phil Davis
				if ($priority == 'tag' and $attributes_data) {
2542 86c707f3 Darren Embry
					$current[$tag . '_attr'] = $attributes_data;
2543 23a193da Phil Davis
				}
2544
			} else {
2545
				if (isset ($current[$tag][0]) and is_array($current[$tag])) {
2546 86c707f3 Darren Embry
					$current[$tag][$repeated_tag_index[$tag . '_' . $level]] = $result;
2547 23a193da Phil Davis
					if ($priority == 'tag' and $get_attributes and $attributes_data) {
2548 86c707f3 Darren Embry
						$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2549
					}
2550
					$repeated_tag_index[$tag . '_' . $level]++;
2551 23a193da Phil Davis
				} else {
2552 86c707f3 Darren Embry
					$current[$tag] = array (
2553
						$current[$tag],
2554
						$result
2555
						);
2556
					$repeated_tag_index[$tag . '_' . $level] = 1;
2557 23a193da Phil Davis
					if ($priority == 'tag' and $get_attributes) {
2558
						if (isset ($current[$tag . '_attr'])) {
2559 86c707f3 Darren Embry
							$current[$tag]['0_attr'] = $current[$tag . '_attr'];
2560
							unset ($current[$tag . '_attr']);
2561
						}
2562 23a193da Phil Davis
						if ($attributes_data) {
2563 86c707f3 Darren Embry
							$current[$tag][$repeated_tag_index[$tag . '_' . $level] . '_attr'] = $attributes_data;
2564
						}
2565
					}
2566
					$repeated_tag_index[$tag . '_' . $level]++; //0 and 1 index is already taken
2567
				}
2568
			}
2569 23a193da Phil Davis
		} elseif ($type == 'close') {
2570 86c707f3 Darren Embry
			$current = & $parent[$level -1];
2571
		}
2572
	}
2573
	return ($xml_array);
2574 3ffa8318 Renato Botelho
}
2575
2576
function get_country_name($country_code) {
2577 23a193da Phil Davis
	if ($country_code != "ALL" && strlen($country_code) != 2) {
2578 3ffa8318 Renato Botelho
		return "";
2579 23a193da Phil Davis
	}
2580 3ffa8318 Renato Botelho
2581
	$country_names_xml = "/usr/local/share/mobile-broadband-provider-info/iso_3166-1_list_en.xml";
2582
	$country_names_contents = file_get_contents($country_names_xml);
2583
	$country_names = xml2array($country_names_contents);
2584
2585 23a193da Phil Davis
	if ($country_code == "ALL") {
2586 3ffa8318 Renato Botelho
		$country_list = array();
2587 23a193da Phil Davis
		foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2588
			$country_list[] = array(
2589
				"code" => $country['ISO_3166-1_Alpha-2_Code_element'],
2590
				"name" => ucwords(strtolower($country['ISO_3166-1_Country_name'])));
2591 3ffa8318 Renato Botelho
		}
2592
		return $country_list;
2593
	}
2594
2595
	foreach ($country_names['ISO_3166-1_List_en']['ISO_3166-1_Entry'] as $country) {
2596
		if ($country['ISO_3166-1_Alpha-2_Code_element'] == strtoupper($country_code)) {
2597
			return ucwords(strtolower($country['ISO_3166-1_Country_name']));
2598
		}
2599
	}
2600
	return "";
2601
}
2602
2603 baaa8bb1 Erik Fonnesbeck
/* sort by interface only, retain the original order of rules that apply to
2604
   the same interface */
2605
function filter_rules_sort() {
2606
	global $config;
2607
2608
	/* mark each rule with the sequence number (to retain the order while sorting) */
2609 23a193da Phil Davis
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
2610 baaa8bb1 Erik Fonnesbeck
		$config['filter']['rule'][$i]['seq'] = $i;
2611 23a193da Phil Davis
	}
2612 baaa8bb1 Erik Fonnesbeck
2613
	usort($config['filter']['rule'], "filter_rules_compare");
2614
2615
	/* strip the sequence numbers again */
2616 23a193da Phil Davis
	for ($i = 0; isset($config['filter']['rule'][$i]); $i++) {
2617 baaa8bb1 Erik Fonnesbeck
		unset($config['filter']['rule'][$i]['seq']);
2618 23a193da Phil Davis
	}
2619 baaa8bb1 Erik Fonnesbeck
}
2620
function filter_rules_compare($a, $b) {
2621 23a193da Phil Davis
	if (isset($a['floating']) && isset($b['floating'])) {
2622 baaa8bb1 Erik Fonnesbeck
		return $a['seq'] - $b['seq'];
2623 23a193da Phil Davis
	} else if (isset($a['floating'])) {
2624 baaa8bb1 Erik Fonnesbeck
		return -1;
2625 23a193da Phil Davis
	} else if (isset($b['floating'])) {
2626 baaa8bb1 Erik Fonnesbeck
		return 1;
2627 23a193da Phil Davis
	} else if ($a['interface'] == $b['interface']) {
2628 cea355a5 Erik Fonnesbeck
		return $a['seq'] - $b['seq'];
2629 23a193da Phil Davis
	} else {
2630 baaa8bb1 Erik Fonnesbeck
		return compare_interface_friendly_names($a['interface'], $b['interface']);
2631 23a193da Phil Davis
	}
2632 baaa8bb1 Erik Fonnesbeck
}
2633
2634 22dae853 Seth Mos
function generate_ipv6_from_mac($mac) {
2635
	$elements = explode(":", $mac);
2636 23a193da Phil Davis
	if (count($elements) <> 6) {
2637 22dae853 Seth Mos
		return false;
2638 23a193da Phil Davis
	}
2639 22dae853 Seth Mos
2640
	$i = 0;
2641 5aa28c86 Seth Mos
	$ipv6 = "fe80::";
2642 23a193da Phil Davis
	foreach ($elements as $byte) {
2643
		if ($i == 0) {
2644 22dae853 Seth Mos
			$hexadecimal =  substr($byte, 1, 2);
2645
			$bitmap = base_convert($hexadecimal, 16, 2);
2646
			$bitmap = str_pad($bitmap, 4, "0", STR_PAD_LEFT);
2647
			$bitmap = substr($bitmap, 0, 2) ."1". substr($bitmap, 3,4);
2648
			$byte = substr($byte, 0, 1) . base_convert($bitmap, 2, 16);
2649
		}
2650
		$ipv6 .= $byte;
2651 23a193da Phil Davis
		if ($i == 1) {
2652 22dae853 Seth Mos
			$ipv6 .= ":";
2653
		}
2654 23a193da Phil Davis
		if ($i == 3) {
2655 22dae853 Seth Mos
			$ipv6 .= ":";
2656
		}
2657 23a193da Phil Davis
		if ($i == 2) {
2658 22dae853 Seth Mos
			$ipv6 .= "ff:fe";
2659
		}
2660 5fa78adc Renato Botelho
2661 22dae853 Seth Mos
		$i++;
2662 5fa78adc Renato Botelho
	}
2663 fcdc8943 Seth Mos
	return $ipv6;
2664 22dae853 Seth Mos
}
2665 325e3163 Bill Marquette
2666 57f2840e Evgeny
/****f* pfsense-utils/load_mac_manufacturer_table
2667
 * NAME
2668
 *   load_mac_manufacturer_table
2669
 * INPUTS
2670
 *   none
2671
 * RESULT
2672
 *   returns associative array with MAC-Manufacturer pairs
2673
 ******/
2674
function load_mac_manufacturer_table() {
2675
	/* load MAC-Manufacture data from the file */
2676 62a29fe3 Ermal
	$macs = false;
2677 23a193da Phil Davis
	if (file_exists("/usr/local/share/nmap/nmap-mac-prefixes")) {
2678 62a29fe3 Ermal
		$macs=file("/usr/local/share/nmap/nmap-mac-prefixes");
2679 23a193da Phil Davis
	}
2680
	if ($macs) {
2681
		foreach ($macs as $line) {
2682
			if (preg_match('/([0-9A-Fa-f]{6}) (.*)$/', $line, $matches)) {
2683 4450527f Evgeny
				/* store values like this $mac_man['000C29']='VMware' */
2684 57f2840e Evgeny
				$mac_man["$matches[1]"]=$matches[2];
2685
			}
2686
		}
2687 5fa78adc Renato Botelho
		return $mac_man;
2688 23a193da Phil Davis
	} else {
2689 57f2840e Evgeny
		return -1;
2690 23a193da Phil Davis
	}
2691 57f2840e Evgeny
2692
}
2693
2694 474f36d1 Scott Ullrich
/****f* pfsense-utils/is_ipaddr_configured
2695
 * NAME
2696
 *   is_ipaddr_configured
2697
 * INPUTS
2698
 *   IP Address to check.
2699 4665dbdd Renato Botelho
 *   If ignore_if is a VIP (not carp), vip array index is passed after string _virtualip
2700 3490b8dd Phil Davis
 *   check_localip - if true then also check for matches with PPTP and LT2P addresses
2701
 *   check_subnets - if true then check if the given ipaddr is contained anywhere in the subnet of any other configured IP address
2702
 *   cidrprefix - the CIDR prefix (16, 20, 24, 64...) of ipaddr.
2703
 *     If check_subnets is true and cidrprefix is specified, 
2704
 *     then check if the ipaddr/cidrprefix subnet overlaps the subnet of any other configured IP address
2705 474f36d1 Scott Ullrich
 * RESULT
2706 3490b8dd Phil Davis
 *   returns true if the IP Address is configured and present on this device or overlaps a configured subnet.
2707 474f36d1 Scott Ullrich
*/
2708 3490b8dd Phil Davis
function is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false, $cidrprefix = "") {
2709
	if (count(where_is_ipaddr_configured($ipaddr, $ignore_if, $check_localip, $check_subnets, $cidrprefix))) {
2710
		return true;
2711
	}
2712
	return false;
2713
}
2714
2715
/****f* pfsense-utils/where_is_ipaddr_configured
2716
 * NAME
2717
 *   where_is_ipaddr_configured
2718
 * INPUTS
2719
 *   IP Address to check.
2720
 *   If ignore_if is a VIP (not carp), vip array index is passed after string _virtualip
2721
 *   check_localip - if true then also check for matches with PPTP and LT2P addresses
2722
 *   check_subnets - if true then check if the given ipaddr is contained anywhere in the subnet of any other configured IP address
2723
 *   cidrprefix - the CIDR prefix (16, 20, 24, 64...) of ipaddr.
2724
 *     If check_subnets is true and cidrprefix is specified, 
2725
 *     then check if the ipaddr/cidrprefix subnet overlaps the subnet of any other configured IP address
2726
 * RESULT
2727
 *   Returns an array of the interfaces 'if' plus IP address or subnet 'ip_or_subnet' that match or overlap the IP address to check.
2728
 *   If there are no matches then an empty array is returned.
2729
*/
2730
function where_is_ipaddr_configured($ipaddr, $ignore_if = "", $check_localip = false, $check_subnets = false, $cidrprefix = "") {
2731 e6c60013 Renato Botelho
	global $config;
2732
2733 3490b8dd Phil Davis
	$where_configured = array();
2734
2735 4665dbdd Renato Botelho
	$pos = strpos($ignore_if, '_virtualip');
2736
	if ($pos !== false) {
2737
		$ignore_vip_id = substr($ignore_if, $pos+10);
2738
		$ignore_vip_if = substr($ignore_if, 0, $pos);
2739
	} else {
2740
		$ignore_vip_id = -1;
2741
		$ignore_vip_if = $ignore_if;
2742
	}
2743
2744 1e5da31d Ermal
	$isipv6 = is_ipaddrv6($ipaddr);
2745
2746 e6c60013 Renato Botelho
	if ($check_subnets) {
2747 3490b8dd Phil Davis
		$cidrprefix = intval($cidrprefix);
2748
		if ($isipv6) {
2749
			if (($cidrprefix < 1) || ($cidrprefix > 128)) {
2750
				$cidrprefix = 128;
2751
			}
2752
		} else {
2753
			if (($cidrprefix < 1) || ($cidrprefix > 32)) {
2754
				$cidrprefix = 32;
2755
			}
2756
		}
2757 e6c60013 Renato Botelho
		$iflist = get_configured_interface_list();
2758
		foreach ($iflist as $if => $ifname) {
2759 23a193da Phil Davis
			if ($ignore_if == $if) {
2760 e6c60013 Renato Botelho
				continue;
2761 23a193da Phil Davis
			}
2762 2c98a935 Renato Botelho
2763 3490b8dd Phil Davis
			if ($isipv6) {
2764
				$if_ipv6 = get_interface_ipv6($if);
2765
				$if_snbitsv6 = get_interface_subnetv6($if);
2766
				if ($if_ipv6 && $if_snbitsv6 && check_subnetsv6_overlap($ipaddr, $cidrprefix, $if_ipv6, $if_snbitsv6)) {
2767
					$where_entry = array();
2768
					$where_entry['if'] = $if;
2769
					$where_entry['ip_or_subnet'] = get_interface_ipv6($if) . "/" . get_interface_subnetv6($if);
2770
					$where_configured[] = $where_entry;
2771
				}
2772 1e5da31d Ermal
			} else {
2773 3490b8dd Phil Davis
				$if_ipv4 = get_interface_ip($if);
2774
				$if_snbitsv4 = get_interface_subnet($if);
2775
				if ($if_ipv4 && $if_snbitsv4 && check_subnets_overlap($ipaddr, $cidrprefix, $if_ipv4, $if_snbitsv4)) {
2776
					$where_entry = array();
2777
					$where_entry['if'] = $if;
2778
					$where_entry['ip_or_subnet'] = get_interface_ip($if) . "/" . get_interface_subnet($if);
2779
					$where_configured[] = $where_entry;
2780
				}
2781 23a193da Phil Davis
			}
2782 e6c60013 Renato Botelho
		}
2783
	} else {
2784 3490b8dd Phil Davis
		if ($isipv6) {
2785 2c98a935 Renato Botelho
			$interface_list_ips = get_configured_ipv6_addresses();
2786 23a193da Phil Davis
		} else {
2787 2c98a935 Renato Botelho
			$interface_list_ips = get_configured_ip_addresses();
2788 23a193da Phil Davis
		}
2789 2c98a935 Renato Botelho
2790 23a193da Phil Davis
		foreach ($interface_list_ips as $if => $ilips) {
2791
			if ($ignore_if == $if) {
2792 e6c60013 Renato Botelho
				continue;
2793 23a193da Phil Davis
			}
2794
			if (strcasecmp($ipaddr, $ilips) == 0) {
2795 3490b8dd Phil Davis
				$where_entry = array();
2796
				$where_entry['if'] = $if;
2797
				$where_entry['ip_or_subnet'] = $ilips;
2798
				$where_configured[] = $where_entry;
2799 23a193da Phil Davis
			}
2800 e6c60013 Renato Botelho
		}
2801 5fa78adc Renato Botelho
	}
2802 a1613b62 Renato Botelho
2803 4021ec36 Renato Botelho
	$interface_list_vips = get_configured_vips_list(true);
2804
	foreach ($interface_list_vips as $id => $vip) {
2805 4665dbdd Renato Botelho
		/* Skip CARP interfaces here since they were already checked above */
2806 23a193da Phil Davis
		if ($id == $ignore_vip_id || (substr($ignore_if, 0, 4) == '_vip') && substr($ignore_vip_if, 5)  == $vip['uniqdid']) {
2807 4021ec36 Renato Botelho
			continue;
2808 23a193da Phil Davis
		}
2809
		if (strcasecmp($ipaddr, $vip['ipaddr']) == 0) {
2810 3490b8dd Phil Davis
			$where_entry = array();
2811
			$where_entry['if'] = $vip['if'];
2812
			$where_entry['ip_or_subnet'] = $vip['ipaddr'];
2813
			$where_configured[] = $where_entry;
2814 23a193da Phil Davis
		}
2815 4021ec36 Renato Botelho
	}
2816
2817 e6c60013 Renato Botelho
	if ($check_localip) {
2818 23a193da Phil Davis
		if (is_array($config['pptpd']) && !empty($config['pptpd']['localip']) && (strcasecmp($ipaddr, $config['pptpd']['localip']) == 0)) {
2819 3490b8dd Phil Davis
			$where_entry = array();
2820
			$where_entry['if'] = 'pptp';
2821
			$where_entry['ip_or_subnet'] = $config['pptpd']['localip'];
2822
			$where_configured[] = $where_entry;
2823 23a193da Phil Davis
		}
2824 e6c60013 Renato Botelho
2825 23a193da Phil Davis
		if (!is_array($config['l2tp']) && !empty($config['l2tp']['localip']) && (strcasecmp($ipaddr, $config['l2tp']['localip']) == 0)) {
2826 3490b8dd Phil Davis
			$where_entry = array();
2827
			$where_entry['if'] = 'l2tp';
2828
			$where_entry['ip_or_subnet'] = $config['l2tp']['localip'];
2829
			$where_configured[] = $where_entry;
2830 23a193da Phil Davis
		}
2831 a1613b62 Renato Botelho
	}
2832
2833 3490b8dd Phil Davis
	return $where_configured;
2834 474f36d1 Scott Ullrich
}
2835
2836 e4a8ed97 Scott Ullrich
/****f* pfsense-utils/pfSense_handle_custom_code
2837
 * NAME
2838
 *   pfSense_handle_custom_code
2839
 * INPUTS
2840
 *   directory name to process
2841
 * RESULT
2842
 *   globs the directory and includes the files
2843
 */
2844 d65962a7 Scott Ullrich
function pfSense_handle_custom_code($src_dir) {
2845 5fa78adc Renato Botelho
	// Allow extending of the nat edit page and include custom input validation
2846 23a193da Phil Davis
	if (is_dir("$src_dir")) {
2847 3dbceb92 Scott Ullrich
		$cf = glob($src_dir . "/*.inc");
2848 23a193da Phil Davis
		foreach ($cf as $nf) {
2849
			if ($nf == "." || $nf == "..") {
2850 d65962a7 Scott Ullrich
				continue;
2851 23a193da Phil Davis
			}
2852 d65962a7 Scott Ullrich
			// Include the extra handler
2853 3dbceb92 Scott Ullrich
			include("$nf");
2854 d65962a7 Scott Ullrich
		}
2855
	}
2856
}
2857
2858 000a8d1d Renato Botelho
function set_language($lang = 'en_US', $encoding = "UTF-8") {
2859 3e139f90 Vinicius Coque
	putenv("LANG={$lang}.{$encoding}");
2860
	setlocale(LC_ALL, "{$lang}.{$encoding}");
2861
	textdomain("pfSense");
2862 a94c4e1f Vinicius Coque
	bindtextdomain("pfSense","/usr/local/share/locale");
2863 3e139f90 Vinicius Coque
	bind_textdomain_codeset("pfSense","{$lang}.{$encoding}");
2864
}
2865
2866
function get_locale_list() {
2867
	$locales = array(
2868
		"en_US" => gettext("English"),
2869 2e2eb012 Vinicius Coque
		"pt_BR" => gettext("Portuguese (Brazil)"),
2870 f079b676 technical50
		"tr" => gettext("Turkish"),
2871 3e139f90 Vinicius Coque
	);
2872
	asort($locales);
2873
	return $locales;
2874
}
2875 20a7cb15 smos
2876 f079b676 technical50
function system_get_language_code() {
2877
	global $config, $g_languages;
2878
2879
	// a language code, as per [RFC3066]
2880
	$language = $config['system']['language'];
2881
	//$code = $g_languages[$language]['code'];
2882
	$code = str_replace("_", "-", $language);
2883
2884 23a193da Phil Davis
	if (empty($code)) {
2885 f079b676 technical50
		$code = "en-US"; // Set default code.
2886 23a193da Phil Davis
	}
2887 f079b676 technical50
2888
	return $code;
2889
}
2890
2891
function system_get_language_codeset() {
2892
	global $config, $g_languages;
2893
2894
	$language = $config['system']['language'];
2895
	$codeset = $g_languages[$language]['codeset'];
2896
2897 23a193da Phil Davis
	if (empty($codeset)) {
2898 f079b676 technical50
		$codeset = "UTF-8"; // Set default codeset.
2899 23a193da Phil Davis
	}
2900 f079b676 technical50
2901
	return $codeset;
2902
}
2903
2904
/* Available languages/locales */
2905
$g_languages = array (
2906
	"sq"    => array("codeset" => "UTF-8", "desc" => gettext("Albanian")),
2907
	"bg"    => array("codeset" => "UTF-8", "desc" => gettext("Bulgarian")),
2908
	"zh_CN" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Simplified)")),
2909
	"zh_TW" => array("codeset" => "UTF-8", "desc" => gettext("Chinese (Traditional)")),
2910
	"nl"    => array("codeset" => "UTF-8", "desc" => gettext("Dutch")),
2911
	"da"    => array("codeset" => "UTF-8", "desc" => gettext("Danish")),
2912 000a8d1d Renato Botelho
	"en_US" => array("codeset" => "UTF-8", "desc" => gettext("English")),
2913 f079b676 technical50
	"fi"    => array("codeset" => "UTF-8", "desc" => gettext("Finnish")),
2914
	"fr"    => array("codeset" => "UTF-8", "desc" => gettext("French")),
2915
	"de"    => array("codeset" => "UTF-8", "desc" => gettext("German")),
2916
	"el"    => array("codeset" => "UTF-8", "desc" => gettext("Greek")),
2917
	"hu"    => array("codeset" => "UTF-8", "desc" => gettext("Hungarian")),
2918
	"it"    => array("codeset" => "UTF-8", "desc" => gettext("Italian")),
2919
	"ja"    => array("codeset" => "UTF-8", "desc" => gettext("Japanese")),
2920
	"ko"    => array("codeset" => "UTF-8", "desc" => gettext("Korean")),
2921
	"lv"    => array("codeset" => "UTF-8", "desc" => gettext("Latvian")),
2922
	"nb"    => array("codeset" => "UTF-8", "desc" => gettext("Norwegian (Bokmal)")),
2923
	"pl"    => array("codeset" => "UTF-8", "desc" => gettext("Polish")),
2924 429112ac Renato Botelho
	"pt_BR" => array("codeset" => "UTF-8", "desc" => gettext("Portuguese (Brazil)")),
2925 f079b676 technical50
	"pt"    => array("codeset" => "UTF-8", "desc" => gettext("Portuguese (Portugal)")),
2926
	"ro"    => array("codeset" => "UTF-8", "desc" => gettext("Romanian")),
2927
	"ru"    => array("codeset" => "UTF-8", "desc" => gettext("Russian")),
2928
	"sl"    => array("codeset" => "UTF-8", "desc" => gettext("Slovenian")),
2929
	"tr"    => array("codeset" => "UTF-8", "desc" => gettext("Turkish")),
2930
	"es"    => array("codeset" => "UTF-8", "desc" => gettext("Spanish")),
2931
	"sv"    => array("codeset" => "UTF-8", "desc" => gettext("Swedish")),
2932
	"sk"    => array("codeset" => "UTF-8", "desc" => gettext("Slovak")),
2933
	"cs"    => array("codeset" => "UTF-8", "desc" => gettext("Czech"))
2934
);
2935
2936 20a7cb15 smos
function return_hex_ipv4($ipv4) {
2937 23a193da Phil Davis
	if (!is_ipaddrv4($ipv4)) {
2938 20a7cb15 smos
		return(false);
2939 23a193da Phil Davis
	}
2940 5fa78adc Renato Botelho
2941 20a7cb15 smos
	/* we need the hex form of the interface IPv4 address */
2942
	$ip4arr = explode(".", $ipv4);
2943 733c6f89 Ermal
	return (sprintf("%02x%02x%02x%02x", $ip4arr[0], $ip4arr[1], $ip4arr[2], $ip4arr[3]));
2944 20a7cb15 smos
}
2945
2946
function convert_ipv6_to_128bit($ipv6) {
2947 23a193da Phil Davis
	if (!is_ipaddrv6($ipv6)) {
2948 20a7cb15 smos
		return(false);
2949 23a193da Phil Davis
	}
2950 20a7cb15 smos
2951
	$ip6arr = array();
2952
	$ip6prefix = Net_IPv6::uncompress($ipv6);
2953
	$ip6arr = explode(":", $ip6prefix);
2954
	/* binary presentation of the prefix for all 128 bits. */
2955
	$ip6prefixbin = "";
2956 23a193da Phil Davis
	foreach ($ip6arr as $element) {
2957 20a7cb15 smos
		$ip6prefixbin .= sprintf("%016b", hexdec($element));
2958
	}
2959
	return($ip6prefixbin);
2960
}
2961
2962
function convert_128bit_to_ipv6($ip6bin) {
2963 23a193da Phil Davis
	if (strlen($ip6bin) <> 128) {
2964 20a7cb15 smos
		return(false);
2965 23a193da Phil Davis
	}
2966 20a7cb15 smos
2967
	$ip6arr = array();
2968
	$ip6binarr = array();
2969
	$ip6binarr = str_split($ip6bin, 16);
2970 23a193da Phil Davis
	foreach ($ip6binarr as $binpart) {
2971 20a7cb15 smos
		$ip6arr[] = dechex(bindec($binpart));
2972 23a193da Phil Davis
	}
2973 20a7cb15 smos
	$ip6addr = Net_IPv6::compress(implode(":", $ip6arr));
2974
2975
	return($ip6addr);
2976
}
2977
2978 8b198c64 smos
2979
/* Returns the calculated bit length of the prefix delegation from the WAN interface */
2980
/* DHCP-PD is variable, calculate from the prefix-len on the WAN interface */
2981
/* 6rd is variable, calculate from 64 - (v6 prefixlen - (32 - v4 prefixlen)) */
2982
/* 6to4 is 16 bits, e.g. 65535 */
2983
function calculate_ipv6_delegation_length($if) {
2984
	global $config;
2985
2986 23a193da Phil Davis
	if (!is_array($config['interfaces'][$if])) {
2987 8b198c64 smos
		return false;
2988 23a193da Phil Davis
	}
2989 8b198c64 smos
2990 23a193da Phil Davis
	switch ($config['interfaces'][$if]['ipaddrv6']) {
2991 8b198c64 smos
		case "6to4":
2992
			$pdlen = 16;
2993
			break;
2994
		case "6rd":
2995
			$rd6cfg = $config['interfaces'][$if];
2996
			$rd6plen = explode("/", $rd6cfg['prefix-6rd']);
2997
			$pdlen = (64 - ($rd6plen[1] + (32 - $rd6cfg['prefix-6rd-v4plen'])));
2998
			break;
2999
		case "dhcp6":
3000
			$dhcp6cfg = $config['interfaces'][$if];
3001
			$pdlen = $dhcp6cfg['dhcp6-ia-pd-len'];
3002
			break;
3003
		default:
3004
			$pdlen = 0;
3005
			break;
3006
	}
3007
	return($pdlen);
3008
}
3009 d23e157a smos
3010
function huawei_rssi_to_string($rssi) {
3011
	$dbm = array();
3012
	$i = 0;
3013 145cc518 smos
	$dbstart = -113;
3014 23a193da Phil Davis
	while ($i < 32) {
3015 145cc518 smos
		$dbm[$i] = $dbstart + ($i * 2);
3016 d23e157a smos
		$i++;
3017
	}
3018
	$percent = round(($rssi / 31) * 100);
3019 145cc518 smos
	$string = "rssi:{$rssi} level:{$dbm[$rssi]}dBm percent:{$percent}%";
3020 d23e157a smos
	return $string;
3021
}
3022
3023
function huawei_mode_to_string($mode, $submode) {
3024
	$modes[0] = "None";
3025 5fa78adc Renato Botelho
	$modes[1] = "AMPS";
3026 d23e157a smos
	$modes[2] = "CDMA";
3027
	$modes[3] = "GSM/GPRS";
3028
	$modes[4] = "HDR";
3029
	$modes[5] = "WCDMA";
3030 5fa78adc Renato Botelho
	$modes[6] = "GPS";
3031 d23e157a smos
3032
	$submodes[0] = "No Service";
3033
	$submodes[1] = "GSM";
3034
	$submodes[2] = "GPRS";
3035
	$submodes[3] = "EDGE";
3036
	$submodes[4] = "WCDMA";
3037
	$submodes[5] = "HSDPA";
3038
	$submodes[6] = "HSUPA";
3039 e313da37 smos
	$submodes[7] = "HSDPA+HSUPA";
3040 d23e157a smos
	$submodes[8] = "TD-SCDMA";
3041
	$submodes[9] = "HSPA+";
3042
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
3043
	return $string;
3044
}
3045
3046
function huawei_service_to_string($state) {
3047
	$modes[0] = "No";
3048 5fa78adc Renato Botelho
	$modes[1] = "Restricted";
3049 d23e157a smos
	$modes[2] = "Valid";
3050
	$modes[3] = "Restricted Regional";
3051
	$modes[4] = "Powersaving";
3052
	$string = "{$modes[$state]} Service";
3053
	return $string;
3054
}
3055
3056
function huawei_simstate_to_string($state) {
3057
	$modes[0] = "Invalid SIM/locked";
3058 5fa78adc Renato Botelho
	$modes[1] = "Valid SIM";
3059 d23e157a smos
	$modes[2] = "Invalid SIM CS";
3060
	$modes[3] = "Invalid SIM PS";
3061
	$modes[4] = "Invalid SIM CS/PS";
3062
	$modes[255] = "Missing SIM";
3063
	$string = "{$modes[$state]} State";
3064
	return $string;
3065
}
3066 4adf752c smos
3067
function zte_rssi_to_string($rssi) {
3068
	return huawei_rssi_to_string($rssi);
3069
}
3070
3071
function zte_mode_to_string($mode, $submode) {
3072
	$modes[0] = "No Service";
3073 5fa78adc Renato Botelho
	$modes[1] = "Limited Service";
3074 4adf752c smos
	$modes[2] = "GPRS";
3075
	$modes[3] = "GSM";
3076
	$modes[4] = "UMTS";
3077
	$modes[5] = "EDGE";
3078 5fa78adc Renato Botelho
	$modes[6] = "HSDPA";
3079 4adf752c smos
3080
	$submodes[0] = "CS_ONLY";
3081
	$submodes[1] = "PS_ONLY";
3082
	$submodes[2] = "CS_PS";
3083
	$submodes[3] = "CAMPED";
3084
	$string = "{$modes[$mode]}, {$submodes[$submode]} Mode";
3085
	return $string;
3086
}
3087
3088
function zte_service_to_string($state) {
3089
	$modes[0] = "Initializing";
3090 5fa78adc Renato Botelho
	$modes[1] = "Network Lock error";
3091 4adf752c smos
	$modes[2] = "Network Locked";
3092
	$modes[3] = "Unlocked or correct MCC/MNC";
3093
	$string = "{$modes[$state]} Service";
3094
	return $string;
3095
}
3096
3097
function zte_simstate_to_string($state) {
3098
	$modes[0] = "No action";
3099 5fa78adc Renato Botelho
	$modes[1] = "Network lock";
3100 4adf752c smos
	$modes[2] = "(U)SIM card lock";
3101
	$modes[3] = "Network Lock and (U)SIM card Lock";
3102
	$string = "{$modes[$state]} State";
3103
	return $string;
3104
}
3105 e9ab2ddb smos
3106
function get_configured_pppoe_server_interfaces() {
3107
	global $config;
3108
	$iflist = array();
3109
	if (is_array($config['pppoes']['pppoe'])) {
3110 23a193da Phil Davis
		foreach ($config['pppoes']['pppoe'] as $pppoe) {
3111 e9ab2ddb smos
			if ($pppoe['mode'] == "server") {
3112
				$int = "poes". $pppoe['pppoeid'];
3113
				$iflist[$int] = strtoupper($int);
3114
			}
3115
		}
3116
	}
3117
	return $iflist;
3118
}
3119
3120
function get_pppoes_child_interfaces($ifpattern) {
3121
	$if_arr = array();
3122 23a193da Phil Davis
	if ($ifpattern == "") {
3123 e9ab2ddb smos
		return;
3124 23a193da Phil Davis
	}
3125 e9ab2ddb smos
3126
	exec("ifconfig", $out, $ret);
3127 23a193da Phil Davis
	foreach ($out as $line) {
3128
		if (preg_match("/^({$ifpattern}[0-9]+):/i", $line, $match)) {
3129 e9ab2ddb smos
			$if_arr[] = $match[1];
3130
		}
3131
	}
3132
	return $if_arr;
3133
3134
}
3135
3136 331166a8 PiBa-NL
/****f* pfsense-utils/pkg_call_plugins
3137
 * NAME
3138
 *   pkg_call_plugins
3139
 * INPUTS
3140
 *   $plugin_type value used to search in package configuration if the plugin is used, also used to create the function name
3141
 *   $plugin_params parameters to pass to the plugin function for passing multiple parameters a array can be used.
3142
 * RESULT
3143
 *   returns associative array results from the plugin calls for each package
3144
 * NOTES
3145
 *   This generic function can be used to notify or retrieve results from functions that are defined in packages.
3146
 ******/
3147
function pkg_call_plugins($plugin_type, $plugin_params) {
3148 eaee3af6 PiBa-NL
	global $g, $config;
3149
	$results = array();
3150 23a193da Phil Davis
	if (!is_array($config['installedpackages']['package'])) {
3151 331166a8 PiBa-NL
		return $results;
3152 23a193da Phil Davis
	}
3153 eaee3af6 PiBa-NL
	foreach ($config['installedpackages']['package'] as $package) {
3154 23a193da Phil Davis
		if (!file_exists("/usr/local/pkg/" . $package['configurationfile'])) {
3155 eaee3af6 PiBa-NL
			continue;
3156 23a193da Phil Davis
		}
3157 eaee3af6 PiBa-NL
		$pkg_config = parse_xml_config_pkg("/usr/local/pkg/" . $package['configurationfile'], 'packagegui');
3158
		$pkgname = substr(reverse_strrchr($package['configurationfile'], "."),0,-1);
3159 23a193da Phil Davis
		if (is_array($pkg_config['plugins']['item'])) {
3160 3fe73243 PiBa-NL
			foreach ($pkg_config['plugins']['item'] as $plugin) {
3161 331166a8 PiBa-NL
				if ($plugin['type'] == $plugin_type) {
3162 23a193da Phil Davis
					if (file_exists($pkg_config['include_file'])) {
3163 eaee3af6 PiBa-NL
						require_once($pkg_config['include_file']);
3164 23a193da Phil Davis
					} else {
3165 eaee3af6 PiBa-NL
						continue;
3166 23a193da Phil Davis
					}
3167 eaee3af6 PiBa-NL
					$plugin_function = $pkgname . '_'. $plugin_type;
3168
					$results[$pkgname] = @eval($plugin_function($plugin_params));
3169
				}
3170
			}
3171 23a193da Phil Davis
		}
3172 eaee3af6 PiBa-NL
	}
3173
	return $results;
3174
}
3175
3176 7c8f3711 jim-p
/* Function to find and return the active XML RPC base URL to avoid code duplication */
3177
function get_active_xml_rpc_base_url() {
3178
	global $config, $g;
3179
	/* If the user has activated the option to enable an alternate xmlrpcbaseurl, and it's not empty, then use it */
3180
	if (isset($config['system']['altpkgrepo']['enable']) && !empty($config['system']['altpkgrepo']['xmlrpcbaseurl'])) {
3181
		return $config['system']['altpkgrepo']['xmlrpcbaseurl'];
3182
	} else {
3183
		return $g['xmlrpcbaseurl'];
3184
	}
3185
}
3186
3187 58005e52 jim-p
?>