OpenVAS Libraries  9.0.3
plugutils.c
Go to the documentation of this file.
1 /* OpenVAS
2  * $Id$
3  * Description: Plugin-specific stuff.
4  *
5  * Authors:
6  * Renaud Deraison <deraison@nessus.org> (Original pre-fork development)
7  *
8  * Copyright:
9  * Based on work Copyright (C) 1998 - 2003 Renaud Deraison
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Library General Public
13  * License as published by the Free Software Foundation; either
14  * version 2 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19  * Library General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <ctype.h>
30 
31 #include <sys/types.h>
32 #include <sys/socket.h>
33 #include <sys/param.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 #include <errno.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39 #include <time.h>
40 
41 #include <glib.h>
42 
43 #include "arglists.h"
44 #include "network.h"
45 #include "plugutils.h"
46 #include "internal_com.h"
47 #include "openvas_logging.h"
48 #include "prefs.h" /* for prefs_get_bool */
49 
50 #include "../base/nvticache.h" /* for nvticache_get_by_oid() */
51 #include "../base/kb.h"
52 
53 /* Used to allow debugging for openvas-nasl */
55 
56 void
57 plug_set_xref (struct arglist *desc, char *name, char *value)
58 {
59  nvti_t *n = arg_get_value (desc, "NVTI");
60  char *new;
61 
62  if (nvti_xref (n))
63  new = g_strconcat (nvti_xref (n), ", ", name, ":", value, NULL);
64  else
65  new = g_strconcat (name, ":", value, NULL);
66 
67  nvti_set_xref (n, new);
68  g_free (new);
69 }
70 
71 void
72 plug_set_tag (struct arglist *desc, char *name, char *value)
73 {
74  nvti_t *n = arg_get_value (desc, "NVTI");
75  char *new;
76 
77  if (nvti_tag (n))
78  new = g_strconcat (nvti_tag (n), "|", name, "=", value, NULL);
79  else
80  new = g_strconcat (name, "=", value, NULL);
81 
82  nvti_set_tag (n, new);
83  g_free (new);
84 }
85 
86 void
87 plug_set_dep (struct arglist *desc, const char *depname)
88 {
89  nvti_t *n = arg_get_value (desc, "NVTI");
90  gchar * old = nvti_dependencies (n);
91  gchar * new;
92 
93  if (!depname) return;
94 
95  if (old)
96  {
97  new = g_strdup_printf ("%s, %s", old, depname);
98  nvti_set_dependencies (n, new);
99  g_free (new);
100  }
101  else
102  nvti_set_dependencies (n, depname);
103 }
104 
105 void
106 host_add_port_proto (struct arglist *args, int portnum, char *proto)
107 {
108  char port_s[255];
109  snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
110  plug_set_key (args, port_s, ARG_INT, (void *) 1);
111 }
112 
118 static int
119 unscanned_ports_as_closed (port_protocol_t ptype)
120 {
121  if (ptype == PORT_PROTOCOL_UDP)
122  return (prefs_get_bool ("unscanned_closed_udp") ? 0 : 1);
123 
124  return (prefs_get_bool ("unscanned_closed") ? 0 : 1);
125 }
126 
130 int
131 kb_get_port_state_proto (kb_t kb, int portnum, char *proto)
132 {
133  char port_s[255], *kbstr;
134  const char *prange = prefs_get ("port_range");
135  port_protocol_t port_type;
136  array_t *port_ranges;
137 
138  if (!proto)
139  proto = "tcp";
140  if (!strcmp (proto, "udp"))
141  {
142  port_type = PORT_PROTOCOL_UDP;
143  kbstr = "Host/udp_scanned";
144  }
145  else
146  {
147  port_type = PORT_PROTOCOL_TCP;
148  kbstr = "Host/scanned";
149  }
150 
151  /* Check that we actually scanned the port */
152  if (kb_item_get_int (kb, kbstr) <= 0)
153  return unscanned_ports_as_closed (port_type);
154 
155  port_ranges = port_range_ranges (prange);
156  if (!port_in_port_ranges (portnum, port_type, port_ranges))
157  {
158  array_free (port_ranges);
159  return unscanned_ports_as_closed (port_type);
160  }
161  array_free (port_ranges);
162 
163  /* Ok, we scanned it. What is its state ? */
164  snprintf (port_s, sizeof (port_s), "Ports/%s/%d", proto, portnum);
165  return kb_item_get_int (kb, port_s) > 0;
166 }
167 
168 int
169 host_get_port_state_proto (struct arglist *plugdata, int portnum, char *proto)
170 {
171  kb_t kb = plug_get_kb (plugdata);
172 
173  return kb_get_port_state_proto (kb, portnum, proto);
174 }
175 
176 int
177 host_get_port_state (struct arglist *plugdata, int portnum)
178 {
179  return (host_get_port_state_proto (plugdata, portnum, "tcp"));
180 }
181 
182 int
183 host_get_port_state_udp (struct arglist *plugdata, int portnum)
184 {
185  return (host_get_port_state_proto (plugdata, portnum, "udp"));
186 }
187 
188 
189 const char *
191 {
192  struct host_info *hinfo = arg_get_value (desc, "HOSTNAME");
193  if (hinfo)
194  return hinfo->name;
195  else
196  return (NULL);
197 }
198 
199 char *
201 {
202  struct host_info *hinfos = arg_get_value (desc, "HOSTNAME");
203  int type;
204  char *vhosts;
205 
206  if (!prefs_get ("vhosts_ip") || !strlen (prefs_get ("vhosts_ip")))
207  return g_strdup (hinfos->fqdn);
208  vhosts = plug_get_key (desc, "hostinfos/vhosts", &type, 0);
209  if (!vhosts)
210  return g_strdup (hinfos->fqdn);
211  return vhosts;
212 }
213 
214 
215 struct in6_addr *
216 plug_get_host_ip (struct arglist *desc)
217 {
218  struct host_info *hinfos = arg_get_value (desc, "HOSTNAME");
219  if (hinfos)
220  return hinfos->ip;
221  return NULL;
222 }
223 
224 char *
226 {
227  return addr6_as_str (plug_get_host_ip (desc));
228 }
229 
235 static void
236 mark_successful_plugin (const char *oid, struct arglist *desc)
237 {
238  char data[512];
239 
240  bzero (data, sizeof (data));
241  snprintf (data, sizeof (data), "Success/%s", oid);
242  plug_set_key (desc, data, ARG_INT, (void *) 1);
243 }
244 
245 static void
246 mark_post (const char *oid, struct arglist *desc, const char *action,
247  const char *content)
248 {
249  char entry_name[255];
250 
251  if (strlen (action) > (sizeof (entry_name) - 20))
252  return;
253 
254  snprintf (entry_name, sizeof (entry_name), "SentData/%s/%s", oid, action);
255  plug_set_key (desc, entry_name, ARG_STRING, content);
256 }
257 
269 void
270 proto_post_wrapped (const char *oid, struct arglist *desc, int port, const char *proto,
271  const char *action, const char *what)
272 {
273  int soc, len;
274  const char *prepend_tags, *append_tags;
275  char *buffer, *data, **nvti_tags = NULL;
276  GString *action_str;
277  gsize length;
278 
279  /* Should not happen, just to avoid trouble stop here if no NVTI found */
280  if (!nvticache_initialized () || !oid)
281  return;
282 
283  if (action == NULL)
284  action_str = g_string_new ("");
285  else
286  {
287  action_str = g_string_new (action);
288  g_string_append (action_str, "\n");
289  }
290 
291  prepend_tags = prefs_get ("result_prepend_tags");
292  append_tags = prefs_get ("result_append_tags");
293 
294  if (prepend_tags || append_tags)
295  {
296  char *tags = nvticache_get_tags (oid);
297  nvti_tags = g_strsplit (tags, "|", 0);
298  g_free (tags);
299  }
300 
301  /* This is convenience functionality in preparation for the breaking up of the
302  * NVT description block and adding proper handling of refined meta
303  * information all over the OpenVAS Framework.
304  */
305  if (nvti_tags != NULL)
306  {
307  if (prepend_tags != NULL)
308  {
309  gchar **tags = g_strsplit (prepend_tags, ",", 0);
310  int i = 0;
311  gchar *tag_prefix;
312  gchar *tag_value;
313  while (tags[i] != NULL)
314  {
315  int j = 0;
316  tag_value = NULL;
317  tag_prefix = g_strconcat (tags[i], "=", NULL);
318  while (nvti_tags[j] != NULL && tag_value == NULL)
319  {
320  if (g_str_has_prefix (nvti_tags[j], tag_prefix))
321  {
322  tag_value = g_strstr_len (nvti_tags[j], -1, "=");
323  }
324  j++;
325  }
326  g_free (tag_prefix);
327 
328  if (tag_value != NULL)
329  {
330  tag_value = tag_value + 1;
331  gchar *tag_line = g_strdup_printf ("%s:\n%s\n\n", tags[i],
332  tag_value);
333  g_string_prepend (action_str, tag_line);
334 
335  g_free (tag_line);
336  }
337  i++;
338  }
339  g_strfreev (tags);
340  }
341 
342  if (append_tags != NULL)
343  {
344  gchar **tags = g_strsplit (append_tags, ",", 0);
345  int i = 0;
346  gchar *tag_prefix;
347  gchar *tag_value;
348 
349  while (tags[i] != NULL)
350  {
351  int j = 0;
352  tag_value = NULL;
353  tag_prefix = g_strconcat (tags[i], "=", NULL);
354  while (nvti_tags[j] != NULL && tag_value == NULL)
355  {
356  if (g_str_has_prefix (nvti_tags[j], tag_prefix))
357  {
358  tag_value = g_strstr_len (nvti_tags[j], -1, "=");
359  }
360  j++;
361  }
362  g_free (tag_prefix);
363 
364  if (tag_value != NULL)
365  {
366  tag_value = tag_value + 1;
367  gchar *tag_line = g_strdup_printf ("%s:\n%s\n\n", tags[i],
368  tag_value);
369  g_string_append (action_str, tag_line);
370 
371  g_free (tag_line);
372  }
373  i++;
374  }
375  g_strfreev (tags);
376  }
377  }
378 
379  len = action_str->len;
380  buffer = g_malloc0 (1024 + len + 1);
381  char idbuffer[105];
382  if (oid == NULL)
383  {
384  *idbuffer = '\0';
385  }
386  else
387  {
388  snprintf (idbuffer, sizeof (idbuffer), "<|> %s ", oid);
389  }
390  if (port > 0)
391  {
392  snprintf (buffer, 1024 + len,
393  "SERVER <|> %s <|> %s <|> %d/%s <|> %s %s<|> SERVER\n",
394  what, plug_get_hostname (desc), port, proto,
395  action_str->str, idbuffer);
396  }
397  else
398  snprintf (buffer, 1024 + len,
399  "SERVER <|> %s <|> %s <|> general/%s <|> %s %s<|> SERVER\n", what,
400  plug_get_hostname (desc), proto, action_str->str,
401  idbuffer);
402 
403  mark_post (oid, desc, what, action);
404  soc = arg_get_value_int (arg_get_value (desc, "globals"), "global_socket");
405  /* Convert to UTF-8 before sending to Manager. */
406  data = g_convert (buffer, -1, "UTF-8", "ISO_8859-1", NULL, &length, NULL);
408  g_free (data);
409 
410  /* Mark in the KB that the plugin was successful */
411  mark_successful_plugin (oid, desc);
412 
413  g_free (buffer);
414  g_string_free (action_str, TRUE);
415 }
416 
417 void
418 proto_post_alarm (const char *oid, struct arglist *desc, int port,
419  const char *proto, const char *action)
420 {
421  proto_post_wrapped (oid, desc, port, proto, action, "ALARM");
422 }
423 
424 void
425 post_alarm (const char *oid, struct arglist *desc, int port, const char *action)
426 {
427  proto_post_alarm (oid, desc, port, "tcp", action);
428 }
429 
430 
434 void
435 proto_post_log (const char *oid, struct arglist *desc, int port,
436  const char *proto, const char *action)
437 {
438  proto_post_wrapped (oid, desc, port, proto, action, "LOG");
439 }
440 
444 void
445 post_log (const char *oid, struct arglist *desc, int port, const char *action)
446 {
447  proto_post_log (oid, desc, port, "tcp", action);
448 }
449 
450 void
451 proto_post_error (const char *oid, struct arglist *desc, int port,
452  const char *proto, const char *action)
453 {
454  proto_post_wrapped (oid, desc, port, proto, action, "ERRMSG");
455 }
456 
457 
458 void
459 post_error (const char *oid, struct arglist *desc, int port, const char *action)
460 {
461  proto_post_error (oid, desc, port, "tcp", action);
462 }
463 
464 void
465 add_plugin_preference (struct arglist *desc, const char *name, const char *type,
466  const char *defaul)
467 {
468  nvti_t *n = arg_get_value (desc, "NVTI");
469  nvtpref_t *np = nvtpref_new ((gchar *)name, (gchar *)type, (gchar *)defaul);
470 
471  nvti_add_pref (n, np);
472 }
473 
474 
475 char *
476 get_plugin_preference (const char *oid, const char *name)
477 {
478  struct arglist *prefs;
479  char *plug_name, *cname, *retval = NULL;
480 
481  prefs = preferences_get ();
482  if (!prefs || !nvticache_initialized () || !oid || !name)
483  return NULL;
484 
485  plug_name = nvticache_get_name (oid);
486  if (!plug_name)
487  return NULL;
488  cname = g_strdup (name);
489 
490  g_strchomp (cname);
491  while (prefs->next)
492  {
493  char *a, *b;
494  char *t = prefs->name;
495 
496  a = strchr (t, '[');
497  b = strchr (t, ']');
498  if (a && b && b[1] == ':')
499  {
500  b += 2 * sizeof (char);
501  if (!strcmp (cname, b))
502  {
503  int old = a[0];
504  a[0] = 0;
505  if (!strcmp (t, plug_name))
506  {
507  a[0] = old;
508  retval = g_strdup (prefs->value);
509  break;
510  }
511  a[0] = old;
512  }
513  }
514  prefs = prefs->next;
515  }
516 
517  /* If no value set by the user, get the default one. */
518  if (!retval)
519  {
520  GSList *tmp, *nprefs;
521 
522  tmp = nprefs = nvticache_get_prefs (oid);
523  while (tmp)
524  {
525  const nvtpref_t *nvtpref = tmp->data;
526  if (!strcmp (cname, nvtpref_name (nvtpref)))
527  {
528  retval = g_strdup (nvtpref_default (nvtpref));
529  break;
530  }
531  tmp = tmp->next;
532  }
533  g_slist_free_full (nprefs, (void (*) (void *)) nvtpref_free);
534  }
535 
536  g_free (cname);
537  return retval;
538 }
539 
550 const char *
551 get_plugin_preference_fname (struct arglist *desc, const char *filename)
552 {
553  const char *content;
554  long contentsize = 0;
555  gint tmpfile;
556  gchar *tmpfilename;
557  GError *error = NULL;
558 
559  content = get_plugin_preference_file_content (desc, filename);
560  if (content == NULL)
561  {
562  return NULL;
563  }
564  contentsize = get_plugin_preference_file_size (desc, filename);
565  if (contentsize <= 0)
566  return NULL;
567 
568  tmpfile =
569  g_file_open_tmp ("openvassd-file-upload.XXXXXX", &tmpfilename, &error);
570  if (tmpfile == -1)
571  {
572  log_legacy_write ("get_plugin_preference_fname: Could not open temporary"
573  " file for %s: %s", filename, error->message);
574  g_error_free (error);
575  return NULL;
576  }
577  close (tmpfile);
578 
579  if (!g_file_set_contents (tmpfilename, content, contentsize, &error))
580  {
581  log_legacy_write ("get_plugin_preference_fname: could set contents of"
582  " temporary file for %s: %s", filename, error->message);
583  g_error_free (error);
584  return NULL;
585  }
586 
587  return tmpfilename;
588 }
589 
590 
604 char *
606  const char *identifier)
607 {
608  struct arglist *globals = arg_get_value (desc, "globals");
609  GHashTable *trans;
610 
611  if (!globals)
612  return NULL;
613 
614  trans = arg_get_value (globals, "files_translation");
615  if (!trans)
616  return NULL;
617 
618  return g_hash_table_lookup (trans, identifier);
619 }
620 
621 
636 long
637 get_plugin_preference_file_size (struct arglist *desc, const char *identifier)
638 {
639  struct arglist *globals = arg_get_value (desc, "globals");
640  GHashTable *trans;
641  gchar *filesize_str;
642 
643  if (!globals)
644  return -1;
645 
646  trans = arg_get_value (globals, "files_size_translation");
647  if (!trans)
648  return -1;
649 
650  filesize_str = g_hash_table_lookup (trans, identifier);
651  if (filesize_str == NULL)
652  return -1;
653 
654  return atol (filesize_str);
655 }
656 
657 void
658 plug_set_key (struct arglist *args, char *name, int type, const void *value)
659 {
660  kb_t kb = plug_get_kb (args);
661 
662  if (name == NULL || value == NULL)
663  return;
664 
665  if (type == ARG_STRING)
666  kb_item_add_str (kb, name, value);
667  else if (type == ARG_INT)
668  kb_item_add_int (kb, name, GPOINTER_TO_SIZE (value));
669  if (global_nasl_debug == 1)
670  {
671  if (type == ARG_STRING)
672  log_legacy_write ("set key %s -> %s\n", name, (char *) value);
673  else if (type == ARG_INT)
674  log_legacy_write ("set key %s -> %d\n", name,
675  (int) GPOINTER_TO_SIZE (value));
676  }
677 }
678 
679 
680 void
681 plug_replace_key (struct arglist *args, char *name, int type, void *value)
682 {
683  kb_t kb = plug_get_kb (args);
684 
685  if (name == NULL || value == NULL)
686  return;
687 
688  if (type == ARG_STRING)
689  kb_item_set_str (kb, name, value);
690  else if (type == ARG_INT)
691  kb_item_set_int (kb, name, GPOINTER_TO_SIZE (value));
692  if (global_nasl_debug == 1)
693  {
694  if (type == ARG_STRING)
695  log_legacy_write ("replace key %s -> %s\n", name, (char *) value);
696  else if (type == ARG_INT)
697  log_legacy_write ("replace key %s -> %d\n", name,
698  (int) GPOINTER_TO_SIZE (value));
699  }
700 }
701 
702 void
703 scanner_add_port (struct arglist *args, int port, char *proto)
704 {
705  host_add_port_proto (args, port, proto);
706 }
707 
708 
709 kb_t
710 plug_get_kb (struct arglist *args)
711 {
712  return (kb_t) arg_get_value (args, "key");
713 }
714 
715 /*
716  * plug_get_key() may fork(). We use this signal handler to kill
717  * its son in case the process which calls this function is killed
718  * itself
719  */
720 static int _plug_get_key_son = 0;
721 
722 static void
723 plug_get_key_sighand_term ()
724 {
725  int son = _plug_get_key_son;
726 
727  if (son != 0)
728  {
729  kill (son, SIGTERM);
730  _plug_get_key_son = 0;
731  }
732  _exit (0);
733 }
734 
735 static void
736 plug_get_key_sigchld ()
737 {
738  int status;
739 
740  wait (&status);
741 }
742 
743 static void
744 sig_n (int signo, void (*fnc) (int))
745 {
746  struct sigaction sa;
747 
748  sa.sa_handler = fnc;
749  sa.sa_flags = 0;
750  sigemptyset (&sa.sa_mask);
751  sigaction (signo, &sa, (struct sigaction *) 0);
752 }
753 
754 static void
755 sig_term (void (*fcn) ())
756 {
757  sig_n (SIGTERM, fcn);
758 }
759 
760 static void
761 sig_chld (void (*fcn) ())
762 {
763  sig_n (SIGCHLD, fcn);
764 }
765 
766 void *
767 plug_get_key (struct arglist *args, char *name, int *type, int single)
768 {
769  kb_t kb = plug_get_kb (args);
770  struct kb_item *res = NULL, *res_list;
771  int sockpair[2];
772  int upstream = 0;
773 
774  if (type != NULL)
775  *type = -1;
776 
777  if (kb == NULL)
778  return NULL;
779 
780  res = kb_item_get_all (kb, name);
781 
782  if (res == NULL)
783  return NULL;
784 
785  if (res->next == NULL || single) /* No fork - good */
786  {
787  void *ret;
788  if (res->type == KB_TYPE_INT)
789  {
790  if (type != NULL)
791  *type = KB_TYPE_INT;
792  ret = g_memdup (&res->v_int, sizeof (res->v_int));
793  }
794  else
795  {
796  if (type != NULL)
797  *type = KB_TYPE_STR;
798  ret = g_strdup (res->v_str);
799  }
800  kb_item_free (res);
801  return ret;
802  }
803 
804 
805  /* More than one value - we will fork() then */
806  sig_chld (plug_get_key_sigchld);
807  res_list = res;
808  while (res != NULL)
809  {
810  pid_t pid;
811 
812  socketpair (AF_UNIX, SOCK_STREAM, 0, sockpair);
813  if ((pid = fork ()) == 0)
814  {
815  int old;
816  struct arglist *globals;
817  void *ret;
818 
819  sig_term (_exit);
820  kb_lnk_reset (kb);
821  nvticache_reset ();
822  close (sockpair[0]);
823  globals = arg_get_value (args, "globals");
824  old = arg_get_value_int (globals, "global_socket");
825  if (old > 0)
826  close (old);
827  arg_set_value (globals, "global_socket", GSIZE_TO_POINTER (sockpair[1]));
828 
829  srand48 (getpid () + getppid () + time (NULL));
830 
831  if (res->type == KB_TYPE_INT)
832  {
833  if (type != NULL)
834  *type = KB_TYPE_INT;
835  ret = g_memdup (&res->v_int, sizeof (res->v_int));
836  }
837  else
838  {
839  if (type != NULL)
840  *type = KB_TYPE_STR;
841  ret = g_strdup (res->v_str);
842  }
843  kb_item_free (res_list);
844  return ret;
845  }
846  else if (pid < 0)
847  {
848  log_legacy_write ("libopenvas:%s:%s(): fork() failed (%s)", __FILE__,
849  __func__, strerror (errno));
850  kb_item_free (res_list);
851  return NULL;
852  }
853  else
854  {
855  int e;
856  int status;
857  struct arglist *globals;
858 
859  globals = arg_get_value (args, "globals");
860  upstream = arg_get_value_int (globals, "global_socket");
861  close (sockpair[1]);
862  _plug_get_key_son = pid;
863  sig_term (plug_get_key_sighand_term);
864  for (;;)
865  {
866  fd_set rd;
867  struct timeval tv;
868  int type;
869 
870  do
871  {
872  tv.tv_sec = 0;
873  tv.tv_usec = 100000;
874  FD_ZERO (&rd);
875  FD_SET (sockpair[0], &rd);
876  e = select (sockpair[0] + 1, &rd, NULL, NULL, &tv);
877  }
878  while (e < 0 && errno == EINTR);
879 
880  if (e > 0)
881  {
882  char *buf = NULL;
883  int bufsz = 0;
884 
885  e = internal_recv (sockpair[0], &buf, &bufsz, &type);
886  if (e < 0 || (type & INTERNAL_COMM_MSG_TYPE_CTRL))
887  {
888  waitpid (pid, &status, WNOHANG);
889  _plug_get_key_son = 0;
890  close (sockpair[0]);
891  sig_term (_exit);
892  g_free (buf); /* Left NULL on error, harmless */
893  break;
894  }
895  else
896  internal_send (upstream, buf, type);
897 
898  g_free (buf);
899  }
900  }
901  }
902  res = res->next;
903  }
904  kb_item_free (res_list);
905  internal_send (upstream, NULL,
907  exit (0);
908 }
909 
916 unsigned int
918 {
919  kb_t kb = plug_get_kb (desc);
920  struct kb_item *res, *k;
921  int open21 = 0, open80 = 0;
922 #define MAX_CANDIDATES 16
923  u_short candidates[MAX_CANDIDATES];
924  int num_candidates = 0;
925 
926  k = res = kb_item_get_pattern (kb, "Ports/tcp/*");
927  if (res == NULL)
928  return 0;
929  else
930  {
931  int ret;
932  char *s;
933 
934  for (;;)
935  {
936  s = res->name + sizeof ("Ports/tcp/") - 1;
937  ret = atoi (s);
938  if (ret == 21)
939  open21 = 1;
940  else if (ret == 80)
941  open80 = 1;
942  else
943  {
944  candidates[num_candidates++] = ret;
945  if (num_candidates >= MAX_CANDIDATES)
946  break;
947  }
948  res = res->next;
949  if (res == NULL)
950  break;
951  }
952 
953  kb_item_free (k);
954  if (num_candidates != 0)
955  return candidates[lrand48 () % num_candidates];
956  else if (open21)
957  return 21;
958  else if (open80)
959  return 80;
960  else
961  return 0;
962  }
963 
964  /* Not reachable */
965  return 0;
966 }
967 
968 
969 
975 void
976 plug_set_port_transport (struct arglist *args, int port, int tr)
977 {
978  char s[256];
979 
980  snprintf (s, sizeof (s), "Transports/TCP/%d", port);
981  plug_set_key (args, s, ARG_INT, GSIZE_TO_POINTER (tr));
982 }
983 
984 
985 /* Return the transport encapsulation mode (OPENVAS_ENCAPS_*) for the
986  given PORT. If no such encapsulation mode has been stored in the
987  knowledge base (or its value is < 0), OPENVAS_ENCAPS_IP is
988  currently returned. */
989 int
990 plug_get_port_transport (struct arglist *args, int port)
991 {
992  char s[256];
993  int trp;
994 
995  snprintf (s, sizeof (s), "Transports/TCP/%d", port);
996  trp = kb_item_get_int (plug_get_kb (args), s);
997  if (trp >= 0)
998  return trp;
999  else
1000  return OPENVAS_ENCAPS_IP; /* Change this to 0 for ultra smart SSL negotiation, at the expense
1001  of possibly breaking stuff */
1002 }
1003 
1004 static void
1005 plug_set_ssl_item (struct arglist *args, char *item, char *itemfname)
1006 {
1007  char s[256];
1008  snprintf (s, sizeof (s), "SSL/%s", item);
1009  plug_set_key (args, s, ARG_STRING, itemfname);
1010 }
1011 
1012 void
1013 plug_set_ssl_cert (struct arglist *args, char *cert)
1014 {
1015  plug_set_ssl_item (args, "cert", cert);
1016 }
1017 
1018 void
1019 plug_set_ssl_key (struct arglist *args, char *key)
1020 {
1021  plug_set_ssl_item (args, "key", key);
1022 }
1023 
1024 void
1025 plug_set_ssl_pem_password (struct arglist *args, char *key)
1026 {
1027  plug_set_ssl_item (args, "password", key);
1028 }
1029 
1034 void
1035 plug_set_ssl_CA_file (struct arglist *args, char *key)
1036 {
1037  plug_set_ssl_item (args, "CA", key);
1038 }
1039 
1040 char *
1041 find_in_path (char *name, int safe)
1042 {
1043  char *buf = getenv ("PATH"), *pbuf, *p1, *p2;
1044  static char cmd[MAXPATHLEN];
1045  int len = strlen (name);
1046 
1047  if (len >= MAXPATHLEN)
1048  return NULL;
1049 
1050  if (buf == NULL) /* Should we use a standard PATH here? */
1051  return NULL;
1052 
1053  pbuf = buf;
1054  while (*pbuf != '\0')
1055  {
1056  for (p1 = pbuf, p2 = cmd; *p1 != ':' && *p1 != '\0';)
1057  *p2++ = *p1++;
1058  *p2 = '\0';
1059  if (*p1 == ':')
1060  p1++;
1061  pbuf = p1;
1062  if (p2 == cmd) /* :: found in $PATH */
1063  strcpy (cmd, ".");
1064 
1065  if (cmd[0] != '/' && safe)
1066  continue;
1067  if (p2 - cmd + 1 + len >= MAXPATHLEN)
1068  /* path too long: cannot be reached */
1069  continue;
1070 
1071  snprintf (p2, MAXPATHLEN, "/%s", name);
1072  if (access (cmd, X_OK) == 0)
1073  {
1074  struct stat st;
1075  if (stat (cmd, &st) < 0)
1076  perror (cmd);
1077  else if (S_ISREG (st.st_mode))
1078  {
1079  *p2 = '\0';
1080  return cmd;
1081  }
1082  }
1083  }
1084  return NULL;
1085 }
void proto_post_error(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
Definition: plugutils.c:451
#define ARG_INT
Definition: arglists.h:40
gchar * nvtpref_default(const nvtpref_t *np)
Get the Default of a NVT Preference.
Definition: nvti.c:137
void proto_post_alarm(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
Definition: plugutils.c:418
gchar * nvtpref_name(const nvtpref_t *np)
Get the Name of a NVT Preference.
Definition: nvti.c:109
gchar * nvti_xref(const nvti_t *n)
Get the xref's.
Definition: nvti.c:302
Definition: kb.h:49
void plug_replace_key(struct arglist *args, char *name, int type, void *value)
Definition: plugutils.c:681
void kb_item_free(struct kb_item *)
Release a KB item (or a list).
Definition: kb_redis.c:501
void plug_set_dep(struct arglist *desc, const char *depname)
Definition: plugutils.c:87
void nvticache_reset()
Reset connection to KB. To be called after a fork().
Definition: nvticache.c:138
void plug_set_tag(struct arglist *desc, char *name, char *value)
Definition: plugutils.c:72
int arg_set_value(struct arglist *arglst, const char *name, void *value)
Definition: arglists.c:225
int port_in_port_ranges(int pnum, port_protocol_t ptype, array_t *pranges)
Checks if a port num is in port ranges array.
void host_add_port_proto(struct arglist *args, int portnum, char *proto)
Definition: plugutils.c:106
nvtpref_t * nvtpref_new(gchar *name, gchar *type, gchar *dflt)
Create a new nvtpref structure filled with the given values.
Definition: nvti.c:63
void post_error(const char *oid, struct arglist *desc, int port, const char *action)
Definition: plugutils.c:459
void plug_set_key(struct arglist *args, char *name, int type, const void *value)
Definition: plugutils.c:658
void * plug_get_key(struct arglist *args, char *name, int *type, int single)
Definition: plugutils.c:767
The structure of a information record that corresponds to a NVT.
Definition: nvti.h:64
Knowledge base item (defined by name, type (int/char*) and value). Implemented as a singly linked lis...
Definition: kb.h:81
void plug_set_ssl_CA_file(struct arglist *args, char *key)
Definition: plugutils.c:1035
struct kb_item * next
Definition: kb.h:91
int host_get_port_state(struct arglist *plugdata, int portnum)
Definition: plugutils.c:177
const char * oid
enum kb_item_type type
Definition: kb.h:83
void nvtpref_free(nvtpref_t *np)
Free memory of a nvtpref structure.
Definition: nvti.c:86
int nvti_set_tag(nvti_t *n, const gchar *tag)
Set the tags of a NVT.
Definition: nvti.c:648
Protos and data structures for NVT Information data sets.
gchar * nvti_tag(const nvti_t *n)
Get the tag.
Definition: nvti.c:316
void array_free(GPtrArray *array)
Free global array value.
Definition: array.c:64
void log_legacy_write(const char *format,...)
Legacy function to write a log message.
char * plug_get_host_fqdn(struct arglist *desc)
Definition: plugutils.c:200
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:86
void proto_post_wrapped(const char *oid, struct arglist *desc, int port, const char *proto, const char *action, const char *what)
Post a security message (e.g. LOG, NOTE, WARNING ...).
Definition: plugutils.c:270
int type
Definition: arglists.h:34
const char * get_plugin_preference_fname(struct arglist *desc, const char *filename)
Get the file name of a plugins preference that is of type "file".
Definition: plugutils.c:551
char * plug_get_host_ip_str(struct arglist *desc)
Definition: plugutils.c:225
gchar * nvti_dependencies(const nvti_t *n)
Get the dependencies list.
Definition: nvti.c:344
void plug_set_port_transport(struct arglist *args, int port, int tr)
Definition: plugutils.c:976
char * vhosts
Definition: network.h:60
void * value
Definition: arglists.h:32
void plug_set_ssl_pem_password(struct arglist *args, char *key)
Definition: plugutils.c:1025
kb_t plug_get_kb(struct arglist *args)
Definition: plugutils.c:710
void post_log(const char *oid, struct arglist *desc, int port, const char *action)
Post a log message about a tcp port.
Definition: plugutils.c:445
Definition: kb.h:48
char * name
Definition: network.h:58
#define INTERNAL_COMM_CTRL_FINISHED
Definition: internal_com.h:33
int internal_recv(int soc, char **data, int *data_sz, int *msg_type)
Definition: network.c:2318
GPtrArray array_t
Definition: array.h:31
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:102
char * addr6_as_str(const struct in6_addr *addr6)
struct in6_addr * ip
Definition: network.h:61
int kb_get_port_state_proto(kb_t kb, int portnum, char *proto)
Definition: plugutils.c:131
int nvti_set_xref(nvti_t *n, const gchar *xref)
Set the xrefs of a NVT.
Definition: nvti.c:624
port_protocol_t
Possible port types.
#define INTERNAL_COMM_MSG_TYPE_DATA
Definition: internal_com.h:31
#define MAX_CANDIDATES
struct arglist * next
Definition: arglists.h:33
struct in6_addr * plug_get_host_ip(struct arglist *desc)
Definition: plugutils.c:216
void post_alarm(const char *oid, struct arglist *desc, int port, const char *action)
Definition: plugutils.c:425
void plug_set_xref(struct arglist *desc, char *name, char *value)
Definition: plugutils.c:57
int nvti_add_pref(nvti_t *n, nvtpref_t *np)
Add a preference to the NVT Info.
Definition: nvti.c:1109
char * find_in_path(char *name, int safe)
Definition: plugutils.c:1041
const char * name
Definition: nasl_init.c:524
void scanner_add_port(struct arglist *args, int port, char *proto)
Definition: plugutils.c:703
int nvticache_initialized(void)
Return whether the nvt cache is initialized.
Definition: nvticache.c:60
#define INTERNAL_COMM_MSG_TYPE_CTRL
Definition: internal_com.h:30
char * get_plugin_preference_file_content(struct arglist *desc, const char *identifier)
Get the file contents of a plugins preference that is of type "file".
Definition: plugutils.c:605
int v_int
Definition: kb.h:88
struct timeval timeval(unsigned long val)
GSList * nvticache_get_prefs(const char *oid)
Get the prefs from a plugin OID.
Definition: nvticache.c:518
#define ARG_STRING
Definition: arglists.h:38
struct arglist * preferences_get(void)
Get the pointer to the global preferences structure. Eventually this function should not be used anyw...
Definition: prefs.c:68
int host_get_port_state_udp(struct arglist *plugdata, int portnum)
Definition: plugutils.c:183
int arg_get_value_int(struct arglist *args, const char *name)
Definition: arglists.c:246
int internal_send(int soc, char *data, int msg_type)
Definition: network.c:2263
int global_nasl_debug
Definition: plugutils.c:54
void add_plugin_preference(struct arglist *desc, const char *name, const char *type, const char *defaul)
Definition: plugutils.c:465
int host_get_port_state_proto(struct arglist *plugdata, int portnum, char *proto)
Definition: plugutils.c:169
void plug_set_ssl_cert(struct arglist *args, char *cert)
Definition: plugutils.c:1013
void plug_set_ssl_key(struct arglist *args, char *key)
Definition: plugutils.c:1019
The structure for a preference of a NVT.
Definition: nvti.h:45
char * name
Definition: arglists.h:31
unsigned int plug_get_host_open_port(struct arglist *desc)
Definition: plugutils.c:917
int nvti_set_dependencies(nvti_t *n, const gchar *dependencies)
Set the dependencies of a NVT.
Definition: nvti.c:696
const char * plug_get_hostname(struct arglist *desc)
Definition: plugutils.c:190
char * nvticache_get_name(const char *oid)
Get the name from a plugin OID.
Definition: nvticache.c:406
long get_plugin_preference_file_size(struct arglist *desc, const char *identifier)
Get the file size of a plugins preference that is of type "file".
Definition: plugutils.c:637
char name[0]
Definition: kb.h:94
void proto_post_log(const char *oid, struct arglist *desc, int port, const char *proto, const char *action)
Post a log message.
Definition: plugutils.c:435
void * arg_get_value(struct arglist *args, const char *name)
Definition: arglists.c:252
int plug_get_port_transport(struct arglist *args, int port)
Definition: plugutils.c:990
int prefs_get_bool(const gchar *key)
Get a boolean expression of a preference value via a key.
Definition: prefs.c:109
char * fqdn
Definition: network.h:59
char * nvticache_get_tags(const char *oid)
Get the tags from a plugin OID.
Definition: nvticache.c:504
char * v_str
Definition: kb.h:87
char * get_plugin_preference(const char *oid, const char *name)
Definition: plugutils.c:476
array_t * port_range_ranges(const char *port_range)
Create a range array from a port_range string.