Project

General

Profile

Download (33.1 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
		if (preg_match("/nameserver (.*)/", $dns, $matches))
54
			$dns_servers[] = $matches[1];		
55
	}
56
	return $dns_servers;
57
}
58

    
59
/****f* pfsense-utils/log_error
60
 * NAME
61
 *   log_error	- Sends a string to syslog.
62
 * INPUTS
63
 *   $error	- string containing the syslog message.
64
 * RESULT
65
 *   null
66
 ******/
67
function log_error($error) {
68
    syslog(LOG_WARNING, $error);
69
    return;
70
}
71

    
72
/****f* pfsense-utils/get_interface_mac_address
73
 * NAME
74
 *   get_interface_mac_address - Return a interfaces mac address
75
 * INPUTS
76
 *   $interface	- interface to obtain mac address from
77
 * RESULT
78
 *   $mac - the mac address of the interface
79
 ******/
80
function get_interface_mac_address($interface) {
81
    $mac = exec("ifconfig {$interface} | awk '/ether/ {print $2}'");
82
    return trim($mac);
83
}
84

    
85
/****f* pfsense-utils/return_dir_as_array
86
 * NAME
87
 *   return_dir_as_array - Return a directory's contents as an array.
88
 * INPUTS
89
 *   $dir	- string containing the path to the desired directory.
90
 * RESULT
91
 *   $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
92
 ******/
93
function return_dir_as_array($dir) {
94
    $dir_array = array();
95
    if (is_dir($dir)) {
96
	if ($dh = opendir($dir)) {
97
	    while (($file = readdir($dh)) !== false) {
98
		$canadd = 0;
99
		if($file == ".") $canadd = 1;
100
		if($file == "..") $canadd = 1;
101
		if($canadd == 0)
102
		    array_push($dir_array, $file);
103
	    }
104
	    closedir($dh);
105
	}
106
    }
107
    return $dir_array;
108
}
109

    
110
/****f* pfsense-utils/enable_hardware_offloading
111
 * NAME
112
 *   enable_hardware_offloading - Enable a NIC's supported hardware features.
113
 * INPUTS
114
 *   $interface	- string containing the physical interface to work on.
115
 * RESULT
116
 *   null
117
 * NOTES
118
 *   This function only supports the fxp driver's loadable microcode.
119
 ******/
120
function enable_hardware_offloading($interface) {
121
    global $g;
122
    if($g['booting']) {
123
		$supported_ints = array('fxp');
124
		foreach($supported_ints as $int) {
125
			if(stristr($interface,$int) != false) {
126
		    	mwexec("/sbin/ifconfig $interface link0");
127
			}
128
		}
129
    }
130
    mwexec("/sbin/ifconfig $interface txcsum 2>/dev/null");
131
    mwexec("/sbin/ifconfig $interface rxcsum 2>/dev/null");    
132
    return;
133
}
134

    
135
/****f* pfsense-utils/setup_microcode
136
 * NAME
137
 *   enumerates all interfaces and calls enable_hardware_offloading which
138
 *   enables a NIC's supported hardware features.
139
 * INPUTS
140
 *   
141
 * RESULT
142
 *   null
143
 * NOTES
144
 *   This function only supports the fxp driver's loadable microcode.
145
 ******/
146
function setup_microcode() {
147
   global $config;
148
    $ifdescrs = array('wan', 'lan');
149
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
150
	$ifdescrs['opt' . $j] = "opt" . $j;
151
    }
152
    foreach($ifdescrs as $if)
153
	enable_hardware_offloading($if);
154
}
155

    
156
/****f* pfsense-utils/return_filename_as_array
157
 * NAME
158
 *   return_filename_as_array - Return a file's contents as an array.
159
 * INPUTS
160
 *   $filename	- string containing the path to the desired file.
161
 *   $strip	- array of characters to strip - default is '#'.
162
 * RESULT
163
 *   $file	- array containing the file's contents.
164
 * NOTES
165
 *   This function strips lines starting with '#' and leading/trailing whitespace by default.
166
 ******/
167
function return_filename_as_array($filename, $strip = array('#')) {
168
    if(file_exists($filename)) $file = file($filename);
169
    if(is_array($file)) {
170
	foreach($file as $line) $line = trim($line);
171
        foreach($strip as $tostrip) $file = preg_grep("/^{$tostrip}/", $file, PREG_GREP_INVERT);
172
    }
173
    return $file;
174
}
175

    
176
/****f* pfsense-utils/file_put_contents
177
 * NAME
178
 *   file_put_contents - Wrapper for file_put_contents if it doesn't exist
179
 * RESULT
180
 *   none
181
 ******/
182
if(!function_exists("file_put_contents")) {
183
    function file_put_contents($filename, $data) {
184
	$fd = fopen($filename,"w");
185
	fwrite($fd, $data);
186
	fclose($fd);
187
    }
188
}
189

    
190
/****f* pfsense-utils/get_carp_status
191
 * NAME
192
 *   get_carp_status - Return whether CARP is enabled or disabled.
193
 * RESULT
194
 *   boolean	- true if CARP is enabled, false if otherwise.
195
 ******/
