Project

General

Profile

Download (42.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/****h* pfSense/pfsense-utils
3
 * NAME
4
 *   pfsense-utils.inc - Utilities specific to pfSense
5
 * DESCRIPTION
6
 *   This include contains various pfSense specific functions.
7
 * HISTORY
8
 *   $Id$
9
 ******
10
 *
11
 * Copyright (C) 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
	$lastseen = "";
50
	$matches = "";
51
	$dns_servers = array();
52
	$dns = `cat /etc/resolv.conf`;
53
	$dns_s = split("\n", $dns);
54
	foreach($dns_s as $dns) {
55
		if (preg_match("/nameserver (.*)/", $dns, $matches))
56
			$dns_servers[] = $matches[1];		
57
	}
58
	$dns_server_master = array();
59
	sort($dns_servers);
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
    if(isset($config['system']['do_not_use_nic_microcode']))
134
	return;
135
    if($g['booting']) {
136
	/* translate wan, lan, opt -> real interface if needed */
137
	$int = filter_translate_type_to_real_interface($interface);
138
	if(stristr($int,"lnc"))
139
		return;    	
140
	if($int <> "") $interface = $int;
141
        $int_family = preg_split("/[0-9]+/", $int);
142
	$options = strtolower(`/sbin/ifconfig {$interface} | grep options`);
143
	echo $interface . " ";
144
	$supported_ints = array('fxp');
145
	if (in_array($int_family, $supported_ints))
146
		mwexec("/sbin/ifconfig {$interface} link0");
147
	if(stristr($options, "txcsum") == true)
148
	    mwexec("/sbin/ifconfig {$interface} txcsum 2>/dev/null");
149
	if(stristr($options, "rxcsum") == true)    
150
	    mwexec("/sbin/ifconfig {$interface} rxcsum 2>/dev/null");    
151
	if(stristr($options, "polling") == true)
152
	    mwexec("/sbin/ifconfig {$interface} polling 2>/dev/null");
153
    }
154
    return;
155
}
156

    
157
/****f* pfsense-utils/is_alias_inuse
158
 * NAME
159
 *   checks to see if an alias is currently in use by a rule
160
 * INPUTS
161
 *   
162
 * RESULT
163
 *   true or false
164
 * NOTES
165
 *   
166
 ******/
167
function is_alias_inuse($alias) {
168
    global $g, $config;
169
    if($alias == "") return false;
170
    /* loop through firewall rules looking for alias in use */
171
    if(is_array($config['nat']['rule']))
172
	    foreach($config['filter']['rule'] as $rule) {
173
			if(is_array($rule['source']['address']))
174
				if($rule['source']['address'] == $alias)
175
					return true;
176
			if(is_array($rule['destination']['address']))
177
				if($rule['destination']['address'] == $alias)
178
					return true;
179
	    }
180
    /* loop through nat rules looking for alias in use */
181
    if(is_array($config['nat']['rule']))
182
	    foreach($config['nat']['rule'] as $rule) {
183
			if($rule['target'] == $alias)
184
				return true;
185
			if($rule['external-address'] == $alias)
186
				return true;	
187
	    }
188
    return false;
189
}
190

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

    
211
/****f* pfsense-utils/setup_polling
212
 * NAME
213
 *   sets up polling
214
 * INPUTS
215
 *   
216
 * RESULT
217
 *   null
218
 * NOTES
219
 *   
220
 ******/
