Source for file funcs.inc.php
Documentation is available at funcs.inc.php
* /opt/nac/bin/funcs.inc.php
* common PHP functions used by several scripts
* LICENSE: This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as published
* by the Free Software Foundation.
* @author Sean Boran (FreeNAC Core Team)
* @copyright 2007 FreeNAC
* @license http://www.gnu.org/copyleft/gpl.html GNU Public License Version 2
* @link http://www.freenac.net
* Common PHP functions used by several scripts
* Load automagically a file containing the class specified by classname
* @param object $classname Class to load
require_once "../lib/$classname.php";
require_once "../enterprise/$classname.php";
* Get configuration variables from config file
require_once 'etc/config.inc';
* @global object $conf Contains configuration parameters from the config table
* @global object $logger Provides for logging facilities
* Tell if an IPv4 address is valid (well-formed)
* @param string $ip IP address to test
* return boolean True if IP address is valid, false otherwise
if (($sub <
1) ||
($sub >
255))
* Get WINS Name from IP Address
* Original contribution from johnboy68
* @param string $ip Ip Address
* @return string WINS Name
#Try to avoid command injection
added interface ip=192.168.201.216 bcast=192.168.201.255 nmask=255.255.255.0
Looking up status of 192.168.202.222
WORK <00> - <GROUP> M <ACTIVE>
WORK <1e> - <GROUP> M <ACTIVE>
..__MSBROWSE__. <01> - <GROUP> M <ACTIVE>
MAC Address = CC-00-FF-EE-EE-EE
added interface ip=192.168.201.216 bcast=192.168.201.255 nmask=255.255.255.0
Looking up status of 192.168.202.223
No reply from 192.168.202.223
Case where we are trying to lookup the hostname of the server running this script
added interface ip=192.168.201.216 bcast=192.168.201.255 nmask=255.255.255.0
Looking up status of 192.168.202.216
No reply from 192.168.202.216
#Call nmblookup for this ip address
$command =
"nmblookup -A $ip";
$foutput =
explode("$ip",$output); // all after the IP
$foutput =
explode(" ",$foutput[1]); // fields sep. by spaces
$foutput =
trim($foutput[0]); // get first filed, i.e. STNS59 above
# Check if we have a result;
#No result, return IP address
else if (strpos($ip,"bcast="))
#Return the hostname we've just learnt
* Converts a vlan id to a vlan name
* @param integer $vlanID Vlan ID
* @return mixed Vlan name
// Todo: Proper Error Handling, and use better Database abstraction
$vlan_name=
v_sql_1_select("select default_name from vlan where id='$vlanID' limit 1");
if ($vlan_name !==
false)
* Get the last number of an SNMP OID
* The OID is separated by dots and we use them as a separator.
* Example: OID=1.2.3.4.5.6.7.8
* @param mixed $oid OID of interest
* @return mixed Last part of the OID.
return $temp[count($temp)-
1];
* Returns the difference between 2 dates in secs
* @param mixed $date1 Date to substract from
* @param mixed $date2 Date
* @return mixed Difference in second between those 2 dates
//Return false if there are no spaces in $date1
$time_info_1=
explode(':',$temp[1]);
//Return false because the date format is not what we expected
if ( strcmp($time_info_1[0],$temp[1]) ===
0 )
$date_info_1=
explode('-',$temp[0]);
//Return false because the date format is not what we expected
if ( strcmp($date_info_1[0], $temp[0]) ===
0 )
//Return false if there are no spaces in $date2
$time_info_2=
explode(':',$temp[1]);
//Return false because the date format is not what we expected
if ( strcmp($time_info_2[0],$temp[1]) ===
0 )
$date_info_2=
explode('-',$temp[0]);
//Return false because the date format is not what we expected
if ( strcmp($date_info_2[0], $temp[0]) ===
0 )
$time1=
mktime((int)
$time_info_1[0],(int)
$time_info_1[1],(int)
$time_info_1[2],(int)
$date_info_1[1],(int)
$date_info_1[2],(int)
$date_info_1[0]);
$time2=
mktime((int)
$time_info_2[0],(int)
$time_info_2[1],(int)
$time_info_2[2],(int)
$date_info_2[1],(int)
$date_info_2[2],(int)
$date_info_2[0]);
if ( ($time1 !==
false) &&
($time2 !==
false) )
//No dates were specified
* Wrapper around the debug method part of the logger object.
* It will be soon depreciated. Present only for backwards compatibility.
* @param mixed $msg Message to log
* Wrapper around the debug method part of the logger object.
* It will be soon depreciated. Present only for backwards compatibility.
* @param mixed $msg Message to log
* Wrapper around the debug method part of the logger object.
* It will be soon depreciated. Present only for backwards compatibility.
* @param mixed $msg Message to log
* Wrapper around the logit method part of the logger object.
* It will be soon depreciated. Present only for backwards compatibility.
* @param mixed $msg Message to log
* Write key events to naclog which is visible from the GUI
* This should NOT be called from a secondary server, i.e.
* avoid it in vmpsd_external
* @param mixed $level Level of severity of the message
* @param mixed $msg Message to log
#$query="INSERT DELAYED INTO naclog "
if (isset
($_SERVER['HOSTNAME']))
$host=
$_SERVER['HOSTNAME'];
#$query="INSERT INTO naclog "
# . "SET what='" . mysql_real_escape_string($msg ) . "', "
# . "host='" . mysql_real_escape_string($_SERVER["HOSTNAME"]) . "', "
# . "priority='$level' ";
#$logger->logit("$query\n");
// To view recent entries:
// select * from naclog ORDER BY datetime DESC LIMIT 5;
## log2db3: write to naclog if debug level=3
global $connect, $logger;
if (($logger->getDebugLevel()==
3) &&
(strlen($msg)>
0) ) {
#$query="INSERT INTO naclog "
$query=
"INSERT DELAYED INTO naclog "
.
"SET what='" .
$msg .
"', "
.
"priority='" .
$level .
"' ";
#$logger->logit("$query\n");
* Creates a connection to the MySQL database with the parameters defined in config.inc
global $connect, $dbhost, $dbuser, $dbpass, $dbname, $logger;
* Abstract calling of unix commands.
* Problem: popen does not pass back command success
* so syscall cannot say if the command works.
* @param mixed $command Command to be executed
* @return mixed Result from that command
#if ( $proc = popen("($command) ","r") ) {
if ( $proc =
popen("($command) 2>&1","r") ) {
$result .=
fgets($proc, 1000);
#debug2("syscall(): executed $command, RETURN=$result");
#} else { # will never be reaches, popen does not pass back command success
# logit("syscall error ", $proc);
# Return: true=Ping successful
global $connect, $logger;
$query=
"SELECT r_ip from systems "
.
" WHERE mac='" .
$mac .
"'";
#$logger->logit("$query\n");
debug2("Invalid IP - $ip for mac $mac");
#debug2("ping $ip - $mac");
logit("ping $ip - $mac");
// ping for max 1 sec, make sure it does not sty running/hung
#$answer=syscall("ping -c 1 -w 1 $ip");
$answer=
syscall("ping -c 3 -w 1 $ip");
#$logger->logit "Ping Error: $answer\n";
logit("Ping Error no answer: $answer");
} else if ( preg_match("/\d+ received,/m", $answer) ) {
#$logger->logit "Ping OK\n";
logit("Ping OK: $answer");
#$logger->logit "Ping Error: $answer\n";
logit("Ping Error: $answer");
logit("No IP found for $mac");
* Perform a case insensitive search for a given value in an array and return its key
* @param mixed $str Value to look for
* @param array $array Array to look in
* @return mixed Key for that value, or false otherwise
foreach($array as $k =>
$v)
* Perform a case insensitive search for a given value in a bi-dimensional array and return its key
* @param mixed $str Value to look for
* @param array $array Array to look in
* @return mixed Key for that value, or false otherwise
* Search the array for a given key and return its value, but using tokenizers
* @param mixed $str String to look for
* @param array $array Array where we should look in
* @param mixed $token Token to use as a separator
* @param integer $number The number of parts we want to return
* @return mixed Desired value or false otherwise
if ($str &&
$array &&
$token &&
$number)
#What if we have a string without tokens?
#Add the token to the beginning of the string
#so we cover that case without affecting the strings
foreach($array as $k =>
$v)
* Search the array for a given value and return it, but using tokenizers
* @param mixed $str String to look for
* @param array $array Array where we should look in
* @param mixed $token Token to use as a separator
* @param integer $number The number of parts we want to return
* @return mixed Desired value or false otherwise
if ($str &&
$array &&
$token &&
$number)
foreach($array as $k =>
$v)
* Return the last parts of a tokenized string
* @param $string String to split
* @param $token Token to use to split the string
* @param $number How many parts we want to return
* @return mixed Desired string