OpenVAS Libraries  9.0.3
openvas_server.c File Reference

GnuTLS based functions for communication with an OpenVAS server. More...

#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netdb.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <gcrypt.h>
#include <glib.h>
#include <string.h>
#include <stdio.h>
#include "openvas_server.h"
Include dependency graph for openvas_server.c:

Go to the source code of this file.

Macros

#define _GNU_SOURCE
 
#define G_LOG_DOMAIN   "lib serv"
 GLib log domain. More...
 

Functions

void openvas_connection_free (openvas_connection_t *client_connection)
 Free connection. More...
 
int openvas_server_verify (gnutls_session_t session)
 Verify certificate. More...
 
int load_gnutls_file (const char *file, gnutls_datum_t *loaded_file)
 Loads a file's data into gnutls_datum_t struct. More...
 
void unload_gnutls_file (gnutls_datum_t *data)
 Unloads a gnutls_datum_t struct's data. More...
 
int openvas_server_open_verify (gnutls_session_t *session, const char *host, int port, const char *ca_mem, const char *pub_mem, const char *priv_mem, int verify)
 Connect to the server using a given host, port and cert. More...
 
int openvas_server_open_with_cert (gnutls_session_t *session, const char *host, int port, const char *ca_mem, const char *pub_mem, const char *priv_mem)
 Connect to the server using a given host, port and cert. More...
 
int openvas_server_open (gnutls_session_t *session, const char *host, int port)
 Connect to the server using a given host and port. More...
 
int openvas_server_close (int socket, gnutls_session_t session)
 Close a server connection and its socket. More...
 
void openvas_connection_close (openvas_connection_t *connection)
 Close a server connection and its socket. More...
 
int openvas_server_connect (int server_socket, struct sockaddr_in *server_address, gnutls_session_t *server_session)
 Connect to a server. More...
 
int openvas_server_attach (int socket, gnutls_session_t *session)
 Attach a socket to a session, and shake hands with the peer. More...
 
int openvas_server_vsendf (gnutls_session_t *session, const char *fmt, va_list ap)
 Send a string to the server. More...
 
int openvas_connection_vsendf (openvas_connection_t *connection, const char *fmt, va_list ap)
 Send a string to the server. More...
 
int openvas_server_vsendf_quiet (gnutls_session_t *session, const char *fmt, va_list ap)
 Send a string to the server, refraining from logging besides warnings. More...
 
int openvas_connection_vsendf_quiet (openvas_connection_t *connection, const char *fmt, va_list ap)
 Send a string to the server, refraining from logging besides warnings. More...
 
int openvas_server_sendf (gnutls_session_t *session, const char *format,...)
 Format and send a string to the server. More...
 
int openvas_connection_sendf (openvas_connection_t *connection, const char *format,...)
 Format and send a string to the server. More...
 
int openvas_server_sendf_quiet (gnutls_session_t *session, const char *format,...)
 Format and send a string to the server. More...
 
int openvas_connection_sendf_quiet (openvas_connection_t *connection, const char *format,...)
 Format and send a string to the server. More...
 
int openvas_server_sendf_xml (gnutls_session_t *session, const char *format,...)
 Format and send an XML string to the server. More...
 
int openvas_connection_sendf_xml (openvas_connection_t *connection, const char *format,...)
 Format and send an XML string to the server. More...
 
int openvas_server_sendf_xml_quiet (gnutls_session_t *session, const char *format,...)
 Format and send an XML string to the server. More...
 
int openvas_connection_sendf_xml_quiet (openvas_connection_t *connection, const char *format,...)
 Format and send an XML string to the server. More...
 
int openvas_server_new (unsigned int end_type, gchar *ca_cert_file, gchar *cert_file, gchar *key_file, gnutls_session_t *server_session, gnutls_certificate_credentials_t *server_credentials)
 Make a session for connecting to a server. More...
 
int openvas_server_new_mem (unsigned int end_type, const char *ca_cert, const char *pub_key, const char *priv_key, gnutls_session_t *session, gnutls_certificate_credentials_t *credentials)
 Make a session for connecting to a server, with certificates stored in memory. More...
 
int set_gnutls_dhparams (gnutls_certificate_credentials_t creds, const char *dhparams_file)
 Set a gnutls session's Diffie-Hellman parameters. More...
 
int openvas_server_free (int server_socket, gnutls_session_t server_session, gnutls_certificate_credentials_t server_credentials)
 Cleanup a server session. More...
 

Variables

struct sockaddr_in address
 Server address. More...
 

Detailed Description

GnuTLS based functions for communication with an OpenVAS server.

GnuTLS based functions for communication with an OpenVAS server. Copyright (C) 2009, 2012 Greenbone Networks GmbH

