Project

General

Profile

Download (47 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
	}
156
	return;
157
}
158

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

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

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

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

    
227
	setup_polling_defaults();
228
	/* build an array of interfaces to work with */
229
	$iflist = array("lan" => "LAN", "wan" => "WAN");
230
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
231
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];		
232
	/*    activate polling for interface if it supports it
233
	 *    man polling on a freebsd box for the following list
234
	 */
235
	/* loop through all interfaces and handle pftpx redirections */
236
	foreach ($iflist as $ifent => $ifname) {	
237
		$supported_ints = array('dc', 'em', 'fwe', 'fwip', 'fxp', 'ixgb', 'ste',
238
			'nge', 're', 'rl', 'sf', 'sis', 'ste', 'vge', 'vr', 'xl');
239
		if (in_array($ifname, $supported_ints) and isset($config['system']['polling'])) {
240
			mwexec("/sbin/ifconfig {$interface} polling");
241
		} else {
242
			mwexec("/sbin/ifconfig {$interface} -polling");
243
		}
244
	}
245
}
246

    
247
/****f* pfsense-utils/setup_microcode
248
 * NAME
249
 *   enumerates all interfaces and calls enable_hardware_offloading which
250
 *   enables a NIC's supported hardware features.
251
 * INPUTS
252
 *   
253
 * RESULT
254
 *   null
255
 * NOTES
256
 *   This function only supports the fxp driver's loadable microcode.
257
 ******/
258
function setup_microcode() {
259
	global $config;
260

    
261
	$ifdescrs = array('wan', 'lan');
262
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
263
		$ifdescrs['opt' . $j] = "opt" . $j;
264
	}
265
	foreach($ifdescrs as $if)
266
		enable_hardware_offloading($if);
267
}
268

    
269
/****f* pfsense-utils/return_filename_as_array
270
 * NAME
271
 *   return_filename_as_array - Return a file's contents as an array.
272
 * INPUTS
273
 *   $filename	- string containing the path to the desired file.
274
 *   $strip	- array of characters to strip - default is '#'.
275
 * RESULT
276
 *   $file	- array containing the file's contents.
277
 * NOTES
278
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
279
 ******/
280
function return_filename_as_array($filename, $strip = array('#')) {
281
	if(file_exists($filename)) $file = file($filename);
282
	if(is_array($file)) {
283
		foreach($file as $line) $line = trim($line);
284
		foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
285
	}
286
	return $file;
287
}
288

    
289
/****f* pfsense-utils/file_put_contents
290
 * NAME
291
 *   file_put_contents - Wrapper for file_put_contents if it doesn't exist
292
 * RESULT
293
 *   none
294
 ******/
295
if(!function_exists("file_put_contents")) {
296
	function file_put_contents($filename, $data) {
297
		$fd = fopen($filename,"w");
298
		fwrite($fd, $data);
299
		fclose($fd);
300
	}
301
}
302

    
303
/****f* pfsense-utils/get_carp_status
304
 * NAME
305
 *   get_carp_status - Return whether CARP is enabled or disabled.
306
 * RESULT
307
 *   boolean	- true if CARP is enabled, false if otherwise.
308
 ******/
309
function get_carp_status() {
310
    /* grab the current status of carp */
311
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
312
    if(intval($status) == "0") return false;
313
    return true;
314
}
315

    
316
/****f* pfsense-utils/is_carp_defined
317
 * NAME
318
 *   is_carp_defined - Return whether CARP is detected in the kernel.
319
 * RESULT
320
 *   boolean	- true if CARP is detected, false otherwise.
321
 ******/
322
function is_carp_defined() {
323
	/* is carp compiled into the kernel and userland? */
324
	$command = "/sbin/sysctl -a | grep carp";
325
	$fd = popen($command . " 2>&1 ", "r");
326
	if(!$fd) {
327
		log_error("Warning, could not execute command {$command}");
328
		return 0;
329
	}
330
	while(!feof($fd)) {
331
		$tmp .= fread($fd,49);
332
	}
333
	fclose($fd);
334

    
335
	if($tmp == "")
336
		return false;
337
	else
338
		return true;
339
}
340

    
341
/****f* pfsense-utils/get_interface_mtu
342
 * NAME
343
 *   get_interface_mtu - Return the mtu of an interface
344
 * RESULT
345
 *   $tmp	- Returns the mtu of an interface
346
 ******/
347
function get_interface_mtu($interface) {
348
	$mtu = `/sbin/ifconfig {$interface} | /usr/bin/grep mtu | /usr/bin/cut -d" " -f4`;
349
	return $mtu;
350
}
351

    
352
/****f* pfsense-utils/is_interface_wireless
353
 * NAME
354
 *   is_interface_wireless - Returns if an interface is wireless
355
 * RESULT
356
 *   $tmp	- Returns if an interface is wireless
357
 ******/
358
function is_interface_wireless($interface) {
359
	global $config, $g;
360
	$interface = convert_real_interface_to_friendly_interface_name($interface);
361
	if(isset($config['interfaces'][$interface]['wireless']))
362
		return true;
363
	else
364
		return false;
365
}
366

    
367
/****f* pfsense-utils/find_number_of_created_carp_interfaces
368
 * NAME
369
 *   find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
370
 * RESULT
371
 *   $tmp	- Number of currently created CARP interfaces.
372
 ******/
373
function find_number_of_created_carp_interfaces() {
374
	$command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
375
	$fd = popen($command . " 2>&1 ", "r");
376
	if(!$fd) {
377
		log_error("Warning, could not execute command {$command}");
378
		return 0;
379
	}
380
	while(!feof($fd)) {
381
		$tmp .= fread($fd,49);
382
	}
383
	fclose($fd);
384
	$tmp = intval($tmp);
385
	return $tmp;
386
}
387

    
388
/****f* pfsense-utils/link_ip_to_carp_interface
389
 * NAME
390
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
391
 * INPUTS
392
 *   $ip
393
 * RESULT
394
 *   $carp_ints
395
 ******/
396
function link_ip_to_carp_interface($ip) {
397
	global $config;
398
	if($ip == "") return;
399

    
400
	$ifdescrs = array('wan', 'lan');
401
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
402
		$ifdescrs['opt' . $j] = "opt" . $j;
403
	}
