Skip to content

Commit

Permalink
More robust setting of the default discovery subnet attribute.
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-unwin committed Aug 16, 2021
1 parent 2e4dd8c commit 8429cf1
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 32 deletions.
27 changes: 18 additions & 9 deletions code_igniter/application/controllers/db_upgrades/db_4.2.0.php
Original file line number Diff line number Diff line change
Expand Up @@ -453,17 +453,26 @@
$this->db->query($sql);
$this->log_db($this->db->last_query() . ';');

$ips = $this->config->config['server_ip'];
$ips = server_ip();
$ips = explode(',', $ips);
$ip = trim($ips[0]);
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
$subnet = '';
foreach ($ips as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) and !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
break;
}
}

$sql = "INSERT INTO `discoveries` (id, name, org_id, description, type, subnet, edited_date, edited_by) VALUES (null,'Default Discovery',1,'Automatically created default discovery for $subnet.','subnet','$subnet',NOW(),'system')";
$this->db->query($sql);
$this->log_db($this->db->last_query() . ';');
if ($subnet !== '') {
$sql = "INSERT INTO `discoveries` (id, name, org_id, description, type, subnet, edited_date, edited_by) VALUES (null,'Default Discovery',1,'Automatically created default discovery for $subnet.','subnet','$subnet',NOW(),'system')";
$this->db->query($sql);
$this->log_db($this->db->last_query() . ';');
} else {
$this->log_db('WARNING - Could not determine a private IP for server, no default discovery created.');
}

// set our versions
$sql = "UPDATE `configuration` SET `value` = '20210810' WHERE `name` = 'internal_version'";
Expand Down
14 changes: 4 additions & 10 deletions code_igniter/application/controllers/util.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,30 +130,24 @@ public function audit_my_pc()

if ($client == 'aix') {
$filename = 'audit_aix.sh';

} elseif ($client == 'esxi') {
$filename = 'audit_esxi.sh';

} elseif ($client == 'hpux') {
$filename = 'audit_hpux.sh';

} elseif ($client == 'linux') {
$filename = 'audit_linux.sh';

} elseif ($client == 'osx') {
$filename = 'audit_osx.sh';

} elseif ($client == 'solaris') {
$filename = 'audit_solaris.sh';

} elseif ($client == 'windows') {
$filename = 'audit_windows.vbs';
}

$sql = "SELECT `id` AS `id` FROM `scripts` WHERE `name` = '$filename' ORDER BY id LIMIT 1";
$query = $this->db->query($sql);

foreach($query->result_array() as $row) {
foreach ($query->result_array() as $row) {
$id = $row['id'];
}

Expand Down Expand Up @@ -440,9 +434,9 @@ public function queue()
$this->db->query($sql);
// POP an item off the queue
$this->load->model('m_queue');
while ( true ) {
while (true) {
$item = $this->m_queue->pop();
if ( ! empty($item->details) && is_string($item->details)) {
if (!empty($item->details) && is_string($item->details)) {
$details = @json_decode($item->details);
}

Expand Down Expand Up @@ -493,7 +487,7 @@ public function queue()
if ($item->type === 'ip_scan') {
$result = ip_scan($details);
$result = json_encode($result);
if ( ! empty($result)) {
if (!empty($result)) {
$queue_item = new stdClass();
$queue_item->ip = $details->ip;
$queue_item->discovery_id = $details->discovery_id;
Expand Down
36 changes: 23 additions & 13 deletions code_igniter/application/models/m_discoveries.php
Original file line number Diff line number Diff line change
Expand Up @@ -403,15 +403,20 @@ public function read_for_discovery($id = 0)
return false;
}
if (empty($result[0]->subnet) and $result[0]->type === 'subnet' and $result[0]->name === 'Default Discovery') {
$ips = $this->config->config['server_ip'];
$ips = server_ip();
$ips = explode(',', $ips);
$ip = trim($ips[0]);
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
$subnet = '';
foreach ($ips as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) and !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
break;
}
}
$sql = "UPDATE discoveries SET subnet = '$subnet', description = 'Automatically created default discovery for $subnet.' WHERE id = $id";
$result = $this->run_sql($sql);
$this->run_sql($sql);
$result[0]->subnet = $subnet;
}
if (empty($result[0]->scan_options)) {
Expand Down Expand Up @@ -517,13 +522,18 @@ public function read($id = 0)
return false;
}
if (empty($result[0]->subnet) and $result[0]->type === 'subnet' and $result[0]->name === 'Default Discovery') {
$ips = $CI->config->config['ip'];
$ips = server_ip();
$ips = explode(',', $ips);
$ip = trim($ips[0]);
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
$subnet = '';
foreach ($ips as $ip) {
if (filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4) and !filter_var($ip, FILTER_VALIDATE_IP, FILTER_FLAG_NO_PRIV_RANGE)) {
$ip = explode('.', $ip);
$ip[3] = 0;
$ip = implode('.', $ip);
$subnet = $ip . '/24';
break;
}
}
$sql = "UPDATE discoveries SET subnet = '$subnet', description = 'Automatically created default discovery for $subnet.' WHERE id = $id";
$this->run_sql($sql);
$result[0]->subnet = $subnet;
Expand Down

0 comments on commit 8429cf1

Please sign in to comment.