OpenVAS Scanner  7.0.0~git
nasl_socket.h File Reference
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

tree_cellnasl_open_sock_tcp (lex_ctxt *)
 Open a TCP socket to the target host. More...
 
tree_cellnasl_open_sock_udp (lex_ctxt *)
 
tree_cellnasl_open_sock_tcp_bufsz (lex_ctxt *, int)
 
tree_cellnasl_socket_get_error (lex_ctxt *)
 
tree_cellnasl_open_priv_sock_tcp (lex_ctxt *)
 
tree_cellnasl_open_priv_sock_udp (lex_ctxt *)
 
tree_cellnasl_send (lex_ctxt *)
 
tree_cellnasl_socket_negotiate_ssl (lex_ctxt *)
 
tree_cellnasl_recv (lex_ctxt *)
 
tree_cellnasl_recv_line (lex_ctxt *)
 
tree_cellnasl_socket_get_cert (lex_ctxt *)
 
tree_cellnasl_socket_get_ssl_session_id (lex_ctxt *)
 
tree_cellnasl_socket_get_ssl_version (lex_ctxt *)
 
tree_cellnasl_socket_get_ssl_ciphersuite (lex_ctxt *)
 
tree_cellnasl_socket_cert_verify (lex_ctxt *)
 Verify a certificate. More...
 
tree_cellnasl_close_socket (lex_ctxt *)
 
tree_cellnasl_join_multicast_group (lex_ctxt *)
 
tree_cellnasl_leave_multicast_group (lex_ctxt *)
 
tree_cellnasl_get_source_port (lex_ctxt *)
 
tree_cellnasl_get_sock_info (lex_ctxt *lexic)
 Get info pertaining to a socket. More...
 

Function Documentation

◆ nasl_close_socket()

tree_cell* nasl_close_socket ( lex_ctxt )

Definition at line 905 of file nasl_socket.c.

906 {
907  int soc;
908  int type;
909  unsigned int opt_len = sizeof (type);
910  int e;
911 
912  soc = get_int_var_by_num (lexic, 0, -1);
913  if (fd_is_stream (soc))
914  {
916  return close_stream_connection (soc) < 0 ? NULL : FAKE_CELL;
917  }
918  if (lowest_socket == 0 || soc < lowest_socket)
919  {
920  nasl_perror (lexic, "close(%d): Invalid socket value\n", soc);
921  return NULL;
922  }
923 
924  e = getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &opt_len);
925  if (e == 0)
926  {
927  if (type == SOCK_DGRAM)
928  {
929  rm_udp_data (lexic->script_infos, soc);
930  return FAKE_CELL;
931  }
932  close (soc);
933  return FAKE_CELL;
934  }
935  else
936  nasl_perror (lexic, "close(%d): %s\n", soc, strerror (errno));
937 
938  return NULL;
939 }

References close_stream_connection(), FAKE_CELL, fd_is_stream(), get_int_var_by_num(), lowest_socket, nasl_perror(), rm_udp_data(), struct_lex_ctxt::script_infos, and wait_before_next_probe().

Referenced by http_close_socket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_get_sock_info()

tree_cell* nasl_get_sock_info ( lex_ctxt lexic)

Get info pertaining to a socket.

NASL Function: get_sock_info\n

This function is used to retrieve various information about an active socket. It requires the NASL socket number and a string to select the information to retrieve.

Supported keywords are:

  • dport Return the destination port. This is an integer. NOTE: Not yet implemented.
  • sport Return the source port. This is an integer. NOTE: Not yet implemented.
  • encaps Return the encapsulation of the socket. Example output: "TLScustom".
  • tls-proto Return a string with the actual TLS protocol in use. n/a" is returned if no SSL/TLS session is active. Example output: "TLSv1".
  • tls-kx Return a string describing the key exchange algorithm. Example output: "RSA".
  • tls-certtype Return the type of the certificate in use by the session. Example output: "X.509"
  • tls-cipher Return the cipher algorithm in use by the session; Example output: "AES-256-CBC".
  • tls-mac Return the message authentication algorithms used by the session. Example output: "SHA1".
  • tls-auth Return the peer's authentication type. Example output: "CERT".
  • tls-cert Return the peer's certificates for an SSL or TLS connection. This is an array of binary strings or NULL if no certificate is known.
NASL Unnamed Parameters:\n
  • A NASL socket
  • A string keyword; see above.
NASL Named Parameters:\n
  • asstring If true return a human readable string instead of an integer. Used only with these keywords: encaps.
NASL Returns:\n An integer or a string or NULL on error.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 1190 of file nasl_socket.c.

