Project

General

Profile

Download (26.1 KB) Statistics
| Branch: | Tag: | Revision:
1
<?php /* $Id$ */ /*
2
	util.inc
3
	part of m0n0wall (http://m0n0.ch/wall)
4

    
5
	Copyright (C) 2003-2004 Manuel Kasper <mk@neon1.net>.
6
	All rights reserved.
7

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

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

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

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

    
30
/* kill a process by pid file */
31
function killbypid($pidfile) {
32
	sigkillbypid($pidfile, "TERM");
33
}
34

    
35
function isvalidpid($pid) {
36
	$running = `ps -p $pid | wc -l`;
37
	if(intval($running) > 1)
38
		return true;
39
	else 
40
		return false;
41
}
42

    
43
function is_process_running($process) {
44
        $running = (trim(shell_exec("ps axwu | grep '\b{$process}\b' | grep -v 'grep'")) != '');
45

    
46
        return $running;
47
}
48

    
49
function isvalidproc($proc) {
50
	$running = is_process_running($proc);
51
	if (intval($running) >= 1)
52
		return true;
53
	else 
54
		return false;
55
}
56

    
57
/* sigkill a process by pid file */
58
/* return 1 for success and 0 for a failure */
59
function sigkillbypid($pidfile, $sig) {
60
	if (is_file($pidfile)) {
61
		$pid = trim(file_get_contents($pidfile));
62
		if(isvalidpid($pid))
63
			return mwexec("/bin/kill -s $sig {$pid}", true);
64
	}
65
	return 0;
66
}
67

    
68
/* kill a process by name */
69
function sigkillbyname($procname, $sig) {
70
	if(isvalidproc($procname))
71
		return mwexec("/usr/bin/killall -{$sig} " . escapeshellarg($procname), true);
72
}
73

    
74
/* kill a process by name */
75
function killbyname($procname) {
76
	if(isvalidproc($procname))
77
		mwexec("/usr/bin/killall " . escapeshellarg($procname));
78
}
79

    
80
function is_subsystem_dirty($subsystem = "") {
81
	global $g;
82

    
83
	if ($subsystem == "")
84
		return false;
85

    
86
	if (file_exists("{$g['varrun_path']}/{$subsystem}.dirty"))
87
		return true;
88

    
89
	return false;
90
}
91

    
92
function mark_subsystem_dirty($subsystem = "") {
93
	global $g;
94

    
95
	if (!file_put_contents("{$g['varrun_path']}/{$subsystem}.dirty", "DIRTY"))
96
		log_error("WARNING: Could not mark subsystem: {$subsytem} dirty");
97
}
98

    
99
function clear_subsystem_dirty($subsystem = "") {
100
	global $g;
101

    
102
	@unlink("{$g['varrun_path']}/{$subsystem}.dirty");
103
}
104

    
105
function config_lock() {
106
	log_error("config_lock() is depricated please use lock().");
107
	return;
108
}
109
function config_unlock() {
110
	log_error("config_unlock() is depricated please use unlock().");
111
	return;
112
}
113

    
114
/* lock configuration file */
115
function lock($lock) {
116
        global $g, $cfglckkeyconsumers;
117

    
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

    
123
		$config_lock_key = ftok("{$g['tmp_path']}/{$lock}.lock", 'a');
124
		$cfglckkey = sem_get($config_lock_key, 1);
125
		$cfglckkeyconsumers++;
126

    
127
		// Rely on or own logging for errors here.
128
		ini_set('error_reporting', E_NONE);
129

    
130
		if (!sem_acquire($cfglckkey)) {
131
				log_error("WARNING: lock() -  Could not acquire {$lock} lock!");
132
				sem_remove($cfglckkey);
133
				return NULL;
134
		} else if ($g['debug'])
135
			log_error("lock() -  Got {$file} lock.");
136

    
137
		ini_set('error_reporting', E_ALL  & ~E_NOTICE);
138

    
139
		return $cfglckkey;
140
}
141

    
142
/* unlock configuration file */
143
function unlock($cfglckkey = 0) {
144
        global $g, $cfglckkeyconsumers;
145

    
146
        if ($cfglckkey == 0)
147
                return;
148

    
149
		// Rely on or own logging for errors here.
150
		ini_set('error_reporting', E_NONE);
151

    
152
        if (!sem_release($cfglckkey))
153
                log_error("WARNING: unlock() - Could not unlock lock.");
154
        else {
155
                if ($g['debug'])
156
                        log_error("Released lock.");
157
				ini_set('error_reporting', E_NONE);
158
                sem_remove($cfglckkey);
159
				ini_set('error_reporting', E_ALL  & ~E_NOTICE);
160
        }
161
        $cfglckkeyconsumers--;
162

    
163
		ini_set('error_reporting', E_ALL  & ~E_NOTICE);
164
}
165

    
166
function is_module_loaded($module_name) {
167
	$running = `/sbin/kldstat | grep {$module_name} | /usr/bin/grep -v grep | /usr/bin/wc -l`;
168
	if (intval($running) >= 1)
169
		return true;
170
	else
171
		return false;
172
}
173

    
174
/* return the subnet address given a host address and a subnet bit count */
175
function gen_subnet($ipaddr, $bits) {
176
	if (!is_ipaddr($ipaddr) || !is_numeric($bits))
177
		return "";
178

    
179
	return long2ip(ip2long($ipaddr) & gen_subnet_mask_long($bits));
180
}
181

    
182
/* return the highest (broadcast) address in the subnet given a host address and a subnet bit count */
183
function gen_subnet_max($ipaddr, $bits) {
184
	if (!is_ipaddr($ipaddr) || !is_numeric($bits))
185
		return "";
186

    
187
	return long2ip(ip2long($ipaddr) | ~gen_subnet_mask_long($bits));
188
}
189

    
190
/* returns a subnet mask (long given a bit count) */
191
function gen_subnet_mask_long($bits) {
192
	$sm = 0;
193
	for ($i = 0; $i < $bits; $i++) {
194
		$sm >>= 1;
195
		$sm |= 0x80000000;
196
	}
197
	return $sm;
198
}
199

    
200
/* same as above but returns a string */
201
function gen_subnet_mask($bits) {
202
	return long2ip(gen_subnet_mask_long($bits));
203
}
204

    
205
function is_numericint($arg) {
206
	return (preg_match("/[^0-9]/", $arg) ? false : true);
207
}
208

    
209
/* returns true if $ipaddr is a valid dotted IPv4 address */
210
function is_ipaddr($ipaddr) {
211
	if (!is_string($ipaddr))
212
		return false;
213

    
214
	$ip_long = ip2long($ipaddr);
215
	$ip_reverse = long2ip($ip_long);
216

    
217
	if ($ipaddr == $ip_reverse)
218
		return true;
219
	else
220
		return false;
221
}
222

    
223
/* returns true if $ipaddr is a valid dotted IPv4 address or an alias thereof */
224
function is_ipaddroralias($ipaddr) {
225

    
226
	global $aliastable, $config;
227

    
228
	if(is_array($config['aliases']['alias'])) {
229
		foreach($config['aliases']['alias'] as $alias) {
230
			if($alias['name'] == $ipaddr)
231
				return true;
232
		}
233
	}
234

    
235
	if (isset($aliastable[$ipaddr]) && is_ipaddr($aliastable[$ipaddr]))
236
		return true;
237
	else
238
		return is_ipaddr($ipaddr);
239

    
240
}
241

    
242
/* returns true if $ipaddr is a valid dotted IPv4 address or any alias */
243
function is_ipaddroranyalias($ipaddr) {
244

    
245
	global $aliastable;
246

    
247
	if (isset($aliastable[$ipaddr]))
248
		return true;
249
	else
250
		return is_ipaddr($ipaddr);
251
}
252

    
253
/* returns true if $subnet is a valid subnet in CIDR format */
254
function is_subnet($subnet) {
255
	if (!is_string($subnet))
256
		return false;
257

    
258
	list($hp,$np) = explode('/', $subnet);
259

    
260
	if (!is_ipaddr($hp))
261
		return false;
262

    
263
	if (!is_numeric($np) || ($np < 1) || ($np > 32))
264
		return false;
265

    
266
	return true;
267
}
268

    
269
/* returns true if $subnet is a valid subnet in CIDR format or an alias thereof */
270
function is_subnetoralias($subnet) {
271

    
272
	global $aliastable;
273

    
274
	if (isset($aliastable[$subnet]) && is_subnet($aliastable[$subnet]))
275
		return true;
276
	else
277
		return is_subnet($subnet);
278
}
279

    
280
/* returns true if $hostname is a valid hostname */
281
function is_hostname($hostname) {
282
	if (!is_string($hostname))
283
		return false;
284

    
285
	if (preg_match("/^([_a-z0-9\-]+\.?)+$/i", $hostname))
286
		return true;
287
	else
288
		return false;
289
}
290

    
291
/* returns true if $domain is a valid domain name */
292
function is_domain($domain) {
293
	if (!is_string($domain))
294
		return false;
295

    
296
	if (preg_match("/^([a-z0-9\-]+\.?)+$/i", $domain))
297
		return true;
298
	else
299
		return false;
300
}
301

    
302
/* returns true if $macaddr is a valid MAC address */
303
function is_macaddr($macaddr) {
304
	if (!is_string($macaddr))
305
		return false;
306

    
307
	$maca = explode(":", $macaddr);
308
	if (count($maca) != 6)
309
		return false;
310

    
311
	foreach ($maca as $macel) {
312
		if (($macel === "") || (strlen($macel) > 2))
313
			return false;
314
		if (preg_match("/[^0-9a-f]/i", $macel))
315
			return false;
316
	}
317

    
318
	return true;
319
}
320

    
321
/* returns true if $name is a valid name for an alias */
322
/* returns NULL if a reserved word is used */
323
function is_validaliasname($name) {
324
	/* Array of reserved words */
325
	$reserved = array("port", "pass");
326
	if (in_array($name, $reserved, true))
327
		return; /* return NULL */
328

    
329
	if (!preg_match("/[^a-zA-Z0-9_]/", $name))
330
		return true;
331
	else
332
		return false;
333
}
334

    
335
/* returns true if $port is a valid TCP/UDP port */
336
function is_port($port) {
337
	if (!is_numericint($port))
338
		return false;
339

    
340
	if (($port < 1) || ($port > 65535))
341
		return false;
342
	else
343
		return true;
344
}
345

    
346
/* returns true if $portrange is a valid TCP/UDP portrange ("<port>:<port>") */
347
function is_portrange($portrange) {
348
        $ports = explode(":", $portrange);
349

    
350
        if(count($ports) == 2 && is_port($ports[0]) && is_port($ports[1]))
351
                return true;
352
        else
353
                return false;
354
}
355

    
356
/* returns true if $val is a valid shaper bandwidth value */
357
function is_valid_shaperbw($val) {
358
	return (preg_match("/^(\d+(?:\.\d+)?)([MKG]?b|%)$/", $val));
359
}
360

    
361
/* return the configured interfaces list. */
362
function get_configured_interface_list($only_opt = false, $withdisabled = false) {
363
	global $config;
364

    
365
	$iflist = array();
366

    
367
	if (!$only_opt) {
368
		if (isset($config['interfaces']['wan']))
369
			$iflist['wan'] = "wan";
370
		if (isset($config['interfaces']['lan']))
371
			$iflist['lan'] = "lan";
372
	}
373

    
374
	/* if list */
375
        foreach($config['interfaces'] as $if => $ifdetail) {
376
		if ($if == "wan" || $if == "lan")
377
			continue;
378
		if (isset($ifdetail['enable']) || $withdisabled == true)
379
			$iflist[$if] = $if;
380
	}
381

    
382
	return $iflist;
383
}
384

    
385
/* return the configured interfaces list. */
386
function get_configured_interface_list_by_realif($only_opt = false, $withdisabled = false) {
387
        global $config;
388

    
389
        $iflist = array();
390

    
391
        if (!$only_opt) {
392
                if (isset($config['interfaces']['wan'])) {
393
			$tmpif = get_real_interface("wan");
394
			if (!empty($tmpif))
395
				$iflist[$tmpif] = "wan";
396
		}
397
                if (isset($config['interfaces']['lan'])) {
398
			$tmpif = get_real_interface("lan");
399
			if (!empty($tmpif))
400
				$iflist[$tmpif] = "lan";
401
		}
402
        }
403

    
404
        /* if list */
405
        foreach($config['interfaces'] as $if => $ifdetail) {
406
                if ($if == "wan" || $if == "lan")
407
                        continue;
408
                if (isset($ifdetail['enable']) || $withdisabled == true) {
409
			$tmpif = get_real_interface($if);
410
			if (!empty($tmpif))
411
				$iflist[$tmpif] = $if;
412
		}
413
        }
414

    
415
        return $iflist;
416
}
417

    
418
/* return the configured interfaces list with their description. */
419
function get_configured_interface_with_descr($only_opt = false, $withdisabled = false) {
420
	global $config;
421

    
422
	$iflist = array();
423

    
424
	if (!$only_opt) {
425
		if (isset($config['interfaces']['wan'])) {
426
			if (empty($config['interfaces']['wan']['descr']))
427
				$iflist['wan'] = "WAN";
428
			else
429
				$iflist['wan'] = strtoupper($config['interfaces']['wan']['descr']);
430
		}
431
		if (isset($config['interfaces']['lan'])) {
432
			if (empty($config['interfaces']['lan']['descr']))
433
				$iflist['lan'] = "LAN";
434
			else
435
				$iflist['lan'] = strtoupper($config['interfaces']['lan']['descr']);
436
		}
437
	}
438

    
439
	/* if list */
440
	foreach($config['interfaces'] as $if => $ifdetail) {
441
		if (isset($ifdetail['enable']) || $withdisabled == true) {
442
			if($ifdetail['descr'] == "")
443
				$iflist[$if] = strtoupper($if);
444
			else
445
				$iflist[$if] = strtoupper($ifdetail['descr']);
446
		}
447
	}
448

    
449
	return $iflist;
450
}
451

    
452

    
453
/*
454
 *   get_interface_list() - Return a list of all physical interfaces
455
 *   along with MAC and status.
456
 *
457
 *   $mode = "active" - use ifconfig -lu
458
 *           "media"  - use ifconfig to check physical connection
459
 *			status (much slower)
460
 */