221
function setup_polling() {
222
	setup_polling_defaults();
223
	global $g, $config;
224
	/* build an array of interfaces to work with */
225
	$iflist = array("lan" => "LAN", "wan" => "WAN");
226
	for ($i = 1; isset($config['interfaces']['opt' . $i]); $i++) 
227
	$iflist['opt' . $i] = $config['interfaces']['opt' . $i]['descr'];		
228
	/*    activate polling for interface if it supports it
229
	 *    man polling on a freebsd box for the following list
230
	 */
231
	/* loop through all interfaces and handle pftpx redirections */
232
	foreach ($iflist as $ifent => $ifname) {	
233
		$supported_ints = array('dc', 'em', 'fwe', 'fwip', 'fxp', 'ixgb', 'ste',
234
			'nge', 're', 'rl', 'sf', 'sis', 'ste', 'vge', 'vr', 'xl');
235
		if (in_array($ifname, $supported_ints) and isset($config['system']['polling'])) {
236
			mwexec("/sbin/ifconfig {$interface} polling");
237
		} else {
238
			mwexec("/sbin/ifconfig {$interface} -polling");
239
		}
240
	}
241
}
242

    
243
/****f* pfsense-utils/setup_microcode
244
 * NAME
245
 *   enumerates all interfaces and calls enable_hardware_offloading which
246
 *   enables a NIC's supported hardware features.
247
 * INPUTS
248
 *   
249
 * RESULT
250
 *   null
251
 * NOTES
252
 *   This function only supports the fxp driver's loadable microcode.
253
 ******/
254
function setup_microcode() {
255
   global $config;
256
    $ifdescrs = array('wan', 'lan');
257
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
258
	$ifdescrs['opt' . $j] = "opt" . $j;
259
    }
260
    foreach($ifdescrs as $if)
261
	enable_hardware_offloading($if);
262
}
263

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

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

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

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

    
330
    if($tmp == "")
331
	return false;
332
    else
333
	return true;
334
}
335

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

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

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

    
383
/****f* pfsense-utils/link_ip_to_carp_interface
384
 * NAME
385
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
386
 * INPUTS
387
 *   $ip
388
 * RESULT
389
 *   $carp_ints
390
 ******/
391
function link_ip_to_carp_interface($ip) {
392
	global $config;
393
	if($ip == "") return;
394

    
395
	$ifdescrs = array('wan', 'lan');
396
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
397
		$ifdescrs['opt' . $j] = "opt" . $j;
398
	}
399

    
400
	$ft = split("\.", $ip);
401
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
402

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

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

    
437
/****f* interfaces/is_jumbo_capable
438
 * NAME
439
 *   is_jumbo_capable - Test if interface is jumbo frame capable.  Useful for determining VLAN capability.
440
 * INPUTS
441
 *   $int             - string containing interface name
442
 * RESULT
443
 *   boolean          - true or false
444
 ******/
445
function is_jumbo_capable($int) {
446
	/* Per:
447
	 * http://www.freebsd.org/cgi/man.cgi?query=vlan&manpath=FreeBSD+6.0-RELEASE&format=html
448
	 * Only the following drivers support large frames
449
	 */
450
	/* 'de' chipset purposely left out of this list
451
	 * requires defining BIG_PACKET in the
452
	 * /usr/src/sys/pci/if_de.c source file and rebuilding the
453
	 * kernel or module.  The hack works only for the 21041,
454
	 * 21140, and 21140A chips.
455
	 */
456
	$capable = array("bfe", "bge", "dc", "em", "fxp", "gem", "hme", 
457
		"ixgb", "nge", "re", "rl", "sis", "ste", "ti", "tl", "tx",
458
		"txp", "xl", "sk");
459
	
460
	$int_family = preg_split("/[0-9]+/", $int);
461

    
462
	if (in_array($int_family[0], $capable))
463
		return true;
464
	else
465
		return false;
466
}
467

    
468
/*
469
 * does_interface_exist($interface): return true or false if a interface is detected.
470
 */
471
function does_interface_exist($interface) {
472
    $ints = exec_command("/sbin/ifconfig -l");
473
    if(stristr($ints, $interface) !== false)
474
	return true;
475
    else
476
	return false;
477
}
478

    
479
/*
480
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
481
 */
