Greenbone Vulnerability Management Libraries 23.1.2
credentialutils.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Greenbone AG
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later
4 */
5
10
11#include "credentialutils.h"
12
13#undef G_LOG_DOMAIN
17#define G_LOG_DOMAIN "libgvm util"
18
23{
24 gchar *type;
25 gchar *service;
26 gchar *port;
27 GHashTable *auth_data;
28};
29
40scan_credential_new (const char *type, const char *service, const char *port)
41{
42 scan_credential_t *new_credential;
43
44 new_credential = g_malloc0 (sizeof (scan_credential_t));
45
46 new_credential->type = type ? g_strdup (type) : NULL;
47 new_credential->service = service ? g_strdup (service) : NULL;
48 new_credential->port = port ? g_strdup (port) : NULL;
49 new_credential->auth_data =
50 g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
51
52 return new_credential;
53}
54
62const gchar *
64{
65 if (!credential)
66 return NULL;
67 return credential->type;
68}
69
77const gchar *
79{
80 if (!credential)
81 return NULL;
82 return credential->service;
83}
84
92const gchar *
94{
95 if (!credential)
96 return NULL;
97 return credential->port;
98}
99
108void
110 void (*func) (const char *name,
111 const char *value,
112 void *user_data),
113 void *user_data)
114{
115 if (!credential || !func)
116 return;
117
118 GHashTableIter iter;
119 gpointer key, value;
120
121 g_hash_table_iter_init (&iter, credential->auth_data);
122 while (g_hash_table_iter_next (&iter, &key, &value))
123 {
124 func ((const char *) key, (const char *) value, user_data);
125 }
126}
127
133void
135{
136 if (!credential)
137 return;
138
139 g_free (credential->type);
140 g_free (credential->service);
141 g_free (credential->port);
142 g_hash_table_destroy (credential->auth_data);
143 g_free (credential);
144}
145
154const gchar *
156{
157 if (credential == NULL || name == NULL)
158 return NULL;
159 return g_hash_table_lookup (credential->auth_data, name);
160}
161
169void
171 const char *value)
172{
173 if (credential == NULL || name == NULL)
174 return;
175
176 if (g_regex_match_simple ("^[[:alpha:]][[:alnum:]_]*$", name, 0, 0))
177 {
178 if (value)
179 g_hash_table_replace (credential->auth_data, g_strdup (name),
180 g_strdup (value));
181 else
182 g_hash_table_remove (credential->auth_data, name);
183 }
184 else
185 {
186 g_warning ("%s: Invalid auth data name: %s", __func__, name);
187 }
188}
void scan_credential_set_auth_data(scan_credential_t *credential, const char *name, const char *value)
Set authentication data for a scan credential.
Definition credentialutils.c:170
const gchar * scan_credential_get_port(scan_credential_t *credential)
Get the port of a scan credential.
Definition credentialutils.c:93
void scan_credential_free(scan_credential_t *credential)
Free a scan credential.
Definition credentialutils.c:134
const gchar * scan_credential_get_auth_data(scan_credential_t *credential, const char *name)
Get authentication data from a scan credential.
Definition credentialutils.c:155
const gchar * scan_credential_get_type(scan_credential_t *credential)
Get the type of a scan credential.
Definition credentialutils.c:63
void scan_credential_foreach_auth_data(scan_credential_t *credential, void(*func)(const char *name, const char *value, void *user_data), void *user_data)
Iterate over each authentication data item in a scan credential.
Definition credentialutils.c:109
const gchar * scan_credential_get_service(scan_credential_t *credential)
Get the service of a scan credential.
Definition credentialutils.c:78
scan_credential_t * scan_credential_new(const char *type, const char *service, const char *port)
Allocate and initialize a new scan credential.
Definition credentialutils.c:40
Functions for handling scan credentials.
struct scan_credential scan_credential_t
Definition credentialutils.h:16
Struct credential information.
Definition credentialutils.c:23
gchar * port
Definition credentialutils.c:26
gchar * service
Definition credentialutils.c:25
gchar * type
Definition credentialutils.c:24
GHashTable * auth_data
Definition credentialutils.c:27