diff --git a/CHANGELOG.md b/CHANGELOG.md index d1fb62695c..69a4ab498b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/misc/plugutils.c b/misc/plugutils.c index 7949da51ed..942a436116 100644 --- a/misc/plugutils.c +++ b/misc/plugutils.c @@ -393,33 +393,50 @@ post_error (const char *oid, struct script_infos *desc, int port, } 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) { @@ -428,7 +445,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; @@ -437,7 +455,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; } diff --git a/misc/plugutils.h b/misc/plugutils.h index 86e235db46..45793c8e46 100644 --- a/misc/plugutils.h +++ b/misc/plugutils.h @@ -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 *); diff --git a/nasl/nasl_builtin_find_service.c b/nasl/nasl_builtin_find_service.c index 3e7048b885..6d27aef1ab 100644 --- a/nasl/nasl_builtin_find_service.c +++ b/nasl/nasl_builtin_find_service.c @@ -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; @@ -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); @@ -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); diff --git a/nasl/nasl_builtin_nmap.c b/nasl/nasl_builtin_nmap.c index 0783550fa7..cba9443eb4 100644 --- a/nasl/nasl_builtin_nmap.c +++ b/nasl/nasl_builtin_nmap.c @@ -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 */ @@ -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; @@ -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; @@ -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; diff --git a/nasl/nasl_http.c b/nasl/nasl_http.c index b43d898922..10cb62e1f8 100644 --- a/nasl/nasl_http.c +++ b/nasl/nasl_http.c @@ -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); diff --git a/nasl/nasl_scanner_glue.c b/nasl/nasl_scanner_glue.c index e58a48291a..3f49f1bd80 100644 --- a/nasl/nasl_scanner_glue.c +++ b/nasl/nasl_scanner_glue.c @@ -496,19 +496,21 @@ 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()\n"); + nasl_perror (lexic, + "Function usage is : pref = script_get_preference(, " + "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); @@ -546,7 +548,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; @@ -587,7 +589,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 (