Skip to content

Commit

Permalink
Add functions to store the file type nvti preference in a hash table.
Browse files Browse the repository at this point in the history
This function where removed when OTP was removed with
greenbone#337
They were modified and adapted to the new protocol.
  • Loading branch information
jjnicola committed Oct 10, 2019
1 parent 2bcd180 commit 7f328d4
Show file tree
Hide file tree
Showing 3 changed files with 111 additions and 7 deletions.
9 changes: 8 additions & 1 deletion src/openvas.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
#include "pluginlaunch.h" /* for init_loading_shm */
#include "processes.h" /* for create_process */
#include "sighand.h" /* for openvas_signal */
#include "utils.h" /* for store_file */

#include <errno.h> /* for errno() */
#include <fcntl.h> /* for open() */
Expand Down Expand Up @@ -250,8 +251,14 @@ load_scan_preferences (struct scan_globals *globals)
!strncmp (pref_name[2], "file", 4))
{
char *file_hash = gvm_uuid_make ();
int ret;
prefs_set (pref[0], file_hash);
store_file (globals, pref[1], file_hash);
ret = store_file (globals, pref[1], file_hash);
if (ret)
g_debug ("Load preference: Failed to upload file "
"for nvt %s preference.", pref_name[0]);

g_free(file_hash);
}
else
prefs_set (pref[0], pref[1] ?: "");
Expand Down
106 changes: 100 additions & 6 deletions src/utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,14 @@
* @brief A bunch of miscellaneous functions, mostly file conversions.
*/

#include <errno.h> /* for errno() */
#include <gvm/base/prefs.h> /* for prefs_get() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for strcmp() */
#include <sys/ioctl.h> /* for ioctl() */
#include <sys/wait.h> /* for waitpid() */
#include "../misc/scanneraux.h" /* for struct scan_globals */

#include <errno.h> /* for errno() */
#include <gvm/base/prefs.h> /* for prefs_get() */
#include <stdlib.h> /* for atoi() */
#include <string.h> /* for strcmp() */
#include <sys/ioctl.h> /* for ioctl() */
#include <sys/wait.h> /* for waitpid() */

extern int global_max_hosts;
extern int global_max_checks;
Expand All @@ -39,6 +41,98 @@ extern int global_max_checks;
*/
#define G_LOG_DOMAIN "sd main"


/**
* @brief Adds a 'translation' entry for a file sent by the client.
*
* Files sent by the client are stored in memory on the server side.
* In order to access these files, their original name ('local' to the client)
* can be 'translated' into the file contents of the in-memory copy of the
* file on the server side.
*
* @param globals Global struct.
* @param file_hash hash to reference the file.
* @param contents Contents of the file.
*/
static void
files_add_translation (struct scan_globals *globals,
const char *file_hash, char *contents)
{
GHashTable *trans = globals->files_translation;
// Register the mapping table if none there yet
if (trans == NULL)
{
trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
globals->files_translation = trans;
}

g_hash_table_insert (trans, g_strdup (file_hash), contents);
}

/**
* @brief Adds a 'content size' entry for a file sent by the client.
*
* Files sent by the client are stored in memory on the server side.
* Because they may be binary we need to store the size of the uploaded file as
* well. This function sets up a mapping from the original name sent by the
* client to the file size.
*
* @param globals Global struct.
* @param file_hash hash to reference the file.
* @param filesize Size of the file in bytes.
*/
static void
files_add_size_translation (struct scan_globals *globals,
const char *file_hash, const long filesize)
{
GHashTable *trans = globals->files_size_translation;
gchar *filesize_str = g_strdup_printf ("%ld", filesize);

// Register the mapping table if none there yet
if (trans == NULL)
{
trans = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
globals->files_size_translation = trans;
}

g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str));
}

/**
* @brief Stores a file type preference.
*
* @return 0 if successful, -1 in case of errors.
*/
int
store_file (struct scan_globals *globals, const char *file,
const char *file_hash)
{
char *origname;
gchar *contents = NULL;

size_t bytes = 0;

if (!file_hash && *file_hash == '\0')
return -1;

origname = g_strdup (file_hash);

contents = (gchar *) g_base64_decode (file, &bytes);

if (contents == NULL)
{
g_debug ("store_file: Failed to allocate memory for uploaded file.");
g_free (origname);
return -1;
}

files_add_translation (globals, origname, contents);
files_add_size_translation (globals, origname, bytes);

g_free (origname);
return 0;
}

/**
* Get the max number of hosts to test at the same time.
*/
Expand Down
3 changes: 3 additions & 0 deletions src/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,7 @@ wait_for_children1 (void);
int
is_scanner_only_pref (const char *);

int
store_file (struct scan_globals *, const char *, const char *);

#endif

0 comments on commit 7f328d4

Please sign in to comment.