OpenVAS Scanner  7.0.1~git
plugutils.c File Reference

Plugin-specific stuff. More...

#include "plugutils.h"
#include "network.h"
#include <errno.h>
#include <gvm/base/hosts.h>
#include <gvm/base/networking.h>
#include <gvm/base/prefs.h>
#include <gvm/util/nvticache.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>
Include dependency graph for plugutils.c:

Go to the source code of this file.

Macros

#define G_LOG_DOMAIN   "lib misc"
 GLib logging domain. More...
 
#define MAX_CANDIDATES   16
 

Functions

const char * plug_current_vhost (void)
 
static int plug_fork_child (kb_t)
 
void plug_set_dep (struct script_infos *args, const char *depname)
 
void host_add_port_proto (struct script_infos *args, int portnum, char *proto)
 
static int unscanned_ports_as_closed (port_protocol_t ptype)
 Report state of preferences "unscanned_closed". More...
 
int kb_get_port_state_proto (kb_t kb, int portnum, char *proto)
 
int host_get_port_state_proto (struct script_infos *args, int portnum, char *proto)
 
int host_get_port_state (struct script_infos *plugdata, int portnum)
 
int host_get_port_state_udp (struct script_infos *plugdata, int portnum)
 
int plug_add_host_fqdn (struct script_infos *args, const char *hostname, const char *source)
 
char * plug_get_host_fqdn (struct script_infos *args)
 
GSList * plug_get_host_fqdn_list (struct script_infos *args)
 
char * plug_get_host_source (struct script_infos *args, const char *hostname)
 
struct in6_addr * plug_get_host_ip (struct script_infos *args)
 
char * plug_get_host_ip_str (struct script_infos *desc)
 
void proto_post_wrapped (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *what)
 Post a security message (e.g. LOG, NOTE, WARNING ...). More...
 
