Project

General

Profile

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

    
36
function get_tmp_file() {
37
	return "/tmp/tmp-" . time();
38
}
39

    
40
/****f* pfsense-utils/get_dns_servers
41
 * NAME
42
 *   get_dns_servres - get system dns servers
43
 * INPUTS
44
 *   $dns_servers - an array of the dns servers
45
 * RESULT
46
 *   null
47
 ******/
48
function get_dns_servers() {
49
	$dns_servers = array();
50
	$dns = `cat /etc/resolv.conf`;
51
	$dns_s = split("\n", $dns);
52
	foreach($dns_s as $dns) {
53
		$matches = "";
54
		if (preg_match("/nameserver (.*)/", $dns, $matches))
55
			$dns_servers[] = $matches[1];		
56
	}
57
	$dns_server_master = array();
58
	sort($dns_servers);
59
	$lastseen = "";
60
	foreach($dns_servers as $t) {
61
		if($t <> $lastseen)
62
			if($t <> "")
63
				$dns_server_master[] = $t;
64
		$lastseen = $t;
65
	}
66
	return $dns_server_master;
67
}
68
 	
69
/****f* pfsense-utils/log_error
70
* NAME
71
*   log_error  - Sends a string to syslog.
72
* INPUTS
73
*   $error     - string containing the syslog message.
74
* RESULT
75
*   null
76
******/
77
function log_error($error) {
78
	$page = $_SERVER['PHP_SELF'];
79
	syslog(LOG_WARNING, "$page: $error");
80
	return;
81
}
82

    
83
/****f* pfsense-utils/get_interface_mac_address
84
 * NAME
85
 *   get_interface_mac_address - Return a interfaces mac address
86
 * INPUTS
87
 *   $interface	- interface to obtain mac address from
88
 * RESULT
89
 *   $mac - the mac address of the interface
90
 ******/
91
function get_interface_mac_address($interface) {
92
	$mac = exec("ifconfig {$interface} | awk '/ether/ {print $2}'");
93
	return trim($mac);
94
}
95

    
96
/****f* pfsense-utils/return_dir_as_array
97
 * NAME
98
 *   return_dir_as_array - Return a directory's contents as an array.
99
 * INPUTS
100
 *   $dir	- string containing the path to the desired directory.
101
 * RESULT
102
 *   $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
103
 ******/
104
function return_dir_as_array($dir) {
105
	$dir_array = array();
106
	if (is_dir($dir)) {
107
		if ($dh = opendir($dir)) {
108
			while (($file = readdir($dh)) !== false) {
109
				$canadd = 0;
110
				if($file == ".") $canadd = 1;
111
				if($file == "..") $canadd = 1;
112
				if($canadd == 0)
113
					array_push($dir_array, $file);
114
			}
115
			closedir($dh);
116
		}
117
	}
118
	return $dir_array;
119
}
120

    
121
/****f* pfsense-utils/enable_hardware_offloading
122
 * NAME
123
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
124
 * INPUTS
125
 *   $interface	- string containing the physical interface to work on.
126
 * RESULT
127
 *   null
128
 * NOTES
129
 *   This function only supports the fxp driver's loadable microcode.
130
 ******/
131
function enable_hardware_offloading($interface) {
132
	global $g, $config;
133

    
134
	if(stristr($interface,"lnc"))
135
		return;
136
	if(isset($config['system']['do_not_use_nic_microcode']))
137
		return;
138

    
139
	if($g['booting']) {
140
	/* translate wan, lan, opt -> real interface if needed */
141
	$int = filter_translate_type_to_real_interface($interface);
142
	if($int <> "") $interface = $int;
143
	$int_family = preg_split("/[0-9]+/", $int);
144
	$options = strtolower(`/sbin/ifconfig {$interface} | grep options`);
145
	$supported_ints = array('fxp');
146
	if (in_array($int_family, $supported_ints))
147
		mwexec("/sbin/ifconfig {$interface} link0");
148

    
149
	if(stristr($options, "txcsum") == true)
150
	    mwexec("/sbin/ifconfig {$interface} txcsum 2>/dev/null");
151
	if(stristr($options, "rxcsum") == true)    
152
	    mwexec("/sbin/ifconfig {$interface} rxcsum 2>/dev/null");    
153
	if(stristr($options, "polling") == true)
154
	    mwexec("/sbin/ifconfig {$interface} polling 2>/dev/null");
155
	} else {
156
		mwexec("sysctl kern.polling.enable=0");
157
	}
158
	return;
159
}
160

    
161
/****f* pfsense-utils/is_alias_inuse
162
 * NAME
163
 *   checks to see if an alias is currently in use by a rule
164
 * INPUTS
165
 *   
166
 * RESULT
167
 *   true or false
168
 * NOTES
169
 *   
170
 ******/
171
function is_alias_inuse($alias) {
172
	global $g, $config;
173

    
174
	if($alias == "") return false;
175
	/* loop through firewall rules looking for alias in use */
176
	if(is_array($config['nat']['rule']))
177
		foreach($config['filter']['rule'] as $rule) {
178
			if(is_array($rule['source']['address']))
179
				if($rule['source']['address'] == $alias)
180
					return true;
181
			if(is_array($rule['destination']['address']))
182
				if($rule['destination']['address'] == $alias)
183
					return true;
184
		}
185
	/* loop through nat rules looking for alias in use */
186
	if(is_array($config['nat']['rule']))
187
		foreach($config['nat']['rule'] as $rule) {
188
			if($rule['target'] == $alias)
189
				return true;
190
			if($rule['external-address'] == $alias)
191
				return true;	
192
		}
193
	return false;
194
}
195

    
196
/****f* pfsense-utils/setup_polling_defaults
197
 * NAME
198
 *   sets up sysctls for pollingS
199
 * INPUTS
200
 *   
201
 * RESULT
202
 *   null
203
 * NOTES
204
 *   
205
 ******/
206
function setup_polling_defaults() {
207
	global $g, $config;
208
	if($config['system']['polling_each_burst'])
209
		mwexec("sysctl kern.polling.each_burst={$config['system']['polling_each_burst']}");
210
	if($config['system']['polling_burst_max'])
211
		mwexec("sysctl kern.polling.burst_max={$config['system']['polling_burst_max']}");
212
	if($config['system']['polling_user_frac'])
213
		mwexec("sysctl kern.polling.user_frac={$config['system']['polling_user_frac']}");		
214
}
215

    
216
/****f* pfsense-utils/setup_polling
217
 * NAME
218
 *   sets up polling
219
 * INPUTS
220
 *   
221
 * RESULT
222
 *   null
223
 * NOTES
224
 *   
225
 ******/
226
function setup_polling() {
227
	global $g, $config;		
228

    
229
	setup_polling_defaults();
230
	
231
	if(isset($config['system']['polling']))
232
		$supported_ints = array('dc', 'em', 'fwe', 'fwip', 'fxp', 'ixgb', 'ste', 'nge', 're', 'rl', 'sf', 'sis', 'ste', 'vge', 'vr', 'xl');
233
	else
234
		$supported_ints = array();
235

    
236
	/* build an array of interfaces to work with */
237
	$iflist = array("lan" => "LAN", "wan" => "WAN");
238
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
239
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];			
240

    
241
	foreach ($iflist as $ifent => $ifname) {
242
		$real_interface = convert_friendly_interface_to_real_interface_name($ifname);
243
		$supported = false;
244
		foreach($supported_ints as $supported) {
245
			if(stristr($real_interface, $supported)) {
246
				$supported = true;
247
			} 
248
		}
249
		if ($supported == true) {
250
			mwexec("/sbin/ifconfig {$real_interface} polling");
251
		} else {
252
			mwexec("/sbin/ifconfig {$real_interface} -polling");
253
		}
254
	}
