#include "viralloc.h"
#include "configmake.h"
#include "virconf.h"
-#if WITH_GNUTLS
-# include "rpc/virnettlscontext.h"
-#endif
+#include "rpc/virnettlscontext.h"
#include "vircommand.h"
#include "virfile.h"
#include "virrandom.h"
virLogSetFromEnv();
-#ifdef WITH_GNUTLS
virNetTLSInit();
-#endif
#if WITH_CURL
curl_global_init(CURL_GLOBAL_DEFAULT);
int counter; /* Serial number for RPC */
-#ifdef WITH_GNUTLS
virNetTLSContextPtr tls;
-#endif
int is_secure; /* Secure if TLS or SASL or UNIX sockets */
char *type; /* Cached return from remoteType. */
virConfGetValueString(conf, "tls_priority", &tls_priority) < 0)
goto failed;
-#ifdef WITH_GNUTLS
priv->tls = virNetTLSContextNewClientPath(pkipath,
geteuid() != 0 ? true : false,
tls_priority,
goto failed;
priv->is_secure = 1;
G_GNUC_FALLTHROUGH;
-#else
- (void)tls_priority;
- (void)sanity;
- (void)verify;
- virReportError(VIR_ERR_INVALID_ARG, "%s",
- _("GNUTLS support not available in this build"));
- goto failed;
-#endif
case REMOTE_DRIVER_TRANSPORT_TCP:
priv->client = virNetClientNewTCP(priv->hostname, port, AF_UNSPEC);
if (!priv->client)
goto failed;
-#ifdef WITH_GNUTLS
if (priv->tls) {
VIR_DEBUG("Starting TLS session");
if (virNetClientSetTLSSession(priv->client, priv->tls) < 0)
goto failed;
}
-#endif
break;
priv->client = NULL;
virObjectUnref(priv->closeCallback);
priv->closeCallback = NULL;
-#ifdef WITH_GNUTLS
virObjectUnref(priv->tls);
priv->tls = NULL;
-#endif
VIR_FREE(priv->hostname);
return VIR_DRV_OPEN_ERROR;
(xdrproc_t) xdr_void, (char *) NULL) == -1)
ret = -1;
-#ifdef WITH_GNUTLS
virObjectUnref(priv->tls);
priv->tls = NULL;
-#endif
virNetClientSetCloseCallback(priv->client,
NULL,
/* saslcb is now owned by sasl */
saslcb = NULL;
-# ifdef WITH_GNUTLS
/* Initialize some connection props we care about */
if (priv->tls) {
if ((ssf = virNetClientGetTLSKeySize(priv->client)) < 0)
if (virNetSASLSessionExtKeySize(sasl, ssf) < 0)
goto cleanup;
}
-# endif
/* If we've got a secure channel (TLS or UNIX sock), we don't care about SSF */
/* If we're not secure, then forbid any anonymous or trivially crackable auth */
virNetSocketPtr sock;
bool asyncIO;
-#if WITH_GNUTLS
virNetTLSSessionPtr tls;
-#endif
char *hostname;
virNetClientProgramPtr *programs;
if (client->sock)
virNetSocketRemoveIOCallback(client->sock);
virObjectUnref(client->sock);
-#if WITH_GNUTLS
virObjectUnref(client->tls);
-#endif
#if WITH_SASL
virObjectUnref(client->sasl);
#endif
virObjectUnref(client->sock);
client->sock = NULL;
-#if WITH_GNUTLS
virObjectUnref(client->tls);
client->tls = NULL;
-#endif
#if WITH_SASL
virObjectUnref(client->sasl);
client->sasl = NULL;
#endif
-#if WITH_GNUTLS
int virNetClientSetTLSSession(virNetClientPtr client,
virNetTLSContextPtr tls)
{
sigset_t oldmask, blockedsigs;
sigemptyset(&blockedsigs);
-# ifdef SIGWINCH
+#ifdef SIGWINCH
sigaddset(&blockedsigs, SIGWINCH);
-# endif
-# ifdef SIGCHLD
+#endif
+#ifdef SIGCHLD
sigaddset(&blockedsigs, SIGCHLD);
-# endif
+#endif
sigaddset(&blockedsigs, SIGPIPE);
virObjectLock(client);
virObjectUnlock(client);
return -1;
}
-#endif
bool virNetClientIsEncrypted(virNetClientPtr client)
{
bool ret = false;
virObjectLock(client);
-#if WITH_GNUTLS
if (client->tls)
ret = true;
-#endif
#if WITH_SASL
if (client->sasl)
ret = true;
return virNetSocketRemoteAddrStringSASL(client->sock);
}
-#if WITH_GNUTLS
int virNetClientGetTLSKeySize(virNetClientPtr client)
{
int ret = 0;
virObjectUnlock(client);
return ret;
}
-#endif
static int
virNetClientCallDispatchReply(virNetClientPtr client)
#pragma once
-#ifdef WITH_GNUTLS
-# include "virnettlscontext.h"
-#endif
+#include "virnettlscontext.h"
#include "virnetmessage.h"
#ifdef WITH_SASL
# include "virnetsaslcontext.h"
virNetSASLSessionPtr sasl);
#endif
-#ifdef WITH_GNUTLS
int virNetClientSetTLSSession(virNetClientPtr client,
virNetTLSContextPtr tls);
-#endif
bool virNetClientIsEncrypted(virNetClientPtr client);
bool virNetClientIsOpen(virNetClientPtr client);
const char *virNetClientLocalAddrStringSASL(virNetClientPtr client);
const char *virNetClientRemoteAddrStringSASL(virNetClientPtr client);
-#ifdef WITH_GNUTLS
int virNetClientGetTLSKeySize(virNetClientPtr client);
-#endif
void virNetClientClose(virNetClientPtr client);
char *remoteAddrStrSASL;
char *remoteAddrStrURI;
-#if WITH_GNUTLS
virNetTLSSessionPtr tlsSession;
-#endif
#if WITH_SASL
virNetSASLSessionPtr saslSession;
goto error;
}
#endif
-#if WITH_GNUTLS
if (sock->tlsSession) {
virReportError(VIR_ERR_OPERATION_INVALID, "%s",
_("Unable to save socket state when TLS session is active"));
goto error;
}
-#endif
if (!(object = virJSONValueNewObject()))
goto error;
unlink(sock->localAddr.data.un.sun_path);
#endif
-#if WITH_GNUTLS
/* Make sure it can't send any more I/O during shutdown */
if (sock->tlsSession)
virNetTLSSessionSetIOCallbacks(sock->tlsSession, NULL, NULL, NULL);
virObjectUnref(sock->tlsSession);
-#endif
#if WITH_SASL
virObjectUnref(sock->saslSession);
#endif
return sock->remoteAddrStrURI;
}
-#if WITH_GNUTLS
static ssize_t virNetSocketTLSSessionWrite(const char *buf,
size_t len,
void *opaque)
sock);
virObjectUnlock(sock);
}
-#endif
#if WITH_SASL
void virNetSocketSetSASLSession(virNetSocketPtr sock,
#endif
reread:
-#if WITH_GNUTLS
if (sock->tlsSession &&
virNetTLSSessionGetHandshakeStatus(sock->tlsSession) ==
VIR_NET_TLS_HANDSHAKE_COMPLETE) {
ret = virNetTLSSessionRead(sock->tlsSession, buf, len);
} else {
-#endif
ret = read(sock->fd, buf, len);
-#if WITH_GNUTLS
}
-#endif
if ((ret < 0) && (errno == EINTR))
goto reread;
#endif
rewrite:
-#if WITH_GNUTLS
if (sock->tlsSession &&
virNetTLSSessionGetHandshakeStatus(sock->tlsSession) ==
VIR_NET_TLS_HANDSHAKE_COMPLETE) {
ret = virNetTLSSessionWrite(sock->tlsSession, buf, len);
} else {
-#endif
ret = write(sock->fd, buf, len);
-#if WITH_GNUTLS
}
-#endif
if (ret < 0) {
if (errno == EINTR)
#include "virsocketaddr.h"
#include "vircommand.h"
-#ifdef WITH_GNUTLS
-# include "virnettlscontext.h"
-#endif
+#include "virnettlscontext.h"
#include "virobject.h"
#ifdef WITH_SASL
# include "virnetsaslcontext.h"
int virNetSocketSendFD(virNetSocketPtr sock, int fd);
int virNetSocketRecvFD(virNetSocketPtr sock, int *fd);
-#ifdef WITH_GNUTLS
void virNetSocketSetTLSSession(virNetSocketPtr sock,
virNetTLSSessionPtr sess);
-#endif
#ifdef WITH_SASL
void virNetSocketSetSASLSession(virNetSocketPtr sock,
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
-#ifdef WITH_GNUTLS
-# include <gnutls/gnutls.h>
-# include <gnutls/crypto.h>
-#endif
+#include <gnutls/gnutls.h>
+#include <gnutls/crypto.h>
#include "virrandom.h"
#include "virthread.h"
VIR_LOG_INIT("util.random");
-#define RANDOM_SOURCE "/dev/urandom"
-
/**
* virRandomBits:
* @nbits: Number of bits of randomness required
* @buf: Pointer to location to store bytes
* @buflen: Number of bytes to store
*
- * Generate a stream of random bytes from RANDOM_SOURCE
+ * Generate a stream of random bytes using gnutls_rnd()
* into @buf of size @buflen
*
* Returns 0 on success or -1 (with error reported)
virRandomBytes(unsigned char *buf,
size_t buflen)
{
-#if WITH_GNUTLS
int rv;
- /* Generate the byte stream using gnutls_rnd() if possible */
if ((rv = gnutls_rnd(GNUTLS_RND_RANDOM, buf, buflen)) < 0) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("failed to generate byte stream: %s"),
return -1;
}
-#else /* !WITH_GNUTLS */
-
- int fd;
-
- if ((fd = open(RANDOM_SOURCE, O_RDONLY)) < 0) {
- virReportSystemError(errno,
- _("unable to open %s"),
- RANDOM_SOURCE);
- return -1;
- }
-
- while (buflen > 0) {
- ssize_t n;
-
- if ((n = saferead(fd, buf, buflen)) <= 0) {
- virReportSystemError(errno,
- _("unable to read from %s"),
- RANDOM_SOURCE);
- VIR_FORCE_CLOSE(fd);
- return n < 0 ? -errno : -ENODATA;
- }
-
- buf += n;
- buflen -= n;
- }
-
- VIR_FORCE_CLOSE(fd);
-#endif /* !WITH_GNUTLS */
-
return 0;
}