482
function convert_ip_to_network_format($ip, $subnet) {
483
    $ipsplit = split('[.]', $ip);
484
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
485
    return $string;
486
}
487

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

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

    
511
function filter_opt_interface_to_real($opt) {
512
    global $config;
513
    return $config['interfaces'][$opt]['if'];
514
}
515

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

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

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

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

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

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

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

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

    
633
/*
634
 * find_number_of_created_bridges(): returns the number of currently created bridges
635
 */
636
function find_number_of_created_bridges() {
637
    return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
638
}
639

    
640
/*
641
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
642
 */
643
function add_rule_to_anchor($anchor, $rule, $label) {
644
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
645
}
646

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

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

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

    
701
/*
702
 * get_filename_from_url($url): converts a url to its filename.
703
 */
704
function get_filename_from_url($url) {
705
	return basename($url);
706
}
707

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

    
718
/*
719
 *   get_dir: return an array of $dir
720
 */
721
function get_dir($dir) {
722
    $dir_array = array();
723
    $d = dir($dir);
724
    while (false !== ($entry = $d->read())) {
725
	array_push($dir_array, $entry);
726
    }
727
    $d->close();
728
    return $dir_array;
729
}
730

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

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

    
753
/*
754
 *   exec_command_and_return_text: execute command and return output
755
 */
756
function exec_command_and_return_text($command) {
757
    return exec_command($command);
758
}
759

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

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

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

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

    
844
/*
845
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
846
 *                             is queuename|qlength|measured_packets
847
 *                             NOTE: this command takes 5 seconds to run
848
 */
849
function gather_altq_queue_stats($dont_return_root_queues) {
850
    mwexec("/usr/bin/killall -9 pfctl");
851
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
852
    $stats_array = split("\n", $stats);
853
    $queue_stats = array();
854
    $match_array = "";
855
    foreach ($stats_array as $stats_line) {
856
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
857
            $queue_name = $match_array[1][0];
858
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
859
            $speed = $match_array[1][0];
860
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
861
            $borrows = $match_array[1][0];
862
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
863
            $suspends = $match_array[1][0];
864
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
865
            $drops = $match_array[1][0];
866
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
867
            $measured = $match_array[1][0];
868
	    if($dont_return_root_queues == true)
869
		if(stristr($queue_name,"root_") == false)
870
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
871
        }
872
    }
873
    return $queue_stats;
874
}
875

    
876
/*
877
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
878
 *					 Useful for finding paths and stripping file extensions.
879
 */
880
function reverse_strrchr($haystack, $needle)
881
{
882
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
883
}
884

    
885
/*
886
 *  backup_config_section($section): returns as an xml file string of
887
 *                                   the configuration section
888
 */
889
function backup_config_section($section) {
890
    global $config;
891
    $new_section = &$config[$section];
892
    /* generate configuration XML */
893
    $xmlconfig = dump_xml_config($new_section, $section);
894
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
895
    return $xmlconfig;
896
}
897

    
898
/*
899
 *  backup_config_section($section): returns as an xml file string of
900
 *                                   the configuration section
901
 */
902
function backup_vip_config_section() {
903
    global $config;
904
    $new_section = &$config['virtualip'];
905
    foreach($new_section['vip'] as $section) {
906
	if($section['mode'] == "proxyarp") {
907
		unset($section);		
908
	}
909
	if($section['advskew'] <> "") {
910
		$section_val = intval($section['advskew']);
911
		$section_val=$section_val+100;
912
		if($section_val > 255)
913
			$section_val = 255;
914
		$section['advskew'] = $section_val;
915
	}
916
	$temp['vip'][] = $section;
917
    }
918
    return $temp;
919
}
920

    
921
/*
922
 *  restore_config_section($section, new_contents): restore a configuration section,
923
 *                                                  and write the configuration out
924
 *                                                  to disk/cf.
925
 */