255
}
256

    
257
/****f* pfsense-utils/setup_microcode
258
 * NAME
259
 *   enumerates all interfaces and calls enable_hardware_offloading which
260
 *   enables a NIC's supported hardware features.
261
 * INPUTS
262
 *   
263
 * RESULT
264
 *   null
265
 * NOTES
266
 *   This function only supports the fxp driver's loadable microcode.
267
 ******/
268
function setup_microcode() {
269
	global $config;
270

    
271
	$ifdescrs = array('wan', 'lan');
272
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
273
		$ifdescrs['opt' . $j] = "opt" . $j;
274
	}
275
	foreach($ifdescrs as $if)
276
		enable_hardware_offloading($if);
277
}
278

    
279
/****f* pfsense-utils/return_filename_as_array
280
 * NAME
281
 *   return_filename_as_array - Return a file's contents as an array.
282
 * INPUTS
283
 *   $filename	- string containing the path to the desired file.
284
 *   $strip	- array of characters to strip - default is '#'.
285
 * RESULT
286
 *   $file	- array containing the file's contents.
287
 * NOTES
288
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
289
 ******/
290
function return_filename_as_array($filename, $strip = array('#')) {
291
	if(file_exists($filename)) $file = file($filename);
292
	if(is_array($file)) {
293
		foreach($file as $line) $line = trim($line);
294
		foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
295
	}
296
	return $file;
297
}
298

    
299
/****f* pfsense-utils/file_put_contents
300
 * NAME
301
 *   file_put_contents - Wrapper for file_put_contents if it doesn't exist
302
 * RESULT
303
 *   none
304
 ******/
305
if(!function_exists("file_put_contents")) {
306
	function file_put_contents($filename, $data) {
307
		$fd = fopen($filename,"w");
308
		fwrite($fd, $data);
309
		fclose($fd);
310
	}
311
}
312

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

    
326
/****f* pfsense-utils/is_carp_defined
327
 * NAME
328
 *   is_carp_defined - Return whether CARP is detected in the kernel.
329
 * RESULT
330
 *   boolean	- true if CARP is detected, false otherwise.
331
 ******/
332
function is_carp_defined() {
333
	/* is carp compiled into the kernel and userland? */
334
	$command = "/sbin/sysctl -a | grep carp";
335
	$fd = popen($command . " 2>&1 ", "r");
336
	if(!$fd) {
337
		log_error("Warning, could not execute command {$command}");
338
		return 0;
339
	}
340
	while(!feof($fd)) {
341
		$tmp .= fread($fd,49);
342
	}
343
	fclose($fd);
344

    
345
	if($tmp == "")
346
		return false;
347
	else
348
		return true;
349
}
350

    
351
/****f* pfsense-utils/get_interface_mtu
352
 * NAME
353
 *   get_interface_mtu - Return the mtu of an interface
354
 * RESULT
355
 *   $tmp	- Returns the mtu of an interface
356
 ******/
357
function get_interface_mtu($interface) {
358
	$mtu = `/sbin/ifconfig {$interface} | /usr/bin/grep mtu | /usr/bin/cut -d" " -f4`;
359
	return $mtu;
360
}
361

    
362
/****f* pfsense-utils/is_interface_wireless
363
 * NAME
364
 *   is_interface_wireless - Returns if an interface is wireless
365
 * RESULT
366
 *   $tmp	- Returns if an interface is wireless
367
 ******/
368
function is_interface_wireless($interface) {
369
	global $config, $g;
370
	$interface = convert_real_interface_to_friendly_interface_name($interface);
371
	if(isset($config['interfaces'][$interface]['wireless']))
372
		return true;
373
	else
374
		return false;
375
}
376

    
377
/****f* pfsense-utils/find_number_of_created_carp_interfaces
378
 * NAME
379
 *   find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
380
 * RESULT
381
 *   $tmp	- Number of currently created CARP interfaces.
382
 ******/
383
function find_number_of_created_carp_interfaces() {
384
	$command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
385
	$fd = popen($command . " 2>&1 ", "r");
386
	if(!$fd) {
387
		log_error("Warning, could not execute command {$command}");
388
		return 0;
389
	}
390
	while(!feof($fd)) {
391
		$tmp .= fread($fd,49);
392
	}
393
	fclose($fd);
394
	$tmp = intval($tmp);
395
	return $tmp;
396
}
397

    
398
/****f* pfsense-utils/link_ip_to_carp_interface
399
 * NAME
400
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
401
 * INPUTS
402
 *   $ip
403
 * RESULT
404
 *   $carp_ints
405
 ******/
406
function link_ip_to_carp_interface($ip) {
407
	global $config;
408
	if($ip == "") return;
409

    
410
	$ifdescrs = array('wan', 'lan');
411
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
412
		$ifdescrs['opt' . $j] = "opt" . $j;
413
	}
414

    
415
	$ft = split("\.", $ip);
416
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
417

    
418
	$carp_ints = "";
419
	$num_carp_ints = find_number_of_created_carp_interfaces();
420
	foreach ($ifdescrs as $ifdescr => $ifname) {
421
		for($x=0; $x<$num_carp_ints; $x++) {
422
			$carp_int = "carp{$x}";
423
			$carp_ip = find_interface_ip($carp_int);
424
			$carp_ft = split("\.", $carp_ip);
425
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
426
			$result = does_interface_exist($carp_int);
427
			if($result <> true) break;
428
			if($ft_ip == $carp_ft_ip)
429
			if(stristr($carp_ints,$carp_int) == false)
430
			$carp_ints .= " " . $carp_int;
431
		}
432
	}
433
	return $carp_ints;
434
}
435

    
436
/****f* pfsense-utils/exec_command
437
 * NAME
438
 *   exec_command - Execute a command and return a string of the result.
439
 * INPUTS
440
 *   $command	- String of the command to be executed.
441
 * RESULT
442
 *   String containing the command's result.
443
 * NOTES
444
 *   This function returns the command's stdout and stderr.
445
 ******/
446
function exec_command($command) {
447
	$output = array();
448
	exec($command . ' 2>&1 ', $output);
449
	return(implode("\n", $output));
450
}
451

    
452
/****f* interfaces/is_jumbo_capable
453
 * NAME
454
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
455
 * INPUTS
456
 *   $int             - string containing interface name
457
 * RESULT
458
 *   boolean          - true or false
459
 ******/
460
function is_jumbo_capable($int) {
461
	/* Per:
462
	 * http://www.freebsd.org/cgi/man.cgi?query=vlan&manpath=FreeBSD+6.0-RELEASE&format=html
463
	 * Only the following drivers support large frames
464
         * 
465
	 * 'de' chipset purposely left out of this list
466
	 * requires defining BIG_PACKET in the
467
	 * /usr/src/sys/pci/if_de.c source file and rebuilding the
468
	 * kernel or module.  The hack works only for the 21041,
469
	 * 21140, and 21140A chips.
470
	 */
471
	global $g;
472
	
473
	$capable = $g['vlan_long_frame'];
474
	
475
	$int_family = preg_split("/[0-9]+/", $int);
476

    
477
	if (in_array($int_family[0], $capable))
478
		return true;
479
	else
480
		return false;
481
}
482

    
483
/*
484
 * does_interface_exist($interface): return true or false if a interface is detected.
485
 */