Authors: Matthew Mundell matt@.nosp@m.mund.nosp@m.ell.u.nosp@m.kfsn.nosp@m..org Jan-Oliver Wagner jan-o.nosp@m.live.nosp@m.r.wag.nosp@m.ner@.nosp@m.green.nosp@m.bone.nosp@m..net Michael Wiegand micha.nosp@m.el.w.nosp@m.iegan.nosp@m.d@gr.nosp@m.eenbo.nosp@m.ne.n.nosp@m.et Werner Koch wk@gn.nosp@m.upg..nosp@m.org

This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. This library supplies low-level communication functions for communication with an OpenVAS server over GnuTLS.

Definition in file openvas_server.c.

Macro Definition Documentation

◆ _GNU_SOURCE

#define _GNU_SOURCE
Todo:
Ensure that every global init gets a free.

Definition at line 37 of file openvas_server.c.

◆ G_LOG_DOMAIN

#define G_LOG_DOMAIN   "lib serv"

GLib log domain.

Todo:
This module nearly fulfils the requirements to be placed in the base library (the gnutls dependency makes it a candidate for the net library).

Definition at line 75 of file openvas_server.c.

Function Documentation

◆ load_gnutls_file()

int load_gnutls_file ( const char *  file,
gnutls_datum_t *  loaded_file 
)

Loads a file's data into gnutls_datum_t struct.

Parameters
[in]fileFile to load.
[out]loaded_fileDestination to load file into.
Returns
0 if success, -1 if error.

Definition at line 211 of file openvas_server.c.

212 {
213  FILE *f = NULL;
214  int64_t filelen;
215  void *ptr;
216 
217  if (!(f = fopen (file, "r"))
218  || fseek (f, 0, SEEK_END) != 0
219  || (filelen = ftell (f)) < 0
220  || fseek (f, 0, SEEK_SET) != 0
221  || !(ptr = g_malloc0 ((size_t) filelen))
222  || fread (ptr, 1, (size_t) filelen, f) < (size_t) filelen)
223  {
224  if (f)
225  fclose (f);
226  return -1;
227  }
228 
229  loaded_file->data = ptr;
230  loaded_file->size = filelen;
231  fclose (f);
232  return 0;
233 }

Referenced by set_gnutls_dhparams().

Here is the caller graph for this function:

◆ openvas_connection_close()

void openvas_connection_close ( openvas_connection_t connection)

Close a server connection and its socket.

Parameters
[in]connectionConnection.
Returns
0 on success, -1 on error.

Definition at line 528 of file openvas_server.c.

529 {
530  openvas_connection_free (connection);
531 }
void openvas_connection_free(openvas_connection_t *client_connection)
Free connection.

References openvas_connection_free().

Here is the call graph for this function:

◆ openvas_connection_free()

void openvas_connection_free ( openvas_connection_t client_connection)

Free connection.

Parameters
[in]client_connectionConnection.

Definition at line 139 of file openvas_server.c.

140 {
141  if (client_connection->tls)
142  openvas_server_free (client_connection->socket,
143  client_connection->session,
144  client_connection->credentials);
145  else
146  close_unix (client_connection);
147 }
gnutls_session_t session
Session.
gnutls_certificate_credentials_t credentials
Credentials.
int tls
Whether uses TCP-TLS (vs UNIX socket).
int openvas_server_free(int server_socket, gnutls_session_t server_session, gnutls_certificate_credentials_t server_credentials)
Cleanup a server session.

References openvas_connection_t::credentials, openvas_server_free(), openvas_connection_t::session, openvas_connection_t::socket, and openvas_connection_t::tls.

Referenced by openvas_connection_close().

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

◆ openvas_connection_sendf()

int openvas_connection_sendf ( openvas_connection_t connection,
const char *  format,
  ... 
)

Format and send a string to the server.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 934 of file openvas_server.c.

937 {
938  va_list ap;
939  int rc;
940 
941  va_start (ap, format);
942  rc = openvas_connection_vsendf (connection, format, ap);
943  va_end (ap);
944  return rc;
945 }
int openvas_connection_vsendf(openvas_connection_t *connection, const char *fmt, va_list ap)
Send a string to the server.

References openvas_connection_vsendf().

Referenced by omp_ping_c(), omp_resume_task_report_c(), omp_start_task_report_c(), omp_stop_task_c(), and openvas_connection_sendf_xml().

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

◆ openvas_connection_sendf_quiet()

int openvas_connection_sendf_quiet ( openvas_connection_t connection,
const char *  format,
  ... 
)

Format and send a string to the server.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 976 of file openvas_server.c.

979 {
980  va_list ap;
981  int rc;
982 
983  va_start (ap, format);
984  rc = openvas_connection_vsendf_quiet (connection, format, ap);
985  va_end (ap);
986  return rc;
987 }
int openvas_connection_vsendf_quiet(openvas_connection_t *connection, const char *fmt, va_list ap)
Send a string to the server, refraining from logging besides warnings.