1191 {
1192  int sock;
1193  int type;
1194  int err;
1195  const char *keyword, *s;
1196  tree_cell *retc;
1197  int as_string;
1198  int transport;
1199  gnutls_session_t tls_session;
1200  char *strval;
1201  int intval;
1202 
1203  sock = get_int_var_by_num (lexic, 0, -1);
1204  if (sock <= 0)
1205  {
1206  nasl_perror (lexic, "error: socket %d is not valid\n");
1207  return NULL;
1208  }
1209 
1210  keyword = get_str_var_by_num (lexic, 1);
1211  if (!keyword
1212  || !((type = get_var_type_by_num (lexic, 1)) == VAR2_STRING
1213  || type == VAR2_DATA))
1214  {
1215  nasl_perror (lexic, "error: second argument is not of type string\n");
1216  return NULL;
1217  }
1218 
1219  as_string = !!get_int_var_by_name (lexic, "asstring", 0);
1220 
1221  transport = 0;
1222  strval = NULL;
1223  intval = 0;
1224  retc = FAKE_CELL; /* Dummy value to detect retc == NULL. */
1225 
1226  {
1227  void *tmp = NULL;
1228  err = get_sock_infos (sock, &transport, &tmp);
1229  tls_session = tmp;
1230  }
1231  if (err)
1232  {
1233  nasl_perror (lexic, "error retrieving infos for socket %d: %s\n", sock,
1234  strerror (err));
1235  retc = NULL;
1236  }
1237  else if (!strcmp (keyword, "encaps"))
1238  {
1239  if (as_string)
1240  strval = g_strdup (get_encaps_name (transport));
1241  else
1242  intval = transport;
1243  }
1244  else if (!strcmp (keyword, "tls-proto"))
1245  {
1246  if (!tls_session)
1247  s = "n/a";
1248  else
1249  s =
1250  gnutls_protocol_get_name (gnutls_protocol_get_version (tls_session));
1251  strval = g_strdup (s ? s : "[?]");
1252  }
1253  else if (!strcmp (keyword, "tls-kx"))
1254  {
1255  if (!tls_session)
1256  s = "n/a";
1257  else
1258  s = gnutls_kx_get_name (gnutls_kx_get (tls_session));
1259  strval = g_strdup (s ? s : "");
1260  }
1261  else if (!strcmp (keyword, "tls-certtype"))
1262  {
1263  if (!tls_session)
1264  s = "n/a";
1265  else
1266  s = gnutls_certificate_type_get_name (
1267  gnutls_certificate_type_get (tls_session));
1268  strval = g_strdup (s ? s : "");
1269  }
1270  else if (!strcmp (keyword, "tls-cipher"))
1271  {
1272  if (!tls_session)
1273  s = "n/a";
1274  else
1275  s = gnutls_cipher_get_name (gnutls_cipher_get (tls_session));
1276  strval = g_strdup (s ? s : "");
1277  }
1278  else if (!strcmp (keyword, "tls-mac"))
1279  {
1280  if (!tls_session)
1281  s = "n/a";
1282  else
1283  s = gnutls_mac_get_name (gnutls_mac_get (tls_session));
1284  strval = g_strdup (s ? s : "");
1285  }
1286  else if (!strcmp (keyword, "tls-auth"))
1287  {
1288  if (!tls_session)
1289  s = "n/a";
1290  else
1291  {
1292  switch (gnutls_auth_get_type (tls_session))
1293  {
1294  case GNUTLS_CRD_ANON:
1295  s = "ANON";
1296  break;
1297  case GNUTLS_CRD_CERTIFICATE:
1298  s = "CERT";
1299  break;
1300  case GNUTLS_CRD_PSK:
1301  s = "PSK";
1302  break;
1303  case GNUTLS_CRD_SRP:
1304  s = "SRP";
1305  break;
1306  default:
1307  s = "[?]";
1308  break;
1309  }
1310  }
1311  strval = g_strdup (s);
1312  }
1313  else if (!strcmp (keyword, "tls-cert"))
1314  {
1315  /* We only support X.509 for now. GNUTLS also allows for
1316  OpenPGP, but we are not prepared for that. */
1317  if (tls_session
1318  && gnutls_certificate_type_get (tls_session) == GNUTLS_CRT_X509)
1319  {
1320  const gnutls_datum_t *list;
1321  unsigned int nlist = 0;
1322  nasl_array *a;
1323  anon_nasl_var v;
1324 
1325  list = gnutls_certificate_get_peers (tls_session, &nlist);
1326  if (!list)
1327  retc = NULL; /* No certificate or other error. */
1328  else
1329  {
1330  unsigned int i;
1331  retc = alloc_typed_cell (DYN_ARRAY);
1332  retc->x.ref_val = a = g_malloc0 (sizeof *a);
1333 
1334  for (i = 0; i < nlist; i++)
1335  {
1336  memset (&v, 0, sizeof v);
1337  v.var_type = VAR2_DATA;
1338  v.v.v_str.s_val = list[i].data;
1339  v.v.v_str.s_siz = list[i].size;
1340  add_var_to_list (a, i, &v);
1341  }
1342  }
1343  }
1344  }
1345  else
1346  {
1347  nasl_perror (lexic, "unknown keyword '%s'\n", keyword);
1348  retc = NULL;
1349  }
1350 
1351  if (!retc)
1352  ;
1353  else if (retc != FAKE_CELL)
1354  ; /* Already allocated. */
1355  else if (strval)
1356  {
1357  retc = alloc_typed_cell (CONST_STR);
1358  retc->x.str_val = strval;
1359  retc->size = strlen (strval);
1360  }
1361  else
1362  {
1363  retc = alloc_typed_cell (CONST_INT);
1364  retc->x.i_val = intval;
1365  }
1366 
1367  return retc;
1368 }

References add_var_to_list(), alloc_typed_cell(), CONST_INT, CONST_STR, DYN_ARRAY, FAKE_CELL, get_encaps_name(), get_int_var_by_name(), get_int_var_by_num(), get_sock_infos(), get_str_var_by_num(), get_var_type_by_num(), TC::i_val, nasl_perror(), TC::ref_val, st_nasl_string::s_siz, st_nasl_string::s_val, TC::size, TC::str_val, st_a_nasl_var::v, st_a_nasl_var::v_str, VAR2_DATA, VAR2_STRING, st_a_nasl_var::var_type, and TC::x.

Here is the call graph for this function:

◆ nasl_get_source_port()

tree_cell* nasl_get_source_port ( lex_ctxt )

Definition at line 1048 of file nasl_socket.c.

