Project

General

Profile

Download (4.88 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
 * return_dir_as_array($filenaem): returns $filename contents as a string
62
 */
63
function return_filename_as_string($filename) {
64
    $tmp = "";
65
    $fd = fopen($filename, "r");
66
    while(!feof($fd)) {
67
	$tmp .= fread($fd,49);
68
    }
69
    fclose($fd);
70
    return $tmp;
71
}
72

    
73
/*
74
 * is_carp_defined: returns true if carp is detected in kernel
75
 */
76
function is_carp_defined() {
77
	/* is carp compiled into the kernel and userland? */
78
	$command = "/sbin/sysctl -a | grep carp";
79
	$fd = popen($command . " 2>&1 ", "r");
80
	if(!$fd) {
81
		log_error("Warning, could not execute command ");
82
		return 0;
83
	}
84
	while(!feof($fd)) {
85
		    $tmp .= fread($fd,49);
86
	}
87
	fclose($fd);
88

    
89
	if($tmp == "")
90
		return false;
91
	else
92
		return true;
93
}
94

    
95
/*
96
 * exec_command($command): execute command return string of result
97
 */
98
function exec_command($command) {
99
            $counter = 0;
100
            $tmp = "";
101
            $fd = popen($command . " 2>&1 ", "r");
102
            while(!feof($fd)) {
103
                        $tmp .= fread($fd,49);
104
            }
105
            fclose($fd);
106
            return $tmp;
107
}
108

    
109
/*
110
 * convert_ip_to_network_format($ip, $subnet): converts an ip address to network form
111
 */
112
function convert_ip_to_network_format($ip, $subnet) {
113
    $ipsplit = split('[.]', $ip);
114
    $string = $ipsplit[0] . "." . $ipsplit[1] . "." . $ipsplit[2] . ".0/" . $subnet;
115
    return $string;
116
}
117

    
118
/*
119
 * find_interface_ip($interface): return the interface ip (first found)
120
 */
121
function find_interface_ip($interface) {
122
    $ip = system("/sbin/ifconfig {$interface} | grep \"inet \" | cut -d\" \" -f 2");
123
    return $ip;
124
}
125

    
126
/*
127
 * find_ip_interface($ip): return the interface where an ip is defined
128
 */
129
function find_ip_interface($ip) {
130
	$i = 0;
131
	$ifdescrs = array('wan', 'lan');
132
	for ($j = 1; isset($config['interfaces']['opt' . $j]); $j++) {
133
		$ifdescrs['opt' . $j] = "opt" . $j;
134
	}
135
	foreach ($ifdescrs as $ifdescr => $ifname) {
136
	    $int = filter_translate_type_to_real_interface($ifname);
137
	    $ifconfig = exec_command("/sbin/ifconfig {$int}");
138
	    if(stristr($ifconfig,$ip) <> false)
139
		return $int;
140
	}
141
	return false;
142
}
143

    
144
/*
145
 * get_carp_interface_status($carpinterface): returns the status of a carp ip
146
 */
147
function get_carp_interface_status($carpinterface) {
148
    $status = exec_command("/sbin/ifconfig {$carpinterface} | grep \"carp:\" | cut -d\" \" -f2");
149
    return $status;
150
}
151

    
152
/*
153
 * get_pfsync_interface_status($pfsyncinterface): returns the status of a pfsync
154
 */
155
function get_pfsync_interface_status($pfsyncinterface) {
156
    $status = exec_command("/sbin/ifconfig {$pfsyncinterface} | grep \"pfsync:\" | cut -d\" \" -f5");
157
    return $status;
158
}
159

    
160
/*
161
 * find_carp_interface($ip): return the carp interface where an ip is defined
162
 */
163
function find_carp_interface($ip) {
164
    for($x=0; $x<99; $x++) {
165
	$ifconfig = exec_command("/sbin/ifconfig {$int}");
166
	if(stristr($ifconfig,$ip) <> false)
167
	    return "carp" . $x;
168
    }
169
}
170

    
171
/*
172
 * add_rule_to_anchor($anchor, $rule): adds the specified rule to an anchor
173
 */
174
function add_rule_to_anchor($anchor, $rule) {
175
    mwexec($rule . " | pfctl -a " . $anchor . " -f -");
176
}
177

    
178
?>
(9-9/14)