404

    
405
	$ft = split("\.", $ip);
406
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
407

    
408
	$carp_ints = "";
409
	$num_carp_ints = find_number_of_created_carp_interfaces();
410
	foreach ($ifdescrs as $ifdescr => $ifname) {
411
		for($x=0; $x<$num_carp_ints; $x++) {
412
			$carp_int = "carp{$x}";
413
			$carp_ip = find_interface_ip($carp_int);
414
			$carp_ft = split("\.", $carp_ip);
415
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
416
			$result = does_interface_exist($carp_int);
417
			if($result <> true) break;
418
			if($ft_ip == $carp_ft_ip)
419
			if(stristr($carp_ints,$carp_int) == false)
420
			$carp_ints .= " " . $carp_int;
421
		}
422
	}
423
	return $carp_ints;
424
}
425

    
426
/****f* pfsense-utils/exec_command
427
 * NAME
428
 *   exec_command - Execute a command and return a string of the result.
429
 * INPUTS
430
 *   $command	- String of the command to be executed.
431
 * RESULT
432
 *   String containing the command's result.
433
 * NOTES
434
 *   This function returns the command's stdout and stderr.
435
 ******/
436
function exec_command($command) {
437
	$output = array();
438
	exec($command . ' 2>&1 ', $output);
439
	return(implode("\n", $output));
440
}
441

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

    
467
	if (in_array($int_family[0], $capable))
468
		return true;
469
	else
470
		return false;
471
}
472

    
473
/*
474
 * does_interface_exist($interface): return true or false if a interface is detected.
475
 */
476
function does_interface_exist($interface) {
477
	$ints = exec_command("/sbin/ifconfig -l");
478
	if(stristr($ints, $interface) !== false)
479
		return true;
480
	else
481
		return false;
482
}
483

    
484
/*
485
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
486
 */
487
function convert_ip_to_network_format($ip, $subnet) {
488
	$ipsplit = split('[.]', $ip);
489
	$string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
490
	return $string;
491
}
492

    
493
/*
494
 * find_interface_ip($interface): return the interface ip (first found)
495
 */
496
function find_interface_ip($interface) {
497
	if(does_interface_exist($interface) == false) return;
498
	$ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
499
	$ip = str_replace("\n","",$ip);
500
	return $ip;
501
}
502

    
503
function guess_interface_from_ip($ipaddress) {
504
	$ints = `/sbin/ifconfig -l`;
505
	$ints_split = split(" ", $ints);
506
	$ip_subnet_split = split("\.", $ipaddress);
507
	$ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
508
	foreach($ints_split as $int) {
509
		$ip = find_interface_ip($int);
510
		$ip_split = split("\.", $ip);
511
		$ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
512
		if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
513
	}
514
}
515

    
516
function filter_opt_interface_to_real($opt) {
517
	global $config;
518
	return $config['interfaces'][$opt]['if'];
519
}
520

    
521
function filter_get_opt_interface_descr($opt) {
522
	global $config;
523
	return $config['interfaces'][$opt]['descr'];
524
}
525

    
526
function get_friendly_interface_list_as_array() {
527
	global $config;
528
	$ints = array();
529
	$ifdescrs = array('wan', 'lan');
530
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
531
		$ifdescrs['opt' . $j] = "opt" . $j;
532
	}
533
	$ifdescrs = get_interface_list();
534
	foreach ($ifdescrs as $ifdescr => $ifname) {
535
		array_push($ints,$ifdescr);
536
	}
537
	return $ints;
538
}
539

    
540
/*
541
 * find_ip_interface($ip): return the interface where an ip is defined
542
 */
543
function find_ip_interface($ip) {
544
	global $config;
545
	$ifdescrs = array('wan', 'lan');
546
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
547
		$ifdescrs['opt' . $j] = "opt" . $j;
548
	}
549
	foreach ($ifdescrs as $ifdescr => $ifname) {
550
		$int = filter_translate_type_to_real_interface($ifname);
551
		$ifconfig = exec_command("/sbin/ifconfig {$int}");
552
	if(stristr($ifconfig,$ip) <> false)
553
		return $int;
554
	}
555
	return false;
556
}
557

    
558
/*
559
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
560
 *                                                       for a friendly interface.  ie: wan
561
 */
562
function filter_translate_type_to_real_interface($interface) {
563
	global $config;
564
	if($config['interfaces'][$interface]['if'] <> "") {
565
		return $config['interfaces'][$interface]['if'];
566
	} else {
567
		return $interface;
568
	}
569
}
570

    
571
/*
572
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
573
 */
574
function get_carp_interface_status($carpinterface) {
575
	/* basically cache the contents of ifconfig statement
576
	to speed up this routine */
577
	global $carp_query;
578
	if($carp_query == "")
579
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
580
	$found_interface = 0;
581
	foreach($carp_query as $int) {
582
		if($found_interface == 1) {
583
			if(stristr($int, "MASTER") == true) return "MASTER";
584
			if(stristr($int, "BACKUP") == true) return "BACKUP";
585
			if(stristr($int, "INIT") == true) return "INIT";
586
			return false;
587
		}
588
		if(stristr($int, $carpinterface) == true)
589
		$found_interface=1;
590
	}
591
	return;
592
}
593

    
594
/*
595
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
596
 */
597
function get_pfsync_interface_status($pfsyncinterface) {
598
    $result = does_interface_exist($pfsyncinterface);
599
    if($result <> true) return;
600
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
601
    return $status;
602
}
603

    
604
/*
605
 * find_carp_interface($ip): return the carp interface where an ip is defined
606
 */
607
function find_carp_interface($ip) {
608
	global $find_carp_ifconfig;
609
	if($find_carp_ifconfig == "") {
610
		$find_carp_ifconfig = array();
611
		$num_carp_ints = find_number_of_created_carp_interfaces();
612
		for($x=0; $x<$num_carp_ints; $x++) {
613
			$find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}");
614
		}
615
	}
616
	$carps = 0;
617
	foreach($find_carp_ifconfig as $fci) {
618
		if(stristr($fci, $ip) == true)
619
			return "carp{$carps}";
620
		$carps++;
621
	}