486
function does_interface_exist($interface) {
487
	$ints = exec_command("/sbin/ifconfig -l");
488
	if(stristr($ints, $interface) !== false)
489
		return true;
490
	else
491
		return false;
492
}
493

    
494
/*
495
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
496
 */
497
function convert_ip_to_network_format($ip, $subnet) {
498
	$ipsplit = split('[.]', $ip);
499
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
500
	return $string;
501
}
502

    
503
/*
504
 * find_interface_ip($interface): return the interface ip (first found)
505
 */
506
function find_interface_ip($interface) {
507
	if(does_interface_exist($interface) == false) return;
508
	$ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
509
	$ip = str_replace("\n","",$ip);
510
	return $ip;
511
}
512

    
513
function guess_interface_from_ip($ipaddress) {
514
	$ints = `/sbin/ifconfig -l`;
515
	$ints_split = split(" ", $ints);
516
	$ip_subnet_split = split("\.", $ipaddress);
517
	$ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
518
	/* check first 3 octets */
519
	foreach($ints_split as $int) {
520
		$ip = find_interface_ip($int);
521
		$ip_split = split("\.", $ip);
522
		$ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
523
		if(stristr($ip_subnet, $ip_tocheck) != false) return $int;
524
	}
525
	/* check first 2 octets since nothing found while checking 3 */
526
	foreach($ints_split as $int) {
527
		$ip = find_interface_ip($int);
528
		$ip_split = split("\.", $ip);
529
		$ip_tocheck = $ip_split[0] . "." . $ip_split[1] . ".";
530
		if(stristr($ip_subnet, $ip_tocheck) != false) return $int;
531
	}
532
}
533

    
534
function filter_opt_interface_to_real($opt) {
535
	global $config;
536
	return $config['interfaces'][$opt]['if'];
537
}
538

    
539
function filter_get_opt_interface_descr($opt) {
540
	global $config;
541
	return $config['interfaces'][$opt]['descr'];
542
}
543

    
544
function get_friendly_interface_list_as_array() {
545
	global $config;
546
	$ints = array();
547
	$ifdescrs = array('wan', 'lan');
548
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
549
		$ifdescrs['opt' . $j] = "opt" . $j;
550
	}
551
	$ifdescrs = get_interface_list();
552
	foreach ($ifdescrs as $ifdescr => $ifname) {
553
		array_push($ints,$ifdescr);
554
	}
555
	return $ints;
556
}
557

    
558
/*
559
 * find_ip_interface($ip): return the interface where an ip is defined
560
 */
561
function find_ip_interface($ip) {
562
	global $config;
563
	$ifdescrs = array('wan', 'lan');
564
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
565
		$ifdescrs['opt' . $j] = "opt" . $j;
566
	}
567
	foreach ($ifdescrs as $ifdescr => $ifname) {
568
		$int = filter_translate_type_to_real_interface($ifname);
569
		$ifconfig = exec_command("/sbin/ifconfig {$int}");
570
	if(stristr($ifconfig,$ip) <> false)
571
		return $int;
572
	}
573
	return false;
574
}
575

    
576
/*
577
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
578
 *                                                       for a friendly interface.  ie: wan
579
 */
580
function filter_translate_type_to_real_interface($interface) {
581
	global $config;
582
	if($config['interfaces'][$interface]['if'] <> "") {
583
		return $config['interfaces'][$interface]['if'];
584
	} else {
585
		return $interface;
586
	}
587
}
588

    
589
/*
590
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
591
 */
592
function get_carp_interface_status($carpinterface) {
593
	/* basically cache the contents of ifconfig statement
594
	to speed up this routine */
595
	global $carp_query;
596
	if($carp_query == "")
597
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
598
	$found_interface = 0;
599
	foreach($carp_query as $int) {
600
		if($found_interface == 1) {
601
			if(stristr($int, "MASTER") == true) return "MASTER";
602
			if(stristr($int, "BACKUP") == true) return "BACKUP";
603
			if(stristr($int, "INIT") == true) return "INIT";
604
			return false;
605
		}
606
		if(stristr($int, $carpinterface) == true)
607
		$found_interface=1;
608
	}
609
	return;
610
}
611

    
612
/*
613
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
614
 */
615
function get_pfsync_interface_status($pfsyncinterface) {
616
    $result = does_interface_exist($pfsyncinterface);
617
    if($result <> true) return;
618
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
619
    return $status;
620
}
621

    
622
/*
623
 * find_carp_interface($ip): return the carp interface where an ip is defined
624
 */
625
function find_carp_interface($ip) {
626
	global $find_carp_ifconfig;
627
	if($find_carp_ifconfig == "") {
628
		$find_carp_ifconfig = array();
629
		$num_carp_ints = find_number_of_created_carp_interfaces();
630
		for($x=0; $x<$num_carp_ints; $x++) {
631
			$find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}");
632
		}
633
	}
634
	$carps = 0;
635
	foreach($find_carp_ifconfig as $fci) {
636
		if(stristr($fci, $ip) == true)
637
			return "carp{$carps}";
638
		$carps++;
639
	}
640
}
641

    
642
/*
643
 * setup_filter_bridge(): toggle filtering bridge
644
 */
645
function setup_filter_bridge() {
646
	global $config, $g;
647
	if(isset($config['bridge']['filteringbridge'])) {
648
		mwexec("/sbin/sysctl net.link.bridge.pfil_member=1");
649
		mwexec("/sbin/sysctl net.link.bridge.pfil_bridge=1");
650
	} else {		
651
		mwexec("/sbin/sysctl net.link.bridge.pfil_member=0");
652
		mwexec("/sbin/sysctl net.link.bridge.pfil_bridge=0");
653
	}
654
}
655

    
656
/*
657
 * find_number_of_created_bridges(): returns the number of currently created bridges
658
 */
659
function find_number_of_created_bridges() {
660
	return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
661
}
662

    
663
/*
664
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
665
 */
666
function add_rule_to_anchor($anchor, $rule, $label) {
667
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
668
}
669

    
670
/*
671
 * remove_text_from_file
672
 * remove $text from file $file
673
 */
674
function remove_text_from_file($file, $text) {
675
	global $fd_log;
676
	fwrite($fd_log, "Adding needed text items:\n");
677
	$filecontents = exec_command_and_return_text("cat " . $file);
678
	$textTMP = str_replace($text, "", $filecontents);
679
	$text .= $textTMP;
680
	fwrite($fd_log, $text . "\n");
681
	$fd = fopen($file, "w");
682
	fwrite($fd, $text);
683
	fclose($fd);
684
}
685

    
686
/*
687
 * add_text_to_file($file, $text): adds $text to $file.
688
 * replaces the text if it already exists.
689
 */
690
function add_text_to_file($file, $text) {
691
	if(file_exists($file) and is_writable($file)) {
692
		$filecontents = file($file);
693
		$filecontents[] = $text;
694
		$tmpfile = get_tmp_file();
695
		$fout = fopen($tmpfile, "w");
696
		foreach($filecontents as $line) {
697
			fwrite($fout, rtrim($line) . "\n");
698
		}
699
		fclose($fout);
700
		rename($tmpfile, $file);
701
		return true;
702
	} else {
703
		return false;
704
	}
705
}
706

    
707
/*
708
 *   after_sync_bump_adv_skew(): create skew values by 1S
709
 */
710
function after_sync_bump_adv_skew() {
711
	global $config, $g;
712
	$processed_skew = 1;
713
	$a_vip = &$config['virtualip']['vip'];
714
	foreach ($a_vip as $vipent) {
715
		if($vipent['advskew'] <> "") {
716
			$processed_skew = 1;
717
			$vipent['advskew'] = $vipent['advskew']+1;
718
		}
719
	}
720
	if($processed_skew == 1)
721
		write_config("After synch increase advertising skew");
722
}
723

    
724
/*
725
 * get_filename_from_url($url): converts a url to its filename.
726
 */
