Skip to content

Commit

Permalink
Change: rename kb_check*() functions to kb_item_*_with_main_kb_check.
Browse files Browse the repository at this point in the history
This makes the name more clear, that an item can be stored in a kb (host or main kb)
but the main kb will check for validity.
Also, improve docstrings.

Don't use the old kb_check_* functions where an existing main_kb can't be provide:
- plugin upload and nvtcache related stuff.
- initializing step, where a main kb was not provided yet.
- openvas-nasl standalone tool.
  • Loading branch information
jjnicola authored and ArnoStiefvater committed Nov 28, 2022
1 parent acf53f7 commit 641f2b8
Show file tree
Hide file tree
Showing 10 changed files with 75 additions and 47 deletions.
7 changes: 4 additions & 3 deletions misc/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -1970,7 +1970,7 @@ open_sock_tcp (struct script_infos *args, unsigned int port, int timeout)
{
g_message ("open_sock_tcp: %s:%d time-out.", ip_str, port);
log_count++;
kb_check_set_int (kb, buffer, log_count);
kb_item_set_int_with_main_kb_check (kb, buffer, log_count);
}
if ((log_count >= attempts) && (attempts != 0))
{
Expand All @@ -1985,7 +1985,7 @@ open_sock_tcp (struct script_infos *args, unsigned int port, int timeout)
g_message ("open_sock_tcp: %s:%d too many timeouts. "
"This port will be set to closed.",
host_port_ip_str, port);
kb_check_set_int (kb, buffer, 0);
kb_item_set_int_with_main_kb_check (kb, buffer, 0);

addr6_to_str (args->ip, host_port_ip_str);
snprintf (
Expand All @@ -1994,7 +1994,8 @@ open_sock_tcp (struct script_infos *args, unsigned int port, int timeout)
" was set to closed.",
host_port_ip_str,
plug_current_vhost () ? plug_current_vhost () : " ", port);
kb_check_push_str (args->results, "internal/results", buffer);
kb_item_push_str_with_main_kb_check (args->results,
"internal/results", buffer);
}
}
g_free (ip_str);
Expand Down
52 changes: 34 additions & 18 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,8 @@ msg_type_to_str (msg_t type)
* original scan main kb.
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
Expand Down Expand Up @@ -458,13 +460,12 @@ get_main_kb (void)
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param main_kb Current main kb.
* @param name key name to be used in the kb
*
* @return 0 on success, -1 on inconsistency.
*/
static int
Expand Down Expand Up @@ -507,18 +508,21 @@ check_kb_inconsistency_log (void)
* original scanid, if it matches it kb_item_push_str.
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_push_str (kb_t kb, const char *name, const char *value)
kb_item_push_str_with_main_kb_check (kb_t kb, const char *name,
const char *value)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_push_str (kb, name, value) : -1;
Expand All @@ -529,18 +533,21 @@ kb_check_push_str (kb_t kb, const char *name, const char *value)
* original scanid, if it matches it call kb_item_set_str.
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_set_str (kb_t kb, const char *name, const char *value, size_t len)
kb_item_set_str_with_main_kb_check (kb_t kb, const char *name,
const char *value, size_t len)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_set_str (kb, name, value, len) : -1;
Expand All @@ -551,19 +558,22 @@ kb_check_set_str (kb_t kb, const char *name, const char *value, size_t len)
* original scanid, if it matches it call kb_item_add_str_unique.
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_add_str_unique (kb_t kb, const char *name, const char *value,
size_t len, int pos)
kb_item_add_str_unique_with_main_kb_check (kb_t kb, const char *name,
const char *value, size_t len,
int pos)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_add_str_unique (kb, name, value, len, pos) : -1;
Expand All @@ -574,18 +584,20 @@ kb_check_add_str_unique (kb_t kb, const char *name, const char *value,
* original scanid, if it matches it call kb_item_set_int.
* @description Compares the scan id in get_scan_id, set at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_set_int (kb_t kb, const char *name, int value)
kb_item_set_int_with_main_kb_check (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_set_int (kb, name, value) : -1;
Expand All @@ -596,18 +608,20 @@ kb_check_set_int (kb_t kb, const char *name, int value)
* original scanid, if it matches it call kb_item_add_int.
* @description Compares the scan id in get_scan_id, add at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_add_int (kb_t kb, const char *name, int value)
kb_item_add_int_with_main_kb_check (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_add_int (kb, name, value) : -1;
Expand All @@ -618,18 +632,20 @@ kb_check_add_int (kb_t kb, const char *name, int value)
* original scanid, if it matches it call kb_item_add_int_unique.
* @description Compares the scan id in get_scan_id, add at the beginning
* of the scan, with the one found in the main kb.
* Therefore it is mandatory that the global main_kb
* variable to be set.
* It helps to detect that the kb was not taken by another
* task/scan, and that the current plugins does not stores
* results in a wrong kb.
*
* @param kb Current main kb.
* @param kb Kb where to store the item into.
* @param name key for the given value.
* @param value to store under key within kb.
*
* @return 0 on success, -1 on inconsistency.
*/
int
kb_check_add_int_unique (kb_t kb, const char *name, int value)
kb_item_add_int_unique_with_main_kb_check (kb_t kb, const char *name, int value)
{
int result = check_kb_inconsistency_log ();
return result == 0 ? kb_item_add_int_unique (kb, name, value) : -1;
Expand Down Expand Up @@ -694,7 +710,7 @@ proto_post_wrapped (const char *oid, struct script_infos *desc, int port,
}

kb = plug_get_results_kb (desc);
kb_check_push_str (kb, "internal/results", data);
kb_item_push_str_with_main_kb_check (kb, "internal/results", data);
g_free (data);
g_free (buffer);
g_string_free (action_str, TRUE);
Expand Down Expand Up @@ -1057,7 +1073,7 @@ plug_replace_key_len (struct script_infos *args, char *name, int type,
return;

if (type == ARG_STRING)
kb_check_set_str (kb, name, value, len);
kb_item_set_str (kb, name, value, len);
else if (type == ARG_INT)
kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
if (global_nasl_debug == 1)
Expand Down
13 changes: 7 additions & 6 deletions misc/plugutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,22 +153,23 @@ kb_t
get_main_kb (void);

int
kb_check_push_str (kb_t, const char *, const char *);
kb_item_push_str_with_main_kb_check (kb_t, const char *, const char *);

int
kb_check_set_str (kb_t, const char *, const char *, size_t);
kb_item_set_str_with_main_kb_check (kb_t, const char *, const char *, size_t);

int
kb_check_add_str_unique (kb_t, const char *, const char *, size_t, int);
kb_item_add_str_unique_with_main_kb_check (kb_t, const char *, const char *,
size_t, int);

int
kb_check_set_int (kb_t, const char *, int);
kb_item_set_int_with_main_kb_check (kb_t, const char *, int);

int
kb_check_add_int (kb_t, const char *, int);
kb_item_add_int_with_main_kb_check (kb_t, const char *, int);

int
kb_check_add_int_unique (kb_t, const char *, int);
kb_item_add_int_unique_with_main_kb_check (kb_t, const char *, int);

void
plug_set_key (struct script_infos *, char *, int, const void *);
Expand Down
2 changes: 1 addition & 1 deletion nasl/nasl.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ main (int argc, char **argv)
*kb_values_aux);
exit (1);
}
kb_check_add_str_unique (kb, splits[0], splits[1], 0, pos);
kb_item_add_str_unique (kb, splits[0], splits[1], 0, pos);
kb_values_aux++;
g_strfreev (splits);
}
Expand Down
25 changes: 16 additions & 9 deletions src/attack.c
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,18 @@ connect_main_kb (kb_t *main_kb)
/**
* @brief Add the Host KB index to the list of readable KBs
* used by ospd-openvas.
*
* @param host_kb_index The Kb index used for the host, to be stored
* in a list key in the main_kb.
*/
static void
set_kb_readable (int host_kb_index)
{
kb_t main_kb = NULL;

connect_main_kb (&main_kb);
kb_check_add_int_unique (main_kb, "internal/dbindex", host_kb_index);
kb_item_add_int_unique_with_main_kb_check (main_kb, "internal/dbindex",
host_kb_index);
kb_lnk_reset (main_kb);
}

Expand All @@ -155,7 +159,7 @@ set_scan_status (char *status)
}
scan_id = kb_item_get_str (main_kb, ("internal/scanid"));
snprintf (buffer, sizeof (buffer), "internal/%s", scan_id);
kb_check_set_str (main_kb, buffer, status, 0);
kb_item_set_str_with_main_kb_check (main_kb, buffer, status, 0);
kb_lnk_reset (main_kb);
g_free (scan_id);
}
Expand Down Expand Up @@ -187,7 +191,7 @@ comm_send_status_host_dead (kb_t main_kb, char *ip_str)
if (strlen (ip_str) > 1998)
return -1;
status = g_strjoin ("/", ip_str, host_dead_status_code, NULL);
kb_check_push_str (main_kb, topic, status);
kb_item_push_str_with_main_kb_check (main_kb, topic, status);
g_free (status);