1049 {
1050  struct sockaddr_in ia;
1051  int s, fd;
1052  unsigned int l;
1053  tree_cell *retc;
1054  int type;
1055  unsigned int type_len = sizeof (type);
1056 
1057  s = get_int_var_by_num (lexic, 0, -1);
1058  if (s < 0)
1059  {
1060  nasl_perror (lexic, "get_source_port: missing socket parameter\n");
1061  return NULL;
1062  }
1063  if (!fd_is_stream (s)
1064  && getsockopt (s, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0
1065  && type == SOCK_DGRAM)
1066  fd = s;
1067  else
1069 
1070  if (fd < 0)
1071  {
1072  nasl_perror (lexic, "get_source_port: invalid socket parameter %d\n", s);
1073  return NULL;
1074  }
1075  l = sizeof (ia);
1076  if (getsockname (fd, (struct sockaddr *) &ia, &l) < 0)
1077  {
1078  nasl_perror (lexic, "get_source_port: getsockname(%d): %s\n", fd,
1079  strerror (errno));
1080  return NULL;
1081  }
1082  retc = alloc_typed_cell (CONST_INT);
1083  retc->x.i_val = ntohs (ia.sin_port);
1084  return retc;
1085 }

References alloc_typed_cell(), CONST_INT, fd_is_stream(), get_int_var_by_num(), TC::i_val, nasl_perror(), openvas_get_socket_from_connection(), and TC::x.

Here is the call graph for this function:

◆ nasl_join_multicast_group()

tree_cell* nasl_join_multicast_group ( lex_ctxt )

Definition at line 950 of file nasl_socket.c.

951 {
952  char *a;
953  int i, j;
954  struct ip_mreq m;
955  tree_cell *retc = NULL;
956 
957  a = get_str_var_by_num (lexic, 0);
958  if (a == NULL)
959  {
960  nasl_perror (lexic, "join_multicast_group: missing parameter\n");
961  return NULL;
962  }
963  if (!inet_aton (a, &m.imr_multiaddr))
964  {
965  nasl_perror (lexic, "join_multicast_group: invalid parameter '%s'\n", a);
966  return NULL;
967  }
968  m.imr_interface.s_addr = INADDR_ANY;
969 
970  j = -1;
971  for (i = 0; i < jmg_max; i++)
972  if (jmg_desc[i].in.s_addr == m.imr_multiaddr.s_addr
973  && jmg_desc[i].count > 0)
974  {
975  jmg_desc[i].count++;
976  break;
977  }
978  else if (jmg_desc[i].count <= 0)
979  j = i;
980 
981  if (i >= jmg_max)
982  {
983  int s = socket (AF_INET, SOCK_DGRAM, 0);
984  if (s < 0)
985  {
986  nasl_perror (lexic, "join_multicast_group: socket: %s\n",
987  strerror (errno));
988  return NULL;
989  }
990 
991  if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m, sizeof (m)) < 0)
992  {
993  nasl_perror (
994  lexic, "join_multicast_group: setsockopt(IP_ADD_MEMBERSHIP): %s\n",
995  strerror (errno));
996  close (s);
997  return NULL;
998  }
999 
1000  if (j < 0)
1001  {
1002  jmg_desc = g_realloc (jmg_desc, sizeof (*jmg_desc) * (jmg_max + 1));
1003  j = jmg_max++;
1004  }
1005  jmg_desc[j].s = s;
1006  jmg_desc[j].in = m.imr_multiaddr;
1007  jmg_desc[j].count = 1;
1008  }
1009 
1010  retc = alloc_typed_cell (CONST_INT);
1011  retc->x.i_val = 1;
1012  return retc;
1013 }

References alloc_typed_cell(), CONST_INT, jmg::count, get_str_var_by_num(), jmg::in, jmg_desc, jmg_max, nasl_perror(), and jmg::s.

Here is the call graph for this function:

◆ nasl_leave_multicast_group()

tree_cell* nasl_leave_multicast_group ( lex_ctxt )

Definition at line 1016 of file nasl_socket.c.

1017 {
1018  char *a;
1019  struct in_addr ia;
1020  int i;
1021 
1022  a = get_str_var_by_num (lexic, 0);
1023  if (a == NULL)
1024  {
1025  nasl_perror (lexic, "leave_multicast_group: missing parameter\n");
1026  return NULL;
1027  }
1028  if (!inet_aton (a, &ia))
1029  {
1030  nasl_perror (lexic, "leave_multicast_group: invalid parameter '%s'\n", a);
1031  return NULL;
1032  }
1033 
1034  for (i = 0; i < jmg_max; i++)
1035  if (jmg_desc[i].count > 0 && jmg_desc[i].in.s_addr == ia.s_addr)
1036  {
1037  if (--jmg_desc[i].count <= 0)
1038  close (jmg_desc[i].s);
1039  return FAKE_CELL;
1040  }
1041 
1042  nasl_perror (lexic, "leave_multicast_group: never joined group %s\n", a);
1043  return NULL;
1044 }

References FAKE_CELL, get_str_var_by_num(), jmg_desc, jmg_max, and nasl_perror().

Here is the call graph for this function:

◆ nasl_open_priv_sock_tcp()

tree_cell* nasl_open_priv_sock_tcp ( lex_ctxt )

Definition at line 403 of file nasl_socket.c.

404 {
405  return nasl_open_privileged_socket (lexic, IPPROTO_TCP);
406 }

References nasl_open_privileged_socket().

Here is the call graph for this function:

◆ nasl_open_priv_sock_udp()

tree_cell* nasl_open_priv_sock_udp ( lex_ctxt )

Definition at line 409 of file nasl_socket.c.

410 {
411  return nasl_open_privileged_socket (lexic, IPPROTO_UDP);
412 }

References nasl_open_privileged_socket().

Here is the call graph for this function:

◆ nasl_open_sock_tcp()

tree_cell* nasl_open_sock_tcp ( lex_ctxt lexic)

Open a TCP socket to the target host.

NASL Function: open_sock_tcp\n

This function is used to create a TCP connection to the target host. It requires the port number as its argument and has various optional named arguments to control encapsulation, timeout and buffering.

NASL Unnamed Parameters:\n
  • A non-negative integer with the TCP port number.