461
function get_interface_list($mode = "active", $keyby = "physical", $vfaces = "") {
462
        global $config;
463
	$upints = array();
464
        /* get a list of virtual interface types */
465
        if(!$vfaces) {
466
		$vfaces = array (
467
				'bridge',
468
				'ppp',
469
				'sl',
470
				'gif',
471
				'gre',
472
				'faith',
473
				'lo',
474
				'ng',
475
				'vlan',
476
				'pflog',
477
				'plip',
478
				'pfsync',
479
				'enc',
480
				'tun',
481
				'carp',
482
				'lagg'
483
		);
484
	}
485
	switch($mode) {
486
	case "active":
487
                $upints = explode(" ", trim(shell_exec("/sbin/ifconfig -lu")));
488
        	break;
489
	case "media":
490
                $intlist = explode(" ", trim(shell_exec("/sbin/ifconfig -l")));
491
                $ifconfig = "";
492
                exec("/sbin/ifconfig -a", $ifconfig);
493
                $regexp = '/(' . implode('|', $intlist) . '):\s/';
494
                $ifstatus = preg_grep('/status:/', $ifconfig);
495
		foreach($ifstatus as $status) {
496
			$int = array_shift($intlist);
497
                	if(stristr($status, "active")) $upints[] = $int;
498
		}
499
		break;
500
	}
501
        /* build interface list with netstat */
502
        $linkinfo = "";
503
        exec("/usr/bin/netstat -inW -f link | awk '{ print $1, $4 }'", $linkinfo);
504
        array_shift($linkinfo);
505
	/* build ip address list with netstat */
506
	$ipinfo = "";
507
	exec("/usr/bin/netstat -inW -f inet | awk '{ print $1, $4 }'", $ipinfo);
508
	array_shift($ipinfo);
509
	foreach($linkinfo as $link) {
510
		$friendly = "";
511
                $alink = explode(" ", $link);
512
                $ifname = rtrim(trim($alink[0]), '*');
513
                /* trim out all numbers before checking for vfaces */
514
		if (!in_array(array_shift(preg_split('/\d/', $ifname)), $vfaces)) {
515
			$toput = array(
516
					"mac" => trim($alink[1]),
517
					"up" => in_array($ifname, $upints)
518
				);
519
			foreach($ipinfo as $ip) {
520
				$aip = explode(" ", $ip);
521
				if($aip[0] == $ifname) {
522
					$toput['ipaddr'] = $aip[1];
523
				}
524
			}
525
			foreach($config['interfaces'] as $name => $int) {
526
				if($int['if'] == $ifname) $friendly = $name;
527
			}
528
			switch($keyby) {
529
			case "physical":
530
				if($friendly != "") {
531
					$toput['friendly'] = $friendly;
532
				}
533
				$dmesg_arr = array();
534
				exec("/sbin/dmesg |grep $ifname | head -n1", $dmesg_arr);
535
				preg_match_all("/<(.*?)>/i", $dmesg_arr[0], $dmesg);
536
				$toput['dmesg'] = $dmesg[1][0];
537
				$iflist[$ifname] = $toput;
538
				break;
539
			case "friendly":
540
				if($friendly != "") {
541
					$toput['if'] = $ifname;
542
					$iflist[$friendly] = $toput;
543
				}
544
				break;
545
			}
546
                }
547
        }
548
        return $iflist;
549
}
550

    
551
/****f* util/log_error
552
* NAME
553
*   log_error  - Sends a string to syslog.
554
* INPUTS
555
*   $error     - string containing the syslog message.
556
* RESULT
557
*   null
558
******/
559
function log_error($error) {
560
        global $g;
561
        $page = $_SERVER['SCRIPT_NAME'];
562
        syslog(LOG_WARNING, "$page: $error");
563
        if ($g['debug'])
564
                syslog(LOG_WARNING, var_dump(debug_backtrace()));
565
        return;
566
}
567

    
568
/****f* util/exec_command
569
 * NAME
570
 *   exec_command - Execute a command and return a string of the result.
571
 * INPUTS
572
 *   $command   - String of the command to be executed.
573
 * RESULT
574
 *   String containing the command's result.
575
 * NOTES
576
 *   This function returns the command's stdout and stderr.
577
 ******/
