1 |
417fc5c4
|
Scott Ullrich
|
<?php
|
2 |
|
|
/*
|
3 |
5b237745
|
Scott Ullrich
|
util.inc
|
4 |
417fc5c4
|
Scott Ullrich
|
part of the pfSense project (http://www.pfsense.com)
|
5 |
98bbf05a
|
Scott Ullrich
|
|
6 |
417fc5c4
|
Scott Ullrich
|
originally part of m0n0wall (http://m0n0.ch/wall)
|
7 |
5b237745
|
Scott Ullrich
|
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
|
8 |
|
|
All rights reserved.
|
9 |
98bbf05a
|
Scott Ullrich
|
|
10 |
5b237745
|
Scott Ullrich
|
Redistribution and use in source and binary forms, with or without
|
11 |
|
|
modification, are permitted provided that the following conditions are met:
|
12 |
98bbf05a
|
Scott Ullrich
|
|
13 |
5b237745
|
Scott Ullrich
|
1. Redistributions of source code must retain the above copyright notice,
|
14 |
|
|
this list of conditions and the following disclaimer.
|
15 |
98bbf05a
|
Scott Ullrich
|
|
16 |
5b237745
|
Scott Ullrich
|
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 |
98bbf05a
|
Scott Ullrich
|
|
20 |
5b237745
|
Scott Ullrich
|
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 |
523855b0
|
Scott Ullrich
|
/*
|
33 |
|
|
pfSense_BUILDER_BINARIES: /bin/ps /bin/kill /usr/bin/killall /sbin/ifconfig /usr/bin/netstat
|
34 |
|
|
pfSense_BUILDER_BINARIES: /usr/bin/awk /sbin/dmesg /sbin/ping /usr/local/sbin/gzsig /usr/sbin/arp
|
35 |
|
|
pfSense_BUILDER_BINARIES: /sbin/conscontrol /sbin/devd /bin/ps
|
36 |
|
|
pfSense_MODULE: utils
|
37 |
|
|
*/
|
38 |
|
|
|
39 |
5b237745
|
Scott Ullrich
|
/* kill a process by pid file */
|
40 |
|
|
function killbypid($pidfile) {
|
41 |
435a418f
|
Ermal
|
return sigkillbypid($pidfile, "TERM");
|
42 |
5b237745
|
Scott Ullrich
|
}
|
43 |
|
|
|
44 |
53aca1fd
|
Scott Ullrich
|
function isvalidpid($pid) {
|
45 |
0e604b3a
|
Ermal
|
$output = "";
|
46 |
|
|
exec("/bin/pgrep -F {$pid}", $output, $retval);
|
47 |
|
|
|
48 |
5bbd08e1
|
Warren Baker
|
return (intval($retval) == 0);
|
49 |
53aca1fd
|
Scott Ullrich
|
}
|
50 |
|
|
|
51 |
6dc3a5c2
|
Ermal Lu?i
|
function is_process_running($process) {
|
52 |
01d4b621
|
Ermal
|
$output = "";
|
53 |
05c4bfa0
|
Ermal
|
exec("/bin/pgrep -ax {$process}", $output, $retval);
|
54 |
6dc3a5c2
|
Ermal Lu?i
|
|
55 |
5bbd08e1
|
Warren Baker
|
return (intval($retval) == 0);
|
56 |
6dc3a5c2
|
Ermal Lu?i
|
}
|
57 |
|
|
|
58 |
53aca1fd
|
Scott Ullrich
|
function isvalidproc($proc) {
|
59 |
ba8495f0
|
Ermal
|
return is_process_running($proc);
|
60 |
53aca1fd
|
Scott Ullrich
|
}
|
61 |
|
|
|
62 |
5b237745
|
Scott Ullrich
|
/* sigkill a process by pid file */
|
63 |
53aca1fd
|
Scott Ullrich
|
/* return 1 for success and 0 for a failure */
|
64 |
5b237745
|
Scott Ullrich
|
function sigkillbypid($pidfile, $sig) {
|
65 |
ba8495f0
|
Ermal
|
if (is_file($pidfile))
|
66 |
|
|
return mwexec("/bin/pkill -{$sig} -F {$pidfile}", true);
|
67 |
|
|
|
68 |
53aca1fd
|
Scott Ullrich
|
return 0;
|
69 |
|
|
}
|
70 |
|
|
|
71 |
|
|
/* kill a process by name */
|
72 |
|
|
function sigkillbyname($procname, $sig) {
|
73 |
|
|
if(isvalidproc($procname))
|
74 |
73239086
|
Seth Mos
|
return mwexec("/usr/bin/killall -{$sig} " . escapeshellarg($procname), true);
|
75 |
5b237745
|
Scott Ullrich
|
}
|
76 |
|
|
|
77 |
|
|
/* kill a process by name */
|
78 |
|
|
function killbyname($procname) {
|
79 |
123f030c
|
Chris Buechler
|
if(isvalidproc($procname))
|
80 |
53aca1fd
|
Scott Ullrich
|
mwexec("/usr/bin/killall " . escapeshellarg($procname));
|
81 |
5b237745
|
Scott Ullrich
|
}
|
82 |
|
|
|
83 |
a368a026
|
Ermal Lu?i
|
function is_subsystem_dirty($subsystem = "") {
|
84 |
|
|
global $g;
|
85 |
|
|
|
86 |
|
|
if ($subsystem == "")
|
87 |
|
|
return false;
|
88 |
|
|
|
89 |
|
|
if (file_exists("{$g['varrun_path']}/{$subsystem}.dirty"))
|
90 |
|
|
return true;
|
91 |
|
|
|
92 |
|
|
return false;
|
93 |
|
|
}
|
94 |
|
|
|
95 |
|
|
function mark_subsystem_dirty($subsystem = "") {
|
96 |
|
|
global $g;
|
97 |
|
|
|
98 |
|
|
if (!file_put_contents("{$g['varrun_path']}/{$subsystem}.dirty", "DIRTY"))
|
99 |
|
|
log_error("WARNING: Could not mark subsystem: {$subsytem} dirty");
|
100 |
|
|
}
|
101 |
|
|
|
102 |
|
|
function clear_subsystem_dirty($subsystem = "") {
|
103 |
|
|
global $g;
|
104 |
|
|
|
105 |
|
|
@unlink("{$g['varrun_path']}/{$subsystem}.dirty");
|
106 |
|
|
}
|
107 |
|
|
|
108 |
0027de0a
|
Ermal Lu?i
|
function config_lock() {
|
109 |
|
|
return;
|
110 |
|
|
}
|
111 |
|
|
function config_unlock() {
|
112 |
|
|
return;
|
113 |
|
|
}
|
114 |
|
|
|
115 |
|
|
/* lock configuration file */
|
116 |
b6c34bfc
|
Ermal
|
function lock($lock, $op = LOCK_SH) {
|
117 |
9e7ef1a5
|
Scott Ullrich
|
global $g, $cfglckkeyconsumers;
|
118 |
|
|
if (!$lock)
|
119 |
|
|
die("WARNING: You must give a name as parameter to lock() function.");
|
120 |
|
|
if (!file_exists("{$g['tmp_path']}/{$lock}.lock"))
|
121 |
|
|
@touch("{$g['tmp_path']}/{$lock}.lock");
|
122 |
|
|
$cfglckkeyconsumers++;
|
123 |
b6c34bfc
|
Ermal
|
if ($fp = fopen("{$g['tmp_path']}/{$lock}.lock", "w")) {
|
124 |
|
|
if (flock($fp, $op))
|
125 |
9e7ef1a5
|
Scott Ullrich
|
return $fp;
|
126 |
b6c34bfc
|
Ermal
|
else
|
127 |
|
|
fclose($fp);
|
128 |
9e7ef1a5
|
Scott Ullrich
|
}
|
129 |
0027de0a
|
Ermal Lu?i
|
}
|
130 |
|
|
|
131 |
|
|
/* unlock configuration file */
|
132 |
|
|
function unlock($cfglckkey = 0) {
|
133 |
9e7ef1a5
|
Scott Ullrich
|
global $g, $cfglckkeyconsumers;
|
134 |
|
|
flock($cfglckkey, LOCK_UN);
|
135 |
cb6fd90b
|
Ermal Lu?i
|
fclose($cfglckkey);
|
136 |
9e7ef1a5
|
Scott Ullrich
|
return;
|
137 |
0027de0a
|
Ermal Lu?i
|
}
|
138 |
|
|
|
139 |
0ae6daf8
|
Ermal
|
function send_event($cmd) {
|
140 |
|
|
global $g;
|
141 |
|
|
|
142 |
1015b3a9
|
Warren Baker
|
if(!isset($g['event_address']))
|
143 |
|
|
$g['event_address'] = "unix:///var/run/check_reload_status";
|
144 |
|
|
|
145 |
838feb14
|
Ermal
|
$try = 0;
|
146 |
|
|
while ($try < 3) {
|
147 |
|
|
$fd = @fsockopen($g['event_address']);
|
148 |
|
|
if ($fd) {
|
149 |
|
|
fwrite($fd, $cmd);
|
150 |
|
|
$resp = fread($fd, 4096);
|
151 |
|
|
if ($resp != "OK\n")
|
152 |
|
|
log_error("send_event: sent {$cmd} got {$resp}");
|
153 |
|
|
fclose($fd);
|
154 |
|
|
$try = 3;
|
155 |
|
|
} else
|
156 |
|
|
mwexec_bg("/usr/bin/nice -n20 /usr/local/sbin/check_reload_status");
|
157 |
|
|
$try++;
|
158 |
0ae6daf8
|
Ermal
|
}
|
159 |
|
|
}
|
160 |
|
|
|
161 |
|
|
function send_multiple_events($cmds) {
|
162 |
1015b3a9
|
Warren Baker
|
global $g;
|
163 |
0ae6daf8
|
Ermal
|
|
164 |
1015b3a9
|
Warren Baker
|
if(!isset($g['event_address']))
|
165 |
|
|
$g['event_address'] = "unix:///var/run/check_reload_status";
|
166 |
|
|
|
167 |
0ae6daf8
|
Ermal
|
if (!is_array($cmds))
|
168 |
|
|
return;
|
169 |
1015b3a9
|
Warren Baker
|
$fd = fsockopen($g['event_address']);
|
170 |
|
|
if ($fd) {
|
171 |
0ae6daf8
|
Ermal
|
foreach ($cmds as $cmd) {
|
172 |
1015b3a9
|
Warren Baker
|
fwrite($fd, $cmd);
|
173 |
|
|
$resp = fread($fd, 4096);
|
174 |
|
|
if ($resp != "OK\n")
|
175 |
|
|
log_error("send_event: sent {$cmd} got {$resp}");
|
176 |
0ae6daf8
|
Ermal
|
}
|
177 |
1015b3a9
|
Warren Baker
|
fclose($fd);
|
178 |
|
|
}
|
179 |
0ae6daf8
|
Ermal
|
}
|
180 |
|
|
|
181 |
ef3af02e
|
Ermal Lu?i
|
function refcount_init($reference) {
|
182 |
2ae24c22
|
Ermal
|
$shmid = shmop_open($reference, "c", 0644, 10);
|
183 |
ef3af02e
|
Ermal Lu?i
|
shmop_write($shmid, 0, 0);
|
184 |
|
|
shmop_close($shmid);
|
185 |
|
|
}
|
186 |
|
|
|
187 |
|
|
function refcount_reference($reference) {
|
188 |
a45e27ba
|
Ermal
|
$shmid = @shmop_open($reference, "w", 0644, 10);
|
189 |
|
|
if (!$shmid) {
|
190 |
|
|
refcount_init($reference);
|
191 |
|
|
$shmid = shmop_open($reference, "w", 0644, 10);
|
192 |
|
|
}
|
193 |
ef3af02e
|
Ermal Lu?i
|
$shm_data = shmop_read($shmid, 0, 10);
|
194 |
|
|
$shm_data = intval($shm_data) + 1;
|
195 |
|
|
shmop_write($shmid, $shm_data, 0);
|
196 |
|
|
shmop_close($shmid);
|
197 |
|
|
|
198 |
|
|
return $shm_data;
|
199 |
|
|
}
|
200 |
|
|
|
201 |
|
|
function refcount_unreference($reference) {
|
202 |
|
|
/* We assume that the shared memory exists. */
|
203 |
2ae24c22
|
Ermal
|
$shmid = shmop_open($reference, "w", 0644, 10);
|
204 |
ef3af02e
|
Ermal Lu?i
|
$shm_data = shmop_read($shmid, 0, 10);
|
205 |
|
|
$shm_data = intval($shm_data) - 1;
|
206 |
|
|
if ($shm_data < 0) {
|
207 |
|
|
//debug_backtrace();
|
208 |
|
|
log_error("Reference {$reference} is going negative, not doing unreference.");
|
209 |
|
|
} else
|
210 |
|
|
shmop_write($shmid, $shm_data, 0);
|
211 |
|
|
shmop_close($shmid);
|
212 |
f2f0a748
|
Ermal Lu?i
|
|
213 |
|
|
return $shm_data;
|
214 |
ef3af02e
|
Ermal Lu?i
|
}
|
215 |
|
|
|
216 |
1ab56363
|
Ermal Lu?i
|
function is_module_loaded($module_name) {
|
217 |
de5de07c
|
Ermal Lu?i
|
$running = `/sbin/kldstat | grep {$module_name} | /usr/bin/grep -v grep | /usr/bin/wc -l`;
|
218 |
1ab56363
|
Ermal Lu?i
|
if (intval($running) >= 1)
|
219 |
|
|
return true;
|
220 |
|
|
else
|
221 |
|
|
return false;
|
222 |
|
|
}
|
223 |
|
|
|
224 |
5b237745
|
Scott Ullrich
|
/* return the subnet address given a host address and a subnet bit count */
|
225 |
|
|
function gen_subnet($ipaddr, $bits) {
|
226 |
|
|
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
|
227 |
|
|
return "";
|
228 |
98bbf05a
|
Scott Ullrich
|
|
229 |
5b237745
|
Scott Ullrich
|
return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits));
|
230 |
|
|
}
|
231 |
|
|
|
232 |
|
|
/* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */
|
233 |
|
|
function gen_subnet_max($ipaddr, $bits) {
|
234 |
|
|
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
|
235 |
|
|
return "";
|
236 |
98bbf05a
|
Scott Ullrich
|
|
237 |
96033063
|
Erik Fonnesbeck
|
return long2ip32(ip2long($ipaddr) | ~gen_subnet_mask_long($bits));
|
238 |
5b237745
|
Scott Ullrich
|
}
|
239 |
|
|
|
240 |
|
|
/* returns a subnet mask (long given a bit count) */
|
241 |
|
|
function gen_subnet_mask_long($bits) {
|
242 |
|
|
$sm = 0;
|
243 |
|
|
for ($i = 0; $i < $bits; $i++) {
|
244 |
|
|
$sm >>= 1;
|
245 |
|
|
$sm |= 0x80000000;
|
246 |
|
|
}
|
247 |
|
|
return $sm;
|
248 |
|
|
}
|
249 |
|
|
|
250 |
|
|
/* same as above but returns a string */
|
251 |
|
|
function gen_subnet_mask($bits) {
|
252 |
|
|
return long2ip(gen_subnet_mask_long($bits));
|
253 |
|
|
}
|
254 |
|
|
|
255 |
96033063
|
Erik Fonnesbeck
|
/* Convert long int to IP address, truncating to 32-bits. */
|
256 |
|
|
function long2ip32($ip) {
|
257 |
|
|
return long2ip($ip & 0xFFFFFFFF);
|
258 |
|
|
}
|
259 |
|
|
|
260 |
|
|
/* Convert IP address to long int, truncated to 32-bits to avoid sign extension on 64-bit platforms. */
|
261 |
|
|
function ip2long32($ip) {
|
262 |
|
|
return ( ip2long($ip) & 0xFFFFFFFF );
|
263 |
|
|
}
|
264 |
|
|
|
265 |
ecd1f2d9
|
jim-p
|
/* Convert IP address to unsigned long int. */
|
266 |
|
|
function ip2ulong($ip) {
|
267 |
96033063
|
Erik Fonnesbeck
|
return sprintf("%u", ip2long32($ip));
|
268 |
ecd1f2d9
|
jim-p
|
}
|
269 |
|
|
|
270 |
|
|
/* Find out how many IPs are contained within a given IP range
|
271 |
|
|
* e.g. 192.168.0.0 to 192.168.0.255 returns 256
|
272 |
|
|
*/
|
273 |
|
|
function ip_range_size($startip, $endip) {
|
274 |
|
|
if (is_ipaddr($startip) && is_ipaddr($endip)) {
|
275 |
|
|
// Operate as unsigned long because otherwise it wouldn't work
|
276 |
|
|
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier
|
277 |
|
|
return abs(ip2ulong($startip) - ip2ulong($endip)) + 1;
|
278 |
|
|
}
|
279 |
|
|
return -1;
|
280 |
|
|
}
|
281 |
|
|
|
282 |
|
|
/* Find the smallest possible subnet mask which can contain a given number of IPs
|
283 |
|
|
* e.g. 512 IPs can fit in a /23, but 513 IPs need a /22
|
284 |
|
|
*/
|
285 |
|
|
function find_smallest_cidr($number) {
|
286 |
|
|
$smallest = 1;
|
287 |
|
|
for ($b=32; $b > 0; $b--) {
|
288 |
|
|
$smallest = ($number <= pow(2,$b)) ? $b : $smallest;
|
289 |
|
|
}
|
290 |
|
|
return (32-$smallest);
|
291 |
|
|
}
|
292 |
|
|
|
293 |
|
|
/* Return the previous IP address before the given address */
|
294 |
|
|
function ip_before($ip) {
|
295 |
96033063
|
Erik Fonnesbeck
|
return long2ip32(ip2long($ip)-1);
|
296 |
ecd1f2d9
|
jim-p
|
}
|
297 |
|
|
|
298 |
|
|
/* Return the next IP address after the given address */
|
299 |
|
|
function ip_after($ip) {
|
300 |
96033063
|
Erik Fonnesbeck
|
return long2ip32(ip2long($ip)+1);
|
301 |
ecd1f2d9
|
jim-p
|
}
|
302 |
|
|
|
303 |
|
|
/* Return true if the first IP is 'before' the second */
|
304 |
|
|
function ip_less_than($ip1, $ip2) {
|
305 |
|
|
// Compare as unsigned long because otherwise it wouldn't work when
|
306 |
|
|
// crossing over from 127.255.255.255 / 128.0.0.0 barrier
|
307 |
|
|
return ip2ulong($ip1) < ip2ulong($ip2);
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
/* Return true if the first IP is 'after' the second */
|
311 |
|
|
function ip_greater_than($ip1, $ip2) {
|
312 |
|
|
// Compare as unsigned long because otherwise it wouldn't work
|
313 |
|
|
// when crossing over from 127.255.255.255 / 128.0.0.0 barrier
|
314 |
|
|
return ip2ulong($ip1) > ip2ulong($ip2);
|
315 |
|
|
}
|
316 |
|
|
|
317 |
|
|
/* Convert a range of IPs to an array of subnets which can contain the range. */
|
318 |
|
|
function ip_range_to_subnet_array($startip, $endip) {
|
319 |
|
|
if (!is_ipaddr($startip) || !is_ipaddr($endip)) {
|
320 |
|
|
return array();
|
321 |
|
|
}
|
322 |
|
|
|
323 |
|
|
// Container for subnets within this range.
|
324 |
|
|
$rangesubnets = array();
|
325 |
|
|
|
326 |
|
|
// Figure out what the smallest subnet is that holds the number of IPs in the given range.
|
327 |
|
|
$cidr = find_smallest_cidr(ip_range_size($startip, $endip));
|
328 |
|
|
|
329 |
|
|
// Loop here to reduce subnet size and retest as needed. We need to make sure
|
330 |
|
|
// that the target subnet is wholly contained between $startip and $endip.
|
331 |
|
|
for ($cidr; $cidr <= 32; $cidr++) {
|
332 |
|
|
// Find the network and broadcast addresses for the subnet being tested.
|
333 |
|
|
$targetsub_min = gen_subnet($startip, $cidr);
|
334 |
|
|
$targetsub_max = gen_subnet_max($startip, $cidr);
|
335 |
|
|
|
336 |
|
|
// Check best case where the range is exactly one subnet.
|
337 |
|
|
if (($targetsub_min == $startip) && ($targetsub_max == $endip)) {
|
338 |
|
|
// Hooray, the range is exactly this subnet!
|
339 |
|
|
return array("{$startip}/{$cidr}");
|
340 |
|
|
}
|
341 |
|
|
|
342 |
|
|
// These remaining scenarios will find a subnet that uses the largest
|
343 |
|
|
// chunk possible of the range being tested, and leave the rest to be
|
344 |
|
|
// tested recursively after the loop.
|
345 |
|
|
|
346 |
|
|
// Check if the subnet begins with $startip and ends before $endip
|
347 |
|
|
if (($targetsub_min == $startip) && ip_less_than($targetsub_max, $endip)) {
|
348 |
|
|
break;
|
349 |
|
|
}
|
350 |
|
|
|
351 |
|
|
// Check if the subnet ends at $endip and starts after $startip
|
352 |
|
|
if (ip_greater_than($targetsub_min, $startip) && ($targetsub_max == $endip)) {
|
353 |
|
|
break;
|
354 |
|
|
}
|
355 |
|
|
|
356 |
|
|
// Check if the subnet is between $startip and $endip
|
357 |
|
|
if (ip_greater_than($targetsub_min, $startip) && ip_less_than($targetsub_max, $endip)) {
|
358 |
|
|
break;
|
359 |
|
|
}
|
360 |
|
|
}
|
361 |
|
|
|
362 |
|
|
// Some logic that will recursivly search from $startip to the first IP before the start of the subnet we just found.
|
363 |
|
|
// NOTE: This may never be hit, the way the above algo turned out, but is left for completeness.
|
364 |
|
|
if ($startip != $targetsub_min) {
|
365 |
|
|
$rangesubnets = array_merge($rangesubnets, ip_range_to_subnet_array($startip, ip_before($targetsub_min)));
|
366 |
|
|
}
|
367 |
|
|
|
368 |
|
|
// Add in the subnet we found before, to preserve ordering
|
369 |
|
|
$rangesubnets[] = "{$targetsub_min}/{$cidr}";
|
370 |
|
|
|
371 |
|
|
// And some more logic that will search after the subnet we found to fill in to the end of the range.
|
372 |
|
|
if ($endip != $targetsub_max) {
|
373 |
|
|
$rangesubnets = array_merge($rangesubnets, ip_range_to_subnet_array(ip_after($targetsub_max), $endip));
|
374 |
|
|
}
|
375 |
|
|
return $rangesubnets;
|
376 |
|
|
}
|
377 |
|
|
|
378 |
|
|
function is_iprange($range) {
|
379 |
|
|
if (substr_count($range, '-') != 1) {
|
380 |
|
|
return false;
|
381 |
|
|
}
|
382 |
|
|
list($ip1, $ip2) = explode ('-', $range);
|
383 |
|
|
return (is_ipaddr($ip1) && is_ipaddr($ip2));
|
384 |
|
|
}
|
385 |
|
|
|
386 |
5b237745
|
Scott Ullrich
|
function is_numericint($arg) {
|
387 |
|
|
return (preg_match("/[^0-9]/", $arg) ? false : true);
|
388 |
|
|
}
|
389 |
|
|
|
390 |
|
|
/* returns true if $ipaddr is a valid dotted IPv4 address */
|
391 |
|
|
function is_ipaddr($ipaddr) {
|
392 |
|
|
if (!is_string($ipaddr))
|
393 |
|
|
return false;
|
394 |
98bbf05a
|
Scott Ullrich
|
|
395 |
5b237745
|
Scott Ullrich
|
$ip_long = ip2long($ipaddr);
|
396 |
96033063
|
Erik Fonnesbeck
|
$ip_reverse = long2ip32($ip_long);
|
397 |
98bbf05a
|
Scott Ullrich
|
|
398 |
5b237745
|
Scott Ullrich
|
if ($ipaddr == $ip_reverse)
|
399 |
|
|
return true;
|
400 |
|
|
else
|
401 |
|
|
return false;
|
402 |
|
|
}
|
403 |
|
|
|
404 |
87f0be87
|
Chris Buechler
|
/* returns true if $ipaddr is a valid dotted IPv4 address or an alias thereof */
|
405 |
5b237745
|
Scott Ullrich
|
function is_ipaddroralias($ipaddr) {
|
406 |
1e578a7f
|
Ermal Lu?i
|
global $config;
|
407 |
87f0be87
|
Chris Buechler
|
|
408 |
1e578a7f
|
Ermal Lu?i
|
if (is_alias($ipaddr)) {
|
409 |
|
|
if (is_array($config['aliases']['alias'])) {
|
410 |
|
|
foreach ($config['aliases']['alias'] as $alias) {
|
411 |
5bbd08e1
|
Warren Baker
|
if ($alias['name'] == $ipaddr && $alias['type'] != "port")
|
412 |
1e578a7f
|
Ermal Lu?i
|
return true;
|
413 |
|
|
}
|
414 |
5bbd08e1
|
Warren Baker
|
}
|
415 |
1e578a7f
|
Ermal Lu?i
|
return false;
|
416 |
|
|
} else
|
417 |
87f0be87
|
Chris Buechler
|
return is_ipaddr($ipaddr);
|
418 |
|
|
|
419 |
5b237745
|
Scott Ullrich
|
}
|
420 |
|
|
|
421 |
|
|
/* returns true if $subnet is a valid subnet in CIDR format */
|
422 |
|
|
function is_subnet($subnet) {
|
423 |
|
|
if (!is_string($subnet))
|
424 |
|
|
return false;
|
425 |
98bbf05a
|
Scott Ullrich
|
|
426 |
5b237745
|
Scott Ullrich
|
list($hp,$np) = explode('/', $subnet);
|
427 |
98bbf05a
|
Scott Ullrich
|
|
428 |
5b237745
|
Scott Ullrich
|
if (!is_ipaddr($hp))
|
429 |
|
|
return false;
|
430 |
98bbf05a
|
Scott Ullrich
|
|
431 |
5b237745
|
Scott Ullrich
|
if (!is_numeric($np) || ($np < 1) || ($np > 32))
|
432 |
|
|
return false;
|
433 |
98bbf05a
|
Scott Ullrich
|
|
434 |
5b237745
|
Scott Ullrich
|
return true;
|
435 |
|
|
}
|
436 |
|
|
|
437 |
|
|
/* returns true if $subnet is a valid subnet in CIDR format or an alias thereof */
|
438 |
|
|
function is_subnetoralias($subnet) {
|
439 |
|
|
global $aliastable;
|
440 |
98bbf05a
|
Scott Ullrich
|
|
441 |
5b237745
|
Scott Ullrich
|
if (isset($aliastable[$subnet]) && is_subnet($aliastable[$subnet]))
|
442 |
|
|
return true;
|
443 |
|
|
else
|
444 |
|
|
return is_subnet($subnet);
|
445 |
|
|
}
|
446 |
|
|
|
447 |
|
|
/* returns true if $hostname is a valid hostname */
|
448 |
|
|
function is_hostname($hostname) {
|
449 |
|
|
if (!is_string($hostname))
|
450 |
|
|
return false;
|
451 |
98bbf05a
|
Scott Ullrich
|
|
452 |
88efcf04
|
Erik Fonnesbeck
|
if (preg_match('/^(?:(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])\.)*(?:[a-z0-9_]|[a-z0-9_][a-z0-9_\-]*[a-z0-9_])$/i', $hostname))
|
453 |
5b237745
|
Scott Ullrich
|
return true;
|
454 |
|
|
else
|
455 |
|
|
return false;
|
456 |
|
|
}
|
457 |
|
|
|
458 |
|
|
/* returns true if $domain is a valid domain name */
|
459 |
|
|
function is_domain($domain) {
|
460 |
|
|
if (!is_string($domain))
|
461 |
|
|
return false;
|
462 |
98bbf05a
|
Scott Ullrich
|
|
463 |
88efcf04
|
Erik Fonnesbeck
|
if (preg_match('/^(?:(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])\.)*(?:[a-z0-9]|[a-z0-9][a-z0-9\-]*[a-z0-9])$/i', $domain))
|
464 |
5b237745
|
Scott Ullrich
|
return true;
|
465 |
|
|
else
|
466 |
|
|
return false;
|
467 |
|
|
}
|
468 |
|
|
|
469 |
|
|
/* returns true if $macaddr is a valid MAC address */
|
470 |
|
|
function is_macaddr($macaddr) {
|
471 |
56f25370
|
Erik Fonnesbeck
|
return preg_match('/^[0-9A-F]{2}(?:[:][0-9A-F]{2}){5}$/i', $macaddr) == 1 ? true : false;
|
472 |
5b237745
|
Scott Ullrich
|
}
|
473 |
|
|
|
474 |
3caa8aa1
|
Bill Marquette
|
/* returns true if $name is a valid name for an alias */
|
475 |
9499c2d2
|
Bill Marquette
|
/* returns NULL if a reserved word is used */
|
476 |
5b237745
|
Scott Ullrich
|
function is_validaliasname($name) {
|
477 |
beeef1f0
|
Bill Marquette
|
/* Array of reserved words */
|
478 |
0c2badde
|
Colin Smith
|
$reserved = array("port", "pass");
|
479 |
|
|
if (in_array($name, $reserved, true))
|
480 |
9499c2d2
|
Bill Marquette
|
return; /* return NULL */
|
481 |
bac9941b
|
jim-p
|
if (!preg_match("/[^a-zA-Z0-9_]/", $name) && (strlen($name) < 32))
|
482 |
5b237745
|
Scott Ullrich
|
return true;
|
483 |
|
|
else
|
484 |
|
|
return false;
|
485 |
|
|
}
|
486 |
|
|
|
487 |
|
|
/* returns true if $port is a valid TCP/UDP port */
|
488 |
|
|
function is_port($port) {
|
489 |
231e0606
|
Ermal Lu?i
|
$tmpports = explode(":", $port);
|
490 |
|
|
foreach($tmpports as $tmpport) {
|
491 |
|
|
if (getservbyname($tmpport, "tcp") || getservbyname($tmpport, "udp"))
|
492 |
5bbd08e1
|
Warren Baker
|
continue;
|
493 |
231e0606
|
Ermal Lu?i
|
if (!ctype_digit($tmpport))
|
494 |
|
|
return false;
|
495 |
b52f883a
|
Ermal Lu?i
|
else if ((intval($tmpport) < 1) || (intval($tmpport) > 65535))
|
496 |
231e0606
|
Ermal Lu?i
|
return false;
|
497 |
|
|
}
|
498 |
|
|
return true;
|
499 |
5b237745
|
Scott Ullrich
|
}
|
500 |
|
|
|
501 |
5a1eebc7
|
Scott Ullrich
|
/* returns true if $portrange is a valid TCP/UDP portrange ("<port>:<port>") */
|
502 |
|
|
function is_portrange($portrange) {
|
503 |
5bbd08e1
|
Warren Baker
|
$ports = explode(":", $portrange);
|
504 |
5a1eebc7
|
Scott Ullrich
|
|
505 |
5bbd08e1
|
Warren Baker
|
if(count($ports) == 2 && is_port($ports[0]) && is_port($ports[1]))
|
506 |
|
|
return true;
|
507 |
|
|
else
|
508 |
|
|
return false;
|
509 |
5a1eebc7
|
Scott Ullrich
|
}
|
510 |
|
|
|
511 |
1e578a7f
|
Ermal Lu?i
|
/* returns true if $port is a valid port number or an alias thereof */
|
512 |
|
|
function is_portoralias($port) {
|
513 |
|
|
global $config;
|
514 |
|
|
|
515 |
5bbd08e1
|
Warren Baker
|
if (is_alias($port)) {
|
516 |
|
|
if (is_array($config['aliases']['alias'])) {
|
517 |
|
|
foreach ($config['aliases']['alias'] as $alias) {
|
518 |
|
|
if ($alias['name'] == $port && $alias['type'] == "port")
|
519 |
|
|
return true;
|
520 |
|
|
}
|
521 |
|
|
}
|
522 |
|
|
return false;
|
523 |
|
|
} else
|
524 |
|
|
return is_port($port);
|
525 |
1e578a7f
|
Ermal Lu?i
|
}
|
526 |
|
|
|
527 |
b8014f9d
|
Scott Ullrich
|
/* returns true if $val is a valid shaper bandwidth value */
|
528 |
|
|
function is_valid_shaperbw($val) {
|
529 |
eaa37259
|
Ermal Luçi
|
return (preg_match("/^(\d+(?:\.\d+)?)([MKG]?b|%)$/", $val));
|
530 |
b8014f9d
|
Scott Ullrich
|
}
|
531 |
|
|
|
532 |
abcb2bed
|
Ermal Lu?i
|
/* return the configured carp interface list */
|
533 |
|
|
function get_configured_carp_interface_list() {
|
534 |
|
|
global $config;
|
535 |
|
|
|
536 |
|
|
$iflist = array();
|
537 |
|
|
|
538 |
|
|
if(is_array($config['virtualip']['vip'])) {
|
539 |
5bbd08e1
|
Warren Baker
|
$viparr = &$config['virtualip']['vip'];
|
540 |
|
|
foreach ($viparr as $vip) {
|
541 |
|
|
switch ($vip['mode']) {
|
542 |
|
|
case "carp":
|
543 |
|
|
case "carpdev-dhcp":
|
544 |
|
|
$vipif = "vip" . $vip['vhid'];
|
545 |
|
|
$iflist[$vipif] = $vip['subnet'];
|
546 |
|
|
break;
|
547 |
|
|
}
|
548 |
|
|
}
|
549 |
|
|
}
|
550 |
abcb2bed
|
Ermal Lu?i
|
|
551 |
|
|
return $iflist;
|
552 |
|
|
}
|
553 |
|
|
|
554 |
67b0902f
|
pierrepomes
|
/* return the configured IP aliases list */
|
555 |
|
|
function get_configured_ip_aliases_list() {
|
556 |
5bbd08e1
|
Warren Baker
|
global $config;
|
557 |
67b0902f
|
pierrepomes
|
|
558 |
5bbd08e1
|
Warren Baker
|
$alias_list=array();
|
559 |
67b0902f
|
pierrepomes
|
|
560 |
5bbd08e1
|
Warren Baker
|
if(is_array($config['virtualip']['vip'])) {
|
561 |
|
|
$viparr = &$config['virtualip']['vip'];
|
562 |
|
|
foreach ($viparr as $vip) {
|
563 |
|
|
if ($vip['mode']=="ipalias") {
|
564 |
|
|
$alias_list[$vip['subnet']] = $vip['interface'];
|
565 |
|
|
}
|
566 |
|
|
}
|
567 |
|
|
}
|
568 |
67b0902f
|
pierrepomes
|
|
569 |
5bbd08e1
|
Warren Baker
|
return $alias_list;
|
570 |
67b0902f
|
pierrepomes
|
}
|
571 |
|
|
|
572 |
|
|
|
573 |
88bc2760
|
Erik Fonnesbeck
|
/* comparison function for sorting by the order in which interfaces are normally created */
|
574 |
|
|
function compare_interface_friendly_names($a, $b) {
|
575 |
|
|
if ($a == $b)
|
576 |
|
|
return 0;
|
577 |
|
|
else if ($a == 'wan')
|
578 |
|
|
return -1;
|
579 |
|
|
else if ($b == 'wan')
|
580 |
|
|
return 1;
|
581 |
|
|
else if ($a == 'lan')
|
582 |
|
|
return -1;
|
583 |
|
|
else if ($b == 'lan')
|
584 |
|
|
return 1;
|
585 |
|
|
|
586 |
|
|
return strnatcmp($a, $b);
|
587 |
|
|
}
|
588 |
|
|
|
589 |
c8abe1d4
|
Ermal Luçi
|
/* return the configured interfaces list. */
|
590 |
3ad5e089
|
Ermal Luçi
|
function get_configured_interface_list($only_opt = false, $withdisabled = false) {
|
591 |
c8abe1d4
|
Ermal Luçi
|
global $config;
|
592 |
|
|
|
593 |
|
|
$iflist = array();
|
594 |
14f49fd0
|
Erik Fonnesbeck
|
|
595 |
263aeb4b
|
Scott Ullrich
|
if(!is_array($config['interfaces']))
|
596 |
94436824
|
Scott Ullrich
|
$config = parse_config(true);
|
597 |
|
|
|
598 |
c8abe1d4
|
Ermal Luçi
|
/* if list */
|
599 |
8735afe8
|
Erik Fonnesbeck
|
foreach($config['interfaces'] as $if => $ifdetail) {
|
600 |
|
|
if ($only_opt && ($if == "wan" || $if == "lan"))
|
601 |
42c9d20e
|
Ermal Luçi
|
continue;
|
602 |
47c8b036
|
Ermal Lu?i
|
if (isset($ifdetail['enable']) || $withdisabled == true)
|
603 |
c8abe1d4
|
Ermal Luçi
|
$iflist[$if] = $if;
|
604 |
42c9d20e
|
Ermal Luçi
|
}
|
605 |
c8abe1d4
|
Ermal Luçi
|
|
606 |
|
|
return $iflist;
|
607 |
|
|
}
|
608 |
|
|
|
609 |
bb34737f
|
Ermal Lu?i
|
/* return the configured interfaces list. */
|
610 |
|
|
function get_configured_interface_list_by_realif($only_opt = false, $withdisabled = false) {
|
611 |
8735afe8
|
Erik Fonnesbeck
|
global $config;
|
612 |
bb34737f
|
Ermal Lu?i
|
|
613 |
8735afe8
|
Erik Fonnesbeck
|
$iflist = array();
|
614 |
bb34737f
|
Ermal Lu?i
|
|
615 |
8735afe8
|
Erik Fonnesbeck
|
/* if list */
|
616 |
|
|
foreach($config['interfaces'] as $if => $ifdetail) {
|
617 |
|
|
if ($only_opt && ($if == "wan" || $if == "lan"))
|
618 |
|
|
continue;
|
619 |
|
|
if (isset($ifdetail['enable']) || $withdisabled == true) {
|
620 |
bb34737f
|
Ermal Lu?i
|
$tmpif = get_real_interface($if);
|
621 |
|
|
if (!empty($tmpif))
|
622 |
|
|
$iflist[$tmpif] = $if;
|
623 |
|
|
}
|
624 |
8735afe8
|
Erik Fonnesbeck
|
}
|
625 |
bb34737f
|
Ermal Lu?i
|
|
626 |
8735afe8
|
Erik Fonnesbeck
|
return $iflist;
|
627 |
bb34737f
|
Ermal Lu?i
|
}
|
628 |
|
|
|
629 |
c8abe1d4
|
Ermal Luçi
|
/* return the configured interfaces list with their description. */
|
630 |
3ad5e089
|
Ermal Luçi
|
function get_configured_interface_with_descr($only_opt = false, $withdisabled = false) {
|
631 |
a42d1da2
|
Scott Ullrich
|
global $config;
|
632 |
c8abe1d4
|
Ermal Luçi
|
|
633 |
a42d1da2
|
Scott Ullrich
|
$iflist = array();
|
634 |
c8abe1d4
|
Ermal Luçi
|
|
635 |
a42d1da2
|
Scott Ullrich
|
/* if list */
|
636 |
|
|
foreach($config['interfaces'] as $if => $ifdetail) {
|
637 |
8735afe8
|
Erik Fonnesbeck
|
if ($only_opt && ($if == "wan" || $if == "lan"))
|
638 |
|
|
continue;
|
639 |
47c8b036
|
Ermal Lu?i
|
if (isset($ifdetail['enable']) || $withdisabled == true) {
|
640 |
8735afe8
|
Erik Fonnesbeck
|
if(empty($ifdetail['descr']))
|
641 |
8e74cb8d
|
Ermal Luçi
|
$iflist[$if] = strtoupper($if);
|
642 |
a42d1da2
|
Scott Ullrich
|
else
|
643 |
44b0ec83
|
Scott Ullrich
|
$iflist[$if] = strtoupper($ifdetail['descr']);
|
644 |
0e218dc1
|
Ermal Luçi
|
}
|
645 |
42c9d20e
|
Ermal Luçi
|
}
|
646 |
c8abe1d4
|
Ermal Luçi
|
|
647 |
a42d1da2
|
Scott Ullrich
|
return $iflist;
|
648 |
c8abe1d4
|
Ermal Luçi
|
}
|
649 |
|
|
|
650 |
4fe9c2dc
|
Scott Ullrich
|
/*
|
651 |
|
|
* get_configured_ip_addresses() - Return a list of all configured
|
652 |
|
|
* interfaces IP Addresses
|
653 |
|
|
*
|
654 |
|
|
*/
|
655 |
|
|
function get_configured_ip_addresses() {
|
656 |
|
|
require_once("interfaces.inc");
|
657 |
|
|
$ip_array = array();
|
658 |
|
|
$interfaces = get_configured_interface_list();
|
659 |
d9114ce0
|
Scott Ullrich
|
if(is_array($interfaces)) {
|
660 |
|
|
foreach($interfaces as $int) {
|
661 |
|
|
$ipaddr = get_interface_ip($int);
|
662 |
|
|
$ip_array[$int] = $ipaddr;
|
663 |
|
|
}
|
664 |
4fe9c2dc
|
Scott Ullrich
|
}
|
665 |
19f101d7
|
Scott Ullrich
|
$interfaces = get_configured_carp_interface_list();
|
666 |
d9114ce0
|
Scott Ullrich
|
if(is_array($interfaces))
|
667 |
|
|
foreach($interfaces as $int => $ipaddr)
|
668 |
|
|
$ip_array[$int] = $ipaddr;
|
669 |
4fe9c2dc
|
Scott Ullrich
|
return $ip_array;
|
670 |
|
|
}
|
671 |
c8abe1d4
|
Ermal Luçi
|
|
672 |
36f546e9
|
Scott Ullrich
|
/*
|
673 |
|
|
* get_interface_list() - Return a list of all physical interfaces
|
674 |
|
|
* along with MAC and status.
|
675 |
|
|
*
|
676 |
|
|
* $mode = "active" - use ifconfig -lu
|
677 |
|
|
* "media" - use ifconfig to check physical connection
|
678 |
|
|
* status (much slower)
|
679 |
|
|
*/
|
680 |
|
|
function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") {
|
681 |
20203646
|
Colin Smith
|
global $config;
|
682 |
65bed2d2
|
Scott Ullrich
|
$upints = array();
|
683 |
20203646
|
Colin Smith
|
/* get a list of virtual interface types */
|
684 |
36f546e9
|
Scott Ullrich
|
if(!$vfaces) {
|
685 |
9ce38409
|
Scott Ullrich
|
$vfaces = array (
|
686 |
|
|
'bridge',
|
687 |
|
|
'ppp',
|
688 |
27c0c7c6
|
Ermal Lu?i
|
'pppoe',
|
689 |
|
|
'pptp',
|
690 |
|
|
'l2tp',
|
691 |
9ce38409
|
Scott Ullrich
|
'sl',
|
692 |
|
|
'gif',
|
693 |
613571ea
|
Ermal Luçi
|
'gre',
|
694 |
9ce38409
|
Scott Ullrich
|
'faith',
|
695 |
|
|
'lo',
|
696 |
|
|
'ng',
|
697 |
27616d6e
|
Seth Mos
|
'_vlan',
|
698 |
7c53bc7b
|
Erik Fonnesbeck
|
'_wlan',
|
699 |
9ce38409
|
Scott Ullrich
|
'pflog',
|
700 |
a42d1da2
|
Scott Ullrich
|
'plip',
|
701 |
9ce38409
|
Scott Ullrich
|
'pfsync',
|
702 |
|
|
'enc',
|
703 |
|
|
'tun',
|
704 |
0a140d2e
|
Ermal Luçi
|
'carp',
|
705 |
1fb2bf25
|
Ermal Lu?i
|
'lagg',
|
706 |
1fd35e95
|
Ermal
|
'vip',
|
707 |
|
|
'ipfw'
|
708 |
9ce38409
|
Scott Ullrich
|
);
|
709 |
36f546e9
|
Scott Ullrich
|
}
|
710 |
20203646
|
Colin Smith
|
switch($mode) {
|
711 |
|
|
case "active":
|
712 |
|
|
$upints = explode(" ", trim(shell_exec("/sbin/ifconfig -lu")));
|
713 |
|
|
break;
|
714 |
|
|
case "media":
|
715 |
|
|
$intlist = explode(" ", trim(shell_exec("/sbin/ifconfig -l")));
|
716 |
767a716e
|
Scott Ullrich
|
$ifconfig = "";
|
717 |
20203646
|
Colin Smith
|
exec("/sbin/ifconfig -a", $ifconfig);
|
718 |
|
|
$regexp = '/(' . implode('|', $intlist) . '):\s/';
|
719 |
|
|
$ifstatus = preg_grep('/status:/', $ifconfig);
|
720 |
49149b86
|
Colin Smith
|
foreach($ifstatus as $status) {
|
721 |
bb3b9159
|
Colin Smith
|
$int = array_shift($intlist);
|
722 |
5bbd08e1
|
Warren Baker
|
if(stristr($status, "active")) $upints[] = $int;
|
723 |
49149b86
|
Colin Smith
|
}
|
724 |
20203646
|
Colin Smith
|
break;
|
725 |
72993196
|
Ermal
|
default:
|
726 |
|
|
$upints = explode(" ", trim(shell_exec("/sbin/ifconfig -l")));
|
727 |
|
|
break;
|
728 |
20203646
|
Colin Smith
|
}
|
729 |
|
|
/* build interface list with netstat */
|
730 |
767a716e
|
Scott Ullrich
|
$linkinfo = "";
|
731 |
89d1f0f2
|
Scott Ullrich
|
exec("/usr/bin/netstat -inW -f link | awk '{ print $1, $4 }'", $linkinfo);
|
732 |
20203646
|
Colin Smith
|
array_shift($linkinfo);
|
733 |
89d1f0f2
|
Scott Ullrich
|
/* build ip address list with netstat */
|
734 |
767a716e
|
Scott Ullrich
|
$ipinfo = "";
|
735 |
89d1f0f2
|
Scott Ullrich
|
exec("/usr/bin/netstat -inW -f inet | awk '{ print $1, $4 }'", $ipinfo);
|
736 |
|
|
array_shift($ipinfo);
|
737 |
|
|
foreach($linkinfo as $link) {
|
738 |
|
|
$friendly = "";
|
739 |
5bbd08e1
|
Warren Baker
|
$alink = explode(" ", $link);
|
740 |
|
|
$ifname = rtrim(trim($alink[0]), '*');
|
741 |
|
|
/* trim out all numbers before checking for vfaces */
|
742 |
494be6e8
|
Ermal Lu?i
|
if (!in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces) &&
|
743 |
7c53bc7b
|
Erik Fonnesbeck
|
!stristr($ifname, "_vlan") && !stristr($ifname, "_wlan")) {
|
744 |
20203646
|
Colin Smith
|
$toput = array(
|
745 |
|
|
"mac" => trim($alink[1]),
|
746 |
|
|
"up" => in_array($ifname, $upints)
|
747 |
|
|
);
|
748 |
89d1f0f2
|
Scott Ullrich
|
foreach($ipinfo as $ip) {
|
749 |
|
|
$aip = explode(" ", $ip);
|
750 |
|
|
if($aip[0] == $ifname) {
|
751 |
|
|
$toput['ipaddr'] = $aip[1];
|
752 |
|
|
}
|
753 |
|
|
}
|
754 |
72993196
|
Ermal
|
if (is_array($config['interfaces'])) {
|
755 |
|
|
foreach($config['interfaces'] as $name => $int)
|
756 |
|
|
if($int['if'] == $ifname) $friendly = $name;
|
757 |
20203646
|
Colin Smith
|
}
|
758 |
|
|
switch($keyby) {
|
759 |
|
|
case "physical":
|
760 |
89d1f0f2
|
Scott Ullrich
|
if($friendly != "") {
|
761 |
|
|
$toput['friendly'] = $friendly;
|
762 |
|
|
}
|
763 |
a296c95d
|
Seth Mos
|
$dmesg_arr = array();
|
764 |
|
|
exec("/sbin/dmesg |grep $ifname | head -n1", $dmesg_arr);
|
765 |
|
|
preg_match_all("/<(.*?)>/i", $dmesg_arr[0], $dmesg);
|
766 |
|
|
$toput['dmesg'] = $dmesg[1][0];
|
767 |
20203646
|
Colin Smith
|
$iflist[$ifname] = $toput;
|
768 |
3154d7ed
|
Colin Smith
|
break;
|
769 |
4aca19b3
|
Scott Ullrich
|
case "ppp":
|
770 |
|
|
|
771 |
20203646
|
Colin Smith
|
case "friendly":
|
772 |
89d1f0f2
|
Scott Ullrich
|
if($friendly != "") {
|
773 |
|
|
$toput['if'] = $ifname;
|
774 |
|
|
$iflist[$friendly] = $toput;
|
775 |
|
|
}
|
776 |
3154d7ed
|
Colin Smith
|
break;
|
777 |
|
|
}
|
778 |
5bbd08e1
|
Warren Baker
|
}
|
779 |
|
|
}
|
780 |
|
|
return $iflist;
|
781 |
5b237745
|
Scott Ullrich
|
}
|
782 |
|
|
|
783 |
2b4d37de
|
Ermal Lu?i
|
/****f* util/log_error
|
784 |
|
|
* NAME
|
785 |
|
|
* log_error - Sends a string to syslog.
|
786 |
|
|
* INPUTS
|
787 |
|
|
* $error - string containing the syslog message.
|
788 |
|
|
* RESULT
|
789 |
|
|
* null
|
790 |
|
|
******/
|
791 |
|
|
function log_error($error) {
|
792 |
5bbd08e1
|
Warren Baker
|
global $g;
|
793 |
|
|
$page = $_SERVER['SCRIPT_NAME'];
|
794 |
|
|
syslog(LOG_WARNING, "$page: $error");
|
795 |
|
|
if ($g['debug'])
|
796 |
|
|
syslog(LOG_WARNING, var_dump(debug_backtrace()));
|
797 |
|
|
return;
|
798 |
2b4d37de
|
Ermal Lu?i
|
}
|
799 |
|
|
|
800 |
3aba1835
|
Scott Ullrich
|
/****f* util/log_auth
|
801 |
|
|
* NAME
|
802 |
|
|
* log_error - Sends a string to syslog as LOG_AUTH facility
|
803 |
|
|
* INPUTS
|
804 |
|
|
* $error - string containing the syslog message.
|
805 |
|
|
* RESULT
|
806 |
|
|
* null
|
807 |
|
|
******/
|
808 |
|
|
function log_auth($error) {
|
809 |
5bbd08e1
|
Warren Baker
|
global $g;
|
810 |
|
|
$page = $_SERVER['SCRIPT_NAME'];
|
811 |
|
|
syslog(LOG_AUTH, "$page: $error");
|
812 |
|
|
if ($g['debug'])
|
813 |
|
|
syslog(LOG_WARNING, var_dump(debug_backtrace()));
|
814 |
|
|
return;
|
815 |
3aba1835
|
Scott Ullrich
|
}
|
816 |
|
|
|
817 |
83bc3749
|
Ermal Lu?i
|
/****f* util/exec_command
|
818 |
|
|
* NAME
|
819 |
|
|
* exec_command - Execute a command and return a string of the result.
|
820 |
|
|
* INPUTS
|
821 |
|
|
* $command - String of the command to be executed.
|
822 |
|
|
* RESULT
|
823 |
|
|
* String containing the command's result.
|
824 |
|
|
* NOTES
|
825 |
|
|
* This function returns the command's stdout and stderr.
|
826 |
|
|
******/
|
827 |
|
|
function exec_command($command) {
|
828 |
5bbd08e1
|
Warren Baker
|
$output = array();
|
829 |
|
|
exec($command . ' 2>&1 ', $output);
|
830 |
|
|
return(implode("\n", $output));
|
831 |
83bc3749
|
Ermal Lu?i
|
}
|
832 |
|
|
|
833 |
5b237745
|
Scott Ullrich
|
/* wrapper for exec() */
|
834 |
12169c92
|
Seth Mos
|
function mwexec($command, $mute = false) {
|
835 |
5b237745
|
Scott Ullrich
|
global $g;
|
836 |
435a418f
|
Ermal
|
|
837 |
5b237745
|
Scott Ullrich
|
if ($g['debug']) {
|
838 |
|
|
if (!$_SERVER['REMOTE_ADDR'])
|
839 |
|
|
echo "mwexec(): $command\n";
|
840 |
f9db3cda
|
Seth Mos
|
}
|
841 |
435a418f
|
Ermal
|
$oarr = array();
|
842 |
|
|
$retval = 0;
|
843 |
|
|
$garbage = exec("$command 2>&1", $oarr, $retval);
|
844 |
|
|
|
845 |
61c6b6c1
|
Chris Buechler
|
if(isset($config['system']['developerspew']))
|
846 |
5bbd08e1
|
Warren Baker
|
$mute = false;
|
847 |
12169c92
|
Seth Mos
|
if(($retval <> 0) && ($mute === false)) {
|
848 |
f9db3cda
|
Seth Mos
|
$output = implode(" ", $oarr);
|
849 |
4cc0d38a
|
Seth Mos
|
log_error("The command '$command' returned exit code '$retval', the output was '$output' ");
|
850 |
5b237745
|
Scott Ullrich
|
}
|
851 |
98bbf05a
|
Scott Ullrich
|
return $retval;
|
852 |
5b237745
|
Scott Ullrich
|
}
|
853 |
|
|
|
854 |
|
|
/* wrapper for exec() in background */
|
855 |
|
|
function mwexec_bg($command) {
|
856 |
|
|
global $g;
|
857 |
98bbf05a
|
Scott Ullrich
|
|
858 |
5b237745
|
Scott Ullrich
|
if ($g['debug']) {
|
859 |
|
|
if (!$_SERVER['REMOTE_ADDR'])
|
860 |
|
|
echo "mwexec(): $command\n";
|
861 |
|
|
}
|
862 |
98bbf05a
|
Scott Ullrich
|
|
863 |
5b237745
|
Scott Ullrich
|
exec("nohup $command > /dev/null 2>&1 &");
|
864 |
|
|
}
|
865 |
|
|
|
866 |
|
|
/* unlink a file, if it exists */
|
867 |
|
|
function unlink_if_exists($fn) {
|
868 |
336cb718
|
Scott Ullrich
|
$to_do = glob($fn);
|
869 |
3b378be5
|
Scott Ullrich
|
if(is_array($to_do)) {
|
870 |
336cb718
|
Scott Ullrich
|
foreach($to_do as $filename)
|
871 |
9ff926a2
|
Colin Smith
|
@unlink($filename);
|
872 |
336cb718
|
Scott Ullrich
|
} else {
|
873 |
9ff926a2
|
Colin Smith
|
@unlink($fn);
|
874 |
336cb718
|
Scott Ullrich
|
}
|
875 |
5b237745
|
Scott Ullrich
|
}
|
876 |
|
|
/* make a global alias table (for faster lookups) */
|
877 |
918a884d
|
Bill Marquette
|
function alias_make_table($config) {
|
878 |
|
|
global $aliastable;
|
879 |
98bbf05a
|
Scott Ullrich
|
|
880 |
5b237745
|
Scott Ullrich
|
$aliastable = array();
|
881 |
98bbf05a
|
Scott Ullrich
|
|
882 |
5b237745
|
Scott Ullrich
|
if (is_array($config['aliases']['alias'])) {
|
883 |
|
|
foreach ($config['aliases']['alias'] as $alias) {
|
884 |
|
|
if ($alias['name'])
|
885 |
|
|
$aliastable[$alias['name']] = $alias['address'];
|
886 |
|
|
}
|
887 |
|
|
}
|
888 |
|
|
}
|
889 |
|
|
/* check if an alias exists */
|
890 |
|
|
function is_alias($name) {
|
891 |
|
|
global $aliastable;
|
892 |
98bbf05a
|
Scott Ullrich
|
|
893 |
5b237745
|
Scott Ullrich
|
return isset($aliastable[$name]);
|
894 |
b8014f9d
|
Scott Ullrich
|
}
|
895 |
27ff8a3c
|
Scott Ullrich
|
|
896 |
5b237745
|
Scott Ullrich
|
/* expand a host or network alias, if necessary */
|
897 |
|
|
function alias_expand($name) {
|
898 |
|
|
global $aliastable;
|
899 |
98bbf05a
|
Scott Ullrich
|
|
900 |
87f0be87
|
Chris Buechler
|
if (isset($aliastable[$name]))
|
901 |
4335dc87
|
Bill Marquette
|
return "\${$name}";
|
902 |
a584475a
|
Ermal Lu?i
|
else if (is_ipaddr($name) || is_subnet($name) || is_port($name))
|
903 |
57989da5
|
Scott Ullrich
|
return "{$name}";
|
904 |
87f0be87
|
Chris Buechler
|
else
|
905 |
5b237745
|
Scott Ullrich
|
return null;
|
906 |
|
|
}
|
907 |
|
|
|
908 |
c7de8be4
|
jim-p
|
function alias_expand_urltable($name) {
|
909 |
|
|
global $config;
|
910 |
|
|
$urltable_prefix = "/var/db/aliastables/";
|
911 |
|
|
$urltable_filename = $urltable_prefix . $name . ".txt";
|
912 |
|
|
|
913 |
df58fd46
|
Ermal
|
foreach ($config['aliases']['alias'] as $alias) {
|
914 |
|
|
if (($alias['type'] == 'urltable') && ($alias['name'] == $name)) {
|
915 |
|
|
if (is_URL($alias["url"]) && file_exists($urltable_filename) && filesize($urltable_filename))
|
916 |
|
|
return $urltable_filename;
|
917 |
|
|
else if (process_alias_urltable($name, $alias["url"], 0, true))
|
918 |
|
|
return $urltable_filename;
|
919 |
c7de8be4
|
jim-p
|
}
|
920 |
|
|
}
|
921 |
|
|
return null;
|
922 |
|
|
}
|
923 |
|
|
|
924 |
5b237745
|
Scott Ullrich
|
/* find out whether two subnets overlap */
|
925 |
|
|
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
|
926 |
|
|
|
927 |
|
|
if (!is_numeric($bits1))
|
928 |
|
|
$bits1 = 32;
|
929 |
|
|
if (!is_numeric($bits2))
|
930 |
|
|
$bits2 = 32;
|
931 |
|
|
|
932 |
|
|
if ($bits1 < $bits2)
|
933 |
|
|
$relbits = $bits1;
|
934 |
|
|
else
|
935 |
|
|
$relbits = $bits2;
|
936 |
98bbf05a
|
Scott Ullrich
|
|
937 |
5b237745
|
Scott Ullrich
|
$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
|
938 |
|
|
$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
|
939 |
98bbf05a
|
Scott Ullrich
|
|
940 |
5b237745
|
Scott Ullrich
|
if ($sn1 == $sn2)
|
941 |
|
|
return true;
|
942 |
|
|
else
|
943 |
|
|
return false;
|
944 |
|
|
}
|
945 |
|
|
|
946 |
|
|
/* compare two IP addresses */
|
947 |
|
|
function ipcmp($a, $b) {
|
948 |
96033063
|
Erik Fonnesbeck
|
if (ip_less_than($a, $b))
|
949 |
5b237745
|
Scott Ullrich
|
return -1;
|
950 |
96033063
|
Erik Fonnesbeck
|
else if (ip_greater_than($a, $b))
|
951 |
5b237745
|
Scott Ullrich
|
return 1;
|
952 |
|
|
else
|
953 |
|
|
return 0;
|
954 |
|
|
}
|
955 |
|
|
|
956 |
|
|
/* return true if $addr is in $subnet, false if not */
|
957 |
|
|
function ip_in_subnet($addr,$subnet) {
|
958 |
|
|
list($ip, $mask) = explode('/', $subnet);
|
959 |
96033063
|
Erik Fonnesbeck
|
$mask = (0xffffffff << (32 - $mask)) & 0xffffffff;
|
960 |
5b237745
|
Scott Ullrich
|
return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
|
961 |
|
|
}
|
962 |
|
|
|
963 |
|
|
/* verify (and remove) the digital signature on a file - returns 0 if OK */
|
964 |
|
|
function verify_digital_signature($fname) {
|
965 |
|
|
global $g;
|
966 |
|
|
|
967 |
c50da179
|
Scott Ullrich
|
if(!file_exists("/usr/local/sbin/gzsig"))
|
968 |
9f007e8c
|
Chris Buechler
|
return 4;
|
969 |
c50da179
|
Scott Ullrich
|
|
970 |
f024f52d
|
Scott Ullrich
|
return mwexec("/usr/local/sbin/gzsig verify {$g['etc_path']}/pubkey.pem < " . escapeshellarg($fname));
|
971 |
5b237745
|
Scott Ullrich
|
}
|
972 |
|
|
|
973 |
|
|
/* obtain MAC address given an IP address by looking at the ARP table */
|
974 |
|
|
function arp_get_mac_by_ip($ip) {
|
975 |
f3ebffee
|
Ermal Lu?i
|
mwexec("/sbin/ping -c 1 -t 1 {$ip}", true);
|
976 |
767a716e
|
Scott Ullrich
|
$arpoutput = "";
|
977 |
5b237745
|
Scott Ullrich
|
exec("/usr/sbin/arp -n {$ip}", $arpoutput);
|
978 |
98bbf05a
|
Scott Ullrich
|
|
979 |
5b237745
|
Scott Ullrich
|
if ($arpoutput[0]) {
|
980 |
|
|
$arpi = explode(" ", $arpoutput[0]);
|
981 |
|
|
$macaddr = $arpi[3];
|
982 |
|
|
if (is_macaddr($macaddr))
|
983 |
|
|
return $macaddr;
|
984 |
|
|
else
|
985 |
|
|
return false;
|
986 |
|
|
}
|
987 |
98bbf05a
|
Scott Ullrich
|
|
988 |
5b237745
|
Scott Ullrich
|
return false;
|
989 |
|
|
}
|
990 |
|
|
|
991 |
98bbf05a
|
Scott Ullrich
|
/* return a fieldname that is safe for xml usage */
|
992 |
|
|
function xml_safe_fieldname($fieldname) {
|
993 |
87f0be87
|
Chris Buechler
|
$replace = array('/', '-', ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
|
994 |
|
|
'_', '+', '=', '{', '}', '[', ']', '|', '/', '<', '>', '?',
|
995 |
ddce8ef2
|
Colin Smith
|
':', ',', '.', '\'', '\\'
|
996 |
|
|
);
|
997 |
|
|
return strtolower(str_replace($replace, "", $fieldname));
|
998 |
98bbf05a
|
Scott Ullrich
|
}
|
999 |
|
|
|
1000 |
4129df39
|
Scott Ullrich
|
function mac_format($clientmac) {
|
1001 |
|
|
$mac =explode(":", $clientmac);
|
1002 |
|
|
|
1003 |
|
|
global $config;
|
1004 |
|
|
|
1005 |
|
|
$mac_format = $config['captiveportal']['radmac_format'] ? $config['captiveportal']['radmac_format'] : false;
|
1006 |
|
|
|
1007 |
|
|
switch($mac_format) {
|
1008 |
|
|
|
1009 |
|
|
case 'singledash':
|
1010 |
|
|
return "$mac[0]$mac[1]$mac[2]-$mac[3]$mac[4]$mac[5]";
|
1011 |
|
|
|
1012 |
|
|
case 'ietf':
|
1013 |
|
|
return "$mac[0]-$mac[1]-$mac[2]-$mac[3]-$mac[4]-$mac[5]";
|
1014 |
|
|
|
1015 |
|
|
case 'cisco':
|
1016 |
|
|
return "$mac[0]$mac[1].$mac[2]$mac[3].$mac[4]$mac[5]";
|
1017 |
|
|
|
1018 |
|
|
case 'unformatted':
|
1019 |
|
|
return "$mac[0]$mac[1]$mac[2]$mac[3]$mac[4]$mac[5]";
|
1020 |
|
|
|
1021 |
|
|
default:
|
1022 |
|
|
return $clientmac;
|
1023 |
|
|
}
|
1024 |
|
|
}
|
1025 |
|
|
|
1026 |
979cd6db
|
Scott Ullrich
|
function resolve_retry($hostname, $retries = 5) {
|
1027 |
|
|
|
1028 |
5bbd08e1
|
Warren Baker
|
if (is_ipaddr($hostname))
|
1029 |
|
|
return $hostname;
|
1030 |
979cd6db
|
Scott Ullrich
|
|
1031 |
5bbd08e1
|
Warren Baker
|
for ($i = 0; $i < $retries; $i++) {
|
1032 |
|
|
$ip = gethostbyname($hostname);
|
1033 |
979cd6db
|
Scott Ullrich
|
|
1034 |
5bbd08e1
|
Warren Baker
|
if ($ip && $ip != $hostname) {
|
1035 |
|
|
/* success */
|
1036 |
|
|
return $ip;
|
1037 |
|
|
}
|
1038 |
979cd6db
|
Scott Ullrich
|
|
1039 |
5bbd08e1
|
Warren Baker
|
sleep(1);
|
1040 |
|
|
}
|
1041 |
979cd6db
|
Scott Ullrich
|
|
1042 |
5bbd08e1
|
Warren Baker
|
return false;
|
1043 |
979cd6db
|
Scott Ullrich
|
}
|
1044 |
|
|
|
1045 |
44bfd1fa
|
Scott Ullrich
|
function format_bytes($bytes) {
|
1046 |
|
|
if ($bytes >= 1073741824) {
|
1047 |
|
|
return sprintf("%.2f GB", $bytes/1073741824);
|
1048 |
|
|
} else if ($bytes >= 1048576) {
|
1049 |
|
|
return sprintf("%.2f MB", $bytes/1048576);
|
1050 |
|
|
} else if ($bytes >= 1024) {
|
1051 |
|
|
return sprintf("%.0f KB", $bytes/1024);
|
1052 |
|
|
} else {
|
1053 |
|
|
return sprintf("%d bytes", $bytes);
|
1054 |
|
|
}
|
1055 |
|
|
}
|
1056 |
|
|
|
1057 |
2b4d37de
|
Ermal Lu?i
|
function update_filter_reload_status($text) {
|
1058 |
5bbd08e1
|
Warren Baker
|
global $g;
|
1059 |
2b4d37de
|
Ermal Lu?i
|
|
1060 |
5bbd08e1
|
Warren Baker
|
file_put_contents("{$g['varrun_path']}/filter_reload_status", $text);
|
1061 |
2b4d37de
|
Ermal Lu?i
|
}
|
1062 |
|
|
|
1063 |
|
|
/****f* util/return_dir_as_array
|
1064 |
|
|
* NAME
|
1065 |
|
|
* return_dir_as_array - Return a directory's contents as an array.
|
1066 |
|
|
* INPUTS
|
1067 |
|
|
* $dir - string containing the path to the desired directory.
|
1068 |
|
|
* RESULT
|
1069 |
|
|
* $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
|
1070 |
|
|
******/
|
1071 |
|
|
function return_dir_as_array($dir) {
|
1072 |
5bbd08e1
|
Warren Baker
|
$dir_array = array();
|
1073 |
|
|
if (is_dir($dir)) {
|
1074 |
|
|
if ($dh = opendir($dir)) {
|
1075 |
|
|
while (($file = readdir($dh)) !== false) {
|
1076 |
|
|
$canadd = 0;
|
1077 |
|
|
if($file == ".") $canadd = 1;
|
1078 |
|
|
if($file == "..") $canadd = 1;
|
1079 |
|
|
if($canadd == 0)
|
1080 |
|
|
array_push($dir_array, $file);
|
1081 |
|
|
}
|
1082 |
|
|
closedir($dh);
|
1083 |
|
|
}
|
1084 |
|
|
}
|
1085 |
|
|
return $dir_array;
|
1086 |
2b4d37de
|
Ermal Lu?i
|
}
|
1087 |
|
|
|
1088 |
|
|
function run_plugins($directory) {
|
1089 |
5bbd08e1
|
Warren Baker
|
global $config, $g;
|
1090 |
|
|
|
1091 |
|
|
/* process packager manager custom rules */
|
1092 |
|
|
$files = return_dir_as_array($directory);
|
1093 |
|
|
if (is_array($files)) {
|
1094 |
|
|
foreach ($files as $file) {
|
1095 |
|
|
if (stristr($file, ".sh") == true)
|
1096 |
|
|
mwexec($directory . $file . " start");
|
1097 |
|
|
else if (!is_dir($directory . "/" . $file) && stristr($file,".inc"))
|
1098 |
|
|
require_once($directory . "/" . $file);
|
1099 |
2990acf8
|
Scott Ullrich
|
}
|
1100 |
5bbd08e1
|
Warren Baker
|
}
|
1101 |
2b4d37de
|
Ermal Lu?i
|
}
|
1102 |
|
|
|
1103 |
|
|
/*
|
1104 |
|
|
* safe_mkdir($path, $mode = 0755)
|
1105 |
|
|
* create directory if it doesn't already exist and isn't a file!
|
1106 |
|
|
*/
|
1107 |
|
|
function safe_mkdir($path, $mode=0755) {
|
1108 |
5bbd08e1
|
Warren Baker
|
global $g;
|
1109 |
2b4d37de
|
Ermal Lu?i
|
|
1110 |
5bbd08e1
|
Warren Baker
|
if (!is_file($path) && !is_dir($path)) {
|
1111 |
|
|
return @mkdir($path, $mode, true);
|
1112 |
|
|
} else {
|
1113 |
|
|
return false;
|
1114 |
|
|
}
|
1115 |
2b4d37de
|
Ermal Lu?i
|
}
|
1116 |
|
|
|
1117 |
|
|
/*
|
1118 |
|
|
* make_dirs($path, $mode = 0755)
|
1119 |
|
|
* create directory tree recursively (mkdir -p)
|
1120 |
|
|
*/
|
1121 |
|
|
function make_dirs($path, $mode = 0755) {
|
1122 |
5bbd08e1
|
Warren Baker
|
$base = '';
|
1123 |
|
|
foreach (explode('/', $path) as $dir) {
|
1124 |
|
|
$base .= "/$dir";
|
1125 |
|
|
if (!is_dir($base)) {
|
1126 |
|
|
if (!@mkdir($base, $mode))
|
1127 |
|
|
return false;
|
1128 |
|
|
}
|
1129 |
|
|
}
|
1130 |
|
|
return true;
|
1131 |
2b4d37de
|
Ermal Lu?i
|
}
|
1132 |
|
|
|
1133 |
aa4f498d
|
Erik Fonnesbeck
|
/*
|
1134 |
|
|
* get_sysctl($names)
|
1135 |
|
|
* Get values of sysctl OID's listed in $names (accepts an array or a single
|
1136 |
|
|
* name) and return an array of key/value pairs set for those that exist
|
1137 |
|
|
*/
|
1138 |
|
|
function get_sysctl($names) {
|
1139 |
|
|
if (empty($names))
|
1140 |
|
|
return array();
|
1141 |
|
|
|
1142 |
|
|
if (is_array($names)) {
|
1143 |
|
|
$name_list = array();
|
1144 |
|
|
foreach ($names as $name) {
|
1145 |
|
|
$name_list[] = escapeshellarg($name);
|
1146 |
|
|
}
|
1147 |
|
|
} else
|
1148 |
|
|
$name_list = array(escapeshellarg($names));
|
1149 |
|
|
|
1150 |
|
|
exec("/sbin/sysctl -i " . implode(" ", $name_list), $output);
|
1151 |
|
|
$values = array();
|
1152 |
|
|
foreach ($output as $line) {
|
1153 |
|
|
$line = explode(": ", $line, 2);
|
1154 |
|
|
if (count($line) == 2)
|
1155 |
|
|
$values[$line[0]] = $line[1];
|
1156 |
|
|
}
|
1157 |
|
|
|
1158 |
|
|
return $values;
|
1159 |
|
|
}
|
1160 |
|
|
|
1161 |
|
|
/*
|
1162 |
|
|
* set_sysctl($value_list)
|
1163 |
|
|
* Set sysctl OID's listed as key/value pairs and return
|
1164 |
|
|
* an array with keys set for those that succeeded
|
1165 |
|
|
*/
|
1166 |
|
|
function set_sysctl($values) {
|
1167 |
|
|
if (empty($values))
|
1168 |
|
|
return array();
|
1169 |
|
|
|
1170 |
|
|
$value_list = array();
|
1171 |
|
|
foreach ($values as $key => $value) {
|
1172 |
|
|
$value_list[] = escapeshellarg($key) . "=" . escapeshellarg($value);
|
1173 |
|
|
}
|
1174 |
|
|
|
1175 |
|
|
exec("/sbin/sysctl -i " . implode(" ", $value_list), $output, $success);
|
1176 |
|
|
|
1177 |
|
|
/* Retry individually if failed (one or more read-only) */
|
1178 |
|
|
if ($success <> 0 && count($value_list) > 1) {
|
1179 |
|
|
foreach ($value_list as $value) {
|
1180 |
|
|
exec("/sbin/sysctl -i " . $value, $output);
|
1181 |
|
|
}
|
1182 |
|
|
}
|
1183 |
|
|
|
1184 |
|
|
$ret = array();
|
1185 |
|
|
foreach ($output as $line) {
|
1186 |
|
|
$line = explode(": ", $line, 2);
|
1187 |
|
|
if (count($line) == 2)
|
1188 |
|
|
$ret[$line[0]] = true;
|
1189 |
|
|
}
|
1190 |
|
|
|
1191 |
|
|
return $ret;
|
1192 |
|
|
}
|
1193 |
|
|
|
1194 |
2b4d37de
|
Ermal Lu?i
|
/*
|
1195 |
|
|
* get_memory()
|
1196 |
|
|
* returns an array listing the amount of
|
1197 |
|
|
* memory installed in the hardware
|
1198 |
|
|
* [0]real and [1]available
|
1199 |
|
|
*/
|
1200 |
|
|
function get_memory() {
|
1201 |
5bbd08e1
|
Warren Baker
|
$matches = "";
|
1202 |
|
|
if(file_exists("/var/log/dmesg.boot"))
|
1203 |
|
|
$mem = `cat /var/log/dmesg.boot | grep memory`;
|
1204 |
|
|
else
|
1205 |
|
|
$mem = `dmesg -a | grep memory`;
|
1206 |
|
|
if (preg_match_all("/avail memory.* \((.*)MB\)/", $mem, $matches))
|
1207 |
|
|
return array($matches[1][0], $matches[1][0]);
|
1208 |
|
|
if(!$real && !$avail) {
|
1209 |
|
|
$real = trim(`sysctl hw.physmem | cut -d' ' -f2`);
|
1210 |
|
|
$avail = trim(`sysctl hw.realmem | cut -d' ' -f2`);
|
1211 |
|
|
/* convert from bytes to megabytes */
|
1212 |
|
|
return array(($real/1048576),($avail/1048576));
|
1213 |
|
|
}
|
1214 |
2b4d37de
|
Ermal Lu?i
|
}
|
1215 |
|
|
|
1216 |
|
|
function mute_kernel_msgs() {
|
1217 |
5bbd08e1
|
Warren Baker
|
global $config;
|
1218 |
|
|
// Do not mute serial console. The kernel gets very very cranky
|
1219 |
|
|
// and will start dishing you cannot control tty errors.
|
1220 |
|
|
if(trim(file_get_contents("/etc/platform")) == "nanobsd")
|
1221 |
|
|
return;
|
1222 |
|
|
if($config['system']['enableserial'])
|
1223 |
|
|
return;
|
1224 |
|
|
exec("/sbin/conscontrol mute on");
|
1225 |
2b4d37de
|
Ermal Lu?i
|
}
|
1226 |
|
|
|
1227 |
|
|
function unmute_kernel_msgs() {
|
1228 |
5bbd08e1
|
Warren Baker
|
global $config;
|
1229 |
|
|
// Do not mute serial console. The kernel gets very very cranky
|
1230 |
|
|
// and will start dishing you cannot control tty errors.
|
1231 |
|
|
if(trim(file_get_contents("/etc/platform")) == "nanobsd")
|
1232 |
|
|
return;
|
1233 |
|
|
exec("/sbin/conscontrol mute off");
|
1234 |
2b4d37de
|
Ermal Lu?i
|
}
|
1235 |
|
|
|
1236 |
|
|
function start_devd() {
|
1237 |
6955830f
|
Ermal Lu?i
|
global $g;
|
1238 |
|
|
|
1239 |
5bbd08e1
|
Warren Baker
|
exec("/sbin/devd");
|
1240 |
|
|
sleep(1);
|
1241 |
2b4d37de
|
Ermal Lu?i
|
}
|
1242 |
|
|
|
1243 |
66bcba1b
|
Ermal
|
function is_interface_vlan_mismatch() {
|
1244 |
5bbd08e1
|
Warren Baker
|
global $config, $g;
|
1245 |
66bcba1b
|
Ermal
|
|
1246 |
5bbd08e1
|
Warren Baker
|
if (is_array($config['vlans']['vlan'])) {
|
1247 |
|
|
foreach ($config['vlans']['vlan'] as $vlan) {
|
1248 |
|
|
if (does_interface_exist($vlan['if']) == false)
|
1249 |
66bcba1b
|
Ermal
|
return true;
|
1250 |
5bbd08e1
|
Warren Baker
|
}
|
1251 |
|
|
}
|
1252 |
66bcba1b
|
Ermal
|
|
1253 |
|
|
return false;
|
1254 |
|
|
}
|
1255 |
|
|
|
1256 |
2b4d37de
|
Ermal Lu?i
|
function is_interface_mismatch() {
|
1257 |
857da904
|
Scott Ullrich
|
global $config, $g;
|
1258 |
2b4d37de
|
Ermal Lu?i
|
|
1259 |
857da904
|
Scott Ullrich
|
$do_assign = false;
|
1260 |
|
|
$i = 0;
|
1261 |
72993196
|
Ermal
|
if (is_array($config['interfaces'])) {
|
1262 |
857da904
|
Scott Ullrich
|
foreach ($config['interfaces'] as $ifname => $ifcfg) {
|
1263 |
|
|
if (preg_match("/^enc|^cua|^tun|^l2tp|^pptp|^ppp|^ovpn|^gif|^gre|^lagg|^bridge|vlan|_wlan/i", $ifcfg['if'])) {
|
1264 |
|
|
// Do not check these interfaces.
|
1265 |
|
|
$i++;
|
1266 |
|
|
continue;
|
1267 |
|
|
}
|
1268 |
|
|
else if (does_interface_exist($ifcfg['if']) == false) {
|
1269 |
72993196
|
Ermal
|
$do_assign = true;
|
1270 |
857da904
|
Scott Ullrich
|
} else
|
1271 |
|
|
$i++;
|
1272 |
|
|
}
|
1273 |
72993196
|
Ermal
|
}
|
1274 |
2b4d37de
|
Ermal Lu?i
|
|
1275 |
857da904
|
Scott Ullrich
|
if ($g['minimum_nic_count'] > $i) {
|
1276 |
|
|
$do_assign = true;
|
1277 |
|
|
} else if (file_exists("{$g['tmp_path']}/assign_complete"))
|
1278 |
|
|
$do_assign = false;
|
1279 |
2b4d37de
|
Ermal Lu?i
|
|
1280 |
857da904
|
Scott Ullrich
|
return $do_assign;
|
1281 |
2b4d37de
|
Ermal Lu?i
|
}
|
1282 |
|
|
|
1283 |
6e8f7b53
|
Ermal Lu?i
|
/* sync carp entries to other firewalls */
|
1284 |
|
|
function carp_sync_client() {
|
1285 |
e14d1c01
|
Ermal Lu?i
|
global $g;
|
1286 |
0ae6daf8
|
Ermal
|
send_event("filter sync");
|
1287 |
6e8f7b53
|
Ermal Lu?i
|
}
|
1288 |
|
|
|
1289 |
6dc88d53
|
Ermal Luci
|
/****f* util/isAjax
|
1290 |
|
|
* NAME
|
1291 |
|
|
* isAjax - reports if the request is driven from prototype
|
1292 |
|
|
* INPUTS
|
1293 |
|
|
* none
|
1294 |
|
|
* RESULT
|
1295 |
|
|
* true/false
|
1296 |
|
|
******/
|
1297 |
|
|
function isAjax() {
|
1298 |
5bbd08e1
|
Warren Baker
|
return isset ($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
|
1299 |
6dc88d53
|
Ermal Luci
|
}
|
1300 |
|
|
|
1301 |
dad2b40e
|
Tim Allender
|
/****f* util/timeout
|
1302 |
|
|
* NAME
|
1303 |
|
|
* timeout - console input with timeout countdown. Note: erases 2 char of screen for timer. Leave space.
|
1304 |
|
|
* INPUTS
|
1305 |
|
|
* optional, seconds to wait before timeout. Default 9 seconds.
|
1306 |
|
|
* RESULT
|
1307 |
|
|
* returns 1 char of user input or null if no input.
|
1308 |
|
|
******/
|
1309 |
|
|
function timeout($timer = 9) {
|
1310 |
|
|
while(!isset($key)) {
|
1311 |
|
|
if ($timer >= 9) { echo chr(8) . chr(8) . ($timer==9 ? chr(32) : null) . "{$timer}"; }
|
1312 |
|
|
else { echo chr(8). "{$timer}"; }
|
1313 |
|
|
`/bin/stty -icanon min 0 time 25`;
|
1314 |
|
|
$key = trim(`KEY=\`dd count=1 2>/dev/null\`; echo \$KEY`);
|
1315 |
|
|
`/bin/stty icanon`;
|
1316 |
|
|
if ($key == '')
|
1317 |
|
|
unset($key);
|
1318 |
|
|
$timer--;
|
1319 |
|
|
if ($timer == 0)
|
1320 |
|
|
break;
|
1321 |
|
|
}
|
1322 |
|
|
return $key;
|
1323 |
|
|
}
|
1324 |
6dc88d53
|
Ermal Luci
|
|
1325 |
fdf3af3f
|
Scott Ullrich
|
/****f* util/msort
|
1326 |
|
|
* NAME
|
1327 |
|
|
* msort - sort array
|
1328 |
|
|
* INPUTS
|
1329 |
|
|
* $array to be sorted, field to sort by, direction of sort
|
1330 |
|
|
* RESULT
|
1331 |
|
|
* returns newly sorted array
|
1332 |
|
|
******/
|
1333 |
4a8bc5a2
|
Scott Ullrich
|
function msort($array, $id="id", $sort_ascending=true) {
|
1334 |
|
|
$temp_array = array();
|
1335 |
|
|
while(count($array)>0) {
|
1336 |
|
|
$lowest_id = 0;
|
1337 |
|
|
$index=0;
|
1338 |
|
|
foreach ($array as $item) {
|
1339 |
|
|
if (isset($item[$id])) {
|
1340 |
|
|
if ($array[$lowest_id][$id]) {
|
1341 |
|
|
if (strtolower($item[$id]) < strtolower($array[$lowest_id][$id])) {
|
1342 |
|
|
$lowest_id = $index;
|
1343 |
|
|
}
|
1344 |
|
|
}
|
1345 |
|
|
}
|
1346 |
|
|
$index++;
|
1347 |
|
|
}
|
1348 |
|
|
$temp_array[] = $array[$lowest_id];
|
1349 |
|
|
$array = array_merge(array_slice($array, 0,$lowest_id), array_slice($array, $lowest_id+1));
|
1350 |
|
|
}
|
1351 |
|
|
if ($sort_ascending) {
|
1352 |
|
|
return $temp_array;
|
1353 |
|
|
} else {
|
1354 |
|
|
return array_reverse($temp_array);
|
1355 |
|
|
}
|
1356 |
|
|
}
|
1357 |
|
|
|
1358 |
fdf3af3f
|
Scott Ullrich
|
/****f* util/color
|
1359 |
|
|
* NAME
|
1360 |
|
|
* color - outputs a color code to the ansi terminal if supported
|
1361 |
|
|
* INPUTS
|
1362 |
6028a72d
|
Scott Ullrich
|
* color code or color name
|
1363 |
fdf3af3f
|
Scott Ullrich
|
* RESULT
|
1364 |
|
|
* Outputs the ansi color sequence for the color specified. Default resets terminal.
|
1365 |
|
|
******/
|
1366 |
|
|
function color($color = "0m") {
|
1367 |
|
|
/*
|
1368 |
|
|
Color codes available:
|
1369 |
|
|
0m reset; clears all colors and styles (to white on black)
|
1370 |
|
|
1m bold on (see below)
|
1371 |
|
|
3m italics on
|
1372 |
|
|
4m underline on
|
1373 |
|
|
7m inverse on; reverses foreground & background colors
|
1374 |
|
|
9m strikethrough on
|
1375 |
|
|
22m bold off (see below)
|
1376 |
|
|
23m italics off
|
1377 |
|
|
24m underline off
|
1378 |
|
|
27m inverse off
|
1379 |
|
|
29m strikethrough off
|
1380 |
|
|
30m set foreground color to black
|
1381 |
|
|
31m set foreground color to red
|
1382 |
|
|
32m set foreground color to green
|
1383 |
|
|
33m set foreground color to yellow
|
1384 |
|
|
34m set foreground color to blue
|
1385 |
|
|
35m set foreground color to magenta (purple)
|
1386 |
|
|
36m set foreground color to cyan
|
1387 |
|
|
37m set foreground color to white
|
1388 |
|
|
40m set background color to black
|
1389 |
|
|
41m set background color to red
|
1390 |
|
|
42m set background color to green
|
1391 |
|
|
43m set background color to yellow
|
1392 |
|
|
44m set background color to blue
|
1393 |
|
|
45m set background color to magenta (purple)
|
1394 |
|
|
46m set background color to cyan
|
1395 |
|
|
47m set background color to white
|
1396 |
|
|
49m set background color to default (black)
|
1397 |
b927a013
|
Scott Ullrich
|
*/
|
1398 |
fdf3af3f
|
Scott Ullrich
|
// Allow caching of TERM to
|
1399 |
|
|
// speedup subequence requests.
|
1400 |
|
|
global $TERM;
|
1401 |
|
|
if(!$TERM)
|
1402 |
|
|
$TERM=`/usr/bin/env | grep color`;
|
1403 |
78e0b65c
|
Scott Ullrich
|
if(!$TERM)
|
1404 |
|
|
$TERM=`/usr/bin/env | grep cons25`;
|
1405 |
b927a013
|
Scott Ullrich
|
if($TERM) {
|
1406 |
6028a72d
|
Scott Ullrich
|
$ESCAPE=chr(27);
|
1407 |
b927a013
|
Scott Ullrich
|
switch ($color) {
|
1408 |
6028a72d
|
Scott Ullrich
|
case "black":
|
1409 |
|
|
return "{$ESCAPE}[30m";
|
1410 |
|
|
case "red":
|
1411 |
|
|
return "{$ESCAPE}[31m";
|
1412 |
|
|
case "green":
|
1413 |
|
|
return "{$ESCAPE}[32m";
|
1414 |
|
|
case "yellow":
|
1415 |
|
|
return "{$ESCAPE}[33m";
|
1416 |
|
|
case "blue":
|
1417 |
|
|
return "{$ESCAPE}[34m";
|
1418 |
|
|
case "magenta":
|
1419 |
|
|
return "{$ESCAPE}[35m";
|
1420 |
|
|
case "cyan":
|
1421 |
|
|
return "{$ESCAPE}[36m";
|
1422 |
|
|
case "white":
|
1423 |
|
|
return "{$ESCAPE}[37m";
|
1424 |
|
|
case "default":
|
1425 |
|
|
return "{$ESCAPE}[39m";
|
1426 |
b927a013
|
Scott Ullrich
|
}
|
1427 |
385a3a31
|
Scott Ullrich
|
return "{$ESCAPE}[{$color}";
|
1428 |
b927a013
|
Scott Ullrich
|
}
|
1429 |
fdf3af3f
|
Scott Ullrich
|
}
|
1430 |
|
|
|
1431 |
5e9dd72a
|
sullrich
|
/****f* util/is_URL
|
1432 |
|
|
* NAME
|
1433 |
|
|
* is_URL
|
1434 |
|
|
* INPUTS
|
1435 |
|
|
* string to check
|
1436 |
|
|
* RESULT
|
1437 |
|
|
* Returns true if item is a URL
|
1438 |
|
|
******/
|
1439 |
|
|
function is_URL($url) {
|
1440 |
|
|
$match = preg_match("'\b(([\w-]+://?|www[.])[^\s()<>]+(?:\([\w\d]+\)|([^[:punct:]\s]|/)))'", $url);
|
1441 |
|
|
if($match)
|
1442 |
|
|
return true;
|
1443 |
|
|
return false;
|
1444 |
|
|
}
|
1445 |
|
|
|
1446 |
ab94ba00
|
Ermal Lu?i
|
function is_file_included($file = "") {
|
1447 |
|
|
$files = get_included_files();
|
1448 |
|
|
if (in_array($file, $files))
|
1449 |
|
|
return true;
|
1450 |
|
|
|
1451 |
|
|
return false;
|
1452 |
|
|
}
|
1453 |
|
|
|
1454 |
0d90fcaf
|
jim-p
|
/*
|
1455 |
|
|
This function was borrowed from a comment on PHP.net at the following URL:
|
1456 |
|
|
http://www.php.net/manual/en/function.array-merge-recursive.php#73843
|
1457 |
|
|
*/
|
1458 |
5bbd08e1
|
Warren Baker
|
function array_merge_recursive_unique($array0, $array1) {
|
1459 |
|
|
|
1460 |
|
|
$arrays = func_get_args();
|
1461 |
|
|
$remains = $arrays;
|
1462 |
0d90fcaf
|
jim-p
|
|
1463 |
5bbd08e1
|
Warren Baker
|
// We walk through each arrays and put value in the results (without
|
1464 |
|
|
// considering previous value).
|
1465 |
|
|
$result = array();
|
1466 |
0d90fcaf
|
jim-p
|
|
1467 |
5bbd08e1
|
Warren Baker
|
// loop available array
|
1468 |
|
|
foreach($arrays as $array) {
|
1469 |
0d90fcaf
|
jim-p
|
|
1470 |
5bbd08e1
|
Warren Baker
|
// The first remaining array is $array. We are processing it. So
|
1471 |
|
|
// we remove it from remaing arrays.
|
1472 |
0d90fcaf
|
jim-p
|
array_shift($remains);
|
1473 |
|
|
|
1474 |
5bbd08e1
|
Warren Baker
|
// We don't care non array param, like array_merge since PHP 5.0.
|
1475 |
|
|
if(is_array($array)) {
|
1476 |
|
|
// Loop values
|
1477 |
|
|
foreach($array as $key => $value) {
|
1478 |
|
|
if(is_array($value)) {
|
1479 |
|
|
// we gather all remaining arrays that have such key available
|
1480 |
|
|
$args = array();
|
1481 |
|
|
foreach($remains as $remain) {
|
1482 |
|
|
if(array_key_exists($key, $remain)) {
|
1483 |
|
|
array_push($args, $remain[$key]);
|
1484 |
|
|
}
|
1485 |
|
|
}
|
1486 |
|
|
|
1487 |
|
|
if(count($args) > 2) {
|
1488 |
|
|
// put the recursion
|
1489 |
|
|
$result[$key] = call_user_func_array(__FUNCTION__, $args);
|
1490 |
|
|
} else {
|
1491 |
|
|
foreach($value as $vkey => $vval) {
|
1492 |
|
|
$result[$key][$vkey] = $vval;
|
1493 |
|
|
}
|
1494 |
|
|
}
|
1495 |
|
|
} else {
|
1496 |
|
|
// simply put the value
|
1497 |
|
|
$result[$key] = $value;
|
1498 |
|
|
}
|
1499 |
|
|
}
|
1500 |
|
|
}
|
1501 |
|
|
}
|
1502 |
|
|
return $result;
|
1503 |
0d90fcaf
|
jim-p
|
}
|
1504 |
|
|
|
1505 |
94436824
|
Scott Ullrich
|
?>
|