196
function get_carp_status() {
197
    /* grab the current status of carp */
198
    $status = `/sbin/sysctl net.inet.carp.allow | cut -d" " -f2`;
199
    if(intval($status) == "0") return false;
200
    return true;
201
}
202

    
203
/****f* pfsense-utils/return_filename_as_string
204
 * NAME
205
 *   return_filename_as_string - Return a file's contents as a string.
206
 * INPUTS
207
 *   $filename  - string containing the path to the desired file.
208
 * RESULT
209
 *   $tmp	- string containing the file's contents.
210
 ******/
211
function return_filename_as_string($filename) {
212
    if(file_exists($filename)) {
213
        return file_get_contents($filename);
214
    } else {
215
        return false;
216
   } 
217
}
218

    
219
/****f* pfsense-utils/is_carp_defined
220
 * NAME
221
 *   is_carp_defined - Return whether CARP is detected in the kernel.
222
 * RESULT
223
 *   boolean	- true if CARP is detected, false otherwise.
224
 ******/
225
function is_carp_defined() {
226
    /* is carp compiled into the kernel and userland? */
227
    $command = "/sbin/sysctl -a | grep carp";
228
    $fd = popen($command . " 2>&1 ", "r");
229
    if(!$fd) {
230
	log_error("Warning, could not execute command {$command}");
231
	return 0;
232
    }
233
    while(!feof($fd)) {
234
	$tmp .= fread($fd,49);
235
    }
236
    fclose($fd);
237

    
238
    if($tmp == "")
239
	return false;
240
    else
241
	return true;
242
}
243

    
244
/****f* pfsense-utils/find_number_of_created_carp_interfaces
245
 * NAME
246
 *   find_number_of_created_carp_interfaces - Return the number of CARP interfaces.
247
 * RESULT
248
 *   $tmp	- Number of currently created CARP interfaces.
249
 ******/
250
function find_number_of_created_carp_interfaces() {
251
    $command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
252
    $fd = popen($command . " 2>&1 ", "r");
253
    if(!$fd) {
254
	log_error("Warning, could not execute command {$command}");
255
	return 0;
256
    }
257
    while(!feof($fd)) {
258
	$tmp .= fread($fd,49);
259
    }
260
    fclose($fd);
261
    $tmp = intval($tmp);
262
    return $tmp;
263
}
264

    
265
/****f* pfsense-utils/link_ip_to_carp_interface
266
 * NAME
267
 *   link_ip_to_carp_interface - Find where a CARP interface links to.
268
 * INPUTS
269
 *   $ip
270
 * RESULT
271
 *   $carp_ints
272
 ******/
273
function link_ip_to_carp_interface($ip) {
274
	global $config;
275
	if($ip == "") return;
276

    
277
	$ifdescrs = array('wan', 'lan');
278
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
279
		$ifdescrs['opt' . $j] = "opt" . $j;
280
	}
281

    
282
	$ft = split("\.", $ip);
283
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
284

    
285
	$carp_ints = "";
286
	$num_carp_ints = find_number_of_created_carp_interfaces();
287
	foreach ($ifdescrs as $ifdescr => $ifname) {
288
		for($x=0; $x<$num_carp_ints; $x++) {
289
			$carp_int = "carp{$x}";
290
			$carp_ip = find_interface_ip($carp_int);
291
			$carp_ft = split("\.", $carp_ip);
292
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
293
			$result = does_interface_exist($carp_int);
294
			if($result <> true) break;
295
			if($ft_ip == $carp_ft_ip)
296
			if(stristr($carp_ints,$carp_int) == false)
297
			$carp_ints .= " " . $carp_int;
298
		}
299
	}
300
	return $carp_ints;
301
}
302

    
303
/****f* pfsense-utils/exec_command
304
 * NAME
305
 *   exec_command - Execute a command and return a string of the result.
306
 * INPUTS
307
 *   $command	- String of the command to be executed.
308
 * RESULT
309
 *   String containing the command's result.
310
 * NOTES
311
 *   This function returns the command's stdout and stderr.
312
 ******/
313
function exec_command($command) {
314
    $output = array();
315
    exec($command . ' 2>&1 ', $output);
316
    return(implode("\n", $output));
317
}
318

    
319
/*
320
 * does_interface_exist($interface): return true or false if a interface is detected.
321
 */
322
function does_interface_exist($interface) {
323
    $ints = exec_command("/sbin/ifconfig -l");
324
    if(stristr($ints, $interface) !== false)
325
	return true;
326
    else
327
	return false;
328
}
329

    
330
/*
331
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
332
 */
333
function convert_ip_to_network_format($ip, $subnet) {
334
    $ipsplit = split('[.]', $ip);
335
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
336
    return $string;
337
}
338

    
339
/*
340
 * find_interface_ip($interface): return the interface ip (first found)
341
 */
