Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for volatile keys #682

Merged
merged 3 commits into from
Mar 17, 2021
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
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
## [21.10] (unreleased)

### Added

- Add support for volatile keys. [#682](https://github.com/greenbone/openvas/pull/682)

### Changed
### Removed
### Fixed
Expand Down
51 changes: 51 additions & 0 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -684,6 +684,57 @@ plug_set_key (struct script_infos *args, char *name, int type,
plug_set_key_len (args, name, type, value, 0);
}

/**
* @brief Set volatile key with expire.
*
* @param args Script infos.
* @param name Key name.
* @param type Key type.
* @param value Key value.
* @param expire Key expire in seconds.
* @param len Len of value.
*/
void
plug_set_key_len_volatile (struct script_infos *args, char *name, int type,
const void *value, int expire, size_t len)
{
kb_t kb = plug_get_kb (args);
int pos = 0; // Append the item on the right position of the list

if (name == NULL || value == NULL || expire == -1)
return;

if (type == ARG_STRING)
kb_add_str_unique_volatile (kb, name, value, expire, len, pos);
else if (type == ARG_INT)
kb_add_int_unique_volatile (kb, name, GPOINTER_TO_SIZE (value),
GPOINTER_TO_SIZE (expire));
if (global_nasl_debug == 1)
{
if (type == ARG_STRING)
g_message ("set volatile key %s -> %s", name, (char *) value);
else if (type == ARG_INT)
g_message ("set volatile key %s -> %d", name,
(int) GPOINTER_TO_SIZE (value));
}
}

/**
* @brief Set volatile key with expire.
*
* @param args Script infos.
* @param name Key name.
* @param type Key type.
* @param value Key value.
* @param expire Key expire in seconds.
*/
void
plug_set_key_volatile (struct script_infos *args, char *name, int type,
const void *value, int expire)
{
plug_set_key_len_volatile (args, name, type, value, expire, 0);
}

void
plug_replace_key_len (struct script_infos *args, char *name, int type,
void *value, size_t len)
Expand Down
7 changes: 7 additions & 0 deletions misc/plugutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,13 @@ plug_set_key (struct script_infos *, char *, int, const void *);
void
plug_set_key_len (struct script_infos *, char *, int, const void *, size_t);

void
plug_set_key_volatile (struct script_infos *, char *, int, const void *, int);

void
plug_set_key_len_volatile (struct script_infos *, char *, int, const void *,
int, size_t);

void
plug_replace_key (struct script_infos *, char *, int, void *);

Expand Down
72 changes: 72 additions & 0 deletions nasl/nasl_scanner_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -832,12 +832,84 @@ replace_kb_item (lex_ctxt *lexic)
return FAKE_CELL;
}

/**
* @brief Set a volate kb item.
*
* @param[in] lexic NASL lexer.
* @param[in] name Name of Item.
* @param[in] value Value of Item.
* @param[in] expire Optional expire for item in seconds.
*
* @return FAKE_CELL
*/
tree_cell *
set_kb_item_volatile (lex_ctxt *lexic)
{
struct script_infos *script_infos = lexic->script_infos;
char *name = get_str_var_by_name (lexic, "name");
int type = get_var_type_by_name (lexic, "value");
int expire = get_int_var_by_name (lexic, "expire", -1);

if (name == NULL)
{
nasl_perror (lexic, "Syntax error with set_kb_item() [null name]\n",
name);
return FAKE_CELL;
}

if (type == VAR2_INT)
{
int value = get_int_var_by_name (lexic, "value", -1);
if (value != -1 && expire != -1)
plug_set_key_volatile (script_infos, name, ARG_INT,
GSIZE_TO_POINTER (value), expire);
else
nasl_perror (lexic,
"Syntax error with set_kb_item() [value=-1 or expire=-1 "
"for name '%s']\n",
name);
}
else
{
char *value = get_str_var_by_name (lexic, "value");
int len = get_var_size_by_name (lexic, "value");
if (value == NULL || expire == -1)
{
nasl_perror (lexic,
"Syntax error with set_kb_item() [null value or "
"expire=-1 for name '%s']\n",
name);
return FAKE_CELL;
}
plug_set_key_len_volatile (script_infos, name, ARG_STRING, value, expire,
len);
}

return FAKE_CELL;
}

/**
* @brief Set a kb item.
*
* If expire is set the key will be removed after it expired.
*
* @param[in] lexic NASL lexer.
* @param[in] name Name of Item.
* @param[in] value Value of Item.
* @param[in] expire Optional expire for item in seconds.
*
* @return FAKE_CELL
*/
tree_cell *
set_kb_item (lex_ctxt *lexic)
{
struct script_infos *script_infos = lexic->script_infos;
char *name = get_str_var_by_name (lexic, "name");
int type = get_var_type_by_name (lexic, "value");
int expire = get_int_var_by_name (lexic, "expire", -1);

if (expire != -1)
return set_kb_item_volatile (lexic);

if (name == NULL)
{
Expand Down