OpenVAS Scanner 23.32.3
user_agent.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "user_agent.h"
12
13#include "ipc_openvas.h"
14#include "plugutils.h" /* plug_get_host_fqdn */
15#include "vendorversion.h"
16
17#include <glib.h>
18#include <gvm/base/prefs.h> /* for prefs_get */
19
20#undef G_LOG_DOMAIN
24#define G_LOG_DOMAIN "lib misc"
25
29static gchar *user_agent = NULL;
30
31static int
33{
34 struct ipc_data *ua = NULL;
35 const char *json = NULL;
36 int ret = 0;
38 json = ipc_data_to_json (ua);
39 ipc_data_destroy (&ua);
40 ret = ipc_send (ipc_context, IPC_MAIN, json, strlen (json));
41 if (-1 == ret)
42 {
43 g_warning ("Unable to send %s to host process", user_agent);
44 }
45 return ret;
46}
47
56static void
58{
59 gchar *ua = NULL;
60
61 ua = get_plugin_preference ("1.3.6.1.4.1.25623.1.0.12288", "HTTP User-Agent",
62 -1);
63 if (!ua || strlen (g_strstrip (ua)) == 0)
64 {
65 g_free (ua);
66 if (!vendor_version_get () || *vendor_version_get () == '\0')
67 ua = g_strdup_printf ("Mozilla/5.0 [en] (X11, U; OpenVAS-VT %s)",
68 OPENVAS_MISC_VERSION);
69 else
70 ua = g_strdup_printf ("Mozilla/5.0 [en] (X11, U; %s)",
72 }
73
74 user_agent = ua;
75}
76
88gchar *
89user_agent_set (const gchar *ua)
90{
91 gchar *ua_aux = NULL;
92
93 ua_aux = g_strdup (user_agent);
94
95 if (ua != NULL && ua[0] != '\0')
96 {
97 g_free (user_agent);
98 user_agent = g_strdup (ua);
99 }
100
101 return ua_aux;
102}
103
113int
114user_agent_get (struct ipc_context *ipc_context, char **useragent)
115{
116 int ret = 0;
117 if (!user_agent || user_agent[0] == '\0')
118 {
121 }
122
123 *useragent = user_agent ? g_strdup (user_agent) : "";
124 return ret;
125}
int ipc_send(struct ipc_context *context, enum ipc_relation to, const char *msg, size_t len)
sends given msg to the target based on the given context
Definition ipc.c:46
@ IPC_MAIN
Definition ipc.h:18
void ipc_data_destroy(ipc_data_t **data)
destroys ipc_data.
const char * ipc_data_to_json(ipc_data_t *data)
transforms ipc_data to a json string
ipc_data_t * ipc_data_type_from_user_agent(const char *user_agent, size_t user_agent_len)
initializes ipc_data for the User-Agent.
char * get_plugin_preference(const char *oid, const char *name, int pref_id)
Get the a plugins preference.
Definition plugutils.c:832
Header file for module plugutils.
static int send_user_agent_via_ipc(struct ipc_context *ipc_context)
Definition user_agent.c:32
int user_agent_get(struct ipc_context *ipc_context, char **useragent)
Get user-agent.
Definition user_agent.c:114
gchar * user_agent_set(const gchar *ua)
Set user-agent.
Definition user_agent.c:89
static void user_agent_create(void)
Create and set the global User-Agent variable.
Definition user_agent.c:57
static gchar * user_agent
user-agent, or NULL.
Definition user_agent.c:29
Header file: user agent functions prototypes.
const gchar * vendor_version_get()
Get vendor version.
Header file: vendor version functions prototypes.