727
function get_filename_from_url($url) {
728
	return basename($url);
729
}
730

    
731
/*
732
 *   update_output_window: update bottom textarea dynamically.
733
 */
734
function update_output_window($text) {
735
	$log = ereg_replace("\n", "\\n", $text);
736
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
737
	/* ensure that contents are written out */
738
	ob_flush();
739
}
740

    
741
/*
742
 *   get_dir: return an array of $dir
743
 */
744
function get_dir($dir) {
745
	$dir_array = array();
746
	$d = dir($dir);
747
	while (false !== ($entry = $d->read())) {
748
		array_push($dir_array, $entry);
749
	}
750
	$d->close();
751
	return $dir_array;
752
}
753

    
754
/*
755
 *   update_output_window: update top textarea dynamically.
756
 */
757
function update_status($status) {
758
	echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
759
	/* ensure that contents are written out */
760
	ob_flush();
761
}
762

    
763
/*
764
 *   exec_command_and_return_text_array: execute command and return output
765
 */
766
function exec_command_and_return_text_array($command) {
767
	$fd = popen($command . " 2>&1 ", "r");
768
	while(!feof($fd)) {
769
		$tmp .= fread($fd,49);
770
	}
771
	fclose($fd);
772
	$temp_array = split("\n", $tmp);
773
	return $temp_array;
774
}
775

    
776
/*
777
 *   exec_command_and_return_text: execute command and return output
778
 */
779
function exec_command_and_return_text($command) {
780
	return exec_command($command);
781
}
782

    
783
/*
784
 *   exec_command_and_return_text: execute command and update output window dynamically
785
 */
786
function execute_command_return_output($command) {
787
	global $fd_log;
788
	$fd = popen($command . " 2>&1 ", "r");
789
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
790
	$counter = 0;
791
	$counter2 = 0;
792
	while(!feof($fd)) {
793
		$tmp = fread($fd, 50);
794
		$tmp1 = ereg_replace("\n","\\n", $tmp);
795
		$text = ereg_replace("\"","'", $tmp1);
796
		$lasttext = "";
797
		if($lasttext == "..") {
798
			$text = "";
799
			$lasttext = "";
800
			$counter=$counter-2;
801
		} else {
802
			$lasttext .= $text;
803
		}
804
		if($counter > 51) {
805
			$counter = 0;
806
			$extrabreak = "\\n";
807
		} else {
808
	    $extrabreak = "";
809
	    $counter++;
810
		}
811
		if($counter2 > 600) {
812
			echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
813
			$counter2 = 0;
814
		} else
815
			$counter2++;
816
		echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
817
	}
818
	fclose($fd);
819
}
820

    
821
/*
822
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
823
 */
824
function convert_friendly_interface_to_real_interface_name($interface) {
825
	global $config;
826
	$lc_interface = strtolower($interface);
827
	if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
828
	if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
829
	$ifdescrs = array();
830
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
831
		$ifdescrs['opt' . $j] = "opt" . $j;
832
	foreach ($ifdescrs as $ifdescr => $ifname) {
833
		if(strtolower($ifname) == $lc_interface)
834
	    return $config['interfaces'][$ifname]['if'];
835
		if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
836
			return $config['interfaces'][$ifname]['if'];
837
   }
838
   return $interface;
839
}
840

    
841
/*
842
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
843
 */
844
function convert_real_interface_to_friendly_interface_name($interface) {
845
	global $config;
846
	$ifdescrs = array('wan', 'lan');
847
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
848
		$ifdescrs['opt' . $j] = "opt" . $j;
849
	foreach ($ifdescrs as $ifdescr => $ifname) {
850
		$int = filter_translate_type_to_real_interface($ifname);
851
		if($ifname == $interface) return $ifname;
852
		if($int == $interface) return $ifname;
853
	}
854
	return $interface;
855
}
856

    
857
/*
858
 * update_progress_bar($percent): updates the javascript driven progress bar.
859
 */
860
function update_progress_bar($percent) {
861
	if($percent > 100) $percent = 1;
862
	echo "\n<script type=\"text/javascript\" language=\"javascript\">";
863
	echo "\ndocument.progressbar.style.width='" . $percent . "%';";
864
	echo "\n</script>";
865
}
866

    
867
/****f* pfsense-utils/WakeOnLan
868
 * NAME
869
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
870
 * RESULT
871
 *   true/false - true if the operation was successful
872
 ******/
873
function WakeOnLan($addr, $mac)
874
{
875
	$addr_byte = explode(':', $mac);
876
	$hw_addr = '';
877
	
878
	for ($a=0; $a < 6; $a++)
879
		$hw_addr .= chr(hexdec($addr_byte[$a]));
880
	
881
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
882
	
883
	for ($a = 1; $a <= 16; $a++)
884
		$msg .= $hw_addr;
885
	
886
	// send it to the broadcast address using UDP
887
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
888
	if ($s == false) {
889
		log_error("Error creating socket!");
890
		log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
891
	} else {
892
		// setting a broadcast option to socket:
893
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
894
		if($opt_ret < 0)
895
			log_error("setsockopt() failed, error: " . strerror($opt_ret));
896
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
897
		socket_close($s);
898
		log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}");
899
		return true;
900
	}
901
	
902
	return false;
903
}
904

    
905
/*
906
 * gather_altq_queue_stats():  gather altq queue stats and return an array that
907
 *                             is queuename|qlength|measured_packets
908
 *                             NOTE: this command takes 5 seconds to run
909
 */
910
function gather_altq_queue_stats($dont_return_root_queues) {
911
	mwexec("/usr/bin/killall -9 pfctl");
912
	$stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
913
	$stats_array = split("\n", $stats);
914
	$queue_stats = array();
915
	foreach ($stats_array as $stats_line) {
916
		$match_array = "";
917
		if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
918
			$queue_name = $match_array[1][0];
919
		if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
920
			$speed = $match_array[1][0];
921
		if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
922
			$borrows = $match_array[1][0];
923
		if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
924
			$suspends = $match_array[1][0];
925
		if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
926
			$drops = $match_array[1][0];
927
		if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
928
			$measured = $match_array[1][0];
929
			if($dont_return_root_queues == true)
930
				if(stristr($queue_name,"root_") == false)
931
					array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
932
		}
933
	}
934
	return $queue_stats;
935
}
936

    
937
/*
938
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
939
 *					 Useful for finding paths and stripping file extensions.
940
 */
941
function reverse_strrchr($haystack, $needle) {
942
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
943
}
944

    
945
/*
946
 *  backup_config_section($section): returns as an xml file string of
947
 *                                   the configuration section
948
 */
949
function backup_config_section($section) {
950
	global $config;
951
	$new_section = &$config[$section];
952
	/* generate configuration XML */
953
	$xmlconfig = dump_xml_config($new_section, $section);
954
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
955
	return $xmlconfig;
956
}
957

    
958
/*
959
 *  backup_vip_config_section($section): returns as an xml file string of
960
 *                                   the configuration section
961
 */