342
function find_interface_ip($interface) {
343
    if(does_interface_exist($interface) == false) return;
344
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
345
    $ip = str_replace("\n","",$ip);
346
    return $ip;
347
}
348

    
349
function guess_interface_from_ip($ipaddress) {
350
    $ints = `/sbin/ifconfig -l`;
351
    $ints_split = split(" ", $ints);
352
    $ip_subnet_split = split("\.", $ipaddress);
353
    $ip_subnet = $ip_subnet_split[0] . "." . $ip_subnet_split[1] . "." . $ip_subnet_split[2] . ".";
354
    foreach($ints_split as $int) {
355
        $ip = find_interface_ip($int);
356
        $ip_split = split("\.", $ip);
357
        $ip_tocheck = $ip_split[0] . "." . $ip_split[1] . "." . $ip_split[2] . ".";
358
        if(stristr($ip_tocheck, $ip_subnet) != false) return $int;
359
    }
360
}
361

    
362
function filter_opt_interface_to_real($opt) {
363
    global $config;
364
    return $config['interfaces'][$opt]['if'];
365
}
366

    
367
function filter_get_opt_interface_descr($opt) {
368
    global $config;
369
    return $config['interfaces'][$opt]['descr'];
370
}
371

    
372
function get_friendly_interface_list_as_array() {
373
    global $config;
374
    $ints = array();
375
    $ifdescrs = array('wan', 'lan');
376
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
377
		$ifdescrs['opt' . $j] = "opt" . $j;
378
    }
379
    $ifdescrs = get_interface_list();
380
    foreach ($ifdescrs as $ifdescr => $ifname) {
381
		array_push($ints,$ifdescr);
382
    }
383
    return $ints;
384
}
385

    
386
/*
387
 * find_ip_interface($ip): return the interface where an ip is defined
388
 */
389
function find_ip_interface($ip) {
390
    global $config;
391
    $ifdescrs = array('wan', 'lan');
392
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
393
	$ifdescrs['opt' . $j] = "opt" . $j;
394
    }
395
    foreach ($ifdescrs as $ifdescr => $ifname) {
396
	$int = filter_translate_type_to_real_interface($ifname);
397
	$ifconfig = exec_command("/sbin/ifconfig {$int}");
398
	if(stristr($ifconfig,$ip) <> false)
399
	    return $int;
400
    }
401
    return false;
402
}
403

    
404
/*
405
 *  filter_translate_type_to_real_interface($interface): returns the real interface name
406
 *                                                       for a friendly interface.  ie: wan
407
 */
408
function filter_translate_type_to_real_interface($interface) {
409
    global $config;
410
    return $config['interfaces'][$interface]['if'];
411
}
412

    
413
/*
414
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
415
 */
416
function get_carp_interface_status($carpinterface) {
417
	/* basically cache the contents of ifconfig statement
418
	to speed up this routine */
419
	global $carp_query;
420
	if($carp_query == "")
421
	$carp_query = split("\n", `/sbin/ifconfig | /usr/bin/grep carp`);
422
	$found_interface = 0;
423
	foreach($carp_query as $int) {
424
		if($found_interface == 1) {
425
			if(stristr($int, "MASTER") == true) return "MASTER";
426
			if(stristr($int, "BACKUP") == true) return "BACKUP";
427
			if(stristr($int, "INIT") == true) return "INIT";
428
			return false;
429
		}
430
		if(stristr($int, $carpinterface) == true)
431
		$found_interface=1;
432
	}
433
	return;
434
}
435

    
436
/*
437
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
438
 */
439
function get_pfsync_interface_status($pfsyncinterface) {
440
    $result = does_interface_exist($pfsyncinterface);
441
    if($result <> true) return;
442
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
443
    return $status;
444
}
445

    
446
/*
447
 * find_carp_interface($ip): return the carp interface where an ip is defined
448
 */
449
function find_carp_interface($ip) {
450
    global $find_carp_ifconfig;
451
    if($find_carp_ifconfig == "") {
452
	$find_carp_ifconfig = array();
453
	$num_carp_ints = find_number_of_created_carp_interfaces();
454
	for($x=0; $x<$num_carp_ints; $x++) {
455
	    $find_carp_ifconfig[$x] = exec_command("/sbin/ifconfig carp{$x}");
456
	}
457
    }
458
    $carps = 0;
459
    foreach($find_carp_ifconfig as $fci) {
460
	if(stristr($fci, $ip) == true)
461
	    return "carp{$carps}";
462
	$carps++;
463
    }
464
}
465

    
466
/*
467
 * find_number_of_created_bridges(): returns the number of currently created bridges
468
 */
469
function find_number_of_created_bridges() {
470
    return `/sbin/ifconfig | grep \"bridge[0-999]\:" | wc -l`;
471
}
472

    
473
/*
474
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
475
 */
476
function add_rule_to_anchor($anchor, $rule, $label) {
477
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
478
}
479

    
480
/*
481
 * remove_text_from_file
482
 * remove $text from file $file
483
 */