NASL Named Parameters:\n
  • bufsz An integer with the the size buffer size. Note that by default, no buffering is used.
  • timeout An integer with the timeout value in seconds. The default timeout is controlled by a global value.
  • transport One of the ENCAPS_* constants to force a specific encapsulation mode or force trying of all modes (ENCAPS_AUTO). This is for example useful to select a specific TLS or SSL version or use specific TLS connection setup priorities. See get_port_transport for a description of the ENCAPS constants.
  • priority A string value with priorities for an TLS encapsulation. For the syntax of the priority string see the GNUTLS manual. This argument is only used in ENCAPS_TLScustom encapsulation.
NASL Returns:\n A positive integer as a NASL socket, 0 on connection error or
NULL on other errors.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 517 of file nasl_socket.c.

518 {
519  return nasl_open_sock_tcp_bufsz (lexic, -1);
520 }

References nasl_open_sock_tcp_bufsz().

Here is the call graph for this function:

◆ nasl_open_sock_tcp_bufsz()

tree_cell* nasl_open_sock_tcp_bufsz ( lex_ctxt ,
int   
)

Definition at line 417 of file nasl_socket.c.

418 {
419  int soc = -1;
420  struct script_infos *script_infos = lexic->script_infos;
421  int to, port;
422  int transport = -1;
423  const char *priority;
424  tree_cell *retc;
425 
426  to = get_int_var_by_name (lexic, "timeout", lexic->recv_timeout * 2);
427  if (to < 0)
428  to = 10;
429 
430  transport = get_int_var_by_name (lexic, "transport", -1);
431 
432  if (transport == OPENVAS_ENCAPS_TLScustom)
433  {
434  int type;
435  priority = get_str_var_by_name (lexic, "priority");
436  if (!priority)
437  priority = NULL;
438  type = get_var_type_by_name (lexic, "priority");
439  if (type != VAR2_STRING && type != VAR2_DATA)
440  priority = NULL;
441  }
442  else
443  priority = NULL;
444 
445  if (bufsz < 0)
446  bufsz = get_int_var_by_name (lexic, "bufsz", 0);
447 
448  port = get_int_var_by_num (lexic, 0, -1);
449  if (port < 0)
450  return NULL;
451 
453 
454  /* If "transport" has not been given, use auto detection if enabled
455  in the KB. if "transport" has been given with a value of 0 force
456  autodetection reagardless of what the KB tells. */
457  if (transport < 0)
458  soc = open_stream_auto_encaps_ext (script_infos, port, to, 0);
459  else if (transport == 0)
460  soc = open_stream_auto_encaps_ext (script_infos, port, to, 1);
461  else
462  soc =
463  open_stream_connection_ext (script_infos, port, transport, to, priority);
464  if (bufsz > 0 && soc >= 0)
465  {
466  if (stream_set_buffer (soc, bufsz) < 0)
467  nasl_perror (lexic, "stream_set_buffer: soc=%d,bufsz=%d\n", soc, bufsz);
468  }
469 
470  retc = alloc_typed_cell (CONST_INT);
471  retc->x.i_val = soc < 0 ? 0 : soc;
472 
473  return retc;
474 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), get_int_var_by_num(), get_str_var_by_name(), get_var_type_by_name(), TC::i_val, nasl_perror(), open_stream_auto_encaps_ext(), open_stream_connection_ext(), OPENVAS_ENCAPS_TLScustom, struct_lex_ctxt::recv_timeout, struct_lex_ctxt::script_infos, stream_set_buffer(), VAR2_DATA, VAR2_STRING, wait_before_next_probe(), and TC::x.

Referenced by http_open_socket(), and nasl_open_sock_tcp().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_open_sock_udp()

tree_cell* nasl_open_sock_udp ( lex_ctxt )

Definition at line 529 of file nasl_socket.c.