return 0;
Expand Down Expand Up @@ -221,7 +225,7 @@ comm_send_status (kb_t main_kb, char *ip_str, int curr, int max)
return -1;

snprintf (status_buf, sizeof (status_buf), "%s/%d/%d", ip_str, curr, max);
kb_check_push_str (main_kb, "internal/status", status_buf);
kb_item_push_str_with_main_kb_check (main_kb, "internal/status", status_buf);
kb_lnk_reset (main_kb);

return 0;
Expand All @@ -236,7 +240,7 @@ message_to_client (kb_t kb, const char *msg, const char *ip_str,
buf = g_strdup_printf ("%s|||%s|||%s|||%s||| |||%s", type,
ip_str ? ip_str : "", ip_str ? ip_str : "",
port ? port : " ", msg ? msg : "No error.");
kb_check_push_str (kb, "internal/results", buf);
kb_item_push_str_with_main_kb_check (kb, "internal/results", buf);
g_free (buf);
}

Expand Down Expand Up @@ -683,7 +687,8 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
"<name>Host dead</name><value>1</value><source>"
"<description/><type/><name/></source></detail></host>",
ip_str);
kb_check_push_str (args->main_kb, "internal/results", buffer);
kb_item_push_str_with_main_kb_check (
args->main_kb, "internal/results", buffer);