622
}
623

    
624
/*
625
 * setup_filter_bridge(): toggle filtering bridge
626
 */
627
function setup_filter_bridge() {
628
	global $config, $g;
629
	if(isset($config['bridge']['filteringbridge'])) {
630
		mwexec("/sbin/sysctl net.link.bridge.pfil_member=1");
631
		mwexec("/sbin/sysctl net.link.bridge.pfil_bridge=1");
632
	} else {		
633
		mwexec("/sbin/sysctl net.link.bridge.pfil_member=0");
634
		mwexec("/sbin/sysctl net.link.bridge.pfil_bridge=0");
635
	}
636
}
637

    
638
/*
639
 * find_number_of_created_bridges(): returns the number of currently created bridges
640
 */
641
function find_number_of_created_bridges() {
642
	return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
643
}
644

    
645
/*
646
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
647
 */
648
function add_rule_to_anchor($anchor, $rule, $label) {
649
	mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
650
}
651

    
652
/*
653
 * remove_text_from_file
654
 * remove $text from file $file
655
 */
656
function remove_text_from_file($file, $text) {
657
	global $fd_log;
658
	fwrite($fd_log, "Adding needed text items:\n");
659
	$filecontents = exec_command_and_return_text("cat " . $file);
660
	$textTMP = str_replace($text, "", $filecontents);
661
	$text .= $textTMP;
662
	fwrite($fd_log, $text . "\n");
663
	$fd = fopen($file, "w");
664
	fwrite($fd, $text);
665
	fclose($fd);
666
}
667

    
668
/*
669
 * add_text_to_file($file, $text): adds $text to $file.
670
 * replaces the text if it already exists.
671
 */
672
function add_text_to_file($file, $text) {
673
	if(file_exists($file) and is_writable($file)) {
674
		$filecontents = file($file);
675
		$filecontents[] = $text;
676
		$tmpfile = get_tmp_file();
677
		$fout = fopen($tmpfile, "w");
678
		foreach($filecontents as $line) {
679
			fwrite($fout, rtrim($line) . "\n");
680
		}
681
		fclose($fout);
682
		rename($tmpfile, $file);
683
		return true;
684
	} else {
685
		return false;
686
	}
687
}
688

    
689
/*
690
 *   after_sync_bump_adv_skew(): create skew values by 1S
691
 */
692
function after_sync_bump_adv_skew() {
693
	global $config, $g;
694
	$processed_skew = 1;
695
	$a_vip = &$config['virtualip']['vip'];
696
	foreach ($a_vip as $vipent) {
697
		if($vipent['advskew'] <> "") {
698
			$processed_skew = 1;
699
			$vipent['advskew'] = $vipent['advskew']+1;
700
		}
701
	}
702
	if($processed_skew == 1)
703
		write_config("After synch increase advertising skew");
704
}
705

    
706
/*
707
 * get_filename_from_url($url): converts a url to its filename.
708
 */
709
function get_filename_from_url($url) {
710
	return basename($url);
711
}
712

    
713
/*
714
 *   update_output_window: update bottom textarea dynamically.
715
 */
716
function update_output_window($text) {
717
	$log = ereg_replace("\n", "\\n", $text);
718
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
719
	/* ensure that contents are written out */
720
	ob_flush();
721
}
722

    
723
/*
724
 *   get_dir: return an array of $dir
725
 */
726
function get_dir($dir) {
727
	$dir_array = array();
728
	$d = dir($dir);
729
	while (false !== ($entry = $d->read())) {
730
		array_push($dir_array, $entry);
731
	}
732
	$d->close();
733
	return $dir_array;
734
}
735

    
736
/*
737
 *   update_output_window: update top textarea dynamically.
738
 */
739
function update_status($status) {
740
	echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
741
	/* ensure that contents are written out */
742
	ob_flush();
743
}
744

    
745
/*
746
 *   exec_command_and_return_text_array: execute command and return output
747
 */
748
function exec_command_and_return_text_array($command) {
749
	$fd = popen($command . " 2>&1 ", "r");
750
	while(!feof($fd)) {
751
		$tmp .= fread($fd,49);
752
	}
753
	fclose($fd);
754
	$temp_array = split("\n", $tmp);
755
	return $temp_array;
756
}
757

    
758
/*
759
 *   exec_command_and_return_text: execute command and return output
760
 */
761
function exec_command_and_return_text($command) {
762
	return exec_command($command);
763
}
764

    
765
/*
766
 *   exec_command_and_return_text: execute command and update output window dynamically
767
 */
768
function execute_command_return_output($command) {
769
	global $fd_log;
770
	$fd = popen($command . " 2>&1 ", "r");
771
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
772
	$counter = 0;
773
	$counter2 = 0;
774
	while(!feof($fd)) {
775
		$tmp = fread($fd, 50);
776
		$tmp1 = ereg_replace("\n","\\n", $tmp);
777
		$text = ereg_replace("\"","'", $tmp1);
778
		$lasttext = "";
779
		if($lasttext == "..") {
780
			$text = "";
781
			$lasttext = "";
782
			$counter=$counter-2;
783
		} else {
784
			$lasttext .= $text;
785
		}
786
		if($counter > 51) {
787
			$counter = 0;
788
			$extrabreak = "\\n";
789
		} else {
790
	    $extrabreak = "";
791
	    $counter++;
792
		}
793
		if($counter2 > 600) {
794
			echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
795
			$counter2 = 0;
796
		} else
797
			$counter2++;
798
		echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
799
	}
800
	fclose($fd);
801
}
802

    
803
/*
804
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
805
 */
806
function convert_friendly_interface_to_real_interface_name($interface) {
807
	global $config;
808
	$lc_interface = strtolower($interface);
809
	if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
810
	if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
811
	$ifdescrs = array();
812
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
813
		$ifdescrs['opt' . $j] = "opt" . $j;
814
	foreach ($ifdescrs as $ifdescr => $ifname) {
815
		if(strtolower($ifname) == $lc_interface)
816
	    return $config['interfaces'][$ifname]['if'];
817
		if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
818
			return $config['interfaces'][$ifname]['if'];
819
   }
820
   return $interface;
821
}
822

    
823
/*
824
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
825
 */