530 {
531  int soc;
532  tree_cell *retc;
533  int port;
534  struct sockaddr_in soca;
535  struct sockaddr_in6 soca6;
536  struct script_infos *script_infos = lexic->script_infos;
537  struct in6_addr *ia;
538 
539  port = get_int_var_by_num (lexic, 0, -1);
540  if (port < 0)
541  return NULL;
542 
544  if (ia == NULL)
545  return NULL;
546  if (IN6_IS_ADDR_V4MAPPED (ia))
547  {
548  bzero (&soca, sizeof (soca));
549  soca.sin_addr.s_addr = ia->s6_addr32[3];
550  soca.sin_port = htons (port);
551  soca.sin_family = AF_INET;
552 
553  soc = socket (AF_INET, SOCK_DGRAM, 0);
554  if (soc < 0)
555  return NULL;
556  gvm_source_set_socket (soc, 0, AF_INET);
557  if (connect (soc, (struct sockaddr *) &soca, sizeof (soca)) < 0)
558  {
559  close (soc);
560  return NULL;
561  }
562  }
563  else
564  {
565  bzero (&soca6, sizeof (soca6));
566  memcpy (&soca6.sin6_addr, ia, sizeof (struct in6_addr));
567  soca6.sin6_port = htons (port);
568  soca6.sin6_family = AF_INET6;
569 
570  soc = socket (AF_INET6, SOCK_DGRAM, 0);
571  if (soc < 0)
572  return NULL;
573  gvm_source_set_socket (soc, 0, AF_INET6);
574  if (connect (soc, (struct sockaddr *) &soca6, sizeof (soca6)) < 0)
575  {
576  close (soc);
577  return NULL;
578  }
579  }
580 
581  if (soc > 0 && lowest_socket == 0)
582  lowest_socket = soc;
583 
584  retc = alloc_typed_cell (CONST_INT);
585  retc->x.i_val = soc;
586  return retc;
587 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_num(), TC::i_val, lowest_socket, plug_get_host_ip(), struct_lex_ctxt::script_infos, and TC::x.

Here is the call graph for this function:

◆ nasl_recv()

tree_cell* nasl_recv ( lex_ctxt )

Definition at line 701 of file nasl_socket.c.

702 {
703  char *data;
704  int len = get_int_var_by_name (lexic, "length", -1);
705  int min_len = get_int_var_by_name (lexic, "min", -1);
706  int soc = get_int_var_by_name (lexic, "socket", 0);
707  int to = get_int_var_by_name (lexic, "timeout", lexic->recv_timeout);
708  fd_set rd;
709  struct timeval tv;
710  int new_len = 0;
711  int type = -1;
712  unsigned int opt_len = sizeof (type);
713  int e;
714 
715  if (len <= 0 || soc <= 0)
716  return NULL;
717 
718  tv.tv_sec = to;
719  tv.tv_usec = 0;
720 
721  data = g_malloc0 (len);
722  if (!fd_is_stream (soc))
723  e = getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &opt_len);
724  else
725  e = -1;
726 
727  if (e == 0 && type == SOCK_DGRAM)
728  {
729  /* As UDP packets may be lost, we retry up to 5 times */
730  int retries = 5;
731  int i;
732 
733  tv.tv_sec = to / retries;
734  tv.tv_usec = (to % retries) * 100000;
735 
736  for (i = 0; i < retries; i++)
737  {
738  FD_ZERO (&rd);
739  FD_SET (soc, &rd);
740 
741  if (select (soc + 1, &rd, NULL, NULL, &tv) > 0)
742  {
743  int e;
744  e = recv (soc, data + new_len, len - new_len, 0);
745 
746  if (e <= 0)
747  {
748  if (!new_len)
749  {
750  g_free (data);
751  return NULL;
752  }
753  }
754  else
755  new_len += e;
756 
757  break; /* UDP data is never fragmented */
758  }
759  else
760  {
761  /* The packet may have been lost en route - we resend it */
762  char *data;
763  int len;
764 
765  data = get_udp_data (lexic->script_infos, soc, &len);
766  if (data != NULL)
767  send (soc, data, len, 0);
768  tv.tv_sec = to / retries;
769  tv.tv_usec = (to % retries) * 100000;
770  }
771  }
772  }
773  else
774  {
775  int old = stream_set_timeout (soc, tv.tv_sec);
776  new_len = read_stream_connection_min (soc, data, min_len, len);
777  stream_set_timeout (soc, old);
778  }
779  if (new_len > 0)
780  {
782  retc->x.str_val = g_memdup (data, new_len);
783  retc->size = new_len;
784  g_free (data);
785  return retc;
786  }
787  else
788  {
789  g_free (data);
790  return NULL;
791  }
792 }

References alloc_typed_cell(), CONST_DATA, fd_is_stream(), get_int_var_by_name(), get_udp_data(), read_stream_connection_min(), struct_lex_ctxt::recv_timeout, struct_lex_ctxt::script_infos, TC::size, TC::str_val, stream_set_timeout(), timeval(), and TC::x.

Here is the call graph for this function:

◆ nasl_recv_line()

tree_cell* nasl_recv_line ( lex_ctxt )

Definition at line 795 of file nasl_socket.c.

796 {
797  int len = get_int_var_by_name (lexic, "length", -1);
798  int soc = get_int_var_by_name (lexic, "socket", 0);
799  int timeout = get_int_var_by_name (lexic, "timeout", -1);
800  char *data;
801  int new_len = 0;
802  int n = 0;
803  tree_cell *retc;
804  time_t t1 = 0;
805 
806  if (len == -1 || soc <= 0)
807  {
808  nasl_perror (lexic, "recv_line: missing or undefined parameter"
809  " length or socket\n");
810  return NULL;
811  }
812 
813  if (timeout >= 0) /* sycalls are much more expensive than simple tests */
814  t1 = time (NULL);
815 
816  if (fd_is_stream (soc) != 0)
817  {
818  int bufsz = stream_get_buffer_sz (soc);
819  if (bufsz <= 0)
820  stream_set_buffer (soc, len + 1);
821  }
822 
823  data = g_malloc0 (len + 1);
824  for (;;)
825  {
826  int e = read_stream_connection_min (soc, data + n, 1, 1);
827  if (e < 0)
828  break;
829  if (e == 0)
830  {
831  if (timeout >= 0 && time (NULL) - t1 < timeout)
832  continue;
833  else
834  break;
835  }
836  n++;
837  if ((data[n - 1] == '\n') || (n >= len))
838  break;
839  }
840 
841  if (n <= 0)
842  {
843  g_free (data);
844  return NULL;
845  }
846 
847  new_len = n;
848 
849  retc = alloc_typed_cell (CONST_DATA);
850  retc->size = new_len;
851  retc->x.str_val = g_memdup (data, new_len + 1);
852 
853  g_free (data);
854 
855  return retc;
856 }

References alloc_typed_cell(), CONST_DATA, fd_is_stream(), get_int_var_by_name(), nasl_perror(), read_stream_connection_min(), TC::size, TC::str_val, stream_get_buffer_sz(), stream_set_buffer(), and TC::x.

Here is the call graph for this function:

◆ nasl_send()

tree_cell* nasl_send ( lex_ctxt )

Definition at line 861 of file nasl_socket.c.

862 {
863  int soc = get_int_var_by_name (lexic, "socket", 0);
864  char *data = get_str_var_by_name (lexic, "data");
865  int option = get_int_var_by_name (lexic, "option", 0);
866  int length = get_int_var_by_name (lexic, "length", 0);
867  int data_length = get_var_size_by_name (lexic, "data");
868  int n;
869  tree_cell *retc;
870  int type;
871  unsigned int type_len = sizeof (type);
872 
873  if (soc <= 0 || data == NULL)
874  {
875  nasl_perror (lexic, "Syntax error with the send() function\n");
876  nasl_perror (lexic,
877  "Correct syntax is : send(socket:<soc>, data:<data>\n");
878  return NULL;
879  }
880 
881  if (length <= 0 || length > data_length)
882  length = data_length;
883 
884  if (!fd_is_stream (soc)
885  && getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0
886  && type == SOCK_DGRAM)
887  {
888  n = send (soc, data, length, option);
889  add_udp_data (lexic->script_infos, soc, data, length);
890  }
891  else
892  {
894  n = nsend (soc, data, length, option);
895  }
896 
897  retc = alloc_typed_cell (CONST_INT);
898  retc->x.i_val = n;
899 
900  return retc;
901 }

