The ssh, libssh, libssh2 & unix transports all need to use a UNIX socket
path, and duplicate some of the same logic for error checking. Pull this
out into a separate method to increase code sharing.
Reviewed-by: Andrea Bolognani <abologna(a)redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange(a)redhat.com>
---
src/remote/remote_driver.c | 124 +++++++++++++++----------------------
1 file changed, 49 insertions(+), 75 deletions(-)
diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c
index 1b36ef3f05..6e1001b385 100644
--- a/src/remote/remote_driver.c
+++ b/src/remote/remote_driver.c
@@ -747,34 +747,35 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr conn,
}
-#ifndef WIN32
-static char *remoteGetUNIXSocketNonRoot(void)
+static char *
+remoteGetUNIXSocket(remoteDriverTransport transport,
+ unsigned int flags)
{
char *sockname = NULL;
- char *userdir = virGetUserRuntimeDirectory();
-
- if (!userdir)
- return NULL;
+ VIR_AUTOFREE(char *) userdir = NULL;
+
+ if (flags & VIR_DRV_OPEN_REMOTE_USER) {
+ if (transport != REMOTE_DRIVER_TRANSPORT_UNIX) {
+ virReportError(VIR_ERR_OPERATION_UNSUPPORTED,
+ _("Connecting to session instance without "
+ "socket path is not supported by the %s "
+ "transport"),
+ remoteDriverTransportTypeToString(transport));
+ return NULL;
+ }
+ if (!(userdir = virGetUserRuntimeDirectory()))
+ return NULL;
- if (virAsprintf(&sockname, "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir)
< 0) {
- VIR_FREE(userdir);
- return NULL;
+ if (virAsprintf(&sockname,
+ "%s/" LIBVIRTD_USER_UNIX_SOCKET, userdir) < 0)
+ return NULL;
+ } else {
+ if (VIR_STRDUP(sockname,
+ flags & VIR_DRV_OPEN_REMOTE_RO ?
+ LIBVIRTD_PRIV_UNIX_SOCKET_RO :
+ LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
+ return NULL;
}
- VIR_FREE(userdir);
-
- VIR_DEBUG("Chosen UNIX sockname %s", sockname);
- return sockname;
-}
-#endif /* WIN32 */
-
-static char *remoteGetUNIXSocketRoot(unsigned int flags)
-{
- char *sockname = NULL;
-
- if (VIR_STRDUP(sockname,
- flags & VIR_DRV_OPEN_REMOTE_RO ?
- LIBVIRTD_PRIV_UNIX_SOCKET_RO : LIBVIRTD_PRIV_UNIX_SOCKET) < 0)
- return NULL;
VIR_DEBUG("Chosen UNIX sockname %s", sockname);
return sockname;
@@ -961,6 +962,30 @@ doRemoteOpen(virConnectPtr conn,
}
VIR_DEBUG("Connecting with transport %d", transport);
+
+ switch ((remoteDriverTransport)transport) {
+ case REMOTE_DRIVER_TRANSPORT_UNIX:
+ case REMOTE_DRIVER_TRANSPORT_SSH:
+ case REMOTE_DRIVER_TRANSPORT_LIBSSH:
+ case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
+ if (!sockname &&
+ !(sockname = remoteGetUNIXSocket(transport, flags)))
+ goto failed;
+ break;
+
+ case REMOTE_DRIVER_TRANSPORT_TCP:
+ case REMOTE_DRIVER_TRANSPORT_TLS:
+ case REMOTE_DRIVER_TRANSPORT_EXT:
+ break;
+
+ case REMOTE_DRIVER_TRANSPORT_LAST:
+ default:
+ virReportEnumRangeError(remoteDriverTransport, transport);
+ goto failed;
+ }
+
+ VIR_DEBUG("Chosen UNIX socket %s", NULLSTR(sockname));
+
/* Connect to the remote service. */
switch ((remoteDriverTransport)transport) {
case REMOTE_DRIVER_TRANSPORT_TLS:
@@ -1002,20 +1027,6 @@ doRemoteOpen(virConnectPtr conn,
break;
case REMOTE_DRIVER_TRANSPORT_LIBSSH2:
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the libssh2
"
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
VIR_DEBUG("Starting LibSSH2 session");
priv->client = virNetClientNewLibSSH2(priv->hostname,
@@ -1037,20 +1048,6 @@ doRemoteOpen(virConnectPtr conn,
break;
case REMOTE_DRIVER_TRANSPORT_LIBSSH:
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the libssh "
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
VIR_DEBUG("Starting libssh session");
priv->client = virNetClientNewLibssh(priv->hostname,
@@ -1073,15 +1070,6 @@ doRemoteOpen(virConnectPtr conn,
#ifndef WIN32
case REMOTE_DRIVER_TRANSPORT_UNIX:
- if (!sockname) {
- if (flags & VIR_DRV_OPEN_REMOTE_USER)
- sockname = remoteGetUNIXSocketNonRoot();
- else
- sockname = remoteGetUNIXSocketRoot(flags);
- if (!sockname)
- goto failed;
- }
-
if ((flags & VIR_DRV_OPEN_REMOTE_AUTOSTART) &&
!(daemonPath = virFileFindResourceFull("libvirtd",
NULL, NULL,
@@ -1102,20 +1090,6 @@ doRemoteOpen(virConnectPtr conn,
if (!command && VIR_STRDUP(command, "ssh") < 0)
goto failed;
- if (!sockname) {
- /* Right now we don't support default session connections */
- if (flags & VIR_DRV_OPEN_REMOTE_USER) {
- virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s",
- _("Connecting to session instance without "
- "socket path is not supported by the ssh "
- "connection driver"));
- goto failed;
- }
-
- if (!(sockname = remoteGetUNIXSocketRoot(flags)))
- goto failed;
- }
-
if (!(priv->client = virNetClientNewSSH(priv->hostname,
port,
command,
--
2.21.0