diff --git a/.github/install-openvas-dependencies.sh b/.github/install-openvas-dependencies.sh index b5e14d72de..d021d9eb8c 100755 --- a/.github/install-openvas-dependencies.sh +++ b/.github/install-openvas-dependencies.sh @@ -37,6 +37,7 @@ apt-get install --no-install-recommends --no-install-suggests -y \ libpopt0 \ libcurl4 \ libcurl4-gnutls-dev \ + libcjson-dev \ libhiredis-dev \ libmagic-dev diff --git a/doc/multiple-credentials.md b/doc/multiple-credentials.md new file mode 100644 index 0000000000..920f911c49 --- /dev/null +++ b/doc/multiple-credentials.md @@ -0,0 +1,47 @@ +# Multiple credentials per service + +A scan target can carry several credentials for the same service (SSH, SMB, +SNMP, KRB5, ESXi). Each credential may be restricted to a list of hosts. +When a host is scanned, the credential matching that host is selected and +published as the scan preferences read by the NASL scripts. + +Credentials are part of the target in the scan JSON. The optional `hosts` +field ties a credential to specific hosts. + +```json +"credentials": [ + { "service": "ssh", "port": 22, + "up": { "username": "default", "password": "pass" } }, + { "service": "ssh", "port": 2222, + "hosts": ["192.168.1.0/24", "web.example.com"], + "usk": { "username": "admin", "password": "passphrase", + "private": "" } }, + { "service": "smb", "hosts": ["10.0.0.1-10"], + "up": { "username": "user", "password": "pass" } } +] +``` + +**openvasd** handles scan preferences. The first credential without +`hosts` field (missing or empty) of each service is written as the regular default +preferences (the samepreferences used before this feature). Credentials with +`hosts` and later credentials without `hosts` are not written as defaults. +The complete credential list is serialized into one scan preference, +`multi-credentials`, as a JSON string. +**openvas** gets the scan preferences and process them. If `multi-credentials` is set, +parses it and stores the credential list in `globals->credentials`. +Later, when a new host process is started and before any plugin +runs, a function for credential overriding is called. Then, for each +credential service, iterates over the list of host in a credential and +the first credential whose `hosts` list contains the current scanned host is stored +and will be used for the rest of host scan. +This only affects the forked per host process, so other hosts are unaffected. +If nothing matches, the default credential stays in place. + +Summarizing, this are the important rules: +- Matching is , in the order given in the JSON. +- A credential without `hosts` never matches in C; it can only be the default, + the first one without hosts, per service, set in **openvasd**. +- Each service is selected independently. E.g. an SSH match doesn't influence SMB. +- A matching credential replaces all values of that service. Missing + optional values are set to an empty NULL, so nothing leaks from the + default credential. diff --git a/misc/CMakeLists.txt b/misc/CMakeLists.txt index 6cf54b468d..b6464ae210 100644 --- a/misc/CMakeLists.txt +++ b/misc/CMakeLists.txt @@ -15,6 +15,7 @@ pkg_check_modules (GLIB REQUIRED glib-2.0>=2.42) pkg_check_modules (GLIB_JSON REQUIRED json-glib-1.0>=1.4.4) pkg_check_modules (GNUTLS REQUIRED gnutls>=3.6.4) pkg_check_modules (CURL REQUIRED libcurl>=7.74.0) +pkg_check_modules(CJSON REQUIRED libcjson>=1.7.14) pkg_check_modules (KRB5 REQUIRED mit-krb5) pkg_check_modules (KRB5_GSSAPI REQUIRED mit-krb5-gssapi) @@ -87,7 +88,7 @@ include_directories (${GLIB_INCLUDE_DIRS} ${GLIB_JSON_INCLUDE_DIRS} set (FILES bpf_share.c ftp_funcs.c vendorversion.c network.c plugutils.c pcap.c scan_id.c strutils.c table_driven_lsc.c ipc.c ipc_openvas.c ipc_pipe.c - user_agent.c scanneraux.c kb_cache.c heartbeat.c openvas-krb5.c) + user_agent.c scanneraux.c kb_cache.c heartbeat.c openvas-krb5.c credentials.c) # On windows we are always PIC and stack-protector is replaces by DEP # Also stack protection needs a shared library to work @@ -105,7 +106,7 @@ set_target_properties (openvas_misc_shared PROPERTIES VERSION "${PROJECT_VERSION target_link_libraries (openvas_misc_shared LINK_PRIVATE ${GNUTLS_LDFLAGS} ${UUID_LDFLAGS} ${GLIB_LDFLAGS} ${GLIB_JSON_LDFLAGS} ${MAGIC_LDFLAGS} - ${PCAP_LDFLAGS} ${LIBGVM_BOREAS_LDFLAGS} ${CURL_LDFLAGS} ${KRB5_LDFLAGS} ${KRB5_GSSAPI_LDFLAGS} + ${PCAP_LDFLAGS} ${LIBGVM_BOREAS_LDFLAGS} ${CURL_LDFLAGS} ${CJSON_LDFLAGS} ${KRB5_LDFLAGS} ${KRB5_GSSAPI_LDFLAGS} ${LINKER_HARDENING_FLAGS}) if (OPENVAS_STATE_DIR) diff --git a/misc/credentials.c b/misc/credentials.c new file mode 100644 index 0000000000..7b4cdb5f44 --- /dev/null +++ b/misc/credentials.c @@ -0,0 +1,993 @@ +/* SPDX-FileCopyrightText: 2023 Greenbone AG + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * @file credentials.c + * @brief Functions to set and get the credentials. + */ + +#include "credentials.h" + +#include "base/hosts.h" +#include "scanneraux.h" + +#include +#include +#include +#include /* for prefs_get */ +#include +#include /* gvm_uuid_make */ +#include + +#undef G_LOG_DOMAIN +/** + * @brief GLib logging domain. + */ +#define G_LOG_DOMAIN "lib misc" + +#define OID_SMB_AUTH_USER "1.3.6.1.4.1.25623.1.0.90023:1:entry:SMB login:" +#define OID_SMB_AUTH_PASS "1.3.6.1.4.1.25623.1.0.90023:2:password:SMB password:" +#define OID_SMB_AUTH_DOMAIN \ + "1.3.6.1.4.1.25623.1.0.90023:3:entry:SMB domain (optional):" + +#define OID_SSH_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.103591:3:password:SSH password (unsafe!):" +#define OID_SSH_AUTH_USER "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:" +#define OID_SSH_AUTH_PRIV_USER \ + "1.3.6.1.4.1.25623.1.0.103591:7:entry:SSH privilege login name:" +#define OID_SSH_AUTH_PRIV_PASS \ + "1.3.6.1.4.1.25623.1.0.103591:8:password:SSH privilege password:" +#define OID_SSH_AUTH_PRIV_KEY \ + "1.3.6.1.4.1.25623.1.0.103591:4:file:SSH private key:" +#define OID_SSH_AUTH_PASSPHRASE \ + "1.3.6.1.4.1.25623.1.0.103591:2:password:SSH key passphrase:" + +#define OID_KRB5_AUTH_USER "1.3.6.1.4.1.25623.1.0.102114:1:entry:KRB5 login:" +#define OID_KRB5_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.102114:2:password:KRB5 password:" +#define OID_KRB5_AUTH_REALM "1.3.6.1.4.1.25623.1.0.102114:3:entry:KRB5 realm:" +#define OID_KRB5_AUTH_KDC "1.3.6.1.4.1.25623.1.0.102114:4:entry:KRB5 kdc:" + +#define OID_ESXI_AUTH_USER \ + "1.3.6.1.4.1.25623.1.0.105058:1:entry:ESXi login name:" +#define OID_ESXI_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.105058:2:password:ESXi login password:" + +#define OID_SNMP_AUTH_USER \ + "1.3.6.1.4.1.25623.1.0.105076:2:entry:SNMPv3 Username:" +#define OID_SNMP_AUTH_PASS \ + "1.3.6.1.4.1.25623.1.0.105076:3:password:SNMPv3 Password:" +#define OID_SNMP_AUTH_COMMUNITY \ + "1.3.6.1.4.1.25623.1.0.105076:1:password:SNMP Community:" +#define OID_SNMP_AUTH_PRIV_PASS \ + "1.3.6.1.4.1.25623.1.0.105076:5:password:SNMPv3 Privacy Password:" +#define OID_SNMP_AUTH_AUTH_ALGO \ + "1.3.6.1.4.1.25623.1.0.105076:4:radio:SNMPv3 Authentication Algorithm:" +#define OID_SNMP_AUTH_PRIV_ALGO \ + "1.3.6.1.4.1.25623.1.0.105076:6:radio:SNMPv3 Privacy Algorithm:" + +int ssh_credential_count = 0; +int smb_credential_count = 0; +int snmp_credential_count = 0; +int krb5_credential_count = 0; +int esxi_credential_count = 0; + +struct ssh_credential_type +{ + int port; + char *username; + char *password; + char *private_key; + char *private_key_uuid; + char *privilege_username; + char *privilege_password; + gvm_hosts_t *host_list; +}; + +struct smb_credential_type +{ + char *username; + char *password; + gvm_hosts_t *host_list; +}; + +struct krb5_credential_type +{ + char *username; + char *password; + char *realm; + char *kdc; + gvm_hosts_t *host_list; +}; + +struct snmp_credential_type +{ + char *username; + char *password; + char *community; + char *privacy_password; + char *auth_proto; // snmp authorization protocol. 0 for md5, 1 for sha1. + char *privacy_proto; // snmp private protocol. 0 for des, 1 for aes + gvm_hosts_t *host_list; +}; + +struct esxi_credential_type +{ + char *username; + char *password; + gvm_hosts_t *host_list; +}; + +static int +get_ssh_credential_port (credential_t *credential) +{ + return credential->ssh_credential->port; +} + +static char * +get_ssh_credential_username (credential_t *credential) +{ + return credential->ssh_credential->username; +} + +static char * +get_ssh_credential_password (credential_t *credential) +{ + return credential->ssh_credential->password; +} + +static char * +get_ssh_credential_private_key (credential_t *credential) +{ + return credential->ssh_credential->private_key; +} + +static char * +get_ssh_credential_private_key_uuid (credential_t *credential) +{ + return credential->ssh_credential->private_key_uuid; +} + +static char * +get_ssh_credential_privilege_username (credential_t *credential) +{ + return credential->ssh_credential->privilege_username; +} + +static char * +get_ssh_credential_privilege_password (credential_t *credential) +{ + return credential->ssh_credential->privilege_password; +} + +static char * +get_smb_credential_username (credential_t *credential) +{ + return credential->smb_credential->username; +} + +static char * +get_smb_credential_password (credential_t *credential) +{ + return credential->smb_credential->password; +} + +static char * +get_krb5_credential_username (credential_t *credential) +{ + return credential->krb5_credential->username; +} +static char * +get_krb5_credential_password (credential_t *credential) +{ + return credential->krb5_credential->password; +} + +static char * +get_krb5_credential_realm (credential_t *credential) +{ + return credential->krb5_credential->realm; +} + +static char * +get_krb5_credential_kdc (credential_t *credential) +{ + return credential->krb5_credential->kdc; +} + +static char * +get_snmp_credential_username (credential_t *credential) +{ + return credential->snmp_credential->username; +} + +static char * +get_snmp_credential_password (credential_t *credential) +{ + return credential->snmp_credential->password; +} + +static char * +get_snmp_credential_community (credential_t *credential) +{ + return credential->snmp_credential->community; +} + +static char * +get_snmp_credential_privacy_password (credential_t *credential) +{ + return credential->snmp_credential->privacy_password; +} + +static char * +get_snmp_credential_auth_proto (credential_t *credential) +{ + return credential->snmp_credential->auth_proto; +} + +static char * +get_snmp_credential_privacy_proto (credential_t *credential) +{ + return credential->snmp_credential->privacy_proto; +} + +static char * +get_esxi_credential_username (credential_t *credential) +{ + return credential->esxi_credential->username; +} + +static char * +get_esxi_credential_password (credential_t *credential) +{ + return credential->esxi_credential->password; +} + +static gvm_hosts_t * +create_host_list (char *host) +{ + gvm_hosts_t *host_list = NULL; + GSList *unresolved; + + if (host && host[0] != '\0') + host_list = gvm_hosts_new (host); + + if (host_list == NULL) + return host_list; + + unresolved = gvm_hosts_resolve (host_list); + while (unresolved) + { + g_warning ("Couldn't resolve hostname '%s' tied to a credential", + (char *) unresolved->data); + unresolved = unresolved->next; + } + g_slist_free_full (unresolved, g_free); + + return host_list; +} + +static int +credential_ssh_new (cJSON *service, credential_t **credential, char *target) +{ + ssh_credential_t *cred = g_malloc0 (sizeof (ssh_credential_t)); + cJSON *item; + gvm_hosts_t *host_list = NULL; + + if (target && target[0] != '\0') + { + host_list = create_host_list (target); + if (host_list) + cred->host_list = host_list; + } + + cred->port = gvm_json_obj_int (service, "port"); + + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->privilege_username = + g_strdup (gvm_json_obj_str (item, "privilege_username")); + cred->privilege_password = + g_strdup (gvm_json_obj_str (item, "privilege_password")); + } + else + { + item = cJSON_GetObjectItem (service, "usk"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->private_key = g_strdup (gvm_json_obj_str (item, "private")); + cred->privilege_username = + g_strdup (gvm_json_obj_str (item, "privilege_username")); + cred->privilege_password = + g_strdup (gvm_json_obj_str (item, "privilege_password")); + } + } + if (item == NULL) + { + gvm_hosts_free (host_list); + g_free (cred); + return -1; + } + (*credential)->type = SSH; + (*credential)->ssh_credential = cred; + ssh_credential_count++; + + return 0; +} + +static int +credential_smb_new (cJSON *service, credential_t **credential, char *target) +{ + smb_credential_t *cred = g_malloc0 (sizeof (smb_credential_t)); + cJSON *item; + gvm_hosts_t *host_list = NULL; + + if (target && target[0] != '\0') + { + host_list = create_host_list (target); + if (host_list) + cred->host_list = host_list; + } + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + } + if (item == NULL) + { + gvm_hosts_free (host_list); + g_free (cred); + return -1; + }; + + (*credential)->type = SMB; + (*credential)->smb_credential = cred; + smb_credential_count++; + + return 0; +} + +static int +credential_esxi_new (cJSON *service, credential_t **credential, char *target) +{ + esxi_credential_t *cred = g_malloc0 (sizeof (esxi_credential_t)); + cJSON *item; + gvm_hosts_t *host_list = NULL; + + if (target && target[0] != '\0') + { + host_list = create_host_list (target); + if (host_list) + cred->host_list = host_list; + } + + item = cJSON_GetObjectItem (service, "up"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + } + if (item == NULL) + { + gvm_hosts_free (host_list); + g_free (cred); + return -1; + } + + (*credential)->type = ESXi; + (*credential)->esxi_credential = cred; + esxi_credential_count++; + + return 0; +} + +static int +credential_snmp_new (cJSON *service, credential_t **credential, char *target) +{ + snmp_credential_t *cred = g_malloc0 (sizeof (snmp_credential_t)); + cJSON *item; + gvm_hosts_t *host_list = NULL; + + if (target && target[0] != '\0') + { + host_list = create_host_list (target); + if (host_list) + cred->host_list = host_list; + } + + item = cJSON_GetObjectItem (service, "snmp"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->community = g_strdup (gvm_json_obj_str (item, "community")); + cred->privacy_password = + g_strdup (gvm_json_obj_str (item, "privacy_password")); + cred->auth_proto = g_strdup (gvm_json_obj_str (item, "auth_algorithm")); + cred->privacy_proto = + g_strdup (gvm_json_obj_str (item, "privacy_algorithm")); + } + if (item == NULL) + { + gvm_hosts_free (host_list); + g_free (cred); + return -1; + } + + (*credential)->type = SNMP; + (*credential)->snmp_credential = cred; + snmp_credential_count++; + return 0; +} + +static int +credential_krb5_new (cJSON *service, credential_t **credential, char *target) +{ + krb5_credential_t *cred = g_malloc0 (sizeof (krb5_credential_t)); + cJSON *item; + gvm_hosts_t *host_list = NULL; + + if (target && target[0] != '\0') + { + host_list = create_host_list (target); + if (host_list) + cred->host_list = host_list; + } + + item = cJSON_GetObjectItem (service, "krb5"); + if (item && cJSON_IsObject (item)) + { + cred->username = g_strdup (gvm_json_obj_str (item, "username")); + cred->password = g_strdup (gvm_json_obj_str (item, "password")); + cred->realm = g_strdup (gvm_json_obj_str (item, "realm")); + cred->kdc = g_strdup (gvm_json_obj_str (item, "kdc")); + } + if (item == NULL) + { + gvm_hosts_free (host_list); + g_free (cred); + return -1; + } + (*credential)->type = KRB5; + (*credential)->krb5_credential = cred; + krb5_credential_count++; + + return 0; +} + +static void +reset_credential_counters (void) +{ + ssh_credential_count = 0; + smb_credential_count = 0; + snmp_credential_count = 0; + krb5_credential_count = 0; + esxi_credential_count = 0; +} + +static int +get_ssh_credential_count (void) +{ + return ssh_credential_count; +} + +static int +get_smb_credential_count (void) +{ + return smb_credential_count; +} + +static int +get_snmp_credential_count (void) +{ + return snmp_credential_count; +} + +static int +get_krb5_credential_count (void) +{ + return krb5_credential_count; +} + +static int +get_esxi_credential_count (void) +{ + return esxi_credential_count; +} + +static int +credential_new (cJSON *service_obj, char *service, char *host_value, + struct scan_globals **globals) +{ + credential_t *credential = g_malloc0 (sizeof (credential_t)); + + int ret = 0; + if (!g_strcmp0 (service, "ssh")) + { + ret = credential_ssh_new (service_obj, &credential, host_value); + if (ret == 0 && credential->ssh_credential->private_key != NULL + && credential->ssh_credential->private_key[0] != '\0') + { + char *file_uuid = gvm_uuid_make (); + if (store_file (*globals, credential->ssh_credential->private_key, + file_uuid)) + { + g_free (file_uuid); + g_debug ("%s: Failed to parse file tipe private key.", + + __func__); + } + else + { + credential->ssh_credential->private_key_uuid = file_uuid; + } + } + } + else if (!g_strcmp0 (service, "smb")) + { + ret = credential_smb_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "esxi")) + { + ret = credential_esxi_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "snmp")) + { + ret = credential_snmp_new (service_obj, &credential, host_value); + } + else if (!g_strcmp0 (service, "krb5")) + { + ret = credential_krb5_new (service_obj, &credential, host_value); + } + else + { + g_free (credential); + g_warning ("%s: Unknown credential service type %s", __func__, service); + ret = 1; + } + if (ret == 0) + (*globals)->credentials = + g_slist_append ((*globals)->credentials, credential); + else + { + g_free (credential); + g_warning ("%s: not possible to parse a credential. Missing information", + __func__); + } + + if (ret != 0) + return 1; + + return ret; +} + +/** @brief Parse a string in json format and stored the credentials + * Returns 0 on success, -1 on error. + */ +int +process_credentials_json (const char *json_credentials, + struct scan_globals **globals, char **err) +{ + cJSON *parser; + cJSON *service_obj = NULL; + int total_err = 0, ret = 0; + + reset_credential_counters (); + + parser = cJSON_Parse (json_credentials); + if (parser == NULL || !cJSON_IsArray (parser)) + { + *err = g_strdup ("Unable to parse credentials data"); + ret = -1; + goto res_cleanup; + } + + cJSON_ArrayForEach (service_obj, parser) + { + if (!cJSON_IsObject (service_obj)) + { + *err = g_strdup ("Unable to parse credential data object"); + ret = -1; + goto res_cleanup; + } + + if (service_obj != NULL) + { + char *service = gvm_json_obj_str (service_obj, "service"); + cJSON *target = NULL; + ret = 0; + + target = cJSON_GetObjectItem (service_obj, "hosts"); + if (target && cJSON_IsArray (target)) + { + for (int i = 0; i < cJSON_GetArraySize (target); i++) + { + cJSON *host_item = cJSON_GetArrayItem (target, i); + char *host_value = cJSON_GetStringValue (host_item); + + if (host_value == NULL || host_value[0] == '\0') + continue; + ret = + credential_new (service_obj, service, host_value, globals); + total_err += ret; + } + } + else + { + ret = credential_new (service_obj, service, NULL, globals); + total_err += ret; + } + } + } + +res_cleanup: + cJSON_Delete (parser); + + if (total_err > 0) + { + g_warning ("%s: %d errors were found during credential parsing", __func__, + total_err); + ret = -1; + } + + return ret; +} + +static void +free_credential (gpointer data) +{ + credential_t *credential = (credential_t *) data; + if (credential->type == SSH) + { + g_free (credential->ssh_credential->username); + g_free (credential->ssh_credential->password); + g_free (credential->ssh_credential->private_key); + g_free (credential->ssh_credential->private_key_uuid); + g_free (credential->ssh_credential->privilege_username); + g_free (credential->ssh_credential->privilege_password); + gvm_hosts_free (credential->ssh_credential->host_list); + g_free (credential->ssh_credential); + } + else if (credential->type == SMB) + { + g_free (credential->smb_credential->username); + g_free (credential->smb_credential->password); + gvm_hosts_free (credential->smb_credential->host_list); + g_free (credential->smb_credential); + } + else if (credential->type == ESXi) + { + g_free (credential->esxi_credential->username); + g_free (credential->esxi_credential->password); + gvm_hosts_free (credential->esxi_credential->host_list); + g_free (credential->esxi_credential); + } + else if (credential->type == KRB5) + { + g_free (credential->krb5_credential->username); + g_free (credential->krb5_credential->password); + g_free (credential->krb5_credential->realm); + g_free (credential->krb5_credential->kdc); + gvm_hosts_free (credential->krb5_credential->host_list); + g_free (credential->krb5_credential); + } + else if (credential->type == SNMP) + { + g_free (credential->snmp_credential->username); + g_free (credential->snmp_credential->password); + g_free (credential->snmp_credential->community); + g_free (credential->snmp_credential->privacy_password); + g_free (credential->snmp_credential->auth_proto); + g_free (credential->snmp_credential->privacy_proto); + gvm_hosts_free (credential->snmp_credential->host_list); + g_free (credential->snmp_credential); + } + g_free (credential); +} + +void +destroy_credentials (GSList **credentials) +{ + g_slist_free_full (*credentials, (GDestroyNotify) free_credential); + *credentials = NULL; +} + +static void +store_smb_credential (credential_t *credential) +{ + // prefs set will replace the old values if any. + prefs_set (OID_SMB_AUTH_USER, get_smb_credential_username (credential) + ? get_smb_credential_username (credential) + : NULL); + prefs_set (OID_SMB_AUTH_PASS, get_smb_credential_password (credential) + ? get_smb_credential_password (credential) + : NULL); +} + +static int +match_smb_host_credential (credential_t *credential, const gvm_host_t *host) +{ + if (host == NULL || credential->smb_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, credential->smb_credential->host_list)) + return 0; + + store_smb_credential (credential); + return 1; +} + +static void +store_krb5_credential (credential_t *credential) +{ + // prefs set will replace the old values if any. + prefs_set (OID_KRB5_AUTH_USER, get_krb5_credential_username (credential) + ? get_krb5_credential_username (credential) + : NULL); + prefs_set (OID_KRB5_AUTH_PASS, get_krb5_credential_password (credential) + ? get_krb5_credential_password (credential) + : NULL); + prefs_set (OID_KRB5_AUTH_REALM, get_krb5_credential_realm (credential) + ? get_krb5_credential_realm (credential) + : NULL); + prefs_set (OID_KRB5_AUTH_KDC, get_krb5_credential_kdc (credential) + ? get_krb5_credential_kdc (credential) + : NULL); +} + +static int +match_krb5_host_credential (credential_t *cred, const gvm_host_t *host) +{ + if (host == NULL || cred->krb5_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, cred->krb5_credential->host_list)) + return 0; + + store_krb5_credential (cred); + return 1; +} + +static void +store_esxi_credential (credential_t *cred) +{ + // prefs set will replace the old values if any. + prefs_set (OID_ESXI_AUTH_USER, get_esxi_credential_username (cred) + ? get_esxi_credential_username (cred) + : NULL); + prefs_set (OID_ESXI_AUTH_PASS, get_esxi_credential_password (cred) + ? get_esxi_credential_password (cred) + : NULL); +} + +static int +match_esxi_host_credential (credential_t *cred, const gvm_host_t *host) +{ + if (host == NULL || cred->esxi_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, cred->esxi_credential->host_list)) + return 0; + + store_esxi_credential (cred); + return 1; +} + +static void +store_snmp_credential (credential_t *credential) +{ + prefs_set (OID_SNMP_AUTH_COMMUNITY, + get_snmp_credential_community (credential) + ? get_snmp_credential_community (credential) + : NULL); + prefs_set (OID_SNMP_AUTH_USER, get_snmp_credential_username (credential) + ? get_snmp_credential_username (credential) + : NULL); + prefs_set (OID_SNMP_AUTH_PASS, get_snmp_credential_password (credential) + ? get_snmp_credential_password (credential) + : NULL); + prefs_set (OID_SNMP_AUTH_AUTH_ALGO, + get_snmp_credential_auth_proto (credential) + ? get_snmp_credential_auth_proto (credential) + : NULL); + prefs_set (OID_SNMP_AUTH_PRIV_PASS, + get_snmp_credential_privacy_password (credential) + ? get_snmp_credential_privacy_password (credential) + : NULL); + prefs_set (OID_SNMP_AUTH_PRIV_ALGO, + get_snmp_credential_privacy_proto (credential) + ? get_snmp_credential_privacy_proto (credential) + : NULL); +} + +static int +match_snmp_host_credential (credential_t *credential, const gvm_host_t *host) +{ + if (host == NULL || credential->snmp_credential->host_list == NULL) + return 0; + + if (!gvm_host_in_hosts (host, NULL, credential->snmp_credential->host_list)) + return 0; + + store_snmp_credential (credential); + return 1; +} + +static void +store_ssh_credential (credential_t *credential) +{ + const char *private_key = get_ssh_credential_private_key (credential); + const char *password = get_ssh_credential_password (credential); + gboolean key_auth = private_key != NULL && private_key[0] != '\0'; + char port_string[6]; + int port = get_ssh_credential_port (credential); + + if (port) + { + g_snprintf (port_string, sizeof (port_string), "%d", port); + prefs_set ("auth_port_ssh", port_string); + } + else + // Let the nasl script set the default port. See kb_ssh_transport() in + // ssh_func.inc + prefs_set ("auth_port_ssh", NULL); + + prefs_set (OID_SSH_AUTH_USER, get_ssh_credential_username (credential) + ? get_ssh_credential_username (credential) + : NULL); + prefs_set (OID_SSH_AUTH_PASS, key_auth ? "" : (password ? password : NULL)); + prefs_set (OID_SSH_AUTH_PASSPHRASE, key_auth && password ? password : NULL); + prefs_set (OID_SSH_AUTH_PRIV_USER, + get_ssh_credential_privilege_username (credential) + ? get_ssh_credential_privilege_username (credential) + : NULL); + prefs_set (OID_SSH_AUTH_PRIV_PASS, + get_ssh_credential_privilege_password (credential) + ? get_ssh_credential_privilege_password (credential) + : NULL); + prefs_set (OID_SSH_AUTH_PRIV_KEY, + key_auth && get_ssh_credential_private_key_uuid (credential) + ? get_ssh_credential_private_key_uuid (credential) + : NULL); +} + +static int +match_ssh_host_credential (credential_t *credential, const gvm_host_t *host) +{ + if (host == NULL || credential->ssh_credential->host_list == NULL) + return 0; + + if (!gvm_host_find_in_hosts (host, NULL, + credential->ssh_credential->host_list)) + return 0; + + store_ssh_credential (credential); + return 1; +} + +struct credential_check_data +{ + const gvm_host_t *host; + credential_type_t type; + gboolean ssh_selected; + gboolean smb_selected; + gboolean snmp_selected; + gboolean krb5_selected; + gboolean esxi_selected; +}; + +/** + * @brief Stores the first matching credential to be used for the current + * scanned host + */ +static void +set_host_credential (gpointer credential, gpointer user_data) +{ + struct credential_check_data *data = user_data; + credential_t *cred = credential; + + if (cred->type != data->type) + return; + + if (cred->type == SSH && data->ssh_selected != TRUE) + { + data->ssh_selected = match_ssh_host_credential (cred, data->host); + } + else if (cred->type == SNMP && data->snmp_selected != TRUE) + { + data->snmp_selected = match_snmp_host_credential (cred, data->host); + } + else if (cred->type == SMB && data->smb_selected != TRUE) + { + data->smb_selected = match_smb_host_credential (cred, data->host); + } + else if (cred->type == KRB5 && data->krb5_selected != TRUE) + { + data->krb5_selected = match_krb5_host_credential (cred, data->host); + } + else if (cred->type == ESXi && data->esxi_selected != TRUE) + { + data->esxi_selected = match_esxi_host_credential (cred, data->host); + } +} + +static void +set_host_credentials (GSList *credentials, const gvm_host_t *host, + credential_type_t type) +{ + if (host == NULL) + return; + + struct credential_check_data *user_data = + g_malloc0 (sizeof (struct credential_check_data)); + user_data->host = host; + user_data->type = type; + + g_slist_foreach (credentials, (GFunc) set_host_credential, + (gpointer) user_data); + g_free (user_data); +} + +static void +select_host_ssh_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_ssh_credential_count () < 1) + return; + + set_host_credentials (credentials, host, SSH); +} + +static void +select_host_smb_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_smb_credential_count () < 1) + return; + + set_host_credentials (credentials, host, SMB); +} + +static void +select_host_snmp_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_snmp_credential_count () < 1) + return; + set_host_credentials (credentials, host, SNMP); +} + +static void +select_host_krb5_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_krb5_credential_count () < 1) + return; + set_host_credentials (credentials, host, KRB5); +} + +static void +select_host_esxi_credential (GSList *credentials, const gvm_host_t *host) +{ + if (get_esxi_credential_count () < 1) + return; + + set_host_credentials (credentials, host, ESXi); +} + +void +override_host_credentials (struct scan_globals *globals, const gvm_host_t *host) +{ + if (globals == NULL || globals->credentials == NULL || host == NULL) + return; + + select_host_ssh_credential (globals->credentials, host); + select_host_smb_credential (globals->credentials, host); + select_host_krb5_credential (globals->credentials, host); + select_host_snmp_credential (globals->credentials, host); + select_host_esxi_credential (globals->credentials, host); +} diff --git a/misc/credentials.h b/misc/credentials.h new file mode 100644 index 0000000000..ae74793bc0 --- /dev/null +++ b/misc/credentials.h @@ -0,0 +1,65 @@ +/* SPDX-FileCopyrightText: 2023 Greenbone AG + * + * SPDX-License-Identifier: GPL-2.0-or-later + */ + +/** + * @file credentials.h + * @brief Module for handling credentials. + */ + +#ifndef MISC_CREDENTIALS_H +#define MISC_CREDENTIALS_H + +#include "base/hosts.h" +#include "scanneraux.h" + +#include +#include +#include /* for prefs_get */ + +typedef struct ssh_credential_type ssh_credential_t; +typedef struct smb_credential_type smb_credential_t; +typedef struct krb5_credential_type krb5_credential_t; +typedef struct snmp_credential_type snmp_credential_t; +typedef struct esxi_credential_type esxi_credential_t; + +enum credential_type +{ + SSH, + SMB, + KRB5, + SNMP, + ESXi, +}; + +typedef enum credential_type credential_type_t; + +struct credential +{ + credential_type_t type; + union + { + ssh_credential_t *ssh_credential; + smb_credential_t *smb_credential; + esxi_credential_t *esxi_credential; + snmp_credential_t *snmp_credential; + krb5_credential_t *krb5_credential; + }; +}; +typedef struct credential credential_t; + +int +process_credentials_json (const char *, struct scan_globals **, char **); + +void +destroy_credentials (GSList **); + +struct scan_globals; + +/* Select and publish one type credential in the per-host scanner process per + * service. */ +void +override_host_credentials (struct scan_globals *, const gvm_host_t *); + +#endif // MISC_CREDENTIALS_H diff --git a/misc/scanneraux.c b/misc/scanneraux.c index 33a351b987..0e069d8ca5 100644 --- a/misc/scanneraux.c +++ b/misc/scanneraux.c @@ -10,6 +10,8 @@ #include "scanneraux.h" +#include "credentials.h" + void destroy_scan_globals (struct scan_globals *globals) { @@ -24,6 +26,101 @@ destroy_scan_globals (struct scan_globals *globals) if (globals->files_size_translation) g_hash_table_destroy (globals->files_size_translation); + destroy_credentials (&globals->credentials); + g_free (globals); globals = NULL; } + +/** + * @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, g_free); + globals->files_size_translation = trans; + } + + g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str)); +} + +/** + * @brief Stores a file type preference in a hash table. + * + * @param globals Global struct. + * @param file File content. + * @param file_hash hash to reference the file. + * + * @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_warning ("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; +} diff --git a/misc/scanneraux.h b/misc/scanneraux.h index 6407212178..ccc6f45d17 100644 --- a/misc/scanneraux.h +++ b/misc/scanneraux.h @@ -19,6 +19,7 @@ struct scan_globals { GHashTable *files_translation; GHashTable *files_size_translation; + GSList *credentials; char *scan_id; pid_t host_pid; }; @@ -44,4 +45,8 @@ struct script_infos void destroy_scan_globals (struct scan_globals *); +int +store_file (struct scan_globals *globals, const char *file, + const char *file_hash); + #endif /* not MISC_SCANNERAUX_H */ diff --git a/rust/src/models/credential.rs b/rust/src/models/credential.rs index 0f741f6249..14a2e8f7d6 100644 --- a/rust/src/models/credential.rs +++ b/rust/src/models/credential.rs @@ -12,6 +12,9 @@ pub struct Credential { #[serde(flatten)] /// Type of the credential to get access. Different services support different types. pub credential_type: CredentialType, + /// Use this credential only for listed hosts + #[serde(skip_serializing_if = "Option::is_none")] + pub hosts: Option>, } impl Default for Credential { @@ -19,6 +22,7 @@ impl Default for Credential { Self { service: Service::SSH, port: Default::default(), + hosts: None, credential_type: CredentialType::UP { username: "root".to_string(), password: "".to_string(), diff --git a/rust/src/openvas/pref_handler.rs b/rust/src/openvas/pref_handler.rs index 6fafa3385d..8010d8f2d1 100644 --- a/rust/src/openvas/pref_handler.rs +++ b/rust/src/openvas/pref_handler.rs @@ -353,12 +353,28 @@ where fn prepare_credentials_for_openvas(&mut self) -> RedisStorageResult<()> { let credentials = self.scan_config.target.credentials.clone(); + // To mark that a default credential was already set for a service. + // In case of multiple credentials for the same service, the first one + // in the list is set as default. + let mut default_ssh_set = false; + let mut default_smb_set = false; + let mut default_snmp_set = false; + let mut default_krb5_set = false; + let mut default_esxi_set = false; let mut credential_preferences: Vec = vec![]; - for credential in credentials { - // TODO: refactor that + for credential in credentials.clone() { + let host_is_set = credential.hosts.map_or(false, |c| !c.is_empty()); + match credential.service { Service::KRB5 => { + // Only the first credential without hosts is set as default. + // Multiple KRB5 candidates are later handed and could override + // the default one. + if host_is_set || default_krb5_set { + continue; + } + if let Some(port) = credential.port { credential_preferences.push(format!("auth_port_krb5|||{port}")); } else { @@ -377,9 +393,16 @@ where .push(format!("{OID_KRB5_AUTH}:2:password::|||{password}")); credential_preferences.push(format!("{OID_KRB5_AUTH}:3:entry::|||{realm}")); credential_preferences.push(format!("{OID_KRB5_AUTH}:4:entry::|||{kdc}")); + default_krb5_set = true; } } Service::SSH => { + // Only the first credential whithout hosts is set as default. + // Multiple SSH candidates are later handed and could override + // the default one. + if host_is_set || default_ssh_set { + continue; + } if let Some(port) = credential.port { credential_preferences.push(format!("auth_port_ssh|||{port}")); } else { @@ -408,6 +431,7 @@ where p.password )); } + default_ssh_set = true; }; if let CredentialType::USK { username, @@ -436,9 +460,18 @@ where p.password )); } + default_ssh_set = true; }; } Service::SMB => { + // Only the first credential is set as default. + // Multiple SMB candidates are later handed and could override + // the default one. + + if host_is_set || default_smb_set { + continue; + } + if let CredentialType::UP { username, password, .. } = credential.credential_type.clone() @@ -448,9 +481,17 @@ where credential_preferences.push(format!( "{OID_SMB_AUTH}:2:password:SMB password:|||{password}" )); + default_smb_set = true; }; } Service::ESXi => { + // Only the first credential is set as default. + // Multiple ESXi candidates are later handed and could override + // the default one. + if host_is_set || default_esxi_set { + continue; + } + if let CredentialType::UP { username, password, .. } = credential.credential_type.clone() @@ -461,9 +502,17 @@ where credential_preferences.push(format!( "{OID_ESXI_AUTH}:2:password:ESXi login password:|||{password}" )); + default_esxi_set = true; }; } Service::SNMP => { + // Only the first credential is set as default. + // Multiple SNMP candidates are later handed and could override + // the default one. + if host_is_set || default_snmp_set { + continue; + } + if let CredentialType::SNMP { username, password, @@ -517,6 +566,7 @@ where credential_preferences.push(format!( "{OID_SNMP_AUTH}:6:radio:SNMPv3 Privacy Algorithm:|||{privacy_algorithm}" )); + default_snmp_set = true; } } @@ -526,6 +576,12 @@ where ), } } + if !credentials.is_empty() { + credential_preferences.push(format!( + "multi-credentials|||{}", + serde_json::to_string(&credentials).unwrap() + )); + } if !credential_preferences.is_empty() { self.redis_connector.push_kb_item( @@ -726,6 +782,7 @@ mod tests { Credential { service: Service::SSH, port: Some(22), + hosts: None, credential_type: CredentialType::UP { username: "user".to_string(), password: "pass".to_string(), @@ -735,6 +792,7 @@ mod tests { Credential { service: Service::KRB5, port: None, + hosts: None, credential_type: CredentialType::KRB5 { username: "user".to_string(), password: "pass".to_string(), @@ -770,6 +828,77 @@ mod tests { )); } + #[test] + fn test_multiple_ssh_credentials_first_is_default_and_all_are_candidates() { + let mut scan = scan_config(); + scan.target.credentials = vec![ + Credential { + service: Service::SSH, + port: Some(2222), + hosts: Some(vec!["localhost".to_string()]), // does not apply as default, it has a host + credential_type: CredentialType::UP { + username: "zero".to_string(), + password: "zero-pass".to_string(), + privilege: None, + }, + }, + Credential { + service: Service::SSH, + port: Some(22), + hosts: None, // first without hosts + credential_type: CredentialType::UP { + username: "first".to_string(), + password: "first-pass".to_string(), + privilege: None, + }, + }, + Credential { + service: Service::SSH, + port: Some(22022), + hosts: None, // first without hosts + credential_type: CredentialType::UP { + username: "second".to_string(), + password: "second-pass".to_string(), + privilege: None, + }, + }, + ]; + let expected = format!( + "multi-credentials|||{}", + serde_json::to_string(&scan.target.credentials).unwrap() + ); + let mut rc = fake_redis(); + let mut prefh = handler(scan, &mut rc); + + assert!(prefh.prepare_credentials_for_openvas().is_ok()); + assert!( + prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", &expected) + ); + // Only the first credential is set as default. + for preference in [ + "auth_port_ssh|||22", + "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||first", + ] { + assert!( + prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", preference) + ); + } + for preference in [ + "auth_port_ssh|||2222", + "1.3.6.1.4.1.25623.1.0.103591:1:entry:SSH login name:|||second", + ] { + assert!( + !prefh + .redis_connector + .item_exists("internal/123-456/scanprefs", preference) + ); + } + } + #[test] fn test_plugins() { let mut scan = scan_config(); diff --git a/rust/src/openvasd/api/error.rs b/rust/src/openvasd/api/error.rs index 7f6ad84eef..2dbc2a5c5c 100644 --- a/rust/src/openvasd/api/error.rs +++ b/rust/src/openvasd/api/error.rs @@ -11,7 +11,7 @@ use axum::{ http::{StatusCode, header::CONTENT_TYPE}, response::{IntoResponse, Response}, }; -use scannerlib::{models, notus::NotusError}; +use scannerlib::notus::NotusError; use serde::Serialize; use thiserror::Error; use tokio::sync::mpsc::error::SendError; @@ -25,8 +25,6 @@ pub enum ApiError { Notus(#[from] NotusError), #[error("scheduling error: {0}")] Scheduling(#[from] SendError), - #[error("duplicate credentials")] - DuplicateCredentials(models::Service), #[error("feed is not synced")] FeedNotSynced, #[error("scan still running")] @@ -60,7 +58,6 @@ impl ApiError { _ => StatusCode::INTERNAL_SERVER_ERROR, }, ApiError::Scheduling(_) => StatusCode::INTERNAL_SERVER_ERROR, - ApiError::DuplicateCredentials(_) => StatusCode::BAD_REQUEST, ApiError::FeedNotSynced => StatusCode::SERVICE_UNAVAILABLE, ApiError::ScanRunning => StatusCode::CONFLICT, ApiError::Json(_) => StatusCode::BAD_REQUEST, @@ -102,10 +99,6 @@ impl From for ErrorJson { | ApiError::VtsError(_) | ApiError::Notus(_) | ApiError::JoinError(_) => ("Internal server error".to_string(), None), - ApiError::DuplicateCredentials(service) => ( - "Duplicate service credentials".to_string(), - Some(service.to_string()), - ), ApiError::FeedNotSynced => ("Feed is not synced".to_string(), None), ApiError::ScanRunning => ( "Unable to perform action".to_string(), diff --git a/rust/src/openvasd/api/routes/scans.rs b/rust/src/openvasd/api/routes/scans.rs index dc365a76a1..c47ad8eccd 100644 --- a/rust/src/openvasd/api/routes/scans.rs +++ b/rust/src/openvasd/api/routes/scans.rs @@ -1,7 +1,7 @@ //! `/scans` routes. #![allow(clippy::result_large_err)] -use std::{collections::HashSet, num::ParseIntError, sync::Arc}; +use std::{num::ParseIntError, sync::Arc}; use axum::{ Extension, Json, Router, @@ -104,10 +104,6 @@ async fn post_scans( ) -> Result { let mut scan = scan?; - if let Some(dup) = get_duplicate_credential_service(&scan.target.credentials) { - return Err(ApiError::DuplicateCredentials(dup)); - } - // generate a random scan id if none was given if scan.scan_id.is_empty() { scan.scan_id = uuid::Uuid::new_v4().into(); @@ -305,19 +301,6 @@ async fn get_scans_id_status( Ok(Json(scanner.get_scan_status(&client_id, &scan_id).await?)) } -/// Finds and returns duplicate credentials for a single service. -pub(super) fn get_duplicate_credential_service( - credentials: &[models::Credential], -) -> Option { - let mut map: HashSet<_> = HashSet::default(); - for c in credentials.iter() { - if !map.insert(c.service.to_string()) { - return Some(c.service.clone()); - } - } - None -} - /// Parses a string either containing either a single number or into an inclusive numerical range. /// /// ## Examples diff --git a/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs b/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs index 1393251947..314f9f7a44 100644 --- a/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs +++ b/rust/src/openvasd/container_image_scanner/scheduling/db/sqlite/scan.rs @@ -212,6 +212,7 @@ impl<'o> Fetch for DBScan<'o, String> { }, service: models::Service::Generic, port: None, + hosts: None, }) .collect(), ..Default::default() diff --git a/rust/src/openvasd/scans/mod.rs b/rust/src/openvasd/scans/mod.rs index 27a866c916..3420a9c867 100644 --- a/rust/src/openvasd/scans/mod.rs +++ b/rust/src/openvasd/scans/mod.rs @@ -183,6 +183,7 @@ pub mod tests { service, port, credential_type, + hosts: None, }) .collect() } diff --git a/rust/src/scannerctl/osp/start_scan.rs b/rust/src/scannerctl/osp/start_scan.rs index a168614cd4..1fe778a29b 100644 --- a/rust/src/scannerctl/osp/start_scan.rs +++ b/rust/src/scannerctl/osp/start_scan.rs @@ -73,6 +73,7 @@ impl From for Vec { service: (&x.service as &str).try_into().ok().unwrap_or(Service::SSH), port: x.port.and_then(|x| x.parse().ok()), credential_type: kind, + hosts: None, //multiple credentials not supported by ospd } }) .collect() diff --git a/src/attack.c b/src/attack.c index b16dc74ceb..09d9f9a8c8 100644 --- a/src/attack.c +++ b/src/attack.c @@ -12,6 +12,7 @@ #include "attack.h" +#include "../misc/credentials.h" #include "../misc/ipc_openvas.h" #include "../misc/kb_cache.h" #include "../misc/network.h" /* for auth_printf */ @@ -563,6 +564,8 @@ attack_host (struct scan_globals *globals, struct in6_addr *ip, struct scheduler_plugin *plugin; pid_t parent; + override_host_credentials (globals, args->host); + addr6_to_str (ip, ip_str); host_kb = args->host_kb; host_vhosts = args->host->vhosts; @@ -1080,7 +1083,7 @@ set_alive_detection_tid (pthread_t tid) alive_detection_tid = tid; } static pthread_t -get_alive_detection_tid () +get_alive_detection_tid (void) { return alive_detection_tid; } @@ -1112,7 +1115,7 @@ handle_scan_stop_signal () } static void -scan_stop_cleanup () +scan_stop_cleanup (void) { kb_t main_kb = NULL; char *pid; diff --git a/src/openvas.c b/src/openvas.c index 6b67ed250a..70a41b15be 100644 --- a/src/openvas.c +++ b/src/openvas.c @@ -22,6 +22,7 @@ #include "openvas.h" +#include "../misc/credentials.h" /* for process_credentials_json */ #include "../misc/kb_cache.h" #include "../misc/plugutils.h" /* nvticache_free */ #include "../misc/scan_id.h" /* to manage global scan_id */ @@ -118,7 +119,7 @@ static openvas_option openvas_defaults[] = { * @brief Set the prefs from the openvas_defaults array. */ static void -set_default_openvas_prefs () +set_default_openvas_prefs (void) { for (int i = 0; openvas_defaults[i].option != NULL; i++) prefs_set (openvas_defaults[i].option, openvas_defaults[i].value); @@ -252,6 +253,22 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) g_strfreev (pref); res = res->next; } + + const char *json = prefs_get ("multi-credentials"); + if (json != NULL && json[0] != '\0') + { + char *err = NULL; + + if (process_credentials_json (json, &globals, &err) < 0) + { + g_warning ( + "There were some errors processing multiple credentials: %s", err); + g_free (err); + } + // Cleanup credential json data + prefs_set ("multi-credentials", ""); + } + kb_del_items (kb, key); snprintf (key, sizeof (key), "internal/%s", globals->scan_id); kb_item_set_str_with_main_kb_check (kb, key, "ready", 0); @@ -259,7 +276,6 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) kb_lnk_reset (kb); g_debug ("End loading scan preferences."); - kb_item_free (res); return 0; } @@ -270,7 +286,7 @@ overwrite_openvas_prefs_with_prefs_from_client (struct scan_globals *globals) * @return 0 on success, -1 on error. */ static int -init_logging () +init_logging (void) { static gchar *log_config_file_name = NULL; int err; @@ -309,7 +325,7 @@ gcrypt_init (void) * @brief Check TLS. */ static void -check_tls () +check_tls (void) { #if GNUTLS_VERSION_NUMBER < 0x030300 if (openvas_SSL_init () < 0) @@ -330,7 +346,7 @@ check_tls () * @brief Print start message. */ static void -openvas_print_start_msg () +openvas_print_start_msg (void) { #ifdef OPENVAS_GIT_REVISION g_message ("openvas %s (GIT revision %s) started", OPENVAS_VERSION, diff --git a/src/utils.c b/src/utils.c index 861ff93328..61c8b45366 100644 --- a/src/utils.c +++ b/src/utils.c @@ -35,101 +35,6 @@ 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, g_free); - globals->files_size_translation = trans; - } - - g_hash_table_insert (trans, g_strdup (file_hash), g_strdup (filesize_str)); -} - -/** - * @brief Stores a file type preference in a hash table. - * - * @param globals Global struct. - * @param file File content. - * @param file_hash hash to reference the file. - * - * @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. */ diff --git a/src/utils.h b/src/utils.h index c47ecdbcb1..f11ff9622c 100644 --- a/src/utils.h +++ b/src/utils.h @@ -34,10 +34,6 @@ wait_for_children1 (void); int is_scanner_only_pref (const char *); -int -store_file (struct scan_globals *globals, const char *file, - const char *file_hash); - int check_host_still_alive (kb_t, const char *);