826
function convert_real_interface_to_friendly_interface_name($interface) {
827
	global $config;
828
	$ifdescrs = array('wan', 'lan');
829
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
830
		$ifdescrs['opt' . $j] = "opt" . $j;
831
	foreach ($ifdescrs as $ifdescr => $ifname) {
832
		$int = filter_translate_type_to_real_interface($ifname);
833
		if($ifname == $interface) return $ifname;
834
		if($int == $interface) return $ifname;
835
	}
836
	return $interface;
837
}
838

    
839
/*
840
 * update_progress_bar($percent): updates the javascript driven progress bar.
841
 */
842
function update_progress_bar($percent) {
843
	if($percent > 100) $percent = 1;
844
	echo "\n<script type=\"text/javascript\" language=\"javascript\">";
845
	echo "\ndocument.progressbar.style.width='" . $percent . "%';";
846
	echo "\n</script>";
847
}
848

    
849
/****f* pfsense-utils/WakeOnLan
850
 * NAME
851
 *   WakeOnLan - Wake a machine up using the wake on lan format/protocol
852
 * RESULT
853
 *   true/false - true if the operation was successful
854
 ******/
855
function WakeOnLan($addr, $mac)
856
{
857
	$addr_byte = explode(':', $mac);
858
	$hw_addr = '';
859
	
860
	for ($a=0; $a < 6; $a++)
861
		$hw_addr .= chr(hexdec($addr_byte[$a]));
862
	
863
	$msg = chr(255).chr(255).chr(255).chr(255).chr(255).chr(255);
864
	
865
	for ($a = 1; $a <= 16; $a++)
866
		$msg .= $hw_addr;
867
	
868
	// send it to the broadcast address using UDP
869
	$s = socket_create(AF_INET, SOCK_DGRAM, SOL_UDP);
870
	if ($s == false) {
871
		log_error("Error creating socket!");
872
		log_error("Error code is '".socket_last_error($s)."' - " . socket_strerror(socket_last_error($s)));
873
	} else {
874
		// setting a broadcast option to socket:
875
		$opt_ret =  socket_set_option($s, 1, 6, TRUE);
876
		if($opt_ret < 0)
877
			log_error("setsockopt() failed, error: " . strerror($opt_ret));
878
		$e = socket_sendto($s, $msg, strlen($msg), 0, $addr, 2050);
879
		socket_close($s);
880
		log_error("Magic Packet sent ({$e}) to {$addr} MAC={$mac}");
881
		return true;
882
	}
883
	
884
	return false;
885
}
886

    
887
/*
888
 * gather_altq_queue_stats():  gather altq queue stats and return an array that
889
 *                             is queuename|qlength|measured_packets
890
 *                             NOTE: this command takes 5 seconds to run
891
 */
892
function gather_altq_queue_stats($dont_return_root_queues) {
893
	mwexec("/usr/bin/killall -9 pfctl");
894
	$stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
895
	$stats_array = split("\n", $stats);
896
	$queue_stats = array();
897
	foreach ($stats_array as $stats_line) {
898
		$match_array = "";
899
		if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
900
			$queue_name = $match_array[1][0];
901
		if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
902
			$speed = $match_array[1][0];
903
		if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
904
			$borrows = $match_array[1][0];
905
		if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
906
			$suspends = $match_array[1][0];
907
		if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
908
			$drops = $match_array[1][0];
909
		if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
910
			$measured = $match_array[1][0];
911
			if($dont_return_root_queues == true)
912
				if(stristr($queue_name,"root_") == false)
913
					array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
914
		}
915
	}
916
	return $queue_stats;
917
}
918

    
919
/*
920
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
921
 *					 Useful for finding paths and stripping file extensions.
922
 */
923
function reverse_strrchr($haystack, $needle) {
924
	return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
925
}
926

    
927
/*
928
 *  backup_config_section($section): returns as an xml file string of
929
 *                                   the configuration section
930
 */
931
function backup_config_section($section) {
932
	global $config;
933
	$new_section = &$config[$section];
934
	/* generate configuration XML */
935
	$xmlconfig = dump_xml_config($new_section, $section);
936
	$xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
937
	return $xmlconfig;
938
}
939

    
940
/*
941
 *  backup_vip_config_section($section): returns as an xml file string of
942
 *                                   the configuration section
943
 */
944
function backup_vip_config_section() {
945
	global $config;
946
	$new_section = &$config['virtualip'];
947
	foreach($new_section['vip'] as $section) {
948
		if($section['mode'] == "proxyarp") {
949
			unset($section);		
950
		}
951
		if($section['advskew'] <> "") {
952
			$section_val = intval($section['advskew']);
953
			$section_val=$section_val+100;
954
			if($section_val > 255)
955
				$section_val = 255;
956
			$section['advskew'] = $section_val;
957
		}
958
		$temp['vip'][] = $section;
959
   }
960
   return $temp;
961
}
962

    
963
/*
964
 *  restore_config_section($section, new_contents): restore a configuration section,
965
 *                                                  and write the configuration out
966
 *                                                  to disk/cf.
967
 */
968
function restore_config_section($section, $new_contents) {
969
	global $config, $g;
970
	conf_mount_rw();
971
	$fout = fopen("{$g['tmp_path']}/tmpxml","w");
972
	fwrite($fout, $new_contents);
973
	fclose($fout);
974
	$section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
975
	$config[$section] = &$section_xml;
976
	unlink($g['tmp_path'] . "/tmpxml");
977
	write_config("Restored {$section} of config file (maybe from CARP partner)");
978
	conf_mount_ro();
979
	return;
980
}
981

    
982
/*
983
 * http_post($server, $port, $url, $vars): does an http post to a web server
984
 *                                         posting the vars array.
985
 * written by nf@bigpond.net.au
986
 */