926
function restore_config_section($section, $new_contents) {
927
    global $config, $g;
928
    conf_mount_rw();
929
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
930
    fwrite($fout, $new_contents);
931
    fclose($fout);
932
    $section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
933
    $config[$section] = &$section_xml;
934
    unlink($g['tmp_path'] . "/tmpxml");
935
    write_config("Restored {$section} of config file (maybe from CARP partner)");
936
    conf_mount_ro();
937
    return;
938
}
939

    
940
/*
941
 * http_post($server, $port, $url, $vars): does an http post to a web server
942
 *                                         posting the vars array.
943
 * written by nf@bigpond.net.au
944
 */
945
function http_post($server, $port, $url, $vars) {
946
	global $errstr;
947
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
948
    $urlencoded = "";
949
    while (list($key,$value) = each($vars))
950
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
951
    $urlencoded = substr($urlencoded,0,-1);
952

    
953
    $content_length = strlen($urlencoded);
954

    
955
    $headers = "POST $url HTTP/1.1
956
Accept: */*
957
Accept-Language: en-au
958
Content-Type: application/x-www-form-urlencoded
959
User-Agent: $user_agent
960
Host: $server
961
Connection: Keep-Alive
962
Cache-Control: no-cache
963
Content-Length: $content_length
964

    
965
";
966

    
967
	$errno = "";
968
    $fp = fsockopen($server, $port, $errno, $errstr);
969
    if (!$fp) {
970
	return false;
971
    }
972

    
973
    fputs($fp, $headers);
974
    fputs($fp, $urlencoded);
975

    
976
    $ret = "";
977
    while (!feof($fp))
978
	$ret.= fgets($fp, 1024);
979

    
980
    fclose($fp);
981

    
982
    return $ret;
983

    
984
}
985

    
986
/*
987
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
988
 */
989
if (!function_exists('php_check_syntax')){
990
   function php_check_syntax($code_to_check, &$errormessage){
991
	return false;
992
        $fout = fopen("/tmp/codetocheck.php","w");
993
        $code = $_POST['content'];
994
        $code = str_replace("<?php", "", $code);
995
        $code = str_replace("?>", "", $code);
996
        fwrite($fout, "<?php\n\n");
997
        fwrite($fout, $code_to_check);
998
        fwrite($fout, "\n\n?>\n");
999
        fclose($fout);
1000
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
1001
        $output = exec_command($command);
1002
        if (stristr($output, "Errors parsing") == false) {
1003
            echo "false\n";
1004
            $errormessage = '';
1005
            return(false);
1006
        } else {
1007
            $errormessage = $output;
1008
            return(true);
1009
        }
1010
    }
1011
}
1012

    
1013
/*
1014
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
1015
 */
1016
if (!function_exists('php_check_syntax')){
1017
   function php_check_syntax($code_to_check, &$errormessage){
1018
	return false;
1019
        $command = "/usr/local/bin/php -l " . $code_to_check;
1020
        $output = exec_command($command);
1021
        if (stristr($output, "Errors parsing") == false) {
1022
            echo "false\n";
1023
            $errormessage = '';
1024
            return(false);
1025
        } else {
1026
            $errormessage = $output;
1027
            return(true);
1028
        }
1029
    }
1030
}
1031

    
1032
/*
1033
 * rmdir_recursive($path,$follow_links=false)
1034
 * Recursively remove a directory tree (rm -rf path)
1035
 * This is for directories _only_
1036
 */
1037
function rmdir_recursive($path,$follow_links=false) {
1038
	$to_do = glob($path);
1039
	if(!is_array($to_do)) $to_do = array($to_do);
1040
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
1041
		if(file_exists($workingdir)) {
1042
			if(is_dir($workingdir)) {
1043
				$dir = opendir($workingdir);
1044
				while ($entry = readdir($dir)) {
1045
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
1046
						unlink("$workingdir/$entry");
1047
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
1048
						rmdir_recursive("$workingdir/$entry");
1049
				}
1050
				closedir($dir);
1051
				rmdir($workingdir);
1052
			} elseif (is_file($workingdir)) {
1053
				unlink($workingdir);
1054
			}
1055
               	}
1056
	}
1057
	return;
1058
}
1059

    
1060
/*
1061
 *     get_memory()
1062
 *     returns an array listing the amount of
1063
 *     memory installed in the hardware
1064
 *     [0]real and [1]available
1065
 */
