OpenVAS Libraries  9.0.3
network.c
Go to the documentation of this file.
1 /* OpenVAS
2  * $Id$
3  * Description: Network Functions.
4  *
5  * Authors:
6  * Renaud Deraison <deraison@nessus.org> (Original pre-fork development)
7  * Michel Arboi (Original pre-fork development)
8  * Werner Koch <wk@gnupg.org>
9  *
10  * Copyright:
11  * Based on work Copyright (C) 1998 - 2002 Renaud Deraison
12  * SSL Support Copyright (C) 2001 Michel Arboi
13  * Copyright (C) 2012 Greenbone Networks GmbH
14  *
15  * This library is free software; you can redistribute it and/or
16  * modify it under the terms of the GNU Library General Public
17  * License as published by the Free Software Foundation; either
18  * version 2 of the License, or (at your option) any later version.
19  *
20  * This library is distributed in the hope that it will be useful,
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
23  * Library General Public License for more details.
24  *
25  * You should have received a copy of the GNU General Public License
26  * along with this program; if not, write to the Free Software
27  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
28  */
29 
30 #include <errno.h>
31 #include <sys/types.h>
32 #include <unistd.h>
33 #include <stdarg.h>
34 #include <string.h>
35 #include <errno.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <signal.h>
39 #include <stdio.h> /* for FILE */
40 #include <sys/time.h> /* for gettimeofday */
41 
42 #include <glib.h>
43 
44 #include <gnutls/gnutls.h>
45 #include <gnutls/x509.h>
46 
47 #include "network.h" /* for socket_close() */
48 #include "../base/kb.h" /* for kb_item_get_str() */
49 
50 #include "ids_send.h"
51 #include "prefs.h"
52 #include "plugutils.h"
53 #include "internal_com.h" /* for INTERNAL_COMM_MSG_TYPE_CTRL */
54 #include "support.h"
55 #include "openvas_logging.h"
56 #include "openvas_server.h"
57 
58 #define TIMEOUT 20
59 
60 #ifndef INADDR_NONE
61 #define INADDR_NONE 0xffffffff
62 #endif
63 
64 /*----------------------------------------------------------------*
65  * Low-level connection management *
66  *----------------------------------------------------------------*/
67 
69 typedef struct
70 {
71  int fd;
72  /*
73  * "transport" layer code when stream is encapsultated. Negative transport
74  * signals a free descriptor.
75  */
77  char *priority;
78  int timeout;
79  int options;
81  int port;
82 
83  gnutls_session_t tls_session;
84  gnutls_certificate_credentials_t tls_cred;
86  pid_t pid;
88  char *buf;
89  int bufsz, bufcnt, bufptr;
90  int last_err;
92 
99 #define OPENVAS_FD_MAX 1024
100 #define OPENVAS_FD_OFF 1000000
101 
102 static openvas_connection connections[OPENVAS_FD_MAX];
103 
108 {
109  struct csc_hook_s *next;
110  int (*fnc)(int fd);
111 };
112 
116 static struct csc_hook_s *csc_hooks;
117 
118 
122 #define OPENVAS_STREAM(x) (((x - OPENVAS_FD_OFF) < OPENVAS_FD_MAX) && ((x - OPENVAS_FD_OFF) >=0))
123 
127 #define OVAS_CONNECTION_FROM_FD(fd) (connections + ((fd) - OPENVAS_FD_OFF))
128 
129 static void
130 renice_myself (void)
131 {
132  static pid_t pid = 0;
133  pid_t cpid = getpid ();
134 
135  if (pid != cpid)
136  {
137  int renice_result;
138  if (nice (0) >= 10)
139  return;
140  pid = cpid;
141  errno = 0;
142  renice_result = nice (1);
143  if (renice_result == -1 && errno != 0)
144  {
145  log_legacy_write ("Unable to renice process: %d", errno);
146  }
147  }
148 }
149 
153 static int
154 pid_perror (const char *error)
155 {
156  log_legacy_write ("[%d] %s : %s\n", getpid (), error, strerror (errno));
157  return 0;
158 }
159 
160 int
162 {
164 
165  if (!OPENVAS_STREAM (fd))
166  {
167  errno = EINVAL;
168  return -1;
169  }
170 
171  p = OVAS_CONNECTION_FROM_FD (fd);
172  return p->last_err;
173 }
174 
178 static int
179 get_connection_fd (void)
180 {
181  int i;
182 
183  for (i = 0; i < OPENVAS_FD_MAX; i++)
184  {
185  if (connections[i].pid == 0) /* Not used */
186  {
187  bzero (&(connections[i]), sizeof (connections[i]));
188  connections[i].pid = getpid ();
189  return i + OPENVAS_FD_OFF;
190  }
191  }
192  log_legacy_write ("[%d] %s:%d : Out of OpenVAS file descriptors\n",
193  getpid (), __FILE__, __LINE__);
194  errno = EMFILE;
195  return -1;
196 }
197 
198 
199 
200 static int
201 release_connection_fd (int fd, int already_closed)
202 {
204 
205  if (!OPENVAS_STREAM (fd))
206  {
207  errno = EINVAL;
208  return -1;
209  }
210  p = OVAS_CONNECTION_FROM_FD (fd);
211 
212  g_free (p->buf);
213  p->buf = 0;
214 
215  /* TLS FIXME: we should call gnutls_bye somewhere. OTOH, the OpenSSL
216  * equivalent SSL_shutdown wasn't called anywhere in the OpenVAS
217  * (libopenvas nor elsewhere) code either.
218  */
219 
220 /* So far, fd is always a socket. If this is changed in the future, this
221  * code shall be fixed. */
222  if (p->fd >= 0)
223  {
224 #if DEBUG_SSL > 1
226  "[%d] release_connection_fd: fd > 0 fd=%d\n", getpid (), p->fd);
227 #endif
228  if (shutdown (p->fd, 2) < 0)
229  {
230 #if DEBUG_SSL > 1
231  /*
232  * It's not uncommon to see that one fail, since a lot of
233  * services close the connection before we ask them to
234  * (ie: http), so we don't show this error by default
235  */
236  pid_perror ("release_connection_fd: shutdown()");
237 #endif
238  }
239  if (!already_closed && socket_close (p->fd) < 0)
240  pid_perror ("release_connection_fd: close()");
241  }
242 
243  if (p->tls_session != NULL)
244  gnutls_deinit (p->tls_session);
245  if (p->tls_cred != NULL)
246  gnutls_certificate_free_credentials (p->tls_cred);
247 
248  g_free (p->priority);
249  p->priority = NULL;
250 
251  bzero (p, sizeof (*p));
252  p->transport = -1;
253  p->pid = 0;
254 
255  return 0;
256 }
257 
258 /* ******** Compatibility function ******** */
259 
266 int
267 openvas_register_connection (int soc, void *ssl,
268  gnutls_certificate_credentials_t certcred,
269  openvas_encaps_t encaps)
270 {
271  int fd;
273 
274  if ((fd = get_connection_fd ()) < 0)
275  return -1;
276  p = OVAS_CONNECTION_FROM_FD (fd);
277 
278  p->tls_session = ssl;
279  p->tls_cred = certcred;
280 
281  p->timeout = TIMEOUT; /* default value */
282  p->port = 0; /* just used for debug */
283  p->fd = soc;
284  p->transport = encaps;
285  p->priority = NULL;
286  p->last_err = 0;
287 
288  return fd;
289 }
290 
291 int
293 {
295  if (!OPENVAS_STREAM (fd))
296  {
297  errno = EINVAL;
298  return -1;
299  }
300 
301  p = connections + (fd - OPENVAS_FD_OFF);
302  /* Fixme: Code duplicated from release_connection_fd. Check usage
303  of this function make sure that TLS stuff is also released in
304  case it is used here. */
305  g_free (p->priority);
306  p->priority = NULL;
307  bzero (p, sizeof (*p));
308  p->transport = -1;
309  return 0;
310 }
311 
312 /*----------------------------------------------------------------*
313  * High-level connection management *
314  *----------------------------------------------------------------*/
315 
316 static int __port_closed;
317 
318 static int
319 unblock_socket (int soc)
320 {
321  int flags = fcntl (soc, F_GETFL, 0);
322  if (flags < 0)
323  {
324  pid_perror ("fcntl(F_GETFL)");
325  return -1;
326  }
327  if (fcntl (soc, F_SETFL, O_NONBLOCK | flags) < 0)
328  {
329  pid_perror ("fcntl(F_SETFL,O_NONBLOCK)");
330  return -1;
331  }
332  return 0;
333 }
334 
335 static int
336 block_socket (int soc)
337 {
338  int flags = fcntl (soc, F_GETFL, 0);
339  if (flags < 0)
340  {
341  pid_perror ("fcntl(F_GETFL)");
342  return -1;
343  }
344  if (fcntl (soc, F_SETFL, (~O_NONBLOCK) & flags) < 0)
345  {
346  pid_perror ("fcntl(F_SETFL,~O_NONBLOCK)");
347  return -1;
348  }
349  return 0;
350 }
351 
352 /*
353  * Initialize the SSL library (error strings and algorithms) and try
354  * to set the pseudo random generator to something less silly than the
355  * default value: 1 according to SVID 3, BSD 4.3, ISO 9899 :-(
356  */
357 
358 void
359 tlserror (char *txt, int err)
360 {
361  log_legacy_write ("[%d] %s: %s\n", getpid (), txt, gnutls_strerror (err));
362 }
363 
364 #ifdef DEBUG_SSL
365 static void
366 log_message_gnutls (int level, const char *msg)
367 {
368  log_legacy_write ("LEVEL %d: %s\n", level, msg);
369 }
370 #endif
371 
375 int
377 {
378 #ifdef DEBUG_SSL
379  gnutls_global_set_log_level (2);
380  gnutls_global_set_log_function (log_message_gnutls);
381 #endif
382 
383  int ret = gnutls_global_init ();
384  if (ret < 0)
385  {
386  tlserror ("gnutls_global_init", ret);
387  return -1;
388  }
389 
390  return 0;
391 }
392 
393 
394 int
396 {
397  openvas_connection *fp;
398 
399  if (!OPENVAS_STREAM (fd))
400  {
402  "[%d] openvas_get_socket_from_connection: bad fd <%d>\n",
403  getpid (), fd);
405  return fd;
406  }
407  fp = connections + (fd - OPENVAS_FD_OFF);
408  if (fp->transport <= 0)
409  {
411  "openvas_get_socket_from_connection: fd <%d> is closed\n", fd);
412  return -1;
413  }
414  return fp->fd;
415 }
416 
417 gnutls_session_t
419 {
420  openvas_connection *fp;
421 
422  if (!OPENVAS_STREAM (fd))
423  return NULL;
424 
425  fp = connections + (fd - OPENVAS_FD_OFF);
426  return fp->tls_session;
427 }
428 
434 static int
435 set_gnutls_protocol (gnutls_session_t session, openvas_encaps_t encaps,
436  const char *priority)
437 {
438  const char * priorities;
439  const char * errloc;
440  int err;
441 
442  switch (encaps)
443  {
445  priorities = "NORMAL:-VERS-TLS-ALL:+VERS-SSL3.0";
446  break;
448  priorities = "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.0";
449  break;
451  priorities = "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.1";
452  break;
454  priorities = "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.2";
455  break;
456  case OPENVAS_ENCAPS_SSLv23: /* Compatibility mode */
457  priorities = "NORMAL:-VERS-TLS-ALL:+VERS-TLS1.0:+VERS-SSL3.0";
458  break;
459  default:
460 #if DEBUG_SSL > 0
461  log_legacy_write ("*Bug* at %s:%d. Unknown transport %d\n", __FILE__,
462  __LINE__, encaps);
463 #endif
465  priorities = priority;
466  break;
467  }
468 
469  if ((err = gnutls_priority_set_direct (session, priorities, &errloc)))
470  {
471  log_legacy_write ("[%d] setting session priorities '%.20s': %s\n",
472  getpid (), errloc, gnutls_strerror (err));
473  return -1;
474  }
475 
476  return 0;
477 }
478 
487 static int
488 load_cert_and_key (gnutls_certificate_credentials_t xcred, const char *cert,
489  const char *key, const char *passwd)
490 {
491  gnutls_x509_crt_t x509_crt = NULL;
492  gnutls_x509_privkey_t x509_key = NULL;
493  gnutls_datum_t data;
494  int ret;
495  int result = 0;
496 
497  if (load_gnutls_file (cert, &data))
498  {
499  log_legacy_write ("[%d] load_cert_and_key: Error loading cert file %s\n",
500  getpid (), cert);
501  result = -1;
502  goto cleanup;
503  }
504  ret = gnutls_x509_crt_init (&x509_crt);
505  if (ret < 0)
506  {
507  tlserror ("gnutls_x509_crt_init", ret);
508  /* x509_crt may be != NULL even if gnutls_x509_crt_init fails */
509  x509_crt = NULL;
510  result = -1;
511  goto cleanup;
512  }
513  ret = gnutls_x509_crt_import (x509_crt, &data, GNUTLS_X509_FMT_PEM);
514  if (ret < 0)
515  {
516  tlserror ("gnutls_x509_crt_import", ret);
517  result = -1;
518  goto cleanup;
519  }
520  unload_gnutls_file (&data);
521 
522  if (load_gnutls_file (key, &data))
523  {
524  log_legacy_write ("[%d] load_cert_and_key: Error loading key file %s\n",
525  getpid (), key);
526  result = -1;
527  goto cleanup;
528  }
529  ret = gnutls_x509_privkey_init (&x509_key);
530  if (ret < 0)
531  {
532  tlserror ("gnutls_x509_privkey_init", ret);
533  /* x509_key may be != NULL even if gnutls_x509_privkey_init fails */
534  x509_key = NULL;
535  result = -1;
536  goto cleanup;
537  }
538  if (passwd)
539  {
540  ret =
541  gnutls_x509_privkey_import_pkcs8 (x509_key, &data, GNUTLS_X509_FMT_PEM,
542  passwd, 0);
543  if (ret < 0)
544  {
545  tlserror ("gnutls_x509_privkey_import_pkcs8", ret);
546  result = -1;
547  goto cleanup;
548  }
549  }
550  else
551  {
552  ret = gnutls_x509_privkey_import (x509_key, &data, GNUTLS_X509_FMT_PEM);
553  if (ret < 0)
554  {
555  tlserror ("gnutls_x509_privkey_import", ret);
556  result = -1;
557  goto cleanup;
558  }
559  }
560  unload_gnutls_file (&data);
561 
562  ret = gnutls_certificate_set_x509_key (xcred, &x509_crt, 1, x509_key);
563  if (ret < 0)
564  {
565  tlserror ("gnutls_certificate_set_x509_key", ret);
566  result = -1;
567  goto cleanup;
568  }
569 
570 cleanup:
571 
572  if (x509_crt)
573  gnutls_x509_crt_deinit (x509_crt);
574  if (x509_key)
575  gnutls_x509_privkey_deinit (x509_key);
576 
577  return result;
578 }
579 
580 static int
581 is_ip_address (const char *str)
582 {
583  struct sockaddr_in sa;
584  struct sockaddr_in6 sa6;
585 
586  if (inet_pton (AF_INET, str, &(sa.sin_addr)) == 1)
587  return 1;
588 
589  return inet_pton (AF_INET6, str, &(sa6.sin6_addr)) == 1;
590 }
591 
592 static int
593 open_SSL_connection (openvas_connection * fp, const char *cert,
594  const char *key, const char *passwd, const char *cafile,
595  const char *hostname)
596 {
597  int ret, err, d;
598  time_t tictac;
599  fd_set fdw, fdr;
600  struct timeval to;
601 
602  ret = gnutls_init (&(fp->tls_session), GNUTLS_CLIENT);
603  if (ret < 0)
604  {
605  tlserror ("gnutls_init", ret);
606  return -1;
607  }
608 
609  /* set_gnutls_protocol handles OPENVAS_ENCAPS_SSLv2 by falling back
610  * to OPENVAS_ENCAPS_SSLv23. However, this function
611  * (open_SSL_connection) is called only by open_stream_connection and
612  * open_stream_connection will exit with an error code if called with
613  * OPENVAS_ENCAPS_SSLv2, so it should never end up calling
614  * open_SSL_connection with OPENVAS_ENCAPS_SSLv2.
615  */
616  if (set_gnutls_protocol (fp->tls_session, fp->transport, fp->priority) < 0)
617  return -1;
618 
619  if (hostname && !is_ip_address (hostname))
620  gnutls_server_name_set (fp->tls_session, GNUTLS_NAME_DNS, hostname,
621  strlen (hostname));
622 
623  ret = gnutls_certificate_allocate_credentials (&(fp->tls_cred));
624  if (ret < 0)
625  {
626  tlserror ("gnutls_certificate_allocate_credentials", ret);
627  return -1;
628  }
629  ret =
630  gnutls_credentials_set (fp->tls_session, GNUTLS_CRD_CERTIFICATE,
631  fp->tls_cred);
632  if (ret < 0)
633  {
634  tlserror ("gnutls_credentials_set", ret);
635  return -1;
636  }
637 
638  if (cert != NULL && key != NULL)
639  {
640  if (load_cert_and_key (fp->tls_cred, cert, key, passwd) < 0)
641  return -1;
642  }
643 
644  if (cafile != NULL)
645  {
646  ret =
647  gnutls_certificate_set_x509_trust_file (fp->tls_cred, cafile,
648  GNUTLS_X509_FMT_PEM);
649  if (ret < 0)
650  {
651  tlserror ("gnutls_certificate_set_x509_trust_file", ret);
652  return -1;
653  }
654  }
655 
656  unblock_socket (fp->fd);
657 
658  gnutls_transport_set_ptr (fp->tls_session,
659  (gnutls_transport_ptr_t) GSIZE_TO_POINTER (fp->fd));
660 
661  tictac = time (NULL);
662 
663  for (;;)
664  {
665  err = gnutls_handshake (fp->tls_session);
666 
667  if (err == 0)
668  return 1;
669 
670  if (err != GNUTLS_E_INTERRUPTED && err != GNUTLS_E_AGAIN
671  && err != GNUTLS_E_WARNING_ALERT_RECEIVED)
672  {
673 #ifdef DEBUG_SSL
674  tlserror ("gnutls_handshake", err);
675 #endif
676  return -1;
677  }
678 
679  FD_ZERO (&fdr);
680  FD_SET (fp->fd, &fdr);
681  FD_ZERO (&fdw);
682  FD_SET (fp->fd, &fdw);
683 
684  do
685  {
686  d = tictac + fp->timeout - time (NULL);
687  if (d <= 0)
688  {
689  fp->last_err = ETIMEDOUT;
690  return -1;
691  }
692  to.tv_sec = d;
693  to.tv_usec = 0;
694  errno = 0;
695  if ((ret = select (fp->fd + 1, &fdr, &fdw, NULL, &to)) <= 0)
696  {
697 #if DEBUG_SSL > 1
698  pid_perror ("select");
699 #endif
700  }
701  }
702  while (ret < 0 && errno == EINTR);
703 
704  if (ret <= 0)
705  {
706  fp->last_err = ETIMEDOUT;
707  return -1;
708  }
709  }
710  /*NOTREACHED*/}
711 
712 
713 static void
714 set_ids_evasion_mode (struct arglist *args, openvas_connection * fp)
715 {
716  kb_t kb = plug_get_kb (args);
717  char *ids_evasion_split, *ids_evasion_inject, *ids_evasion_short_ttl;
718  char *ids_evasion_fake_rst;
719  int option = 0;
720 
721  if (kb_item_get_int (kb, "NIDS/TCP/enabled") != 1)
722  return;
723 
724  ids_evasion_split = kb_item_get_str (kb, "NIDS/TCP/split");
725  ids_evasion_inject = kb_item_get_str (kb, "NIDS/TCP/inject");
726  ids_evasion_short_ttl = kb_item_get_str (kb, "NIDS/TCP/short_ttl");
727  ids_evasion_fake_rst = kb_item_get_str (kb, "NIDS/TCP/fake_rst");
728  /*
729  * These first three options are mutually exclusive
730  */
731  if (ids_evasion_split != NULL && strcmp (ids_evasion_split, "yes") == 0)
733 
734  if (ids_evasion_inject != NULL && strcmp (ids_evasion_inject, "yes") == 0)
736 
737  if (ids_evasion_short_ttl != NULL
738  && strcmp (ids_evasion_short_ttl, "yes") == 0)
740 
741 
742  /*
743  * This is not exclusive with the above
744  */
745  if (ids_evasion_fake_rst != NULL && strcmp (ids_evasion_fake_rst, "yes") == 0)
747 
748  if (option)
749  {
750  int n = 1;
751  (void) setsockopt (fp->fd, SOL_SOCKET, SO_SNDLOWAT, (void *) &n,
752  sizeof (n));
753  fp->options |= option;
754  }
755  g_free (ids_evasion_split);
756  g_free (ids_evasion_inject);
757  g_free (ids_evasion_short_ttl);
758  g_free (ids_evasion_fake_rst);
759 }
760 
761 /*
762  * @brief Upgrade an ENCAPS_IP socket to an SSL/TLS encapsulated one.
763  *
764  * @param[in] fd Socket file descriptor.
765  * @param[in] transport Encapsulation type.
766  * @param[in] arg Script args.
767  *
768  * @return -1 if error, socket file descriptor value otherwise.
769  */
770 int
771 socket_negotiate_ssl (int fd, openvas_encaps_t transport, struct arglist *args)
772 {
773  char *cert = NULL, *key = NULL, *passwd = NULL, *cafile = NULL;
774  char *hostname = NULL;
775  openvas_connection *fp;
776  kb_t kb;
777  char buf[1024];
778 
779  if (!fd_is_stream (fd))
780  {
781  log_legacy_write ("Socket %d is not stream\n", fd);
782  return -1;
783  }
784  fp = OVAS_CONNECTION_FROM_FD(fd);
785  kb = plug_get_kb (args);
786  cert = kb_item_get_str (kb, "SSL/cert");
787  key = kb_item_get_str (kb, "SSL/key");
788  passwd = kb_item_get_str (kb, "SSL/password");
789  cafile = kb_item_get_str (kb, "SSL/CA");
790  snprintf (buf, sizeof (buf), "Host/SNI/%d/force_disable", fp->port);
791  if (kb_item_get_int (kb, buf) <= 0)
792  hostname = plug_get_host_fqdn (args);
793 
794  fp->transport = transport;
795  fp->priority = NULL;
796  if (open_SSL_connection (fp, cert, key, passwd, cafile, hostname) <= 0)
797  {
798  log_legacy_write ("socket_negotiate_ssl: SSL connection failed.\n");
799  release_connection_fd (fd, 0);
800  return -1;
801  }
802  return fd;
803 }
804 
805 /*
806  * @brief Get the peer's certificate from an SSL/TLS encapsulated connection.
807  *
808  * @param[in] fd Socket file descriptor.
809  * @param[out] cert Memory pointer to fill cert pointer.
810  * @param[out] certlen Size of cert.
811  */
812 void
813 socket_get_cert (int fd, void **cert, int *certlen)
814 {
815  gnutls_session_t session;
816  const gnutls_datum_t *cert_list;
817  unsigned int cert_list_len = 0;
818 
819  if (!cert || !certlen)
820  return;
821  if (!fd_is_stream (fd))
822  {
823  log_legacy_write ("Socket %d is not stream\n", fd);
824  return;
825  }
827  if (!session)
828  {
829  log_legacy_write ("Socket %d is not SSL/TLS encapsulated\n", fd);
830  return;
831  }
832  if (gnutls_certificate_type_get (session) != GNUTLS_CRT_X509)
833  return;
834  cert_list = gnutls_certificate_get_peers (session, &cert_list_len);
835  if (cert_list_len == 0)
836  return;
837  *certlen = cert_list[0].size;
838  *cert = g_memdup (cert_list[0].data, *certlen);
839 }
840 
841 /*
842  * @brief Get the TLS version of a connection.
843  *
844  * @param[in] fd Socket file descriptor.
845  *
846  * @return OPENVAS_ENCAPS value if valid session and success, -1 if error.
847  */
848 int
850 {
851  gnutls_session_t session;
852  gnutls_protocol_t version;
853 
854  if (!fd_is_stream (fd))
855  {
856  log_legacy_write ("Socket %d is not stream\n", fd);
857  return -1;
858  }
860  if (!session)
861  {
862  log_legacy_write ("Socket %d is not SSL/TLS encapsulated\n", fd);
863  return -1;
864  }
865 
866  version = gnutls_protocol_get_version (session);
867  switch (version)
868  {
869  case GNUTLS_SSL3:
870  return OPENVAS_ENCAPS_SSLv3;
871  case GNUTLS_TLS1:
872  return OPENVAS_ENCAPS_TLSv1;
873  case GNUTLS_TLS1_1:
874  return OPENVAS_ENCAPS_TLSv11;
875  case GNUTLS_TLS1_2:
876  return OPENVAS_ENCAPS_TLSv12;
877  default:
878  return -1;
879  }
880 }
881 
882 /*
883  * @brief Get the session ID from an SSL/TLS encapsulated connection.
884  *
885  * @param[in] fd Socket file descriptor.
886  * @param[out] sid Pointer where to store Session ID pointer.
887  * @param[out] ssize Size of session id buffer.
888  */
889 void
890 socket_get_ssl_session_id (int fd, void **sid, size_t *ssize)
891 {
892  gnutls_session_t session;
893  void *tmp;
894  *ssize = GNUTLS_MAX_SESSION_ID;
895  int ret;
896 
897  if (!sid)
898  return;
899  if (!fd_is_stream (fd))
900  {
901  log_legacy_write ("Socket %d is not stream\n", fd);
902  return;
903  }
905  if (!session)
906  {
907  log_legacy_write ("Socket %d is not SSL/TLS encapsulated\n", fd);
908  return;
909  }
910  tmp = g_malloc0 (*ssize);
911  ret = gnutls_session_get_id (session, tmp, ssize);
912  if (ret == GNUTLS_E_SUCCESS)
913  *sid = tmp;
914  else
915  {
916  g_free (tmp);
917  *ssize = 0;
918  tlserror ("gnutls_session_id", ret);
919  }
920 }
921 
922 /*
923  * @brief Get the compression method of an SSL/TLS connection.
924  *
925  * @param[in] fd Socket file descriptor.
926  *
927  * @return 0 if null compression, 1 if deflate, 2 if lzs, -1 if error.
928  */
929 int
931 {
932  gnutls_session_t session;
933 
934  if (!fd_is_stream (fd))
935  {
936  log_legacy_write ("Socket %d is not stream\n", fd);
937  return -1;
938  }
940  if (!session)
941  {
942  log_legacy_write ("Socket %d is not SSL/TLS encapsulated\n", fd);
943  return -1;
944  }
945 
946  switch (gnutls_compression_get (session))
947  {
948  case GNUTLS_COMP_NULL:
949  return 0;
950  case GNUTLS_COMP_DEFLATE:
951  return 1;
952  default:
953  return -1;
954  }
955 }
956 
957 /*
958  * @brief Get the cipher suite used by a SSL/TLS connection.
959  *
960  * @param[in] fd Socket file descriptor.
961  *
962  * @return Cipher Suite ID, -1 if error.
963  */
964 int
966 {
967  gnutls_session_t session;
968  gnutls_kx_algorithm_t kx, kx2;
969  gnutls_cipher_algorithm_t cipher, cipher2;
970  gnutls_mac_algorithm_t mac, mac2;
971  size_t idx = 0;
972  unsigned char cs_id[2];
973 
974  if (!fd_is_stream (fd))
975  {
976  log_legacy_write ("Socket %d is not stream\n", fd);
977  return -1;
978  }
980  if (!session)
981  {
982  log_legacy_write ("Socket %d is not SSL/TLS encapsulated\n", fd);
983  return -1;
984  }
985 
986  kx = gnutls_kx_get (session);
987  cipher = gnutls_cipher_get (session);
988  mac = gnutls_mac_get (session);
989  while (gnutls_cipher_suite_info (idx, (void *) cs_id, &kx2, &cipher2, &mac2,
990  NULL))
991  {
992  if (kx == kx2 && cipher == cipher2 && mac == mac2)
993  return cs_id[0] + cs_id[1];
994  idx++;
995  }
996  return -1;
997 }
998 
999 /* Extended version of open_stream_connection to allow passing a
1000  priority string.
1001 
1002  ABI_BREAK_NOTE: Merge this with open_stream_connection. */
1003 int
1004 open_stream_connection_ext (struct arglist *args, unsigned int port,
1005  int transport, int timeout, const char *priority)
1006 {
1007  int fd;
1008  openvas_connection *fp;
1009  char *cert = NULL;
1010  char *key = NULL;
1011  char *passwd = NULL;
1012  char *cafile = NULL;
1013  char *hostname = NULL;
1014 
1015  if (!priority)
1016  priority = ""; /* To us an empty string is equivalent to NULL. */
1017 
1018 #if DEBUG_SSL > 2
1020  "[%d] open_stream_connection: TCP:%d transport:%d timeout:%d "
1021  " priority: '%s'\n",
1022  getpid (), port, transport, timeout, priority);
1023 #endif
1024 
1025  if (timeout == -2)
1026  timeout = TIMEOUT;
1027 
1028  switch (transport)
1029  {
1030  case OPENVAS_ENCAPS_IP:
1031 
1032  case OPENVAS_ENCAPS_SSLv23:
1033  case OPENVAS_ENCAPS_SSLv3:
1034  case OPENVAS_ENCAPS_TLSv1:
1035  case OPENVAS_ENCAPS_TLSv11:
1036  case OPENVAS_ENCAPS_TLSv12:
1038  case OPENVAS_ENCAPS_SSLv2:
1039  break;
1040 
1041  default:
1042  log_legacy_write ("open_stream_connection_ext(): unsupported transport"
1043  " layer %d\n", transport);
1044  errno = EINVAL;
1045  return -1;
1046  }
1047 
1048  if ((fd = get_connection_fd ()) < 0)
1049  return -1;
1050  fp = OVAS_CONNECTION_FROM_FD (fd);
1051 
1052  fp->transport = transport;
1053  g_free (fp->priority);
1054  if (*priority)
1055  fp->priority = g_strdup (priority);
1056  else
1057  fp->priority = NULL;
1058  fp->timeout = timeout;
1059  fp->port = port;
1060  fp->last_err = 0;
1061  set_ids_evasion_mode (args, fp);
1062 
1064  fp->fd = ids_open_sock_tcp (args, port, fp->options, timeout);
1065  else
1066  fp->fd = open_sock_tcp (args, port, timeout);
1067 
1068  if (fp->fd < 0)
1069  goto failed;
1070 
1071  kb_t kb = plug_get_kb (args);
1072  switch (transport)
1073  {
1074  int ret;
1075  char buf[1024];
1076  case OPENVAS_ENCAPS_IP:
1077  break;
1078  case OPENVAS_ENCAPS_SSLv23:
1079  case OPENVAS_ENCAPS_SSLv3:
1080  case OPENVAS_ENCAPS_TLSv1:
1081  case OPENVAS_ENCAPS_TLSv11:
1082  case OPENVAS_ENCAPS_TLSv12:
1084  renice_myself ();
1085  cert = kb_item_get_str (kb, "SSL/cert");
1086  key = kb_item_get_str (kb, "SSL/key");
1087  passwd = kb_item_get_str (kb, "SSL/password");
1088 
1089  cafile = kb_item_get_str (kb, "SSL/CA");
1090 
1091  /* fall through */
1092 
1093  case OPENVAS_ENCAPS_SSLv2:
1094  /* We do not need a client certificate in this case */
1095  snprintf (buf, sizeof (buf), "Host/SNI/%d/force_disable", fp->port);
1096  if (kb_item_get_int (kb, buf) <= 0)
1097  hostname = plug_get_host_fqdn (args);
1098  ret = open_SSL_connection (fp, cert, key, passwd, cafile, hostname);
1099  g_free (cert);
1100  g_free (key);
1101  g_free (passwd);
1102  g_free (cafile);
1103  if (ret <= 0)
1104  goto failed;
1105  break;
1106  }
1107 
1108  return fd;
1109 
1110 failed:
1111  release_connection_fd (fd, 0);
1112  return -1;
1113 }
1114 
1115 
1116 int
1117 open_stream_connection (struct arglist *args, unsigned int port,
1118  int transport, int timeout)
1119 {
1120  return open_stream_connection_ext (args, port, transport, timeout,
1121  "NORMAL:+ARCFOUR-128");
1122 }
1123 
1124 /* Same as open_stream_auto_encaps but allows to force auto detection
1125  of the protocols if FORCE is true. */
1126 int
1127 open_stream_auto_encaps_ext (struct arglist *args, unsigned int port,
1128  int timeout, int force)
1129 {
1130  int fd, transport;
1131 
1132  if (force)
1133  {
1134  /* Try SSL/TLS first */
1135  transport = OPENVAS_ENCAPS_TLScustom;
1136  fd = open_stream_connection (args, port, transport, timeout);
1137  if (fd < 0)
1138  {
1139  transport = OPENVAS_ENCAPS_IP;
1140  fd = open_stream_connection (args, port, OPENVAS_ENCAPS_IP, timeout);
1141  if (fd < 0)
1142  return -1;
1143  }
1144  /* Store that encapsulation mode in the KB. */
1145  plug_set_port_transport (args, port, transport);
1146  return fd;
1147  }
1148  else
1149  {
1150  transport = plug_get_port_transport (args, port);
1151  fd = open_stream_connection (args, port, transport, timeout);
1152  return fd;
1153  }
1154  /*NOTREACHED*/
1155 }
1156 
1157 int
1158 stream_set_timeout (int fd, int timeout)
1159 {
1160  int old;
1161  openvas_connection *fp;
1162  if (!OPENVAS_STREAM (fd))
1163  {
1164  errno = EINVAL;
1165  return 0;
1166  }
1167  fp = OVAS_CONNECTION_FROM_FD (fd);
1168  old = fp->timeout;
1169  fp->timeout = timeout;
1170  return old;
1171 }
1172 
1173 static int
1174 read_stream_connection_unbuffered (int fd, void *buf0, int min_len, int max_len)
1175 {
1176  int ret, realfd, trp, t;
1177  int total = 0, flag = 0, timeout = TIMEOUT, waitall = 0;
1178  unsigned char *buf = (unsigned char *) buf0;
1179  openvas_connection *fp = NULL;
1180  fd_set fdr, fdw;
1181  struct timeval tv;
1182  time_t now, then;
1183 
1184  int select_status;
1185 
1186 #if 0
1187  log_lecacy_write ("read_stream_connection(%d, 0x%x, %d, %d)\n", fd, buf,
1188  min_len, max_len);
1189 #endif
1190 
1191  if (OPENVAS_STREAM (fd))
1192  {
1193  fp = OVAS_CONNECTION_FROM_FD (fd);
1194  trp = fp->transport;
1195  realfd = fp->fd;
1196  fp->last_err = 0;
1197  if (fp->timeout != -2)
1198  timeout = fp->timeout;
1199  }
1200  else
1201  {
1202 #if 0
1203  log_lecacy_write ("read_stream_connection[%d] : supposedly bad fd %d\n",
1204  getpid (), fd);
1205 #endif
1206  trp = OPENVAS_ENCAPS_IP;
1207  if (fd < 0 || fd > 1024)
1208  {
1209  errno = EBADF;
1210  return -1;
1211  }
1212  realfd = fd;
1213  }
1214 
1215 #ifndef INCR_TIMEOUT
1216 # define INCR_TIMEOUT 1
1217 #endif
1218 
1219  if (min_len == max_len || timeout <= 0)
1220  waitall = MSG_WAITALL;
1221  if (trp == OPENVAS_ENCAPS_IP)
1222  {
1223  for (t = 0; total < max_len && (timeout <= 0 || t < timeout);)
1224  {
1225  tv.tv_sec = INCR_TIMEOUT; /* Not timeout! */
1226  tv.tv_usec = 0;
1227  FD_ZERO (&fdr);
1228  FD_SET (realfd, &fdr);
1229  if (select (realfd + 1, &fdr, NULL, NULL, timeout > 0 ? &tv : NULL) <=
1230  0)
1231  {
1232  t += INCR_TIMEOUT;
1233  /* Try to be smart */
1234  if (total > 0 && flag)
1235  return total;
1236  else if (total >= min_len)
1237  flag++;
1238  }
1239  else
1240  {
1241  errno = 0;
1242  ret = recv (realfd, buf + total, max_len - total, waitall);
1243  if (ret < 0)
1244  if (errno != EINTR)
1245  {
1246  fp->last_err = errno;
1247  return total;
1248  }
1249  else
1250  ret = 0;
1251  else if (ret == 0) /* EOF */
1252  {
1253  fp->last_err = EPIPE;
1254  return total;
1255  }
1256  /*ret > 0 */
1257  total += ret;
1258  if (min_len > 0 && total >= min_len)
1259  return total;
1260  flag = 0;
1261  }
1262  }
1263  if (t >= timeout)
1264  fp->last_err = ETIMEDOUT;
1265  return total;
1266  }
1267 
1268  switch (trp)
1269  {
1270  /* OPENVAS_ENCAPS_IP was treated before with the non-OpenVAS fd */
1271  case OPENVAS_ENCAPS_SSLv2:
1272  case OPENVAS_ENCAPS_SSLv23:
1273  case OPENVAS_ENCAPS_SSLv3:
1274  case OPENVAS_ENCAPS_TLSv1:
1275  case OPENVAS_ENCAPS_TLSv11:
1276  case OPENVAS_ENCAPS_TLSv12:
1278 # if DEBUG_SSL > 0
1279  if (getpid () != fp->pid)
1280  {
1281  log_lecacy_write ("PID %d tries to use a SSL connection established "
1282  "by PID %d\n",
1283  getpid (), fp->pid);
1284  errno = EINVAL;
1285  return -1;
1286  }
1287 # endif
1288 
1289  then = time (NULL);
1290  for (t = 0; timeout <= 0 || t < timeout; t = now - then)
1291  {
1292  now = time (NULL);
1293  tv.tv_sec = INCR_TIMEOUT;
1294  tv.tv_usec = 0;
1295  FD_ZERO (&fdr);
1296  FD_ZERO (&fdw);
1297  FD_SET (realfd, &fdr);
1298  FD_SET (realfd, &fdw);
1299 
1300  select_status = select (realfd + 1, &fdr, &fdw, NULL, &tv);
1301 
1302  if (select_status > 0)
1303  {
1304  /* TLS FIXME: handle rehandshake */
1305  ret =
1306  gnutls_record_recv (fp->tls_session, buf + total,
1307  max_len - total);
1308  if (ret > 0)
1309  {
1310  total += ret;
1311  if (total >= max_len)
1312  return total;
1313  }
1314  else if (ret != GNUTLS_E_INTERRUPTED && ret != GNUTLS_E_AGAIN)
1315  {
1316  /* This branch also handles the case where ret == 0,
1317  * i.e. that the connection has been closed. This is
1318  * for compatibility with the old OpenSSL based openvas
1319  * code which treated SSL_ERROR_ZERO_RETURN as an
1320  * error too.
1321  */
1322 #ifdef DEBUG_SSL
1323  if (ret < 0)
1324  {
1325  tlserror ("gnutls_record_recv", ret);
1326  }
1327  else
1328  {
1329  log_lecacy_write ("gnutls_record_recv[%d]: EOF\n",
1330  getpid ());
1331  }
1332 #endif
1333  fp->last_err = EPIPE;
1334  return total;
1335  }
1336  }
1337 
1338  if (min_len > 0 && total >= min_len)
1339  return total;
1340  }
1341  if (t >= timeout)
1342  fp->last_err = ETIMEDOUT;
1343  return total;
1344 
1345  default:
1346  if (fp->transport || fp->fd != 0)
1347  log_legacy_write ("Severe bug! Unhandled transport layer %d (fd=%d)\n",
1348  fp->transport, fd);
1349  else
1350  log_legacy_write ("read_stream_connection_unbuffered: "
1351  "fd=%d is closed\n",
1352  fd);
1353  errno = EINVAL;
1354  return -1;
1355  }
1356  /*NOTREACHED*/
1357 }
1358 
1359 int
1360 read_stream_connection_min (int fd, void *buf0, int min_len, int max_len)
1361 {
1362  openvas_connection *fp;
1363 
1364  if (OPENVAS_STREAM (fd))
1365  {
1366  fp = OVAS_CONNECTION_FROM_FD (fd);
1367  if (fp->buf != NULL)
1368  {
1369  int l1, l2;
1370 
1371  if (max_len == 1)
1372  min_len = 1; /* avoid "magic read" later */
1373  l2 = max_len > fp->bufcnt ? fp->bufcnt : max_len;
1374  if (l2 > 0)
1375  {
1376  memcpy (buf0, fp->buf + fp->bufptr, l2);
1377  fp->bufcnt -= l2;
1378  if (fp->bufcnt == 0)
1379  {
1380  fp->bufptr = 0;
1381  fp->buf[0] = '\0'; /* debug */
1382  }
1383  else
1384  fp->bufptr += l2;
1385  if (l2 >= min_len || l2 >= max_len)
1386  return l2;
1387  max_len -= l2;
1388  min_len -= l2;
1389  }
1390  if (min_len > fp->bufsz)
1391  {
1392  l1 =
1393  read_stream_connection_unbuffered (fd, (char *) buf0 + l2,
1394  min_len, max_len);
1395  if (l1 > 0)
1396  return l1 + l2;
1397  else
1398  return l2;
1399  }
1400  /* Fill buffer */
1401  l1 =
1402  read_stream_connection_unbuffered (fd, fp->buf, min_len, fp->bufsz);
1403  if (l1 <= 0)
1404  return l2;
1405 
1406  fp->bufcnt = l1;
1407  l1 = max_len > fp->bufcnt ? fp->bufcnt : max_len;
1408  memcpy ((char *) buf0 + l2, fp->buf + fp->bufptr, l1);
1409  fp->bufcnt -= l1;
1410  if (fp->bufcnt == 0)
1411  fp->bufptr = 0;
1412  else
1413  fp->bufptr += l1;
1414  return l1 + l2;
1415  }
1416  }
1417  return read_stream_connection_unbuffered (fd, buf0, min_len, max_len);
1418 }
1419 
1420 int
1421 read_stream_connection (int fd, void *buf0, int len)
1422 {
1423  return read_stream_connection_min (fd, buf0, -1, len);
1424 }
1425 
1426 static int
1427 write_stream_connection4 (int fd, void *buf0, int n, int i_opt)
1428 {
1429  int ret, count;
1430  unsigned char *buf = (unsigned char *) buf0;
1431  openvas_connection *fp;
1432  fd_set fdr, fdw;
1433  struct timeval tv;
1434  int e;
1435 
1436  if (!OPENVAS_STREAM (fd))
1437  {
1438 #if DEBUG_SSL > 0
1439  log_lecacy_write ("write_stream_connection: fd <%d> invalid\n", fd);
1440 # if 0
1441  abort ();
1442 # endif
1443 #endif
1444  errno = EINVAL;
1445  return -1;
1446  }
1447 
1448  fp = OVAS_CONNECTION_FROM_FD (fd);
1449  fp->last_err = 0;
1450 
1451 #if DEBUG_SSL > 8
1452  log_lecacy_write (
1453  "> write_stream_connection(%d, %s, %d, 0x%x) \tE=%d 0=0x%x\n", fd,
1454  buf, n, i_opt, fp->transport, fp->options);
1455 #endif
1456 
1457  switch (fp->transport)
1458  {
1459  case OPENVAS_ENCAPS_IP:
1460  for (count = 0; count < n;)
1461  {
1462  if ((fp->options & OPENVAS_CNX_IDS_EVASION_SEND_MASK) != 0)
1463  {
1465  /* IDS evasion */
1466  ret = send (fp->fd, buf + count, 1, i_opt);
1467  else
1468  /* i_opt ignored for ids_send */
1469  ret = ids_send (fp->fd, buf + count, n - count, fp->options);
1470  }
1471  else
1472  ret = send (fp->fd, buf + count, n - count, i_opt);
1473 
1474  if (ret <= 0)
1475  {
1476  if (ret < 0)
1477  fp->last_err = errno;
1478  else
1479  fp->last_err = EPIPE;
1480  break;
1481  }
1482 
1483  count += ret;
1484  }
1485  break;
1486 
1487  case OPENVAS_ENCAPS_SSLv2:
1488  case OPENVAS_ENCAPS_SSLv23:
1489  case OPENVAS_ENCAPS_SSLv3:
1490  case OPENVAS_ENCAPS_TLSv1:
1491  case OPENVAS_ENCAPS_TLSv11:
1492  case OPENVAS_ENCAPS_TLSv12:
1494 
1495  /* i_opt ignored for SSL */
1496  for (count = 0; count < n;)
1497  {
1498  ret = gnutls_record_send (fp->tls_session, buf + count, n - count);
1499 
1500  if (ret > 0)
1501  {
1502  count += ret;
1503  }
1504  else if (ret != GNUTLS_E_INTERRUPTED && ret != GNUTLS_E_AGAIN)
1505  {
1506  /* This branch also handles the case where ret == 0,
1507  * i.e. that the connection has been closed. This is
1508  * for compatibility with the old openvas code which
1509  * treated SSL_ERROR_ZERO_RETURN as an error too.
1510  */
1511 #ifdef DEBUG_SSL
1512  if (ret < 0)
1513  {
1514  tlserror ("gnutls_record_send", ret);
1515  }
1516  else
1517  {
1518  log_lecacy_write ("gnutls_record_send[%d]: EOF\n", getpid ());
1519  }
1520 #endif
1521  fp->last_err = EPIPE;
1522  break;
1523  }
1524 
1525  if (fp->timeout >= 0)
1526  tv.tv_sec = fp->timeout;
1527  else
1528  tv.tv_sec = TIMEOUT;
1529  tv.tv_usec = 0;
1530 
1531  do
1532  {
1533  errno = 0;
1534  FD_ZERO (&fdr);
1535  FD_ZERO (&fdw);
1536  FD_SET (fp->fd, &fdr);
1537  FD_SET (fp->fd, &fdw);
1538  e = select (fp->fd + 1, &fdr, &fdw, NULL, &tv);
1539  }
1540  while (e < 0 && errno == EINTR);
1541 
1542  if (e <= 0)
1543  {
1544 #if DEBUG_SSL > 0
1545  pid_perror ("select");
1546 #endif
1547  fp->last_err = ETIMEDOUT;
1548  break;
1549  }
1550  }
1551  break;
1552 
1553  default:
1554  if (fp->transport || fp->fd != 0)
1555  log_legacy_write ("Severe bug! Unhandled transport layer %d (fd=%d)\n",
1556  fp->transport, fd);
1557  else
1558  log_legacy_write ("read_stream_connection_unbuffered: fd=%d is "
1559  "closed\n", fd);
1560  errno = EINVAL;
1561  return -1;
1562  }
1563 
1564  if (count == 0 && n > 0)
1565  return -1;
1566  else
1567  return count;
1568 }
1569 
1570 int
1571 write_stream_connection (int fd, void *buf0, int n)
1572 {
1573  return write_stream_connection4 (fd, buf0, n, 0);
1574 }
1575 
1576 int
1577 nsend (int fd, void *data, int length, int i_opt)
1578 {
1579  int n = 0;
1580 
1581  if (OPENVAS_STREAM (fd))
1582  {
1583  if (connections[fd - OPENVAS_FD_OFF].fd < 0)
1584  log_legacy_write ("OpenVAS file descriptor %d closed ?!\n", fd);
1585  else
1586  return write_stream_connection4 (fd, data, length, i_opt);
1587  }
1588  /* Trying OS's send() */
1589  block_socket (fd); /* ??? */
1590  do
1591  {
1592  struct timeval tv = {
1593  0, 5
1594  };
1595  fd_set wr;
1596  int e;
1597 
1598  FD_ZERO (&wr);
1599  FD_SET (fd, &wr);
1600 
1601  errno = 0;
1602  e = select (fd + 1, NULL, &wr, NULL, &tv);
1603  if (e > 0)
1604  n = os_send (fd, data, length, i_opt);
1605  else if (e < 0 && errno == EINTR)
1606  continue;
1607  else
1608  break;
1609  }
1610  while (n <= 0 && errno == EINTR);
1611  if (n < 0)
1612  log_legacy_write ("[%d] nsend():send %s\n", getpid (), strerror (errno));
1613 
1614  return n;
1615 }
1616 
1617 int
1618 nrecv (int fd, void *data, int length, int i_opt)
1619 {
1620  int e;
1621 #if DEBUG_SSL > 8
1622  log_legacy_write ("nrecv: fd=%d len=%d\n", fd, length);
1623 #endif
1624  if (OPENVAS_STREAM (fd))
1625  {
1626  if (connections[fd - OPENVAS_FD_OFF].fd < 0)
1627  log_legacy_write ("OpenVAS file descriptor %d closed ?!\n", fd);
1628  else
1629  return read_stream_connection (fd, data, length);
1630  }
1631  /* Trying OS's recv()
1632  *
1633  * Do *NOT* use os_recv() here, as it will be blocking until the exact
1634  * amount of requested data arrives
1635  */
1636  block_socket (fd);
1637  do
1638  {
1639  e = recv (fd, data, length, i_opt);
1640  }
1641  while (e < 0 && errno == EINTR);
1642  return e;
1643 }
1644 
1645 
1659 void
1661 {
1662  struct csc_hook_s *hook;
1663 
1664  for (hook = csc_hooks; hook; hook = hook->next)
1665  if (hook->fnc == fnc)
1666  return; /* Already added. */
1667 
1668  hook = g_malloc0 (sizeof *hook);
1669  hook->fnc = fnc;
1670  hook->next = csc_hooks;
1671  csc_hooks = hook;
1672 }
1673 
1687 static int
1688 run_csc_hooks (int fd)
1689 {
1690  struct csc_hook_s *hook;
1691 
1692  for (hook = csc_hooks; hook; hook = hook->next)
1693  if (hook->fnc && !hook->fnc (fd))
1694  return 0;
1695  return -1;
1696 }
1697 
1698 int
1700 {
1701 #if DEBUG_SSL > 2
1702  openvas_connection *fp;
1703  if (!OPENVAS_STREAM (fd))
1704  {
1705  errno = EINVAL;
1706  return -1;
1707  }
1708  fp = OVAS_CONNECTION_FROM_FD (fd);
1709  log_legacy_write ("close_stream_connection TCP:%d (fd=%d)\n", fp->port, fd);
1710 #endif
1711 
1712  if (!OPENVAS_STREAM (fd)) /* Will never happen if debug is on! */
1713  {
1714  if (fd < 0 || fd > 1024)
1715  {
1716  errno = EINVAL;
1717  return -1;
1718  }
1719  shutdown (fd, 2);
1720  return socket_close (fd);
1721  }
1722  if (!run_csc_hooks (fd))
1723  return release_connection_fd (fd, 1);
1724  else
1725  return release_connection_fd (fd, 0);
1726 }
1727 
1728 const char *
1730 {
1731  static char str[100];
1732  switch (code)
1733  {
1734  case OPENVAS_ENCAPS_AUTO:
1735  return "auto";
1736  case OPENVAS_ENCAPS_IP:
1737  return "IP";
1738  case OPENVAS_ENCAPS_SSLv2:
1739  return "SSLv2";
1740  case OPENVAS_ENCAPS_SSLv23:
1741  return "SSLv23";
1742  case OPENVAS_ENCAPS_SSLv3:
1743  return "SSLv3";
1744  case OPENVAS_ENCAPS_TLSv1:
1745  return "TLSv1";
1746  case OPENVAS_ENCAPS_TLSv11:
1747  return "TLSv11";
1748  case OPENVAS_ENCAPS_TLSv12:
1749  return "TLSv12";
1751  return "TLScustom";
1752  default:
1753  snprintf (str, sizeof (str), "[unknown transport layer - code %d (0x%x)]", code, code);
1754  return str;
1755  }
1756 }
1757 
1758 const char *
1760 {
1761  static char str[100];
1762  switch (code)
1763  {
1764  case OPENVAS_ENCAPS_IP:
1765  return "";
1766  case OPENVAS_ENCAPS_SSLv2:
1767  case OPENVAS_ENCAPS_SSLv23:
1768  case OPENVAS_ENCAPS_SSLv3:
1769  case OPENVAS_ENCAPS_TLSv1:
1770  case OPENVAS_ENCAPS_TLSv11:
1771  case OPENVAS_ENCAPS_TLSv12:
1773  return " through SSL";
1774  default:
1775  snprintf (str, sizeof (str), " through unknown transport layer - code %d (0x%x)", code, code);
1776  return str;
1777  }
1778 }
1779 
1780 static int
1781 open_socket (struct sockaddr *paddr, int type, int protocol,
1782  int timeout, int len)
1783 {
1784  fd_set fd_w;
1785  struct timeval to;
1786  int soc, x;
1787  int opt;
1788  unsigned int opt_sz;
1789  int family;
1790 
1791  __port_closed = 0;
1792 
1793  if (paddr->sa_family == AF_INET)
1794  {
1795  family = AF_INET;
1796  if ((soc = socket (AF_INET, type, protocol)) < 0)
1797  {
1798  pid_perror ("socket");
1799  return -1;
1800  }
1801  }
1802  else
1803  {
1804  family = AF_INET6;
1805  if ((soc = socket (AF_INET6, type, protocol)) < 0)
1806  {
1807  pid_perror ("socket");
1808  return -1;
1809  }
1810  }
1811 
1812  if (timeout == -2)
1813  timeout = TIMEOUT;
1814 
1815  if (timeout > 0)
1816  if (unblock_socket (soc) < 0)
1817  {
1818  close (soc);
1819  return -1;
1820  }
1821 
1822  openvas_source_set_socket (soc, 0, family);
1823 
1824  if (connect (soc, paddr, len) < 0)
1825  {
1826 #if DEBUG_SSL > 2
1827  pid_perror ("connect");
1828 #endif
1829  again:
1830  switch (errno)
1831  {
1832  case EINPROGRESS:
1833  case EAGAIN:
1834  FD_ZERO (&fd_w);
1835  FD_SET (soc, &fd_w);
1836  to.tv_sec = timeout;
1837  to.tv_usec = 0;
1838  x = select (soc + 1, NULL, &fd_w, NULL, &to);
1839  if (x == 0)
1840  {
1841 #if DEBUG_SSL > 2
1842  pid_perror ("connect->select: timeout");
1843 #endif
1844  socket_close (soc);
1845  errno = ETIMEDOUT;
1846  return -1;
1847  }
1848  else if (x < 0)
1849  {
1850  if (errno == EINTR)
1851  {
1852  errno = EAGAIN;
1853  goto again;
1854  }
1855  pid_perror ("select");
1856  socket_close (soc);
1857  return -1;
1858  }
1859 
1860  opt = 0;
1861  opt_sz = sizeof (opt);
1862  if (getsockopt (soc, SOL_SOCKET, SO_ERROR, &opt, &opt_sz) < 0)
1863  {
1864  pid_perror ("getsockopt");
1865  socket_close (soc);
1866  return -1;
1867  }
1868  if (opt == 0)
1869  break;
1870 #if DEBUG_SSL > 2
1871  errno = opt;
1872  pid_perror ("SO_ERROR");
1873 #endif
1874  /* fallthrough */
1875  default:
1876  __port_closed = 1;
1877  socket_close (soc);
1878  return -1;
1879  }
1880  }
1881  block_socket (soc);
1882  return soc;
1883 }
1884 
1885 
1886 
1887 int
1888 open_sock_opt_hn (const char *hostname, unsigned int port, int type,
1889  int protocol, int timeout)
1890 {
1891  struct sockaddr_in addr;
1892  struct sockaddr_in6 addr6;
1893  struct in6_addr in6addr;
1894 
1895  openvas_resolve_as_addr6 (hostname, &in6addr);
1896  if (IN6_IS_ADDR_V4MAPPED (&in6addr))
1897  {
1898  bzero ((void *) &addr, sizeof (addr));
1899  addr.sin_family = AF_INET;
1900  addr.sin_port = htons ((unsigned short) port);
1901  addr.sin_addr.s_addr = in6addr.s6_addr32[3];
1902  return open_socket ((struct sockaddr *) &addr, type, protocol,
1903  timeout, sizeof (struct sockaddr_in));
1904  }
1905  else
1906  {
1907  bzero ((void *) &addr6, sizeof (addr6));
1908  addr6.sin6_family = AF_INET6;
1909  addr6.sin6_port = htons ((unsigned short) port);
1910  memcpy (&addr6.sin6_addr, &in6addr, sizeof (struct in6_addr));
1911  return open_socket ((struct sockaddr *) &addr6, type, protocol,
1912  timeout, sizeof (struct sockaddr_in6));
1913  }
1914 
1915 }
1916 
1917 int
1918 open_sock_tcp (struct arglist *args, unsigned int port, int timeout)
1919 {
1920  int ret, retry = 0;
1921  const char *timeout_retry;
1922 
1923  timeout_retry = prefs_get ("timeout_retry");
1924  if (timeout_retry)
1925  retry = atoi (timeout_retry);
1926  if (retry < 0)
1927  retry = 0;
1928 
1929  while (retry >= 0)
1930  {
1931  errno = 0;
1932  ret = open_sock_option (args, port, SOCK_STREAM, IPPROTO_TCP, timeout);
1933  if (ret >= 0 || errno != ETIMEDOUT)
1934  break;
1935  retry--;
1936  }
1937  if (ret < 0 && errno == ETIMEDOUT)
1938  {
1939  int log_count;
1940  char *ip_str = plug_get_host_ip_str (args), buffer[1024];
1941  kb_t kb = plug_get_kb (args);
1942 
1943  g_snprintf (buffer, sizeof (buffer), "ConnectTimeout/%s/%d", ip_str,
1944  port);
1945  log_count = kb_item_get_int (kb, buffer);
1946  if (log_count == -1)
1947  log_count = 0;
1948  if (log_count < 3)
1949  log_legacy_write ("open_sock_tcp: %s:%d time-out.", ip_str, port);
1950  log_count++;
1951  kb_item_set_int (kb, buffer, log_count);
1952  g_free (ip_str);
1953  }
1954 
1955  return ret;
1956 }
1957 
1958 
1959 int
1960 open_sock_option (struct arglist *args, unsigned int port, int type,
1961  int protocol, int timeout)
1962 {
1963  struct sockaddr_in addr;
1964  struct sockaddr_in6 addr6;
1965  struct in6_addr *t;
1966 
1967 #if 0
1968  /*
1969  * MA 2004-08-15: IMHO, as this is often (always?) tested in the NASL scripts
1970  * this should not be here.
1971  * If it has to be somewhere else, I'd rather put it in libnasl (and add
1972  * a parameter to "force" the connection)
1973  */
1974  if (host_get_port_state (args, port) <= 0)
1975  return (-1);
1976 #endif
1977  t = plug_get_host_ip (args);
1978  if (!t)
1979  {
1980  log_legacy_write ("ERROR ! NO ADDRESS ASSOCIATED WITH NAME\n");
1981  arg_dump (args, 0);
1982  return (-1);
1983  }
1984  if (IN6_ARE_ADDR_EQUAL (t, &in6addr_any))
1985  return (-1);
1986  if (IN6_IS_ADDR_V4MAPPED (t))
1987  {
1988  bzero ((void *) &addr, sizeof (addr));
1989  addr.sin_family = AF_INET;
1990  addr.sin_port = htons ((unsigned short) port);
1991  addr.sin_addr.s_addr = t->s6_addr32[3];
1992  return open_socket ((struct sockaddr *) &addr, type, protocol,
1993  timeout, sizeof (struct sockaddr_in));
1994  }
1995  else
1996  {
1997  bzero ((void *) &addr6, sizeof (addr6));
1998  addr6.sin6_family = AF_INET6;
1999  addr6.sin6_port = htons ((unsigned short) port);
2000  memcpy (&addr6.sin6_addr, t, sizeof (struct in6_addr));
2001  return open_socket ((struct sockaddr *) &addr6, type, protocol,
2002  timeout, sizeof (struct sockaddr_in6));
2003  }
2004 
2005 }
2006 
2007 
2016 int
2017 recv_line (int soc, char *buf, size_t bufsiz)
2018 {
2019  int n;
2020  unsigned int ret = 0;
2021 
2022  /* Dirty SSL hack */
2023  if (OPENVAS_STREAM (soc))
2024  {
2025  unsigned int ret = 0;
2026  buf[0] = '\0';
2027 
2028  do
2029  {
2030  n = read_stream_connection_min (soc, buf + ret, 1, 1);
2031  switch (n)
2032  {
2033  case -1:
2034  if (ret == 0)
2035  return -1;
2036  else
2037  return ret;
2038  break;
2039 
2040  case 0:
2041  return ret;
2042  break;
2043 
2044  default:
2045  ret++;
2046  }
2047  }
2048  while (buf[ret - 1] != '\0' && buf[ret - 1] != '\n' && ret < bufsiz);
2049 
2050  if (ret > 0)
2051  {
2052  if (buf[ret - 1] != '\0')
2053  {
2054  if (ret < bufsiz)
2055  buf[ret] = '\0';
2056  else
2057  buf[bufsiz - 1] = '\0';
2058  }
2059  }
2060 
2061  return ret;
2062  }
2063  else
2064  {
2065  fd_set rd;
2066 
2067  do
2068  {
2069  int e;
2070  again:
2071  errno = 0;
2072  FD_ZERO (&rd);
2073  FD_SET (soc, &rd);
2074  e = select (soc + 1, &rd, NULL, NULL, NULL);
2075  if (e == 0 && !FD_ISSET (soc, &rd))
2076  return -1;
2077  if (e < 0 && errno == EINTR)
2078  goto again;
2079  if (e > 0)
2080  {
2081  n = recv (soc, buf + ret, 1, 0);
2082  switch (n)
2083  {
2084  case -1:
2085  if (errno == EINTR)
2086  continue;
2087  if (ret == 0)
2088  return -1;
2089  else
2090  return ret;
2091  break;
2092  case 0:
2093  return ret;
2094  break;
2095  default:
2096  ret++;
2097  }
2098  }
2099  else
2100  break;
2101  }
2102  while (buf[ret - 1] != '\0' && buf[ret - 1] != '\n' && ret < bufsiz);
2103 
2104  if (ret > 0)
2105  {
2106  if (buf[ret - 1] != '\0')
2107  {
2108  if (ret < bufsiz)
2109  buf[ret] = '\0';
2110  else
2111  buf[bufsiz - 1] = '\0';
2112  }
2113  }
2114  }
2115 
2116  return ret;
2117 }
2118 
2119 int
2120 socket_close (int soc)
2121 {
2122  return close (soc);
2123 }
2124 
2125 /*
2126  * Select() routines
2127  */
2128 
2129 int
2130 stream_zero (fd_set * set)
2131 {
2132  FD_ZERO (set);
2133  return 0;
2134 }
2135 
2136 int
2137 stream_set (int fd, fd_set * set)
2138 {
2139  int soc = openvas_get_socket_from_connection (fd);
2140  if (soc >= 0)
2141  FD_SET (soc, set);
2142  return soc;
2143 }
2144 
2145 int
2147 {
2148  return OPENVAS_STREAM (fd); /* Should probably be smarter... */
2149 }
2150 
2151 
2152 int
2154 {
2155  openvas_connection *p;
2156  if (!OPENVAS_STREAM (fd))
2157  return -1;
2158  p = OVAS_CONNECTION_FROM_FD (fd);
2159  return p->bufsz;
2160 }
2161 
2162 int
2163 stream_set_buffer (int fd, int sz)
2164 {
2165  openvas_connection *p;
2166  char *b;
2167 
2168  if (!OPENVAS_STREAM (fd))
2169  return -1;
2170 
2171  p = OVAS_CONNECTION_FROM_FD (fd);
2172  if (sz < p->bufcnt)
2173  return -1; /* Do not want to lose data */
2174 
2175  if (sz == 0)
2176  {
2177  g_free (p->buf);
2178  p->buf = NULL;
2179  p->bufsz = 0;
2180  return 0;
2181  }
2182  else if (p->buf == 0)
2183  {
2184  p->buf = g_malloc0 (sz);
2185  if (p->buf == NULL)
2186  return -1;
2187  p->bufsz = sz;
2188  p->bufptr = 0;
2189  p->bufcnt = 0;
2190  return 0;
2191  }
2192  else
2193  {
2194  if (p->bufcnt > 0)
2195  {
2196  memmove (p->buf, p->buf + p->bufptr, p->bufcnt);
2197  p->bufptr = 0;
2198  }
2199  b = g_realloc (p->buf, sz);
2200  if (b == NULL)
2201  return -1;
2202  p->buf = b;
2203  p->bufsz = sz;
2204  return 0;
2205  }
2206  /*NOTREACHED*/}
2207 
2208 
2209 
2210 /*------------------------------------------------------------------*/
2211 
2212 
2213 int
2214 os_send (int soc, void *buf, int len, int opt)
2215 {
2216  char *buf0 = (char *) buf;
2217  int e, n;
2218  for (n = 0; n < len;)
2219  {
2220  errno = 0;
2221  e = send (soc, buf0 + n, len - n, opt);
2222  if (e < 0 && errno == EINTR)
2223  continue;
2224  else if (e <= 0)
2225  return -1;
2226  else
2227  n += e;
2228  }
2229  return n;
2230 }
2231 
2232 int
2233 os_recv (int soc, void *buf, int len, int opt)
2234 {
2235  char *buf0 = (char *) buf;
2236  int e, n;
2237  for (n = 0; n < len;)
2238  {
2239  errno = 0;
2240  e = recv (soc, buf0 + n, len - n, opt);
2241  if (e < 0 && errno == EINTR)
2242  continue;
2243  else if (e <= 0)
2244  return -1;
2245  else
2246  n += e;
2247  }
2248  return n;
2249 }
2250 
2262 int
2263 internal_send (int soc, char *data, int msg_type)
2264 {
2265  int len;
2266  int e;
2267  int ack;
2268 
2269  if (data == NULL)
2270  data = "";
2271 
2272  e = os_send (soc, &msg_type, sizeof (msg_type), 0);
2273  if (e < 0)
2274  return -1;
2275 
2276  if ((msg_type & INTERNAL_COMM_MSG_TYPE_CTRL) == 0)
2277  {
2278  len = strlen (data);
2279 
2280  e = os_send (soc, &len, sizeof (len), 0);
2281  if (e < 0)
2282  return -1;
2283  e = os_send (soc, data, len, 0);
2284  if (e < 0)
2285  return -1;
2286  }
2287 
2288  e = os_recv (soc, &ack, sizeof (ack), 0);
2289  if (e < 0)
2290  {
2291  log_legacy_write ("internal_send->os_recv(%d): %s\n", soc,
2292  strerror (errno));
2293  return -1;
2294  }
2295 
2296  return 0;
2297 }
2298 
2317 int
2318 internal_recv (int soc, char **data, int *data_sz, int *msg_type)
2319 {
2320  int len = 0;
2321  int e;
2322  char *buf = NULL;
2323  int sz = 0;
2324  int type;
2325  int ack;
2326 
2327  if (*data != NULL)
2328  {
2329  log_legacy_write("%s doesn't support buffer pre-alloc anymore.", __func__);
2330  return -1;
2331  }
2332 
2333  e = os_recv (soc, &type, sizeof (type), 0);
2334  if (e < 0)
2335  goto error;
2336 
2337  if ((type & INTERNAL_COMM_MSG_TYPE_CTRL) == 0)
2338  {
2339  e = os_recv (soc, &len, sizeof (len), 0);
2340  if (e < 0 || len < 0)
2341  goto error;
2342 
2343  /* length == 0 is perfectly valid though */
2344  if (len > 0)
2345  {
2346  sz = len + 1;
2347  buf = g_malloc0 (sz);
2348 
2349  e = os_recv (soc, buf, len, 0);
2350  if (e < 0)
2351  goto error;
2352  }
2353  }
2354 
2355  *data = buf;
2356  *data_sz = sz;
2357  *msg_type = type;
2358 
2360  e = os_send (soc, &ack, sizeof (ack), 0);
2361  if (e < 0)
2362  goto error;
2363 
2364  return len;
2365 
2366 error:
2367  g_free (buf);
2368  *data = NULL;
2369  *data_sz = 0;
2370  return -1;
2371 }
2372 
2373 /* This is a helper function for nasl_get_sock_info. It is used to
2374  retrieve information about SOCK. */
2375 int
2376 get_sock_infos (int sock, int *r_transport, void **r_tls_session)
2377 {
2378  openvas_connection *fp;
2379 
2380  if (!OPENVAS_STREAM (sock))
2381  return ENOTSOCK;
2382  fp = &(connections[sock - OPENVAS_FD_OFF]);
2383 
2384  *r_transport = fp->transport;
2385  *r_tls_session = fp->tls_session;
2386  return 0;
2387 }
2388 
2389 /*
2390  * 0 is considered as the biggest number, since it
2391  * ends our string
2392  */
2393 static int
2394 qsort_compar (const void *a, const void *b)
2395 {
2396  u_short *aa = (u_short *) a;
2397  u_short *bb = (u_short *) b;
2398  if (*aa == 0)
2399  return (1);
2400  else if (*bb == 0)
2401  return (-1);
2402  else
2403  return (*aa - *bb);
2404 }
2405 
2415 unsigned short *
2416 getpts (char *origexpr, int *len)
2417 {
2418  int exlen;
2419  char *p, *q;
2420  unsigned short *tmp, *ports;
2421  int i = 0, j = 0, start, end;
2422  char *expr;
2423  char *mem;
2424  char *s_start, *s_end;
2425  static unsigned short *last_ret = NULL;
2426  static char *last_expr = NULL;
2427  static int last_num;
2428 
2429  expr = g_strdup (origexpr);
2430  exlen = strlen (origexpr);
2431  mem = expr;
2432 
2433  if (last_expr != NULL)
2434  {
2435  if (strcmp (last_expr, expr) == 0)
2436  {
2437  if (len != NULL)
2438  *len = last_num;
2439  g_free (mem);
2440  return last_ret;
2441  }
2442  else
2443  {
2444  g_free (last_expr);
2445  last_expr = NULL;
2446  g_free (&last_ret);
2447  last_ret = NULL;
2448  }
2449  }
2450 
2451  ports = g_malloc0 (65536 * sizeof (short));
2452  for (; j < exlen; j++)
2453  if (expr[j] != ' ')
2454  expr[i++] = expr[j];
2455  expr[i] = '\0';
2456 
2457  if ((s_start = strstr (expr, "T:")) != NULL)
2458  expr = &(s_start[2]);
2459 
2460  if ((s_end = strstr (expr, "U:")) != NULL)
2461  {
2462  if (s_end[-1] == ',')
2463  s_end--;
2464  s_end[0] = '\0';
2465  }
2466 
2467  i = 0;
2468  while ((p = strchr (expr, ',')))
2469  {
2470  *p = '\0';
2471  if (*expr == '-')
2472  {
2473  start = 1;
2474  end = atoi (expr + 1);
2475  }
2476  else
2477  {
2478  start = end = atoi (expr);
2479  if ((q = strchr (expr, '-')) && *(q + 1))
2480  end = atoi (q + 1);
2481  else if (q && !*(q + 1))
2482  end = 65535;
2483  }
2484  if (start < 1)
2485  start = 1;
2486  if (start > end)
2487  {
2488  g_free (mem);
2489  g_free (ports);
2490  return NULL;
2491  }
2492  for (j = start; j <= end; j++)
2493  ports[i++] = j;
2494  expr = p + 1;
2495  }
2496  if (*expr == '-')
2497  {
2498  start = 1;
2499  end = atoi (expr + 1);
2500  }
2501  else
2502  {
2503  start = end = atoi (expr);
2504  if ((q = strchr (expr, '-')) && *(q + 1))
2505  end = atoi (q + 1);
2506  else if (q && !*(q + 1))
2507  end = 65535;
2508  }
2509  if (start < 1)
2510  start = 1;
2511  if (start > end)
2512  {
2513  g_free (mem);
2514  g_free (ports);
2515  return NULL;
2516  }
2517  for (j = start; j <= end; j++)
2518  ports[i++] = j;
2519  ports[i++] = 0;
2520 
2521  qsort (ports, i, sizeof (u_short), qsort_compar);
2522  tmp = g_realloc (ports, i * sizeof (short));
2523  if (len != NULL)
2524  *len = i - 1;
2525  g_free (mem);
2526 
2527  last_ret = tmp;
2528  last_expr = g_strdup (origexpr);
2529  last_num = i - 1;
2530  return tmp;
2531 }
2532 
2543 struct host_info *
2544 host_info_init (const char *name, const struct in6_addr *ip,
2545  const char *vhosts, const char *fqdn)
2546 {
2547  struct host_info *hostinfo;
2548 
2549  hostinfo = g_malloc0 (sizeof (struct host_info));
2550  hostinfo->name = g_strdup (name);
2551  hostinfo->fqdn = g_strdup (fqdn);
2552  hostinfo->vhosts = g_strdup (vhosts);
2553  if (ip)
2554  {
2555  hostinfo->ip = g_malloc0 (sizeof (struct in6_addr));
2556  memcpy (hostinfo->ip, ip, sizeof (struct in6_addr));
2557  }
2558  return hostinfo;
2559 }
2560 
2566 void
2567 host_info_free (struct host_info *hostinfo)
2568 {
2569  if (!hostinfo)
2570  return;
2571  g_free (hostinfo->name);
2572  g_free (hostinfo->fqdn);
2573  g_free (hostinfo->vhosts);
2574  g_free (hostinfo->ip);
2575 }
int open_stream_connection_ext(struct arglist *args, unsigned int port, int transport, int timeout, const char *priority)
Definition: network.c:1004
Object to store a list of hooks for close_stream_connection.
Definition: network.c:107
int open_sock_option(struct arglist *args, unsigned int port, int type, int protocol, int timeout)
Definition: network.c:1960
#define err(x)
const char * get_encaps_name(openvas_encaps_t code)
Definition: network.c:1729
#define OPENVAS_FD_OFF
Definition: network.c:100
gnutls_session_t tls_session
Definition: network.c:83
int os_send(int soc, void *buf, int len, int opt)
Definition: network.c:2214
void host_info_free(struct host_info *hostinfo)
Free a host_info struct.
Definition: network.c:2567
#define OVAS_CONNECTION_FROM_FD(fd)
Definition: network.c:127
void unload_gnutls_file(gnutls_datum_t *data)
Unloads a gnutls_datum_t struct's data.
#define INCR_TIMEOUT
int open_stream_auto_encaps_ext(struct arglist *args, unsigned int port, int timeout, int force)
Definition: network.c:1127
int(* fnc)(int fd)
Definition: network.c:110
int openvas_deregister_connection(int fd)
Definition: network.c:292
int host_get_port_state(struct arglist *plugdata, int portnum)
Definition: plugutils.c:177
int openvas_get_socket_from_connection(int fd)
Definition: network.c:395
#define option
#define OPENVAS_CNX_IDS_EVASION_SHORT_TTL
Definition: ids_send.h:47
int open_stream_connection(struct arglist *args, unsigned int port, int transport, int timeout)
Definition: network.c:1117
enum openvas_encaps openvas_encaps_t
Protos and data structures for NVT Information data sets.
int ids_open_sock_tcp(struct arglist *args, int port, int method, int timeout)
Definition: ids_send.c:688
int stream_set(int fd, fd_set *set)
Definition: network.c:2137
#define INTERNAL_COMM_CTRL_ACK
Definition: internal_com.h:34
int openvas_register_connection(int soc, void *ssl, gnutls_certificate_credentials_t certcred, openvas_encaps_t encaps)
Definition: network.c:267
#define OPENVAS_CNX_IDS_EVASION_SEND_MASK
Definition: ids_send.h:50
int recv_line(int soc, char *buf, size_t bufsiz)
Reads a text from the socket stream into the argument buffer, always.
Definition: network.c:2017
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
int stream_set_buffer(int fd, int sz)
Definition: network.c:2163
int openvas_SSL_init()
Initializes SSL support.
Definition: network.c:376
const gchar * prefs_get(const gchar *key)
Get a string preference value via a key.
Definition: prefs.c:86
gnutls_session_t ovas_get_tlssession_from_connection(int fd)
Definition: network.c:418
int fd_is_stream(int fd)
Definition: network.c:2146
int nsend(int fd, void *data, int length, int i_opt)
Definition: network.c:1577
openvas_encaps_t transport
Definition: network.c:76
char * plug_get_host_ip_str(struct arglist *desc)
Definition: plugutils.c:225
int read_stream_connection(int fd, void *buf0, int len)
Definition: network.c:1421
#define OPENVAS_CNX_IDS_EVASION_INJECT
Definition: ids_send.h:46
void plug_set_port_transport(struct arglist *args, int port, int tr)
Definition: plugutils.c:976
char * vhosts
Definition: network.h:60
int open_sock_tcp(struct arglist *args, unsigned int port, int timeout)
Definition: network.c:1918
void arg_dump(struct arglist *args, int level)
Definition: arglists.c:280
gnutls_certificate_credentials_t tls_cred
Definition: network.c:84
void add_close_stream_connection_hook(int(*fnc)(int fd))
Register a hook function for close_stream_connection.
Definition: network.c:1660
kb_t plug_get_kb(struct arglist *args)
Definition: plugutils.c:710
int socket_get_ssl_version(int fd)
Definition: network.c:849
int socket_get_ssl_compression(int fd)
Definition: network.c:930
void socket_get_cert(int fd, void **cert, int *certlen)
Definition: network.c:813
unsigned short * getpts(char *origexpr, int *len)
Converts a string like "-100,200-1024,3000-4000,60000-" into an array.
Definition: network.c:2416
void log_legacy_fflush(void)
Legacy function to flush a log message.
#define OPENVAS_STREAM(x)
Definition: network.c:122
char * name
Definition: network.h:58
int read_stream_connection_min(int fd, void *buf0, int min_len, int max_len)
Definition: network.c:1360
int internal_recv(int soc, char **data, int *data_sz, int *msg_type)
Definition: network.c:2318
#define TIMEOUT
Definition: network.c:58
Top-level KB. This is to be inherited by KB implementations.
Definition: kb.h:102
struct in6_addr * ip
Definition: network.h:61
int ids_send(int fd, void *buf0, int n, int method)
Definition: ids_send.c:524
int stream_get_buffer_sz(int fd)
Definition: network.c:2153
GnuTLS based functions for communication with an OpenVAS server - header file.
struct in6_addr * plug_get_host_ip(struct arglist *desc)
Definition: plugutils.c:216
int nrecv(int fd, void *data, int length, int i_opt)
Definition: network.c:1618
const char * name
Definition: nasl_init.c:524
int socket_get_ssl_ciphersuite(int fd)
Definition: network.c:965
int stream_zero(fd_set *set)
Definition: network.c:2130
int stream_get_err(int fd)
Definition: network.c:161
#define INTERNAL_COMM_MSG_TYPE_CTRL
Definition: internal_com.h:30
int socket_negotiate_ssl(int fd, openvas_encaps_t transport, struct arglist *args)
Definition: network.c:771
struct timeval timeval(unsigned long val)
int write_stream_connection(int fd, void *buf0, int n)
Definition: network.c:1571
int openvas_source_set_socket(int socket, int port, int family)
Binds a socket to use the global source address.
int open_sock_opt_hn(const char *hostname, unsigned int port, int type, int protocol, int timeout)
Definition: network.c:1888
const char * get_encaps_through(openvas_encaps_t code)
Definition: network.c:1759
#define OPENVAS_CNX_IDS_EVASION_FAKE_RST
Definition: ids_send.h:48
int internal_send(int soc, char *data, int msg_type)
Definition: network.c:2263
int openvas_resolve_as_addr6(const char *name, struct in6_addr *ip6)
Resolves a hostname to an IPv4-mapped IPv6 or IPv6 address.
void socket_get_ssl_session_id(int fd, void **sid, size_t *ssize)
Definition: network.c:890
int get_sock_infos(int sock, int *r_transport, void **r_tls_session)
Definition: network.c:2376
char * priority
Definition: network.c:77
int os_recv(int soc, void *buf, int len, int opt)
Definition: network.c:2233
int socket_close(int soc)
Definition: network.c:2120
struct csc_hook_s * next
Definition: network.c:109
#define code
int plug_get_port_transport(struct arglist *args, int port)
Definition: plugutils.c:990
#define OPENVAS_CNX_IDS_EVASION_SPLIT
Definition: ids_send.h:45
char * fqdn
Definition: network.h:59
int stream_set_timeout(int fd, int timeout)
Definition: network.c:1158
struct host_info * host_info_init(const char *name, const struct in6_addr *ip, const char *vhosts, const char *fqdn)
Initializes a host_info.
Definition: network.c:2544
int load_gnutls_file(const char *file, gnutls_datum_t *loaded_file)
Loads a file's data into gnutls_datum_t struct.
#define OPENVAS_FD_MAX
Definition: network.c:99
void tlserror(char *txt, int err)
Definition: network.c:359
int close_stream_connection(int fd)
Definition: network.c:1699