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

Extend script_get_preference() to get the value by id. #470

Merged
merged 2 commits into from
Apr 3, 2020
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
[#458](https://github.com/greenbone/openvas/pull/458)
[#459](https://github.com/greenbone/openvas/pull/459)
- Fix format-truncation warning in GCC 8.2 and later. [#461](https://github.com/greenbone/openvas/pull/461)
- Extend script_get_preference() to get the value by id. [#470](https://github.com/greenbone/openvas/pull/470)

### Changed
- The logging of the NASL internal regexp functions was extended to include the pattern in case of a failed regcomp(). [#397](https://github.com/greenbone/openvas/pull/397)
Expand Down
61 changes: 46 additions & 15 deletions misc/plugutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -392,34 +392,63 @@ post_error (const char *oid, struct script_infos *desc, int port,
proto_post_error (oid, desc, port, "tcp", action);
}

/**
* @brief Get the a plugins preference.
*
* Search in the preferences set by the client. If it is not
* present, search in redis cache for the default.
*
* @param[in] oid Script OID to get the preference from
* @param[in] name Name of the preference to get
* @param[in] pref_id Id of the preferences to get
*
* @return script preference on success, Null otherwise.
**/
char *
get_plugin_preference (const char *oid, const char *name)
get_plugin_preference (const char *oid, const char *name, int pref_id)
{
GHashTable *prefs;
GHashTableIter iter;
char *cname, *retval = NULL;
char *cname = NULL, *retval = NULL;
void *itername, *itervalue;
char prefix[1024], suffix[1024];

prefs = preferences_get ();
if (!prefs || !nvticache_initialized () || !oid || !name)
if (!prefs || !nvticache_initialized () || !oid || (!name && pref_id < 1))
return NULL;

cname = g_strdup (name);
g_strchomp (cname);
g_hash_table_iter_init (&iter, prefs);
snprintf (prefix, sizeof (prefix), "%s:", oid);
snprintf (suffix, sizeof (suffix), ":%s", cname);
/* NVT preferences receiveed in OID:PrefID:PrefType:PrefName form */
while (g_hash_table_iter_next (&iter, &itername, &itervalue))

if (pref_id > 0)
{
if (g_str_has_prefix (itername, prefix)
&& g_str_has_suffix (itername, suffix))
snprintf (prefix, sizeof (prefix), "%s:%d:", oid, pref_id);
while (g_hash_table_iter_next (&iter, &itername, &itervalue))
{
retval = g_strdup (itervalue);
break;
if (g_str_has_prefix (itername, prefix))
{
retval = g_strdup (itervalue);
break;
}
}
}
else
{
cname = g_strdup (name);
g_strchomp (cname);
snprintf (prefix, sizeof (prefix), "%s:", oid);
snprintf (suffix, sizeof (suffix), ":%s", cname);
/* NVT preferences received in OID:PrefID:PrefType:PrefName form */
while (g_hash_table_iter_next (&iter, &itername, &itervalue))
{
if (g_str_has_prefix (itername, prefix)
&& g_str_has_suffix (itername, suffix))
{
retval = g_strdup (itervalue);
break;
}
}
}

/* If no value set by the user, get the default one. */
if (!retval)
{
Expand All @@ -428,7 +457,8 @@ get_plugin_preference (const char *oid, const char *name)
tmp = nprefs = nvticache_get_prefs (oid);
while (tmp)
{
if (!strcmp (cname, nvtpref_name (tmp->data)))
if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
|| (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
{
retval = g_strdup (nvtpref_default (tmp->data));
break;
Expand All @@ -437,7 +467,8 @@ get_plugin_preference (const char *oid, const char *name)
}
g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
}
g_free (cname);
if (cname)
g_free (cname);
return retval;
}

Expand Down
2 changes: 1 addition & 1 deletion misc/plugutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ char *
plug_get_host_ip_str (struct script_infos *);

char *
get_plugin_preference (const char *, const char *);
get_plugin_preference (const char *, const char *, int);

const char *
get_plugin_preference_fname (struct script_infos *, const char *);
Expand Down
18 changes: 9 additions & 9 deletions nasl/nasl_builtin_find_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -1495,9 +1495,9 @@ plugin_do_run (struct script_infos *desc, GSList *h, int test_ssl)

int rw_timeout = 20, cnx_timeout = 20, wrap_timeout = 20;
int x, timeout;
char *rw_timeout_s = get_plugin_preference (oid, RW_TIMEOUT_PREF);
char *cnx_timeout_s = get_plugin_preference (oid, CNX_TIMEOUT_PREF);
char *wrap_timeout_s = get_plugin_preference (oid, WRAP_TIMEOUT_PREF);
char *rw_timeout_s = get_plugin_preference (oid, RW_TIMEOUT_PREF, -1);
char *cnx_timeout_s = get_plugin_preference (oid, CNX_TIMEOUT_PREF, -1);
char *wrap_timeout_s = get_plugin_preference (oid, WRAP_TIMEOUT_PREF, -1);
unsigned char *p;
fd_set rfds, wfds;
struct timeval tv;
Expand Down Expand Up @@ -2405,11 +2405,11 @@ plugin_run_find_service (lex_ctxt *lexic)
int port_per_son;
int i;
int test_ssl = 1;
char *key = get_plugin_preference (oid, KEY_FILE);
char *cert = get_plugin_preference (oid, CERT_FILE);
char *pempass = get_plugin_preference (oid, PEM_PASS);
char *cafile = get_plugin_preference (oid, CA_FILE);
char *test_ssl_s = get_plugin_preference (oid, TEST_SSL_PREF);
char *key = get_plugin_preference (oid, KEY_FILE, -1);
char *cert = get_plugin_preference (oid, CERT_FILE, -1);
char *pempass = get_plugin_preference (oid, PEM_PASS, -1);
char *cafile = get_plugin_preference (oid, CA_FILE, -1);
char *test_ssl_s = get_plugin_preference (oid, TEST_SSL_PREF, -1);

if (key && key[0] != '\0')
key = (char *) get_plugin_preference_fname (desc, key);
Expand Down Expand Up @@ -2448,7 +2448,7 @@ plugin_run_find_service (lex_ctxt *lexic)

signal (SIGTERM, sigterm);
signal (SIGCHLD, sigchld);
num_sons_s = get_plugin_preference (oid, NUM_CHILDREN);
num_sons_s = get_plugin_preference (oid, NUM_CHILDREN, -1);
if (num_sons_s != NULL)
num_sons = atoi (num_sons_s);
g_free (num_sons_s);
Expand Down
8 changes: 4 additions & 4 deletions nasl/nasl_builtin_nmap.c
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ nmap_create (lex_ctxt *lexic)
nmap->oid = lexic->oid;

/* import results from external file? */
pref = get_plugin_preference (lexic->oid, PREF_IMPORT_XML_FILE);
pref = get_plugin_preference (lexic->oid, PREF_IMPORT_XML_FILE, -1);
if (!pref || !strlen (pref))
{
/* no: build command line to execute */
Expand Down Expand Up @@ -659,7 +659,7 @@ build_cmd_line (nmap_t *nmap)
{
gchar *optval;

optval = get_plugin_preference (nmap->oid, options[i].optname);
optval = get_plugin_preference (nmap->oid, options[i].optname, -1);
if (!optval)
continue;

Expand Down Expand Up @@ -877,7 +877,7 @@ add_scantype_arguments (nmap_t *nmap)
{"SCTP Init", "-sY", FALSE}, {"SCTP COOKIE_ECHO", "-sZ", FALSE},
{NULL, NULL, FALSE}};

scantype = get_plugin_preference (nmap->oid, PREF_TCP_SCANNING_TECHNIQUE);
scantype = get_plugin_preference (nmap->oid, PREF_TCP_SCANNING_TECHNIQUE, -1);
if (!scantype)
return -1;

Expand Down Expand Up @@ -906,7 +906,7 @@ add_timing_arguments (nmap_t *nmap)
{"Aggressive", "-T4", FALSE}, {"Insane", "-T5", FALSE},
{NULL, NULL, FALSE}};

timing = get_plugin_preference (nmap->oid, PREF_TIMING_POLICY);
timing = get_plugin_preference (nmap->oid, PREF_TIMING_POLICY, -1);
if (!timing)
return -1;

Expand Down
2 changes: 1 addition & 1 deletion nasl/nasl_http.c
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ _http_req (lex_ctxt *lexic, char *keyword)
return NULL;
/* global_settings.nasl */
ua = get_plugin_preference ("1.3.6.1.4.1.25623.1.0.12288",
"HTTP User-Agent");
"HTTP User-Agent", -1);
if (!ua || strlen (g_strstrip (ua)) == 0)
{
g_free (ua);
Expand Down
24 changes: 18 additions & 6 deletions nasl/nasl_scanner_glue.c
Original file line number Diff line number Diff line change
Expand Up @@ -492,23 +492,35 @@ script_add_preference (lex_ctxt *lexic)
return FAKE_CELL;
}

/**
* @brief Get a preferences of the current script.
*
* Search the preference by preference name or by preferences id.
*
* @param[in] lexic NASL lexer.
*
* @return lex cell containing the preferences value as a string.
* Fake cell otherwise
*/
tree_cell *
script_get_preference (lex_ctxt *lexic)
{
tree_cell *retc;
int id = get_int_var_by_name (lexic, "id", -1);
char *pref = get_str_var_by_num (lexic, 0);
char *value;

if (pref == NULL)
if (pref == NULL && id == -1)
{
nasl_perror (lexic,
"Argument error in the function script_get_preference()\n");
nasl_perror (
lexic, "Function usage is : pref = script_get_preference(<name>)\n");
nasl_perror (lexic,
"Function usage is : pref = script_get_preference(<name>, "
"id:<id>)\n");
return FAKE_CELL;
}

value = get_plugin_preference (lexic->oid, pref);
value = get_plugin_preference (lexic->oid, pref, id);
if (value != NULL)
{
retc = alloc_typed_cell (CONST_INT);
Expand Down Expand Up @@ -546,7 +558,7 @@ script_get_preference_file_content (lex_ctxt *lexic)
return NULL;
}

value = get_plugin_preference (lexic->oid, pref);
value = get_plugin_preference (lexic->oid, pref, -1);
if (value == NULL)
return NULL;

Expand Down Expand Up @@ -587,7 +599,7 @@ script_get_preference_file_location (lex_ctxt *lexic)
return NULL;
}

value = get_plugin_preference (lexic->oid, pref);
value = get_plugin_preference (lexic->oid, pref, -1);
if (value == NULL)
{
nasl_perror (
Expand Down