OpenVAS Scanner 23.40.3
plugs_req.c
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Greenbone AG
2 * SPDX-FileCopyrightText: 2006 Software in the Public Interest, Inc.
3 * SPDX-FileCopyrightText: 1998-2006 Tenable Network Security, Inc.
4 *
5 * SPDX-License-Identifier: GPL-2.0-only
6 */
7
12
13#include "plugs_req.h"
14
15#include "pluginscheduler.h"
16
17#include <gvm/base/prefs.h> /* for prefs_get() */
18#include <gvm/util/nvticache.h>
19#include <regex.h> /* for regcomp() */
20#include <stdio.h> /* for snprintf() */
21#include <stdlib.h> /* for atoi() */
22#include <string.h> /* for strcmp() */
23
24/**********************************************************
25
26 Private Functions
27
28***********************************************************/
29
30extern int
31kb_get_port_state_proto (kb_t, int, char *);
32
38static int
39get_closed_ports (kb_t kb, char *ports_list, char *proto)
40{
41 int i;
42 char **ports;
43
44 if (!ports_list)
45 return -1;
46 ports = g_strsplit (ports_list, ", ", 0);
47 for (i = 0; ports[i] != NULL; i++)
48 {
49 int iport = atoi (ports[i]);
50 if (iport > 0 && kb_get_port_state_proto (kb, iport, proto) != 0)
51 {
52 g_strfreev (ports);
53 return iport;
54 }
55 else
56 {
57 if (kb_item_get_int (kb, ports[i]) > 0)
58 {
59 g_strfreev (ports);
60 return 1; /* should be the actual value indeed ! */
61 }
62 }
63 }
64 g_strfreev (ports);
65 return 0; /* found nothing */
66}
67
68/**********************************************************
69
70 Public Functions
71
72***********************************************************/
73
82static int
83kb_missing_keyname_of_namelist (kb_t kb, char *keys, char **keyname)
84{
85 int i;
86 char **keynames;
87 if (!kb || !keys || !*keys)
88 return 0;
89
90 keynames = g_strsplit (keys, ", ", 0);
91 if (!keynames)
92 return 0;
93 for (i = 0; keynames[i] != NULL; i++)
94 {
95 struct kb_item *kbi =
96 kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
97
98 if (kbi == NULL)
99 {
100 if (keyname)
101 *keyname = g_strdup (keynames[i]);
102 g_strfreev (keynames);
103 return 1;
104 }
105
106 kb_item_free (kbi);
107 }
108
109 g_strfreev (keynames);
110 return 0; /* All of the keys are present in the kb */
111}
112
121static int
122kb_present_keyname_of_namelist (kb_t kb, char *keys, char **keyname)
123{
124 int i;
125 char **keynames;
126
127 if (!kb || !keys || !*keys)
128 return 0;
129
130 keynames = g_strsplit (keys, ", ", 0);
131 if (!keynames)
132 return 0;
133 for (i = 0; keynames[i] != NULL; i++)
134 {
135 struct kb_item *kbi =
136 kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
137
138 if (kbi != NULL)
139 {
140 if (keyname)
141 *keyname = g_strdup (keynames[i]);
142 kb_item_free (kbi);
143 g_strfreev (keynames);
144 return 1;
145 }
146 }
147
148 g_strfreev (keynames);
149 return 0;
150}
151
159static int
160check_mandatory_keys (kb_t kb, char *keys)
161{
162 int i;
163 char **keynames;
164
165 if (!kb || !keys || !*keys)
166 return 0;
167 keynames = g_strsplit (keys, ", ", 0);
168 if (!keynames)
169 return 0;
170 for (i = 0; keynames[i] != NULL; i++)
171 {
172 struct kb_item *kbi;
173 char *re_str = NULL, *pos;
174
175 /* Split, if key requires RE matching. */
176 if ((pos = strstr (keynames[i], "=")))
177 {
178 re_str = pos + 1;
179 *pos = '\0';
180 }
181
182 kbi = kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
183 if (!kbi)
184 {
185 g_strfreev (keynames);
186 return 1;
187 }
188
189 if (re_str)
190 {
191 regex_t re;
192
193 /* Check if RE matches. */
194 if (kbi->type != KB_TYPE_STR || !kbi->v_str)
195 {
196 g_strfreev (keynames);
197 kb_item_free (kbi);
198 return 1;
199 }
200 if (regcomp (&re, re_str, REG_EXTENDED | REG_NOSUB | REG_ICASE))
201 {
202 g_warning ("Couldn't compile regex %s", re_str);
203 g_strfreev (keynames);
204 kb_item_free (kbi);
205 return 1;
206 }
207 if (regexec (&re, kbi->v_str, 0, NULL, 0) == REG_NOMATCH)
208 {
209 g_strfreev (keynames);
210 kb_item_free (kbi);
211 regfree (&re);
212 return 1;
213 }
214 regfree (&re);
215 }
216 kb_item_free (kbi);
217 }
218
219 g_strfreev (keynames);
220 return 0;
221}
222
233int
234mandatory_requirements_met (kb_t kb, nvti_t *nvti)
235{
236 int ret;
237
238 ret = check_mandatory_keys (kb, nvti_mandatory_keys (nvti));
239
240 if (ret)
241 return 0;
242 return 1;
243}
244
250char *
251requirements_plugin (kb_t kb, nvti_t *nvti)
252{
253 static char error[64];
254 char *errkey = NULL, *keys, *tcp, *udp;
255 const char *opti = prefs_get ("optimization_level");
256
257 /*
258 * Check whether the good ports are open
259 */
260 error[sizeof (error) - 1] = '\0';
261 tcp = nvti_required_ports (nvti);
262 if (tcp && *tcp && (get_closed_ports (kb, tcp, "tcp")) == 0)
263 {
264 strncpy (error, "none of the required tcp ports are open",
265 sizeof (error) - 1);
266 return error;
267 }
268
269 udp = nvti_required_udp_ports (nvti);
270 if (udp && *udp && (get_closed_ports (kb, udp, "udp")) == 0)
271 {
272 strncpy (error, "none of the required udp ports are open",
273 sizeof (error) - 1);
274 return error;
275 }
276
277 if (opti != NULL && (strcmp (opti, "open_ports") == 0 || atoi (opti) == 1))
278 return NULL;
279
280 /*
281 * Check whether a key we wanted is missing
282 */
283 keys = nvti_required_keys (nvti);
284 if (kb_missing_keyname_of_namelist (kb, keys, &errkey))
285 {
286 snprintf (error, sizeof (error), "because the key %s is missing", errkey);
287 g_free (errkey);
288 return error;
289 }
290
291 if (opti != NULL && (strcmp (opti, "required_keys") == 0 || atoi (opti) == 2))
292 return NULL;
293
294 /*
295 * Check whether a key we do not want is present
296 */
297 keys = nvti_excluded_keys (nvti);
298 if (kb_present_keyname_of_namelist (kb, keys, &errkey))
299 {
300 snprintf (error, sizeof (error), "because the key %s is present", errkey);
301 g_free (errkey);
302 return error;
303 }
304 return NULL;
305}
header for pluginscheduler.c
int kb_get_port_state_proto(kb_t, int, char *)
Definition plugutils.c:149
static int kb_missing_keyname_of_namelist(kb_t kb, char *keys, char **keyname)
Returns the name of the first key which is not present in the kb.
Definition plugs_req.c:83
static int check_mandatory_keys(kb_t kb, char *keys)
Checks mandatory keys presence and value in the KB.
Definition plugs_req.c:160
char * requirements_plugin(kb_t kb, nvti_t *nvti)
Determine if the plugin requirements are met.
Definition plugs_req.c:251
int mandatory_requirements_met(kb_t kb, nvti_t *nvti)
Check whether mandatory requirements for plugin are met.
Definition plugs_req.c:234
static int kb_present_keyname_of_namelist(kb_t kb, char *keys, char **keyname)
Returns the name of the first key which is present in the kb.
Definition plugs_req.c:122
static int get_closed_ports(kb_t kb, char *ports_list, char *proto)
Returns whether a port in a port list is closed or not.
Definition plugs_req.c:39
plugs_req.c header.