References add_udp_data(), alloc_typed_cell(), CONST_INT, fd_is_stream(), get_int_var_by_name(), get_str_var_by_name(), get_var_size_by_name(), TC::i_val, nasl_perror(), nsend(), option, struct_lex_ctxt::script_infos, wait_before_next_probe(), and TC::x.

Referenced by nasl_send_capture().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nasl_socket_cert_verify()

tree_cell* nasl_socket_cert_verify ( lex_ctxt lexic)

Verify a certificate.

NASL Function: socket_cert_verify\n

This function is used to retrieve and verify a certificate from an active socket. It requires the NASL socket number.

NASL Unnamed Parameters:\n
  • A NASL socket.
NASL Returns:\n 0 in case of successfully verification. A positive integer in
case of verification error or NULL on other errors.
Parameters
[in]lexicLexical context of the NASL interpreter.
Returns
A tree cell.

Definition at line 1389 of file nasl_socket.c.

1390 {
1391  int soc, err;
1392  int ret;
1393  tree_cell *retc;
1394  gnutls_x509_crt_t *cert = NULL;
1395  gnutls_x509_trust_list_t ca_list;
1396  unsigned int ca_list_size = 0;
1397  unsigned int i, cert_n = 0;
1398  unsigned int voutput;
1399  const gnutls_datum_t *certs;
1400 
1401  int transport;
1402  gnutls_session_t tls_session;
1403 
1404  soc = get_int_var_by_name (lexic, "socket", -1);
1405  if (soc < 0)
1406  {
1407  nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n", soc);
1408  return NULL;
1409  }
1410 
1411  {
1412  void *tmp = NULL;
1413  err = get_sock_infos (soc, &transport, &tmp);
1414  tls_session = tmp;
1415  }
1416  if (err)
1417  {
1418  nasl_perror (lexic, "error retrieving tls_session for socket %d: %s\n",
1419  soc, strerror (err));
1420  return NULL;
1421  }
1422 
1423  /* We only support X.509 for now. GNUTLS also allows for
1424  OpenPGP, but we are not prepared for that. */
1425  if (tls_session
1426  && gnutls_certificate_type_get (tls_session) == GNUTLS_CRT_X509)
1427  {
1428  certs = gnutls_certificate_get_peers (tls_session, &cert_n);
1429  if (!certs)
1430  return NULL; /* No certificate or other error. */
1431  }
1432  else
1433  return NULL;
1434 
1435  cert = g_malloc0 (sizeof (*cert) * cert_n);
1436  for (i = 0; i < cert_n; i++)
1437  {
1438  if (gnutls_x509_crt_init (&cert[i]) != GNUTLS_E_SUCCESS)
1439  return NULL;
1440  if (gnutls_x509_crt_import (cert[i], &certs[i], GNUTLS_X509_FMT_DER)
1441  != GNUTLS_E_SUCCESS)
1442  return NULL;
1443  }
1444 
1445  /* Init ca_list and load system CA trust list */
1446  if ((ret = gnutls_x509_trust_list_init (&ca_list, ca_list_size)) < 0)
1447  return NULL;
1448  ret = gnutls_x509_trust_list_add_system_trust (ca_list, 0, 0);
1449  if (ret < 0)
1450  return NULL;
1451 
1452  /* Certificate verification against a trust list*/
1453  if (gnutls_x509_trust_list_verify_crt (ca_list, cert, cert_n, 0, &voutput,
1454  NULL)
1455  != GNUTLS_E_SUCCESS)
1456  return NULL;
1457 
1458  ret = voutput;
1459 
1460  retc = alloc_typed_cell (CONST_INT);
1461  retc->x.i_val = ret;
1462  return retc;
1463 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), get_sock_infos(), TC::i_val, nasl_perror(), and TC::x.

Here is the call graph for this function:

◆ nasl_socket_get_cert()

tree_cell* nasl_socket_get_cert ( lex_ctxt )

Definition at line 623 of file nasl_socket.c.

624 {
625  int soc, cert_len = 0;
626  tree_cell *retc;
627  void *cert;
628 
629  soc = get_int_var_by_name (lexic, "socket", -1);
630  if (soc < 0)
631  {
632  nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n", soc);
633  return NULL;
634  }
635  socket_get_cert (soc, &cert, &cert_len);
636  if (cert_len <= 0)
637  return NULL;
638  retc = alloc_typed_cell (CONST_DATA);
639  retc->x.str_val = cert;
640  retc->size = cert_len;
641  return retc;
642 }

References alloc_typed_cell(), CONST_DATA, get_int_var_by_name(), nasl_perror(), TC::size, socket_get_cert(), TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_socket_get_error()

tree_cell* nasl_socket_get_error ( lex_ctxt )

Definition at line 1088 of file nasl_socket.c.