578
function exec_command($command) {
579
        $output = array();
580
        exec($command . ' 2>&1 ', $output);
581
        return(implode("\n", $output));
582
}
583

    
584
/* wrapper for exec() */
585
function mwexec($command, $mute = false) {
586

    
587
	global $g;
588
	$oarr = array();
589
	$retval = 0;
590
	if ($g['debug']) {
591
		if (!$_SERVER['REMOTE_ADDR'])
592
			echo "mwexec(): $command\n";
593
		exec("$command 2>&1", $oarr, $retval);
594
	} else {
595
		exec("$command 2>&1", $oarr, $retval);
596
	}
597
	if(isset($config['system']['developerspew']))
598
                $mute = false;
599
	if(($retval <> 0) && ($mute === false)) {
600
		$output = implode(" ", $oarr);
601
		log_error("The command '$command' returned exit code '$retval', the output was '$output' ");
602
	}
603
	return $retval;
604
}
605

    
606
/* wrapper for exec() in background */
607
function mwexec_bg($command) {
608

    
609
	global $g;
610

    
611
	if ($g['debug']) {
612
		if (!$_SERVER['REMOTE_ADDR'])
613
			echo "mwexec(): $command\n";
614
	}
615

    
616
	exec("nohup $command > /dev/null 2>&1 &");
617
}
618

    
619
/* unlink a file, if it exists */
620
function unlink_if_exists($fn) {
621
	$to_do = glob($fn);
622
	if(is_array($to_do)) {
623
		foreach($to_do as $filename)
624
			@unlink($filename);
625
	} else {
626
		@unlink($fn);
627
	}
628
}
629
/* make a global alias table (for faster lookups) */
630
function alias_make_table($config) {
631

    
632
	global $aliastable;
633

    
634
	$aliastable = array();
635

    
636
	if (is_array($config['aliases']['alias'])) {
637
		foreach ($config['aliases']['alias'] as $alias) {
638
			if ($alias['name'])
639
				$aliastable[$alias['name']] = $alias['address'];
640
		}
641
	}
642
}
643
/* check if an alias exists */
644
function is_alias($name) {
645

    
646
	global $aliastable;
647

    
648
	return isset($aliastable[$name]);
649
}
650

    
651
function alias_expand_value($name) {
652

    
653
	global $aliastable, $config;
654
	$newaddress = "";
655
	$firstentry = true;
656
	if($config['aliases']['alias'])
657
		foreach($config['aliases']['alias'] as $alias) {
658
			if($alias['name'] == $name) {
659
				if($alias['type'] == "openvpn") {
660
					$vpn_address_split = split(" ", $alias['address']);
661
					foreach($vpn_address_split as $vpnsplit) {
662
						foreach($config['openvpn']['user'] as $openvpn) {
663
							if($openvpn['name'] == $vpnsplit) {
664
								if($firstentry == false) 
665
									$newaddress .= " ";
666
								$newaddress .= $openvpn['ip'];
667
								$firstentry = false;
668
							}
669
						}
670
					}
671
				} else {
672
					$newaddress = $alias['address'];
673
				}
674
			}
675
		}
676
		return $newaddress;
677
}
678

    
679
/* expand a host or network alias, if necessary */
680
function alias_expand($name) {
681

    
682
	global $aliastable;
683

    
684
	if (isset($aliastable[$name]))
685
		return "\${$name}";
686
	else if (is_ipaddr($name) || is_subnet($name))
687
		return "{$name}";
688
	else
689
		return null;
690
}
691

    
692
/* expand a host alias, if necessary */
693
function alias_expand_host($name) {
694
	global $aliastable;
695

    
696
	if (isset($aliastable[$name])) {
697
		$ip_arr = explode(" ", $aliastable[$name]);
698
		foreach($ip_arr as $ip) {
699
			if (!is_ipaddr($ip))
700
				return null;
701
		}
702
		return $aliastable[$name];
703
	} else if (is_ipaddr($name))
704
		return $name;
705
	else
706
		return null;
707
}
708

    
709
/* expand a network alias, if necessary */
710
function alias_expand_net($name) {
711

    
712
	global $aliastable;
713

    
714
	if (isset($aliastable[$name]) && is_subnet($aliastable[$name]))
715
		return $aliastable[$name];
716
	else if (is_subnet($name))
717
		return $name;
718
	else
719
		return null;
720
}
721

    
722
/* find out whether two subnets overlap */
723
function check_subnets_overlap($subnet1, $bits1, $subnet2, $bits2) {
724

    
725
	if (!is_numeric($bits1))
726
		$bits1 = 32;
727
	if (!is_numeric($bits2))
728
		$bits2 = 32;
729

    
730
	if ($bits1 < $bits2)
731
		$relbits = $bits1;
732
	else
733
		$relbits = $bits2;
734

    
735
	$sn1 = gen_subnet_mask_long($relbits) & ip2long($subnet1);
736
	$sn2 = gen_subnet_mask_long($relbits) & ip2long($subnet2);
737

    
738
	if ($sn1 == $sn2)
739
		return true;
740
	else
741
		return false;
742
}
743

    
744
/* compare two IP addresses */
745
function ipcmp($a, $b) {
746
	if (ip2long($a) < ip2long($b))
747
		return -1;
748
	else if (ip2long($a) > ip2long($b))
749
		return 1;
750
	else
751
		return 0;
752
}
753

    
754
/* return true if $addr is in $subnet, false if not */
755
function ip_in_subnet($addr,$subnet) {
756
	list($ip, $mask) = explode('/', $subnet);
757
	$mask = 0xffffffff << (32 - $mask);
758
	return ((ip2long($addr) & $mask) == (ip2long($ip) & $mask));
759
}
760

    
761
/* verify (and remove) the digital signature on a file - returns 0 if OK */
762
function verify_digital_signature($fname) {
763

    
764
	global $g;
765

    
766
	return mwexec("/usr/local/sbin/gzsig verify {$g['etc_path']}/pubkey.pem < " . escapeshellarg($fname));
767
}
768

    
769
/* obtain MAC address given an IP address by looking at the ARP table */
770
function arp_get_mac_by_ip($ip) {
771
	mwexec("/sbin/ping -c 1 -t 1 {$ip}", true);
772
	$arpoutput = "";
773
	exec("/usr/sbin/arp -n {$ip}", $arpoutput);
774

    
775
	if ($arpoutput[0]) {
776
		$arpi = explode(" ", $arpoutput[0]);
777
		$macaddr = $arpi[3];
778
		if (is_macaddr($macaddr))
779
			return $macaddr;
780
		else
781
			return false;
782
	}
783

    
784
	return false;
785
}
786

    
787
/* return a fieldname that is safe for xml usage */
788
function xml_safe_fieldname($fieldname) {
789
	$replace = array('/', '-', ' ', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
790
			 '_', '+', '=', '{', '}', '[', ']', '|', '/', '<', '>', '?',
791
			 ':', ',', '.', '\'', '\\'
792
		);
793
	return strtolower(str_replace($replace, "", $fieldname));
794
}
795

    
796
function mac_format($clientmac) {
797
    $mac =explode(":", $clientmac);
798

    
799
    global $config;
800

    
801
    $mac_format = $config['captiveportal']['radmac_format'] ? $config['captiveportal']['radmac_format'] : false;
802

    
803
    switch($mac_format) {
804

    
805
        case 'singledash':
806
        return "$mac[0]$mac[1]$mac[2]-$mac[3]$mac[4]$mac[5]";
807

    
808
        case 'ietf':
809
        return "$mac[0]-$mac[1]-$mac[2]-$mac[3]-$mac[4]-$mac[5]";
810

    
811
        case 'cisco':
812
        return "$mac[0]$mac[1].$mac[2]$mac[3].$mac[4]$mac[5]";
813

    
814
        case 'unformatted':
815
        return "$mac[0]$mac[1]$mac[2]$mac[3]$mac[4]$mac[5]";
816

    
817
        default:
818
        return $clientmac;
819
    }
820
}
821

    
822
function resolve_retry($hostname, $retries = 5) {
823

    
824
       if (is_ipaddr($hostname))
825
               return $hostname;
826

    
827
       for ($i = 0; $i < $retries; $i++) {
828
               $ip = gethostbyname($hostname);
829

    
830
               if ($ip && $ip != $hostname) {
831
                       /* success */
832
                       return $ip;
833
               }
834

    
835
               sleep(1);
836
       }
837

    
838
       return false;
839
}
840

    
841
function format_bytes($bytes) {
842
	if ($bytes >= 1073741824) {
843
		return sprintf("%.2f GB", $bytes/1073741824);
844
	} else if ($bytes >= 1048576) {
845
		return sprintf("%.2f MB", $bytes/1048576);
846
	} else if ($bytes >= 1024) {
847
		return sprintf("%.0f KB", $bytes/1024);
848
	} else {
849
		return sprintf("%d bytes", $bytes);
850
	}
851
}
852

    
853
function update_filter_reload_status($text) {
854
        global $g;
855

    
856
        file_put_contents("{$g['varrun_path']}/filter_reload_status", $text);
857
}
858

    
859
/****f* util/return_dir_as_array
860
 * NAME
861
 *   return_dir_as_array - Return a directory's contents as an array.
862
 * INPUTS
863
 *   $dir       - string containing the path to the desired directory.
864
 * RESULT
865
 *   $dir_array - array containing the directory's contents. This array will be empty if the path specified is invalid.
866
 ******/