References openvas_connection_vsendf_quiet().

Referenced by openvas_connection_sendf_xml_quiet().

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

◆ openvas_connection_sendf_xml()

int openvas_connection_sendf_xml ( openvas_connection_t connection,
const char *  format,
  ... 
)

Format and send an XML string to the server.

Escape XML in string and character args.

Parameters
[in]connectionConnection.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 1025 of file openvas_server.c.

1027 {
1028  va_list ap;
1029  gchar *msg;
1030  int rc;
1031 
1032  va_start (ap, format);
1033  msg = g_markup_vprintf_escaped (format, ap);
1034  rc = openvas_connection_sendf (connection, "%s", msg);
1035  g_free (msg);
1036  va_end (ap);
1037  return rc;
1038 }
int openvas_connection_sendf(openvas_connection_t *connection, const char *format,...)
Format and send a string to the server.

References openvas_connection_sendf().

Here is the call graph for this function:

◆ openvas_connection_sendf_xml_quiet()

int openvas_connection_sendf_xml_quiet ( openvas_connection_t connection,
const char *  format,
  ... 
)

Format and send an XML string to the server.

Escape XML in string and character args.

Quiet version, only logs warnings.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 1081 of file openvas_server.c.

1084 {
1085  va_list ap;
1086  gchar *msg;
1087  int rc;
1088 
1089  va_start (ap, format);
1090  msg = g_markup_vprintf_escaped (format, ap);
1091  rc = openvas_connection_sendf_quiet (connection, "%s", msg);
1092  g_free (msg);
1093  va_end (ap);
1094  return rc;
1095 }
int openvas_connection_sendf_quiet(openvas_connection_t *connection, const char *format,...)
Format and send a string to the server.

References openvas_connection_sendf_quiet().

Referenced by omp_authenticate_info_ext_c().

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

◆ openvas_connection_vsendf()

int openvas_connection_vsendf ( openvas_connection_t connection,
const char *  fmt,
va_list  ap 
)

Send a string to the server.

Parameters
[in]connectionConnection.
[in]fmtFormat of string to send.
[in]apArgs for fmt.
Returns
0 on success, 1 if server closed connection, -1 on error.

Definition at line 866 of file openvas_server.c.

868 {
869  return openvas_connection_vsendf_internal (connection, fmt, ap, 0);
870 }

Referenced by openvas_connection_sendf().

Here is the caller graph for this function:

◆ openvas_connection_vsendf_quiet()

int openvas_connection_vsendf_quiet ( openvas_connection_t connection,
const char *  fmt,
va_list  ap 
)

Send a string to the server, refraining from logging besides warnings.

Parameters
[in]sessionPointer to GNUTLS session.
[in]fmtFormat of string to send.
[in]apArgs for fmt.
Returns
0 on success, 1 if server closed connection, -1 on error.

Definition at line 898 of file openvas_server.c.

901 {
902  return openvas_connection_vsendf_internal (connection, fmt, ap, 1);
903 }

Referenced by openvas_connection_sendf_quiet().

Here is the caller graph for this function:

◆ openvas_server_attach()

int openvas_server_attach ( int  socket,
gnutls_session_t *  session 
)

Attach a socket to a session, and shake hands with the peer.

Parameters
[in]socketSocket.
[in]sessionPointer to GNUTLS session. FIXME: Why is this a pointer to a session?
Returns
0 on success, -1 on error.

Definition at line 644 of file openvas_server.c.

645 {
646  int ret;
647 
648  ret = server_attach_internal (socket, session, NULL, 0);
649  return ret? -1 : 0;
650 }

Referenced by openvas_server_connect().

Here is the caller graph for this function:

◆ openvas_server_close()

int openvas_server_close ( int  socket,
gnutls_session_t  session 
)

Close a server connection and its socket.

Parameters
[in]socketSocket connected to server (from connect_to_server).
[in]sessionGNUTLS session with server.
Returns
0 on success, -1 on error.

Definition at line 515 of file openvas_server.c.

516 {
517  return openvas_server_free (socket, session, NULL);
518 }
int openvas_server_free(int server_socket, gnutls_session_t server_session, gnutls_certificate_credentials_t server_credentials)
Cleanup a server session.

References openvas_server_free().

Referenced by osp_connection_close().

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

◆ openvas_server_connect()

int openvas_server_connect ( int  server_socket,
struct sockaddr_in *  server_address,
gnutls_session_t *  server_session 
)

Connect to a server.

Parameters
[in]server_socketSocket to connect to server.
[in]server_addressServer address.
[in]server_sessionSession to connect to server.
Returns
0 on success, -1 on error.