void proto_post_alarm (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
 
void post_alarm (const char *oid, struct script_infos *desc, int port, const char *action)
 
void proto_post_log (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
 Post a log message. More...
 
void post_log (const char *oid, struct script_infos *desc, int port, const char *action)
 Post a log message about a tcp port. More...
 
void proto_post_error (const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
 
void post_error (const char *oid, struct script_infos *desc, int port, const char *action)
 
char * get_plugin_preference (const char *oid, const char *name, int pref_id)
 Get the a plugins preference. More...
 
const char * get_plugin_preference_fname (struct script_infos *desc, const char *filename)
 Get the file name of a plugins preference that is of type "file". More...
 
char * get_plugin_preference_file_content (struct script_infos *desc, const char *identifier)
 Get the file contents of a plugins preference that is of type "file". More...
 
long get_plugin_preference_file_size (struct script_infos *desc, const char *identifier)
 Get the file size of a plugins preference that is of type "file". More...
 
void plug_set_key_len (struct script_infos *args, char *name, int type, const void *value, size_t len)
 
void plug_set_key (struct script_infos *args, char *name, int type, const void *value)
 
void plug_replace_key_len (struct script_infos *args, char *name, int type, void *value, size_t len)
 
void plug_replace_key (struct script_infos *args, char *name, int type, void *value)
 
void scanner_add_port (struct script_infos *args, int port, char *proto)
 
kb_t plug_get_kb (struct script_infos *args)
 
static void plug_get_key_sigchld ()
 
static void sig_n (int signo, void(*fnc)(int))
 
static void sig_term (void(*fcn)())
 
static void sig_chld (void(*fcn)())
 
void * plug_get_key (struct script_infos *args, char *name, int *type, size_t *len, int single)
 Get values from a kb under the given key name. More...
 
unsigned int plug_get_host_open_port (struct script_infos *desc)
 
void plug_set_port_transport (struct script_infos *args, int port, int tr)
 
int plug_get_port_transport (struct script_infos *args, int port)
 
static void plug_set_ssl_item (struct script_infos *args, char *item, char *itemfname)
 
void plug_set_ssl_cert (struct script_infos *args, char *cert)
 
void plug_set_ssl_key (struct script_infos *args, char *key)
 
void plug_set_ssl_pem_password (struct script_infos *args, char *key)
 
void plug_set_ssl_CA_file (struct script_infos *args, char *key)
 

Variables

int global_nasl_debug = 0
 
gvm_vhost_t * current_vhost = NULL
 

Detailed Description

Plugin-specific stuff.

Definition in file plugutils.c.

Macro Definition Documentation

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib misc"

GLib logging domain.

Definition at line 45 of file plugutils.c.

◆ MAX_CANDIDATES

#define MAX_CANDIDATES   16

Referenced by plug_get_host_open_port().

Function Documentation

◆ get_plugin_preference()

char* get_plugin_preference ( const char *  oid,
const char *  name,
int  pref_id 
)

Get the a plugins preference.

Search in the preferences set by the client. If it is not present, search in redis cache for the default.

Parameters
[in]oidScript OID to get the preference from
[in]nameName of the preference to get
[in]pref_idId of the preferences to get
Returns
script preference on success, Null otherwise.

Definition at line 408 of file plugutils.c.

References name, oid, and prefix().

Referenced by _http_req(), add_scantype_arguments(), add_timing_arguments(), build_cmd_line(), nmap_create(), plugin_do_run(), plugin_run_find_service(), script_get_preference(), script_get_preference_file_content(), and script_get_preference_file_location().

409 {
410  GHashTable *prefs;
411  GHashTableIter iter;
412  char *cname = NULL, *retval = NULL;
413  void *itername, *itervalue;
414  char prefix[1024], suffix[1024];
415 
416  prefs = preferences_get ();
417  if (!prefs || !nvticache_initialized () || !oid || (!name && pref_id < 1))
418  return NULL;
419 
420  g_hash_table_iter_init (&iter, prefs);
421 
422  if (pref_id > 0)
423  {
424  snprintf (prefix, sizeof (prefix), "%s:%d:", oid, pref_id);
425  while (g_hash_table_iter_next (&iter, &itername, &itervalue))
426  {
427  if (g_str_has_prefix (itername, prefix))
428  {
429  retval = g_strdup (itervalue);
430  break;
431  }
432  }
433  }
434  else
435  {
436  cname = g_strdup (name);
437  g_strchomp (cname);
438  snprintf (prefix, sizeof (prefix), "%s:", oid);
439  snprintf (suffix, sizeof (suffix), ":%s", cname);
440  /* NVT preferences received in OID:PrefID:PrefType:PrefName form */
441  while (g_hash_table_iter_next (&iter, &itername, &itervalue))
442  {
443  if (g_str_has_prefix (itername, prefix)
444  && g_str_has_suffix (itername, suffix))
445  {
446  retval = g_strdup (itervalue);
447  break;
448  }
449  }
450  }
451 
452  /* If no value set by the user, get the default one. */
453  if (!retval)
454  {
455  GSList *nprefs, *tmp;
456 
457  tmp = nprefs = nvticache_get_prefs (oid);
458  while (tmp)
459  {
460  if ((cname && !strcmp (cname, nvtpref_name (tmp->data)))
461  || (pref_id >= 0 && pref_id == nvtpref_id (tmp->data)))
462  {
463  retval = g_strdup (nvtpref_default (tmp->data));
464  break;
465  }
466  tmp = tmp->next;
467  }
468  g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
469  }
470  if (cname)
471  g_free (cname);
472  return retval;
473 }
const char * oid
const char * name
Definition: nasl_init.c:377
static void prefix(int n, int i)
Definition: nasl_tree.c:233
Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_plugin_preference_file_content()

char* get_plugin_preference_file_content ( struct script_infos desc,
const char *  identifier 
)

Get the file contents of a plugins preference that is of type "file".

As files sent to the scanner (e.g. as plugin preference) are stored in a hash table with an identifier supplied by the client as the key, the contents have to be looked up here.

Parameters
identifierIdentifier that was supplied by the client when the file was uploaded.
Returns
Contents of the file identified by identifier, NULL if not found or setup broken.

Definition at line 541 of file plugutils.c.

References scan_globals::files_translation, and script_infos::globals.

Referenced by get_plugin_preference_fname(), and script_get_preference_file_content().

543 {
544  struct scan_globals *globals = desc->globals;
545  GHashTable *trans;
546 
547  if (!globals)
548  return NULL;
549 
550  trans = globals->files_translation;
551  if (!trans)
552  return NULL;
553 
554  return g_hash_table_lookup (trans, identifier);
555 }
struct scan_globals * globals
Definition: scanneraux.h:45
GHashTable * files_translation
Definition: scanneraux.h:36
Here is the caller graph for this function:

◆ get_plugin_preference_file_size()

long get_plugin_preference_file_size ( struct script_infos desc,
const char *  identifier 
)

Get the file size of a plugins preference that is of type "file".

Files sent to the scanner (e.g. as plugin preference) are stored in a hash table with an identifier supplied by the client as the key. The size of the file is stored in a separate hash table with the same identifier as key, which can be looked up here.

Parameters
identifierIdentifier that was supplied by the client when the file was uploaded.
Returns
Size of the file identified by identifier, -1 if not found or setup broken.

Definition at line 572 of file plugutils.c.

References scan_globals::files_size_translation, and script_infos::globals.

Referenced by get_plugin_preference_fname(), and script_get_preference_file_content().

574 {
575  struct scan_globals *globals = desc->globals;
576  GHashTable *trans;
577  gchar *filesize_str;
578 
579  if (!globals)
580  return -1;
581 
582  trans = globals->files_size_translation;
583  if (!trans)
584  return -1;
585 
586  filesize_str = g_hash_table_lookup (trans, identifier);
587  if (filesize_str == NULL)
588  return -1;
589 
590  return atol (filesize_str);
591 }
struct scan_globals * globals
Definition: scanneraux.h:45
GHashTable * files_size_translation
Definition: scanneraux.h:37
Here is the caller graph for this function:

◆ get_plugin_preference_fname()

const char* get_plugin_preference_fname ( struct script_infos desc,
const char *  filename 
)

Get the file name of a plugins preference that is of type "file".

As files sent to the server (e.g. as plugin preference) are stored at pseudo-random locations with different names, the "real" file name has to be looked up in a hashtable.

Returns
Filename on disc for filename, NULL if not found or setup broken.

Definition at line 486 of file plugutils.c.

References get_plugin_preference_file_content(), and get_plugin_preference_file_size().

Referenced by nmap_create(), plugin_run_find_service(), and script_get_preference_file_location().

487 {
488  const char *content;
489  long contentsize = 0;
490  gint tmpfile;
491  gchar *tmpfilename;
492  GError *error = NULL;
493 
494  content = get_plugin_preference_file_content (desc, filename);
495  if (content == NULL)
496  {
497  return NULL;
498  }
499  contentsize = get_plugin_preference_file_size (desc, filename);
500  if (contentsize <= 0)
501  return NULL;
502 
503  tmpfile =
504  g_file_open_tmp ("openvas-file-upload.XXXXXX", &tmpfilename, &error);
505  if (tmpfile == -1)
506  {
507  g_message ("get_plugin_preference_fname: Could not open temporary"
508  " file for %s: %s",
509  filename, error->message);
510  g_error_free (error);
511  return NULL;
512  }
513  close (tmpfile);
514 
515  if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
516  {
517  g_message ("get_plugin_preference_fname: could set contents of"
518  " temporary file for %s: %s",
519  filename, error->message);
520  g_error_free (error);
521  return NULL;
522  }
523 
524  return tmpfilename;
525 }
char * get_plugin_preference_file_content(struct script_infos *desc, const char *identifier)
Get the file contents of a plugins preference that is of type "file".
Definition: plugutils.c:541
long get_plugin_preference_file_size(struct script_infos *desc, const char *identifier)
Get the file size of a plugins preference that is of type "file".
Definition: plugutils.c:572
Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_add_port_proto()

void host_add_port_proto ( struct script_infos args,
int  portnum,
char *  proto 
)

Definition at line 85 of file plugutils.c.

References ARG_INT, and plug_set_key().

Referenced by scanner_add_port().

86 {
87  char port_s[255];
88  snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
89  plug_set_key (args, port_s, ARG_INT, (void *) 1);
90 }
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition: plugutils.c:616
#define ARG_INT
Definition: plugutils.h:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state()

int host_get_port_state ( struct script_infos plugdata,
int  portnum 
)

Definition at line 154 of file plugutils.c.

References host_get_port_state_proto().

Referenced by get_port_state(), and open_sock_tcp().

155 {
156  return host_get_port_state_proto (plugdata, portnum, "tcp");
157 }
int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
Definition: plugutils.c:148
Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state_proto()

int host_get_port_state_proto ( struct script_infos args,
int  portnum,
char *  proto 
)

Definition at line 148 of file plugutils.c.

References kb_get_port_state_proto(), and script_infos::key.

Referenced by host_get_port_state(), and host_get_port_state_udp().

149 {
150  return kb_get_port_state_proto (args->key, portnum, proto);
151 }
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
Definition: plugutils.c:110
Here is the call graph for this function:
Here is the caller graph for this function:

◆ host_get_port_state_udp()

int host_get_port_state_udp ( struct script_infos plugdata,
int  portnum 
)

Definition at line 160 of file plugutils.c.

References host_get_port_state_proto().

Referenced by get_udp_port_state().

161 {
162  return host_get_port_state_proto (plugdata, portnum, "udp");
163 }
int host_get_port_state_proto(struct script_infos *args, int portnum, char *proto)
Definition: plugutils.c:148
Here is the call graph for this function:
Here is the caller graph for this function:

◆ kb_get_port_state_proto()

int kb_get_port_state_proto ( kb_t  kb,
int  portnum,
char *  proto 
)
Parameters
protoProtocol (udp/tcp). If NULL, "tcp" will be used.

Definition at line 110 of file plugutils.c.

References unscanned_ports_as_closed().

Referenced by get_closed_ports(), and host_get_port_state_proto().

111 {
112  char port_s[255], *kbstr;
113  const char *prange = prefs_get ("port_range");
114  port_protocol_t port_type;
115  array_t *port_ranges;
116 
117  if (!proto)
118  proto = "tcp";
119  if (!strcmp (proto, "udp"))
120  {
121  port_type = PORT_PROTOCOL_UDP;
122  kbstr = "Host/udp_scanned";
123  }
124  else
125  {
126  port_type = PORT_PROTOCOL_TCP;
127  kbstr = "Host/scanned";
128  }
129 
130  /* Check that we actually scanned the port */
131  if (kb_item_get_int (kb, kbstr) <= 0)
132  return unscanned_ports_as_closed (port_type);
133 
134  port_ranges = port_range_ranges (prange);
135  if (!port_in_port_ranges (portnum, port_type, port_ranges))
136  {
137  array_free (port_ranges);
138  return unscanned_ports_as_closed (port_type);
139  }
140  array_free (port_ranges);
141 
142  /* Ok, we scanned it. What is its state ? */
143  snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
144  return kb_item_get_int (kb, port_s) > 0;
145 }
static int unscanned_ports_as_closed(port_protocol_t ptype)
Report state of preferences "unscanned_closed".
Definition: plugutils.c:98
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_add_host_fqdn()

int plug_add_host_fqdn ( struct script_infos args,
const char *  hostname,
const char *  source 
)

Definition at line 166 of file plugutils.c.

References hostname, and script_infos::vhosts.

Referenced by add_hostname().

168 {
169  gvm_vhost_t *vhost;
170  GSList *vhosts;
171  char **excluded;
172 
173  if (!prefs_get_bool ("expand_vhosts") || !hostname || !source)
174  return -1;
175 
176  /* Check for duplicate vhost value. */
177  vhosts = args->vhosts;
178  while (vhosts)
179  {
180  gvm_vhost_t *tmp = vhosts->data;
181 
182  if (!strcmp (tmp->value, hostname))
183  {
184  g_warning ("%s: Value '%s' exists already", __FUNCTION__, hostname);
185  return -1;
186  }
187  vhosts = vhosts->next;
188  }
189  /* Check for excluded vhost value. */
190  if (prefs_get ("exclude_hosts"))
191  {
192  char **tmp = excluded = g_strsplit (prefs_get ("exclude_hosts"), ",", 0);
193 
194  while (*tmp)
195  {
196  if (!strcmp (g_strstrip (*tmp), hostname))
197  {
198  g_strfreev (excluded);
199  return -1;
200  }
201  tmp++;
202  }
203  g_strfreev (excluded);
204  }
205  vhost = gvm_vhost_new (g_strdup (hostname), g_strdup (source));
206  args->vhosts = g_slist_prepend (args->vhosts, vhost);
207  return 0;
208 }
const char * hostname
Definition: pluginlaunch.c:76
GSList * vhosts
Definition: scanneraux.h:52
Here is the caller graph for this function:

◆ plug_current_vhost()

const char* plug_current_vhost ( void  )

Definition at line 57 of file plugutils.c.

References current_vhost.

Referenced by open_sock_tcp().

58 {
59  return current_vhost->value;
60 }
gvm_vhost_t * current_vhost
Definition: plugutils.c:53
Here is the caller graph for this function:

◆ plug_fork_child()

static int plug_fork_child ( kb_t  kb)
static

Definition at line 695 of file plugutils.c.

References pid, and sig_term().

Referenced by plug_get_host_fqdn(), and plug_get_key().

696 {
697  pid_t pid;
698 
699  if ((pid = fork ()) == 0)
700  {
701  sig_term (_exit);
702  kb_lnk_reset (kb);
703  nvticache_reset ();
704  srand48 (getpid () + getppid () + time (NULL));
705  return 0;
706  }
707  else if (pid < 0)
708  {
709  g_warning ("%s(): fork() failed (%s)", __func__, strerror (errno));
710  return -1;
711  }
712  else
713  waitpid (pid, NULL, 0);
714  return 1;
715 }
static void sig_term(void(*fcn)())
Definition: plugutils.c:683
static pid_t pid
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_fqdn()

char* plug_get_host_fqdn ( struct script_infos args)

Definition at line 211 of file plugutils.c.

References current_vhost, script_infos::ip, script_infos::key, pid, plug_fork_child(), and script_infos::vhosts.

Referenced by _http_req(), get_hostname(), open_stream_connection_ext(), plug_get_host_source(), plugin_do_run(), and socket_negotiate_ssl().

212 {
213  GSList *vhosts = args->vhosts;
214 
215  if (!args->vhosts)
216  return addr6_as_str (args->ip);
217 
218  /* Workaround for rapid growth of forked processes ie. http_get() calls
219  * within foreach() loops. */
220  if (current_vhost)
221  return g_strdup (current_vhost->value);
222  while (vhosts)
223  {
224  pid_t pid = plug_fork_child (args->key);
225 
226  if (pid == 0)
227  {
228  current_vhost = vhosts->data;
229  return g_strdup (current_vhost->value);
230  }
231  else if (pid == -1)
232  return NULL;
233  vhosts = vhosts->next;
234  }
235  exit (0);
236 }
static pid_t pid
gvm_vhost_t * current_vhost
Definition: plugutils.c:53
struct in6_addr * ip
Definition: scanneraux.h:51
static int plug_fork_child(kb_t)
Definition: plugutils.c:695
GSList * vhosts
Definition: scanneraux.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_fqdn_list()

GSList* plug_get_host_fqdn_list ( struct script_infos args)

Definition at line 239 of file plugutils.c.

References script_infos::ip, and script_infos::vhosts.

Referenced by get_hostnames().

240 {
241  GSList *results = NULL, *vhosts = args->vhosts;
242 
243  if (!args->vhosts)
244  results = g_slist_prepend (results, addr6_as_str (args->ip));
245 
246  while (vhosts)
247  {
248  gvm_vhost_t *vhost = vhosts->data;
249 
250  results = g_slist_prepend (results, g_strdup (vhost->value));
251  vhosts = vhosts->next;
252  }
253  return results;
254 }
struct in6_addr * ip
Definition: scanneraux.h:51
GSList * vhosts
Definition: scanneraux.h:52
Here is the caller graph for this function:

◆ plug_get_host_ip()

◆ plug_get_host_ip_str()

char* plug_get_host_ip_str ( struct script_infos desc)

Definition at line 291 of file plugutils.c.

References plug_get_host_ip().

Referenced by open_sock_tcp().

292 {
293  return addr6_as_str (plug_get_host_ip (desc));
294 }
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition: plugutils.c:285
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_open_port()

unsigned int plug_get_host_open_port ( struct script_infos desc)

Don't always return the first open port, otherwise we might get bitten by OSes doing active SYN flood countermeasures. Also, avoid returning 80 and 21 as open ports, as many transparent proxies are acting for these...

Definition at line 817 of file plugutils.c.

References MAX_CANDIDATES, and plug_get_kb().

Referenced by get_host_open_port(), nasl_start_denial(), nasl_tcp_ping(), and nasl_tcp_v6_ping().

818 {
819  kb_t kb = plug_get_kb (desc);
820  struct kb_item *res, *k;
821  int open21 = 0, open80 = 0;
822 #define MAX_CANDIDATES 16
823  u_short candidates[MAX_CANDIDATES];
824  int num_candidates = 0;
825 
826  k = res = kb_item_get_pattern (kb, "Ports/tcp/*");
827  if (res == NULL)
828  return 0;
829  else
830  {
831  int ret;
832  char *s;
833 
834  for (;;)
835  {
836  s = res->name + sizeof ("Ports/tcp/") - 1;
837  ret = atoi (s);
838  if (ret == 21)
839  open21 = 1;
840  else if (ret == 80)
841  open80 = 1;
842  else
843  {
844  candidates[num_candidates++] = ret;
845  if (num_candidates >= MAX_CANDIDATES)
846  break;
847  }
848  res = res->next;
849  if (res == NULL)
850  break;
851  }
852 
853  kb_item_free (k);
854  if (num_candidates != 0)
855  return candidates[lrand48 () % num_candidates];
856  else if (open21)
857  return 21;
858  else if (open80)
859  return 80;
860  }
861 
862  /* Not reachable */
863  return 0;
864 }
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
#define MAX_CANDIDATES
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_host_source()

char* plug_get_host_source ( struct script_infos args,
const char *  hostname 
)

Definition at line 257 of file plugutils.c.

References current_vhost, hostname, plug_get_host_fqdn(), and script_infos::vhosts.

Referenced by get_hostname_source().

258 {
259  if (!args->vhosts)
260  return g_strdup ("IP-address");
261 
262  if (hostname)
263  {
264  GSList *vhosts = args->vhosts;
265 
266  /* Search for source of specified hostname/vhost. */
267  while (vhosts)
268  {
269  gvm_vhost_t *vhost = vhosts->data;
270 
271  if (!strcmp (vhost->value, hostname))
272  return g_strdup (vhost->source);
273  vhosts = vhosts->next;
274  }
275  return NULL;
276  }
277  /* Call plug_get_host_fqdn() to set current_vhost (and fork, in case of
278  * multiple vhosts.) */
279  if (!current_vhost)
280  g_free (plug_get_host_fqdn (args));
281  return g_strdup (current_vhost->source);
282 }
char * plug_get_host_fqdn(struct script_infos *args)
Definition: plugutils.c:211
gvm_vhost_t * current_vhost
Definition: plugutils.c:53
const char * hostname
Definition: pluginlaunch.c:76
GSList * vhosts
Definition: scanneraux.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_kb()

◆ plug_get_key()

void* plug_get_key ( struct script_infos args,
char *  name,
int *  type,
size_t *  len,
int  single 
)

Get values from a kb under the given key name.

Parameters
[in]argsThe script infos where to get the kb from.
[in]nameKey name to search in the kb.
[in/out]type If 1 is given, the answer is forced to be KB_TYPE_INT type. Otherwise it returns the fetched type.
[in]lenDesired string length to be returned.
[in]singleIn case of a list, fetch only the last element
Returns
Null if no result, or a void pointer to the result in success.

Definition at line 730 of file plugutils.c.

References script_infos::key, name, pid, plug_fork_child(), plug_get_key_sigchld(), and sig_chld().

Referenced by banner_grab(), get_kb_item(), get_ssh_port(), nasl_open_sock_kdc(), and plugin_do_run().

732 {
733  kb_t kb = args->key;
734  struct kb_item *res = NULL, *res_list;
735 
736  if (type != NULL && *type != KB_TYPE_INT)
737  *type = -1;
738 
739  if (kb == NULL)
740  return NULL;
741 
742  if (single && *type != KB_TYPE_INT)
743  res = kb_item_get_single (kb, name, KB_TYPE_UNSPEC);
744  else if (*type == KB_TYPE_INT)
745  res = kb_item_get_single (kb, name, KB_TYPE_INT);
746  else
747  res = kb_item_get_all (kb, name);
748 
749  if (res == NULL)
750  return NULL;
751 
752  if (!res->next) /* No fork - good */
753  {
754  void *ret;
755  if (res->type == KB_TYPE_INT)
756  {
757  if (type != NULL)
758  *type = KB_TYPE_INT;
759  ret = g_memdup (&res->v_int, sizeof (res->v_int));
760  }
761  else
762  {
763  if (type != NULL)
764  *type = KB_TYPE_STR;
765  if (len)
766  *len = res->len;
767  ret = g_memdup (res->v_str, res->len + 1);
768  }
769  kb_item_free (res);
770  return ret;
771  }
772 
773  /* More than one value - we will fork() then */
775  res_list = res;
776  while (res)
777  {
778  pid_t pid = plug_fork_child (kb);
779 
780  if (pid == 0)
781  {
782  /* Forked child. */
783  void *ret;
784 
785  if (res->type == KB_TYPE_INT)
786  {
787  if (type != NULL)
788  *type = KB_TYPE_INT;
789  ret = g_memdup (&res->v_int, sizeof (res->v_int));
790  }
791  else
792  {
793  if (type != NULL)
794  *type = KB_TYPE_STR;
795  if (len)
796  *len = res->len;
797  ret = g_memdup (res->v_str, res->len + 1);
798  }
799  kb_item_free (res_list);
800  return ret;
801  }
802  else if (pid == -1)
803  return NULL;
804  res = res->next;
805  }
806  kb_item_free (res_list);
807  exit (0);
808 }
static pid_t pid
static void sig_chld(void(*fcn)())
Definition: plugutils.c:689
static void plug_get_key_sigchld()
Definition: plugutils.c:664
const char * name
Definition: nasl_init.c:377
static int plug_fork_child(kb_t)
Definition: plugutils.c:695
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_get_key_sigchld()

static void plug_get_key_sigchld ( )
static

Definition at line 664 of file plugutils.c.

Referenced by plug_get_key().

665 {
666  int status;
667 
668  wait (&status);
669 }
Here is the caller graph for this function:

◆ plug_get_port_transport()

int plug_get_port_transport ( struct script_infos args,
int  port 
)

Definition at line 885 of file plugutils.c.

References OPENVAS_ENCAPS_IP, and plug_get_kb().

Referenced by get_port_transport(), and open_stream_auto_encaps_ext().

886 {
887  char s[256];
888  int trp;
889 
890  snprintf (s, sizeof (s), "Transports/TCP/%d", port);
891  trp = kb_item_get_int (plug_get_kb (args), s);
892  if (trp >= 0)
893  return trp;
894  else
895  return OPENVAS_ENCAPS_IP; /* Change this to 0 for ultra smart SSL
896  negotiation, at the expense of possibly
897  breaking stuff */
898 }
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_replace_key()

void plug_replace_key ( struct script_infos args,
char *  name,
int  type,
void *  value 
)

◆ plug_replace_key_len()

void plug_replace_key_len ( struct script_infos args,
char *  name,
int  type,
void *  value,
size_t  len 
)

Definition at line 623 of file plugutils.c.

References ARG_INT, ARG_STRING, global_nasl_debug, name, and plug_get_kb().

Referenced by plug_replace_key(), and replace_kb_item().

625 {
626  kb_t kb = plug_get_kb (args);
627 
628  if (name == NULL || value == NULL)
629  return;
630 
631  if (type == ARG_STRING)
632  kb_item_set_str (kb, name, value, len);
633  else if (type == ARG_INT)
634  kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
635  if (global_nasl_debug == 1)
636  {
637  if (type == ARG_STRING)
638  g_message ("replace key %s -> %s", name, (char *) value);
639  else if (type == ARG_INT)
640  g_message ("replace key %s -> %d", name,
641  (int) GPOINTER_TO_SIZE (value));
642  }
643 }
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
const char * name
Definition: nasl_init.c:377
#define ARG_INT
Definition: plugutils.h:34
int global_nasl_debug
Definition: plugutils.c:48
#define ARG_STRING
Definition: plugutils.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_dep()

void plug_set_dep ( struct script_infos args,
const char *  depname 
)

Definition at line 65 of file plugutils.c.

References script_infos::nvti.

Referenced by script_dependencies().

66 {
67  nvti_t *n = args->nvti;
68  gchar *old = nvti_dependencies (n);
69  gchar *new;
70 
71  if (!depname)
72  return;
73 
74  if (old)
75  {
76  new = g_strdup_printf ("%s, %s", old, depname);
77  nvti_set_dependencies (n, new);
78  g_free (new);
79  }
80  else
81  nvti_set_dependencies (n, depname);
82 }
nvti_t * nvti
Definition: scanneraux.h:47
Here is the caller graph for this function:

◆ plug_set_key()

void plug_set_key ( struct script_infos args,
char *  name,
int  type,
const void *  value 
)

Definition at line 616 of file plugutils.c.

References name, and plug_set_key_len().

Referenced by banner_grab(), host_add_port_proto(), mark_unknown_svc(), mark_wrapped_svc(), plug_set_port_transport(), plug_set_ssl_item(), plugin_do_run(), plugin_run_openvas_tcp_scanner(), plugin_run_synscan(), register_service(), save_detected_os(), save_host_state(), save_hostscripts(), save_ipidseq_details(), save_open_ports(), save_portscripts(), save_tcpseq_details(), save_traceroute_details(), scan(), set_kb_item(), and simple_register_host_detail().

618 {
619  plug_set_key_len (args, name, type, value, 0);
620 }
void plug_set_key_len(struct script_infos *args, char *name, int type, const void *value, size_t len)
Definition: plugutils.c:594
const char * name
Definition: nasl_init.c:377
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_key_len()

void plug_set_key_len ( struct script_infos args,
char *  name,
int  type,
const void *  value,
size_t  len 
)

Definition at line 594 of file plugutils.c.

References ARG_INT, ARG_STRING, global_nasl_debug, name, and plug_get_kb().

Referenced by plug_set_key(), and set_kb_item().

596 {
597  kb_t kb = plug_get_kb (args);
598 
599  if (name == NULL || value == NULL)
600  return;
601 
602  if (type == ARG_STRING)
603  kb_item_add_str_unique (kb, name, value, len);
604  else if (type == ARG_INT)
605  kb_item_add_int_unique (kb, name, GPOINTER_TO_SIZE (value));
606  if (global_nasl_debug == 1)
607  {
608  if (type == ARG_STRING)
609  g_message ("set key %s -> %s", name, (char *) value);
610  else if (type == ARG_INT)
611  g_message ("set key %s -> %d", name, (int) GPOINTER_TO_SIZE (value));
612  }
613 }
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
const char * name
Definition: nasl_init.c:377
#define ARG_INT
Definition: plugutils.h:34
int global_nasl_debug
Definition: plugutils.c:48
#define ARG_STRING
Definition: plugutils.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_port_transport()

void plug_set_port_transport ( struct script_infos args,
int  port,
int  tr 
)
Todo:
Those brain damaged functions should probably be in another file They are use to remember who speaks SSL or not

Definition at line 872 of file plugutils.c.

References ARG_INT, and plug_set_key().

Referenced by open_stream_auto_encaps_ext(), and plugin_do_run().

873 {
874  char s[256];
875 
876  snprintf (s, sizeof (s), "Transports/TCP/%d", port);
877  plug_set_key (args, s, ARG_INT, GSIZE_TO_POINTER (tr));
878 }
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition: plugutils.c:616
#define ARG_INT
Definition: plugutils.h:34
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_CA_file()

void plug_set_ssl_CA_file ( struct script_infos args,
char *  key 
)
Todo:
Also, all plug_set_ssl*-functions set values that are only accessed in network.c:open_stream_connection under specific conditions. Check whether these conditions can actually occur. Document the functions on the way.

Definition at line 931 of file plugutils.c.

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

932 {
933  plug_set_ssl_item (args, "CA", key);
934 }
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition: plugutils.c:901
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_cert()

void plug_set_ssl_cert ( struct script_infos args,
char *  cert 
)

Definition at line 909 of file plugutils.c.

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

910 {
911  plug_set_ssl_item (args, "cert", cert);
912 }
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition: plugutils.c:901
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_item()

static void plug_set_ssl_item ( struct script_infos args,
char *  item,
char *  itemfname 
)
static

Definition at line 901 of file plugutils.c.

References ARG_STRING, and plug_set_key().

Referenced by plug_set_ssl_CA_file(), plug_set_ssl_cert(), plug_set_ssl_key(), and plug_set_ssl_pem_password().

902 {
903  char s[256];
904  snprintf (s, sizeof (s), "SSL/%s", item);
905  plug_set_key (args, s, ARG_STRING, itemfname);
906 }
void plug_set_key(struct script_infos *args, char *name, int type, const void *value)
Definition: plugutils.c:616
#define ARG_STRING
Definition: plugutils.h:33
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_key()

void plug_set_ssl_key ( struct script_infos args,
char *  key 
)

Definition at line 915 of file plugutils.c.

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

916 {
917  plug_set_ssl_item (args, "key", key);
918 }
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition: plugutils.c:901
Here is the call graph for this function:
Here is the caller graph for this function:

◆ plug_set_ssl_pem_password()

void plug_set_ssl_pem_password ( struct script_infos args,
char *  key 
)

Definition at line 921 of file plugutils.c.

References plug_set_ssl_item().

Referenced by plugin_run_find_service().

922 {
923  plug_set_ssl_item (args, "password", key);
924 }
static void plug_set_ssl_item(struct script_infos *args, char *item, char *itemfname)
Definition: plugutils.c:901
Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_alarm()

void post_alarm ( const char *  oid,
struct script_infos desc,
int  port,
const char *  action 
)

Definition at line 355 of file plugutils.c.

References oid, and proto_post_alarm().

Referenced by mark_fssniffer(), mark_netbus_server(), mark_sub7_server(), mark_wild_shell(), and security_message().

357 {
358  proto_post_alarm (oid, desc, port, "tcp", action);
359 }
const char * oid
void proto_post_alarm(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
Definition: plugutils.c:348
Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_error()

void post_error ( const char *  oid,
struct script_infos desc,
int  port,
const char *  action 
)

Definition at line 389 of file plugutils.c.

References oid, and proto_post_error().

Referenced by error_message().

391 {
392  proto_post_error (oid, desc, port, "tcp", action);
393 }
const char * oid
void proto_post_error(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
Definition: plugutils.c:382
Here is the call graph for this function:
Here is the caller graph for this function:

◆ post_log()

void post_log ( const char *  oid,
struct script_infos desc,
int  port,
const char *  action 
)

Post a log message about a tcp port.

Definition at line 375 of file plugutils.c.

References oid, and proto_post_log().

Referenced by log_message(), mark_acap_server(), mark_adsgone(), mark_auth_server(), mark_avotus_mm_server(), mark_BitTorrent_server(), mark_chargen_server(), mark_citrix_server(), mark_cvslockserver(), mark_cvspserver(), mark_cvsupserver(), mark_dameware_server(), mark_dictd_server(), mark_direct_connect_hub(), mark_echo_server(), mark_eggdrop_server(), mark_ens_server(), mark_exchg_routing_server(), mark_finger_server(), mark_ftp_server(), mark_fw1(), mark_giop_server(), mark_gnocatan_server(), mark_gnome14_server(), mark_gnuserv(), mark_gopher_server(), mark_http_proxy(), mark_http_server(), mark_imap_server(), mark_interscan_viruswall(), mark_ircxpro_admin_server(), mark_iss_realsecure(), mark_jabber_server(), mark_linuxconf(), mark_LISa_server(), mark_listserv_server(), mark_locked_adsubtract_server(), mark_lpd_server(), mark_lyskom_server(), mark_mldonkey(), mark_mon_server(), mark_msdtc_server(), mark_mysql(), mark_nagiosd_server(), mark_nntp_server(), mark_ofa_express_server(), mark_pblocald_server(), mark_pbmaster_server(), mark_ph_server(), mark_pnsclient(), mark_pop3pw_server(), mark_pop_server(), mark_postgresql(), mark_ppp_daemon(), mark_psybnc(), mark_quicktime_streaming_server(), mark_remote_nc_server(), mark_rmserver(), mark_rsync(), mark_shoutcast_server(), mark_smppd_server(), mark_smtp_server(), mark_smux_server(), mark_snpp_server(), mark_socks_proxy(), mark_spamd_server(), mark_sphinxql(), mark_ssh_server(), mark_stonegate_auth_server(), mark_tcpmux_server(), mark_teamspeak2_server(), mark_telnet_server(), mark_time_server(), mark_unknown_svc(), mark_upsmon_server(), mark_uucp_server(), mark_veritas_backup(), mark_vmware_auth(), mark_vtun_server(), mark_websm_server(), mark_whois_plus2_server(), mark_wrapped_svc(), mark_zebra_server(), and plugin_do_run().

377 {
378  proto_post_log (oid, desc, port, "tcp", action);
379 }
const char * oid
void proto_post_log(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action)
Post a log message.
Definition: plugutils.c:365
Here is the call graph for this function:

◆ proto_post_alarm()

void proto_post_alarm ( const char *  oid,
struct script_infos desc,
int  port,
const char *  proto,
const char *  action 
)

Definition at line 348 of file plugutils.c.

References oid, and proto_post_wrapped().

Referenced by post_alarm(), and security_message().

350 {
351  proto_post_wrapped (oid, desc, port, proto, action, "ALARM");
352 }
const char * oid
void proto_post_wrapped(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
Definition: plugutils.c:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_error()

void proto_post_error ( const char *  oid,
struct script_infos desc,
int  port,
const char *  proto,
const char *  action 
)

Definition at line 382 of file plugutils.c.

References oid, and proto_post_wrapped().

Referenced by error_message(), and post_error().

384 {
385  proto_post_wrapped (oid, desc, port, proto, action, "ERRMSG");
386 }
const char * oid
void proto_post_wrapped(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
Definition: plugutils.c:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_log()

void proto_post_log ( const char *  oid,
struct script_infos desc,
int  port,
const char *  proto,
const char *  action 
)

Post a log message.

Definition at line 365 of file plugutils.c.

References oid, and proto_post_wrapped().

Referenced by log_message(), and post_log().

367 {
368  proto_post_wrapped (oid, desc, port, proto, action, "LOG");
369 }
const char * oid
void proto_post_wrapped(const char *oid, struct script_infos *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
Definition: plugutils.c:307
Here is the call graph for this function:
Here is the caller graph for this function:

◆ proto_post_wrapped()

void proto_post_wrapped ( const char *  oid,
struct script_infos desc,
int  port,
const char *  proto,
const char *  action,
const char *  what 
)

Post a security message (e.g. LOG, NOTE, WARNING ...).

Parameters
oidThe oid of the NVT
descThe script infos where to get settings.
portPort number related to the issue.
protoProtocol related to the issue (tcp or udp).
actionThe actual result text
whatThe type, like "LOG".

Definition at line 307 of file plugutils.c.

References current_vhost, hostname, oid, plug_get_host_ip(), plug_get_kb(), and script_infos::vhosts.

Referenced by proto_post_alarm(), proto_post_error(), and proto_post_log().

309 {
310  const char *hostname = "";
311  char *buffer, *data, port_s[16] = "general";
312  char ip_str[INET6_ADDRSTRLEN];
313  GString *action_str;
314  gsize length;
315  kb_t kb;
316 
317  /* Should not happen, just to avoid trouble stop here if no NVTI found */
318  if (!oid)
319  return;
320 
321  if (action == NULL)
322  action_str = g_string_new ("");
323  else
324  {
325  action_str = g_string_new (action);
326  g_string_append (action_str, "\n");
327  }
328 
329  if (port > 0)
330  snprintf (port_s, sizeof (port_s), "%d", port);
331  if (current_vhost)
332  hostname = current_vhost->value;
333  else if (desc->vhosts)
334  hostname = ((gvm_vhost_t *) desc->vhosts->data)->value;
335  addr6_to_str (plug_get_host_ip (desc), ip_str);
336  buffer = g_strdup_printf ("%s|||%s|||%s/%s|||%s|||%s", what, hostname ?: " ",
337  port_s, proto, oid, action_str->str);
338  /* Convert to UTF-8 before sending to Manager. */
339  data = g_convert (buffer, -1, "UTF-8", "ISO_8859-1", NULL, &length, NULL);
340  kb = plug_get_kb (desc);
341  kb_item_push_str (kb, "internal/results", data);
342  g_free (data);
343  g_free (buffer);
344  g_string_free (action_str, TRUE);
345 }
const char * oid
kb_t plug_get_kb(struct script_infos *args)
Definition: plugutils.c:658
gvm_vhost_t * current_vhost
Definition: plugutils.c:53
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition: plugutils.c:285
const char * hostname
Definition: pluginlaunch.c:76
GSList * vhosts
Definition: scanneraux.h:52
Here is the call graph for this function:
Here is the caller graph for this function:

◆ scanner_add_port()

void scanner_add_port ( struct script_infos args,
int  port,
char *  proto 
)

Definition at line 652 of file plugutils.c.

References host_add_port_proto().

Referenced by banner_grab(), nasl_scanner_add_port(), sendpacket(), and v6_sendpacket().

653 {
654  host_add_port_proto (args, port, proto);
655 }
void host_add_port_proto(struct script_infos *args, int portnum, char *proto)
Definition: plugutils.c:85
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sig_chld()

static void sig_chld ( void(*)()  fcn)
static

Definition at line 689 of file plugutils.c.

References sig_n().

Referenced by plug_get_key().

690 {
691  sig_n (SIGCHLD, fcn);
692 }
static void sig_n(int signo, void(*fnc)(int))
Definition: plugutils.c:672
Here is the call graph for this function:
Here is the caller graph for this function:

◆ sig_n()

static void sig_n ( int  signo,
void(*)(int)  fnc 
)
static

Definition at line 672 of file plugutils.c.

Referenced by sig_chld(), and sig_term().

673 {
674  struct sigaction sa;
675 
676  sa.sa_handler = fnc;
677  sa.sa_flags = 0;
678  sigemptyset (&sa.sa_mask);
679  sigaction (signo, &sa, (struct sigaction *) 0);
680 }
Here is the caller graph for this function:

◆ sig_term()

static void sig_term ( void(*)()  fcn)
static

Definition at line 683 of file plugutils.c.

References sig_n().

Referenced by plug_fork_child().

684 {
685  sig_n (SIGTERM, fcn);
686 }
static void sig_n(int signo, void(*fnc)(int))
Definition: plugutils.c:672
Here is the call graph for this function:
Here is the caller graph for this function:

◆ unscanned_ports_as_closed()

static int unscanned_ports_as_closed ( port_protocol_t  ptype)
static

Report state of preferences "unscanned_closed".

Returns
0 if pref is "yes", 1 otherwise.

Definition at line 98 of file plugutils.c.

Referenced by kb_get_port_state_proto().

99 {
100  if (ptype == PORT_PROTOCOL_UDP)
101  return prefs_get_bool ("unscanned_closed_udp") ? 0 : 1;
102 
103  return prefs_get_bool ("unscanned_closed") ? 0 : 1;
104 }
Here is the caller graph for this function:

Variable Documentation

◆ current_vhost

gvm_vhost_t* current_vhost = NULL

◆ global_nasl_debug

int global_nasl_debug = 0

Definition at line 48 of file plugutils.c.

Referenced by main(), plug_replace_key_len(), and plug_set_key_len().