484
function remove_text_from_file($file, $text) {
485
    global $fd_log;
486
    fwrite($fd_log, "Adding needed text items:\n");
487
    $filecontents = exec_command_and_return_text("cat " . $file);
488
    $textTMP = str_replace($text, "", $filecontents);
489
    $text .= $textTMP;
490
    fwrite($fd_log, $text . "\n");
491
    $fd = fopen($file, "w");
492
    fwrite($fd, $text);
493
    fclose($fd);
494
}
495

    
496
/*
497
 * add_text_to_file($file, $text): adds $text to $file.
498
 * replaces the text if it already exists.
499
 */
500
function add_text_to_file($file, $text) {
501
	if(file_exists($file) and is_writable($file)) {
502
		$filecontents = file($file);
503
		$filecontents[] = $text;
504
		$tmpfile = get_tmp_file();
505
		$fout = fopen($tmpfile, "w");
506
		foreach($filecontents as $line) {
507
			fwrite($fout, rtrim($line) . "\n");
508
		}
509
		fclose($fout);
510
		rename($tmpfile, $file);
511
		return true;
512
	} else {
513
		return false;
514
	}
515
}
516

    
517
/*
518
 * get_filename_from_url($url): converts a url to its filename.
519
 */
520

    
521
function get_filename_from_url($url) {
522
	$filenamesplit = split("/", $url);
523
	foreach($filenamesplit as $fn) $filename = $fn; 	 
524
	return $filename;
525
}
526

    
527
/*
528
 *   update_output_window: update bottom textarea dynamically.
529
 */
530
function update_output_window($text) {
531
    $log = ereg_replace("\n", "\\n", $text);
532
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
533
}
534

    
535
/*
536
 *   get_dir: return an array of $dir
537
 */
538
function get_dir($dir) {
539
    $dir_array = array();
540
    $d = dir($dir);
541
    while (false !== ($entry = $d->read())) {
542
	array_push($dir_array, $entry);
543
    }
544
    $d->close();
545
    return $dir_array;
546
}
547

    
548
/*
549
 *   update_output_window: update top textarea dynamically.
550
 */
551
function update_status($status) {
552
    echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
553
}
554

    
555
/*
556
 *   exec_command_and_return_text_array: execute command and return output
557
 */
558
function exec_command_and_return_text_array($command) {
559
	$fd = popen($command . " 2>&1 ", "r");
560
	while(!feof($fd)) {
561
		$tmp .= fread($fd,49);
562
	}
563
	fclose($fd);
564
	$temp_array = split("\n", $tmp);
565
	return $temp_array;
566
}
567

    
568
/*
569
 *   exec_command_and_return_text: execute command and return output
570
 */
571
function exec_command_and_return_text($command) {
572
    return exec_command($command);
573
}
574

    
575
/*
576
 *   exec_command_and_return_text: execute command and update output window dynamically
577
 */
578
function execute_command_return_output($command) {
579
    global $fd_log;
580
    $fd = popen($command . " 2>&1 ", "r");
581
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
582
    $counter = 0;
583
    $counter2 = 0;
584
    while(!feof($fd)) {
585
	$tmp = fread($fd, 50);
586
	$tmp1 = ereg_replace("\n","\\n", $tmp);
587
	$text = ereg_replace("\"","'", $tmp1);
588
	if($lasttext == "..") {
589
	    $text = "";
590
	    $lasttext = "";
591
	    $counter=$counter-2;
592
	} else {
593
	    $lasttext .= $text;
594
	}
595
	if($counter > 51) {
596
	    $counter = 0;
597
	    $extrabreak = "\\n";
598
	} else {
599
	    $extrabreak = "";
600
	    $counter++;
601
	}
602
	if($counter2 > 600) {
603
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
604
	    $counter2 = 0;
605
	} else
606
	    $counter2++;
607
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
608
    }
609
    fclose($fd);
610
}
611

    
612
/*
613
 * convert_friendly_interface_to_real_interface_name($interface): convert WAN to FXP0
614
 */
615
function convert_friendly_interface_to_real_interface_name($interface) {
616
    global $config;
617
    $lc_interface = strtolower($interface);
618
    if($lc_interface == "lan") return $config['interfaces']['lan']['if'];
619
    if($lc_interface == "wan") return $config['interfaces']['wan']['if'];
620
    $ifdescrs = array();
621
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
622
	$ifdescrs['opt' . $j] = "opt" . $j;
623
    foreach ($ifdescrs as $ifdescr => $ifname) {
624
	if(strtolower($ifname) == $lc_interface)
625
	    return $config['interfaces'][$ifname]['if'];
626
	if(strtolower($config['interfaces'][$ifname]['descr']) == $lc_interface)
627
	    return $config['interfaces'][$ifname]['if'];
628
    }
629
    return $interface;
630
}
631

    
632
/*
633
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
634
 */
635
function convert_real_interface_to_friendly_interface_name($interface) {
636
    global $config;
637
    $ifdescrs = array('wan', 'lan');
638
    for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++)
639
	$ifdescrs['opt' . $j] = "opt" . $j;