867
function return_dir_as_array($dir) {
868
        $dir_array = array();
869
        if (is_dir($dir)) {
870
                if ($dh = opendir($dir)) {
871
                        while (($file = readdir($dh)) !== false) {
872
                                $canadd = 0;
873
                                if($file == ".") $canadd = 1;
874
                                if($file == "..") $canadd = 1;
875
                                if($canadd == 0)
876
                                        array_push($dir_array, $file);
877
                        }
878
                        closedir($dh);
879
                }
880
        }
881
        return $dir_array;
882
}
883

    
884
function run_plugins($directory) {
885
        global $config, $g;
886

    
887
        /* process packager manager custom rules */
888
        $files = return_dir_as_array($directory);
889
        if (is_array($files)) {
890
                foreach ($files as $file) {
891
			if (stristr($file, ".sh") == true)
892
				mwexec($directory . $file . " start");
893
			else if (!is_dir($directory . "/" . $file) && stristr($file,".inc")) {
894
				if ($g['booting'] == true)
895
					echo "\t{$file}... ";
896
				require_once($directory . "/" . $file);
897
                        }
898
                }
899
        }
900
}
901

    
902
/*
903
 *    safe_mkdir($path, $mode = 0755)
904
 *    create directory if it doesn't already exist and isn't a file!
905
 */