Definition at line 543 of file openvas_server.c.

545 {
546  int ret;
547 
548  if (connect (server_socket, (struct sockaddr *) server_address,
549  sizeof (struct sockaddr_in)) == -1)
550  {
551  g_warning ("%s: failed to connect to server: %s\n", __FUNCTION__,
552  strerror (errno));
553  return -1;
554  }
555  g_debug (" Connected to server on socket %i.\n", server_socket);
556 
557  ret = openvas_server_attach (server_socket, server_session);
558  if (ret < 0)
559  return ret;
560  return openvas_server_verify (*server_session);
561 }
int openvas_server_attach(int socket, gnutls_session_t *session)
Attach a socket to a session, and shake hands with the peer.
int openvas_server_verify(gnutls_session_t session)
Verify certificate.

References openvas_server_attach(), and openvas_server_verify().

Here is the call graph for this function:

◆ openvas_server_free()

int openvas_server_free ( int  server_socket,
gnutls_session_t  server_session,
gnutls_certificate_credentials_t  server_credentials 
)

Cleanup a server session.

This shuts down the TLS session, closes the socket and releases the TLS resources.

Parameters
[in]server_socketThe socket connected to the server.
[in]server_sessionThe session with the server.
[in]server_credentialsCredentials or NULL.
Returns
0 success, -1 error.

Definition at line 1355 of file openvas_server.c.

1357 {
1358 #ifndef _WIN32
1359  struct sigaction new_action, original_action;
1360 #endif
1361 
1362 #if 0
1363  /* Turn on blocking. */
1364  // FIX get flags first
1365  if (fcntl (server_socket, F_SETFL, 0L) == -1)
1366  {
1367  g_warning ("%s: failed to set server socket flag: %s\n", __FUNCTION__,
1368  strerror (errno));
1369  return -1;
1370  }
1371 #endif
1372 #if 1
1373  /* Turn off blocking. */
1374  // FIX get flags first
1375 #ifndef _WIN32
1376  if (fcntl (server_socket, F_SETFL, O_NONBLOCK) == -1)
1377  {
1378  g_warning ("%s: failed to set server socket flag: %s\n", __FUNCTION__,
1379  strerror (errno));
1380  return -1;
1381  }
1382 #endif
1383 #endif
1384 
1385 #ifndef _WIN32
1386  new_action.sa_flags = 0;
1387  if (sigemptyset (&new_action.sa_mask))
1388  return -1;
1389  new_action.sa_handler = SIG_IGN;
1390  if (sigaction (SIGPIPE, &new_action, &original_action))
1391  return -1;
1392 #endif
1393 
1394  while (1)
1395  {
1396  int ret = gnutls_bye (server_session, GNUTLS_SHUT_WR);
1397  if (ret == GNUTLS_E_AGAIN || ret == GNUTLS_E_INTERRUPTED)
1398  {
1399  continue;
1400  }
1401  if (ret)
1402  {
1403  g_debug (" Failed to gnutls_bye: %s\n",
1404  gnutls_strerror ((int) ret));
1405  /* Carry on successfully anyway, as this often fails, perhaps
1406  * because the server is closing the connection first. */
1407  break;
1408  }
1409  break;
1410  }
1411 
1412  /* The former separate code in openvas_server_close and here
1413  differed in the order the TLS session and socket was closed. The
1414  way we do it here seems to be the right thing but for full
1415  backward compatibility we do it for calls from
1416  openvas_server_close in the old way. We can distinguish the two
1417  modes by the existence of server_credentials. */
1418  if (server_credentials)
1419  {
1420 #ifndef _WIN32
1421  if (sigaction (SIGPIPE, &original_action, NULL))
1422  return -1;
1423 
1424  if (shutdown (server_socket, SHUT_RDWR) == -1)
1425  {
1426  if (errno == ENOTCONN)
1427  return 0;
1428  g_warning ("%s: failed to shutdown server socket: %s\n", __FUNCTION__,
1429  strerror (errno));
1430  return -1;
1431  }
1432 #endif
1433 
1434  if (close (server_socket) == -1)
1435  {
1436  g_warning ("%s: failed to close server socket: %s\n", __FUNCTION__,
1437  strerror (errno));
1438  return -1;
1439  }
1440  gnutls_deinit (server_session);
1441  gnutls_certificate_free_credentials (server_credentials);
1442  }
1443  else
1444  {
1445  gnutls_deinit (server_session);
1446 #ifndef _WIN32
1447  if (sigaction (SIGPIPE, &original_action, NULL))
1448  return -1;
1449 #endif
1450  close (server_socket);
1451  }
1452 
1453  gnutls_global_deinit ();
1454 
1455  return 0;
1456 }

Referenced by openvas_connection_free(), and openvas_server_close().

Here is the caller graph for this function:

◆ openvas_server_new()

int openvas_server_new ( unsigned int  end_type,
gchar *  ca_cert_file,
gchar *  cert_file,
gchar *  key_file,
gnutls_session_t *  server_session,
gnutls_certificate_credentials_t *  server_credentials 
)

Make a session for connecting to a server.

Parameters
[in]end_typeConnection end type (GNUTLS_SERVER or GNUTLS_CLIENT).
[in]ca_cert_fileCertificate authority file.
[in]cert_fileCertificate file.
[in]key_fileKey file.
[out]server_sessionThe session with the server.
[out]server_credentialsServer credentials.
Returns
0 on success, -1 on error.

Definition at line 1234 of file openvas_server.c.

1239 {
1240  return server_new_internal (end_type, NULL,
1241  ca_cert_file, cert_file, key_file,
1242  server_session, server_credentials);
1243 }

◆ openvas_server_new_mem()

int openvas_server_new_mem ( unsigned int  end_type,
const char *  ca_cert,
const char *  pub_key,
const char *  priv_key,
gnutls_session_t *  session,
gnutls_certificate_credentials_t *  credentials 
)

Make a session for connecting to a server, with certificates stored in memory.

Parameters
[in]end_typeConnecton end type: GNUTLS_SERVER or GNUTLS_CLIENT.
[in]ca_certCertificate authority public key.
[in]pub_keyPublic key.
[in]priv_keyPrivate key.
[out]sessionThe session with the server.
[out]credentialsServer credentials.
Returns
0 on success, -1 on error.

Definition at line 1259 of file openvas_server.c.

1263 {
1264  if (server_new_gnutls_init (credentials))
1265  return -1;
1266 
1267  if (pub_key && priv_key)
1268  {
1269  int ret;
1270  gnutls_datum_t pub, priv;
1271 
1272  pub.data = (void *) pub_key;
1273  pub.size = strlen (pub_key);
1274  priv.data = (void *) priv_key;
1275  priv.size = strlen (priv_key);
1276 
1277  ret = gnutls_certificate_set_x509_key_mem (*credentials, &pub, &priv,
1278  GNUTLS_X509_FMT_PEM);
1279  if (ret < 0)
1280  {
1281  g_warning ("%s: %s\n", __FUNCTION__, gnutls_strerror (ret));
1282  return -1;
1283  }
1284  }
1285 
1286  if (ca_cert)
1287  {
1288  int ret;
1289  gnutls_datum_t data;
1290 
1291  data.data = (void *) ca_cert;
1292  data.size = strlen (ca_cert);
1293  ret = gnutls_certificate_set_x509_trust_mem (*credentials, &data,
1294  GNUTLS_X509_FMT_PEM);
1295  if (ret < 0)
1296  {
1297  g_warning ("%s: %s\n", __FUNCTION__, gnutls_strerror (ret));
1298  gnutls_certificate_free_credentials (*credentials);
1299  return -1;
1300  }
1301  }
1302 
1303  if (server_new_gnutls_set (end_type, NULL, session, credentials))
1304  {
1305  gnutls_certificate_free_credentials (*credentials);
1306  return -1;
1307  }
1308 
1309  return 0;
1310 }

Referenced by openvas_server_open_verify().

Here is the caller graph for this function:

◆ openvas_server_open()

int openvas_server_open ( gnutls_session_t *  session,
const char *  host,
int  port 
)

Connect to the server using a given host and port.

Parameters
[in]sessionPointer to GNUTLS session.
[in]hostHost to connect to.
[in]portPort to connect to.
Returns
0 on success, -1 on error.

Definition at line 501 of file openvas_server.c.

502 {
503  return openvas_server_open_with_cert (session, host, port, NULL, NULL, NULL);
504 }
int openvas_server_open_with_cert(gnutls_session_t *session, const char *host, int port, const char *ca_mem, const char *pub_mem, const char *priv_mem)
Connect to the server using a given host, port and cert.

References openvas_server_open_with_cert().

Here is the call graph for this function:

◆ openvas_server_open_verify()

int openvas_server_open_verify ( gnutls_session_t *  session,
const char *  host,
int  port,
const char *  ca_mem,
const char *  pub_mem,
const char *  priv_mem,
int  verify 
)

Connect to the server using a given host, port and cert.

Parameters
[in]sessionPointer to GNUTLS session.
[in]hostHost to connect to.
[in]portPort to connect to.
[in]ca_memCA cert.
[in]pub_memPublic key.
[in]priv_memPrivate key.
[in]verifyWhether to verify.
Returns
0 on success, -1 on error.
Todo:
Ensure that host and port have sane values.
Todo:
Improve logging.
Todo:
On success we are leaking the credentials. We can't free them because the session only makes a shallow copy. A solution would be to lookup already created credentials and reuse them.
Todo:
Use openvas_server_connect.