comm_send_status_host_dead (args->main_kb, ip_str);
goto host_died;
Expand Down Expand Up @@ -749,7 +754,8 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip,
buffer, sizeof (buffer),
"ERRMSG|||%s||| ||| ||| ||| Unable to launch table driven lsc",
ip_str);
kb_check_push_str (args->main_kb, "internal/results", buffer);
kb_item_push_str_with_main_kb_check (args->main_kb,
"internal/results", buffer);
g_warning ("%s: Unable to launch table driven LSC", __func__);
}
}
Expand Down Expand Up @@ -916,7 +922,8 @@ attack_start (struct ipc_context *ipcc, struct attack_start_args *args)
kb_lnk_reset (main_kb);
gettimeofday (&then, NULL);

kb_check_set_str (kb, "internal/scan_id", globals->scan_id, 0);
kb_item_set_str_with_main_kb_check (kb, "internal/scan_id", globals->scan_id,
0);
set_kb_readable (kb_get_kb_index (kb));

/* The reverse lookup is delayed to this step in order to not slow down the
Expand All @@ -937,7 +944,7 @@ attack_start (struct ipc_context *ipcc, struct attack_start_args *args)
message_to_client (kb, "Host access denied (system-wide restriction.)",
ip_str, NULL, "ERRMSG");

kb_check_set_str (kb, "internal/host_deny", "True", 0);
kb_item_set_str_with_main_kb_check (kb, "internal/host_deny", "True", 0);
g_warning ("Host %s access denied.", ip_str);
return;
}
Expand Down
2 changes: 1 addition & 1 deletion src/attack_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ Ensure (attack, comm_send_status_sends_correct_text)
/* Create a dummy kb. */
kb = &kb_struct;

/* We can't wrap kb_check_push_str because it is inline, so we have to do
/* We can't wrap kb_item_push_str because it is inline, so we have to do
* a little hacking. */
kb_ops_struct.kb_push_str = __wrap_redis_push_str;
kb_ops_struct.kb_lnk_reset = __wrap_redis_lnk_reset;
Expand Down
2 changes: 1 addition & 1 deletion src/hosts.c
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ host_set_time (kb_t kb, char *ip, char *type)
timestr);
g_free (timestr);

kb_check_push_str (kb, "internal/results", log_msg);
kb_item_push_str_with_main_kb_check (kb, "internal/results", log_msg);
}

static void
Expand Down
9 changes: 5 additions & 4 deletions src/openvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals)
}
kb_del_items (kb, key);
snprintf (key, sizeof (key), "internal/%s", globals->scan_id);
kb_check_set_str (kb, key, "ready", 0);
kb_check_set_int (kb, "internal/ovas_pid", getpid ());
kb_item_set_str_with_main_kb_check (kb, key, "ready", 0);
kb_item_set_int_with_main_kb_check (kb, "internal/ovas_pid", getpid ());
kb_lnk_reset (kb);

g_debug ("End loading scan preferences.");
Expand Down Expand Up @@ -401,11 +401,12 @@ send_message_to_client_and_finish_scan (const char *msg)
char key[1024];
kb_t kb;

// We get the main kb. It is still not set as global at this point.
snprintf (key, sizeof (key), "internal/%s/scanprefs", get_scan_id ());
kb = kb_find (prefs_get ("db_address"), key);
kb_check_push_str (kb, "internal/results", msg);
kb_item_push_str (kb, "internal/results", msg);
snprintf (key, sizeof (key), "internal/%s", get_scan_id ());
kb_check_set_str (kb, key, "finished", 0);
kb_item_set_str (kb, key, "finished", 0);
kb_lnk_reset (kb);
}

Expand Down

0 comments on commit 641f2b8

Please sign in to comment.