906
function safe_mkdir($path, $mode=0755) {
907
        global $g;
908

    
909
        if (!is_file($path) && !is_dir($path)) {
910
                return @mkdir($path, $mode);
911
        } else {
912
                return false;
913
        }
914
}
915

    
916
/*
917
 * make_dirs($path, $mode = 0755)
918
 * create directory tree recursively (mkdir -p)
919
 */
920
function make_dirs($path, $mode = 0755) {
921
        $base = '';
922
        foreach (explode('/', $path) as $dir) {
923
                $base .= "/$dir";
924
                if (!is_dir($base)) {
925
                        if (!@mkdir($base, $mode))
926
                                return false;
927
                }
928
        }
929
        return true;
930
}
931

    
932
/*
933
 *     get_memory()
934
 *     returns an array listing the amount of
935
 *     memory installed in the hardware
936
 *     [0]real and [1]available
937
 */
938
function get_memory() {
939
        if(file_exists("/var/log/dmesg.boot")) {
940
                $mem = `cat /var/log/dmesg.boot | grep memory`;
941
                $matches = "";
942
                if (preg_match_all("/real memory  = .* \((.*) MB/", $mem, $matches))
943
                        $real = $matches[1];
944
                if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches))
945
                        $avail = $matches[1];
946
                return array($real[0],$avail[0]);