962
function backup_vip_config_section() {
963
	global $config;
964
	$new_section = &$config['virtualip'];
965
	foreach($new_section['vip'] as $section) {
966
		if($section['mode'] == "proxyarp") {
967
			unset($section);		
968
		}
969
		if($section['advskew'] <> "") {
970
			$section_val = intval($section['advskew']);
971
			$section_val=$section_val+100;
972
			if($section_val > 255)
973
				$section_val = 255;
974
			$section['advskew'] = $section_val;
975
		}
976
		$temp['vip'][] = $section;
977
   }
978
   return $temp;
979
}
980

    
981
/*
982
 *  restore_config_section($section, new_contents): restore a configuration section,
983
 *                                                  and write the configuration out
984
 *                                                  to disk/cf.
985
 */
986
function restore_config_section($section, $new_contents) {
987
	global $config, $g;
988
	conf_mount_rw();
989
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
990
	fwrite($fout, $new_contents);
991
	fclose($fout);
992
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
993
	$config[$section] = &$section_xml;
994
	unlink($g['tmp_path'] . "/tmpxml");
995
	write_config("Restored {$section} of config file (maybe from CARP partner)");
996
	conf_mount_ro();
997
	return;
998
}
999

    
1000
/*
1001
 * http_post($server, $port, $url, $vars): does an http post to a web server
1002
 *                                         posting the vars array.
1003
 * written by nf@bigpond.net.au
1004
 */
1005
function http_post($server, $port, $url, $vars) {
1006
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
1007
	$urlencoded = "";
1008
	while (list($key,$value) = each($vars))
1009
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
1010
	$urlencoded = substr($urlencoded,0,-1);
1011
	$content_length = strlen($urlencoded);
1012
	$headers = "POST $url HTTP/1.1
1013
Accept: */*
1014
Accept-Language: en-au
1015
Content-Type: application/x-www-form-urlencoded
1016
User-Agent: $user_agent
1017
Host: $server
1018
Connection: Keep-Alive
1019
Cache-Control: no-cache
1020
Content-Length: $content_length
1021

    
1022
";
1023

    
1024
	$errno = "";
1025
	$errstr = "";
1026
	$fp = fsockopen($server, $port, $errno, $errstr);
1027
	if (!$fp) {
1028
		return false;
1029
	}
1030

    
1031
	fputs($fp, $headers);
1032
	fputs($fp, $urlencoded);
1033

    
1034
	$ret = "";
1035
	while (!feof($fp))
1036
		$ret.= fgets($fp, 1024);
1037
	fclose($fp);
1038

    
1039
	return $ret;
1040
}
1041

    
1042
/*
1043
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
1044
 */
1045
if (!function_exists('php_check_syntax')){
1046
	function php_check_syntax($code_to_check, &$errormessage){
1047
		return false;
1048
		$fout = fopen("/tmp/codetocheck.php","w");
1049
		$code = $_POST['content'];
1050
		$code = str_replace("<?php", "", $code);
1051
		$code = str_replace("?>", "", $code);
1052
		fwrite($fout, "<?php\n\n");
1053
		fwrite($fout, $code_to_check);
1054
		fwrite($fout, "\n\n?>\n");
1055
		fclose($fout);
1056
		$command = "/usr/local/bin/php -l /tmp/codetocheck.php";
1057
		$output = exec_command($command);
1058
		if (stristr($output, "Errors parsing") == false) {
1059
			echo "false\n";
1060
			$errormessage = '';
1061
			return(false);
1062
		} else {
1063
			$errormessage = $output;
1064
			return(true);
1065
		}
1066
	}
1067
}
1068

    
1069
/*
1070
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
1071
 */
1072
if (!function_exists('php_check_syntax')){
1073
	function php_check_syntax($code_to_check, &$errormessage){
1074
		return false;
1075
		$command = "/usr/local/bin/php -l " . $code_to_check;
1076
		$output = exec_command($command);
1077
		if (stristr($output, "Errors parsing") == false) {
1078
			echo "false\n";
1079
			$errormessage = '';
1080
			return(false);
1081
		} else {
1082
			$errormessage = $output;
1083
			return(true);
1084
		}
1085
	}
1086
}
1087

    
1088
/*
1089
 * rmdir_recursive($path,$follow_links=false)
1090
 * Recursively remove a directory tree (rm -rf path)
1091
 * This is for directories _only_
1092
 */
1093
function rmdir_recursive($path,$follow_links=false) {
1094
	$to_do = glob($path);
1095
	if(!is_array($to_do)) $to_do = array($to_do);
1096
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
1097
		if(file_exists($workingdir)) {
1098
			if(is_dir($workingdir)) {
1099
				$dir = opendir($workingdir);
1100
				while ($entry = readdir($dir)) {
1101
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
1102
						unlink("$workingdir/$entry");
1103
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
1104
						rmdir_recursive("$workingdir/$entry");
1105
				}
1106
				closedir($dir);
1107
				rmdir($workingdir);
1108
			} elseif (is_file($workingdir)) {
1109
				unlink($workingdir);
1110
			}
1111
               	}
1112
	}
1113
	return;
1114
}
1115

    
1116
/*
1117
 *     get_memory()
1118
 *     returns an array listing the amount of
1119
 *     memory installed in the hardware
1120
 *     [0]real and [1]available
1121
 */
1122
function get_memory() {
1123
	if(file_exists("/var/log/dmesg.boot")) {
1124
		$mem = `cat /var/log/dmesg.boot | grep memory`;
1125
		$matches = "";
1126
		if (preg_match_all("/real memory  = .* \((.*) MB/", $mem, $matches))
1127
			$real = $matches[1];
1128
		if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches))
1129
			$avail = $matches[1];
1130
		return array($real[0],$avail[0]);
1131
	}
1132
	return array("64","64");
1133
}
1134

    
1135
/*
1136
 *    safe_mkdir($path, $mode = 0755)
1137
 *    create directory if it doesn't already exist and isn't a file!
1138
 */
1139
function safe_mkdir($path, $mode=0755) {
1140
	global $g;
1141

    
1142
	/* cdrom is ro. */
1143
	if($g['platform'] == "cdrom")
1144
		return false;
1145
	
1146
	if (!is_file($path) && !is_dir($path))
1147
		return mkdir($path, $mode);
1148
	else
1149
		return false;
1150
}
1151

    
1152
/*
1153
 * make_dirs($path, $mode = 0755)
1154
 * create directory tree recursively (mkdir -p)
1155
 */
1156
function make_dirs($path, $mode = 0755) {
1157
	/* is dir already created? */
1158
	if(is_dir($path)) return;
1159
	/* create directory in question */
1160
	$to_create = explode("/", $path);
1161
	foreach($to_create as $tc) 
1162
		if(!is_dir($tc))
1163
			safe_mkdir($path, $mode);
1164
}
1165

    
1166
/*
1167
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
1168
 */
1169
function call_pfsense_method($method, $params, $timeout = 0) {
1170
	global $g, $config;
1171
	$xmlrpc_base_url = $g['xmlrpcbaseurl'];
1172
	$xmlrpc_path = $g['xmlrpcpath'];
1173
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
1174
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
1175
	$resp = $cli->send($msg, $timeout);
1176
	if(!$resp) {
1177
		log_error("XMLRPC communication error: " . $cli->errstr);
1178
		return false;
1179
	} elseif($resp->faultCode()) {
1180
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
1181
		return false;
1182
	} else {
1183
		return XML_RPC_Decode($resp->value());
1184
	}
1185
}
1186

    
1187
/*
1188
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
1189
 */
1190
function check_firmware_version($tocheck = "all", $return_php = true) {
1191
	global $g, $config;
1192
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
1193
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
1194
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
1195
		"platform" => trim(file_get_contents('/etc/platform'))
1196
		);