987
function http_post($server, $port, $url, $vars) {
988
	$user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
989
	$urlencoded = "";
990
	while (list($key,$value) = each($vars))
991
		$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
992
	$urlencoded = substr($urlencoded,0,-1);
993
	$content_length = strlen($urlencoded);
994
	$headers = "POST $url HTTP/1.1
995
Accept: */*
996
Accept-Language: en-au
997
Content-Type: application/x-www-form-urlencoded
998
User-Agent: $user_agent
999
Host: $server
1000
Connection: Keep-Alive
1001
Cache-Control: no-cache
1002
Content-Length: $content_length
1003

    
1004
";
1005

    
1006
	$errno = "";
1007
	$errstr = "";
1008
	$fp = fsockopen($server, $port, $errno, $errstr);
1009
	if (!$fp) {
1010
		return false;
1011
	}
1012

    
1013
	fputs($fp, $headers);
1014
	fputs($fp, $urlencoded);
1015

    
1016
	$ret = "";
1017
	while (!feof($fp))
1018
		$ret.= fgets($fp, 1024);
1019
	fclose($fp);
1020

    
1021
	return $ret;
1022
}
1023

    
1024
/*
1025
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
1026
 */
1027
if (!function_exists('php_check_syntax')){
1028
	function php_check_syntax($code_to_check, &$errormessage){
1029
		return false;
1030
		$fout = fopen("/tmp/codetocheck.php","w");
1031
		$code = $_POST['content'];
1032
		$code = str_replace("<?php", "", $code);
1033
		$code = str_replace("?>", "", $code);
1034
		fwrite($fout, "<?php\n\n");
1035
		fwrite($fout, $code_to_check);
1036
		fwrite($fout, "\n\n?>\n");
1037
		fclose($fout);
1038
		$command = "/usr/local/bin/php -l /tmp/codetocheck.php";
1039
		$output = exec_command($command);
1040
		if (stristr($output, "Errors parsing") == false) {
1041
			echo "false\n";
1042
			$errormessage = '';
1043
			return(false);
1044
		} else {
1045
			$errormessage = $output;
1046
			return(true);
1047
		}
1048
	}
1049
}
1050

    
1051
/*
1052
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
1053
 */
1054
if (!function_exists('php_check_syntax')){
1055
	function php_check_syntax($code_to_check, &$errormessage){
1056
		return false;
1057
		$command = "/usr/local/bin/php -l " . $code_to_check;
1058
		$output = exec_command($command);
1059
		if (stristr($output, "Errors parsing") == false) {
1060
			echo "false\n";
1061
			$errormessage = '';
1062
			return(false);
1063
		} else {
1064
			$errormessage = $output;
1065
			return(true);
1066
		}
1067
	}
1068
}
1069

    
1070
/*
1071
 * rmdir_recursive($path,$follow_links=false)
1072
 * Recursively remove a directory tree (rm -rf path)
1073
 * This is for directories _only_
1074
 */
1075
function rmdir_recursive($path,$follow_links=false) {
1076
	$to_do = glob($path);
1077
	if(!is_array($to_do)) $to_do = array($to_do);
1078
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
1079
		if(file_exists($workingdir)) {
1080
			if(is_dir($workingdir)) {
1081
				$dir = opendir($workingdir);
1082
				while ($entry = readdir($dir)) {
1083
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
1084
						unlink("$workingdir/$entry");
1085
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
1086
						rmdir_recursive("$workingdir/$entry");
1087
				}
1088
				closedir($dir);
1089
				rmdir($workingdir);
1090
			} elseif (is_file($workingdir)) {
1091
				unlink($workingdir);
1092
			}
1093
               	}
1094
	}
1095
	return;
1096
}
1097

    
1098
/*
1099
 *     get_memory()
1100
 *     returns an array listing the amount of
1101
 *     memory installed in the hardware
1102
 *     [0]real and [1]available
1103
 */
1104
function get_memory() {
1105
	if(file_exists("/var/log/dmesg.boot")) {
1106
		$mem = `cat /var/log/dmesg.boot | grep memory`;
1107
		$matches = "";
1108
		if (preg_match_all("/real memory  = .* \((.*) MB/", $mem, $matches))
1109
			$real = $matches[1];
1110
		if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches))
1111
			$avail = $matches[1];
1112
		return array($real[0],$avail[0]);
1113
	}
1114
	return array("64","64");
1115
}
1116

    
1117
/*
1118
 *    safe_mkdir($path, $mode = 0755)
1119
 *    create directory if it doesn't already exist and isn't a file!
1120
 */
1121
function safe_mkdir($path, $mode=0755) {
1122
	global $g;
1123

    
1124
	/* cdrom is ro. */
1125
	if($g['platform'] == "cdrom")
1126
		return false;
1127
	
1128
	if (!is_file($path) && !is_dir($path))
1129
		return mkdir($path, $mode);
1130
	else
1131
		return false;
1132
}
1133

    
1134
/*
1135
 * make_dirs($path, $mode = 0755)
1136
 * create directory tree recursively (mkdir -p)
1137
 */
1138
function make_dirs($path, $mode = 0755) {
1139
	/* is dir already created? */
1140
	if(is_dir($path)) return;
1141
	/* create directory in question */
1142
	$to_create = explode("/", $path);
1143
	foreach($to_create as $tc) 
1144
		if(!is_dir($tc))
1145
			safe_mkdir($path, $mode);
1146
}
1147

    
1148
/*
1149
 * call_pfsense_method(): Call a method exposed by the pfsense.com XMLRPC server.
1150
 */
1151
function call_pfsense_method($method, $params, $timeout = 0) {
1152
	global $g, $config;
1153
	$xmlrpc_base_url = $g['xmlrpcbaseurl'];
1154
	$xmlrpc_path = $g['xmlrpcpath'];
1155
	$msg = new XML_RPC_Message($method, array(XML_RPC_Encode($params)));
1156
	$cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
1157
	$resp = $cli->send($msg, $timeout);
1158
	if(!$resp or $resp->faultCode()) {
1159
		log_error("XMLRPC request failed with error " . $resp->faultCode() . ": " . $resp->faultString());
1160
		return false;
1161
	}
1162
	return XML_RPC_Decode($resp->value());
1163
}
1164

    
1165
/*
1166
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
1167
 */
1168
function check_firmware_version($tocheck = "all", $return_php = true) {
1169
	global $g, $config;
1170
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
1171
		"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
1172
		"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
1173
		"platform" => trim(file_get_contents('/etc/platform'))
1174
		);
1175
	if($tocheck == "all") {
1176
		$params = $rawparams;
1177
	} else {
1178
		foreach($tocheck as $check) {
1179
			$params['check'] = $rawparams['check'];
1180
			$params['platform'] = $rawparams['platform'];
1181
		}
1182
	}