Definition at line 331 of file openvas_server.c.

335 {
336  int ret;
337  int server_socket;
338  struct addrinfo address_hints;
339  struct addrinfo *addresses, *address;
340  gchar *port_string;
341 #ifdef _WIN32
342  WSADATA wsaData;
343 #endif
344 
345  gnutls_certificate_credentials_t credentials;
346 
354  if (openvas_server_new_mem (GNUTLS_CLIENT, ca_mem, pub_mem, priv_mem, session,
355  &credentials))
356  {
357  g_warning ("Failed to create client TLS session.");
358  return -1;
359  }
360 
361  if (ca_mem && pub_mem && priv_mem)
362  {
363  set_cert_pub_mem (pub_mem);
364  set_cert_priv_mem (priv_mem);
365 
366  gnutls_certificate_set_retrieve_function (credentials,
367  client_cert_callback);
368  }
369 
370  /* Create the port string. */
371 
372  port_string = g_strdup_printf ("%i", port);
373 
374  /* WSA Start for win32 */
375 #ifdef _WIN32
376  if (WSAStartup (MAKEWORD (2, 2), &wsaData))
377  {
378  g_warning ("WSAStartup failed");
379  gnutls_deinit (*session);
380  gnutls_certificate_free_credentials (credentials);
381  g_free (port_string);
382  return -1;
383  }
384 #endif
385 
386  /* Get all possible addresses. */
387 
388  memset (&address_hints, 0, sizeof (address_hints));
389  address_hints.ai_family = AF_UNSPEC; /* IPv4 or IPv6. */
390  address_hints.ai_socktype = SOCK_STREAM;
391 #ifndef _WIN32
392  address_hints.ai_flags = AI_NUMERICSERV;
393 #endif
394  address_hints.ai_protocol = 0;
395 
396  if (getaddrinfo (host, port_string, &address_hints, &addresses))
397  {
398  g_free (port_string);
399  g_warning ("Failed to get server addresses for %s: %s", host,
400  gai_strerror (errno));
401  gnutls_deinit (*session);
402  gnutls_certificate_free_credentials (credentials);
403  return -1;
404  }
405  g_free (port_string);
406 
407  /* Try to connect to each address in turn. */
408 
409  for (address = addresses; address; address = address->ai_next)
410  {
411  /* Make server socket. */
412 
413  if (address->ai_family == AF_INET6)
414  server_socket = socket (PF_INET6, SOCK_STREAM, 0);
415  else
416  server_socket = socket (PF_INET, SOCK_STREAM, 0);
417  if (server_socket == -1)
418  {
419  g_warning ("Failed to create server socket");
420  freeaddrinfo (addresses);
421  gnutls_deinit (*session);
422  gnutls_certificate_free_credentials (credentials);
423  return -1;
424  }
425 
428  /* Connect to server. */
429 
430  if (connect (server_socket, address->ai_addr, address->ai_addrlen) == -1)
431  {
432  close (server_socket);
433  continue;
434  }
435  break;
436  }
437 
438  freeaddrinfo (addresses);
439 
440  if (address == NULL)
441  {
442  g_warning ("Failed to connect to server");
443  gnutls_deinit (*session);
444  gnutls_certificate_free_credentials (credentials);
445  return -1;
446  }
447 
448  g_debug (" Connected to server '%s' port %d.", host, port);
449 
450  /* Complete setup of server session. */
451  ret = server_attach_internal (server_socket, session, host, port);
452  if (ret)
453  {
454  if (ret == -2)
455  {
456  close (server_socket);
457  gnutls_deinit (*session);
458  gnutls_certificate_free_credentials (credentials);
459  }
460  return -1;
461  }
462  if (verify && openvas_server_verify (*session))
463  return -1;
464 
465  return server_socket;
466 }
int openvas_server_new_mem(unsigned int end_type, const char *ca_cert, const char *pub_key, const char *priv_key, gnutls_session_t *session, gnutls_certificate_credentials_t *credentials)
Make a session for connecting to a server, with certificates stored in memory.
struct sockaddr_in address
Server address.
int openvas_server_verify(gnutls_session_t session)
Verify certificate.

References address, and openvas_server_new_mem().

Referenced by openvas_server_open_with_cert().

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

◆ openvas_server_open_with_cert()

int openvas_server_open_with_cert ( gnutls_session_t *  session,
const char *  host,
int  port,
const char *  ca_mem,
const char *  pub_mem,
const char *  priv_mem 
)

Connect to the server using a given host, port and cert.

Verify if all cert args are given.

Parameters
[in]sessionPointer to GNUTLS session.
[in]hostHost to connect to.
[in]portPort to connect to.
[in]ca_memCA cert.
[in]pub_memPublic key.
[in]priv_memPrivate key.
Returns
0 on success, -1 on error.