1197
	if($tocheck == "all") {
1198
		$params = $rawparams;
1199
	} else {
1200
		foreach($tocheck as $check) {
1201
			$params['check'] = $rawparams['check'];
1202
			$params['platform'] = $rawparams['platform'];
1203
		}
1204
	}
1205
	if($config['system']['firmware']['branch']) {
1206
		$params['branch'] = $config['system']['firmware']['branch'];
1207
	}
1208
	if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) {
1209
		return false;
1210
	} else {
1211
		$versions["current"] = $params;
1212
	}
1213
	return $versions;
1214
}
1215

    
1216
function get_disk_info() {
1217
	$diskout = "";
1218
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
1219
	return explode(' ', $diskout[0]);
1220
	// $size, $used, $avail, $cap
1221
}
1222

    
1223
/****f* pfsense-utils/display_top_tabs
1224
 * NAME
1225
 *   display_top_tabs - display tabs with rounded edges
1226
 * INPUTS
1227
 *   $text	- array of tabs
1228
 * RESULT
1229
 *   null
1230
 ******/
1231
function display_top_tabs($tab_array) {
1232
	echo "<table cellpadding='0' cellspacing='0'>\n";
1233
	echo " <tr height='1'>\n";
1234
	$tabscounter = 0;
1235
	foreach ($tab_array as $ta) {
1236
		if($ta[1] == true) {
1237
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabactive'></div></td>\n";
1238
		} else {
1239
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
1240
		}
1241
		$tabscounter++;
1242
	}
1243
	echo "</tr>\n<tr>\n";
1244
	foreach ($tab_array as $ta) {
1245
		if($ta[1] == true) {
1246
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
1247
			echo "&nbsp;&nbsp;&nbsp;";
1248
			echo "<font size='-12'>&nbsp;</td>\n";
1249
		} else {
1250
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[2]}'>";
1251
			echo "<font color='white'>{$ta[0]}</a>&nbsp;&nbsp;&nbsp;";
1252
			echo "<font size='-12'>&nbsp;</td>\n";
1253
		}
1254
	}
1255
	echo "</tr>\n<tr height='5px'>\n";
1256
	foreach ($tab_array as $ta) {
1257
		if($ta[1] == true) {
1258
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
1259
		} else {
1260
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
1261
		}
1262
		$tabscounter++;
1263
	}
1264
	echo " </tr>\n";
1265
	echo "</table>\n";
1266
	    
1267
	echo "<script type=\"text/javascript\">";
1268
	echo "NiftyCheck();\n";
1269
	echo "Rounded(\"div#tabactive\",\"top\",\"#FFF\",\"#EEEEEE\",\"smooth\");\n";
1270
	for($x=0; $x<$tabscounter; $x++) 
1271
		echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"#FFF\",\"#777777\",\"smooth\");\n";
1272
	echo "</script>";
1273
}
1274

    
1275

    
1276
/****f* pfsense-utils/display_topbar
1277
 * NAME
1278
 *   display_topbar - top a table off with rounded edges
1279
 * INPUTS
1280
 *   $text	- (optional) Text to include in bar
1281
 * RESULT
1282
 *   null
1283
 ******/
1284
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {	    
1285
	echo "     <table width='100%' cellpadding='0' cellspacing='0'>\n";
1286
	echo "       <tr height='1'>\n";
1287
	echo "         <td width='100%' valign='top' color='{$bg_color}' bgcolor='{$bg_color}'>";
1288
	echo "		<div id='topbar'></div></td>\n";
1289
	echo "       </tr>\n";
1290
	echo "       <tr height='1'>\n";
1291
	if ($text != "")
1292
		echo "         <td height='1' class='listtopic'>{$text}</td>\n";
1293
	else
1294
		echo "         <td height='1' class='listtopic'></td>\n";
1295
	echo "       </tr>\n";
1296
	echo "     </table>";
1297
	echo "<script type=\"text/javascript\">";
1298
	echo "NiftyCheck();\n";
1299
	echo "Rounded(\"div#topbar\",\"top\",\"{$replace_color}\",\"{$bg_color}\",\"{$rounding_style}\");\n";
1300
	echo "</script>";
1301
}
1302

    
1303
/****f* pfsense-utils/generate_random_mac_address
1304
 * NAME
1305
 *   generate_random_mac - generates a random mac address
1306
 * INPUTS
1307
 *   none
1308
 * RESULT
1309
 *   $mac - a random mac address
1310
 ******/
1311
function generate_random_mac_address() {
1312
	$mac = "00:a0:8e";
1313
	for($x=0; $x<3; $x++) 
1314
		$mac .= ":" . dechex(rand(16, 255));
1315

    
1316
	return $mac;
1317
}
1318

    
1319
/****f* pfsense-utils/strncpy
1320
 * NAME
1321
 *   strncpy - copy strings
1322
 * INPUTS
1323
 *   &$dst, $src, $length
1324
 * RESULT
1325
 *   none
1326
 ******/
1327
function strncpy(&$dst, $src, $length) {
1328
	if (strlen($src) > $length) {
1329
		$dst = substr($src, 0, $length);
1330
	} else {
1331
		$dst = $src;
1332
	}
1333
}
1334

    
1335
/****f* pfsense-utils/reload_interfaces_sync
1336
 * NAME
1337
 *   reload_interfaces - reload all interfaces
1338
 * INPUTS
1339
 *   none
1340
 * RESULT
1341
 *   none
1342
 ******/
1343
function reload_interfaces_sync() {
1344
	global $config, $g, $debug;
1345
	
1346
	$shutdown_webgui_needed = false;
1347
	
1348
	touch("{$g['tmp_path']}/reloading_all");
1349
	
1350
	$memory = get_memory();
1351
	$avail = $memory[0];
1352
	/* we have way too little memory.  shutdown webConfigurator temporarily */
1353
	if($avail > 0 and $avail < 66) {
1354
		log_error("Not enough ram for reload.  Temporarily unloading webConfigurator.");
1355
		$shutdown_webgui_needed = true;
1356
		config_lock();
1357
		/* kill any running lighttpd */
1358
		killbypid("{$g['varrun_path']}/lighty-webConfigurator.pid");
1359
		config_unlock();
1360
	}
1361
	
1362
	if($debug)
1363
		log_error("reload_interfaces_sync() is starting.");
1364
	
1365
	if(file_exists("{$g['tmp_path']}/config.cache"))
1366
		unlink("{$g['tmp_path']}/config.cache");
1367
	
1368
	/* parse config.xml again */
1369
	$config = parse_config(true);
1370
	
1371
	$wan_if = $config['interfaces']['wan']['if'];
1372
	$lan_if = $config['interfaces']['lan']['if'];
1373
	
1374
	if($debug)
1375
		log_error("Cleaning up Interfaces");
1376
	
1377
	foreach ($iflist as $ifent => $ifname) {
1378
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1379
		if(stristr($ifname, "lo0") == true)
1380
			continue;
1381
		/* do not process wan interface, its mandatory */
1382
		if(stristr($ifname, "$wan_if") == true)
1383
			continue;
1384
		/* do not process lan interface, its mandatory */
1385
		if(stristr($ifname, "$lan_if") == true)
1386
			continue;
1387
		if($debug)
1388
			log_error("Downing and deleting $ifname_real - $ifname");
1389
		mwexec("/sbin/ifconfig {$ifname_real} down");
1390
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1391
	}
1392

    
1393
	/* set up VLAN virtual interfaces */
1394
	if($debug)
1395
		log_error("Configuring VLANS");
1396
	interfaces_vlan_configure();
1397
	
1398
	/* set up LAN interface */
1399
	if($debug)
1400
		log_error("Configuring LAN");
1401
	interfaces_lan_configure();
1402

    
1403
	/* set up WAN interface */
1404
	if($debug)
1405
		log_error("Configuring WAN");
1406
	interfaces_wan_configure();
1407
	
1408
	/* set up Optional interfaces */
1409
	if($debug)
1410
		log_error("Configuring optional interfaces");
1411
	interfaces_optional_configure();
1412
	    
1413
	/* set up static routes */
1414
	if($debug)
1415
		log_error("Configuring system Routing");
1416
	system_routing_configure();
1417
	
1418
	/* enable routing */
1419
	if($debug)
1420
		log_error("Enabling system routing");
1421
	system_routing_enable();
1422

    
1423
	/* setup captive portal if needed */
1424
	if($debug)
1425
		log_error("Configuring Captive portal");
1426
	captiveportal_configure();
1427

    
1428
	/* bring up carp interfaces */
1429
	if($debug)
1430
		log_error("Configuring CARP");
1431
	interfaces_carp_configure();
1432
	
1433
	/* bring up carp interfaces*/
1434
	if($debug)
1435
		log_error("Bringing up CARP interfaces");
1436
	interfaces_carp_bring_up_final();
1437
	
1438
	/* restart webConfigurator if needed */
1439
	if($shutdown_webgui_needed == true) 
1440
		touch("/tmp/restart_webgui");
1441
	
1442
	/* remove reloading_all trigger */
1443
	if($debug)
1444
		log_error("Removing {$g['tmp_path']}/reloading_all");
1445
	unlink_if_exists("{$g['tmp_path']}/reloading_all");
1446
}
1447

    
1448
/****f* pfsense-utils/reload_all
1449
 * NAME
1450
 *   reload_all - triggers a reload of all settings
1451
 *   * INPUTS
1452
 *   none
1453
 * RESULT
1454
 *   none
1455
 ******/
