# print change if ($Scan->debugging) { print "\nAddress changes:\n----------\n"; print_r($address_change); } # all done, mail diff? if (sizeof($address_change) > 0 && $send_mail) { if (!is_object(@$Scan)) { $Database = new Database_PDO(); $Subnets = new Subnets($Database); $Addresses = new Addresses($Database); $Tools = new Tools($Database); $Scan = new Scan($Database); $Result = new Result(); // set exit flag to true $Scan->ping_set_exit(true); // set debugging $Scan->reset_debugging(false); } # check for recipients foreach ($Tools->fetch_multiple_objects("users", "role", "Administrator") as $admin) { if ($admin->mailNotify == "Yes") { $recepients[] = array("name" => $admin->real_name, "email" => $admin->email); } } # none? if (!isset($recepients)) { die; } # fetch mailer settings $mail_settings = $Tools->fetch_object("settingsMail", "id", 1);
* * Scan type is fetched from DB settings, currently supported scans for cli are: * * ping * * pear * * fping * */ /* functions */ require dirname(__FILE__) . '/../../functions/functions.php'; require dirname(__FILE__) . '/../../functions/classes/class.Thread.php'; # initialize user object $Database = new Database_PDO(); $Subnets = new Subnets($Database); $Scan = new Scan($Database); //set exit flag to true $Scan->ping_set_exit(true); /** * Input checks */ //script can only be run from cli if (php_sapi_name() != "cli") { die(json_encode(array("status" => 1, "error" => "This script can only be run from cli!"))); } //check input parameters if (!isset($argv[1]) || !isset($argv[2])) { die(json_encode(array("status" => 1, "error" => "Missing required input parameters"))); } // test to see if threading is available if (!Thread::available()) { die(json_encode(array("status" => 1, "error" => "Threading is required for scanning subnets. Please recompile PHP with pcntl extension"))); }
/** * Read address functions * * identifiers can be: * - {id} * - {id}/ping/ // pings address * - /search/{ip_address}/ // searches for addresses in database, returns multiple if found * - custom_fields * - tags // all tags * - tags/{id}/ // specific tag * - tags/{id}/addresses // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip_addr); // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); return array("code" => 200, "data" => $result); } else { $this->Response->throw_exception(404, "Address offline. Exit code: " . $result['exit_code'] . "( " . $Scan->ping_exit_explain($result['exit_code']) . " )"); } } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }
/** * Read address functions * * identifiers can be: * - /addresses/{id}/ * - /addresses/{id}/ping/ // pings address * - /addresses/{ip}/{subnetId}/ // Returns address from subnet * - /addresses/search/{ip_address}/ // searches for addresses in database, returns multiple if found * - /addresses/search_hostname/{hostname}/ // searches for addresses in database by hostname, returns multiple if found * - /addresses/search_hostbase/{hostbase}/ // searches for addresses by leading substring (base) of hostname, returns ordered multiple * - /addresses/first_free/{subnetId}/ // returns first available address (subnetId can be provided with parameters) * - /addresses/custom_fields/ // custom fields * - /addresses/tags/ // all tags * - /addresses/tags/{id}/ // specific tag * - /addresses/tags/{id}/addresses/ // returns all addresses that are tagged with this tag ***if subnetId is provided it will be filtered to specific subnet * * @access public * @return void */ public function GET() { // subnet Id > read all addresses in subnet if ($this->_params->id == "custom_fields") { // check result if (sizeof($this->custom_fields) == 0) { $this->Response->throw_exception(404, 'No custom fields defined'); } else { return array("code" => 200, "data" => $this->custom_fields); } } elseif ($this->_params->id == "first_free") { // check for isFull if (isset($this->_params->subnetId)) { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->subnetId); } else { $subnet = $this->Tools->fetch_object("subnets", "id", $this->_params->id2); } if ($subnet->isFull == 1) { $this->Response->throw_exception(404, "No free addresses found"); } $this->_params->ip_addr = $this->Addresses->get_first_available_address($subnet->id, $this->Subnets); // null if ($this->_params->ip_addr == false) { $this->Response->throw_exception(404, 'No free addresses found'); } else { return array("code" => 200, "data" => $this->Addresses->transform_address($this->_params->ip_addr, "dotted")); } } elseif ($this->Tools->validate_ip($this->_params->id) !== false && isset($this->_params->id2)) { // fetch all in subnet $result = $this->Tools->fetch_multiple_objects("ipaddresses", "subnetId", $this->_params->id2); if ($result !== false) { foreach ($result as $k => $r) { if ($r->ip !== $this->_params->id) { unset($result[$k]); } else { $result_filtered = $r; } } if (sizeof($result) == 0) { $result = false; } else { $result = $result_filtered; } } if ($result == false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $result); } } elseif ($this->_params->id == "tags") { // validate $this->validate_tag(); // all addresses with tag if (@$this->_params->id3 == "addresses") { // fetch $result = $this->Tools->fetch_multiple_objects("ipaddresses", "state", $this->_params->id2); // filter by subnetId if ($result !== false) { if (isset($this->_params->subnetId)) { if (is_numeric($this->_params->subnetId)) { // filter foreach ($result as $k => $v) { if ($v->subnetId != $this->_params->subnetId) { unset($result[$k]); } } // any left if (sizeof($result) == 0) { $result = false; } } } } // result if ($result === false) { $this->Response->throw_exception(404, 'No addresses found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses", true, false)); } } else { // fetch all by tag if (isset($this->_params->id2)) { // numeric if (is_numeric($this->_params->id2)) { $result = $this->Tools->fetch_object("ipTags", "id", $this->_params->id2); } else { $result = $this->Tools->fetch_multiple_objects("ipTags", "type", $this->_params->id2); } } else { $result = $this->Tools->fetch_all_objects("ipTags"); } // result if ($result === false) { $this->Response->throw_exception(404, 'Tag not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, "addresses/tags", true, false)); } } } elseif (!isset($this->_params->id)) { $this->Response->throw_exception(400, 'Address ID is required'); } elseif (is_numeric($this->_params->id)) { // ping if (@$this->_params->id2 == "ping") { # scan class $Scan = new Scan($this->Database); $Scan->ping_set_exit(false); // check address $this->validate_address_id(); // set result $result = array(); $result['scan_type'] = $Scan->icmp_type; $result['exit_code'] = $Scan->ping_address($this->old_address->ip); $result['result_code'] = $Scan->ping_exit_explain($result['exit_code']); $result['message'] = $result['exit_code'] == 0 ? "Address online" : "Address offline"; // success if ($result['exit_code'] == 0) { $Scan->ping_update_lastseen($this->_params->id); } return array("code" => 200, "data" => $result); } else { // fetch $result = $this->Addresses->fetch_address("id", $this->_params->id); // check result if ($result == false) { $this->Response->throw_exception(404, "Invalid Id"); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } } elseif (@$this->_params->id == "search") { // validate if (!$this->Addresses->validate_address($this->_params->id2)) { $this->Response->throw_exception(404, 'Invalid address'); } // search $result = $this->Tools->fetch_multiple_objects("ipaddresses", "ip_addr", $this->Subnets->transform_address($this->_params->id2, "decimal")); // check result if ($result === false) { $this->Response->throw_exception(404, 'Address not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, true, true)); } } elseif (@$this->_params->id == "search_hostname") { $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $this->_params->id2); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } elseif (@$this->_params->id == "search_hostbase") { $target = $this->_params->id2 . "%"; $result = $this->Tools->fetch_multiple_objects("ipaddresses", "dns_name", $target, "dns_name", true, true); // check result if ($result === false) { $this->Response->throw_exception(404, 'Host name not found'); } else { return array("code" => 200, "data" => $this->prepare_result($result, $this->_params->controller, false, false)); } } else { $this->Response->throw_exception(400, "Invalid Id"); } }