1066
function get_memory() {
1067
	if(file_exists("/var/log/dmesg.boot")) {
1068
		$matches = "";
1069
		$mem = `cat /var/log/dmesg.boot | grep memory`;
1070
		if (preg_match_all("/real memory  = .* \((.*) MB/", $mem, $matches))
1071
			$real = $matches[1];
1072
		if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches))
1073
			$avail = $matches[1];
1074
		return array($real[0],$avail[0]);
1075
	}
1076
	return array("64","64");
1077
}
1078

    
1079

    
1080
/*
1081
 *    safe_mkdir($path, $mode = 0755)
1082
 *    create directory if it doesn't already exist and isn't a file!
1083
 */
1084
function safe_mkdir($path, $mode=0755) {
1085
	global $g;
1086

    
1087
	/* cdrom is ro. */
1088
	if($g['platform'] == "cdrom")
1089
		return false;
1090
	
1091
	if (!is_file($path) && !is_dir($path))
1092
		return mkdir($path, $mode);
1093
	else
1094
		return false;
1095
}
1096

    
1097
/*
1098
 * make_dirs($path, $mode = 0755)
1099
 * create directory tree recursively (mkdir -p)
1100
 */
1101
function make_dirs($path, $mode = 0755) {
1102
	/* is dir already created? */
1103
	if(is_dir($path)) return;
1104
	/* create directory in question */
1105
	$to_create = explode("/", $path);
1106
	foreach($to_create as $tc) 
1107
	    if(!is_dir($tc))
1108
		safe_mkdir($path, $mode);
1109
}
1110

    
1111
/*
1112
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
1113
 */
1114
function check_firmware_version($tocheck = "all", $return_php = true) {
1115
        global $g, $config;
1116
	$xmlrpc_base_url = $g['xmlrpcbaseurl'];
1117
        $xmlrpc_path = $g['xmlrpcpath'];
1118
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
1119
			"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
1120
			"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
1121
			"platform" => trim(file_get_contents('/etc/platform'))
1122
		);
1123
	if($tocheck == "all") {
1124
		$params = $rawparams;
1125
	} else {
1126
		foreach($tocheck as $check) {
1127
			$params['check'] = $rawparams['check'];
1128
			$params['platform'] = $rawparams['platform'];
1129
		}
1130
	}
1131
	if($config['system']['firmware']['branch']) {
1132
		$params['branch'] = $config['system']['firmware']['branch'];
1133
	}
1134
	$xmlparams = php_value_to_xmlrpc($params);
1135
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
1136
        $cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
1137
	//$cli->setDebug(1);
1138
	$resp = $cli->send($msg, 10);
1139
	if(!$resp or $resp->faultCode()) {
1140
		$raw_versions = false;
1141
	} else {
1142
		$raw_versions = XML_RPC_decode($resp->value());
1143
		$raw_versions["current"] = $params;
1144
	}
1145
	return $raw_versions;
1146
}
1147

    
1148
function get_disk_info() {
1149
		$diskout = "";
1150
        exec("df -h | grep -w '/' | awk '{ print $2, $3, $4, $5 }'", $diskout);
1151
        return explode(' ', $diskout[0]);
1152
        // $size, $used, $avail, $cap
1153
}
1154

    
1155
/****f* pfsense-utils/display_top_tabs
1156
 * NAME
1157
 *   display_top_tabs - display tabs with rounded edges
1158
 * INPUTS
1159
 *   $text	- array of tabs
1160
 * RESULT
1161
 *   null
1162
 ******/
