Project

General

Profile

Download (14.4 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php
2
/*
3
	pfSense-utils.inc
4
	Utilities specific to pfSense
5
	part of pfSense (www.pfSense.com)
6

    
7
	Copyright (C) 2005 Scott Ullrich (sullrich@gmail.com)
8
	All rights reserved.
9

    
10
	Redistribution and use in source and binary forms, with or without
11
	modification, are permitted provided that the following conditions are met:
12

    
13
	1. Redistributions of source code must retain the above copyright notice,
14
	   this list of conditions and the following disclaimer.
15

    
16
	2. Redistributions in binary form must reproduce the above copyright
17
	   notice, this list of conditions and the following disclaimer in the
18
	   documentation and/or other materials provided with the distribution.
19

    
20
	THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
21
	INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
22
	AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23
	AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
24
	OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
	SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
	INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
	CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
	ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
	POSSIBILITY OF SUCH DAMAGE.
30
*/
31

    
32
/*
33
 * log_error: send string to syslog
34
 */
35
function log_error($error) {
36
    syslog(LOG_WARNING, $error);
37
    return;
38
}
39

    
40
/*
41
 * return_dir_as_array($dir): returns $dir contents as an array
42
 */
43
function return_dir_as_array($dir) {
44
    $dir_array = array();
45
    if (is_dir($dir)) {
46
	if ($dh = opendir($dir)) {
47
	    while (($file = readdir($dh)) !== false) {
48
		$canadd = 0;
49
		if($file == ".") $canadd = 1;
50
		if($file == "..") $canadd = 1;
51
		if($canadd == 0)
52
		    array_push($dir_array, $file);
53
	    }
54
	    closedir($dh);
55
	}
56
    }
57
    return $dir_array;
58
}
59

    
60
/*
61
 * enable_hardware_offloading() enables hardware features of nics if they are supported
62
 */
63
function enable_hardware_offloading($interface) {
64
    $supported_ints = array('fxp');
65
    foreach($supported_ints as $int) {
66
	if(stristr($interface,$int) != false) {
67
	    mwexec("/sbin/ifconfig $interface link0");
68
	}
69
    }
70
}
71

    
72
/*
73
 * return_filename_as_array($filename): returns $filename contents as a string
74
 */
75
function return_filename_as_array($filename) {
76
    $file = array();
77
    if(file_exists($filename)) {
78
        $text = return_filename_as_string($filename);
79
        $text_split = split("\n", $text);
80

    
81
        /* Strip out comments */
82
        while (($line = array_shift($text_split)) != NULL) {
83
            if(strpos($line, "#") !== 0)
84
                array_push($file, $line);
85
        }
86
    }
87
    return $file;
88
}
89

    
90
/*
91
 * return_dir_as_array($filename): returns $filename contents as a string
92
 */
93
function return_filename_as_string($filename) {
94
    $tmp = "";
95
    $fd = fopen($filename, "r");
96
    if(!$fd) {
97
	log_error("Could not open {$filename}");
98
	return;
99
    }
100
    while(!feof($fd)) {
101
	$tmp .= fread($fd,49);
102
    }
103
    fclose($fd);
104
    return $tmp;
105
}
106

    
107
/*
108
 * is_carp_defined: returns true if carp is detected in kernel
109
 */
110
function is_carp_defined() {
111
	/* is carp compiled into the kernel and userland? */
112
	$command = "/sbin/sysctl -a | grep carp";
113
	$fd = popen($command . " 2>&1 ", "r");
114
	if(!$fd) {
115
		log_error("Warning, could not execute command {$command}");
116
		return 0;
117
	}
118
	while(!feof($fd)) {
119
		    $tmp .= fread($fd,49);
120
	}
121
	fclose($fd);
122

    
123
	if($tmp == "")
124
		return false;
125
	else
126
		return true;
127
}
128

    
129
/*
130
 * find_number_of_created_carp_interfaces() returns the number of currently created carp interfaces
131
 */
132
function find_number_of_created_carp_interfaces() {
133
	$command = "/sbin/ifconfig | /usr/bin/grep \"carp*:\" | /usr/bin/wc -l";
134
	$fd = popen($command . " 2>&1 ", "r");
135
	if(!$fd) {
136
		log_error("Warning, could not execute command {$command}");
137
		return 0;
138
	}
139
	while(!feof($fd)) {
140
		    $tmp .= fread($fd,49);
141
	}
142
	fclose($fd);
143
	$tmp = intval($tmp) -1;
144
	return $tmp;
145
}
146

    
147
/*
148
 * link_ip_to_carp_interface($ip): finds where a carp interface links to.
149
*/
150
function link_ip_to_carp_interface($ip) {
151
	global $config;
152
	if($ip == "") return;
153
        $i = 0;
154

    
155
        $ifdescrs = array('wan', 'lan');
156
        for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
157
                $ifdescrs['opt' . $j] = "opt" . $j;
158
        }
159

    
160
	$ft = split("\.", $ip);
161
	$ft_ip = $ft[0] . "." . $ft[1] . "." . $ft[2] . ".";
162

    
163
	$carp_ints = "";
164
	$num_carp_ints = find_number_of_created_carp_interfaces();
165
        foreach ($ifdescrs as $ifdescr => $ifname) {
166
                for($x=0; $x<$num_carp_ints; $x++) {
167
                        $carp_int = "carp{$x}";
168
			$carp_ip = find_interface_ip($carp_int);
169
			$carp_ft = split("\.", $carp_ip);
170
			$carp_ft_ip = $carp_ft[0] . "." . $carp_ft[1] . "." . $carp_ft[2] . ".";
171
                        $result = does_interface_exist($carp_int);
172
                        if($result <> true) break;
173
                        $interface = filter_opt_interface_to_real($ifname);
174
			if($ft_ip == $carp_ft_ip)
175
			    if(stristr($carp_ints,$carp_int) == false)
176
				$carp_ints .= " " . $carp_int;
177
                }
178
        }
179
	return $carp_ints;
180
}
181

    
182

    
183
/*
184
 * exec_command($command): execute command return string of result
185
 */