Definition at line 483 of file openvas_server.c.

486 {
487  return openvas_server_open_verify (session, host, port, ca_mem, pub_mem,
488  priv_mem, ca_mem && pub_mem && priv_mem);
489 }
int openvas_server_open_verify(gnutls_session_t *session, const char *host, int port, const char *ca_mem, const char *pub_mem, const char *priv_mem, int verify)
Connect to the server using a given host, port and cert.

References openvas_server_open_verify().

Referenced by openvas_server_open().

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

◆ openvas_server_sendf()

int openvas_server_sendf ( gnutls_session_t *  session,
const char *  format,
  ... 
)

Format and send a string to the server.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 914 of file openvas_server.c.

915 {
916  va_list ap;
917  int rc;
918 
919  va_start (ap, format);
920  rc = openvas_server_vsendf (session, format, ap);
921  va_end (ap);
922  return rc;
923 }
int openvas_server_vsendf(gnutls_session_t *session, const char *fmt, va_list ap)
Send a string to the server.

References openvas_server_vsendf().

Referenced by omp_create_lsc_credential_ext(), omp_create_target_ext(), omp_create_task_ext(), omp_delete_config_ext(), omp_delete_lsc_credential_ext(), omp_delete_port_list_ext(), omp_delete_report(), omp_delete_target_ext(), omp_delete_task(), omp_delete_task_ext(), omp_get_report_ext(), omp_get_system_reports(), omp_get_system_reports_ext(), omp_get_targets(), omp_get_task_ext(), omp_get_tasks(), omp_get_tasks_ext(), omp_modify_task_file(), omp_ping(), omp_resume_task_report(), omp_start_task_report(), omp_stop_task(), and openvas_server_sendf_xml().

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

◆ openvas_server_sendf_quiet()

int openvas_server_sendf_quiet ( gnutls_session_t *  session,
const char *  format,
  ... 
)

Format and send a string to the server.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 956 of file openvas_server.c.

957 {
958  va_list ap;
959  int rc;
960 
961  va_start (ap, format);
962  rc = openvas_server_vsendf_quiet (session, format, ap);
963  va_end (ap);
964  return rc;
965 }
int openvas_server_vsendf_quiet(gnutls_session_t *session, const char *fmt, va_list ap)
Send a string to the server, refraining from logging besides warnings.

References openvas_server_vsendf_quiet().

Referenced by openvas_server_sendf_xml_quiet().

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

◆ openvas_server_sendf_xml()

int openvas_server_sendf_xml ( gnutls_session_t *  session,
const char *  format,
  ... 
)

Format and send an XML string to the server.

Escape XML in string and character args.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 1000 of file openvas_server.c.

1001 {
1002  va_list ap;
1003  gchar *msg;
1004  int rc;
1005 
1006  va_start (ap, format);
1007  msg = g_markup_vprintf_escaped (format, ap);
1008  rc = openvas_server_sendf (session, "%s", msg);
1009  g_free (msg);
1010  va_end (ap);
1011  return rc;
1012 }
int openvas_server_sendf(gnutls_session_t *session, const char *format,...)
Format and send a string to the server.

References openvas_server_sendf().

Referenced by omp_create_lsc_credential(), omp_create_lsc_credential_key(), and omp_create_task().

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

◆ openvas_server_sendf_xml_quiet()

int openvas_server_sendf_xml_quiet ( gnutls_session_t *  session,
const char *  format,
  ... 
)

Format and send an XML string to the server.

Escape XML in string and character args.

Quiet version, only logs warnings.

Parameters
[in]sessionPointer to GNUTLS session.
[in]formatprintf-style format string for message.
Returns
0 on success, -1 on error.

Definition at line 1053 of file openvas_server.c.

1055 {
1056  va_list ap;
1057  gchar *msg;
1058  int rc;
1059 
1060  va_start (ap, format);
1061  msg = g_markup_vprintf_escaped (format, ap);
1062  rc = openvas_server_sendf_quiet (session, "%s", msg);
1063  g_free (msg);
1064  va_end (ap);
1065  return rc;
1066 }
int openvas_server_sendf_quiet(gnutls_session_t *session, const char *format,...)
Format and send a string to the server.

References openvas_server_sendf_quiet().

Referenced by omp_authenticate(), omp_authenticate_info_ext(), and omp_create_lsc_credential().

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

◆ openvas_server_verify()

int openvas_server_verify ( gnutls_session_t  session)

Verify certificate.

Parameters
[in]sessionPointer to GNUTLS session.
Returns
0 on success, 1 on failure, -1 on error.

Definition at line 160 of file openvas_server.c.

