36#include <gnutls/gnutls.h>
37#include <gvm/base/logging.h>
38#include <gvm/base/networking.h>
39#include <gvm/base/prefs.h>
41#include <netinet/in.h>
49#define EADDRNOTAVAIL EADDRINUSE
56#define G_LOG_DOMAIN "lib nasl"
63 int flags = fcntl (soc, F_GETFL, 0);
66 perror (
"fcntl(F_GETFL)");
69 if (fcntl (soc, F_SETFL, O_NONBLOCK | flags) < 0)
71 perror (
"fcntl(F_SETFL,O_NONBLOCK)");
80 int flags = fcntl (soc, F_GETFL, 0);
83 perror (
"fcntl(F_GETFL)");
86 if (fcntl (soc, F_SETFL, (~O_NONBLOCK) & flags) < 0)
88 perror (
"fcntl(F_SETFL,~O_NONBLOCK)");
97 const char *time_between_request;
100 time_between_request = prefs_get (
"time_between_request");
101 if (time_between_request)
102 minwaittime = atoi (time_between_request);
106 static double lastprobesec = 0;
107 static double lastprobeusec = 0;
112 gettimeofday (&tvnow, NULL);
113 if (lastprobesec <= 0)
115 lastprobesec = tvnow.tv_sec - 10;
116 lastprobeusec = tvnow.tv_usec;
119 tvdiff.tv_sec = tvnow.tv_sec - lastprobesec;
120 tvdiff.tv_usec = tvnow.tv_usec - lastprobeusec;
121 if (tvdiff.tv_usec <= 0)
124 tvdiff.tv_usec *= -1;
127 diff_msec = tvdiff.tv_sec * 1000 + tvdiff.tv_usec / 1000;
128 time2wait = (minwaittime - diff_msec) * 1000;
132 gettimeofday (&tvnow, NULL);
133 lastprobesec = tvnow.tv_sec;
134 lastprobeusec = tvnow.tv_usec;
160 int *key = g_memdup2 (&soc,
sizeof (
int));
163 data_record->
data = g_memdup2 ((gconstpointer)
data, (guint)
len);
165 if (udp_data == NULL)
168 g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
172 g_hash_table_replace (udp_data, (gpointer) key, (gpointer) data_record);
181 GHashTable *udp_data;
187 g_hash_table_new_full (g_int_hash, g_int_equal, g_free, g_free);
191 data_record = g_hash_table_lookup (udp_data, (gconstpointer) &soc);
197 return data_record->
data;
207 g_hash_table_remove (udp_data, (gconstpointer) &soc);
218 int sport, current_sport = -1;
222 struct sockaddr_in addr,
daddr;
223 struct sockaddr_in6 addr6, daddr6;
238 lexic,
"open_private_socket: missing or undefined parameter dport!\n");
243 current_sport = 1023;
246 if (proto == IPPROTO_TCP)
249 if (IN6_IS_ADDR_V4MAPPED (p))
252 bzero (&addr,
sizeof (addr));
253 if (proto == IPPROTO_TCP)
254 sock = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP);
256 sock = socket (AF_INET, SOCK_DGRAM, IPPROTO_UDP);
261 bzero (&addr6,
sizeof (addr6));
262 if (proto == IPPROTO_TCP)
263 sock = socket (AF_INET6, SOCK_STREAM, IPPROTO_TCP);
265 sock = socket (AF_INET6, SOCK_DGRAM, IPPROTO_UDP);
277 if (current_sport < 128 && sport < 0)
282 e = gvm_source_set_socket (sock, sport > 0 ? sport : current_sport--, family);
303 if (IN6_IS_ADDR_V4MAPPED (p))
306 daddr.sin_addr.s_addr = p->s6_addr32[3];
307 daddr.sin_family = AF_INET;
308 daddr.sin_port = htons (dport);
310 e = connect (sock, (
struct sockaddr *) &
daddr,
sizeof (
daddr));
314 bzero (&daddr6,
sizeof (daddr6));
315 memcpy (&daddr6.sin6_addr, p, sizeof (
struct in6_addr));
316 daddr6.sin6_family = AF_INET6;
317 daddr6.sin6_port = htons (dport);
319 e = connect (sock, (
struct sockaddr *) &daddr6,
sizeof (daddr6));
332 else if (errno != EINPROGRESS)
345 e = select (sock + 1, NULL, &rd, NULL, to > 0 ? &tv : NULL);
347 while (e < 0 && errno == EINTR);
356 opt_sz =
sizeof (opt);
358 if (getsockopt (sock, SOL_SOCKET, SO_ERROR, &opt, &opt_sz) < 0)
360 g_message (
"[%d] open_priv_sock()->getsockopt() failed : %s", getpid (),
386 if (proto == IPPROTO_TCP)
390 retc->
x.
i_val = sock < 0 ? 0 : sock;
415 const char *priority;
451 else if (transport == 0)
456 if (bufsz > 0 && soc >= 0)
459 nasl_perror (lexic,
"stream_set_buffer: soc=%d,bufsz=%d\n", soc, bufsz);
463 retc->
x.
i_val = soc < 0 ? 0 : soc;
526 struct sockaddr_in soca;
527 struct sockaddr_in6 soca6;
538 if (IN6_IS_ADDR_V4MAPPED (ia))
540 bzero (&soca,
sizeof (soca));
541 soca.sin_addr.s_addr = ia->s6_addr32[3];
542 soca.sin_port = htons (port);
543 soca.sin_family = AF_INET;
545 soc = socket (AF_INET, SOCK_DGRAM, 0);
548 gvm_source_set_socket (soc, 0, AF_INET);
549 if (connect (soc, (
struct sockaddr *) &soca,
sizeof (soca)) < 0)
557 bzero (&soca6,
sizeof (soca6));
558 memcpy (&soca6.sin6_addr, ia, sizeof (
struct in6_addr));
559 soca6.sin6_port = htons (port);
560 soca6.sin6_family = AF_INET6;
562 soc = socket (AF_INET6, SOCK_DGRAM, 0);
565 gvm_source_set_socket (soc, 0, AF_INET6);
566 if (connect (soc, (
struct sockaddr *) &soca6,
sizeof (soca6)) < 0)
584 int soc, transport, ret;
592 nasl_perror (lexic,
"socket_ssl_negotiate: Erroneous socket value %d\n",
601 "socket_ssl_negotiate: Erroneous transport value %d\n",
633 nasl_perror (lexic,
"socket_get_cert: Erroneous socket value %d\n", soc);
665 nasl_perror (lexic,
"socket_get_cert: Erroneous socket value %d\n", soc);
678 int soc, cert_len = 0;
685 nasl_perror (lexic,
"socket_get_cert: Erroneous socket value %d\n", soc);
693 retc->
size = cert_len;
708 nasl_perror (lexic,
"socket_get_cert: Erroneous socket value %d\n", soc);
712 if (sid == NULL || sid_len == 0)
716 retc->
size = sid_len;
756 char *data, *resend_data;
764 long int new_len = 0;
766 unsigned int opt_len =
sizeof (type);
769 if (
len <= 0 || soc <= 0)
775 data = g_malloc0 (
len);
777 e = getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &opt_len);
781 if (e == 0 && type == SOCK_DGRAM)
787 tv.tv_sec = to / retries;
788 tv.tv_usec = (to % retries) * 100000;
790 for (i = 0; i < retries; i++)
795 if (select (soc + 1, &rd, NULL, NULL, &tv) > 0)
798 alen = recv (soc, data + new_len,
len - new_len, 0);
818 if (resend_data != NULL)
819 send (soc, resend_data, rd_len, 0);
820 tv.tv_sec = to / retries;
821 tv.tv_usec = (to % retries) * 100000;
834 retc->
x.
str_val = g_memdup2 (data, new_len);
835 retc->
size = new_len;
853 long int new_len = 0;
858 if (
len == -1 || soc <= 0)
860 nasl_perror (lexic,
"recv_line: missing or undefined parameter"
861 " length or socket\n");
875 data = g_malloc0 (
len + 1);
883 if (timeout >= 0 && time (NULL) - t1 < timeout)
889 if ((data[n - 1] ==
'\n') || (n >=
len))
902 retc->
size = new_len;
903 retc->
x.
str_val = g_memdup2 (data, new_len + 1);
920 memcpy (ifr.ifr_name, device, sizeof (ifr.ifr_name));
921 if ((sd = socket (AF_INET, SOCK_DGRAM, 0)) < 0)
923 if (ioctl (sd, SIOCGIFMTU, &ifr) < 0)
937 if ((r =
get_mtu (dst) - 60 - 8) < 0)
950 "Unable to get MTU of used interface. get_mtu is not available.\n");
971 unsigned int type_len =
sizeof (type);
973 if (soc <= 0 || data == NULL)
975 nasl_perror (lexic,
"Syntax error with the send() function\n");
977 "Correct syntax is : send(socket:<soc>, data:<data>\n");
985 && getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0
986 && type == SOCK_DGRAM)
992 "data payload is larger (%d) than max udp payload (%d)\n",
1016 unsigned int opt_len =
sizeof (type);
1027 nasl_perror (lexic,
"close(%d): Invalid socket value\n", soc);
1031 e = getsockopt (soc, SOL_SOCKET, SO_TYPE, &type, &opt_len);
1034 if (type == SOCK_DGRAM)
1043 nasl_perror (lexic,
"close(%d): %s\n", soc, strerror (errno));
1067 nasl_perror (lexic,
"join_multicast_group: missing parameter\n");
1070 if (!inet_aton (a, &m.imr_multiaddr))
1072 nasl_perror (lexic,
"join_multicast_group: invalid parameter '%s'\n", a);
1075 m.imr_interface.s_addr = INADDR_ANY;
1079 if (
jmg_desc[i].in.s_addr == m.imr_multiaddr.s_addr
1090 int s = socket (AF_INET, SOCK_DGRAM, 0);
1093 nasl_perror (lexic,
"join_multicast_group: socket: %s\n",
1098 if (setsockopt (s, IPPROTO_IP, IP_ADD_MEMBERSHIP, &m,
sizeof (m)) < 0)
1101 lexic,
"join_multicast_group: setsockopt(IP_ADD_MEMBERSHIP): %s\n",
1132 nasl_perror (lexic,
"leave_multicast_group: missing parameter\n");
1135 if (!inet_aton (a, &ia))
1137 nasl_perror (lexic,
"leave_multicast_group: invalid parameter '%s'\n", a);
1149 nasl_perror (lexic,
"leave_multicast_group: never joined group %s\n", a);
1157 struct sockaddr_in ia;
1162 unsigned int type_len =
sizeof (type);
1167 nasl_perror (lexic,
"get_source_port: missing socket parameter\n");
1171 && getsockopt (s, SOL_SOCKET, SO_TYPE, &type, &type_len) == 0
1172 && type == SOCK_DGRAM)
1179 nasl_perror (lexic,
"get_source_port: invalid socket parameter %d\n", s);
1183 if (getsockname (fd, (
struct sockaddr *) &ia, &l) < 0)
1185 nasl_perror (lexic,
"get_source_port: getsockname(%d): %s\n", fd,
1190 retc->
x.
i_val = ntohs (ia.sin_port);
1227 g_message (
"socket_get_error: Erroneous socket value %d", soc);
1231 g_message (
"Unknown error %d %s", err, strerror (err));
1302 const char *keyword, *s;
1306 gnutls_session_t tls_session;
1313 nasl_perror (lexic,
"error: socket %d is not valid\n");
1322 nasl_perror (lexic,
"error: second argument is not of type string\n");
1340 nasl_perror (lexic,
"error retrieving infos for socket %d: %s\n", sock,
1344 else if (!strcmp (keyword,
"encaps"))
1351 else if (!strcmp (keyword,
"tls-proto"))
1357 gnutls_protocol_get_name (gnutls_protocol_get_version (tls_session));
1358 strval = g_strdup (s ? s :
"[?]");
1360 else if (!strcmp (keyword,
"tls-kx"))
1365 s = gnutls_kx_get_name (gnutls_kx_get (tls_session));
1366 strval = g_strdup (s ? s :
"");
1368 else if (!strcmp (keyword,
"tls-certtype"))
1373 s = gnutls_certificate_type_get_name (
1374 gnutls_certificate_type_get (tls_session));
1375 strval = g_strdup (s ? s :
"");
1377 else if (!strcmp (keyword,
"tls-cipher"))
1382 s = gnutls_cipher_get_name (gnutls_cipher_get (tls_session));
1383 strval = g_strdup (s ? s :
"");
1385 else if (!strcmp (keyword,
"tls-mac"))
1390 s = gnutls_mac_get_name (gnutls_mac_get (tls_session));
1391 strval = g_strdup (s ? s :
"");
1393 else if (!strcmp (keyword,
"tls-auth"))
1399 switch (gnutls_auth_get_type (tls_session))
1401 case GNUTLS_CRD_ANON:
1404 case GNUTLS_CRD_CERTIFICATE:
1407 case GNUTLS_CRD_PSK:
1410 case GNUTLS_CRD_SRP:
1418 strval = g_strdup (s);
1420 else if (!strcmp (keyword,
"tls-cert"))
1425 && gnutls_certificate_type_get (tls_session) == GNUTLS_CRT_X509)
1427 const gnutls_datum_t *
list;
1428 unsigned int nlist = 0;
1432 list = gnutls_certificate_get_peers (tls_session, &nlist);
1439 retc->
x.
ref_val = a = g_malloc0 (
sizeof *a);
1441 for (i = 0; i < nlist; i++)
1443 memset (&v, 0,
sizeof v);
1454 nasl_perror (lexic,
"unknown keyword '%s'\n", keyword);
1466 retc->
size = strlen (strval);
1501 gnutls_x509_crt_t *cert = NULL;
1502 gnutls_x509_trust_list_t ca_list;
1503 unsigned int ca_list_size = 0;
1504 unsigned int i, cert_n = 0;
1505 unsigned int voutput;
1506 const gnutls_datum_t *certs;
1509 gnutls_session_t tls_session;
1514 nasl_perror (lexic,
"socket_get_cert: Erroneous socket value %d\n", soc);
1525 nasl_perror (lexic,
"error retrieving tls_session for socket %d: %s\n",
1526 soc, strerror (err));
1533 && gnutls_certificate_type_get (tls_session) == GNUTLS_CRT_X509)
1535 certs = gnutls_certificate_get_peers (tls_session, &cert_n);
1542 cert = g_malloc0 (
sizeof (*cert) * cert_n);
1543 for (i = 0; i < cert_n; i++)
1545 if (gnutls_x509_crt_init (&cert[i]) != GNUTLS_E_SUCCESS)
1550 if (gnutls_x509_crt_import (cert[i], &certs[i], GNUTLS_X509_FMT_DER)
1551 != GNUTLS_E_SUCCESS)
1559 ret = gnutls_x509_trust_list_init (&ca_list, ca_list_size);
1565 ret = gnutls_x509_trust_list_add_system_trust (ca_list, 0, 0);
1573 if (gnutls_x509_trust_list_verify_crt (ca_list, cert, cert_n, 0, &voutput,
1575 != GNUTLS_E_SUCCESS)
#define NASL_ERR_ETIMEDOUT
#define NASL_ERR_ECONNRESET
#define NASL_ERR_EUNREACH
static struct timeval timeval(unsigned long val)
void nasl_perror(lex_ctxt *lexic, char *msg,...)
int get_var_type_by_num(lex_ctxt *, int)
Returns NASL variable/cell type, VAR2_UNDEF if value is NULL.
struct struct_lex_ctxt lex_ctxt
char * get_str_var_by_name(lex_ctxt *, const char *)
char * get_str_var_by_num(lex_ctxt *, int)
long int get_int_var_by_num(lex_ctxt *, int, int)
long int get_int_var_by_name(lex_ctxt *, const char *, int)
int get_var_type_by_name(lex_ctxt *, const char *)
long int get_var_size_by_name(lex_ctxt *, const char *)
static int get_udp_payload_size(struct in6_addr *dst)
tree_cell * nasl_leave_multicast_group(lex_ctxt *lexic)
tree_cell * nasl_open_sock_tcp_bufsz(lex_ctxt *lexic, int bufsz)
tree_cell * nasl_get_sock_info(lex_ctxt *lexic)
Get info pertaining to a socket.
tree_cell * nasl_recv_line(lex_ctxt *lexic)
static void wait_before_next_probe()
tree_cell * nasl_socket_cert_verify(lex_ctxt *lexic)
Verify a certificate.
static int get_mtu(struct in6_addr *dst)
static char * get_udp_data(struct script_infos *script_infos, int soc, int *len)
tree_cell * nasl_get_mtu(lex_ctxt *lexic)
tree_cell * nasl_open_priv_sock_udp(lex_ctxt *lexic)
static void rm_udp_data(struct script_infos *script_infos, int soc)
static struct jmg * jmg_desc
tree_cell * nasl_recv(lex_ctxt *lexic)
tree_cell * nasl_send(lex_ctxt *lexic)
tree_cell * nasl_join_multicast_group(lex_ctxt *lexic)
tree_cell * nasl_socket_get_error(lex_ctxt *lexic)
tree_cell * nasl_socket_ssl_do_handshake(lex_ctxt *lexic)
Do a re-handshake of the TLS/SSL protocol.
tree_cell * nasl_socket_check_ssl_safe_renegotiation(lex_ctxt *lexic)
Check if Secure Renegotiation is supported in the server side.
static int unblock_socket(int soc)
tree_cell * nasl_socket_get_ssl_version(lex_ctxt *lexic)
tree_cell * nasl_get_source_port(lex_ctxt *lexic)
tree_cell * nasl_open_sock_udp(lex_ctxt *lexic)
tree_cell * nasl_close_socket(lex_ctxt *lexic)
static tree_cell * nasl_open_privileged_socket(lex_ctxt *lexic, int proto)
static int block_socket(int soc)
tree_cell * nasl_open_sock_tcp(lex_ctxt *lexic)
Open a TCP socket to the target host.
tree_cell * nasl_socket_get_ssl_session_id(lex_ctxt *lexic)
tree_cell * nasl_socket_negotiate_ssl(lex_ctxt *lexic)
tree_cell * nasl_socket_get_cert(lex_ctxt *lexic)
static int add_udp_data(struct script_infos *script_infos, int soc, char *data, int len)
tree_cell * nasl_socket_get_ssl_ciphersuite(lex_ctxt *lexic)
tree_cell * nasl_open_priv_sock_tcp(lex_ctxt *lexic)
tree_cell * alloc_typed_cell(int typ)
int add_var_to_list(nasl_array *a, int i, const anon_nasl_var *v)
struct st_a_nasl_var anon_nasl_var
struct st_nasl_array nasl_array
int stream_get_err(int fd)
int openvas_get_socket_from_connection(int fd)
int stream_set_buffer(int fd, int sz)
void socket_get_ssl_session_id(int fd, void **sid, size_t *ssize)
int nsend(int fd, void *data, int length, int i_opt)
int socket_negotiate_ssl(int fd, openvas_encaps_t transport, struct script_infos *args)
Upgrade an ENCAPS_IP socket to an SSL/TLS encapsulated one.
int read_stream_connection_min(int fd, void *buf0, int min_len, int max_len)
int socket_ssl_do_handshake(int fd)
Do a re-handshake of the TLS/SSL protocol.
int open_stream_connection_ext(struct script_infos *args, unsigned int port, int transport, int timeout, const char *priority, int flags)
int openvas_register_connection(int soc, void *ssl, gnutls_certificate_credentials_t certcred, openvas_encaps_t encaps)
int socket_get_ssl_version(int fd)
int get_sock_infos(int sock, int *r_transport, void **r_tls_session)
int socket_get_ssl_ciphersuite(int fd)
void socket_get_cert(int fd, void **cert, int *certlen)
int socket_ssl_safe_renegotiation_status(int fd)
Check if Secure Renegotiation is supported in the server side.
const char * get_encaps_name(openvas_encaps_t code)
int stream_set_timeout(int fd, int timeout)
int open_stream_auto_encaps_ext(struct script_infos *args, unsigned int port, int timeout, int force)
int stream_get_buffer_sz(int fd)
int close_stream_connection(int fd)
Header file for module network.
@ OPENVAS_ENCAPS_TLScustom
#define NO_PRIORITY_FLAGS
char * v6_routethrough(struct in6_addr *dest, struct in6_addr *source)
An awesome function to determine what interface a packet to a given destination should be routed thro...
Header file for module pcap.
struct in6_addr * plug_get_host_ip(struct script_infos *args)
Header file for module plugutils.
union TC::@332262321161220155002104006201360276211317150140 x
union st_a_nasl_var::@154137074032032170165360023270032033276061363156 v
struct script_infos * script_infos
Support macros for special platforms.