function save() { $_POST["mac"] = str_replace("-", ":", $_POST["mac"]); $_POST["mac"] = strtolower($_POST["mac"]); $ipClass = new IP(); if (!$ipClass->IsvalidMAC($_POST["mac"])) { echo "MAC: {$_POST["mac"]} Invalid!\n"; return; } if (!$ipClass->isValid($_POST["ipaddr"])) { echo "MAC: {$_POST["ipaddr"]} Invalid!\n"; return; } $cmp = new computers(); $uid = $cmp->ComputerIDFromMAC($_POST["mac"]); if ($uid != null) { $cmp = new computers($uid); } if ($uid == null) { $uid = "{$_POST["hostname"]}\$"; } $cmp->uid = $uid; $cmp->ComputerIP = $_POST["ipaddr"]; $cmp->ComputerMacAddress = $_POST["mac"]; $cmp->ComputerRealName = $_POST["hostname"]; if ($cmp->Add()) { echo $cmp->ldap_error; } }
function Save() { $comp = new computers(); $comp->uid = $_POST["computername"] . "\$"; $comp->ComputerRealName = $_POST["computername"]; $comp->ComputerIP = $_POST["ipaddr"]; $comp->ComputerMacAddress = $_POST["MAC"]; $comp->Add(); }
function SimpleShareAddComputerLDAP() { $tpl = new templates(); $computer = $_GET["computername_add"]; $ip = $_GET["computerip_add"]; if ($computer == null) { echo $tpl->_ENGINE_parse_body("{computer_name} = NULL"); exit; } if ($ip == null) { echo $tpl->_ENGINE_parse_body("{ip_address} = NULL"); exit; } $uid = "{$computer}\$"; $comp = new computers($uid); $comp->ComputerIP = $ip; $comp->ComputerRealName = $computer; if (!$comp->Add()) { echo @implode("\n", $GLOBALS["INJECT_COMPUTER_TOLDAP"]); return; } }
function importcomputersFromList(){ cpulimit(20); $sock=new sockets(); $tbl=explode("\n",$sock->GET_INFO("ComputerListToImport")); writelogs("ComputerListToImport=" . count($tbl)." values",__FUNCTION__,__FILE__,__LINE__); $i=0; $max=count($tbl); while (list ($num, $computername) = each ($tbl)){ $z=$z+1; $computername=trim($computername); $ip=null; $mac=null; if($computername==null){continue;} $ip_arp=unserialize(base64_decode($sock->getFrameWork("cmd.php?arp-ip=".$_GET["arp-ip"]))); if(is_array($ip_arp)){ $ip=$ip_arp[0]; $mac=$ip_arp[1]; unset($ip_arp); } $pourc=round(($z/$max)*100); writelogs("$pourc) $computername",__FUNCTION__,__FILE__,__LINE__); WriteCOmputerBrowseProgress($pourc,"{import}: $computername ($ip/$mac)"); if($mac<>null){$uid=$cmp->ComputerIDFromMAC($mac);}else{$uid="$computername$";} if($uid==null){$uid="$computername$";} $cmp=new computers($uid); if($ip<>null){$cmp->ComputerIP=$ip;} if($mac<>null){$cmp->ComputerMacAddress=$mac;} $cmp->ComputerRealName=$computername; $cmp->Add(); $i=$i+1; } WriteCOmputerBrowseProgress(0,"{waiting}"); }
function COMPUTER_SAVE_INFOS() { $tpl = new templates(); if (preg_match("#newcomputer#", $_GET["uid"])) { echo $tpl->_ENGINE_parse_body('ERROR:{give_computer_name}'); exit; } if ($_GET["add_computer_form"]) { if ($_GET["ComputerMacAddress"] != null) { $comp = new computers(); $uidfound = $comp->ComputerIDFromMAC($_GET["ComputerMacAddress"]); if (trim($uidfound) != null) { if ($uidfound != $_GET["uid"] . '$') { echo $tpl->javascript_parse_text("{this_mac_address_is_already_used_by}:{$uidfound}"); return; } } } } $computer = new computers($_GET["userid"]); $computer->uid = $_GET["uid"] . '$'; $computer->ComputerMacAddress = $_GET["ComputerMacAddress"]; $computer->ComputerIP = $_GET["ComputerIP"]; $computer->DnsZoneName = $_GET["DnsZoneName"]; $computer->ComputerCPU = $_GET["ComputerCPU"]; $computer->DnsType = $_GET["DnsType"]; $computer->DnsMXLength = $_GET["DnsMXLength"]; $computer->dhcpfixed = $_GET["dhcpfixed"]; $computer->VolatileIPAddress = $_GET["VolatileIPAddress"]; if ($_GET["userid"] == "newcomputer\$") { if (!$computer->Add()) { echo "ERROR:{$computer->ldap_error}"; exit; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); if (isset($_GET["gpid"])) { writelogs("adding computer to group {$_GET["gpid"]}", __FUNCTION__, __FILE__, __LINE__); $group = new groups($_GET["gpid"]); $group->AddUsertoThisGroup($computer->uid); exit; } exit; } } if (!$computer->Edit()) { echo $computer->ldap_error; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); } }
function nmap_scan_results() { if (!is_file("/etc/artica-postfix/nmap.map")) { return; } $f = explode("\n", @file_get_contents("/etc/artica-postfix/nmap.map")); while (list($index, $ligne) = each($f)) { if (preg_match("#Nmap scan report for\\s+(.+?)\\s+\\(([0-9\\.]+)#", $ligne, $re)) { $ipaddr = $re[2]; $computer[$ipaddr]["IPADDR"] = $re[2]; $computer[$ipaddr]["HOSTNAME"] = trim($re[1]); $LOGS[] = "Found {$ipaddr} hostname= {$re[1]}"; continue; } if (preg_match("#Nmap scan report for ([0-9\\.]+)\$#", trim($ligne), $re)) { $ipaddr = $re[1]; $computer[$ipaddr]["IPADDR"] = $re[1]; $LOGS[] = "Found {$ipaddr} without computername "; continue; } if (preg_match("#^MAC Address:\\s+([0-9A-Z:]+)\$#", trim($ligne), $re)) { if (isset($MACSSCAN[trim($re[1])])) { continue; } $computer[$ipaddr]["MAC"] = trim($re[1]); $LOGS[] = "Found {$ipaddr} with mac {$re[1]} "; $MACSSCAN[trim($re[1])] = true; continue; } if (preg_match("#^MAC Address:(.+).+?\\((.+?)\\)#", $ligne, $re)) { if (isset($MACSSCAN[trim($re[1])])) { continue; } $MACSSCAN[trim($re[1])] = true; $computer[$ipaddr]["MAC"] = trim($re[1]); $computer[$ipaddr]["MACHINE_TYPE"] = trim($re[2]); $LOGS[] = "Found {$ipaddr} with mac {$re[1]} and machine type {$re[2]}"; continue; } if (preg_match("#^Running:(.+)#", $ligne, $re)) { $computer[$ipaddr]["RUNNING"] = trim($re[1]); continue; } if (preg_match("#^OS details:(.+)#", $ligne, $re)) { $LOGS[] = "Found {$ipaddr} with OS {$re[1]}"; $computer[$ipaddr]["OS"] = trim($re[1]); continue; } } nmap_logs(count($f) . " analyzed lines", @implode("\n", $LOGS)); $c = 0; while (list($ipaddr, $array) = each($computer)) { if (isset($already[$mac])) { continue; } $mac = trim($array["MAC"]); if ($mac == null) { continue; } $c++; $already[$mac] = true; $ldap_ipaddr = null; $ComputerRealName = null; $uid = null; $RAISON = array(); if (!isset($array["HOSTNAME"])) { $array["HOSTNAME"] = null; } if (!isset($array["OS"])) { $array["OS"] = null; } if (!isset($array["RUNNING"])) { $array["RUNNING"] = null; } if (!isset($array["MACHINE_TYPE"])) { $array["MACHINE_TYPE"] = null; } $cmp = new computers(null); $uid = $cmp->ComputerIDFromMAC($mac); if ($uid != null) { if ($GLOBALS["VERBOSE"]) { echo "{$mac} = {$uid}\n"; } $cmp = new computers($uid); $ldap_ipaddr = $cmp->ComputerIP; $ComputerRealName = $cmp->ComputerRealName; if ($GLOBALS["VERBOSE"]) { echo "{$mac} = {$uid}\nLDAP:{$ldap_ipaddr}<>NMAP:{$ipaddr}\nLDAP CMP:{$ComputerRealName}<>NMAP:{$array["HOSTNAME"]}"; } if ($array["HOSTNAME"] != null) { $EXPECTED_UID = strtoupper($array["HOSTNAME"]) . "\$"; if ($EXPECTED_UID != $uid) { $RAISON[] = "UID: {$uid} is different from {$EXPECTED_UID}"; nmap_logs("EDIT UID: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); $cmp->update_uid($EXPECTED_UID); } } if ($ldap_ipaddr != $ipaddr) { writelogs("Change {$ldap_ipaddr} -> to {$ipaddr} for {$cmp->uid}", __FUNCTION__, __FILE__, __LINE__); $RAISON[] = "LDAP IP ADDR: {$ldap_ipaddr} is different from {$ipaddr}"; $RAISON[] = "DN: {$cmp->dn}"; $RAISON[] = "UID: {$cmp->uid}"; $RAISON[] = "MAC: {$cmp->ComputerMacAddress}"; if (!$cmp->update_ipaddr($ipaddr)) { $RAISON[] = "ERROR:{$cmp->ldap_last_error}"; } nmap_logs("EDIT IP: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); } if ($array["OS"] != null) { if (strtolower($cmp->ComputerOS == "Unknown")) { $cmp->ComputerOS = null; } if ($cmp->ComputerOS == null) { $RAISON[] = "LDAP OS: {$cmp->ComputerOS} is different from {$array["OS"]}"; nmap_logs("EDIT OS: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); $cmp->update_OS($array["OS"]); } } } else { if ($array["HOSTNAME"] != null) { $uid = "{$array["HOSTNAME"]}\$"; } else { $uid = "{$ipaddr}\$"; } nmap_logs("ADD NEW: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), "{$uid}"); $cmp = new computers(); $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; $cmp->uid = "{$uid}"; $cmp->ComputerOS = $array["OS"]; $cmp->ComputerRunning = $array["RUNNING"]; $cmp->ComputerMachineType = $array["MACHINE_TYPE"]; $cmp->Add(); } } nmap_logs("{$c} hosts analyzed in databases"); @unlink("/etc/artica-postfix/nmap.map"); //print_r($computer); }
function scanarp() { $GLOBALS["CLASS_USERS"] = new usersMenus(); $GLOBALS["CLASS_SOCKETS"] = new sockets(); if (!$GLOBALS["CLASS_USERS"]->ARPD_INSTALLED) { if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . " ARPD_INSTALLED = FALSE\n"; } return; } $EnableArpDaemon = $GLOBALS["CLASS_SOCKETS"]->GET_INFO("EnableArpDaemon"); if (!is_numeric($EnableArpDaemon)) { $EnableArpDaemon = 1; } if ($EnableArpDaemon == 0) { if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . " EnableArpDaemon = {$EnableArpDaemon}\n"; } return; } $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid"; $unix = new unix(); if ($unix->process_exists(@file_get_contents($pidfile))) { if ($GLOBALS["VERBOSE"]) { echo " --> Already executed.. " . @file_get_contents($pidfile) . " aborting the process\n"; } events(basename(__FILE__) . ":Already executed.. aborting the process", __FUNCTION__, __LINE__); die; } @file_put_contents($pidfile, getmypid()); if (!is_file("/var/lib/arpd/arpd.db")) { die; } $GLOBALS["CLASS_UNIX"] = $unix; $GLOBALS["nmblookup"] = $unix->find_program("nmblookup"); $GLOBALS["arpd"] = $unix->find_program("arpd"); $GLOBALS["arp"] = $unix->find_program("arp"); $GLOBALS["ARP_DB"] = "/var/lib/arpd/arpd.db"; $GLOBALS["CACHE_DB"] = "/etc/artica-postfix/arpd.cache"; $ArpdArray = unserialize(base64_decode(@file_get_contents($GLOBALS["CACHE_DB"]))); if ($GLOBALS["FLUSH"]) { $ArpdArray = array(); } if (!is_array($ArpdArray)) { $ArpdArray = array(); } if (!isset($ArpdArray["LAST"])) { $ArpdArray["LAST"] = 0; } $last_modified = filemtime($GLOBALS["ARP_DB"]); $TimeArpd = $ArpdArray["LAST"]; if ($TimeArpd == $last_modified) { events("{$TimeArpd} -> {$last_modified} No modification time", __FUNCTION__, __LINE__); return; } events("Scanning ARP table....", __FUNCTION__, __LINE__); $ArpdArray["LAST"] = $last_modified; exec("{$GLOBALS["arpd"]} -l 2>&1", $results); events("{$GLOBALS["arpd"]} -l return " . count($results) . " element(s)", __FUNCTION__, __LINE__); while (list($num, $ligne) = each($results)) { if (preg_match("#unexpected file type or format#", $ligne)) { @unlink($GLOBALS["ARP_DB"]); @unlink($GLOBALS["CACHE_DB"]); shell_exec("/etc/init.d/arpd restart"); die; } if (!preg_match("#^[0-9]+\\s+\\s+(.+?)\\s+(.+)#", $ligne, $re)) { if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, unexpected line..\n"; } continue; } if (preg_match("#FAILED:#", $re[2])) { continue; } $mac = $re[2]; $ipaddr = $re[1]; if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, MAC:{$mac} -> {$ipaddr}\n"; } if (isset($ArpdArray["MACS"][$mac])) { if ($GLOBALS["VERBOSE"]) { echo "MAC:{$mac} Already cached, aborting....\n"; } continue; } $ArpdArray["MACS"][$mac] = true; $cmp = new computers(); $uid = $cmp->ComputerIDFromMAC($mac); if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, MAC:{$mac} -> {$uid}\n"; } if ($uid == null) { $res2 = array(); $computer_name = null; events("It is time to add {$mac}/{$ipaddr} in database", __FUNCTION__, __LINE__); exec("{$GLOBALS["arp"]} -a {$ipaddr} 2>&1", $res2); if (preg_match("#^(.+?)\\s+\\(#", trim(@implode("", $res2)), $rz)) { $computer_name = $rz[1]; } if (strlen($computer_name) < 3) { $computer_name = $ipaddr; } $cmp->uid = "{$computer_name}\$"; $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; $cmp->Add(); } else { if ($GLOBALS["FLUSH"]) { $res2 = array(); $cmp = new computers($uid); $computer_name = null; events("It is time to edit {$uid}/{$mac}/{$ipaddr} in database", __FUNCTION__, __LINE__); exec("{$GLOBALS["arp"]} -a {$ipaddr} 2>&1", $res2); if ($GLOBALS["VERBOSE"]) { echo "{$GLOBALS["arp"]} -a {$ipaddr} 2>&1 = >" . trim(@implode("", $res2)); } if (preg_match("#^(.+?)\\s+\\(#", trim(@implode("", $res2)), $rz)) { $computer_name = $rz[1]; } else { if ($GLOBALS["VERBOSE"]) { echo "Unable to find computer name\n"; } } if (strlen($computer_name) < 3) { $computer_name = $ipaddr; } if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, UID:{$mac} -> {$uid}\n"; } if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, NAME:{$computer_name} -> {$uid}\n"; } $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; $cmp->Add(); } } } @file_put_contents($GLOBALS["CACHE_DB"], base64_encode(serialize($ArpdArray))); }
function COMPUTER_SAVE_INFOS() { $tpl = new templates(); if (preg_match("#newcomputer#", $_GET["uid"])) { echo $tpl->_ENGINE_parse_body('ERROR:{give_computer_name}'); exit; } if ($_GET["add_computer_form"]) { if ($_GET["ComputerMacAddress"] != null) { $comp = new computers(); $uidfound = $comp->ComputerIDFromMAC($_GET["ComputerMacAddress"]); if (trim($uidfound) != null) { if ($uidfound != $_GET["uid"] . '$') { echo $tpl->javascript_parse_text("{this_mac_address_is_already_used_by}:{$uidfound}"); return; } } } } $_GET["uid"] = str_replace("%24", "\$", $_GET["uid"]); $_GET["uid"] = str_replace("\$\$", "\$", $_GET["uid"]); $computer = new computers($_GET["userid"]); $fIP = explode(".", $_GET["ComputerIP"]); while (list($index, $val) = each($fIP)) { $fIP[$index] = intval($val); } $_GET["ComputerIP"] = @implode(".", $fIP); $computer->uid = $_GET["uid"] . '$'; $computer->ComputerMacAddress = $_GET["ComputerMacAddress"]; $computer->ComputerIP = $_GET["ComputerIP"]; if (isset($_GET["DnsZoneName"])) { $computer->DnsZoneName = $_GET["DnsZoneName"]; } if (isset($_GET["ComputerCPU"])) { $computer->ComputerCPU = $_GET["ComputerCPU"]; } if (isset($_GET["DnsType"])) { $computer->DnsType = $_GET["DnsType"]; } if (isset($_GET["DnsMXLength"])) { $computer->DnsMXLength = $_GET["DnsMXLength"]; } if (isset($_GET["ComputerCPU"])) { $computer->ComputerCPU = $_GET["ComputerCPU"]; } if (isset($_GET["ComputerOS"])) { $computer->ComputerOS = $_GET["ComputerOS"]; } if (isset($_GET["ComputerMachineType"])) { $computer->ComputerMachineType = $_GET["ComputerMachineType"]; } if ($_GET["userid"] == "newcomputer\$") { if (!$computer->Add()) { echo "ERROR:{$computer->ldap_error}"; exit; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); if (isset($_GET["gpid"])) { writelogs("adding computer to group {$_GET["gpid"]}", __FUNCTION__, __FILE__, __LINE__); $group = new groups($_GET["gpid"]); $group->AddUsertoThisGroup($computer->uid); exit; } exit; } } if (!$computer->Edit()) { writelogs("!!FAILED updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); echo $computer->ldap_error; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); } }
function AutomaticInjection() { $users = new usersMenus(); if (!$users->OCSI_INSTALLED) { return; } $file = "/etc/artica-postfix/cron.2/AutomaticInjection.pid"; $sock = new sockets(); $OCSImportToLdap = $sock->GET_INFO("OCSImportToLdap"); if ($OCSImportToLdap == null) { $OCSImportToLdap = 60; } if ($OCSImportToLdap == 0) { return; } if (!$GLOBALS["FORCE"]) { $filetime = file_time_min($file); if ($GLOBALS["VERBOSE"]) { echo "{$file}={$filetime} against {$OCSImportToLdap} minutes\n"; } if (!$GLOBALS["VERBOSE"]) { if ($filetime < $OCSImportToLdap) { return; } } } writelogs("Starting OCS injection from OCS database", __FUNCTION__, __FILE__, __LINE__); $ocs = new ocs(); $sql = $ocs->COMPUTER_SEARCH_QUERY(null, 1); if ($GLOBALS["VERBOSE"]) { echo $sql . "\n"; } $q = new mysql(); $results = $q->QUERY_SQL($sql, "ocsweb"); while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) { if ($ligne["IPADDRESS"] == "0.0.0.0") { continue; } if ($ligne["MACADDR"] == "00:00:00:00:00:00") { continue; } if ($already[$ligne["MACADDR"]]) { continue; } $already[$ligne["MACADDR"]] = true; $f = new computers(); $uid = $f->ComputerIDFromMAC($ligne["MACADDR"]); if ($GLOBALS["VERBOSE"]) { echo "Check {$ligne["MACADDR"]} against {$uid}\n"; } if ($uid == null) { writelogs("uid is null for {$ligne["MACADDR"]}, add it into LDAP", __FUNCTION__, __FILE__, __LINE__); AutomaticInjectionAdd($ligne["MACADDR"]); continue; } $update = false; $f = new computers($uid); if ($GLOBALS["VERBOSE"]) { echo "Checking uid={$uid} NAME={$f->ComputerRealName}; IP={$f->ComputerIP}; OS={$f->ComputerOS}\n"; } $ComputerIP = trim($f->ComputerIP); $ComputerOS = trim($f->ComputerOS); $OSNAME = trim(utf8_encode($ligne["OSNAME"])); $PROCESSORT = trim($ligne["PROCESSORT"]); if ($GLOBALS["VERBOSE"]) { echo "OCS SOURCE {$ligne["MACADDR"]} IP={$ligne["IPSRC"]}; OS={$OSNAME} CPU={$PROCESSORT}\n"; } if ($ComputerIP != $ligne["IPSRC"]) { $f->ComputerIP = $ligne["IPSRC"]; if ($GLOBALS["VERBOSE"]) { echo "IPSRC not match ({$ComputerIP})\n"; } $update = true; } if ($PROCESSORT != null) { if (trim($f->ComputerCPU) != $ligne["PROCESSORT"]) { if ($GLOBALS["VERBOSE"]) { echo "PROCESSORT not match\n"; } $f->ComputerOS = $ligne["PROCESSORT"]; $update = true; } } if ($update) { echo "update {$ligne["MACADDR"]}\n"; $f->Add(); } } @unlink($file); @file_put_contents($file, getmypid()); writelogs("Finish OCS injection from OCS database", __FUNCTION__, __FILE__, __LINE__); }
function update_computer($ip, $mac, $name) { $sock = new sockets(); $ComputersAllowDHCPLeases = $sock->GET_INFO("ComputersAllowDHCPLeases"); if ($ComputersAllowDHCPLeases == null) { $ComputersAllowDHCPLeases = 1; } if ($ComputersAllowDHCPLeases == 0) { writelogs("ComputersAllowDHCPLeases is disabled, aborting...", "update_computer", __FILE__, __LINE__); die; } $mac = trim($mac); $name = trim(strtolower($name)); $ip = trim($ip); if ($ip == null) { return; } if ($mac == null) { return; } if ($name == null) { return; } $ip = nmblookup($name, $ip); $unix = new unix(); $unix->add_EtcHosts($name, $ip); $dhcp = new dhcpd(); $GLOBALS["domain"] = $dhcp->ddns_domainname; $comp = new computers(); $uid = $comp->ComputerIDFromMAC($mac); if ($uid == null) { $add = true; $uid = "{$name}\$"; $comp = new computers(); $comp->ComputerRealName = $name; $comp->ComputerMacAddress = $mac; $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; $comp->uid = $uid; $ComputerRealName = $HOST; $comp->Add(); } else { $comp = new computers($uid); if ($comp->ComputerRealName == null) { $ComputerRealName = $name; } if (preg_match("#[0-9]+\\.[0-9]+\\.#", $comp->ComputerRealName)) { $comp->ComputerRealName = $name; } $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; $comp->Edit(); } $dns = new pdns($GLOBALS["domain"]); $dns->EditIPName(strtolower($name), $ip, 'A', $mac); }
function importcomputersFromList() { $unix = new unix(); if (!is_file("/usr/bin/arp-scan")) { $unix->DEBIAN_INSTALL_PACKAGE("arp-scan"); } $sock = new sockets(); $ipClass = new IP(); $tbl = explode("\n", $sock->GET_INFO("ComputerListToImport")); $CountOfLines = count($tbl); build_progress("{$CountOfLines} {computers}", 10); echo "[" . __LINE__ . "] {$CountOfLines} lines\n"; $i = 0; $z = 0; $max = $CountOfLines; $FAILED = 0; $SUCCESS = 0; while (list($num, $line) = each($tbl)) { $z++; $prc = $z / $CountOfLines * 100; $prc = round($prc); if ($prc < 10) { $prc = 10; } if ($prc > 90) { $prc = 90; } $proxy_alias = null; $computername = null; $IPADDR = null; $MAC = null; //pc001,192.168.1.5,d8:9e:3f:34:2d:8d,jhon_pc[br] $EXPLODED = explode(",", $line); $computername = $EXPLODED[0]; $IPADDR = $EXPLODED[1]; $MAC = $EXPLODED[2]; $MAC = str_replace("-", ":", $MAC); $MAC = strtolower($MAC); if (isset($EXPLODED[3])) { $proxy_alias = trim(strtolower($EXPLODED[3])); } if ($proxy_alias != null) { proxy_alias_add($IPADDR, $MAC, $proxy_alias); } $computername = trim($computername); if ($MAC == null) { $MAC = arp_scan_IpToMac($IPADDR); } if ($computername == null) { echo "[" . __LINE__ . "] Computer Name is null, aborting\n"; $FAILED++; continue; } if (!$ipClass->isValid($IPADDR)) { $IPADDR = null; } if (!$ipClass->IsvalidMAC($MAC)) { $MAC = null; } build_progress("{$computername} {$IPADDR}/{$MAC}/{$proxy_alias}", $prc); $cmp = new computers(); if ($MAC != null) { $uid = $cmp->ComputerIDFromMAC($MAC); } else { $uid = "{$computername}\$"; } if ($uid == null) { $uid = "{$computername}\$"; } if ($IPADDR == null) { echo "Try to resolve {$computername}\n"; $IPADDR = @gethostbyname($computername); if (!$ipClass->isValid($IPADDR)) { $IPADDR = null; } } $cmp = new computers($uid); if ($IPADDR != null) { $cmp->ComputerIP = $IPADDR; } if ($MAC != null) { $cmp->ComputerMacAddress = $MAC; } $cmp->ComputerRealName = $computername; if (!$cmp->Add()) { echo "{$computername}: {$cmp->ldap_error}\n"; $FAILED++; } else { $SUCCESS++; } $i = $i + 1; } echo "Success: {$SUCCESS}\n"; echo "Failed : {$FAILED}\n"; build_progress("{$SUCCESS} {added_computers}", 95); sleep(10); build_progress("{done}", 100); }
<?php include_once dirname(__FILE__) . '/ressources/class.ldap.inc'; include_once dirname(__FILE__) . '/ressources/class.computers.inc'; writelogs("user executed : " . posix_getuid(), __FUNCTION__, __FILE__, __LINE__); if (posix_getuid() != 0) { write_syslog("requested has " . posix_getuid() . " uid \"{$argv[1]}\" but Cannot be used in web server mode", __FILE__); die; } $log = false; $username = $argv[1]; $verbose = $argv[2]; if ($verbose == '--verbose') { $_GET["log"] = true; } if (trim($username) == null) { die; } $GLOBALS["ADDLOG"] = "/usr/share/artica-postfix/ressources/logs/web/samba-add-computer.log"; writelogs("Adding new computer {$username}", __FUNCTION__, __FILE__, __LINE__); $computer = new computers($username); if ($computer->Add()) { write_syslog("Success adding new computer {$username}", __FILE__); } else { write_syslog("Failed adding new computer {$username}", __FILE__); }
function popup_connected_add() { $q = new mysql(); $sql = "SELECT HARDWARE_ID FROM networks WHERE MACADDR='{$_GET["connected-mac"]}'"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "ocsweb")); if ($ligne["HARDWARE_ID"] < 1) { echo "{corrupted_database}"; return; } $HARDWARE_ID = $ligne["HARDWARE_ID"]; $sql = "SELECT * FROM hardware WHERE ID='{$HARDWARE_ID}'"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "ocsweb")); $cp = new computers(); $cp->ComputerIP = $ligne["IPSRC"]; $cp->ComputerMacAddress = $_GET["connected-mac"]; $cp->ComputerOS = $ligne["OSNAME"]; $cp->ComputerCPU = $ligne["PROCESSORT"]; $cp->uid = $ligne["NAME"]; $sql = "SELECT * FROM bios WHERE HARDWARE_ID='{$HARDWARE_ID}'"; $ligne = mysql_fetch_array($q->QUERY_SQL($sql, "ocsweb")); $cp->ComputerMachineType = trim("{$ligne["SMODEL"]} {$ligne["SMANUFACTURER"]}"); $cp->Add(); }
function nmap_scan_results() { if (!is_file("/etc/artica-postfix/nmap.map")) { return; } $f = explode("\n", @file_get_contents("/etc/artica-postfix/nmap.map")); $ipaddr = null; $computer = array(); while (list($index, $ligne) = each($f)) { $ligne = trim($ligne); if ($ligne == null) { continue; } if ($ligne == "PORT STATE SERVICE") { continue; } if (strpos(" {$ligne}", "Network Distance:") > 0) { continue; } if (strpos(" {$ligne}", "tcp closed tcpmux") > 0) { continue; } if (strpos(" {$ligne}", "Too many fingerprints match") > 0) { continue; } if (strpos(" {$ligne}", "OS detection performed. Please report") > 0) { continue; } if (strpos(" {$ligne}", "OSScan results may be unreliable") > 0) { continue; } if (strpos(" {$ligne}", "/tcp filtered") > 0) { continue; } if (preg_match("#Nmap scan report for\\s+(.+?)\\s+\\(([0-9\\.]+)#", $ligne, $re)) { $ipaddr = $re[2]; $computer[$ipaddr]["IPADDR"] = $re[2]; $computer[$ipaddr]["HOSTNAME"] = trim($re[1]); if ($GLOBALS["VERBOSE"]) { echo "Found IP:{$ipaddr} hostname=`{$re[1]}` in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} hostname= {$re[1]}"; continue; } if (preg_match("#Interesting ports on (.*?)\\s+\\(([0-9\\.]+)\\)#", $ligne, $re)) { $ipaddr = $re[2]; $computer[$ipaddr]["IPADDR"] = $re[2]; $computer[$ipaddr]["HOSTNAME"] = trim($re[1]); if ($GLOBALS["VERBOSE"]) { echo "Found IP:{$ipaddr} hostname=`{$re[1]}` in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} hostname= {$re[1]}"; continue; } if (preg_match("#Interesting ports on ([0-9\\.]+):#", $ligne, $re)) { $ipaddr = $re[1]; $computer[$ipaddr]["IPADDR"] = $re[1]; if ($GLOBALS["VERBOSE"]) { echo "Found IP:{$ipaddr} only in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} only"; continue; } if (preg_match("#Nmap scan report for ([0-9\\.]+)\$#", trim($ligne), $re)) { $ipaddr = $re[1]; $computer[$ipaddr]["IPADDR"] = $re[1]; if ($GLOBALS["VERBOSE"]) { echo "[{$ipaddr}]: Found IP address `{$ipaddr}` without computername in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} without computername "; continue; } if (preg_match("#^MAC Address:\\s+([0-9A-Z:]+)\$#", trim($ligne), $re)) { if (trim($ipaddr) == null) { continue; } if (isset($MACSSCAN[trim($re[1])])) { continue; } $computer[$ipaddr]["MAC"] = trim($re[1]); $LOGS[] = "Found {$ipaddr} with mac {$re[1]} "; if ($GLOBALS["VERBOSE"]) { echo "[{$ipaddr}]: Found mac {$re[1]} in `{$ligne}`\n"; } $MACSSCAN[trim($re[1])] = true; continue; } if (preg_match("#^MAC Address:(.+).+?\\((.+?)\\)#", $ligne, $re)) { if (trim($ipaddr) == null) { continue; } if (isset($MACSSCAN[trim($re[1])])) { continue; } $MACSSCAN[trim($re[1])] = true; $computer[$ipaddr]["MAC"] = trim($re[1]); $computer[$ipaddr]["MACHINE_TYPE"] = trim($re[2]); if ($GLOBALS["VERBOSE"]) { echo "[{$ipaddr}]: Found mac {$re[1]} and machine type {$re[2]} in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} with mac {$re[1]} and machine type {$re[2]}"; continue; } if (preg_match("#^Running:(.+)#", $ligne, $re)) { if (trim($ipaddr) == null) { continue; } if ($GLOBALS["VERBOSE"]) { echo "Found running in `{$line}`\n"; } $computer[$ipaddr]["RUNNING"] = trim($re[1]); continue; } if (preg_match("#^OS details:(.+)#", $ligne, $re)) { if ($GLOBALS["VERBOSE"]) { echo "[{$ipaddr}]: Found OS {$re[1]} in `{$ligne}`\n"; } $LOGS[] = "Found {$ipaddr} with OS {$re[1]}"; $computer[$ipaddr]["OS"] = trim($re[1]); continue; } if ($GLOBALS["VERBOSE"]) { echo "[{$ipaddr}]: Not understood in `{$ligne}`\n"; } } nmap_logs(count($f) . " analyzed lines", @implode("\n", $LOGS)); $c = 0; $prefix_sql = "INSERT IGNORE INTO computers_lastscan (`MAC`, `zDate`,`ipaddr`,`hostname`,`Info`) VALUES "; while (list($ipaddr, $array) = each($computer)) { if (!isset($array["MAC"])) { continue; } $mac = trim($array["MAC"]); if (isset($already[$mac])) { continue; } if ($mac == null) { continue; } $c++; $already[$mac] = true; $ldap_ipaddr = null; $ComputerRealName = null; $uid = null; $RAISON = array(); if (!isset($array["HOSTNAME"])) { $array["HOSTNAME"] = null; } if (!isset($array["OS"])) { $array["OS"] = null; } if (!isset($array["RUNNING"])) { $array["RUNNING"] = null; } if (!isset($array["MACHINE_TYPE"])) { $array["MACHINE_TYPE"] = null; } $date = date('Y-m-d H:i:s'); $infos = addslashes($array["OS"] . " Type:{$array["MACHINE_TYPE"]} "); $SQLAD[] = "('{$mac}','{$date}','{$ipaddr}','{$array["HOSTNAME"]}','{$infos}')"; $cmp = new computers(null); $uid = $cmp->ComputerIDFromMAC($mac); if ($uid != null) { if ($GLOBALS["VERBOSE"]) { echo "{$mac} = {$uid}\n"; } $cmp = new computers($uid); $ldap_ipaddr = $cmp->ComputerIP; $ComputerRealName = $cmp->ComputerRealName; if ($GLOBALS["VERBOSE"]) { echo "{$mac} = {$uid}\nLDAP:{$ldap_ipaddr}<>NMAP:{$ipaddr}\nLDAP CMP:{$ComputerRealName}<>NMAP:{$array["HOSTNAME"]}"; } if ($array["HOSTNAME"] != null) { $EXPECTED_UID = strtoupper($array["HOSTNAME"]) . "\$"; if ($EXPECTED_UID != $uid) { $RAISON[] = "UID: {$uid} is different from {$EXPECTED_UID}"; nmap_logs("EDIT UID: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); $cmp->update_uid($EXPECTED_UID); } } if ($ldap_ipaddr != $ipaddr) { writelogs("Change {$ldap_ipaddr} -> to {$ipaddr} for {$cmp->uid}", __FUNCTION__, __FILE__, __LINE__); $RAISON[] = "LDAP IP ADDR: {$ldap_ipaddr} is different from {$ipaddr}"; $RAISON[] = "DN: {$cmp->dn}"; $RAISON[] = "UID: {$cmp->uid}"; $RAISON[] = "MAC: {$cmp->ComputerMacAddress}"; if (!$cmp->update_ipaddr($ipaddr)) { $RAISON[] = "ERROR:{$cmp->ldap_last_error}"; } nmap_logs("EDIT IP: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); } if ($array["OS"] != null) { if (strtolower($cmp->ComputerOS == "Unknown")) { $cmp->ComputerOS = null; } if ($cmp->ComputerOS == null) { $RAISON[] = "LDAP OS: {$cmp->ComputerOS} is different from {$array["OS"]}"; nmap_logs("EDIT OS: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), $uid); $cmp->update_OS($array["OS"]); } } } else { if ($array["HOSTNAME"] != null) { $uid = "{$array["HOSTNAME"]}\$"; } else { continue; } nmap_logs("ADD NEW: {$mac}:[{$array["HOSTNAME"]}] ({$ipaddr})", @implode("\n", $array) . "\n" . @implode("\n", $RAISON), "{$uid}"); $cmp = new computers(); $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; $cmp->uid = "{$uid}"; $cmp->ComputerOS = $array["OS"]; $cmp->ComputerRunning = $array["RUNNING"]; $cmp->ComputerMachineType = $array["MACHINE_TYPE"]; $cmp->Add(); } } if ($GLOBALS["VERBOSE"]) { echo "*** " . count($SQLAD) . " MYsql queries...***\n"; } system_admin_events("{$c} hosts analyzed in networks", __FUNCTION__, __FILE__, __LINE__, "nmap"); nmap_logs("{$c} hosts analyzed in networks", @file_get_contents("/etc/artica-postfix/nmap.map"), null); if (count($SQLAD) > 0) { $q = new mysql(); $q->QUERY_SQL("DROP TABLE computers_lastscan", "artica_backup"); $q->check_storage_table(true); $final = $prefix_sql . @implode(",", $SQLAD); if ($GLOBALS["VERBOSE"]) { echo "*** {$final} ***\n"; } $q->QUERY_SQL($prefix_sql . @implode(",", $SQLAD), "artica_backup"); if (!$q->ok) { echo $q->mysql_error . "\n"; } } @unlink("/etc/artica-postfix/nmap.map"); //print_r($computer); }
function build() { $unix = new unix(); if (!is_file("/etc/artica-postfix/discover.txt")) { if ($GLOBALS["VERBOSE"]) { echo "/etc/artica-postfix/discover.txt no such file\n"; } return; } if ($GLOBALS["VERBOSE"]) { echo "Open /etc/artica-postfix/discover_cache.db\n"; } $ARRAY_CACHE = unserialize(@file_get_contents("/etc/artica-postfix/discover_cache.db")); if ($GLOBALS["VERBOSE"]) { echo "Open /etc/artica-postfix/discover.txt\n"; } $results = explode("\n", @file_get_contents("/etc/artica-postfix/discover.txt")); while (list($num, $line) = each($results)) { $line = trim($line); if ($line == null) { continue; } $hostname = null; if (!preg_match("#([0-9\\.]+)\\s+(.+?)\\s+[0-9]+\\s+[0-9]+\\s+(.+?)\$#", $line, $re)) { continue; } $ipaddr = $re[1]; $MAC = $re[2]; $text = $re[3]; $timeEx = 0; $CacheMin = $ARRAY_CACHE[$MAC]; if ($CacheMin > 0) { $timeEx = computer_time_min($CacheMin); if ($timeEx < 60) { if ($GLOBALS["VERBOSE"]) { echo "{$MAC} = {$timeEx}Mn\n"; } continue; } } if ($GLOBALS["VERBOSE"]) { echo "{$ipaddr}\t{$MAC}\t{$text}\t[{$CacheMin}]={$timeEx}Mn\n"; } $computer = new computers(); $cpid = $computer->ComputerIDFromMAC($MAC); if ($cpid != null) { $hostname = $cpid; $hostname = str_replace("\$", "", $hostname); if (preg_match("#[0-9\\.]+#", $hostname)) { $cpidR = gethostbyaddr($ipaddr); if ($cpidR != $ipaddr) { $hostname = $cpidR; } } } if ($hostname == null) { $hostname = gethostbyaddr($ipaddr); } $computer = new computers($cpid); if ($computer->ComputerMachineType == null) { $computer->ComputerMachineType = $text; } $computer->ComputerMacAddress = $MAC; $computer->ComputerIP = $ipaddr; $computer->ComputerRealName = $hostname; if ($computer->Add()) { $ARRAY_CACHE[$MAC] = time(); } else { if ($GLOBALS["VERBOSE"]) { echo "{$MAC} - {$hostname} Failed\n"; } } } @file_put_contents("/etc/artica-postfix/discover_cache.db", serialize($ARRAY_CACHE)); }
function COMPUTER_SAVE_INFOS() { $tpl = new templates(); if (preg_match("#newcomputer#", $_GET["uid"])) { echo $tpl->_ENGINE_parse_body('ERROR:{give_computer_name}'); exit; } $computer = new computers($_GET["userid"]); $computer->uid = $_GET["uid"] . '$'; $computer->ComputerMacAddress = $_GET["ComputerMacAddress"]; $computer->ComputerIP = $_GET["ComputerIP"]; $computer->DnsZoneName = $_GET["DnsZoneName"]; $computer->ComputerCPU = $_GET["ComputerCPU"]; $computer->DnsType = $_GET["DnsType"]; $computer->DnsMXLength = $_GET["DnsMXLength"]; $computer->dhcpfixed = $_GET["dhcpfixed"]; $computer->VolatileIPAddress = $_GET["VolatileIPAddress"]; if ($_GET["userid"] == "newcomputer\$") { if (!$computer->Add()) { echo "ERROR:{$computer->ldap_error}"; exit; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); echo html_entity_decode($tpl->_ENGINE_parse_body('{success}')); if (isset($_GET["gpid"])) { writelogs("adding computer to group {$_GET["gpid"]}", __FUNCTION__, __FILE__, __LINE__); $group = new groups($_GET["gpid"]); $group->AddUsertoThisGroup($computer->uid); exit; } exit; } } if (!$computer->Edit()) { echo $computer->ldap_error; } else { writelogs("Success updating/adding {$computer->uid}", __FUNCTION__, __FILE__, __LINE__); echo html_entity_decode($tpl->_ENGINE_parse_body('{success}')); } }
function update_computer($ip, $mac, $name) { $mac = trim($mac); $name = trim(strtolower($name)); $ip = trim($ip); if ($ip == null) { return; } if ($mac == null) { return; } if ($name == null) { return; } $ip = nmblookup($name, $ip); $unix = new unix(); $unix->add_EtcHosts($name, $ip); $dhcp = new dhcpd(); $GLOBALS["domain"] = $dhcp->ddns_domainname; $comp = new computers(); $uid = $comp->ComputerIDFromMAC($mac); if ($uid == null) { $add = true; $uid = "{$name}\$"; $comp = new computers(); $comp->ComputerRealName = $name; $comp->ComputerMacAddress = $mac; $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; $comp->uid = $uid; $ComputerRealName = $HOST; $comp->Add(); } else { $comp = new computers($uid); if ($comp->ComputerRealName == null) { $ComputerRealName = $name; } if (preg_match("#[0-9]+\\.[0-9]+\\.#", $comp->ComputerRealName)) { $comp->ComputerRealName = $name; } $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; $comp->Edit(); } $dns = new pdns($GLOBALS["domain"]); $dns->EditIPName(strtolower($name), $ip, 'A', $mac); }
function importcomputersFromList() { $sock = new sockets(); $ipClass = new IP(); $tbl = explode("\n", $sock->GET_INFO("ComputerListToImport")); writelogs("ComputerListToImport=" . count($tbl) . " values", __FUNCTION__, __FILE__, __LINE__); $i = 0; $max = count($tbl); while (list($num, $computername) = each($tbl)) { $z = $z + 1; $computername = trim($computername); $ip = null; $mac = null; if ($computername == null) { continue; } if (strpos($computername, " ") > 0) { $TRB = explode(" ", $computername); $computername = $TRB[0]; unset($TRB[0]); while (list($a, $b) = each($TRB)) { if ($b == null) { continue; } if ($ipClass->isValid($b)) { $ip = $b; continue; } if ($ipClass->IsvalidMAC($b)) { $mac = $b; continue; } } } if (isset($_GET["arp-ip"])) { $ip_arp = unserialize(base64_decode($sock->getFrameWork("cmd.php?arp-ip=" . $_GET["arp-ip"]))); if (is_array($ip_arp)) { $ip = $ip_arp[0]; $mac = $ip_arp[1]; unset($ip_arp); } } $pourc = round($z / $max * 100); writelogs("{$pourc}) {$computername}", __FUNCTION__, __FILE__, __LINE__); WriteCOmputerBrowseProgress($pourc, "{import}: {$computername} ({$ip}/{$mac})"); $cmp = new computers(); if ($mac != null) { $uid = $cmp->ComputerIDFromMAC($mac); } else { $uid = "{$computername}\$"; } if ($uid == null) { $uid = "{$computername}\$"; } $cmp = new computers($uid); if ($ip != null) { $cmp->ComputerIP = $ip; } if ($mac != null) { $cmp->ComputerMacAddress = $mac; } $cmp->ComputerRealName = $computername; $cmp->Add(); $i = $i + 1; } WriteCOmputerBrowseProgress(0, "{waiting}"); }
function parseContent($file) { if (!is_file($file)) { exit; } $gp = new groups(null); $gp->BuildOrdinarySambaGroups(); $datas = file_get_contents($file); $tbl = explode("\n", $datas); while (list($num, $line) = each($tbl)) { if (trim($line) == null) { continue; } $computer_name = null; $ip = null; $line = trim($line); if (preg_match("#^Interesting ports on (.+?)\\s+\\((.+?)\\)#", $line, $re)) { if (preg_match('#([0-9]+)\\.([0-9]+)\\.([0-9]+)\\.([0-9]+)#', $re[2])) { $ip = $re[2]; $computer_name = $re[1]; compevents("found computer: {$computer_name}/{$ip} (line {$num})"); $arr[$computer_name]["IP"] = $ip; continue; } } if (preg_match("#^Interesting ports on ([0-9\\.]+):#", $line, $re)) { $ip = $re[1]; $computer_name = "{$ip}"; compevents("found {$computer_name}/{$ip} (line {$num})"); $arr[$computer_name]["IP"] = $ip; continue; } if (preg_match("#^Nmap scan report for\\s+(.+?)\\s+\\((.+?)\\)#", $line, $re)) { $ip = $re[2]; $computer_name = $re[1]; compevents("found computer: {$computer_name}/{$ip} (line {$num})"); $arr[$computer_name]["IP"] = $ip; continue; } if (preg_match("#^Nmap scan report for\\s+(.+?)\$#", trim($line), $re)) { $ip = $re[1]; $computer_name = $ip; compevents("found computer: just {$ip} (line {$num})"); $arr[$computer_name]["IP"] = $ip; continue; } if (preg_match("#^MAC Address.+?\\((.+?)\\)#", $line, $re)) { if ($_GET["VERBOSE"]) { echo "{$computer_name}:: Found type={$re[1]}\n"; } compevents("{$computer_name}: found type={$re[1]} (line {$num})"); $arr[$computer_name]["TYPE"] = $re[1]; continue; } if (preg_match('#^MAC Address:\\s+(.+?)\\s+#', $line, $re)) { compevents("{$computer_name}: found MAC={$re[1]} (line {$num})"); if ($_GET["VERBOSE"]) { echo "{$computer_name}:: Found MAC={$re[1]}\n"; } $arr[$computer_name]["MAC"] = $re[1]; continue; } if (preg_match('#Running:\\s+(.+)#', $line, $re)) { compevents("{$computer_name}: found OS={$re[1]} (line {$num})"); if ($_GET["VERBOSE"]) { echo "{$computer_name}:: Found OS={$re[1]}\n"; } $arr[$computer_name]["OS"] = $re[1]; continue; } } if (!is_array($arr)) { return null; } while (list($num, $line) = each($arr)) { if (trim($num) == null) { continue; } compevents("Add entry: " . $num . '$'); $cp = new computers($num . '$'); $cp->ComputerIP = $line["IP"]; $cp->ComputerOS = $line["OS"]; $cp->ComputerMacAddress = $line["MAC"]; if ($line["TYPE"] != null) { $cp->ComputerMachineType = $line["TYPE"]; } if (!$cp->Add()) { compevents($cp->ldap_error . " for {$num}\$"); } } @unlink($file); }
function impcomputers() { $sock = new sockets(); $ldap = new clladp(); $users = new usersMenus(); $EnableKerbAuth = 1; if (!$ldap->IsKerbAuth()) { $EnableKerbAuth = 0; } if (!is_numeric($EnableKerbAuth)) { $EnableKerbAuth = 0; } if ($GLOBALS["VERBOSE"]) { echo "EnableKerbAuth={$EnableKerbAuth}\n"; } if ($EnableKerbAuth == 0) { return; } $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/exec.adusers.php.impcomputers.time"; $pid = @file_get_contents($pidfile); if ($unix->process_exists($pid, __FILE__)) { return; } $ztime = $unix->file_time_min($timefile); if ($ztime < 240) { die; } @unlink($timefile); @file_put_contents($timefile, time()); array('a', 'b', 'c', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2', '3', '4', '5', '6', '7', '8', '9', '0'); $iptcp = new IP(); while (list($num, $letter) = each($array)) { $ad = new external_ad_search(); $CMPS = $ad->find_computers("{$letter}*"); $count = $CMPS["count"]; if ($GLOBALS["VERBOSE"]) { echo "{$letter}* == {$count}\n"; } for ($i = 0; $i < $count; $i++) { $arraCMP = $CMPS[$i]; $hostname = null; $domain = null; $operatingsystem = null; $uid = $arraCMP["samaccountname"][0]; $computer = new computers($uid); if ($computer->IsExists) { continue; } if (isset($arraCMP["dnshostname"][0])) { $hostname = $arraCMP["dnshostname"][0]; } if (isset($arraCMP["operatingsystem"][0])) { $operatingsystem = $arraCMP["operatingsystem"][0]; } if ($hostname == null) { if (isset($arraCMP["name"])) { $hostname = $arraCMP["name"][0]; } } if ($hostname == null) { if (isset($arraCMP["cn"])) { $hostname = $arraCMP["cn"][0]; } } if ($hostname == null) { continue; } if (strpos($hostname, ".") > 0) { $DD = explode(".", $hostname); unset($DD[0]); $domain = @implode(".", $DD); } $ipaddr = gethostbyname($hostname); if (!$iptcp->isIPAddress($ipaddr)) { if ($GLOBALS["VERBOSE"]) { echo "{$hostname} - > {$ipaddr} -> abort\n"; } continue; } $computer->ComputerIP = $ipaddr; $computer->uid = "{$uid}"; if ($domain != null) { $computer->DnsZoneName = $domain; } $computer->ComputerRealName = $arraCMP["name"]; $computer->ComputerOS = $operatingsystem; if ($GLOBALS["VERBOSE"]) { echo "Add {$hostname}/{$ipaddr} {$operatingsystem}\n"; } $computer->Add(); } } }
function ImportDatas($datas) { $import = unserialize(base64_decode($datas)); $hash = $import["COMPUTERS"]; for ($i = 0; $i < $hash["count"]; $i++) { $comp = new computers(); $uidorg = $hash[$i]["uid"][0]; $dnszonename = $hash[$i]["dnszonename"][0]; $dnstype = $hash[$i]["dnstype"][0]; $computermachinetype = $hash[$i]["computermachinetype"][0]; $computerip = $hash[$i]["computerip"][0]; $computermacaddress = $hash[$i]["computermacaddress"][0]; $ComputerRealName = $hash[$i][strtolower("ComputerRealName")][0]; if ($ComputerRealName == null) { $ComputerRealName = str_replace('$', '', $uidorg); } writelogs("Importing computer: {$uid} {$computerip} {$computermacaddress}", __FUNCTION__, __FILE__, __LINE__); $uid = $comp->ComputerIDFromMAC($computermacaddress); if ($uid == null) { $uid = $uidorg; $comp = new computers(); $comp->ComputerRealName = $ComputerRealName; $comp->ComputerMacAddress = $computermacaddress; $comp->ComputerIP = $computerip; $comp->DnsZoneName = $dnszonename; $comp->ComputerMachineType = $computermachinetype; $comp->uid = $uid; $comp->Add(); } else { $comp = new computers($uid); $comp->ComputerIP = $computerip; $comp->DnsZoneName = $dnszonename; $comp->ComputerMachineType = $computermachinetype; $comp->Edit(); } $dns = new pdns($dnszonename); $dns->EditIPName(strtolower($ComputerRealName), $computerip, $dnstype, $computermacaddress); } }
function receive_noop_connection() { $datas = base64_decode($_GET["noop"]); // 00-0C-29-09-BB-77|192.168.1.248|GRAPHICS|Windows XP SP3|Windows XP|SP3|=Intel;Intel(R) Core(TM)2 Quad CPU Q6600 @ 2.40GHz;2400;MMX|C:;34949382144 $datas = explode("|", $datas); @file_put_contents("/tmp/datas.txt", implode("\n", $datas)); while (list($num, $ligne) = each($datas)) { writelogs("=>{$num}): {$ligne}", __FUNCTION__, __FILE__, __LINE__); } $MAC = $datas[0]; $MAC = str_replace("-", ":", $MAC); $LOCAL_IP = $datas[1]; $COMPUTER_NAME = $datas[2]; $OS_VER = $datas[3]; $CPUAR = explode(";", $datas[6]); $CPU_INFO = $CPUAR[1]; $CPU_INFO = str_replace(" ", " ", $CPU_INFO); $domain = strtolower($datas[9]); $uptime = strtolower($datas[13]); if (!preg_match("#(.+?)\\.(.+)#", $domain)) { $domain = null; } $computer = new computers(); $uid = $computer->ComputerIDFromMAC($MAC); writelogs("[{$uid}] ({$COMPUTER_NAME}):: mac={$MAC}, ip={$LOCAL_IP}, system {$OS_VER} ({$CPU_INFO}) uptime={$uptime}", __FUNCTION__, __LINE__); if ($uid == null) { $computer->uid = "{$COMPUTER_NAME}\$"; $computer->ComputerMacAddress = $MAC; $computer->ComputerIP = $LOCAL_IP; if ($domain != null) { $computer->DnsZoneName = $domain; } $computer->ComputerRealName = $COMPUTER_NAME; $computer->ComputerCPU = $CPU_INFO; $computer->ComputerOS = $OS_VER; if ($uptime != null) { $computer->ComputerUpTime = $uptime; } $computer->Add(); } else { $computer = new computers($uid); $computer->ComputerIP = $LOCAL_IP; $computer->ComputerRealName = $COMPUTER_NAME; $computer->ComputerCPU = $CPU_INFO; $computer->ComputerOS = $OS_VER; $computer->ComputerMacAddress = $MAC; if ($domain != null) { $computer->DnsZoneName = $domain; } if ($uptime != null) { $computer->ComputerUpTime = $uptime; } $computer->Edit(); } $MAC = str_replace("-", ':', $MAC); $sql = "INSERT INTO computers_events (MAC,zDate,events_type,events) VALUES('{$MAC}',NOW(),'communication','{success_artica_agent_updateinfo}')"; $q = new mysql(); $q->QUERY_SQL($sql, 'artica_backup'); $sql = "SELECT * FROM computers_tasks WHERE MAC='{$MAC}' AND task_enabled=1 ORDER BY ID DESC"; $results = $q->QUERY_SQL($sql, "artica_backup"); while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) { echo "<task>{$ligne["ID"]}</task><task_type>{$ligne["task_type"]}</task_type><schedule>{$ligne["schedule"]}</schedule><path>{$ligne["path"]}</path>\n"; } $sql = "SELECT * FROM computers_orders WHERE MAC='{$MAC}' ORDER BY ID DESC"; $results = $q->QUERY_SQL($sql, "artica_backup"); while ($ligne = mysql_fetch_array($results, MYSQL_ASSOC)) { $ligne["parameters"] = base64_encode($ligne["parameters"]); echo "<order>{$ligne["ID"]}</order><task_type>{$ligne["task_type"]}</task_type><parameters>{$ligne["parameters"]}</parameters><taskid>{$ligne["taskid"]}</taskid>\n"; } }
function update_computer($ip, $mac, $name) { $sock = new sockets(); $ComputersAllowDHCPLeases = $sock->GET_INFO("ComputersAllowDHCPLeases"); if ($ComputersAllowDHCPLeases == null) { $ComputersAllowDHCPLeases = 1; } if ($ComputersAllowDHCPLeases == 0) { localsyslog("`ComputersAllowDHCPLeases` Aborting updating the LDAP database"); return; } $mac = trim($mac); $name = trim(strtolower($name)); $ip = trim($ip); if ($ip == null) { return; } if ($mac == null) { return; } if ($name == null) { return; } $mac = strtolower(str_replace("-", ":", $mac)); $ipClass = new IP(); if ($ipClass->isIPAddress($name)) { localsyslog("`{$name}` is a TCP IP address, aborting updating the LDAP database"); return; } $ip = nmblookup($name, $ip); $dhcp = new dhcpd(); $GLOBALS["domain"] = $dhcp->ddns_domainname; $comp = new computers(); $uid = $comp->ComputerIDFromMAC($mac); if (strpos($name, ".") > 0) { $NAMETR = explode(".", $name); $name = $NAMETR[0]; unset($NAMETR[0]); $GLOBALS["domain"] = @implode(".", $NAMETR); } if ($ipClass->isIPAddress($uid)) { $comp = new computers($uid); localsyslog("Removing computer ({$uid}) {$mac}"); $comp->DeleteComputer(); $uid = null; $uid = $comp->ComputerIDFromMAC($mac); } localsyslog("{$mac} -> uid:`{$uid}`"); if ($uid == null) { $add = true; $uid = "{$name}\$"; $comp = new computers(); $comp->ComputerRealName = $name; $comp->ComputerMacAddress = $mac; $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; $comp->uid = $uid; $ComputerRealName = $name; localsyslog("Create new computer {$name[$ip]} ({$uid}) {$mac} in domain {$comp->DnsZoneName}"); $comp->Add(); } else { $comp = new computers($uid); if (strpos($comp->ComputerRealName, ".") > 0) { $NAMETR = explode(".", $name); $comp->ComputerRealName = $NAMETR[0]; } if ($comp->ComputerRealName == null) { $comp->ComputerRealName = $name; } if ($ipClass->isIPAddress($comp->ComputerRealName)) { $comp->ComputerRealName = $name; } $comp->ComputerIP = $ip; $comp->DnsZoneName = $GLOBALS["domain"]; localsyslog("Update computer {$comp->ComputerRealName}[{$ip}] ({$uid}) {$mac} in domain {$comp->DnsZoneName}"); $comp->Edit(); } $dns = new pdns($GLOBALS["domain"]); $dns->EditIPName(strtolower($name), $ip, 'A', $mac); }
function main_add_computer() { $computer = $_POST["add_computer"]; if (trim($computer) != null) { $comp = new computers(); $comp->uid = $computer; if (!$comp->Add()) { echo $comp->ldap_error; } } }
function scanarp() { $GLOBALS["CLASS_USERS"] = new usersMenus(); $GLOBALS["CLASS_SOCKETS"] = new sockets(); if (!$GLOBALS["CLASS_USERS"]->ARPD_INSTALLED) { if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . " ARPD_INSTALLED = FALSE\n"; } return; } $EnableArpDaemon = $GLOBALS["CLASS_SOCKETS"]->GET_INFO("EnableArpDaemon"); if (!is_numeric($EnableArpDaemon)) { $EnableArpDaemon = 1; } if ($EnableArpDaemon == 0) { if ($GLOBALS["VERBOSE"]) { echo __FUNCTION__ . " EnableArpDaemon = {$EnableArpDaemon}\n"; } return; } $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . ".pid"; $unix = new unix(); $me = basename(__FILE__); $pid = $unix->get_pid_from_file($pidfile); if ($unix->process_exists($pid, $me)) { if ($GLOBALS["VERBOSE"]) { echo " {$pid} --> Already executed.. aborting the process\n"; } $time = $unix->PROCCESS_TIME_MIN($pid); system_admin_events("Already executed pid {$pid} since {$time}Mn.. aborting the process", __FUNCTION__, __FILE__, __LINE__, "system"); die; } @file_put_contents($pidfile, getmypid()); if (!is_file("/var/lib/arpd/arpd.db")) { die; } $GLOBALS["CLASS_UNIX"] = $unix; $GLOBALS["nmblookup"] = $unix->find_program("nmblookup"); $GLOBALS["arpd"] = $unix->find_program("arpd"); $GLOBALS["arp"] = $unix->find_program("arp"); $GLOBALS["ARP_DB"] = "/var/lib/arpd/arpd.db"; $GLOBALS["CACHE_DB"] = "/etc/artica-postfix/arpd.cache"; $GLOBALS["EnableMacAddressFilter"] = trim(@file_get_contents("/etc/artica-postfix/settings/Daemons/EnableMacAddressFilter")); if (!is_numeric($GLOBALS["EnableMacAddressFilter"])) { $GLOBALS["EnableMacAddressFilter"] = 1; } $squidbin = $unix->LOCATE_SQUID_BIN(); if (is_file($squidbin)) { if ($GLOBALS["EnableMacAddressFilter"] == 0) { return; } } $ArpdArray = unserialize(base64_decode(@file_get_contents($GLOBALS["CACHE_DB"]))); if ($GLOBALS["FLUSH"]) { $ArpdArray = array(); } if (!is_array($ArpdArray)) { $ArpdArray = array(); } if (!isset($ArpdArray["LAST"])) { $ArpdArray["LAST"] = 0; } $last_modified = filemtime($GLOBALS["ARP_DB"]); $TimeArpd = $ArpdArray["LAST"]; if ($TimeArpd == $last_modified) { events("{$TimeArpd} -> {$last_modified} No modification time", __FUNCTION__, __LINE__); return; } events("Scanning ARP table....", __FUNCTION__, __LINE__); $ArpdArray["LAST"] = $last_modified; exec("{$GLOBALS["arpd"]} -l 2>&1", $results); events("{$GLOBALS["arpd"]} -l return " . count($results) . " element(s)", __FUNCTION__, __LINE__); while (list($num, $ligne) = each($results)) { if (preg_match("#unexpected file type or format#", $ligne)) { @unlink($GLOBALS["ARP_DB"]); @unlink($GLOBALS["CACHE_DB"]); shell_exec("/etc/init.d/arpd restart"); die; } if (!preg_match("#^[0-9]+\\s+\\s+(.+?)\\s+(.+)#", $ligne, $re)) { if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, unexpected line..\n"; } continue; } if (preg_match("#FAILED:#", $re[2])) { continue; } $mac = $re[2]; $ipaddr = $re[1]; if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, MAC:{$mac} -> {$ipaddr}\n"; } if (isset($ArpdArray["MACS"][$mac])) { if ($GLOBALS["VERBOSE"]) { echo "MAC:{$mac} Already cached, aborting....\n"; } continue; } $ArpdArray["MACS"][$mac] = true; $cmp = new computers(); $uid = $cmp->ComputerIDFromMAC($mac); if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, MAC:{$mac} -> {$uid}\n"; } if ($uid == null) { $res2 = array(); $computer_name = null; events("It is time to add {$mac}/{$ipaddr} in database", __FUNCTION__, __LINE__); exec("{$GLOBALS["arp"]} -a {$ipaddr} 2>&1", $res2); if (preg_match("#^(.+?)\\s+\\(#", trim(@implode("", $res2)), $rz)) { $computer_name = $rz[1]; } if (strlen($computer_name) < 3) { $computer_name = $ipaddr; } $cmp->uid = "{$computer_name}\$"; $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; system_admin_events("adding/editing {$computer_name} with MAC:{$mac}", __FUNCTION__, __FILE__, __LINE__, "network"); $cmp->Add(); } else { if ($GLOBALS["FLUSH"]) { $res2 = array(); $cmp = new computers($uid); $computer_name = null; events("It is time to edit {$uid}/{$mac}/{$ipaddr} in database", __FUNCTION__, __LINE__); exec("{$GLOBALS["arp"]} -a {$ipaddr} 2>&1", $res2); if ($GLOBALS["VERBOSE"]) { echo "{$GLOBALS["arp"]} -a {$ipaddr} 2>&1 = >" . trim(@implode("", $res2)); } if (preg_match("#^(.+?)\\s+\\(#", trim(@implode("", $res2)), $rz)) { $computer_name = $rz[1]; } else { if ($GLOBALS["VERBOSE"]) { echo "Unable to find computer name\n"; } } if (strlen($computer_name) < 3) { $computer_name = $ipaddr; } if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, UID:{$mac} -> {$uid}\n"; } if ($GLOBALS["VERBOSE"]) { echo "line: {$num}, NAME:{$computer_name} -> {$uid}\n"; } system_admin_events("adding/editing {$computer_name} with MAC:{$mac}", __FUNCTION__, __FILE__, __LINE__, "network"); $cmp->ComputerIP = $ipaddr; $cmp->ComputerMacAddress = $mac; $cmp->Add(); } } if (system_is_overloaded(basename(__FILE__))) { @file_put_contents($GLOBALS["CACHE_DB"], base64_encode(serialize($ArpdArray))); system_admin_events("Overloaded system, aborting the task...", __FUNCTION__, __FILE__, __LINE__, "network"); return; } @file_put_contents($GLOBALS["CACHE_DB"], base64_encode(serialize($ArpdArray))); $nice = EXEC_NICE(); $unix = new unix(); $nohup = $unix->find_program("nohup"); $php5 = $unix->LOCATE_PHP5_BIN(); shell_exec("{$nohup} {$nice} {$php5} " . __FILE__ . " --tomysql schedule-id={$GLOBALS["SCHEDULE_ID"]} >/dev/null 2>&1 &"); } }
function macscan() { $unix = new unix(); $pidfile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".pid"; $timefile = "/etc/artica-postfix/pids/" . basename(__FILE__) . "." . __FUNCTION__ . ".time"; if ($GLOBALS["VERBOSE"]) { echo "Time File: {$timefile}\n"; } $pid = @file_get_contents($pidfile); if ($GLOBALS["FORCE"]) { ToSyslog("macscan(): Executed in --force mode"); } if (!$GLOBALS["FORCE"]) { if ($pid < 100) { $pid = null; } $unix = new unix(); if ($unix->process_exists($pid, basename(__FILE__))) { if ($GLOBALS["VERBOSE"]) { echo "Already executed pid {$pid}\n"; } ToSyslog("macscan(): already executed pid {$pid}"); return; } $timeexec = $unix->file_time_min($timefile); if ($timeexec < 30) { return; } $mypid = getmypid(); @file_put_contents($pidfile, $mypid); } @unlink($timefile); @file_put_contents($timefile, time()); if ($GLOBALS["FORCE"]) { ToSyslog("macscan(): start analyze MAC addresses"); } $CACHE_FILE = "/etc/artica-postfix/" . basename(__FILE__) . ".cache"; $cachetime = $unix->file_time_min($CACHE_FILE); if ($cachetime < 1440) { $CACHE = unserialize(base64_decode(@file_get_contents($CACHE_FILE))); } if (!isset($GLOBALS["Q"])) { $GLOBALS["Q"] = new mysql_squid_builder(); } $sql = "SELECT * FROM macscan"; $results = $GLOBALS["Q"]->QUERY_SQL($sql); if (!$GLOBALS["Q"]->ok) { ToSyslog("Aborted task with a MySQL error:{$GLOBALS["Q"]->mysql_error}"); return; } while ($ligne = @mysql_fetch_array($results, MYSQL_ASSOC)) { $ipaddr = $ligne["ipaddr"]; $MAC = $ligne["MAC"]; if (isset($CACHE[$MAC])) { $GLOBALS["Q"]->QUERY_SQL("DELETE FROM macscan WHERE MAC='{$MAC}'"); continue; } $hostname = gethostbyaddr($ipaddr); $cmp = new computers(); $uid = $cmp->ComputerIDFromMAC($MAC); $cmp->ComputerIP = $ipaddr; if ($uid == null) { $cmp = new computers("{$hostname}\$"); $cmp->ComputerRealName = $hostname; $cmp->ComputerMacAddress = $MAC; $cmp->Add(); } $CACHE[$MAC] = true; $GLOBALS["Q"]->QUERY_SQL("DELETE FROM macscan WHERE MAC='{$MAC}'"); } @file_put_contents($CACHE_FILE, base64_encode(serialize($CACHE))); }