161 {
162  unsigned int status;
163  int ret;
164 
165  ret = gnutls_certificate_verify_peers2 (session, &status);
166  if (ret < 0)
167  {
168  g_warning ("%s: failed to verify peers: %s",
169  __FUNCTION__,
170  gnutls_strerror (ret));
171  return -1;
172  }
173 
174  if (status & GNUTLS_CERT_INVALID)
175  g_warning ("%s: the certificate is not trusted", __FUNCTION__);
176 
177  if (status & GNUTLS_CERT_SIGNER_NOT_CA)
178  g_warning ("%s: the certificate's issuer is not a CA", __FUNCTION__);
179 
180  if (status & GNUTLS_CERT_INSECURE_ALGORITHM)
181  g_warning ("%s: the certificate was signed using an insecure algorithm",
182  __FUNCTION__);
183 
184  if (status & GNUTLS_CERT_SIGNER_NOT_FOUND)
185  g_warning ("%s: the certificate hasn't got a known issuer", __FUNCTION__);
186 
187  if (status & GNUTLS_CERT_REVOKED)
188  g_warning ("%s: the certificate has been revoked", __FUNCTION__);
189 
190  if (status & GNUTLS_CERT_EXPIRED)
191  g_warning ("%s: the certificate has expired", __FUNCTION__);
192 
193  if (status & GNUTLS_CERT_NOT_ACTIVATED)
194  g_warning ("%s: the certificate is not yet activated", __FUNCTION__);
195 
196  if (status)
197  return 1;
198 
199  return 0;
200 }

Referenced by openvas_server_connect().

Here is the caller graph for this function:

◆ openvas_server_vsendf()

int openvas_server_vsendf ( gnutls_session_t *  session,
const char *  fmt,
va_list  ap 
)

Send a string to the server.

Parameters
[in]sessionPointer to GNUTLS session.
[in]fmtFormat of string to send.
[in]apArgs for fmt.
Returns
0 on success, 1 if server closed connection, -1 on error.

Definition at line 851 of file openvas_server.c.

852 {
853  return openvas_server_vsendf_internal (session, fmt, ap, 0);
854 }

Referenced by openvas_server_sendf().

Here is the caller graph for this function:

◆ openvas_server_vsendf_quiet()

int openvas_server_vsendf_quiet ( gnutls_session_t *  session,
const char *  fmt,
va_list  ap 
)

Send a string to the server, refraining from logging besides warnings.

Parameters
[in]sessionPointer to GNUTLS session.
[in]fmtFormat of string to send.
[in]apArgs for fmt.
Returns
0 on success, 1 if server closed connection, -1 on error.

Definition at line 882 of file openvas_server.c.

884 {
885  return openvas_server_vsendf_internal (session, fmt, ap, 1);
886 }

Referenced by openvas_server_sendf_quiet().

Here is the caller graph for this function:

◆ set_gnutls_dhparams()

int set_gnutls_dhparams ( gnutls_certificate_credentials_t  creds,
const char *  dhparams_file 
)

Set a gnutls session's Diffie-Hellman parameters.

Parameters
[in]credsGnuTLS credentials.
[in]dhparams_filePath to PEM file containing the DH parameters.
Returns
0 on success, -1 on error.

Definition at line 1321 of file openvas_server.c.

1323 {
1324  int ret;
1325  gnutls_datum_t data;
1326 
1327  if (!creds || !dhparams_file)
1328  return -1;
1329 
1330  if (load_gnutls_file (dhparams_file, &data))
1331  return -1;
1332  gnutls_dh_params_t params = g_malloc0 (sizeof (gnutls_dh_params_t));
1333  ret = gnutls_dh_params_import_pkcs3 (params, &data, GNUTLS_X509_FMT_PEM);
1334  unload_gnutls_file (&data);
1335  if (ret)
1336  return -1;
1337  else
1338  gnutls_certificate_set_dh_params (creds, params);
1339  return 0;
1340 }
void unload_gnutls_file(gnutls_datum_t *data)
Unloads a gnutls_datum_t struct's data.
int load_gnutls_file(const char *file, gnutls_datum_t *loaded_file)
Loads a file's data into gnutls_datum_t struct.

References load_gnutls_file(), and unload_gnutls_file().

Here is the call graph for this function:

◆ unload_gnutls_file()

void unload_gnutls_file ( gnutls_datum_t *  data)

Unloads a gnutls_datum_t struct's data.

Parameters
[in]dataPointer to gnutls_datum_t struct to be unloaded.

Definition at line 241 of file openvas_server.c.

242 {
243  if (data)
244  g_free (data->data);
245 }

Referenced by set_gnutls_dhparams().

Here is the caller graph for this function:

Variable Documentation

◆ address

struct sockaddr_in address

Server address.

Definition at line 80 of file openvas_server.c.

Referenced by openvas_server_open_verify().