1163
    function display_top_tabs($tab_array) {
1164
	    echo "<table cellpadding='0' cellspacing='0'>\n";
1165
	    echo " <tr height='1'>\n";
1166
	    $tabscounter = 0;
1167
	    foreach ($tab_array as $ta) {
1168
		    if($ta[1] == true) {
1169
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><div id='tabactive'></div></td>\n";
1170
		    } else {
1171
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
1172
		    }
1173
		    $tabscounter++;
1174
	    }
1175
	    echo "</tr>\n<tr>\n";
1176
	    foreach ($tab_array as $ta) {
1177
		    if($ta[1] == true) {
1178
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
1179
			    echo "&nbsp;&nbsp;&nbsp;";
1180
			    echo "<font size='-12'>&nbsp;</td>\n";
1181
		    } else {
1182
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[2]}'>";
1183
			    echo "<font color='white'>{$ta[0]}</a>&nbsp;&nbsp;&nbsp;";
1184
			    echo "<font size='-12'>&nbsp;</td>\n";
1185
		    }
1186
	    }
1187
	    echo "</tr>\n<tr height='5px'>\n";
1188
	    foreach ($tab_array as $ta) {
1189
		    if($ta[1] == true) {
1190
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"></td>\n";
1191
		    } else {
1192
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"></td>\n";
1193
		    }
1194
		    $tabscounter++;
1195
	    }
1196
	    echo " </tr>\n";
1197
	    echo "</table>\n";
1198
	    
1199
	    echo "<script type=\"text/javascript\">";
1200
	    echo "NiftyCheck();\n";
1201
	    echo "Rounded(\"div#tabactive\",\"top\",\"#FFF\",\"#EEEEEE\",\"smooth\");\n";
1202
	    for($x=0; $x<$tabscounter; $x++) 
1203
		    echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"#FFF\",\"#777777\",\"smooth\");\n";
1204
	    echo "</script>";
1205
    }
1206

    
1207

    
1208
/****f* pfsense-utils/display_topbar
1209
 * NAME
1210
 *   display_topbar - top a table off with rounded edges
1211
 * INPUTS
1212
 *   $text	- (optional) Text to include in bar
1213
 * RESULT
1214
 *   null
1215
 ******/
1216
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {	    
1217
	echo "     <table width='100%' cellpadding='0' cellspacing='0'>\n";
1218
	echo "       <tr height='1'>\n";
1219
	echo "         <td width='100%' valign='top' color='{$bg_color}' bgcolor='{$bg_color}'>";
1220
	echo "		<div id='topbar'></div></td>\n";
1221
	echo "       </tr>\n";
1222
	echo "       <tr height='1'>\n";
1223
	if ($text != "")
1224
		echo "         <td height='1' class='listtopic'>{$text}</td>\n";
1225
	else
1226
		echo "         <td height='1' class='listtopic'></td>\n";
1227
	echo "       </tr>\n";
1228
	echo "     </table>";
1229
	echo "<script type=\"text/javascript\">";
1230
	echo "NiftyCheck();\n";
1231
	echo "Rounded(\"div#topbar\",\"top\",\"{$replace_color}\",\"{$bg_color}\",\"{$rounding_style}\");\n";
1232
	echo "</script>";
1233
}
1234

    
1235
/****f* pfsense-utils/generate_random_mac_address
1236
 * NAME
1237
 *   generate_random_mac - generates a random mac address
1238
 * INPUTS
1239
 *   none
1240
 * RESULT
1241
 *   $mac - a random mac address
1242
 ******/
1243
function generate_random_mac_address() {
1244
	$mac = "00:a0:8e";
1245
	for($x=0; $x<3; $x++) 
1246
	    $mac .= ":" . dechex(rand(16, 255));
1247

    
1248
	return $mac;
1249
}
1250

    
1251
/****f* pfsense-utils/strncpy
1252
 * NAME
1253
 *   strncpy - copy strings
1254
 * INPUTS
1255
 *   &$dst, $src, $length
1256
 * RESULT
1257
 *   none
1258
 ******/