186
function exec_command($command) {
187
            $counter = 0;
188
            $tmp = "";
189
            $fd = popen($command . " 2>&1 ", "r");
190
            while(!feof($fd)) {
191
                        $tmp .= fread($fd,49);
192
            }
193
            fclose($fd);
194
            return $tmp;
195
}
196

    
197
/*
198
 * does_interface_exist($interface): return true or false if a interface is detected.
199
 */
200
function does_interface_exist($interface) {
201
    $ints = exec_command("/sbin/ifconfig -l");
202
    if(stristr($ints, $interface) !== false)
203
	return true;
204
    else
205
	return false;
206
}
207

    
208
/*
209
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
210
 */
211
function convert_ip_to_network_format($ip, $subnet) {
212
    $ipsplit = split('[.]', $ip);
213
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
214
    return $string;
215
}
216

    
217
/*
218
 * find_interface_ip($interface): return the interface ip (first found)
219
 */
220
function find_interface_ip($interface) {
221
    if(does_interface_exist($interface) == false) return;
222
    $ip = exec_command("/sbin/ifconfig {$interface} | /usr/bin/grep -w \"inet\" | /usr/bin/cut -d\" \" -f 2");
223
    $ip = str_replace("\n","",$ip);
224
    return $ip;
225
}
226

    
227
function filter_opt_interface_to_real($opt) {
228
	global $config;
229
	return $config['interfaces'][$opt]['if'];
230
}
231

    
232
function filter_get_opt_interface_descr($opt) {
233
	global $config;
234
	return $config['interfaces'][$opt]['descr'];
235
}
236

    
237
/*
238
 * find_ip_interface($ip): return the interface where an ip is defined
239
 */
240
function find_ip_interface($ip) {
241
	global $config;
242
	$i = 0;
243
	$ifdescrs = array('wan', 'lan');
244
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
245
		$ifdescrs['opt' . $j] = "opt" . $j;
246
	}
247
	foreach ($ifdescrs as $ifdescr => $ifname) {
248
	    $int = filter_translate_type_to_real_interface($ifname);
249
	    $ifconfig = exec_command("/sbin/ifconfig {$int}");
250
	    if(stristr($ifconfig,$ip) <> false)
251
		return $int;
252
	}
253
	return false;
254
}
255

    
256
/*
257
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
258
 */
259
function get_carp_interface_status($carpinterface) {
260
    $result = does_interface_exist($carpinterface);
261
    if($result <> true) return false;
262
    $status = exec_command("/sbin/ifconfig {$carpinterface} | /usr/bin/grep \"carp:\" | /usr/bin/cut -d\" \" -f2");
263
    return $status;
264
}
265

    
266
/*
267
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
268
 */