1456
function reload_all() {
1457
	touch("/tmp/reload_all");
1458
}
1459

    
1460
/****f* pfsense-utils/reload_interfaces
1461
 * NAME
1462
 *   reload_interfaces - triggers a reload of all interfaces
1463
 * INPUTS
1464
 *   none
1465
 * RESULT
1466
 *   none
1467
 ******/
1468
function reload_interfaces() {
1469
	touch("/tmp/reload_interfaces");
1470
}
1471

    
1472
/****f* pfsense-utils/sync_webgui_passwords
1473
 * NAME
1474
 *   sync_webgui_passwords - syncs webgui and ssh passwords
1475
 * INPUTS
1476
 *   none
1477
 * RESULT
1478
 *   none
1479
 ******/
1480
function sync_webgui_passwords() {
1481
	global $config, $g;
1482
	conf_mount_rw();
1483
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
1484
	if (!$fd) {
1485
		printf("Error: cannot open htpasswd in system_password_configure().\n");
1486
		return 1;
1487
	}
1488
	/* set admin account */
1489
	$username = $config['system']['username'];
1490
	
1491
	/* set defined user account */
1492
	if($username <> "admin") {
1493
		$username = $config['system']['username'];
1494
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
1495
	} else {
1496
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");	
1497
	}	
1498
	fclose($fd);
1499
	chmod("{$g['varrun_path']}/htpasswd", 0600);	
1500
	$crypted_pw = $config['system']['password'];
1501
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1502
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1503
	/* sync root */
1504
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
1505
	fwrite($fd, $crypted_pw);
1506
	pclose($fd);
1507
	mwexec("/usr/sbin/pw usermod -n root -s /bin/sh");
1508
	/* sync admin */
1509
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
1510
	fwrite($fd, $crypted_pw);
1511
	pclose($fd);
1512
	mwexec("/usr/sbin/pw usermod -n admin -s /etc/rc.initial");
1513
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1514
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1515
	conf_mount_ro();
1516
}
1517

    
1518
/****f* pfsense-utils/cleanup_opt_interfaces_after_removal
1519
 * NAME
1520
 *   cleanup_opt_interfaces_after_removal - renumber interfaces after removing
1521
 *   * INPUTS
1522
 *   optional interface number
1523
 * RESULT
1524
 *   none
1525
 ******/
1526
function cleanup_opt_interfaces_after_removal($opt_interface_num) {
1527
	global $config;
1528
	unlink_if_exists("/tmp/config.cache");
1529
	$config_file = file_get_contents("/cf/conf/config.xml");
1530
	/* loop through and reassign optional items */
1531
	for ($i = 255; isset($config['interfaces']['opt' . $i]); $i--) {
1532
		if($i < $opt_interface_num)
1533
			break;
1534
		/* replace opt$i with $i -1 */
1535
		str_replace("opt" . $i, "opt" . ($i-1), $config_file);
1536
	}
1537
	$fd = fopen("/cf/conf/config.xml", "w");
1538
	fwrite($fd, $config_file);
1539
	fclose($fd);
1540
	$config = parse_config(true);
1541
	return true;
1542
}
1543

    
1544
/****f* pfsense-utils/get_number_of_wan_netgraph_interfaces_needed
1545
 * NAME
1546
 *   get_number_of_wan_netgraph_interfaces_needed - returns the
1547
 *   		amount of netgraph interfaces needed for system wans
1548
 *   * INPUTS
1549
 *   none
1550
 * RESULT
1551
 *   number of needed netgraph (ng) interfaces
1552
 ******/
1553
function get_number_of_wan_netgraph_interfaces_needed() {
1554
	global $config, $g;
1555
	/* build an array of interfaces to work with */
1556
	$iflist = array("wan" => "WAN");
1557
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
1558
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
1559
	$ng_interfaces_needed = 0;
1560
	foreach ($iflist as $ifent => $ifname) {
1561
		if($config['inerfaces'][$ifname]['ipaddr'] == "pppoe") {
1562
			$ng_interfaces_needed++;
1563
		}
1564
	}
1565
	return $ng_interfaces_needed;
1566
}
1567

    
1568
function get_netgaph_interface_assignment($friendly_interface) {
1569
	global $config, $g;
1570
	/* build an array of interfaces to work with */
1571
	$iflist = array("wan" => "WAN");
1572
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
1573
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
1574
		$ng_interfaces_needed = 0;
1575
		$ng_interfaces_number = 0;
1576
		foreach ($iflist as $ifent => $ifname) {
1577
		if($config['inerfaces'][$ifname]['ipaddr'] == "pppoe") {
1578
			$ng_interfaces_number++;
1579
		}
1580
		if($friendly_interface == $ifname)
1581
			break;
1582
	}
1583
	return $ng_interfaces_number;	
1584
}
1585

    
1586
/****f* pfsense-utils/reload_all_sync
1587
 * NAME
1588
 *   reload_all - reload all settings
1589
 *   * INPUTS
1590
 *   none
1591
 * RESULT
1592
 *   none
1593
 ******/