1183
	if($config['system']['firmware']['branch']) {
1184
		$params['branch'] = $config['system']['firmware']['branch'];
1185
	}
1186
	if(!$versions = call_pfsense_method('pfsense.get_firmware_version', $params)) {
1187
		return false;
1188
	} else {
1189
		$versions["current"] = $params;
1190
	}
1191
	return $versions;
1192
}
1193

    
1194
function get_disk_info() {
1195
	$diskout = "";
1196
	exec("/bin/df -h | /usr/bin/grep -w '/' | /usr/bin/awk '{ print $2, $3, $4, $5 }'", $diskout);
1197
	return explode(' ', $diskout[0]);
1198
	// $size, $used, $avail, $cap
1199
}
1200

    
1201
/****f* pfsense-utils/display_top_tabs
1202
 * NAME
1203
 *   display_top_tabs - display tabs with rounded edges
1204
 * INPUTS
1205
 *   $text	- array of tabs
1206
 * RESULT
1207
 *   null
1208
 ******/
1209
function display_top_tabs($tab_array) {
1210
	echo "<table cellpadding='0' cellspacing='0'>\n";
1211
	echo " <tr height='1'>\n";
1212
	$tabscounter = 0;
1213
	foreach ($tab_array as $ta) {
1214
		if($ta[1] == true) {
1215
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabactive'></div></td>\n";
1216
		} else {
1217
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
1218
		}
1219
		$tabscounter++;
1220
	}
1221
	echo "</tr>\n<tr>\n";
1222
	foreach ($tab_array as $ta) {
1223
		if($ta[1] == true) {
1224
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
1225
			echo "&nbsp;&nbsp;&nbsp;";
1226
			echo "<font size='-12'>&nbsp;</td>\n";
1227
		} else {
1228
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[2]}'>";
1229
			echo "<font color='white'>{$ta[0]}</a>&nbsp;&nbsp;&nbsp;";
1230
			echo "<font size='-12'>&nbsp;</td>\n";
1231
		}
1232
	}
1233
	echo "</tr>\n<tr height='5px'>\n";
1234
	foreach ($tab_array as $ta) {
1235
		if($ta[1] == true) {
1236
			echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
1237
		} else {
1238
			echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\" style=\"cursor: pointer;\"></td>\n";
1239
		}
1240
		$tabscounter++;
1241
	}
1242
	echo " </tr>\n";
1243
	echo "</table>\n";
1244
	    
1245
	echo "<script type=\"text/javascript\">";
1246
	echo "NiftyCheck();\n";
1247
	echo "Rounded(\"div#tabactive\",\"top\",\"#FFF\",\"#EEEEEE\",\"smooth\");\n";
1248
	for($x=0; $x<$tabscounter; $x++) 
1249
		echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"#FFF\",\"#777777\",\"smooth\");\n";
1250
	echo "</script>";
1251
}
1252

    
1253

    
1254
/****f* pfsense-utils/display_topbar
1255
 * NAME
1256
 *   display_topbar - top a table off with rounded edges
1257
 * INPUTS
1258
 *   $text	- (optional) Text to include in bar
1259
 * RESULT
1260
 *   null
1261
 ******/
1262
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {	    
1263
	echo "     <table width='100%' cellpadding='0' cellspacing='0'>\n";
1264
	echo "       <tr height='1'>\n";
1265
	echo "         <td width='100%' valign='top' color='{$bg_color}' bgcolor='{$bg_color}'>";
1266
	echo "		<div id='topbar'></div></td>\n";
1267
	echo "       </tr>\n";
1268
	echo "       <tr height='1'>\n";
1269
	if ($text != "")
1270
		echo "         <td height='1' class='listtopic'>{$text}</td>\n";
1271
	else
1272
		echo "         <td height='1' class='listtopic'></td>\n";
1273
	echo "       </tr>\n";
1274
	echo "     </table>";
1275
	echo "<script type=\"text/javascript\">";
1276
	echo "NiftyCheck();\n";
1277
	echo "Rounded(\"div#topbar\",\"top\",\"{$replace_color}\",\"{$bg_color}\",\"{$rounding_style}\");\n";
1278
	echo "</script>";
1279
}
1280

    
1281
/****f* pfsense-utils/generate_random_mac_address
1282
 * NAME
1283
 *   generate_random_mac - generates a random mac address
1284
 * INPUTS
1285
 *   none
1286
 * RESULT
1287
 *   $mac - a random mac address
1288
 ******/
1289
function generate_random_mac_address() {
1290
	$mac = "00:a0:8e";
1291
	for($x=0; $x<3; $x++) 
1292
		$mac .= ":" . dechex(rand(16, 255));
1293

    
1294
	return $mac;
1295
}
1296

    
1297
/****f* pfsense-utils/strncpy
1298
 * NAME
1299
 *   strncpy - copy strings
1300
 * INPUTS
1301
 *   &$dst, $src, $length
1302
 * RESULT
1303
 *   none
1304
 ******/
1305
function strncpy(&$dst, $src, $length) {
1306
	if (strlen($src) > $length) {
1307
		$dst = substr($src, 0, $length);
1308
	} else {
1309
		$dst = $src;
1310
	}
1311
}
1312

    
1313
/****f* pfsense-utils/reload_interfaces_sync
1314
 * NAME
1315
 *   reload_interfaces - reload all interfaces
1316
 * INPUTS
1317
 *   none
1318
 * RESULT
1319
 *   none
1320
 ******/