640
    foreach ($ifdescrs as $ifdescr => $ifname) {
641
	$int = filter_translate_type_to_real_interface($ifname);
642
	if($ifname == $interface) return $ifname;
643
	if($int == $interface) return $ifname;
644
    }
645
    return $interface;
646
}
647

    
648
/*
649
 * update_progress_bar($percent): updates the javascript driven progress bar.
650
 */
651
function update_progress_bar($percent) {
652
    if($percent > 100) $percent = 1;
653
    echo "\n<script type=\"text/javascript\" language=\"javascript\">";
654
    echo "\ndocument.progressbar.style.width='" . $percent . "%';";
655
    echo "\n</script>";
656
}
657

    
658
/*
659
 * gather_altq_queue_stats():  gather alq queue stats and return an array that
660
 *                             is queuename|qlength|measured_packets
661
 *                             NOTE: this command takes 5 seconds to run
662
 */
663
function gather_altq_queue_stats($dont_return_root_queues) {
664
    mwexec("/usr/bin/killall -9 pfctl");
665
    $stats = `/sbin/pfctl -vvsq & /bin/sleep 5;/usr/bin/killall pfctl 2>/dev/null`;
666
    $stats_array = split("\n", $stats);
667
    $queue_stats = array();
668
    foreach ($stats_array as $stats_line) {
669
        if (preg_match_all("/queue\s+(\w+)\s+/",$stats_line,$match_array))
670
            $queue_name = $match_array[1][0];
671
        if (preg_match_all("/measured:\s+.*packets\/s\,\s(.*)\s+\]/",$stats_line,$match_array))
672
            $speed = $match_array[1][0];
673
        if (preg_match_all("/borrows:\s+(.*)/",$stats_line,$match_array))
674
            $borrows = $match_array[1][0];
675
        if (preg_match_all("/suspends:\s+(.*)/",$stats_line,$match_array))
676
            $suspends = $match_array[1][0];
677
        if (preg_match_all("/dropped pkts:\s+(.*)/",$stats_line,$match_array))
678
            $drops = $match_array[1][0];
679
        if (preg_match_all("/measured:\s+(.*)packets/",$stats_line,$match_array)) {
680
            $measured = $match_array[1][0];
681
	    if($dont_return_root_queues == true)
682
		if(stristr($queue_name,"root_") == false)
683
		    array_push($queue_stats, "{$queue_name}|{$speed}|{$measured}|{$borrows}|{$suspends}|{$drops}");
684
        }
685
    }
686
    return $queue_stats;
687
}
688

    
689
/*
690
 * reverse_strrchr($haystack, $needle):  Return everything in $haystack up to the *last* instance of $needle.
691
 *					 Useful for finding paths and stripping file extensions.
692
 */
693
function reverse_strrchr($haystack, $needle)
694
{
695
               return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) +1 ) : false;
696
}
697

    
698
/*
699
 *  backup_config_section($section): returns as an xml file string of
700
 *                                   the configuration section
701
 */
702
function backup_config_section($section) {
703
    global $config;
704
    $new_section = &$config[$section];
705
    /* generate configuration XML */
706
    $xmlconfig = dump_xml_config($new_section, $section);
707
    $xmlconfig = str_replace("<?xml version=\"1.0\"?>", "", $xmlconfig);
708
    return $xmlconfig;
709
}
710

    
711
/*
712
 *  restore_config_section($section, new_contents): restore a configuration section,
713
 *                                                  and write the configuration out
714
 *                                                  to disk/cf.
715
 */
716
function restore_config_section($section, $new_contents) {
717
    global $config;
718
    conf_mount_rw();
719
    $fout = fopen("{$g['tmp_path']}/tmpxml","w");
720
    fwrite($fout, $new_contents);
721
    fclose($fout);
722
    $section_xml = parse_xml_config($g['tmp_path'] . "/tmpxml", $section);
723
    $config[$section] = &$section_xml;
724
    unlink($g['tmp_path'] . "/tmpxml");
725
    write_config("Restored {$section} of config file (maybe from CARP partner)");
726
    conf_mount_ro();
727
    return;
728
}
729

    
730
/*
731
 * http_post($server, $port, $url, $vars): does an http post to a web server
732
 *                                         posting the vars array.
733
 * written by nf@bigpond.net.au
734
 */