269
function get_pfsync_interface_status($pfsyncinterface) {
270
    $result = does_interface_exist($pfsyncinterface);
271
    if($result <> true) return;
272
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | /usr/bin/grep \"pfsync:\" | /usr/bin/cut -d\" \" -f5");
273
    return $status;
274
}
275

    
276
/*
277
 * find_carp_interface($ip): return the carp interface where an ip is defined
278
 */
279
function find_carp_interface($ip) {
280
    $num_carp_ints = find_number_of_created_carp_interfaces();
281
    for($x=0; $x<$num_carp_ints; $x++) {
282
        $result = does_interface_exist("carp{$x}");
283
	if($result <> true) return;
284
	$ifconfig = exec_command("/sbin/ifconfig carp{$x}");
285
	if(stristr($ifconfig,$ip))
286
	    return "carp" . $x;
287
    }
288
}
289

    
290
/*
291
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
292
 */
293
function add_rule_to_anchor($anchor, $rule, $label) {
294
    mwexec("echo " . $rule . " | /sbin/pfctl -a " . $anchor . ":" . $label . " -f -");
295
}
296

    
297
/*
298
 * remove_text_from_file
299
 * remove $text from file $file
300
 */
301
function remove_text_from_file($file, $text) {
302
    global $fd_log;
303
    fwrite($fd_log, "Adding needed text items:\n");
304
    $filecontents = exec_command_and_return_text("cat " . $file);
305
    $textTMP = str_replace($text, "", $filecontents);
306
    $text .= $textTMP;
307
    fwrite($fd_log, $text . "\n");
308
    $fd = fopen($file, "w");
309
    fwrite($fd, $text);
310
    fclose($fd);
311
}
312

    
313
/*
314
 * lookup pkg array id#
315
 */
316
function get_pkg_id($pkg_name) {
317
            global $config;
318
            global $pkg_config;
319
            $i=0;
320
            foreach ($config['installedpackages']['package'] as $pkg) {
321
                        if($pkg['name'] == $pkg_name) return $i;
322
                        $i++;
323
            }
324
            return -1;
325
}
326

    
327
/*
328
 *  get_latest_package_version($pkgname): get current version of a package.
329
 *  returns latest package version
330
 */
331
function get_latest_package_version($pkg_name) {
332
    global $g;
333
    fetch_latest_pkg_config();
334
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
335
    foreach($pkg_config['packages']['package'] as $pkg) {
336
	if($pkg['name'] == $pkg_name) {
337
	    return $pkg['version'];
338
	}
339
    }
340
    return;
341
}
342

    
343
/*
344
 * Lookup pkg_id in pkg_config.xml
345
 */
346
function get_available_pkg_id($pkg_name) {
347
    fetch_latest_pkg_config();
348
    $pkg_config = parse_xml_config_pkg("{$g['tmp_path']}/pkg_config.xml", "pfsensepkgs");
349
    $id = 0;
350
    foreach($pkg_config as $pkg) {
351
	if($pkg_config['name'] == $pkg_name) {
352
	    return $id;
353
	}
354
	$id++;
355
    }
356
    return;
357
}
358

    
359
/*
360
 * fetch_latest_pkg_config: download the latest pkg_config.xml to /tmp/ directory
361
 */
362
function fetch_latest_pkg_config() {
363
    global $g;
364
    if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
365
	mwexec("/usr/bin/fetch -o {$g['tmp_path']}/pkg_config.xml {$g['pkg_config_location']}");
366
	if(!file_exists("{$g['tmp_path']}/pkg_config.xml")) {
367
	    print_info_box_np("Could not download pkg_config.xml from pfSense.com.  Check your DNS settings.");
368
	    die;
369
	}
370
    }
371
    return;
372
}
373

    
374
/*
375
 * add_text_to_file($file, $text): adds $text to $file.
376
 * replaces the text if it already exists.
377
 */
378
function add_text_to_file($file, $text) {
379
    global $fd_log;
380
    fwrite($fd_log, "Adding needed text items:\n");
381
    $filecontents = exec_command_and_return_text("cat " . $file);
382
    $filecontents = str_replace($text, "", $filecontents);
383
    $text = $filecontents . $text;
384
    fwrite($fd_log, $text . "\n");
385
    $fd = fopen($file, "w");
386
    fwrite($fd, $text . "\n");
387
    fclose($fd);
388
}
389

    
390
/*
391
 * get_filename_from_url($url): converts a url to its filename.
392
 */