947
        } else {
948
                $mem = `dmesg -a`;
949
                $matches = "";
950
                if (preg_match_all("/real memory  = .* \((.*) MB/", $mem, $matches))
951
                        $real = $matches[1];
952
                if (preg_match_all("/avail memory = .* \((.*) MB/", $mem, $matches))
953
                        $avail = $matches[1];
954
                return array($real[0],$avail[0]);
955
        }
956
}
957

    
958
function mute_kernel_msgs() {
959
        return;
960
        exec("/sbin/conscontrol mute on");
961
}
962

    
963
function unmute_kernel_msgs() {
964
        exec("/sbin/conscontrol mute off");
965
}
966

    
967
function start_devd() {
968
        exec("/sbin/devd");
969
        sleep(1);
970
        if(file_exists("/tmp/rc.linkup"))
971
                unlink("/tmp/rc.linkup");
972
}
973

    
974
function is_interface_mismatch() {
975
        global $config, $g;
976

    
977
        /* XXX: Should we process only enabled interfaces?! */
978
        $do_assign = false;
979
        $i = 0;
980
        foreach ($config['interfaces'] as $ifname => $ifcfg) {
981
                if (preg_match("/^enc|^tun|^ppp|^pptp|^pppoe|^ovpn|^gif|^gre|^lagg|^bridge|vlan/i", $ifcfg['if'])) {
982
                        $i++;
983
                }
984
                else if (does_interface_exist($ifcfg['if']) == false) {
985
                        file_notice("interfaces", "{$ifcfg['if']} is not present anymore on the system, you need to reassign interfaces or take appropriate actions.", "System", "", 2);
986
                        $do_assign = true;
987
                } else
988
                        $i++;
989
        }
990

    
991
        if ($g['minimum_nic_count'] > $i) {
992
                file_notice("interfaces", "Minimum allowed interfaces is set to {$g['minimum_nic_count']} but system has only {$i} interfaces!", "", "System", 2);
993
                $do_assign = true;
994
        } else if (file_exists("{$g['tmp_path']}/assign_complete"))
995
                $do_assign = false;
996

    
997
        return $do_assign;
998
}
999

    
1000
/* sync carp entries to other firewalls */
1001
function carp_sync_client() {
1002
	global $g;
1003
	update_filter_reload_status("Syncing CARP data");
1004
	touch($g['tmp_path'] . "/filter_sync"); 
1005
}
1006

    
1007
/****f* util/isAjax
1008
 * NAME
1009
 *   isAjax - reports if the request is driven from prototype
1010
 * INPUTS
1011
 *   none
1012
 * RESULT
1013
 *   true/false
1014
 ******/
1015
function isAjax() {
1016
        return isset ($_SERVER['HTTP_X_REQUESTED_WITH']) && $_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest';
1017
}
1018

    
1019

    
1020
?>
(35-35/42)