1594
function reload_all_sync() {
1595
	global $config, $g;
1596
	
1597
	touch("{$g['tmp_path']}/reloading_all");
1598

    
1599
	$shutdown_webgui_needed = false;
1600

    
1601
	$memory = get_memory();
1602
	$avail = $memory[0];
1603
	/* we have way too little memory.  shutdown webConfigurator temporarily */
1604
	if($avail > 0 and $avail < 66) {
1605
		log_error("Not enough ram for reload.  Temporarily unloading webConfigurator.");
1606
		$shutdown_webgui_needed = true;
1607
		config_lock();
1608
		/* kill any running lighttpd */
1609
		killbypid("{$g['varrun_path']}/lighty-webConfigurator.pid");
1610
		config_unlock();
1611
	}
1612
	
1613
	if(file_exists("{$g['tmp_path']}/config.cache"))
1614
		unlink("{$g['tmp_path']}/config.cache");
1615
	
1616
	/* parse config.xml again */
1617
	$config = parse_config(true);
1618

    
1619
	/* set up our timezone */
1620
	system_timezone_configure();
1621

    
1622
	/* set up our hostname */
1623
	system_hostname_configure();
1624

    
1625
	/* make hosts file */
1626
	system_hosts_generate();
1627

    
1628
	/* generate resolv.conf */
1629
	system_resolvconf_generate();
1630

    
1631
	/* delete all old interface information */
1632
	$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
1633
	
1634
	$wan_if = $config['interfaces']['wan']['if'];
1635
	$lan_if = $config['interfaces']['lan']['if'];
1636
	
1637
	foreach ($iflist as $ifent => $ifname) {
1638
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1639
		if(stristr($ifname, "lo0") == true)
1640
			continue;
1641
		/* do not process wan interface, its mandatory */
1642
		if($wan_if == $ifname_real)
1643
			continue;
1644
		/* do not process lan interface, its mandatory */
1645
		if($lan_if == $ifname_real)
1646
			continue;
1647
		mwexec("/sbin/ifconfig {$ifname_real} down");
1648
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1649
	}
1650

    
1651
	/* set up VLAN virtual interfaces */
1652
	interfaces_vlan_configure();
1653
	
1654
	/* set up LAN interface */
1655
	interfaces_lan_configure();
1656

    
1657
	/* set up WAN interface */
1658
	interfaces_wan_configure();
1659

    
1660
	/* set up Optional interfaces */
1661
	interfaces_optional_configure();
1662
        
1663
	/* bring up carp interfaces */
1664
	interfaces_carp_configure();
1665
	
1666
	/* set up static routes */
1667
	system_routing_configure();
1668

    
1669
	/* enable routing */
1670
	system_routing_enable();
1671
	
1672
	/* ensure passwords are sync'd */
1673
	system_password_configure();
1674

    
1675
	/* start dnsmasq service */
1676
	services_dnsmasq_configure();
1677

    
1678
	/* start dyndns service */
1679
	services_dyndns_configure();
1680

    
1681
	/* start DHCP service */
1682
	services_dhcpd_configure();
1683

    
1684
	/* start the NTP client */
1685
	system_ntp_configure();
1686

    
1687
	/* start ftp proxy helpers if they are enabled */
1688
	system_start_ftp_helpers();
1689

    
1690
	/* start the captive portal */
1691
	captiveportal_configure();
1692

    
1693
        /* reload the filter */
1694
	filter_configure_sync();
1695

    
1696
	/* bring up carp interfaces*/
1697
	interfaces_carp_bring_up_final();
1698
	
1699
	/* sync pw database */
1700
	conf_mount_rw();
1701
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
1702
	conf_mount_ro();
1703

    
1704
	/* restart sshd */
1705
	touch("/tmp/start_sshd");
1706

    
1707
	/* restart webConfigurator if needed */
1708
	if($shutdown_webgui_needed == true) 
1709
		touch("/tmp/restart_webgui");
1710
	
1711
	unlink_if_exists("{$g['tmp_path']}/reloading_all");
1712
	
1713
}
1714

    
1715
function auto_login($status) {
1716
	$gettytab = file_get_contents("/etc/gettytab");
1717
	$getty_split = split("\n", $gettytab);
1718
	conf_mount_rw();
1719
	$fd = fopen("/etc/gettytab", "w");
1720
	foreach($getty_split as $gs) {
1721
		if(stristr($gs, "\:ht\:np\:sp\#115200") == true) {
1722
			if($status == true) {
1723
				fwrite($fd, "	:ht:np:sp#115200:al=root:\n");
1724
			} else {
1725
				fwrite($fd, "	:ht:np:sp#115200:\n");
1726
			}
1727
		} else {
1728
			fwrite($fd, "{$gs}\n");
1729
		}
1730
	}
1731
	fclose($fd);
1732
	conf_mount_ro();	
1733
}
1734

    
1735
function setup_serial_port() {
1736
	global $g, $config;
1737
	conf_mount_rw();
1738
	/* serial console - write out /boot.config */
1739
	if(file_exists("/boot.config"))
1740
		$boot_config = file_get_contents("/boot.config");
1741
	else
1742
		$boot_config = "";
1743
	
1744
	if($g['platform'] <> "cdrom") {
1745
		$boot_config_split = split("\n", $boot_config);
1746
		$fd = fopen("/boot.config","w");
1747
		if($fd) {
1748
			foreach($boot_config_split as $bcs) {
1749
				if(stristr($bcs, "-D")) {
1750
					/* DONT WRITE OUT, WE'LL DO IT LATER */	
1751
				} else {
1752
					if($bcs <> "")
1753
						fwrite($fd, "{$bcs}\n");
1754
				}
1755
			}
1756
			if(isset($config['system']['enableserial'])) {
1757
				fwrite($fd, "-D");
1758
			}			
1759
			fclose($fd);
1760
		}
1761
		/* serial console - write out /boot/loader.conf */
1762
		$boot_config = file_get_contents("/boot/loader.conf");	
1763
		$boot_config_split = split("\n", $boot_config);
1764
		$fd = fopen("/boot/loader.conf","w");
1765
		if($fd) {
1766
			foreach($boot_config_split as $bcs) {
1767
				if(stristr($bcs, "console")) {
1768
					/* DONT WRITE OUT, WE'LL DO IT LATER */	
1769
				} else {
1770
					if($bcs <> "")
1771
						fwrite($fd, "{$bcs}\n");
1772
				}
1773
			}
1774
			if(isset($config['system']['enableserial'])) {
1775
				fwrite($fd, "console=\"comconsole\"\n");
1776
			}
1777
			fclose($fd);
1778
		}
1779
	}
1780
	if(isset($config['system']['disableconsolemenu'])) {
1781
		auto_login(true);
1782
	} else {
1783
		auto_login(false);
1784
	}	
1785
	conf_mount_ro();
1786
	return;	
1787
}
1788

    
1789
function print_value_list($list, $count = 10, $separator = ",") {
1790
	$list = implode($separator, array_slice($list, 0, $count));
1791
	if(count($list) < $count) {
1792
		$list .= ".";
1793
	} else {
1794
		$list .= "...";
1795
	}
1796
	return $list;
1797
}
1798

    
1799
function convert_friendly_interface_to_friendly_descr($interface) {
1800
	global $config;
1801
	/* attempt to resolve interface to friendly descr */
1802
	if($config['interfaces'][$interface]['descr']) 
1803
		return $config['interfaces'][$interface]['descr'];
1804
	$tmp = convert_real_interface_to_friendly_descr($interface);
1805
	/* could not resolve, return back what was passed */
1806
	return $interface;
1807
}
1808

    
1809
function convert_real_interface_to_friendly_descr($interface) {
1810
	global $config;
1811
	if($interface == $config['interfaces']['wan']['if'])
1812
		return "wan";
1813
	if($interface == $config['interfaces']['lan']['if'])
1814
		return "lan";
1815
	/* attempt to resolve interface to friendly descr */
1816
	$friendly_int = convert_real_interface_to_friendly_interface_name($interface);
1817
	if($config['interfaces'][$friendly_int]['descr'])
1818
		return $config['interfaces'][$friendly_int]['descr'];
1819
	/* could not resolve, return back what was passed */
1820
	return $interface;
1821
}
1822

    
1823
?>
(14-14/27)