1089 {
1090  int soc = get_int_var_by_num (lexic, 0, -1);
1091  tree_cell *retc;
1092  int err;
1093 
1094  if (soc < 0 || !fd_is_stream (soc))
1095  return NULL;
1096 
1097  err = stream_get_err (soc);
1098  retc = alloc_typed_cell (CONST_INT);
1099 
1100  switch (err)
1101  {
1102  case 0:
1103  retc->x.i_val = NASL_ERR_NOERR;
1104  break;
1105  case ETIMEDOUT:
1106  retc->x.i_val = NASL_ERR_ETIMEDOUT;
1107  break;
1108  case EBADF:
1109  case EPIPE:
1110  case ECONNRESET:
1111  case ENOTSOCK:
1112  retc->x.i_val = NASL_ERR_ECONNRESET;
1113  break;
1114 
1115  case ENETUNREACH:
1116  case EHOSTUNREACH:
1117  retc->x.i_val = NASL_ERR_EUNREACH;
1118  break;
1119  case -1:
1120  g_message ("socket_get_error: Erroneous socket value %d", soc);
1121  break;
1122 
1123  default:
1124  g_message ("Unknown error %d %s", err, strerror (err));
1125  }
1126 
1127  return retc;
1128 }

References alloc_typed_cell(), CONST_INT, fd_is_stream(), get_int_var_by_num(), TC::i_val, NASL_ERR_ECONNRESET, NASL_ERR_ETIMEDOUT, NASL_ERR_EUNREACH, NASL_ERR_NOERR, stream_get_err(), and TC::x.

Here is the call graph for this function:

◆ nasl_socket_get_ssl_ciphersuite()

tree_cell* nasl_socket_get_ssl_ciphersuite ( lex_ctxt )

Definition at line 684 of file nasl_socket.c.

685 {
686  int soc, result;
687  tree_cell *retc;
688 
689  soc = get_int_var_by_name (lexic, "socket", -1);
690  result = socket_get_ssl_ciphersuite (soc);
691  if (result < 0)
692  return NULL;
693  retc = alloc_typed_cell (CONST_INT);
694  retc->x.i_val = result;
695  return retc;
696 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), TC::i_val, socket_get_ssl_ciphersuite(), and TC::x.

Here is the call graph for this function:

◆ nasl_socket_get_ssl_session_id()

tree_cell* nasl_socket_get_ssl_session_id ( lex_ctxt )

Definition at line 645 of file nasl_socket.c.

646 {
647  int soc;
648  size_t sid_len = 0;
649  tree_cell *retc;
650  void *sid;
651 
652  soc = get_int_var_by_name (lexic, "socket", -1);
653  if (soc < 0)
654  {
655  nasl_perror (lexic, "socket_get_cert: Erroneous socket value %d\n", soc);
656  return NULL;
657  }
658  socket_get_ssl_session_id (soc, &sid, &sid_len);
659  if (sid == NULL || sid_len == 0)
660  return NULL;
661  retc = alloc_typed_cell (CONST_DATA);
662  retc->x.str_val = sid;
663  retc->size = sid_len;
664  return retc;
665 }

References alloc_typed_cell(), CONST_DATA, get_int_var_by_name(), nasl_perror(), TC::size, socket_get_ssl_session_id(), TC::str_val, and TC::x.

Here is the call graph for this function:

◆ nasl_socket_get_ssl_version()

tree_cell* nasl_socket_get_ssl_version ( lex_ctxt )

Definition at line 668 of file nasl_socket.c.

669 {
670  int soc;
671  int version;
672  tree_cell *retc;
673 
674  soc = get_int_var_by_name (lexic, "socket", -1);
675  version = socket_get_ssl_version (soc);
676  if (version < 0)
677  return NULL;
678  retc = alloc_typed_cell (CONST_INT);
679  retc->x.i_val = version;
680  return retc;
681 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), TC::i_val, socket_get_ssl_version(), and TC::x.

Here is the call graph for this function:

◆ nasl_socket_negotiate_ssl()

tree_cell* nasl_socket_negotiate_ssl ( lex_ctxt )

Definition at line 590 of file nasl_socket.c.

591 {
592  int soc, transport, ret;
593  tree_cell *retc;
594 
595  soc = get_int_var_by_name (lexic, "socket", -1);
596  transport =
597  get_int_var_by_name (lexic, "transport", OPENVAS_ENCAPS_TLScustom);
598  if (soc < 0)
599  {
600  nasl_perror (lexic, "socket_ssl_negotiate: Erroneous socket value %d\n",
601  soc);
602  return NULL;
603  }
604  if (transport == -1)
605  transport = OPENVAS_ENCAPS_TLScustom;
606  else if (!IS_ENCAPS_SSL (transport))
607  {
608  nasl_perror (lexic,
609  "socket_ssl_negotiate: Erroneous transport value %d\n",
610  transport);
611  return NULL;
612  }
613  ret = socket_negotiate_ssl (soc, transport, lexic->script_infos);
614  if (ret < 0)
615  return NULL;
616 
617  retc = alloc_typed_cell (CONST_INT);
618  retc->x.i_val = ret;
619  return retc;
620 }

References alloc_typed_cell(), CONST_INT, get_int_var_by_name(), TC::i_val, IS_ENCAPS_SSL, nasl_perror(), OPENVAS_ENCAPS_TLScustom, struct_lex_ctxt::script_infos, socket_negotiate_ssl(), and TC::x.