1321
function reload_interfaces_sync() {
1322
	global $config, $g;
1323
	
1324
	touch("{$g['tmp_path']}/reloading_all");
1325
	
1326
	if(file_exists("{$g['tmp_path']}/config.cache"))
1327
		unlink("{$g['tmp_path']}/config.cache");
1328
	
1329
	/* parse config.xml again */
1330
	$config = parse_config(true);
1331
	
1332
	/* delete all old interface information */
1333
	$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
1334
	foreach ($iflist as $ifent => $ifname) {
1335
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1336
		mwexec("/sbin/ifconfig {$ifname_real} down");
1337
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1338
	}
1339

    
1340
	/* set up VLAN virtual interfaces */
1341
	interfaces_vlan_configure();
1342
	
1343
	/* set up LAN interface */
1344
	interfaces_lan_configure();
1345

    
1346
	/* set up WAN interface */
1347
	interfaces_wan_configure();
1348

    
1349
	/* set up Optional interfaces */
1350
	interfaces_optional_configure();
1351
        
1352
	/* set up static routes */
1353
	system_routing_configure();
1354
	
1355
	/* enable routing */
1356
	system_routing_enable();
1357
	
1358
	/* setup captive portal if needed */
1359
	captiveportal_configure();
1360

    
1361
	/* bring up carp interfaces */
1362
	interfaces_carp_configure();
1363
	
1364
	/* bring up carp interfaces*/
1365
	interfaces_carp_bring_up_final();
1366
	
1367
	unlink_if_exists("{$g['tmp_path']}/reloading_all");
1368
}
1369

    
1370
/****f* pfsense-utils/reload_all
1371
 * NAME
1372
 *   reload_all - triggers a reload of all settings
1373
 *   * INPUTS
1374
 *   none
1375
 * RESULT
1376
 *   none
1377
 ******/
1378
function reload_all() {
1379
	touch("/tmp/reload_all");
1380
}
1381

    
1382
/****f* pfsense-utils/reload_interfaces
1383
 * NAME
1384
 *   reload_interfaces - triggers a reload of all interfaces
1385
 * INPUTS
1386
 *   none
1387
 * RESULT
1388
 *   none
1389
 ******/
1390
function reload_interfaces() {
1391
	touch("/tmp/reload_interfaces");
1392
}
1393

    
1394
/****f* pfsense-utils/sync_webgui_passwords
1395
 * NAME
1396
 *   sync_webgui_passwords - syncs webgui and ssh passwords
1397
 * INPUTS
1398
 *   none
1399
 * RESULT
1400
 *   none
1401
 ******/
1402
function sync_webgui_passwords() {
1403
	global $config, $g;
1404
	conf_mount_rw();
1405
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
1406
	if (!$fd) {
1407
		printf("Error: cannot open htpasswd in system_password_configure().\n");
1408
		return 1;
1409
	}
1410
	/* set admin account */
1411
	$username = $config['system']['username'];
1412
	
1413
	/* set defined user account */
1414
	if($username <> "admin") {
1415
		$username = $config['system']['username'];
1416
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
1417
	} else {
1418
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");	
1419
	}	
1420
	fclose($fd);
1421
	chmod("{$g['varrun_path']}/htpasswd", 0600);	
1422
	$crypted_pw = $config['system']['password'];
1423
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1424
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1425
	/* sync root */
1426
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
1427
	fwrite($fd, $crypted_pw);
1428
	pclose($fd);
1429
	mwexec("/usr/sbin/pw usermod -n root -s /bin/sh");
1430
	/* sync admin */
1431
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
1432
	fwrite($fd, $crypted_pw);
1433
	pclose($fd);
1434
	mwexec("/usr/sbin/pw usermod -n admin -s /etc/rc.initial");
1435
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1436
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1437
	conf_mount_ro();
1438
}
1439

    
1440
/****f* pfsense-utils/cleanup_opt_interfaces_after_removal
1441
 * NAME
1442
 *   cleanup_opt_interfaces_after_removal - renumber interfaces after removing
1443
 *   * INPUTS
1444
 *   optional interface number
1445
 * RESULT
1446
 *   none
1447
 ******/
1448
function cleanup_opt_interfaces_after_removal($opt_interface_num) {
1449
	global $config;
1450
	unlink_if_exists("/tmp/config.cache");
1451
	$config_file = file_get_contents("/cf/conf/config.xml");
1452
	/* loop through and reassign optional items */
1453
	for ($i = 255; isset($config['interfaces']['opt' . $i]); $i--) {
1454
		if($i < $opt_interface_num)
1455
			break;
1456
		/* replace opt$i with $i -1 */
1457
		str_replace("opt" . $i, "opt" . ($i-1), $config_file);
1458
	}
1459
	$fd = fopen("/cf/conf/config.xml", "w");
1460
	fwrite($fd, $config_file);
1461
	fclose($fd);
1462
	$config = parse_config(true);
1463
	return true;
1464
}
1465

    
1466
/****f* pfsense-utils/get_number_of_wan_netgraph_interfaces_needed
1467
 * NAME
1468
 *   get_number_of_wan_netgraph_interfaces_needed - returns the
1469
 *   		amount of netgraph interfaces needed for system wans
1470
 *   * INPUTS
1471
 *   none
1472
 * RESULT
1473
 *   number of needed netgraph (ng) interfaces
1474
 ******/
1475
function get_number_of_wan_netgraph_interfaces_needed() {
1476
	global $config, $g;
1477
	/* build an array of interfaces to work with */
1478
	$iflist = array("wan" => "WAN");
1479
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
1480
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
1481
	$ng_interfaces_needed = 0;
1482
	foreach ($iflist as $ifent => $ifname) {
1483
		if($config['inerfaces'][$ifname]['ipaddr'] == "pppoe") {
1484
			$ng_interfaces_needed++;
1485
		}
1486
	}
1487
	return $ng_interfaces_needed;
1488
}
1489

    
1490
function get_netgaph_interface_assignment($friendly_interface) {
1491
	global $config, $g;
1492
	/* build an array of interfaces to work with */
1493
	$iflist = array("wan" => "WAN");
1494
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
1495
		$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];
1496
		$ng_interfaces_needed = 0;
1497
		$ng_interfaces_number = 0;
1498
		foreach ($iflist as $ifent => $ifname) {
1499
		if($config['inerfaces'][$ifname]['ipaddr'] == "pppoe") {
1500
			$ng_interfaces_number++;
1501
		}
1502
		if($friendly_interface == $ifname)
1503
			break;
1504
	}
1505
	return $ng_interfaces_number;	
1506
}
1507

    
1508
/****f* pfsense-utils/reload_all_sync
1509
 * NAME
1510
 *   reload_all - reload all settings
1511
 *   * INPUTS
1512
 *   none
1513
 * RESULT
1514
 *   none
1515
 ******/
