Skip to content

Commit 6759253

Browse files
Merge pull request #399 from jjnicola/file-type-prefs-7
Fix handling of file type nvt preferences. (openvas-7)
2 parents b5c3a26 + b7e540b commit 6759253

File tree

4 files changed

+128
-6
lines changed

4 files changed

+128
-6
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
3838
- Set a key in redis when the scan finishes and fix stop scan using the right pid. [#390](https://github.com/greenbone/openvas/pull/390)
3939
- Fix detection of finger service. [#391](https://github.com/greenbone/openvas/pull/391)
4040
- Wait for zombie process in case of timed out nvts. [#379](https://github.com/greenbone/openvas/pull/379)
41+
- Fix handling of file type nvt preferences. [#399](https://github.com/greenbone/openvas/pull/399)
4142

4243
### Removed
4344
- Unused be_nice scan preferences has been removed. [#313](https://github.com/greenbone/openvas/pull/313)

src/openvas.c

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include "pluginlaunch.h" /* for init_loading_shm */
4040
#include "processes.h" /* for create_process */
4141
#include "sighand.h" /* for openvas_signal */
42+
#include "utils.h" /* for store_file */
4243

4344
#include <errno.h> /* for errno() */
4445
#include <fcntl.h> /* for open() */
@@ -221,17 +222,17 @@ reload_openvas ()
221222
* the kb.
222223
*/
223224
static int
224-
load_scan_preferences (const char *scan_id)
225+
load_scan_preferences (struct scan_globals *globals)
225226
{
226227
char key[1024];
227228
kb_t kb;
228229
struct kb_item *res = NULL;
229230

230231
g_debug ("Start loading scan preferences.");
231-
if (!scan_id)
232+
if (!globals->scan_id)
232233
return -1;
233234

234-
snprintf (key, sizeof (key), "internal/%s/scanprefs", scan_id);
235+
snprintf (key, sizeof (key), "internal/%s/scanprefs", globals->scan_id);
235236
kb = kb_find (prefs_get ("db_address"), key);
236237
if (!kb)
237238
return -1;
@@ -244,11 +245,31 @@ load_scan_preferences (const char *scan_id)
244245
{
245246
gchar **pref = g_strsplit (res->v_str, "|||", 2);
246247
if (pref[0])
247-
prefs_set (pref[0], pref[1] ?: "");
248+
{
249+
gchar **pref_name = g_strsplit (pref[0], ":", 3);
250+
if (pref_name[1] && pref_name[2]
251+
&& !strncmp (pref_name[2], "file", 4))
252+
{
253+
char *file_hash = gvm_uuid_make ();
254+
int ret;
255+
prefs_set (pref[0], file_hash);
256+
ret = store_file (globals, pref[1], file_hash);
257+
if (ret)
258+
g_debug ("Load preference: Failed to upload file "
259+
"for nvt %s preference.",
260+
pref_name[0]);
261+
262+
g_free (file_hash);
263+
}
264+
else
265+
prefs_set (pref[0], pref[1] ?: "");
266+
g_strfreev (pref_name);
267+
}
268+
248269
g_strfreev (pref);
249270
res = res->next;
250271
}
251-
snprintf (key, sizeof (key), "internal/%s", scan_id);
272+
snprintf (key, sizeof (key), "internal/%s", globals->scan_id);
252273
kb_item_set_str (kb, key, "ready", 0);
253274
kb_item_set_int (kb, "internal/ovas_pid", getpid ());
254275

@@ -264,7 +285,7 @@ handle_client (struct scan_globals *globals)
264285
kb_t net_kb = NULL;
265286

266287
/* Load preferences from Redis. Scan started with a scan_id. */
267-
if (load_scan_preferences (globals->scan_id))
288+
if (load_scan_preferences (globals))
268289
{
269290
g_warning ("No preferences found for the scan %s", globals->scan_id);
270291
exit (0);

src/utils.c

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
* @brief A bunch of miscellaneous functions, mostly file conversions.
2424
*/
2525

26+
#include "../misc/scanneraux.h" /* for struct scan_globals */
27+
2628
#include <errno.h> /* for errno() */
2729
#include <gvm/base/prefs.h> /* for prefs_get() */
2830
#include <stdlib.h> /* for atoi() */
@@ -39,6 +41,101 @@ extern int global_max_checks;
3941
*/
4042
#define G_LOG_DOMAIN "sd main"
4143

44+
/**
45+
* @brief Adds a 'translation' entry for a file sent by the client.
46+
*
47+
* Files sent by the client are stored in memory on the server side.
48+
* In order to access these files, their original name ('local' to the client)
49+
* can be 'translated' into the file contents of the in-memory copy of the
50+
* file on the server side.
51+
*
52+
* @param globals Global struct.
53+
* @param file_hash hash to reference the file.
54+
* @param contents Contents of the file.
55+
*/
56+
static void
57+
files_add_translation (struct scan_globals *globals, const char *file_hash,
58+
char *contents)
59+
{
60+
GHashTable *trans = globals->files_translation;
61+
// Register the mapping table if none there yet
62+
if (trans == NULL)
63+
{
64+
trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
65+
globals->files_translation = trans;
66+
}
67+
68+
g_hash_table_insert (trans, g_strdup (file_hash), contents);
69+
}
70+
71+
/**
72+
* @brief Adds a 'content size' entry for a file sent by the client.
73+
*
74+
* Files sent by the client are stored in memory on the server side.
75+
* Because they may be binary we need to store the size of the uploaded file as
76+
* well. This function sets up a mapping from the original name sent by the
77+
* client to the file size.
78+
*
79+
* @param globals Global struct.
80+
* @param file_hash hash to reference the file.
81+
* @param filesize Size of the file in bytes.
82+
*/
83+
static void
84+
files_add_size_translation (struct scan_globals *globals, const char *file_hash,
85+
const long filesize)
86+
{
87+
GHashTable *trans = globals->files_size_translation;
88+
gchar *filesize_str = g_strdup_printf ("%ld", filesize);
89+
90+
// Register the mapping table if none there yet
91+
if (trans == NULL)
92+
{
93+
trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
94+
globals->files_size_translation = trans;
95+
}
96+
97+
g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str));
98+
}
99+
100+
/**
101+
* @brief Stores a file type preference in a hash table.
102+
*
103+
* @param globals Global struct.
104+
* @param file File content.
105+
* @param file_hash hash to reference the file.
106+
*
107+
* @return 0 if successful, -1 in case of errors.
108+
*/
109+
int
110+
store_file (struct scan_globals *globals, const char *file,
111+
const char *file_hash)
112+
{
113+
char *origname;
114+
gchar *contents = NULL;
115+
116+
size_t bytes = 0;
117+
118+
if (!file_hash && *file_hash == '\0')
119+
return -1;
120+
121+
origname = g_strdup (file_hash);
122+
123+
contents = (gchar *) g_base64_decode (file, &bytes);
124+
125+
if (contents == NULL)
126+
{
127+
g_debug ("store_file: Failed to allocate memory for uploaded file.");
128+
g_free (origname);
129+
return -1;
130+
}
131+
132+
files_add_translation (globals, origname, contents);
133+
files_add_size_translation (globals, origname, bytes);
134+
135+
g_free (origname);
136+
return 0;
137+
}
138+
42139
/**
43140
* Get the max number of hosts to test at the same time.
44141
*/

src/utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,4 +45,7 @@ wait_for_children1 (void);
4545
int
4646
is_scanner_only_pref (const char *);
4747

48+
int
49+
store_file (struct scan_globals *, const char *, const char *);
50+
4851
#endif

0 commit comments

Comments
 (0)