393
function get_filename_from_url($url) {
394
            $filenamesplit = split("/", $url);
395
            foreach($filenamesplit as $fn) $filename = $fn;
396
            return $filename;
397
}
398

    
399
/*
400
 *   update_output_window: update bottom textarea dynamically.
401
 */
402
function update_output_window($text) {
403
            $log = ereg_replace("\n", "\\n", $text);
404
            echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"" . $log . "\";</script>";
405
}
406

    
407
/*
408
 *   get_dir: return an array of $dir
409
 */
410
function get_dir($dir) {
411
            $dir_array = array();
412
            $d = dir($dir);
413
            while (false !== ($entry = $d->read())) {
414
                        array_push($dir_array, $entry);
415
            }
416
            $d->close();
417
            return $dir_array;
418
}
419

    
420
/*
421
 *   update_output_window: update top textarea dynamically.
422
 */
423
function update_status($status) {
424
            echo "\n<script language=\"JavaScript\">document.forms[0].status.value=\"" . $status . "\";</script>";
425
}
426

    
427
/*
428
 *   exec_command_and_return_text_array: execute command and return output
429
 */
430
function exec_command_and_return_text_array($command) {
431
            $counter = 0;
432
            $fd = popen($command . " 2>&1 ", "r");
433
            while(!feof($fd)) {
434
                        $tmp .= fread($fd,49);
435
            }
436
            fclose($fd);
437
            $temp_array = split("\n", $tmp);
438
            return $tmp_array;
439
}
440

    
441
/*
442
 *   exec_command_and_return_text: execute command and return output
443
 */
444
function exec_command_and_return_text($command) {
445
	    return exec_command($command);
446
}
447

    
448
/*
449
 *   exec_command_and_return_text: execute command and update output window dynamically
450
 */
451
function execute_command_return_output($command) {
452
    global $fd_log;
453
    $fd = popen($command . " 2>&1 ", "r");
454
    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
455
    $counter = 0;
456
    $counter2 = 0;
457
    while(!feof($fd)) {
458
	$tmp = fread($fd, 50);
459
	$tmp1 = ereg_replace("\n","\\n", $tmp);
460
	$text = ereg_replace("\"","'", $tmp1);
461
	if($lasttext == "..") {
462
	    $text = "";
463
	    $lasttext = "";
464
	    $counter=$counter-2;
465
	} else {
466
	    $lasttext .= $text;
467
	}
468
	if($counter > 51) {
469
	    $counter = 0;
470
	    $extrabreak = "\\n";
471
	} else {
472
	    $extrabreak = "";
473
	    $counter++;
474
	}
475
	if($counter2 > 600) {
476
	    echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = \"\";</script>";
477
	    $counter2 = 0;
478
	} else
479
	    $counter2++;
480
	echo "\n<script language=\"JavaScript\">this.document.forms[0].output.value = this.document.forms[0].output.value + \"" . $text . $extrabreak .  "\"; f('output'); </script>";
481
    }
482
    fclose($fd);
483
}
484

    
485
/*
486
 * convert_real_interface_to_friendly_interface_name($interface): convert fxp0 -> wan, etc.
487
 */
488
function convert_real_interface_to_friendly_interface_name($interface) {
489
	global $config;
490
	$i = 0;
491
	$ifdescrs = array('wan', 'lan');
492
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
493
		$ifdescrs['opt' . $j] = "opt" . $j;
494
	}
495
	foreach ($ifdescrs as $ifdescr => $ifname) {
496
	    $int = filter_translate_type_to_real_interface($ifname);
497
	    if($int == $interface) return $ifname;
498
	}
499
	return $interface;
500
}
501

    
502
/*
503
 * update_progress_bar($percent): updates the javascript driven progress bar.
504
 */
505
function update_progress_bar($percent) {
506
            if($percent > 100) $percent = 1;
507
            echo "\n<script type=\"text/javascript\" language=\"javascript\">";
508
            echo "\ndocument.progressbar.style.width='" . $percent . "%';";
509
            echo "\n</script>";
510
}
511

    
512

    
513
?>
(9-9/14)