Performs various checks for requirements set in a given plugin.
More...
#include "plugs_req.h"
#include "pluginscheduler.h"
#include <gvm/base/prefs.h>
#include <gvm/util/nvticache.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Go to the source code of this file.
|
| int | kb_get_port_state_proto (kb_t, int, char *) |
| |
| 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. More...
|
| |
| 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. More...
|
| |
| 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. More...
|
| |
| static int | check_mandatory_keys (kb_t kb, char *keys) |
| | Checks mandatory keys presence and value in the KB. More...
|
| |
| int | mandatory_requirements_met (kb_t kb, nvti_t *nvti) |
| | Check whether mandatory requirements for plugin are met. More...
|
| |
| char * | requirements_plugin (kb_t kb, nvti_t *nvti) |
| | Determine if the plugin requirements are met. More...
|
| |
Performs various checks for requirements set in a given plugin.
Definition in file plugs_req.c.
◆ check_mandatory_keys()
| static int check_mandatory_keys |
( |
kb_t |
kb, |
|
|
char * |
keys |
|
) |
| |
|
static |
Checks mandatory keys presence and value in the KB.
- Parameters
-
| [in] | kb | KB handle where to search for the keys. |
| [in] | keys | Comma separated list of mandatory keys. |
- Returns
- 1 if a key is missing or not matching its value, 0 otherwise.
Definition at line 160 of file plugs_req.c.
165 if (!kb || !keys || !*keys)
167 keynames = g_strsplit (keys,
", ", 0);
170 for (i = 0; keynames[i] != NULL; i++)
173 char *re_str = NULL, *pos;
176 if ((pos = strstr (keynames[i],
"=")))
182 kbi = kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
185 g_strfreev (keynames);
194 if (kbi->type != KB_TYPE_STR || !kbi->v_str)
196 g_strfreev (keynames);
200 if (regcomp (&re, re_str, REG_EXTENDED | REG_NOSUB | REG_ICASE))
202 g_warning (
"Couldn't compile regex %s", re_str);
203 g_strfreev (keynames);
207 if (regexec (&re, kbi->v_str, 0, NULL, 0) == REG_NOMATCH)
209 g_strfreev (keynames);
219 g_strfreev (keynames);
Referenced by mandatory_requirements_met().
◆ get_closed_ports()
| static int get_closed_ports |
( |
kb_t |
kb, |
|
|
char * |
ports_list, |
|
|
char * |
proto |
|
) |
| |
|
static |
Returns whether a port in a port list is closed or not.
- Returns
- Whether a port in a port list is closed or not.
Definition at line 39 of file plugs_req.c.
46 ports = g_strsplit (ports_list,
", ", 0);
47 for (i = 0; ports[i] != NULL; i++)
49 int iport = atoi (ports[i]);
57 if (kb_item_get_int (kb, ports[i]) > 0)
References kb_get_port_state_proto().
Referenced by requirements_plugin().
◆ kb_get_port_state_proto()
| int kb_get_port_state_proto |
( |
kb_t |
kb, |
|
|
int |
portnum, |
|
|
char * |
proto |
|
) |
| |
- Parameters
-
| proto | Protocol (udp/tcp). If NULL, "tcp" will be used. |
Definition at line 100 of file plugutils.c.
102 char port_s[255], *kbstr;
103 const char *prange = prefs_get (
"port_range");
104 port_protocol_t port_type;
105 array_t *port_ranges;
109 if (!strcmp (proto,
"udp"))
111 port_type = PORT_PROTOCOL_UDP;
112 kbstr =
"Host/udp_scanned";
116 port_type = PORT_PROTOCOL_TCP;
117 kbstr =
"Host/scanned";
121 if (kb_item_get_int (kb, kbstr) <= 0)
124 port_ranges = port_range_ranges (prange);
125 if (!port_in_port_ranges (portnum, port_type, port_ranges))
127 array_free (port_ranges);
130 array_free (port_ranges);
133 snprintf (port_s,
sizeof (port_s),
"Ports/%s/%d", proto, portnum);
134 return kb_item_get_int (kb, port_s) > 0;
Referenced by get_closed_ports().
◆ kb_missing_keyname_of_namelist()
| static int kb_missing_keyname_of_namelist |
( |
kb_t |
kb, |
|
|
char * |
keys, |
|
|
char ** |
keyname |
|
) |
| |
|
static |
Returns the name of the first key which is not present in the kb.
- Parameters
-
| [in] | kb | KB handle where to search for the keys. |
| [in] | keys | Comma separated list of keys. |
| [out] | keyname | Key that was missing. Free with g_free(). |
- Returns
- 1 if a key is missing in KB, 0 otherwise.
Definition at line 83 of file plugs_req.c.
87 if (!kb || !keys || !*keys)
90 keynames = g_strsplit (keys,
", ", 0);
93 for (i = 0; keynames[i] != NULL; i++)
96 kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
101 *keyname = g_strdup (keynames[i]);
102 g_strfreev (keynames);
109 g_strfreev (keynames);
Referenced by requirements_plugin().
◆ kb_present_keyname_of_namelist()
| static int kb_present_keyname_of_namelist |
( |
kb_t |
kb, |
|
|
char * |
keys, |
|
|
char ** |
keyname |
|
) |
| |
|
static |
Returns the name of the first key which is present in the kb.
- Parameters
-
| [in] | kb | KB handle where to search for the keys. |
| [in] | keys | Comma separated list of keys. |
| [out] | keyname | Key that was found. Free with g_free(). |
- Returns
- 1 if a key is present in KB, 0 otherwise.
Definition at line 122 of file plugs_req.c.
127 if (!kb || !keys || !*keys)
130 keynames = g_strsplit (keys,
", ", 0);
133 for (i = 0; keynames[i] != NULL; i++)
135 struct kb_item *kbi =
136 kb_item_get_single (kb, keynames[i], KB_TYPE_UNSPEC);
141 *keyname = g_strdup (keynames[i]);
143 g_strfreev (keynames);
148 g_strfreev (keynames);
Referenced by requirements_plugin().
◆ mandatory_requirements_met()
| int mandatory_requirements_met |
( |
kb_t |
kb, |
|
|
nvti_t * |
nvti |
|
) |
| |
Check whether mandatory requirements for plugin are met.
- Parameters
-
| kb | The knowledge base with all keys. |
| plugin | The scheduler plugin. |
- Returns
- 1 if all mandatory requirements for the plugin are met. 0 if it is not the case.
Definition at line 234 of file plugs_req.c.
References check_mandatory_keys().
Referenced by launch_plugin().
◆ requirements_plugin()
| char* requirements_plugin |
( |
kb_t |
kb, |
|
|
nvti_t * |
nvti |
|
) |
| |
Determine if the plugin requirements are met.
- Returns
- Returns NULL is everything is ok, else an error message.
Definition at line 251 of file plugs_req.c.
253 static char error[64];
254 char *errkey = NULL, *keys, *tcp, *udp;
255 const char *opti = prefs_get (
"optimization_level");
260 error[
sizeof (error) - 1] =
'\0';
261 tcp = nvti_required_ports (nvti);
264 strncpy (error,
"none of the required tcp ports are open",
269 udp = nvti_required_udp_ports (nvti);
272 strncpy (error,
"none of the required udp ports are open",
277 if (opti != NULL && (strcmp (opti,
"open_ports") == 0 || atoi (opti) == 1))
283 keys = nvti_required_keys (nvti);
286 snprintf (error,
sizeof (error),
"because the key %s is missing", errkey);
291 if (opti != NULL && (strcmp (opti,
"required_keys") == 0 || atoi (opti) == 2))
297 keys = nvti_excluded_keys (nvti);
300 snprintf (error,
sizeof (error),
"because the key %s is present", errkey);
References get_closed_ports(), kb_missing_keyname_of_namelist(), and kb_present_keyname_of_namelist().
Referenced by launch_plugin().