Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/manage.h
Original file line number Diff line number Diff line change
Expand Up @@ -1603,9 +1603,6 @@ prognosis_iterator_description (iterator_t*);
int
manage_count_hosts (const char *, const char *);

gboolean
find_target_with_permission (const char *, target_t *, const char *);

int
create_target (const char*, const char*, const char*, const char*, const char*,
const char *, const char*, credential_t, credential_t,
Expand All @@ -1615,9 +1612,6 @@ create_target (const char*, const char*, const char*, const char*, const char*,
GPtrArray *, const char *,
const char *, target_t*);

int
copy_target (const char*, const char*, const char *, target_t*);

int
modify_target (const char*, const char*, const char*, const char*, const char*,
const char*, const char*, const char*, const char*, const char*,
Expand Down
171 changes: 0 additions & 171 deletions src/manage_sql.c
Original file line number Diff line number Diff line change
Expand Up @@ -441,28 +441,6 @@ parse_iso_time (const char *text_time)
return parse_iso_time_tz (text_time, current_credentials.timezone);
}

/**
* @brief Find a string in an array.
*
* @param[in] array Array.
* @param[in] string String.
*
* @return The string from the array if found, else NULL.
*/
static gchar*
array_find_string (array_t *array, const gchar *string)
{
guint index;
for (index = 0; index < array->len; index++)
{
gchar *ele;
ele = (gchar*) g_ptr_array_index (array, index);
if (ele && (strcmp (ele, string) == 0))
return ele;
}
return NULL;
}

/**
* @brief Find a string in a glib style string vector.
*
Expand Down Expand Up @@ -20179,22 +20157,6 @@ modify_task (const gchar *task_id, const gchar *name,

/* Targets. */

/**
* @brief Find a target for a specific permission, given a UUID.
*
* @param[in] uuid UUID of target.
* @param[out] target Target return, 0 if successfully failed to find target.
* @param[in] permission Permission.
*
* @return FALSE on success (including if failed to find target), TRUE on error.
*/
gboolean
find_target_with_permission (const char* uuid, target_t* target,
const char *permission)
{
return find_resource_with_permission ("target", uuid, target, permission, 0);
}

/**
* @brief Return number of hosts described by a hosts string.
*
Expand All @@ -20211,102 +20173,6 @@ manage_count_hosts (const char *given_hosts, const char *exclude_hosts)
manage_max_hosts ());
}

/**
* @brief Trim leading and trailing space from a hosts string.
*
* @param[in] string String. May be modified.
*
* @return Either string or some address within string.
*/
static gchar *
trim_hosts (gchar *string)
{
gchar *host, *end;

/* Trim leading and trailing space. */
host = string;
while ((*host == ' ') || (*host == '\t'))
host++;
end = host;
while (*end)
{
if ((*end == ' ') || (*end == '\t'))
{
*end = '\0';
break;
}
end++;
}
return host;
}

/**
* @brief Clean a hosts string.
*
* @param[in] given_hosts String describing hosts.
* @param[out] max Max number of hosts, adjusted for duplicates.
*
* @return Freshly allocated new hosts string, or NULL on error.
*/
gchar*
clean_hosts (const char *given_hosts, int *max)
{
array_t *clean_array;
GString *clean;
gchar **split, **point, *hosts, *hosts_start, *host;
guint index;

/* Treat newlines like commas. */
hosts = hosts_start = g_strdup (given_hosts);
while (*hosts)
{
if (*hosts == '\n') *hosts = ',';
hosts++;
}

split = g_strsplit (hosts_start, ",", 0);
g_free (hosts_start);
point = split;

if ((point == NULL) || (*point == NULL))
{
g_strfreev (split);
return g_strdup ("");
}

clean_array = make_array ();
while (*point)
{
host = trim_hosts (*point);

if (*host)
{
/* Prevent simple duplicates. */
if (array_find_string (clean_array, host) == NULL)
array_add (clean_array, host);
else if (max)
(*max)--;
}

point += 1;
}

clean = g_string_new ("");

host = (gchar*) g_ptr_array_index (clean_array, 0);
if (host)
g_string_append_printf (clean, "%s", host);

for (index = 1; index < clean_array->len; index++)
{
host = (gchar*) g_ptr_array_index (clean_array, index);
if (host)
g_string_append_printf (clean, ", %s", host);
}

return g_string_free (clean, FALSE);
}

/**
* @brief Validate a single port.
*
Expand Down Expand Up @@ -20899,43 +20765,6 @@ create_target (const char* name, const char* asset_hosts_filter,
return 0;
}

/**
* @brief Create a target from an existing target.
*
* @param[in] name Name of new target. NULL to copy from existing.
* @param[in] comment Comment on new target. NULL to copy from existing.
* @param[in] target_id UUID of existing target.
* @param[out] new_target New target.
*
* @return 0 success, 1 target exists already, 2 failed to find existing
* target, 99 permission denied, -1 error.
*/
int
copy_target (const char* name, const char* comment, const char *target_id,
target_t* new_target)
{
int ret;
target_t old_target;

assert (new_target);

ret = copy_resource ("target", name, comment, target_id,
"hosts, exclude_hosts, port_list, reverse_lookup_only,"
" reverse_lookup_unify, alive_test,"
" allow_simultaneous_ips",
1, new_target, &old_target);
if (ret)
return ret;

sql ("INSERT INTO targets_login_data (target, type, credential, port)"
" SELECT %llu, type, credential, port"
" FROM targets_login_data"
" WHERE target = %llu;",
*new_target, old_target);

return 0;
}

/**
* @brief Delete a target.
*
Expand Down
3 changes: 0 additions & 3 deletions src/manage_sql.h
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,4 @@ ldap_auth_enabled ();
int
radius_auth_enabled ();

gchar*
clean_hosts (const char *, int *);

#endif /* not _GVMD_MANAGE_SQL_H */
56 changes: 56 additions & 0 deletions src/manage_sql_targets.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,34 @@
*/

#include "manage_sql_targets.h"
#include "manage_sql_resources.h"
#include "sql.h"

#include <assert.h>

/**
* @file
* @brief GVM management layer: Targets SQL
*
* The Targets SQL for the GVM management layer.
*/

/**
* @brief Find a target for a specific permission, given a UUID.
*
* @param[in] uuid UUID of target.
* @param[out] target Target return, 0 if successfully failed to find target.
* @param[in] permission Permission.
*
* @return FALSE on success (including if failed to find target), TRUE on error.
*/
gboolean
find_target_with_permission (const char* uuid, target_t* target,
const char *permission)
{
return find_resource_with_permission ("target", uuid, target, permission, 0);
}

/**
* @brief Return the UUID of a target.
*
Expand Down Expand Up @@ -324,3 +343,40 @@ target_krb5_credential (target_t target)
{
return target_credential (target, "krb5");
}

/**
* @brief Create a target from an existing target.
*
* @param[in] name Name of new target. NULL to copy from existing.
* @param[in] comment Comment on new target. NULL to copy from existing.
* @param[in] target_id UUID of existing target.
* @param[out] new_target New target.
*
* @return 0 success, 1 target exists already, 2 failed to find existing
* target, 99 permission denied, -1 error.
*/
int
copy_target (const char* name, const char* comment, const char *target_id,
target_t* new_target)
{
int ret;
target_t old_target;

assert (new_target);

ret = copy_resource ("target", name, comment, target_id,
"hosts, exclude_hosts, port_list, reverse_lookup_only,"
" reverse_lookup_unify, alive_test,"
" allow_simultaneous_ips",
1, new_target, &old_target);
if (ret)
return ret;

sql ("INSERT INTO targets_login_data (target, type, credential, port)"
" SELECT %llu, type, credential, port"
" FROM targets_login_data"
" WHERE target = %llu;",
*new_target, old_target);

return 0;
}
6 changes: 6 additions & 0 deletions src/manage_targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ manage_max_hosts ();
void
manage_set_max_hosts (int);

gboolean
find_target_with_permission (const char *, target_t *, const char *);

char*
target_uuid (target_t);

Expand Down Expand Up @@ -44,4 +47,7 @@ target_allow_simultaneous_ips (target_t);
char*
target_ssh_port (target_t);

int
copy_target (const char *, const char *, const char *, target_t *);

#endif /* not _GVMD_MANAGE_TARGETS_H */
Loading
Loading