1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
|
<?php
/* $Id$ */
/*
util.inc
part of m0n0wall (http://m0n0.ch/wall)
Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
/* kill a process by pid file */
function killbypid($pidfile) {
sigkillbypid($pidfile, "TERM");
}
/* sigkill a process by pid file */
function sigkillbypid($pidfile, $sig) {
if (file_exists($pidfile)) {
$pid = trim(file_get_contents($pidfile));
mwexec("/bin/kill -s $sig {$pid}");
} else {
mwexec("/bin/kill -s $sig {$pidfile}");
}
}
/* kill a process by name */
function killbyname($procname) {
mwexec("/usr/bin/killall " . escapeshellarg($procname));
}
/* return the subnet address given a host address and a subnet bit count */
function gen_subnet($ipaddr, $bits) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
return "";
return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits));
}
/* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */
function gen_subnet_max($ipaddr, $bits) {
if (!is_ipaddr($ipaddr) || !is_numeric($bits))
return "";
return long2ip(ip2long($ipaddr) | ~gen_subnet_mask_long($bits));
}
/* returns a subnet mask (long given a bit count) */
function gen_subnet_mask_long($bits) {
$sm = 0;
for ($i = 0; $i < $bits; $i++) {
$sm >>= 1;
$sm |= 0x80000000;
}
return $sm;
}
/* same as above but returns a string */
function gen_subnet_mask($bits) {
return long2ip(gen_subnet_mask_long($bits));
}
function is_numericint($arg) {
return (preg_match("/[^0-9]/", $arg) ? false : true);
}
/* returns true if $ipaddr is a valid dotted IPv4 address */
function is_ipaddr($ipaddr) {
if (!is_string($ipaddr))
return false;
$ip_long = ip2long($ipaddr);
$ip_reverse = long2ip($ip_long);
if ($ipaddr == $ip_reverse)
return true;
else
return false;
}
/* returns true if $ipaddr is a valid dotted IPv4 address or an alias thereof */
function is_ipaddroralias($ipaddr) {
global $aliastable, $config;
if(is_array($config['aliases']['alias'])) {
foreach($config['aliases']['alias'] as $alias) {
if($alias['name'] == $ipaddr)
return true;
}
}
if (isset($aliastable[$ipaddr]) && is_ipaddr($aliastable[$ipaddr]))
return true;
else
return is_ipaddr($ipaddr);
}
/* returns true if $ipaddr is a valid dotted IPv4 address or any alias */
function is_ipaddroranyalias($ipaddr) {
global $aliastable;
if (isset($aliastable[$ipaddr]))
return true;
else
return is_ipaddr($ipaddr);
}
/* returns true if $subnet is a valid subnet in CIDR format */
function is_subnet($subnet) {
if (!is_string($subnet))
return false;
list($hp,$np) = explode('/', $subnet);
if (!is_ipaddr($hp))
return false;
if (!is_numeric($np) || ($np < 1) || ($np > 32))
return false;
return true;
}
/* returns true if $subnet is a valid subnet in CIDR format or an alias thereof */
function is_subnetoralias($subnet) {
global $aliastable;
if (isset($aliastable[$subnet]) && is_subnet($aliastable[$subnet]))
return true;
else
return is_subnet($subnet);
}
/* returns true if $hostname is a valid hostname */
function is_hostname($hostname) {
if (!is_string($hostname))
return false;
if (preg_match("/^([_a-z0-9\-]+\.?)+$/i", $hostname))
return true;
else
return false;
}
/* returns true if $domain is a valid domain name */
function is_domain($domain) {
if (!is_string($domain))
return false;
if (preg_match("/^([a-z0-9\-]+\.?)+$/i", $domain))
return true;
else
return false;
}
/* returns true if $uname is a valid DynDNS username */
function is_dyndns_username($uname) {
if (!is_string($uname))
return false;
if (preg_match("/[^a-z0-9\-.@_]/i", $uname))
return false;
else
return true;
}
/* returns true if $macaddr is a valid MAC address */
function is_macaddr($macaddr) {
if (!is_string($macaddr))
return false;
$maca = explode(":", $macaddr);
if (count($maca) != 6)
return false;
foreach ($maca as $macel) {
if (($macel === "") || (strlen($macel) > 2))
return false;
if (preg_match("/[^0-9a-f]/i", $macel))
return false;
}
return true;
}
/* returns true if $name is a valid name for an alias */
/* returns NULL if a reserved word is used */
function is_validaliasname($name) {
/* Array of reserved words */
$reserved = array("port", "pass");
if (in_array($name, $reserved, true))
return; /* return NULL */
if (!preg_match("/[^a-zA-Z0-9]/", $name))
return true;
else
return false;
}
/* returns true if $port is a valid TCP/UDP port */
function is_port($port) {
if (!is_numericint($port))
return false;
if (($port < 1) || ($port > 65535))
return false;
else
return true;
}
/* returns true if $val is a valid shaper bandwidth value */
function is_valid_shaperbw($val) {
return (preg_match("/^[0-9]+(Kb|Mb|Gb|%)$/", $val));
}
/*
* get_interface_list() - Return a list of all physical interfaces
* along with MAC and status.
*
* $mode = "active" - use ifconfig -lu
* "media" - use ifconfig to check physical connection
* status (much slower)
*/
/*
* get_interface_list() - Return a list of all physical interfaces
* along with MAC and status.
*
* $mode = "active" - use ifconfig -lu
* "media" - use ifconfig to check physical connection
* status (much slower)
*/
function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") {
global $config;
$upints = array();
/* get a list of virtual interface types */
if(!$vfaces) {
$vfaces = array(
'bridge',
'ppp',
'sl',
'gif',
'faith',
'lo',
'ng',
'vlan',
'pflog',
'pfsync',
'enc',
'tun',
'carp'
);
}
switch($mode) {
case "active":
$upints = explode(" ", trim(shell_exec("/sbin/ifconfig -lu")));
break;
case "media":
$intlist = explode(" ", trim(shell_exec("/sbin/ifconfig -l")));
$ifconfig = "";
exec("/sbin/ifconfig -a", $ifconfig);
$regexp = '/(' . implode('|', $intlist) . '):\s/';
$ifstatus = preg_grep('/status:/', $ifconfig);
foreach($ifstatus as $status) {
$int = array_shift($intlist);
if(stristr($status, "active")) $upints[] = $int;
}
break;
}
/* build interface list with netstat */
$linkinfo = "";
exec("/usr/bin/netstat -inW -f link | awk '{ print $1, $4 }'", $linkinfo);
array_shift($linkinfo);
/* build ip address list with netstat */
$ipinfo = "";
exec("/usr/bin/netstat -inW -f inet | awk '{ print $1, $4 }'", $ipinfo);
array_shift($ipinfo);
foreach($linkinfo as $link) {
$friendly = "";
$alink = explode(" ", $link);
$ifname = rtrim(trim($alink[0]), '*');
/* trim out all numbers before checking for vfaces */
if (!in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces)) {
$toput = array(
"mac" => trim($alink[1]),
"up" => in_array($ifname, $upints)
);
foreach($ipinfo as $ip) {
$aip = explode(" ", $ip);
if($aip[0] == $ifname) {
$toput['ipaddr'] = $aip[1];
}
}
foreach($config['interfaces'] as $name => $int) {
if($int['if'] == $ifname) $friendly = $name;
}
switch($keyby) {
case "physical":
if($friendly != "") {
$toput['friendly'] = $friendly;
}
$iflist[$ifname] = $toput;
break;
case "friendly":
if($friendly != "") {
$toput['if'] = $ifname;
$iflist[$friendly] = $toput;
}
break;
}
}
}
return $iflist;
}
/* wrapper for exec() */
function mwexec($command) {
global $g;
$oarr = "";
$retval = "";
if ($g['debug']) {
if (!$_SERVER['REMOTE_ADDR'])
echo "mwexec(): $command\n";
exec("$command > /dev/null 2>&1", $oarr, $retval);
} else {
exec("$command > /dev/null 2>&1", $oarr, $retval);
}
return $retval;
}
/* wrapper for exec() in background */
function mwexec_bg($command) {
global $g;
if ($g['debug']) {
if (!$_SERVER['REMOTE_ADDR'])
echo "mwexec(): $command\n";
}
exec("nohup $command > /dev/null 2>&1 &");
}
/* unlink a file, if it exists */
function unlink_if_exists($fn) {
$to_do = glob($fn);
if(is_array($to_do)) {
foreach($to_do as $filename)
@unlink($filename);
} else {
@unlink($fn);
}
}
/* make a global alias table (for faster lookups) */
function alias_make_table($config) {
global $aliastable;
$aliastable = array();
if (is_array($config['aliases']['alias'])) {
foreach ($config['aliases']['alias'] as $alias) {
if ($alias['name'])
$aliastable[$alias['name']] = $alias['address'];
}
}
}
/* check if an alias exists */
function is_alias($name) {
global $aliastable;
return isset($aliastable[$name]);
}
function alias_expand_value($name) {
global $aliastable, $config;
foreach($config['aliases']['alias'] as $alias) {
if($alias['name'] == $name)
return $alias['address'];
}
}
/* expand a host or network alias, if necessary */
function alias_expand($name) {
global $aliastable;
if (isset($aliastable[$name]))
return "\${$name}";
else if (is_ipaddr($name) || is_subnet($name))
return "{$name}";
else
return null;
}
/* expand a host alias, if necessary */
function alias_expand_host($name) {
global $aliastable;
if (isset($aliastable[$name])) {
$ip_arr = explode(" ", $aliastable[$name]);
foreach($ip_arr as $ip) {
if (!is_ipaddr($ip))
return null;
}
return $aliastable[$name];
} else if (is_ipaddr($name))
return $name;
else
return null;
}
/* expand a network alias, if necessary */
function alias_expand_net($name) {
global $aliastable;
if (isset($aliastable[$name]) && is_subnet($aliastable[$name]))
return $aliastable[$name];
else if (is_subnet($name))
return $name;
else
return null;
}
/* find out whether two subnets overlap */
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
if (!is_numeric($bits1))
$bits1 = 32;
if (!is_numeric($bits2))
$bits2 = 32;
if ($bits1 < $bits2)
$relbits = $bits1;
else
$relbits = $bits2;
$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
if ($sn1 == $sn2)
return true;
else
return false;
}
/* compare two IP addresses */
function ipcmp($a, $b) {
if (ip2long($a) < ip2long($b))
return -1;
else if (ip2long($a) > ip2long($b))
return 1;
else
return 0;
}
/* return true if $addr is in $subnet, false if not */
function ip_in_subnet($addr,$subnet) {
list($ip, $mask) = explode('/', $subnet);
$mask = 0xffffffff << (32 - $mask);
return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
}
/* verify (and remove) the digital signature on a file - returns 0 if OK */
function verify_digital_signature($fname) {
global $g;
return mwexec("/usr/local/sbin/gzsig verify {$g['etc_path']}/pubkey.pem < " . escapeshellarg($fname));
}
/* obtain MAC address given an IP address by looking at the ARP table */
function arp_get_mac_by_ip($ip) {
mwexec("/sbin/ping -c 1 -t 1 {$ip}");
$arpoutput = "";
exec("/usr/sbin/arp -n {$ip}", $arpoutput);
if ($arpoutput[0]) {
$arpi = explode(" ", $arpoutput[0]);
$macaddr = $arpi[3];
if (is_macaddr($macaddr))
return $macaddr;
else
return false;
}
return false;
}
/* return a fieldname that is safe for xml usage */
function xml_safe_fieldname($fieldname) {
$replace = array('/', '-', ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
'_', '+', '=', '{', '}', '[', ']', '|', '/', '<', '>', '?',
':', ',', '.', '\'', '\\'
);
return strtolower(str_replace($replace, "", $fieldname));
}
function mac_format($clientmac) {
$mac =explode(":", $clientmac);
global $config;
$mac_format = $config['captiveportal']['radmac_format'] ? $config['captiveportal']['radmac_format'] : false;
switch($mac_format) {
case 'singledash':
return "$mac[0]$mac[1]$mac[2]-$mac[3]$mac[4]$mac[5]";
case 'ietf':
return "$mac[0]-$mac[1]-$mac[2]-$mac[3]-$mac[4]-$mac[5]";
case 'cisco':
return "$mac[0]$mac[1].$mac[2]$mac[3].$mac[4]$mac[5]";
case 'unformatted':
return "$mac[0]$mac[1]$mac[2]$mac[3]$mac[4]$mac[5]";
default:
return $clientmac;
}
}
?>
|