1516
function reload_all_sync() {
1517
	global $config, $g;
1518
	
1519
	touch("{$g['tmp_path']}/reloading_all");
1520
	
1521
	if(file_exists("{$g['tmp_path']}/config.cache"))
1522
		unlink("{$g['tmp_path']}/config.cache");
1523
	
1524
	/* parse config.xml again */
1525
	$config = parse_config(true);
1526

    
1527
	/* set up our timezone */
1528
	system_timezone_configure();
1529

    
1530
	/* set up our hostname */
1531
	system_hostname_configure();
1532

    
1533
	/* make hosts file */
1534
	system_hosts_generate();
1535

    
1536
	/* generate resolv.conf */
1537
	system_resolvconf_generate();
1538

    
1539
	/* delete all old interface information */
1540
	$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
1541
	foreach ($iflist as $ifent => $ifname) {
1542
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1543
		if($ifname_real == "lo0")
1544
		    continue;
1545
		mwexec("/sbin/ifconfig {$ifname_real} down");
1546
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1547
	}
1548

    
1549
	/* set up VLAN virtual interfaces */
1550
	interfaces_vlan_configure();
1551
	
1552
	/* set up LAN interface */
1553
	interfaces_lan_configure();
1554

    
1555
	/* set up WAN interface */
1556
	interfaces_wan_configure();
1557

    
1558
	/* set up Optional interfaces */
1559
	interfaces_optional_configure();
1560
        
1561
	/* bring up carp interfaces */
1562
	interfaces_carp_configure();
1563
	
1564
	/* set up static routes */
1565
	system_routing_configure();
1566

    
1567
	/* enable routing */
1568
	system_routing_enable();
1569
	
1570
	/* ensure passwords are sync'd */
1571
	system_password_configure();
1572

    
1573
	/* start dnsmasq service */
1574
	services_dnsmasq_configure();
1575

    
1576
	/* start dyndns service */
1577
	services_dyndns_configure();
1578

    
1579
	/* start DHCP service */
1580
	services_dhcpd_configure();
1581

    
1582
	/* start the NTP client */
1583
	system_ntp_configure();
1584

    
1585
	/* start ftp proxy helpers if they are enabled */
1586
	system_start_ftp_helpers();
1587

    
1588
	/* start the captive portal */
1589
	captiveportal_configure();
1590

    
1591
        /* reload the filter */
1592
	filter_configure_sync();
1593

    
1594
	/* bring up carp interfaces*/
1595
	interfaces_carp_bring_up_final();
1596
	
1597
	/* sync pw database */
1598
	conf_mount_rw();
1599
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
1600
	conf_mount_ro();
1601

    
1602
	/* restart sshd */
1603
	touch("/tmp/start_sshd");
1604
	
1605
	unlink_if_exists("{$g['tmp_path']}/reloading_all");
1606
	
1607
}
1608

    
1609
function auto_login($status) {
1610
	$gettytab = file_get_contents("/etc/gettytab");
1611
	$getty_split = split("\n", $gettytab);
1612
	conf_mount_rw();
1613
	$fd = fopen("/etc/gettytab", "w");
1614
	foreach($getty_split as $gs) {
1615
		if(stristr($gs, "cb:ce:ck:lc") == true) {
1616
			if($status == true) {
1617
				fwrite($fd, ":cb:ce:ck:lc:fd#1000:im=\\r\\n%s/%m (%h) (%t)\\r\\n\\r\\n:sp#1200:\\\n");
1618
			} else {
1619
				fwrite($fd, ":al=root:cb:ce:ck:lc:fd#1000:im=\\r\\n%s/%m (%h) (%t)\\r\\n\\r\\n:sp#1200:\\\n");
1620
			}
1621
		} else {
1622
			fwrite($fd, "{$gs}\n");
1623
		}
1624
	}
1625
	fclose($fd);
1626
	conf_mount_ro();	
1627
}
1628

    
1629
function setup_serial_port() {
1630
	global $g, $config;
1631
	conf_mount_rw();
1632
	/* serial console - write out /boot.config */
1633
	if(file_exists("/boot.config"))
1634
		$boot_config = file_get_contents("/boot.config");
1635
	else
1636
		$boot_config = "";
1637
	
1638
	if($g['platform'] <> "cdrom") {
1639
		$boot_config_split = split("\n", $boot_config);
1640
		$fd = fopen("/boot.config","w");
1641
		if($fd) {
1642
			foreach($boot_config_split as $bcs) {
1643
				if(stristr($bcs, "-D")) {
1644
					/* DONT WRITE OUT, WE'LL DO IT LATER */	
1645
				} else {
1646
					if($bcs <> "")
1647
						fwrite($fd, "{$bcs}\n");
1648
				}
1649
			}
1650
			if(isset($config['system']['enableserial'])) {
1651
				fwrite($fd, "-D");
1652
			}			
1653
			fclose($fd);
1654
		}
1655
		/* serial console - write out /boot/loader.conf */
1656
		$boot_config = file_get_contents("/boot/loader.conf");	
1657
		$boot_config_split = split("\n", $boot_config);
1658
		$fd = fopen("/boot/loader.conf","w");
1659
		if($fd) {
1660
			foreach($boot_config_split as $bcs) {
1661
				if(stristr($bcs, "console")) {
1662
					/* DONT WRITE OUT, WE'LL DO IT LATER */	
1663
				} else {
1664
					if($bcs <> "")
1665
						fwrite($fd, "{$bcs}\n");
1666
				}
1667
			}
1668
			if(isset($config['system']['enableserial'])) {
1669
				fwrite($fd, "console=\"comconsole\"\n");
1670
			}
1671
			fclose($fd);
1672
		}
1673
	}
1674
	if(isset($config['system']['disableconsolemenu'])) {
1675
		auto_login(true);
1676
	} else {
1677
		auto_login(false);
1678
	}	
1679
	conf_mount_ro();
1680
	return;	
1681
}
1682

    
1683
function print_value_list($list, $count = 10, $separator = ",") {
1684
	$list = implode($separator, array_slice($list, 0, $count));
1685
	if(count($list) < $count) {
1686
		$list .= ".";
1687
	} else {
1688
		$list .= "...";
1689
	}
1690
	return $list;
1691
}
1692

    
1693
?>
(14-14/27)