1259
function strncpy(&$dst, $src, $length) {
1260
	if (strlen($src) > $length) {
1261
		$dst = substr($src, 0, $length);
1262
	} else {
1263
		$dst = $src;
1264
	}
1265
}
1266

    
1267
/****f* pfsense-utils/reload_interfaces_sync
1268
 * NAME
1269
 *   reload_interfaces - reload all interfaces
1270
 * INPUTS
1271
 *   none
1272
 * RESULT
1273
 *   none
1274
 ******/
1275
function reload_interfaces_sync() {
1276
	global $config, $g;
1277
	
1278
	if(file_exists("{$g['tmp_path']}/config.cache"))
1279
		unlink("{$g['tmp_path']}/config.cache");
1280
	
1281
	/* parse config.xml again */
1282
	$config = parse_config(true);
1283

    
1284
	/* delete all old interface information */
1285
	$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
1286
	foreach ($iflist as $ifent => $ifname) {
1287
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1288
		mwexec("/sbin/ifconfig {$ifname_real} down");
1289
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1290
	}
1291

    
1292
	/* set up VLAN virtual interfaces */
1293
	interfaces_vlan_configure();
1294

    
1295
	/* set up LAN interface */
1296
	interfaces_lan_configure();
1297

    
1298
	/* set up WAN interface */
1299
	interfaces_wan_configure();
1300

    
1301
	/* set up Optional interfaces */
1302
	interfaces_optional_configure();
1303
        
1304
	/* set up static routes */
1305
	system_routing_configure();
1306
	
1307
	/* enable routing */
1308
	system_routing_enable();
1309
	
1310
	/* setup captive portal if needed */
1311
	captiveportal_configure();
1312
	
1313
	/* bring up carp interfaces */
1314
	interfaces_carp_configure();
1315
	
1316
	/* bring up carp interfaces*/
1317
	interfaces_carp_bring_up_final();	
1318
}
1319

    
1320
/****f* pfsense-utils/reload_all
1321
 * NAME
1322
 *   reload_all - triggers a reload of all settings
1323
 *   * INPUTS
1324
 *   none
1325
 * RESULT
1326
 *   none
1327
 ******/
1328
function reload_all() {
1329
	touch("/tmp/reload_all");
1330
}
1331

    
1332
/****f* pfsense-utils/reload_interfaces
1333
 * NAME
1334
 *   reload_interfaces - triggers a reload of all interfaces
1335
 * INPUTS
1336
 *   none
1337
 * RESULT
1338
 *   none
1339
 ******/
1340
function reload_interfaces() {
1341
	touch("/tmp/reload_interfaces");
1342
}
1343

    
1344
/****f* pfsense-utils/sync_webgui_passwords
1345
 * NAME
1346
 *   sync_webgui_passwords - syncs webgui and ssh passwords
1347
 * INPUTS
1348
 *   none
1349
 * RESULT
1350
 *   none
1351
 ******/
1352
function sync_webgui_passwords() {
1353
	global $config, $g;
1354
	conf_mount_rw();
1355
	$fd = fopen("{$g['varrun_path']}/htpasswd", "w");
1356
	if (!$fd) {
1357
		printf("Error: cannot open htpasswd in system_password_configure().\n");
1358
		return 1;
1359
	}
1360
	/* set admin account */
1361
	$username = $config['system']['username'];
1362
	
1363
	/* set defined user account */
1364
	if($username <> "admin") {
1365
		$username = $config['system']['username'];
1366
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");
1367
	} else {
1368
		fwrite($fd, $username . ":" . $config['system']['password'] . "\n");	
1369
	}	
1370
	fclose($fd);
1371
	chmod("{$g['varrun_path']}/htpasswd", 0600);	
1372
	$crypted_pw = $config['system']['password'];
1373
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1374
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1375
	/* sync root */
1376
	$fd = popen("/usr/sbin/pw usermod -n root -H 0", "w");
1377
	fwrite($fd, $crypted_pw);
1378
	pclose($fd);
1379
	mwexec("/usr/sbin/pw usermod -n root -s /bin/sh");
1380
	/* sync admin */
1381
	$fd = popen("/usr/sbin/pw usermod -n admin -H 0", "w");
1382
	fwrite($fd, $crypted_pw);
1383
	pclose($fd);
1384
	mwexec("/usr/sbin/pw usermod -n admin -s /etc/rc.initial");
1385
	mwexec("/usr/sbin/pwd_mkdb -d /etc -p /etc/master.passwd");
1386
	mwexec("/usr/sbin/pwd_mkdb -p /etc/master.passwd");
1387
	conf_mount_ro();
1388
}
1389

    
1390
/****f* pfsense-utils/reload_all_sync
1391
 * NAME
1392
 *   reload_all - reload all settings
1393
 *   * INPUTS
1394
 *   none
1395
 * RESULT
1396
 *   none
1397
 ******/