735
function http_post($server, $port, $url, $vars) {
736
    $user_agent = "Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)";
737
    $urlencoded = "";
738
    while (list($key,$value) = each($vars))
739
	$urlencoded.= urlencode($key) . "=" . urlencode($value) . "&";
740
    $urlencoded = substr($urlencoded,0,-1);
741

    
742
    $content_length = strlen($urlencoded);
743

    
744
    $headers = "POST $url HTTP/1.1
745
Accept: */*
746
Accept-Language: en-au
747
Content-Type: application/x-www-form-urlencoded
748
User-Agent: $user_agent
749
Host: $server
750
Connection: Keep-Alive
751
Cache-Control: no-cache
752
Content-Length: $content_length
753

    
754
";
755

    
756
    $fp = fsockopen($server, $port, $errno, $errstr);
757
    if (!$fp) {
758
	return false;
759
    }
760

    
761
    fputs($fp, $headers);
762
    fputs($fp, $urlencoded);
763

    
764
    $ret = "";
765
    while (!feof($fp))
766
	$ret.= fgets($fp, 1024);
767

    
768
    fclose($fp);
769

    
770
    return $ret;
771

    
772
}
773

    
774
/*
775
 *  php_check_syntax($code_tocheck, $errormessage): checks $code_to_check for errors
776
 */
777
if (!function_exists('php_check_syntax')){
778
   function php_check_syntax($code_to_check, &$errormessage){
779
	return false;
780
        $fout = fopen("/tmp/codetocheck.php","w");
781
        $code = $_POST['content'];
782
        $code = str_replace("<?php", "", $code);
783
        $code = str_replace("?>", "", $code);
784
        fwrite($fout, "<?php\n\n");
785
        fwrite($fout, $code_to_check);
786
        fwrite($fout, "\n\n?>\n");
787
        fclose($fout);
788
        $command = "/usr/local/bin/php -l /tmp/codetocheck.php";
789
        $output = exec_command($command);
790
        if (stristr($output, "Errors parsing") == false) {
791
            echo "false\n";
792
            $errormessage = '';
793
            return(false);
794
        } else {
795
            $errormessage = $output;
796
            return(true);
797
        }
798
    }
799
}
800

    
801
/*
802
 *  php_check_filename_syntax($filename, $errormessage): checks the file $filename for errors
803
 */
804
if (!function_exists('php_check_syntax')){
805
   function php_check_syntax($code_to_check, &$errormessage){
806
	return false;
807
        $command = "/usr/local/bin/php -l " . $code_to_check;
808
        $output = exec_command($command);
809
        if (stristr($output, "Errors parsing") == false) {
810
            echo "false\n";
811
            $errormessage = '';
812
            return(false);
813
        } else {
814
            $errormessage = $output;
815
            return(true);
816
        }
817
    }
818
}
819

    
820
/*
821
 * rmdir_recursive($path,$follow_links=false)
822
 * Recursively remove a directory tree (rm -rf path)
823
 * This is for directories _only_
824
 */
825
function rmdir_recursive($path,$follow_links=false) {
826
	$to_do = glob($path);
827
	if(!is_array($to_do)) $to_do = array($to_do);
828
	foreach($to_do as $workingdir) { // Handle wildcards by foreaching.
829
		if(file_exists($workingdir)) {
830
			if(is_dir($workingdir)) {
831
				$dir = opendir($workingdir);
832
				while ($entry = readdir($dir)) {
833
					if (is_file("$workingdir/$entry") || ((!$follow_links) && is_link("$workingdir/$entry")))
834
						unlink("$workingdir/$entry");
835
					elseif (is_dir("$workingdir/$entry") && $entry!='.' && $entry!='..')
836
						rmdir_recursive("$workingdir/$entry");
837
				}
838
				closedir($dir);
839
				rmdir($workingdir);
840
			} elseif (is_file($workingdir)) {
841
				unlink($workingdir);
842
			}
843
               	}
844
	}
845
	return;
846
}
847

    
848
/*
849
 * safe_mkdir($path, $mode = 0755)
850
 * create directory if it doesn't already exist and isn't a file!
851
 */
852
function safe_mkdir($path, $mode=0755) {
853
	global $g;
854

    
855
	/* cdrom is ro. */
856
	if($g['platform'] == "cdrom")
857
		return false;
858
	
859
	if (!is_file($path) && !is_dir($path))
860
		return mkdir($path, $mode);
861
	else
862
		return false;
863
}
864

    
865
/*
866
 * make_dirs($path, $mode = 0755)
867
 * create directory tree recursively (mkdir -p)
868
 */
869
function make_dirs($path, $mode = 0755) {
870
	/* is dir already created? */
871
	if(is_dir($path)) return;
872
	/* create directory in question */
873
	$to_create = explode("/", $path);
874
	foreach($to_create as $tc) 
875
	    if(!is_dir($tc))
876
		safe_mkdir($path, $mode);
877
}
878

    
879
/*
880
 * check_firmware_version(): Check whether the current firmware installed is the most recently released.
881
 */
882
function check_firmware_version($tocheck = "all", $return_php = true) {
883
        global $g, $config;
884
	$xmlrpc_base_url = $g['xmlrpcbaseurl'];
885
        $xmlrpc_path = $g['xmlrpcpath'];
886
	$rawparams = array("firmware" => array("version" => trim(file_get_contents('/etc/version'))),
887
			"kernel"   => array("version" => trim(file_get_contents('/etc/version_kernel'))),
888
			"base"     => array("version" => trim(file_get_contents('/etc/version_base'))),
889
			"platform" => trim(file_get_contents('/etc/platform'))
890
		);
891
	if($tocheck == "all") {
892
		$params = $rawparams;
893
	} else {
894
		foreach($tocheck as $check) {
895
			$params['check'] = $rawparams['check'];
896
			$params['platform'] = $rawparams['platform'];
897
		}
898
	}
899
	if($config['system']['firmware']['branch']) {
900
		$params['branch'] = $config['system']['firmware']['branch'];
901
	}
902
	$xmlparams = php_value_to_xmlrpc($params);
903
        $msg = new XML_RPC_Message('pfsense.get_firmware_version', array($xmlparams));
904
        $cli = new XML_RPC_Client($xmlrpc_path, $xmlrpc_base_url);
905
	//$cli->setDebug(1);
906
	$resp = $cli->send($msg, 10);
907
	if(!$resp or $resp->faultCode()) {
908
		$raw_versions = false;
909
	} else {
910
		$raw_versions = xmlrpc_value_to_php($resp->value());
911
		$raw_versions["current"] = $params;
912
	}
913
	return $raw_versions;
914
}
915

    
916
function get_disk_info() {
917
        exec("df -h | grep -w '/' | awk '{ print $2, $3, $4, $5 }'", $diskout);
918
        return explode(' ', $diskout[0]);
919
        // $size, $used, $avail, $cap
920
}
921

    
922
/****f* pfsense-utils/display_top_tabs
923
 * NAME
924
 *   display_top_tabs - display tabs with rounded edges
925
 * INPUTS
926
 *   $text	- array of tabs
927
 * RESULT
928
 *   null
929
 ******/
930
    function display_top_tabs($tab_array) {
931
	    echo "<table cellpadding='0' cellspacing='0'>\n";
932
	    echo " <tr height='1'>\n";
933
	    $tabscounter = 0;
934
	    foreach ($tab_array as $ta) {
935
		    if($ta[1] == true) {
936
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><div id='tabactive'></div></td>\n";
937
		    } else {
938
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><div id='tabdeactive{$tabscounter}'></div></td>\n";
939
		    }
940
		    $tabscounter++;
941
	    }
942
	    echo "</tr>\n<tr>\n";
943
	    foreach ($tab_array as $ta) {
944
		    if($ta[1] == true) {
945
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;{$ta[0]}";
946
			    echo "&nbsp;&nbsp;&nbsp;";
947
			    echo "<font size='-12'>&nbsp;</td>\n";
948
		    } else {
949
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"><B>&nbsp;&nbsp;&nbsp;<a href='{$ta[2]}'>";
950
			    echo "<font color='white'>{$ta[0]}</a>&nbsp;&nbsp;&nbsp;";
951
			    echo "<font size='-12'>&nbsp;</td>\n";
952
		    }
953
	    }
954
	    echo "</tr>\n<tr height='5px'>\n";
955
	    foreach ($tab_array as $ta) {
956
		    if($ta[1] == true) {
957
			    echo "  <td bgcolor='#EEEEEE' onClick=\"document.location='{$ta[2]}'\"></td>\n";
958
		    } else {
959
			    echo "  <td bgcolor='#777777' onClick=\"document.location='{$ta[2]}'\"></td>\n";
960
		    }
961
		    $tabscounter++;
962
	    }
963
	    echo " </tr>\n";
964
	    echo "</table>\n";
965
	    
966
	    echo "<script type=\"text/javascript\">";
967
	    echo "NiftyCheck();\n";
968
	    echo "Rounded(\"div#tabactive\",\"top\",\"#FFF\",\"#EEEEEE\",\"smooth\");\n";
969
	    for($x=0; $x<$tabscounter; $x++) 
970
		    echo "Rounded(\"div#tabdeactive{$x}\",\"top\",\"#FFF\",\"#777777\",\"smooth\");\n";
971
	    echo "</script>";
972
    }
973

    
974

    
975
/****f* pfsense-utils/display_topbar
976
 * NAME
977
 *   display_topbar - top a table off with rounded edges
978
 * INPUTS
979
 *   $text	- (optional) Text to include in bar
980
 * RESULT
981
 *   null
982
 ******/
983
function display_topbar($text = "", $bg_color="#990000", $replace_color="#FFFFFF", $rounding_style="smooth") {	    
984
	echo "     <table width='100%' cellpadding='0' cellspacing='0'>\n";
985
	echo "       <tr height='1'>\n";
986
	echo "         <td width='100%' valign='top' color='{$bg_color}' bgcolor='{$bg_color}'>";
987
	echo "		<div id='topbar'></div></td>\n";
988
	echo "       </tr>\n";
989
	echo "       <tr height='1'>\n";
990
	if ($text != "")
991
		echo "         <td height='1' class='listtopic'>{$text}</td>\n";
992
	else
993
		echo "         <td height='1' class='listtopic'></td>\n";
994
	echo "       </tr>\n";
995
	echo "     </table>";
996
	echo "<script type=\"text/javascript\">";
997
	echo "NiftyCheck();\n";
998
	echo "Rounded(\"div#topbar\",\"top\",\"{$replace_color}\",\"{$bg_color}\",\"{$rounding_style}\");\n";
999
	echo "</script>";
1000
}
1001

    
1002
/****f* pfsense-utils/generate_random_mac
1003
 * NAME
1004
 *   generate_random_mac - generates a random mac address
1005
 * INPUTS
1006
 *   none
1007
 * RESULT
1008
 *   $mac - a random mac address
1009
 ******/
1010
function generate_random_mac() {
1011
	$mac = "00:a0:8e";
1012
	for($x=0; $x<3; $x++) 
1013
	    $mac .= ":" . dechex(rand(16, 255));
1014

    
1015
	return $mac;
1016
}
1017

    
1018
/****f* pfsense-utils/strncpy
1019
 * NAME
1020
 *   strncpy - copy strings
1021
 * INPUTS
1022
 *   &$dst, $src, $length
1023
 * RESULT
1024
 *   none
1025
 ******/
1026
function strncpy(&$dst, $src, $length) {
1027
	if (strlen($src) > $length) {
1028
		$dst = substr($src, 0, $length);
1029
	} else {
1030
		$dst = $src;
1031
	}
1032
}
1033

    
1034
/****f* pfsense-utils/reload_interfaces_sync
1035
 * NAME
1036
 *   reload_interfaces - reload all interfaces
1037
 * INPUTS
1038
 *   none
1039
 * RESULT
1040
 *   none
1041
 ******/
1042
function reload_interfaces_sync() {
1043
	global $config, $g;
1044
	
1045
	if(file_exists("{$g['tmp_path']}/config.cache"))
1046
		unlink("{$g['tmp_path']}/config.cache");
1047
	
1048
	/* parse config.xml again */
1049
	$config = parse_config(true);
1050
	
1051
	/* set up LAN interface */
1052
	interfaces_lan_configure();
1053

    
1054
	/* set up WAN interface */
1055
	interfaces_wan_configure();
1056

    
1057
	/* set up Optional interfaces */
1058
	interfaces_optional_configure();
1059
        
1060
	/* set up static routes */
1061
	system_routing_configure();
1062
	
1063
	/* bring up carp interfaces */
1064
	interfaces_carp_bringup();
1065

    
1066
	/* enable routing */
1067
	system_routing_enable();
1068
}
1069

    
1070
/****f* pfsense-utils/reload_all
1071
 * NAME
1072
 *   reload_all - triggers a reload of all settings
1073
 *   * INPUTS
1074
 *   none
1075
 * RESULT
1076
 *   none
1077
 ******/
1078
function reload_all() {
1079
	touch("/tmp/reload_all");
1080
}
1081

    
1082
/****f* pfsense-utils/reload_interfaces
1083
 * NAME
1084
 *   reload_interfaces - triggers a reload of all interfaces
1085
 * INPUTS
1086
 *   none
1087
 * RESULT
1088
 *   none
1089
 ******/
1090
function reload_interfaces() {
1091
	touch("/tmp/reload_interfaces");
1092
}
1093

    
1094
/****f* pfsense-utils/reload_all_sync
1095
 * NAME
1096
 *   reload_all - reload all settings
1097
 *   * INPUTS
1098
 *   none
1099
 * RESULT
1100
 *   none
1101
 ******/
1102
function reload_all_sync() {
1103
	global $config, $g;
1104
	
1105
	if(file_exists("{$g['tmp_path']}/config.cache"))
1106
		unlink("{$g['tmp_path']}/config.cache");
1107
	
1108
	/* parse config.xml again */
1109
	$config = parse_config(true);
1110

    
1111
	/* set up our timezone */
1112
	system_timezone_configure();
1113

    
1114
	/* set up our hostname */
1115
	system_hostname_configure();
1116

    
1117
	/* make hosts file */
1118
	system_hosts_generate();
1119

    
1120
	/* generate resolv.conf */
1121
	system_resolvconf_generate();
1122

    
1123
	/* set up LAN interface */
1124
	interfaces_lan_configure();
1125

    
1126
	/* set up WAN interface */
1127
	interfaces_wan_configure();
1128

    
1129
	/* set up Optional interfaces */
1130
	interfaces_optional_configure();
1131
        
1132
	/* bring up carp interfaces */
1133
	interfaces_carp_configure();
1134
	
1135
	/* set up static routes */
1136
	system_routing_configure();
1137

    
1138
	/* enable routing */
1139
	system_routing_enable();
1140
	
1141
	/* ensure passwords are sync'd */
1142
	system_password_configure();
1143

    
1144
	/* start dnsmasq service */
1145
	services_dnsmasq_configure();
1146

    
1147
	/* start dyndns service */
1148
	services_dyndns_configure();
1149

    
1150
	/* start DHCP service */
1151
	services_dhcpd_configure();
1152

    
1153
	/* start the NTP client */
1154
	system_ntp_configure();
1155

    
1156
	/* start ftp proxy helpers if they are enabled */
1157
	system_start_ftp_helpers();
1158

    
1159
	/* bring up carp interfaces */
1160
	interfaces_carp_bringup();
1161

    
1162
        /* reload the filter */
1163
	filter_configure();	
1164
}
1165

    
1166
?>
(12-12/22)