Here is the call graph for this function:
st_a_nasl_var
Definition: nasl_var.h:50
script_infos
Definition: scanneraux.h:43
get_udp_data
static char * get_udp_data(struct script_infos *script_infos, int soc, int *len)
Definition: nasl_socket.c:187
CONST_DATA
@ CONST_DATA
Definition: nasl_tree.h:93
get_var_size_by_name
int get_var_size_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1147
plug_get_host_ip
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Definition: plugutils.c:285
TC::str_val
char * str_val
Definition: nasl_tree.h:112
CONST_STR
@ CONST_STR
Definition: nasl_tree.h:91
openvas_get_socket_from_connection
int openvas_get_socket_from_connection(int fd)
Definition: network.c:367
timeval
struct timeval timeval(unsigned long val)
Definition: nasl_builtin_synscan.c:105
DYN_ARRAY
@ DYN_ARRAY
Definition: nasl_tree.h:101
get_encaps_name
const char * get_encaps_name(openvas_encaps_t code)
Definition: network.c:1546
FAKE_CELL
#define FAKE_CELL
Definition: nasl_tree.h:119
TC::x
union TC::@2 x
get_str_var_by_name
char * get_str_var_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1127
st_a_nasl_var::v_str
nasl_string_t v_str
Definition: nasl_var.h:58
open_stream_auto_encaps_ext
int open_stream_auto_encaps_ext(struct script_infos *args, unsigned int port, int timeout, int force)
Definition: network.c:1002
socket_get_cert
void socket_get_cert(int fd, void **cert, int *certlen)
Definition: network.c:730
NASL_ERR_NOERR
#define NASL_ERR_NOERR
Definition: nasl.h:63
socket_get_ssl_version
int socket_get_ssl_version(int fd)
Definition: network.c:766
st_nasl_string::s_siz
int s_siz
Definition: nasl_var.h:38
NASL_ERR_EUNREACH
#define NASL_ERR_EUNREACH
Definition: nasl.h:66
st_nasl_array
Definition: nasl_var.h:43
OPENVAS_ENCAPS_TLScustom
@ OPENVAS_ENCAPS_TLScustom
Definition: network.h:52
stream_set_buffer
int stream_set_buffer(int fd, int sz)
Definition: network.c:1975
nasl_perror
void nasl_perror(lex_ctxt *lexic, char *msg,...)
Definition: nasl_debug.c:120
get_var_type_by_name
int get_var_type_by_name(lex_ctxt *, const char *)
Definition: nasl_var.c:1171
rm_udp_data
static void rm_udp_data(struct script_infos *script_infos, int soc)
Definition: nasl_socket.c:210
TC::size
int size
Definition: nasl_tree.h:109
stream_get_err
int stream_get_err(int fd)
Definition: network.c:145
nsend
int nsend(int fd, void *data, int length, int i_opt)
Definition: network.c:1402
option
#define option
add_udp_data
static int add_udp_data(struct script_infos *script_infos, int soc, char *data, int len)
Definition: nasl_socket.c:164
IS_ENCAPS_SSL
#define IS_ENCAPS_SSL(x)
Definition: network.h:56
jmg::s
int s
Definition: nasl_socket.c:945
NASL_ERR_ECONNRESET
#define NASL_ERR_ECONNRESET
Definition: nasl.h:65
get_int_var_by_name
long int get_int_var_by_name(lex_ctxt *, const char *, int)
Definition: nasl_var.c:1113
socket_get_ssl_ciphersuite
int socket_get_ssl_ciphersuite(int fd)
Definition: network.c:847
VAR2_DATA
@ VAR2_DATA
Definition: nasl_var.h:29
read_stream_connection_min
int read_stream_connection_min(int fd, void *buf0, int min_len, int max_len)
Definition: network.c:1212
nasl_open_sock_tcp_bufsz
tree_cell * nasl_open_sock_tcp_bufsz(lex_ctxt *lexic, int bufsz)
Definition: nasl_socket.c:417
fd_is_stream
int fd_is_stream(int fd)
Definition: network.c:1959
TC::ref_val
void * ref_val
Definition: nasl_tree.h:114
get_int_var_by_num
long int get_int_var_by_num(lex_ctxt *, int, int)
Definition: nasl_var.c:1106
get_str_var_by_num
char * get_str_var_by_num(lex_ctxt *, int)
Definition: nasl_var.c:1120
st_a_nasl_var::var_type
int var_type
Definition: nasl_var.h:52
TC
Definition: nasl_tree.h:104
stream_get_buffer_sz
int stream_get_buffer_sz(int fd)
Definition: network.c:1965
wait_before_next_probe
static void wait_before_next_probe()
Definition: nasl_socket.c:103
jmg::count
int count
Definition: nasl_socket.c:944
socket_get_ssl_session_id
void socket_get_ssl_session_id(int fd, void **sid, size_t *ssize)
Definition: network.c:807
socket_negotiate_ssl
int socket_negotiate_ssl(int fd, openvas_encaps_t transport, struct script_infos *args)
Definition: network.c:683
nasl_open_privileged_socket
static tree_cell * nasl_open_privileged_socket(lex_ctxt *lexic, int proto)
Definition: nasl_socket.c:223
CONST_INT
@ CONST_INT
Definition: nasl_tree.h:90
jmg::in
struct in_addr in
Definition: nasl_socket.c:943
jmg_desc
static struct jmg * jmg_desc
get_var_type_by_num
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
Definition: nasl_var.c:1164
add_var_to_list
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
Definition: nasl_var.c:1254
st_a_nasl_var::v
union st_a_nasl_var::@4 v
VAR2_STRING
@ VAR2_STRING
Definition: nasl_var.h:28
jmg_max
static int jmg_max
Definition: nasl_socket.c:947
close_stream_connection
int close_stream_connection(int fd)
Definition: network.c:1518
get_sock_infos
int get_sock_infos(int sock, int *r_transport, void **r_tls_session)
Definition: network.c:2063
alloc_typed_cell
tree_cell * alloc_typed_cell(int typ)
Definition: nasl_tree.c:40
NASL_ERR_ETIMEDOUT
#define NASL_ERR_ETIMEDOUT
Definition: nasl.h:64
lowest_socket
int lowest_socket
Definition: nasl_socket.c:220
list
Definition: nasl_builtin_synscan.c:259
open_stream_connection_ext
int open_stream_connection_ext(struct script_infos *args, unsigned int port, int transport, int timeout, const char *priority)
Definition: network.c:886
st_nasl_string::s_val
unsigned char * s_val
Definition: nasl_var.h:37
TC::i_val
long int i_val
Definition: nasl_tree.h:113
stream_set_timeout
int stream_set_timeout(int fd, int timeout)
Definition: network.c:1033