1398
function reload_all_sync() {
1399
	global $config, $g;
1400
	
1401
	if(file_exists("{$g['tmp_path']}/config.cache"))
1402
		unlink("{$g['tmp_path']}/config.cache");
1403
	
1404
	/* parse config.xml again */
1405
	$config = parse_config(true);
1406

    
1407
	/* set up our timezone */
1408
	system_timezone_configure();
1409

    
1410
	/* set up our hostname */
1411
	system_hostname_configure();
1412

    
1413
	/* make hosts file */
1414
	system_hosts_generate();
1415

    
1416
	/* generate resolv.conf */
1417
	system_resolvconf_generate();
1418

    
1419
	/* delete all old interface information */
1420
	$iflist = split(" ", str_replace("\n", "", `/sbin/ifconfig -l`));
1421
	foreach ($iflist as $ifent => $ifname) {
1422
		$ifname_real = convert_friendly_interface_to_real_interface_name($ifname);
1423
		if($ifname_real == "lo0")
1424
		    continue;		
1425
		mwexec("/sbin/ifconfig {$ifname_real} down");
1426
		mwexec("/sbin/ifconfig {$ifname_real} delete");
1427
	}
1428

    
1429
	/* set up VLAN virtual interfaces */
1430
	interfaces_vlan_configure();
1431

    
1432
	/* set up LAN interface */
1433
	interfaces_lan_configure();
1434

    
1435
	/* set up WAN interface */
1436
	interfaces_wan_configure();
1437

    
1438
	/* set up Optional interfaces */
1439
	interfaces_optional_configure();
1440
        
1441
	/* bring up carp interfaces */
1442
	interfaces_carp_configure();
1443
	
1444
	/* set up static routes */
1445
	system_routing_configure();
1446

    
1447
	/* enable routing */
1448
	system_routing_enable();
1449
	
1450
	/* ensure passwords are sync'd */
1451
	system_password_configure();
1452

    
1453
	/* start dnsmasq service */
1454
	services_dnsmasq_configure();
1455

    
1456
	/* start dyndns service */
1457
	services_dyndns_configure();
1458

    
1459
	/* start DHCP service */
1460
	services_dhcpd_configure();
1461

    
1462
	/* start the NTP client */
1463
	system_ntp_configure();
1464

    
1465
	/* start ftp proxy helpers if they are enabled */
1466
	system_start_ftp_helpers();
1467
	
1468
	/* start the captive portal */
1469
	captiveportal_configure();
1470

    
1471
        /* reload the filter */
1472
	filter_configure_sync();
1473

    
1474
	/* bring up carp interfaces*/
1475
	interfaces_carp_bring_up_final();
1476

    
1477
	/* sync pw database */
1478
	conf_mount_rw();
1479
	mwexec("/usr/sbin/pwd_mkdb -d /etc/ /etc/master.passwd");
1480
	conf_mount_ro();
1481

    
1482
	/* restart sshd */
1483
	touch("/tmp/start_sshd");
1484
	
1485
}
1486

    
1487
?>
(14-14/27)