From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319826; cv=none; d=zohomail.com; s=zohoarc; b=jvm9Shid2QHjjTUdgu/+/poTJ/0hIPtRgNJE0NdE6rIeN4KTCS4P0pzRt4XeRoqcks+DHq/OShJ0lJQ6wPFX6Q6wWB9QlmbEirTBfaTGNZ/yX5s8gTHQrj4qZAesFm1ZlzE2WGP4VfB3yZN75dvoE7y4vbQuSaycwueMlFv7Bec= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319826; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=W1f0+hkY7klQuVteXxhdFuGfgIZHA3YQxQWGZbTPaWA=; b=lGVMzwpk+GUBm56AF1bDQg5eN7QS0cMlu3ftl8iScaCCgmVbANA8ffpcTKzwJcxfAwFzblNCDxTRHuTTT1mtKJGAZR3JO7dXI60S789cSRNAaZ/GDesO0ijBW/UjmEge5DDqqVt7RJentbdRDuOprmKmEYrAs1FimnaQvXb5dfE= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1594319826471962.73275987648; Thu, 9 Jul 2020 11:37:06 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-487-OZnRGVDBPU-Zem6g_ntIoQ-1; Thu, 09 Jul 2020 14:37:02 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 1977D8005B0; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E978619D7C; Thu, 9 Jul 2020 18:36:55 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id B9C2A1809563; Thu, 9 Jul 2020 18:36:55 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Iarra023326 for ; Thu, 9 Jul 2020 14:36:53 -0400 Received: by smtp.corp.redhat.com (Postfix) id 70A8C60E1C; Thu, 9 Jul 2020 18:36:53 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 316A760F8D; Thu, 9 Jul 2020 18:36:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319825; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=W1f0+hkY7klQuVteXxhdFuGfgIZHA3YQxQWGZbTPaWA=; b=gRpqDLK3ZjhFXifDpeUMKJCKdvorm5NT8Ye9JvSyonXwk79eN2DHzQniEoaAbSMI+Ne66J 4q9yOzCuCUdMkweFywWTSBgpsLvbHcs1WY1XQ0fcK1RlijlQ0i7xkIE5NEs3UQL9scJXNo X7x6qwCkuqCrhWxGlyyaxrmbqhZXnGQ= X-MC-Unique: OZnRGVDBPU-Zem6g_ntIoQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 1/9] rpc: merge logic for generating remote SSH shell script Date: Thu, 9 Jul 2020 19:36:38 +0100 Message-Id: <20200709183646.4016586-2-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Three parts of the code all build up the same SSH shell script snippet for remote tunneling the RPC protocol, but in slightly different ways. Combine them all into one helper method in the virNetClient code, since this logic doesn't really belong in the virNetSocket code. Note that the this change means the shell snippet is passed to the SSH binary as a single arg, instead of three separate args, but this is functionally identical, as the three separate args were combined into one already when passed to the remote system. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/libvirt_remote.syms | 1 + src/rpc/virnetclient.c | 105 +++++++++++++++++++++------------------ src/rpc/virnetclient.h | 3 ++ src/rpc/virnetsocket.c | 37 +------------- src/rpc/virnetsocket.h | 3 +- tests/virnetsockettest.c | 9 +++- 6 files changed, 70 insertions(+), 88 deletions(-) diff --git a/src/libvirt_remote.syms b/src/libvirt_remote.syms index 0018a0c41d..0b00bce1fa 100644 --- a/src/libvirt_remote.syms +++ b/src/libvirt_remote.syms @@ -42,6 +42,7 @@ virNetClientSendStream; virNetClientSendWithReply; virNetClientSetCloseCallback; virNetClientSetTLSSession; +virNetClientSSHHelperCommand; =20 =20 # rpc/virnetclientprogram.h diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index 1c5bef86a1..aee2b52bf6 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -391,28 +391,75 @@ virNetClientPtr virNetClientNewTCP(const char *nodena= me, return virNetClientNew(sock, nodename); } =20 + +/* + * The SSH Server uses shell to spawn the command we give + * it. Our command then invokes shell again. Thus we need + * to apply two levels of escaping, so that commands with + * whitespace in their path get correctly interpreted. + */ +static char * +virNetClientDoubleEscapeShell(const char *str) +{ + virBuffer buf =3D VIR_BUFFER_INITIALIZER; + g_autofree char *tmp =3D NULL; + + virBufferEscapeShell(&buf, str); + + tmp =3D virBufferContentAndReset(&buf); + + virBufferEscapeShell(&buf, tmp); + + return virBufferContentAndReset(&buf); +} + +char * +virNetClientSSHHelperCommand(const char *netcatPath, + const char *socketPath) +{ + g_autofree char *netcatPathSafe =3D virNetClientDoubleEscapeShell(netc= atPath); + + return g_strdup_printf( + "sh -c " + "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1;= then " + "ARG=3D-q0;" + "else " + "ARG=3D;" + "fi;" + "'%s' $ARG -U %s'", + netcatPathSafe, netcatPathSafe, socketPath); +} + + +#define DEFAULT_VALUE(VAR, VAL) \ + if (!VAR) \ + VAR =3D VAL; + virNetClientPtr virNetClientNewSSH(const char *nodename, const char *service, const char *binary, const char *username, bool noTTY, bool noVerify, - const char *netcat, + const char *netcatPath, const char *keyfile, - const char *path) + const char *socketPath) { virNetSocketPtr sock; =20 + g_autofree char *command =3D NULL; + + DEFAULT_VALUE(netcatPath, "nc"); + + command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); + if (virNetSocketNewConnectSSH(nodename, service, binary, username, noT= TY, - noVerify, netcat, keyfile, path, &sock) = < 0) + noVerify, keyfile, command, &sock) < 0) return NULL; =20 return virNetClientNew(sock, NULL); } =20 -#define DEFAULT_VALUE(VAR, VAL) \ - if (!VAR) \ - VAR =3D VAL; virNetClientPtr virNetClientNewLibSSH2(const char *host, const char *port, int family, @@ -428,8 +475,6 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, { virNetSocketPtr sock =3D NULL; =20 - virBuffer buf =3D VIR_BUFFER_INITIALIZER; - g_autofree char *nc =3D NULL; g_autofree char *command =3D NULL; =20 g_autofree char *homedir =3D NULL; @@ -442,9 +487,7 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, knownhosts =3D g_strdup(knownHostsPath); } else { confdir =3D virGetUserConfigDirectory(); - virBufferAsprintf(&buf, "%s/known_hosts", confdir); - if (!(knownhosts =3D virBufferContentAndReset(&buf))) - return NULL; + knownhosts =3D g_strdup_printf("%s/known_hosts", confdir); } =20 if (privkeyPath) { @@ -468,26 +511,7 @@ virNetClientPtr virNetClientNewLibSSH2(const char *hos= t, DEFAULT_VALUE(netcatPath, "nc"); DEFAULT_VALUE(knownHostsVerify, "normal"); =20 - virBufferEscapeShell(&buf, netcatPath); - if (!(nc =3D virBufferContentAndReset(&buf))) - return NULL; - virBufferEscapeShell(&buf, nc); - VIR_FREE(nc); - if (!(nc =3D virBufferContentAndReset(&buf))) - return NULL; - - virBufferAsprintf(&buf, - "sh -c " - "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1= ; then " - "ARG=3D-q0;" - "else " - "ARG=3D;" - "fi;" - "'%s' $ARG -U %s'", - nc, nc, socketPath); - - if (!(command =3D virBufferContentAndReset(&buf))) - return NULL; + command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); =20 if (virNetSocketNewConnectLibSSH2(host, port, family, @@ -498,11 +522,7 @@ virNetClientPtr virNetClientNewLibSSH2(const char *hos= t, =20 return virNetClientNew(sock, NULL); } -#undef DEFAULT_VALUE =20 -#define DEFAULT_VALUE(VAR, VAL) \ - if (!VAR) \ - VAR =3D VAL; virNetClientPtr virNetClientNewLibssh(const char *host, const char *port, int family, @@ -518,8 +538,6 @@ virNetClientPtr virNetClientNewLibssh(const char *host, { virNetSocketPtr sock =3D NULL; =20 - virBuffer buf =3D VIR_BUFFER_INITIALIZER; - g_autofree char *nc =3D NULL; g_autofree char *command =3D NULL; =20 g_autofree char *homedir =3D NULL; @@ -556,18 +574,7 @@ virNetClientPtr virNetClientNewLibssh(const char *host, DEFAULT_VALUE(netcatPath, "nc"); DEFAULT_VALUE(knownHostsVerify, "normal"); =20 - virBufferEscapeShell(&buf, netcatPath); - if (!(nc =3D virBufferContentAndReset(&buf))) - return NULL; - virBufferEscapeShell(&buf, nc); - VIR_FREE(nc); - if (!(nc =3D virBufferContentAndReset(&buf))) - return NULL; - - command =3D g_strdup_printf("sh -c " - "'if '%s' -q 2>&1 | grep \"requires an argum= ent\" >/dev/null 2>&1; then " - "ARG=3D-q0;" "else " "ARG=3D;" "fi;" "'%s' $= ARG -U %s'", nc, nc, - socketPath); + command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); =20 if (virNetSocketNewConnectLibssh(host, port, family, diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h index 778910b575..0005de46f3 100644 --- a/src/rpc/virnetclient.h +++ b/src/rpc/virnetclient.h @@ -30,6 +30,9 @@ #include "virobject.h" #include "viruri.h" =20 +char * +virNetClientSSHHelperCommand(const char *netcatPath, + const char *socketPath); =20 virNetClientPtr virNetClientNewUNIX(const char *path, bool spawnDaemon, diff --git a/src/rpc/virnetsocket.c b/src/rpc/virnetsocket.c index 3ea863f625..6909a92a93 100644 --- a/src/rpc/virnetsocket.c +++ b/src/rpc/virnetsocket.c @@ -842,14 +842,11 @@ int virNetSocketNewConnectSSH(const char *nodename, const char *username, bool noTTY, bool noVerify, - const char *netcat, const char *keyfile, - const char *path, + const char *command, virNetSocketPtr *retsock) { - char *quoted; virCommandPtr cmd; - virBuffer buf =3D VIR_BUFFER_INITIALIZER; =20 *retsock =3D NULL; =20 @@ -874,38 +871,8 @@ int virNetSocketNewConnectSSH(const char *nodename, if (noVerify) virCommandAddArgList(cmd, "-o", "StrictHostKeyChecking=3Dno", NULL= ); =20 - if (!netcat) - netcat =3D "nc"; - - virCommandAddArgList(cmd, "--", nodename, "sh", "-c", NULL); - - virBufferEscapeShell(&buf, netcat); - quoted =3D virBufferContentAndReset(&buf); + virCommandAddArgList(cmd, "--", nodename, command, NULL); =20 - virBufferEscapeShell(&buf, quoted); - VIR_FREE(quoted); - quoted =3D virBufferContentAndReset(&buf); - - /* - * This ugly thing is a shell script to detect availability of - * the -q option for 'nc': debian and suse based distros need this - * flag to ensure the remote nc will exit on EOF, so it will go away - * when we close the connection tunnel. If it doesn't go away, subsequ= ent - * connection attempts will hang. - * - * Fedora's 'nc' doesn't have this option, and defaults to the desired - * behavior. - */ - virCommandAddArgFormat(cmd, - "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1= ; then " - "ARG=3D-q0;" - "else " - "ARG=3D;" - "fi;" - "'%s' $ARG -U %s'", - quoted, quoted, path); - - VIR_FREE(quoted); return virNetSocketNewConnectCommand(cmd, retsock); } =20 diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h index f2b74f3ccb..d39b270480 100644 --- a/src/rpc/virnetsocket.h +++ b/src/rpc/virnetsocket.h @@ -78,9 +78,8 @@ int virNetSocketNewConnectSSH(const char *nodename, const char *username, bool noTTY, bool noVerify, - const char *netcat, const char *keyfile, - const char *path, + const char *command, virNetSocketPtr *addr); =20 int virNetSocketNewConnectLibSSH2(const char *host, diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c index 78fb9cbffd..842eb1bcfc 100644 --- a/tests/virnetsockettest.c +++ b/tests/virnetsockettest.c @@ -32,6 +32,7 @@ #include "virstring.h" =20 #include "rpc/virnetsocket.h" +#include "rpc/virnetclient.h" =20 #define VIR_FROM_THIS VIR_FROM_RPC =20 @@ -463,6 +464,8 @@ static int testSocketSSH(const void *opaque) virNetSocketPtr csock =3D NULL; /* Client socket */ int ret =3D -1; char buf[1024]; + g_autofree char *command =3D virNetClientSSHHelperCommand(data->netcat, + data->path); =20 if (virNetSocketNewConnectSSH(data->nodename, data->service, @@ -470,9 +473,8 @@ static int testSocketSSH(const void *opaque) data->username, data->noTTY, data->noVerify, - data->netcat, data->keyfile, - data->path, + command, &csock) < 0) goto cleanup; =20 @@ -570,6 +572,7 @@ mymain(void) struct testSSHData sshData1 =3D { .nodename =3D "somehost", .path =3D "/tmp/socket", + .netcat =3D "nc", .expectOut =3D "-T -e none -- somehost sh -c '" "if 'nc' -q 2>&1 | grep \"requires an argument\" >/de= v/null 2>&1; then " "ARG=3D-q0;" @@ -630,6 +633,7 @@ mymain(void) struct testSSHData sshData5 =3D { .nodename =3D "crashyhost", .path =3D "/tmp/socket", + .netcat =3D "nc", .expectOut =3D "-T -e none -- crashyhost sh -c " "'if 'nc' -q 2>&1 | grep \"requires an argument\" >/d= ev/null 2>&1; then " "ARG=3D-q0;" @@ -645,6 +649,7 @@ mymain(void) struct testSSHData sshData6 =3D { .nodename =3D "example.com", .path =3D "/tmp/socket", + .netcat =3D "nc", .keyfile =3D "/root/.ssh/example_key", .noVerify =3D true, .expectOut =3D "-i /root/.ssh/example_key -T -e none -o StrictHost= KeyChecking=3Dno -- example.com sh -c '" --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319826; cv=none; d=zohomail.com; s=zohoarc; b=gQ+qOfB02u94mvUtcaSItKJNMtE1IwezELnXtmfuMk8ZPA995+HSS4MasDuNeun4cRXl1C/QpEovCr6fZEs1tEer6b8bpb87JizOELroeg5Vopwney6Wuku/fQD6QY+HHgWcV6DdiokmIFQExc55j8xV+Y+yCJms+UIm8Cp5SMg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319826; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=j9qYoqUK+ALu55ixggpkeM9jMOTdW7e5pYqdxX8F6uY=; b=BKNkMZN92m+e7oyRJOQ3eQVzZsrk2LQU1NI0rkO/qa65mssK9Xk3ee39bc0ET0krQz3w90NGoGwbibvwBQNdg/WOw6kFJhm56ntFNWpbb6+pCOsEGzbpm7yxhSZojcW9A3SUy5dd/Sis5Ap2DPGAUbZ8Ff84TJhIuA7dw9nU0do= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594319826284335.63973422710103; Thu, 9 Jul 2020 11:37:06 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-466-ZDhvpJvSOQydrwuWeeBLlw-1; Thu, 09 Jul 2020 14:37:02 -0400 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6BB59107B7EF; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 43F8210016DA; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 11E71180043D; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Ias3p023338 for ; Thu, 9 Jul 2020 14:36:54 -0400 Received: by smtp.corp.redhat.com (Postfix) id D053C60F8D; Thu, 9 Jul 2020 18:36:54 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 1EA3A60E1C; Thu, 9 Jul 2020 18:36:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319824; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=j9qYoqUK+ALu55ixggpkeM9jMOTdW7e5pYqdxX8F6uY=; b=VHKl0R07pvy6Fk3ekWZcjLL6bwa/b66DpuZKBqhi5sOS5y9AfUjuHxOIWXeIKuBCGrPOGi T/ZBXrehrJejWM/tnPLpIamcMik7YVqFDc4R8BybfN6G7tDNFcsqIJ7SXUQmoeLvP6aMv/ x0rXzwm7EXBCanwNizxl9Y9bTUVOapY= X-MC-Unique: ZDhvpJvSOQydrwuWeeBLlw-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 2/9] remote: split off enums into separate source file Date: Thu, 9 Jul 2020 19:36:39 +0100 Message-Id: <20200709183646.4016586-3-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The remoteDriverTransport and remoteDriverMode enums are going to be needed by source files beyond the remote driver client. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/remote/Makefile.inc.am | 2 ++ src/remote/remote_driver.c | 41 +----------------------------- src/remote/remote_sockets.c | 39 +++++++++++++++++++++++++++++ src/remote/remote_sockets.h | 50 +++++++++++++++++++++++++++++++++++++ 4 files changed, 92 insertions(+), 40 deletions(-) create mode 100644 src/remote/remote_sockets.c create mode 100644 src/remote/remote_sockets.h diff --git a/src/remote/Makefile.inc.am b/src/remote/Makefile.inc.am index 1b1be8340d..0ae97f4107 100644 --- a/src/remote/Makefile.inc.am +++ b/src/remote/Makefile.inc.am @@ -13,6 +13,8 @@ REMOTE_DRIVER_GENERATED =3D \ $(NULL) =20 REMOTE_DRIVER_SOURCES =3D \ + remote/remote_sockets.c \ + remote/remote_sockets.h \ remote/remote_driver.c \ remote/remote_driver.h \ $(NULL) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 653c68472a..880fce6e62 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -38,6 +38,7 @@ #include "virbuffer.h" #include "remote_driver.h" #include "remote_protocol.h" +#include "remote_sockets.h" #include "lxc_protocol.h" #include "qemu_protocol.h" #include "viralloc.h" @@ -54,46 +55,6 @@ =20 VIR_LOG_INIT("remote.remote_driver"); =20 -typedef enum { - REMOTE_DRIVER_TRANSPORT_TLS, - REMOTE_DRIVER_TRANSPORT_UNIX, - REMOTE_DRIVER_TRANSPORT_SSH, - REMOTE_DRIVER_TRANSPORT_LIBSSH2, - REMOTE_DRIVER_TRANSPORT_EXT, - REMOTE_DRIVER_TRANSPORT_TCP, - REMOTE_DRIVER_TRANSPORT_LIBSSH, - - REMOTE_DRIVER_TRANSPORT_LAST, -} remoteDriverTransport; - -VIR_ENUM_DECL(remoteDriverTransport); -VIR_ENUM_IMPL(remoteDriverTransport, - REMOTE_DRIVER_TRANSPORT_LAST, - "tls", - "unix", - "ssh", - "libssh2", - "ext", - "tcp", - "libssh"); - -typedef enum { - /* Try to figure out the "best" choice magically */ - REMOTE_DRIVER_MODE_AUTO, - /* Always use the legacy libvirtd */ - REMOTE_DRIVER_MODE_LEGACY, - /* Always use the per-driver virt*d daemons */ - REMOTE_DRIVER_MODE_DIRECT, - - REMOTE_DRIVER_MODE_LAST -} remoteDriverMode; - -VIR_ENUM_DECL(remoteDriverMode); -VIR_ENUM_IMPL(remoteDriverMode, - REMOTE_DRIVER_MODE_LAST, - "auto", - "legacy", - "direct"); =20 #if SIZEOF_LONG < 8 # define HYPER_TO_TYPE(_type, _to, _from) \ diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c new file mode 100644 index 0000000000..0662cbad14 --- /dev/null +++ b/src/remote/remote_sockets.c @@ -0,0 +1,39 @@ +/* + * remote_sockets.c: helpers for getting remote driver socket paths + * + * Copyright (C) 2007-2019 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include "remote_sockets.h" + +VIR_ENUM_IMPL(remoteDriverTransport, + REMOTE_DRIVER_TRANSPORT_LAST, + "tls", + "unix", + "ssh", + "libssh2", + "ext", + "tcp", + "libssh"); + +VIR_ENUM_IMPL(remoteDriverMode, + REMOTE_DRIVER_MODE_LAST, + "auto", + "legacy", + "direct"); diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h new file mode 100644 index 0000000000..1d4ae3f9c1 --- /dev/null +++ b/src/remote/remote_sockets.h @@ -0,0 +1,50 @@ +/* + * remote_sockets.h: helpers for getting remote driver socket paths + * + * Copyright (C) 2007-2020 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#pragma once + +#include "virenum.h" + +typedef enum { + REMOTE_DRIVER_TRANSPORT_TLS, + REMOTE_DRIVER_TRANSPORT_UNIX, + REMOTE_DRIVER_TRANSPORT_SSH, + REMOTE_DRIVER_TRANSPORT_LIBSSH2, + REMOTE_DRIVER_TRANSPORT_EXT, + REMOTE_DRIVER_TRANSPORT_TCP, + REMOTE_DRIVER_TRANSPORT_LIBSSH, + + REMOTE_DRIVER_TRANSPORT_LAST, +} remoteDriverTransport; + +VIR_ENUM_DECL(remoteDriverTransport); + +typedef enum { + /* Try to figure out the "best" choice magically */ + REMOTE_DRIVER_MODE_AUTO, + /* Always use the legacy libvirtd */ + REMOTE_DRIVER_MODE_LEGACY, + /* Always use the per-driver virt*d daemons */ + REMOTE_DRIVER_MODE_DIRECT, + + REMOTE_DRIVER_MODE_LAST +} remoteDriverMode; + +VIR_ENUM_DECL(remoteDriverMode); --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319827; cv=none; d=zohomail.com; s=zohoarc; b=hgA/B2yFwNlaswSDPeCk64RKz8L2syCb5gEH+QoGxPqBkzYewg/ebN3Xio7DBGEmjLYjNWb8mkS/1CcCRhA3RdjWOgyoPMPVkzqINa6LDDowCoH8Y9gKwvGLNN3tYi88tRzqTKG63BULLFxkppW2kUOMzR0XP3u+J47eZcuEoV8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319827; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=q6/6RsA3gbymAXH23aQbdE0S3FBHTl785yagJbDm+vQ=; b=iZS2AAaDitGcNwSr35YuDbmq7vLty4N14qJaovp++aK09WhQSYuHFv35YLiAOjd5QKJb5tFFTZ5TE15s44DcST+Kgja0n8Hj5Tq4PdCWXVZChbz4o4TpqSozWkgO9gzih53BAyxuv4/TLWw5OTIje8WJpa14iVdnw57bjrLqEJ0= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594319827983337.65260522165624; Thu, 9 Jul 2020 11:37:07 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-102-JmhxLHJDPnWNmYAYR8Wt4g-1; Thu, 09 Jul 2020 14:37:04 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 91852107B7EB; Thu, 9 Jul 2020 18:36:58 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6F0EB19D7E; Thu, 9 Jul 2020 18:36:58 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 3C1C084343; Thu, 9 Jul 2020 18:36:58 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Iauca023353 for ; Thu, 9 Jul 2020 14:36:56 -0400 Received: by smtp.corp.redhat.com (Postfix) id A70D060C80; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 6CF5560F8D; Thu, 9 Jul 2020 18:36:54 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319826; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=q6/6RsA3gbymAXH23aQbdE0S3FBHTl785yagJbDm+vQ=; b=fHzCUfQAQfmL8bbMx+KZKR3riCH0/qSce8KBfGCn6VoCuLEYO5qLoJ+bkxZZ2+cG1RCr2Y YbtevBG5hxX+AVZyep1iS0iyB/jGbac1W4DZLpr7JHHnU6LAccQ17B8GRTXLnK26QCELyZ P5hMXzpLCcGXP+YY/awBTRIkAxRdELI= X-MC-Unique: JmhxLHJDPnWNmYAYR8Wt4g-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 3/9] remote: split out function for parsing URI scheme Date: Thu, 9 Jul 2020 19:36:40 +0100 Message-Id: <20200709183646.4016586-4-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The remoteSplitURISCheme method will be needed by source files beyond the remote driver client. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/remote/remote_driver.c | 25 ------------------------- src/remote/remote_sockets.c | 28 ++++++++++++++++++++++++++++ src/remote/remote_sockets.h | 6 ++++++ 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index 880fce6e62..b84b72522a 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -165,31 +165,6 @@ static void make_nonnull_domain_snapshot(remote_nonnul= l_domain_snapshot *snapsho /*----------------------------------------------------------------------*/ =20 /* Helper functions for remoteOpen. */ -static int remoteSplitURIScheme(virURIPtr uri, - char **driver, - char **transport) -{ - char *p =3D strchr(uri->scheme, '+'); - - *driver =3D *transport =3D NULL; - - if (p) - *driver =3D g_strndup(uri->scheme, p - uri->scheme); - else - *driver =3D g_strdup(uri->scheme); - - if (p) { - *transport =3D g_strdup(p + 1); - - p =3D *transport; - while (*p) { - *p =3D g_ascii_tolower(*p); - p++; - } - } - - return 0; -} =20 =20 static int diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 0662cbad14..976124d0ed 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -37,3 +37,31 @@ VIR_ENUM_IMPL(remoteDriverMode, "auto", "legacy", "direct"); + + +int +remoteSplitURIScheme(virURIPtr uri, + char **driver, + char **transport) +{ + char *p =3D strchr(uri->scheme, '+'); + + *driver =3D *transport =3D NULL; + + if (p) + *driver =3D g_strndup(uri->scheme, p - uri->scheme); + else + *driver =3D g_strdup(uri->scheme); + + if (p) { + *transport =3D g_strdup(p + 1); + + p =3D *transport; + while (*p) { + *p =3D g_ascii_tolower(*p); + p++; + } + } + + return 0; +} diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h index 1d4ae3f9c1..bef3cdada9 100644 --- a/src/remote/remote_sockets.h +++ b/src/remote/remote_sockets.h @@ -21,6 +21,7 @@ #pragma once =20 #include "virenum.h" +#include "viruri.h" =20 typedef enum { REMOTE_DRIVER_TRANSPORT_TLS, @@ -48,3 +49,8 @@ typedef enum { } remoteDriverMode; =20 VIR_ENUM_DECL(remoteDriverMode); + +int +remoteSplitURIScheme(virURIPtr uri, + char **driver, + char **transport); --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) client-ip=205.139.110.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319829; cv=none; d=zohomail.com; s=zohoarc; b=dpWaKXgek0egQ3d77BAXSHEC6Vdhl93AMZwtt+6sH656YTIZdNpX/S0M/KHjnbhnGjI1h4uXsiOYHUUpVXtZtFzVAirHgEtAs10uVYT7jCXMg8eO6G8zZQf0ZPT8nmWDld1yhfezDnQuh+a0xT4l8nKKzhvX/JXQthQOMlDdHR4= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319829; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=OLSI36oaCchqtUXMLHudMgjVAV6+qEY8QrNpGqDz8q4=; b=hpiRPXwkg3k8L51PUBCljeADDc8/+DlVm+hXScoSGYgbOWimjQ/xTvZUrfZ3BS0TP5yLN2ygTha/oTBzLCSOAT3XU63Qn+B+Uiz6a9LnJkrdQJZO3iWTVL4eS8EbwqID6sH76sbNPsywz0JpxLqBSLRlPwNX3DrcRrUJqT+p/+M= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.120]) by mx.zohomail.com with SMTPS id 1594319829285229.6751752136281; Thu, 9 Jul 2020 11:37:09 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-434-3BDUAILlN5SOU4yA1c_1NA-1; Thu, 09 Jul 2020 14:37:05 -0400 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id BEF7180040A; Thu, 9 Jul 2020 18:36:59 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9EF0B6FEE8; Thu, 9 Jul 2020 18:36:59 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 6F9C984358; Thu, 9 Jul 2020 18:36:59 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069IawDO023365 for ; Thu, 9 Jul 2020 14:36:58 -0400 Received: by smtp.corp.redhat.com (Postfix) id 3C4E061100; Thu, 9 Jul 2020 18:36:58 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 21D3860E1C; Thu, 9 Jul 2020 18:36:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319828; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=OLSI36oaCchqtUXMLHudMgjVAV6+qEY8QrNpGqDz8q4=; b=dkkhh/sjRCVHPJDzy5rIzX6Lh5IFl6RQ8tXth/EFxxGa1qe71rVwyVsUuUsUKENE0LcQ+u xQO+zPlLtW2xIrInWQ3YUse+6r/SFKvyIVxxTmZN9yuwNLj6bkbPZBEpP0QbqYCY5DYipH tHTBAcYDd46EF3/65s0OLlZSCgLWoVs= X-MC-Unique: 3BDUAILlN5SOU4yA1c_1NA-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 4/9] remote: parse the remote transport string earlier Date: Thu, 9 Jul 2020 19:36:41 +0100 Message-Id: <20200709183646.4016586-5-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) We delay converting the remote transport string to enum form until fairly late. As a result we're doing string comparisons when we could be just doing enum comparisons. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- po/POTFILES.in | 1 + src/remote/remote_driver.c | 51 ++++++++++--------------------------- src/remote/remote_sockets.c | 35 +++++++++++++++++++++---- src/remote/remote_sockets.h | 2 +- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/po/POTFILES.in b/po/POTFILES.in index af52054aa4..8fd391a63a 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -180,6 +180,7 @@ @SRCDIR@/src/remote/remote_daemon_dispatch.c @SRCDIR@/src/remote/remote_daemon_stream.c @SRCDIR@/src/remote/remote_driver.c +@SRCDIR@/src/remote/remote_sockets.c @SRCDIR@/src/rpc/virkeepalive.c @SRCDIR@/src/rpc/virnetclient.c @SRCDIR@/src/rpc/virnetclientprogram.c diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index b84b72522a..c39085951e 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -863,12 +863,11 @@ static int doRemoteOpen(virConnectPtr conn, struct private_data *priv, const char *driver_str, - const char *transport_str, + remoteDriverTransport transport, virConnectAuthPtr auth G_GNUC_UNUSED, virConfPtr conf, unsigned int flags) { - int transport; #ifndef WIN32 g_autofree char *daemonPath =3D NULL; #endif @@ -903,34 +902,6 @@ doRemoteOpen(virConnectPtr conn, /* We handle *ALL* URIs here. The caller has rejected any * URIs we don't care about */ =20 - if (conn->uri) { - if (!transport_str) { - if (conn->uri->server) - transport =3D REMOTE_DRIVER_TRANSPORT_TLS; - else - transport =3D REMOTE_DRIVER_TRANSPORT_UNIX; - } else { - if ((transport =3D remoteDriverTransportTypeFromString(transpo= rt_str)) < 0) { - virReportError(VIR_ERR_INVALID_ARG, "%s", - _("remote_open: transport in URL not recogn= ised " - "(should be tls|unix|ssh|ext|tcp|libssh2|= libssh)")); - return VIR_DRV_OPEN_ERROR; - } - - if (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX && - conn->uri->server) { - virReportError(VIR_ERR_INVALID_ARG, - _("using unix socket and remote " - "server '%s' is not supported."), - conn->uri->server); - return VIR_DRV_OPEN_ERROR; - } - } - } else { - /* No URI, then must be probing so use UNIX socket */ - transport =3D REMOTE_DRIVER_TRANSPORT_UNIX; - } - /* Remote server defaults to "localhost" if not specified. */ if (conn->uri && conn->uri->port !=3D 0) { port =3D g_strdup_printf("%d", conn->uri->port); @@ -1352,11 +1323,16 @@ remoteConnectOpen(virConnectPtr conn, int rflags =3D 0; const char *autostart =3D getenv("LIBVIRT_AUTOSTART"); char *driver =3D NULL; - char *transport =3D NULL; + remoteDriverTransport transport; + + if (conn->uri) { + if (remoteSplitURIScheme(conn->uri, &driver, &transport) < 0) + goto cleanup; + } else { + /* No URI, then must be probing so use UNIX socket */ + transport =3D REMOTE_DRIVER_TRANSPORT_UNIX; + } =20 - if (conn->uri && - remoteSplitURIScheme(conn->uri, &driver, &transport) < 0) - goto cleanup; =20 if (inside_daemon) { if (!conn->uri) { @@ -1398,12 +1374,12 @@ remoteConnectOpen(virConnectPtr conn, rflags |=3D VIR_DRV_OPEN_REMOTE_USER; =20 /* - * Furthermore if no servername is given, and no +XXX - * transport is listed, or transport is unix, + * Furthermore if no servername is given, + * and the transport is unix, * and uid is unprivileged then auto-spawn a daemon. */ if (!conn->uri->server && - (transport =3D=3D NULL || STREQ(transport, "unix")) && + (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX) && (!autostart || STRNEQ(autostart, "0"))) { VIR_DEBUG("Try daemon autostart"); @@ -1438,7 +1414,6 @@ remoteConnectOpen(virConnectPtr conn, =20 cleanup: VIR_FREE(driver); - VIR_FREE(transport); return ret; } =20 diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 976124d0ed..cdc0a00293 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -21,6 +21,9 @@ #include =20 #include "remote_sockets.h" +#include "virerror.h" + +#define VIR_FROM_THIS VIR_FROM_REMOTE =20 VIR_ENUM_IMPL(remoteDriverTransport, REMOTE_DRIVER_TRANSPORT_LAST, @@ -42,25 +45,47 @@ VIR_ENUM_IMPL(remoteDriverMode, int remoteSplitURIScheme(virURIPtr uri, char **driver, - char **transport) + remoteDriverTransport *transport) { char *p =3D strchr(uri->scheme, '+'); =20 - *driver =3D *transport =3D NULL; - if (p) *driver =3D g_strndup(uri->scheme, p - uri->scheme); else *driver =3D g_strdup(uri->scheme); =20 if (p) { - *transport =3D g_strdup(p + 1); + g_autofree char *tmp =3D g_strdup(p + 1); + int val; =20 - p =3D *transport; + p =3D tmp; while (*p) { *p =3D g_ascii_tolower(*p); p++; } + + if ((val =3D remoteDriverTransportTypeFromString(tmp)) < 0) { + virReportError(VIR_ERR_INVALID_ARG, "%s", + _("remote_open: transport in URL not recognised= " + "(should be tls|unix|ssh|ext|tcp|libssh2|libs= sh)")); + return -1; + } + + if (val =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX && + uri->server) { + virReportError(VIR_ERR_INVALID_ARG, + _("using unix socket and remote " + "server '%s' is not supported."), + uri->server); + return -1; + } + + *transport =3D val; + } else { + if (uri->server) + *transport =3D REMOTE_DRIVER_TRANSPORT_TLS; + else + *transport =3D REMOTE_DRIVER_TRANSPORT_UNIX; } =20 return 0; diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h index bef3cdada9..ade3feab88 100644 --- a/src/remote/remote_sockets.h +++ b/src/remote/remote_sockets.h @@ -53,4 +53,4 @@ VIR_ENUM_DECL(remoteDriverMode); int remoteSplitURIScheme(virURIPtr uri, char **driver, - char **transport); + remoteDriverTransport *transport); --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319837; cv=none; d=zohomail.com; s=zohoarc; b=VIrUo8do/1nON3tqYyBNx+MfWiYrmCUyeZHG1Sg96S3cx9Casc2z9nY/oZwudGDX0F6amBnNNZSpwsGi58AkFygtITa31aLf7O7PptoVrBTd9tATkqHo8aqwHxWzyQh0PBDMhgv9c6E5FrOJTU/UyMs6QlLaTQb4GxjH3HXoSPM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319837; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=yhzMGDbATKkt8N22P5j3dHrjL1luyMsCNHHKWf+Q23c=; b=HbuB+vsfblTcbnMOJKYc7EfYjwl23E0Qp+/Ch3R6MgeWhtfGJjywMEg6TqwPUpwGpc8Kyi2mAW132EE4M9fO79rYYs0bDzWxXQ/RQtuBI+IVACq5tLbnGk5u5PcO47vFyY2GKVFsLvRXEtseVCCsvg6nkShOdaWnD/vwpLXZz1w= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594319837543705.376212374758; Thu, 9 Jul 2020 11:37:17 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-70-jRMDxqfVNKyhz3FqGLcz2g-1; Thu, 09 Jul 2020 14:37:09 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 31BC48015F3; Thu, 9 Jul 2020 18:37:02 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 105B879CF5; Thu, 9 Jul 2020 18:37:02 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id D03E38435B; Thu, 9 Jul 2020 18:37:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069IaxrI023383 for ; Thu, 9 Jul 2020 14:36:59 -0400 Received: by smtp.corp.redhat.com (Postfix) id AF38060F8D; Thu, 9 Jul 2020 18:36:59 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id A477660E1C; Thu, 9 Jul 2020 18:36:58 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319836; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=yhzMGDbATKkt8N22P5j3dHrjL1luyMsCNHHKWf+Q23c=; b=ZHcIdLWu1cQQBgQoUm9SkgzA69pGQrQAgOhXG71vJN+A+7gDiGgnU3fd+7p1EnYd5orRN0 zh5H4uQQcbzgzJIGhZToC+/LsBXuk6jczVOzDhqq0LTFqe3ravGrZZPdxKbaCMPHnh/vvL 7vFx9zOn6+HIyQd2HoXuHVFJqIwCQ9A= X-MC-Unique: jRMDxqfVNKyhz3FqGLcz2g-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 5/9] remote: split out function for constructing socket path Date: Thu, 9 Jul 2020 19:36:42 +0100 Message-Id: <20200709183646.4016586-6-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) The remoteGetUNIXSocketHelper method will be needed by source files beyond the remote driver client. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/remote/remote_driver.c | 129 +--------------------------------- src/remote/remote_sockets.c | 134 ++++++++++++++++++++++++++++++++++++ src/remote/remote_sockets.h | 8 +++ 3 files changed, 145 insertions(+), 126 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index c39085951e..c7fd24625e 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -697,131 +697,6 @@ remoteConnectSupportsFeatureUnlocked(virConnectPtr co= nn, } =20 =20 -static char * -remoteGetUNIXSocketHelper(remoteDriverTransport transport, - const char *sock_prefix, - unsigned int flags) -{ - char *sockname =3D NULL; - g_autofree char *userdir =3D NULL; - - if (flags & VIR_DRV_OPEN_REMOTE_USER) { - if (transport !=3D 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; - } - userdir =3D virGetUserRuntimeDirectory(); - - sockname =3D g_strdup_printf("%s/%s-sock", userdir, sock_prefix); - } else { - /* Intentionally do *NOT* use RUNSTATEDIR here. We might - * be connecting to a remote machine, and cannot assume - * the remote host has /run. The converse is ok though, - * any machine with /run will have a /var/run symlink. - * The portable option is to thus use $LOCALSTATEDIR/run - */ - sockname =3D g_strdup_printf("%s/run/libvirt/%s-%s", LOCALSTATEDIR, - sock_prefix, - flags & VIR_DRV_OPEN_REMOTE_RO ? "sock-= ro" : "sock"); - } - - VIR_DEBUG("Built UNIX sockname %s for transport %s prefix %s flags=3D0= x%x", - sockname, remoteDriverTransportTypeToString(transport), - sock_prefix, flags); - return sockname; -} - - -static char * -remoteGetUNIXSocket(remoteDriverTransport transport, - remoteDriverMode mode, - const char *driver, - char **daemon, - unsigned int flags) -{ - char *sock_name =3D NULL; - g_autofree char *direct_daemon =3D NULL; - g_autofree char *legacy_daemon =3D NULL; - g_autofree char *direct_sock_name =3D NULL; - g_autofree char *legacy_sock_name =3D NULL; - - if (driver) - direct_daemon =3D g_strdup_printf("virt%sd", driver); - - legacy_daemon =3D g_strdup("libvirtd"); - - if (driver && - !(direct_sock_name =3D remoteGetUNIXSocketHelper(transport, direct= _daemon, flags))) - return NULL; - - if (!(legacy_sock_name =3D remoteGetUNIXSocketHelper(transport, "libvi= rt", flags))) - return NULL; - - if (mode =3D=3D REMOTE_DRIVER_MODE_AUTO) { - if (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX) { - if (direct_sock_name && virFileExists(direct_sock_name)) { - mode =3D REMOTE_DRIVER_MODE_DIRECT; - } else if (virFileExists(legacy_sock_name)) { - mode =3D REMOTE_DRIVER_MODE_LEGACY; - } else if (driver) { - /* - * This constant comes from the configure script and - * maps to either the direct or legacy mode constant - */ - mode =3D REMOTE_DRIVER_MODE_DEFAULT; - } else { - mode =3D REMOTE_DRIVER_MODE_LEGACY; - } - } else { - mode =3D REMOTE_DRIVER_MODE_LEGACY; - } - } - - switch ((remoteDriverMode)mode) { - case REMOTE_DRIVER_MODE_LEGACY: - sock_name =3D g_steal_pointer(&legacy_sock_name); - *daemon =3D g_steal_pointer(&legacy_daemon); - break; - - case REMOTE_DRIVER_MODE_DIRECT: - if (transport !=3D REMOTE_DRIVER_TRANSPORT_UNIX) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, - _("Cannot use direct socket mode for %s transpo= rt"), - remoteDriverTransportTypeToString(transport)); - return NULL; - } - - if (!direct_sock_name) { - virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", - _("Cannot use direct socket mode if no URI is s= et")); - return NULL; - } - - sock_name =3D g_steal_pointer(&direct_sock_name); - *daemon =3D g_steal_pointer(&direct_daemon); - break; - - case REMOTE_DRIVER_MODE_AUTO: - case REMOTE_DRIVER_MODE_LAST: - default: - virReportEnumRangeError(remoteDriverMode, mode); - return NULL; - } - - VIR_DEBUG("Chosen UNIX sockname %s daemon %s " - "for mode %s transport %s flags=3D0x%x", - sock_name, NULLSTR(*daemon), - remoteDriverModeTypeToString(mode), - remoteDriverTransportTypeToString(transport), - flags); - return sock_name; -} - - #ifndef WIN32 static const char * remoteGetDaemonPathEnv(void) @@ -1015,7 +890,9 @@ doRemoteOpen(virConnectPtr conn, case REMOTE_DRIVER_TRANSPORT_LIBSSH2: if (!sockname && !(sockname =3D remoteGetUNIXSocket(transport, mode, driver_str, - &daemon_name, flags))) + flags & VIR_DRV_OPEN_REMOTE_R= O, + flags & VIR_DRV_OPEN_REMOTE_U= SER, + &daemon_name))) goto failed; break; =20 diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index cdc0a00293..28e02e24d5 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -22,9 +22,15 @@ =20 #include "remote_sockets.h" #include "virerror.h" +#include "virlog.h" +#include "virfile.h" +#include "virutil.h" +#include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_REMOTE =20 +VIR_LOG_INIT("remote.remote_sockets"); + VIR_ENUM_IMPL(remoteDriverTransport, REMOTE_DRIVER_TRANSPORT_LAST, "tls", @@ -90,3 +96,131 @@ remoteSplitURIScheme(virURIPtr uri, =20 return 0; } + + +static char * +remoteGetUNIXSocketHelper(remoteDriverTransport transport, + const char *sock_prefix, + bool ro, + bool session) +{ + char *sockname =3D NULL; + g_autofree char *userdir =3D NULL; + + if (session) { + if (transport !=3D 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; + } + userdir =3D virGetUserRuntimeDirectory(); + + sockname =3D g_strdup_printf("%s/%s-sock", userdir, sock_prefix); + } else { + /* Intentionally do *NOT* use RUNSTATEDIR here. We might + * be connecting to a remote machine, and cannot assume + * the remote host has /run. The converse is ok though, + * any machine with /run will have a /var/run symlink. + * The portable option is to thus use $LOCALSTATEDIR/run + */ + sockname =3D g_strdup_printf("%s/run/libvirt/%s-%s", LOCALSTATEDIR, + sock_prefix, + ro ? "sock-ro" : "sock"); + } + + VIR_DEBUG("Built UNIX sockname=3D%s for transport=3D%s " + "prefix=3D%s ro=3D%d session=3D%d", + sockname, remoteDriverTransportTypeToString(transport), + sock_prefix, ro, session); + return sockname; +} + + +char * +remoteGetUNIXSocket(remoteDriverTransport transport, + remoteDriverMode mode, + const char *driver, + bool ro, + bool session, + char **daemon) +{ + char *sock_name =3D NULL; + g_autofree char *direct_daemon =3D NULL; + g_autofree char *legacy_daemon =3D NULL; + g_autofree char *direct_sock_name =3D NULL; + g_autofree char *legacy_sock_name =3D NULL; + + if (driver) + direct_daemon =3D g_strdup_printf("virt%sd", driver); + + legacy_daemon =3D g_strdup("libvirtd"); + + if (driver && + !(direct_sock_name =3D remoteGetUNIXSocketHelper(transport, direct= _daemon, ro, session))) + return NULL; + + if (!(legacy_sock_name =3D remoteGetUNIXSocketHelper(transport, "libvi= rt", ro, session))) + return NULL; + + if (mode =3D=3D REMOTE_DRIVER_MODE_AUTO) { + if (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX) { + if (direct_sock_name && virFileExists(direct_sock_name)) { + mode =3D REMOTE_DRIVER_MODE_DIRECT; + } else if (virFileExists(legacy_sock_name)) { + mode =3D REMOTE_DRIVER_MODE_LEGACY; + } else if (driver) { + /* + * This constant comes from the configure script and + * maps to either the direct or legacy mode constant + */ + mode =3D REMOTE_DRIVER_MODE_DEFAULT; + } else { + mode =3D REMOTE_DRIVER_MODE_LEGACY; + } + } else { + mode =3D REMOTE_DRIVER_MODE_LEGACY; + } + } + + switch ((remoteDriverMode)mode) { + case REMOTE_DRIVER_MODE_LEGACY: + sock_name =3D g_steal_pointer(&legacy_sock_name); + *daemon =3D g_steal_pointer(&legacy_daemon); + break; + + case REMOTE_DRIVER_MODE_DIRECT: + if (transport !=3D REMOTE_DRIVER_TRANSPORT_UNIX) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, + _("Cannot use direct socket mode for %s transpo= rt"), + remoteDriverTransportTypeToString(transport)); + return NULL; + } + + if (!direct_sock_name) { + virReportError(VIR_ERR_OPERATION_UNSUPPORTED, "%s", + _("Cannot use direct socket mode if no URI is s= et")); + return NULL; + } + + sock_name =3D g_steal_pointer(&direct_sock_name); + *daemon =3D g_steal_pointer(&direct_daemon); + break; + + case REMOTE_DRIVER_MODE_AUTO: + case REMOTE_DRIVER_MODE_LAST: + default: + virReportEnumRangeError(remoteDriverMode, mode); + return NULL; + } + + VIR_DEBUG("Chosen UNIX sockname=3D%s daemon=3D%s " + "for mode=3D%s transport=3D%s ro=3D%d session=3D%d", + sock_name, NULLSTR(*daemon), + remoteDriverModeTypeToString(mode), + remoteDriverTransportTypeToString(transport), + ro, session); + return sock_name; +} diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h index ade3feab88..64055f3d44 100644 --- a/src/remote/remote_sockets.h +++ b/src/remote/remote_sockets.h @@ -54,3 +54,11 @@ int remoteSplitURIScheme(virURIPtr uri, char **driver, remoteDriverTransport *transport); + +char * +remoteGetUNIXSocket(remoteDriverTransport transport, + remoteDriverMode mode, + const char *driver, + bool ro, + bool session, + char **daemon); --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) client-ip=205.139.110.61; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319836; cv=none; d=zohomail.com; s=zohoarc; b=AaE4DFx/+D44BU79rdQeYJezo4yE5BtC82kJZxPH6itG5PoY60diyiVcFBXZbtHSQjrBmELnlAgYDroXa9TLh2YxrtXiP8d3/v4z01DzuQ8JX20kcfehHH/tI+x2mtys6j9T+76ZiVsyx7X4el2MbFOMKa1SWy8E4nWIxJEehe0= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319836; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=owxKp2VKEEWwvNKEUmVe7ZZGgXfli1skJ6x+E0WNusY=; b=cpqIoGd2vKdz+iMcretAO935cfsfUNdPsXf1ih4sYofVM3knGq0RdRZ+Uy/RU+7818+ruMATy5ip9iuJJacQiHZj8Cy2RhHKqOw/+TzrQDmlC7RGGzg9RuYxnZY0c1f/ABp/IOBvv8qhsbNbmNxiH3VemskxdXqJyB9ymFl627o= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 205.139.110.61 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-1.mimecast.com [205.139.110.61]) by mx.zohomail.com with SMTPS id 1594319836138607.6920793702487; Thu, 9 Jul 2020 11:37:16 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-62-Tj-wtjkJNX2GgoZb81TNNg-1; Thu, 09 Jul 2020 14:37:11 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 4EDF6855AC4; Thu, 9 Jul 2020 18:37:06 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2B78B79CF1; Thu, 9 Jul 2020 18:37:06 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id F17041809557; Thu, 9 Jul 2020 18:37:05 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Ib49k023407 for ; Thu, 9 Jul 2020 14:37:04 -0400 Received: by smtp.corp.redhat.com (Postfix) id BE8FD60C80; Thu, 9 Jul 2020 18:37:04 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 424A160F8D; Thu, 9 Jul 2020 18:37:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319835; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=owxKp2VKEEWwvNKEUmVe7ZZGgXfli1skJ6x+E0WNusY=; b=D5HVkK53+GY5oaDHr7T+Fco1f4D1TBXmc36vQFufZ3sTZKtijI6EucFB1tfNoklFVCndII M4kRQScHBDX5OR4KYFN6pUn4n9uwyj0SOWO38b0tPldquzTAp47kvHYPlWtZcoawFyR+sq 1DCarL3B+tWNsjC1U8nbfSxktzoMuGg= X-MC-Unique: Tj-wtjkJNX2GgoZb81TNNg-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 6/9] remote: extract logic for determining daemon to connect to Date: Thu, 9 Jul 2020 19:36:43 +0100 Message-Id: <20200709183646.4016586-7-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) We'll shortly want to reuse code for determining whether to connect to the system or session daemon from places outside the remote driver client. Pulling it out into a self contained function facilitates reuse. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/remote/remote_driver.c | 51 ++++---------------------------- src/remote/remote_sockets.c | 59 +++++++++++++++++++++++++++++++++++++ src/remote/remote_sockets.h | 6 ++++ 3 files changed, 71 insertions(+), 45 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index c7fd24625e..c2dcf20f91 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -1198,7 +1198,8 @@ remoteConnectOpen(virConnectPtr conn, struct private_data *priv; int ret =3D VIR_DRV_OPEN_ERROR; int rflags =3D 0; - const char *autostart =3D getenv("LIBVIRT_AUTOSTART"); + bool user; + bool autostart; char *driver =3D NULL; remoteDriverTransport transport; =20 @@ -1233,51 +1234,11 @@ remoteConnectOpen(virConnectPtr conn, if (flags & VIR_CONNECT_RO) rflags |=3D VIR_DRV_OPEN_REMOTE_RO; =20 - /* - * User session daemon is used for - * - * - Any URI with /session suffix - * - Test driver, if a protocol is given - * - * provided we are running non-root - */ - if (conn->uri && - conn->uri->path && - conn->uri->scheme && - (STREQ(conn->uri->path, "/session") || - STRPREFIX(conn->uri->scheme, "test+")) && - geteuid() > 0) { - VIR_DEBUG("User session daemon required"); + remoteGetURIDaemonInfo(conn->uri, transport, &user, &autostart); + if (user) rflags |=3D VIR_DRV_OPEN_REMOTE_USER; - - /* - * Furthermore if no servername is given, - * and the transport is unix, - * and uid is unprivileged then auto-spawn a daemon. - */ - if (!conn->uri->server && - (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX) && - (!autostart || - STRNEQ(autostart, "0"))) { - VIR_DEBUG("Try daemon autostart"); - rflags |=3D VIR_DRV_OPEN_REMOTE_AUTOSTART; - } - } - - /* - * If URI is NULL, then do a UNIX connection possibly auto-spawning - * unprivileged server and probe remote server for URI. - */ - if (!conn->uri) { - VIR_DEBUG("Auto-probe remote URI"); - if (geteuid() > 0) { - VIR_DEBUG("Auto-spawn user daemon instance"); - rflags |=3D VIR_DRV_OPEN_REMOTE_USER; - if (!autostart || - STRNEQ(autostart, "0")) - rflags |=3D VIR_DRV_OPEN_REMOTE_AUTOSTART; - } - } + if (autostart) + rflags |=3D VIR_DRV_OPEN_REMOTE_AUTOSTART; =20 ret =3D doRemoteOpen(conn, priv, driver, transport, auth, conf, rflags= ); if (ret !=3D VIR_DRV_OPEN_SUCCESS) { diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 28e02e24d5..854775f401 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -224,3 +224,62 @@ remoteGetUNIXSocket(remoteDriverTransport transport, ro, session); return sock_name; } + + +void +remoteGetURIDaemonInfo(virURIPtr uri, + remoteDriverTransport transport, + bool *session, + bool *autostart) +{ + const char *autostart_str =3D getenv("LIBVIRT_AUTOSTART"); + + *session =3D false; + *autostart =3D false; + + /* + * User session daemon is used for + * + * - Any URI with /session suffix + * - Test driver, if a protocol is given + * + * provided we are running non-root + */ + if (uri && + uri->path && + uri->scheme && + (STREQ(uri->path, "/session") || + STRPREFIX(uri->scheme, "test+")) && + geteuid() > 0) { + VIR_DEBUG("User session daemon required"); + *session =3D true; + + /* + * Furthermore if no servername is given, + * and the transport is unix, + * and uid is unprivileged then auto-spawn a daemon. + */ + if (!uri->server && + (transport =3D=3D REMOTE_DRIVER_TRANSPORT_UNIX) && + (!autostart_str || + STRNEQ(autostart_str, "0"))) { + VIR_DEBUG("Try daemon autostart"); + *autostart =3D true; + } + } + + /* + * If URI is NULL, then do a UNIX connection possibly auto-spawning + * unprivileged server and probe remote server for URI. + */ + if (!uri) { + VIR_DEBUG("Auto-probe remote URI"); + if (geteuid() > 0) { + VIR_DEBUG("Auto-spawn user daemon instance"); + *session =3D true; + if (!autostart_str || + STRNEQ(autostart_str, "0")) + *autostart =3D true; + } + } +} diff --git a/src/remote/remote_sockets.h b/src/remote/remote_sockets.h index 64055f3d44..7526752835 100644 --- a/src/remote/remote_sockets.h +++ b/src/remote/remote_sockets.h @@ -62,3 +62,9 @@ remoteGetUNIXSocket(remoteDriverTransport transport, bool ro, bool session, char **daemon); + +void +remoteGetURIDaemonInfo(virURIPtr uri, + remoteDriverTransport transport, + bool *session, + bool *autostart); --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319839; cv=none; d=zohomail.com; s=zohoarc; b=l+jMITltuH46AGGOoKmEmsU7QxvRVUYM4U1NoH+k3dUuAioX/dBUxyoZo84U43OIGTW4HEmHw290EEs+7uoY9yfRu3R8r7kHu+GgrpX4oTsqYJuwXKQu+/Bt/FOUcdC7rQkNTUNsbgo1LcBXpyW62ydCVY50+80QW096m+uRUYY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319839; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=9gyIBViqYYAZrbldjgIkUDWuRRpCIpZAm18qz3kokT8=; b=VOwaRfBXdqBqLrrE1TeK3zo0keJIYu0czedQES+lkJroklg6SR2T5idiw9hO6FBfIncmc7a+VQanWJOlUlVvyKjYU9vM983Ri+kmO3rQmgAG0ghBgmSU8ZJFitELcKkY0R9GvQzX7eaWHDl0PqbcJ/ApwpIrzS5NlyD0+JCeAqg= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1594319839493573.9842482836591; Thu, 9 Jul 2020 11:37:19 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-387-YQ5ZAKF2MlKTuazVaF0anQ-1; Thu, 09 Jul 2020 14:37:15 -0400 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id B237F855AD1; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8BECE60C80; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 5CB648435E; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Ib6b9023452 for ; Thu, 9 Jul 2020 14:37:06 -0400 Received: by smtp.corp.redhat.com (Postfix) id 2AE5560C80; Thu, 9 Jul 2020 18:37:06 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 387BF610F3; Thu, 9 Jul 2020 18:37:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319838; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=9gyIBViqYYAZrbldjgIkUDWuRRpCIpZAm18qz3kokT8=; b=VyjfUEm1Edomu4CoGcf89A3G4qCGuOFae/m/iNe8iBRa8Iy4PyGvz5B2EYdt2lfPkbAVje xCkaqx3cfQKmk2fUS8IfU2uOPbN2WH8xwcNmm8UMDjnfhwEdX6bfMZg4/soGeY2UpDTHvZ QNtBk+Y0QKOrMZNu+pJbl3hlTp8vdxQ= X-MC-Unique: YQ5ZAKF2MlKTuazVaF0anQ-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 7/9] remote: introduce virtd-nc helper binary Date: Thu, 9 Jul 2020 19:36:44 +0100 Message-Id: <20200709183646.4016586-8-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) When accessing libvirtd over a SSH tunnel, the remote driver must spawn the remote 'nc' process, pointing it to the libvirtd socket path. This is problematic for a number of reasons: - The socket path varies according to the --prefix chosen at build time. The remote client is seeing the local prefix, but what we need is the remote prefix - The socket path varies according to remote env variables, such as the XDG_RUNTIME_DIR location. Again we see the local XDG_RUNTIME_DIR value, but what we need is the remote value (if any) - We can not able to autospawn the libvirtd daemon for session mode access To address these problems this patch introduces the 'virtd-nc' helper program which takes the URI for the remote driver as a CLI parameter. It then figures out the socket path to connect to using the same code as the remote driver does on the remote host. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- build-aux/syntax-check.mk | 2 +- po/POTFILES.in | 1 + src/remote/Makefile.inc.am | 30 +++ src/remote/remote_nc.c | 424 +++++++++++++++++++++++++++++++++++++ src/rpc/virnetsocket.h | 1 + 5 files changed, 457 insertions(+), 1 deletion(-) create mode 100644 src/remote/remote_nc.c diff --git a/build-aux/syntax-check.mk b/build-aux/syntax-check.mk index d47a92b530..81b307ebe8 100644 --- a/build-aux/syntax-check.mk +++ b/build-aux/syntax-check.mk @@ -1967,7 +1967,7 @@ group-qemu-caps: # List all syntax-check exemptions: exclude_file_name_regexp--sc_avoid_strcase =3D ^tools/vsh\.h$$ =20 -_src1=3Dlibvirt-stream|qemu/qemu_monitor|util/vir(command|file|fdstream)|r= pc/virnetsocket|lxc/lxc_controller|locking/lock_daemon|logging/log_daemon +_src1=3Dlibvirt-stream|qemu/qemu_monitor|util/vir(command|file|fdstream)|r= pc/virnetsocket|lxc/lxc_controller|locking/lock_daemon|logging/log_daemon|r= emote/remote_nc _test1=3Dshunloadtest|virnettlscontexttest|virnettlssessiontest|vircgroupm= ock|commandhelper exclude_file_name_regexp--sc_avoid_write =3D \ ^(src/($(_src1))|tools/virsh-console|tests/($(_test1)))\.c$$ diff --git a/po/POTFILES.in b/po/POTFILES.in index 8fd391a63a..8fa47ec276 100644 --- a/po/POTFILES.in +++ b/po/POTFILES.in @@ -180,6 +180,7 @@ @SRCDIR@/src/remote/remote_daemon_dispatch.c @SRCDIR@/src/remote/remote_daemon_stream.c @SRCDIR@/src/remote/remote_driver.c +@SRCDIR@/src/remote/remote_nc.c @SRCDIR@/src/remote/remote_sockets.c @SRCDIR@/src/rpc/virkeepalive.c @SRCDIR@/src/rpc/virnetclient.c diff --git a/src/remote/Makefile.inc.am b/src/remote/Makefile.inc.am index 0ae97f4107..2527cc193f 100644 --- a/src/remote/Makefile.inc.am +++ b/src/remote/Makefile.inc.am @@ -221,6 +221,8 @@ if WITH_LIBVIRTD =20 sbin_PROGRAMS +=3D libvirtd virtproxyd =20 +libexec_PROGRAMS +=3D virt-nc + augeas_DATA +=3D \ remote/libvirtd.aug \ remote/virtproxyd.aug \ @@ -286,6 +288,34 @@ remote/virtproxyd.conf: remote/libvirtd.conf.in -e 's/[@]DAEMON_NAME[@]/virtproxyd/' \ $< > $@ =20 +virt_nc_SOURCES =3D \ + remote/remote_sockets.h \ + remote/remote_sockets.c \ + remote/remote_nc.c \ + $(NULL) + +virt_nc_CFLAGS =3D \ + $(LIBXML_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(WARN_CFLAGS) \ + $(PIE_CFLAGS) \ + -I$(srcdir)/access \ + -I$(srcdir)/rpc \ + $(NULL) + +virt_nc_LDFLAGS =3D \ + $(RELRO_LDFLAGS) \ + $(PIE_LDFLAGS) \ + $(NO_INDIRECT_LDFLAGS) \ + $(NO_UNDEFINED_LDFLAGS) \ + $(NULL) + +virt_nc_LDADD =3D \ + libvirt.la \ + $(LIBXML_LIBS) \ + $(NULL) + + INSTALL_DATA_DIRS +=3D remote =20 install-data-remote: diff --git a/src/remote/remote_nc.c b/src/remote/remote_nc.c new file mode 100644 index 0000000000..d304db1a04 --- /dev/null +++ b/src/remote/remote_nc.c @@ -0,0 +1,424 @@ +/* + * remote_nc.c: a netcat equivalent for remote driver tunnelling + * + * Copyright (C) 2020 Red Hat, Inc. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library 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 + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + */ + +#include + +#include + +#include "virnetsocket.h" +#include "viralloc.h" +#include "virlog.h" +#include "virgettext.h" +#include "virfile.h" + +#include "remote_sockets.h" + +#define VIR_FROM_THIS VIR_FROM_REMOTE + +VIR_LOG_INIT("remote.remote_nc"); + +struct virRemoteProxyBuffer { + size_t length; + size_t offset; + char *data; +}; + +typedef struct virRemoteProxy virRemoteProxy; +typedef virRemoteProxy *virRemoteProxyPtr; +struct virRemoteProxy { + bool quit; + virNetSocketPtr sock; + int stdinWatch; + int stdoutWatch; + + struct virRemoteProxyBuffer sockToTerminal; + struct virRemoteProxyBuffer terminalToSock; +}; + + +static void +virRemoteProxyShutdown(virRemoteProxyPtr proxy) +{ + if (proxy->sock) { + virNetSocketRemoveIOCallback(proxy->sock); + virNetSocketClose(proxy->sock); + virObjectUnref(proxy->sock); + proxy->sock =3D NULL; + } + VIR_FREE(proxy->sockToTerminal.data); + VIR_FREE(proxy->terminalToSock.data); + if (proxy->stdinWatch !=3D -1) + virEventRemoveHandle(proxy->stdinWatch); + if (proxy->stdoutWatch !=3D -1) + virEventRemoveHandle(proxy->stdoutWatch); + proxy->stdinWatch =3D -1; + proxy->stdoutWatch =3D -1; + if (!proxy->quit) + proxy->quit =3D true; +} + + +static void +virRemoteProxyEventOnSocket(virNetSocketPtr sock, + int events, void *opaque) +{ + virRemoteProxyPtr proxy =3D opaque; + + /* we got late event after proxy was shutdown */ + if (!proxy->sock) + return; + + if (events & VIR_EVENT_HANDLE_READABLE) { + size_t avail =3D proxy->sockToTerminal.length - + proxy->sockToTerminal.offset; + int got; + + if (avail < 1024) { + if (VIR_REALLOC_N(proxy->sockToTerminal.data, + proxy->sockToTerminal.length + 1024) < 0) { + virRemoteProxyShutdown(proxy); + return; + } + proxy->sockToTerminal.length +=3D 1024; + avail +=3D 1024; + } + + got =3D virNetSocketRead(sock, + proxy->sockToTerminal.data + + proxy->sockToTerminal.offset, + avail); + if (got =3D=3D -2) + return; /* blocking */ + if (got =3D=3D 0) { + VIR_DEBUG("EOF on socket, shutting down"); + virRemoteProxyShutdown(proxy); + return; + } + if (got < 0) { + virRemoteProxyShutdown(proxy); + return; + } + proxy->sockToTerminal.offset +=3D got; + if (proxy->sockToTerminal.offset) + virEventUpdateHandle(proxy->stdoutWatch, + VIR_EVENT_HANDLE_WRITABLE); + } + + if (events & VIR_EVENT_HANDLE_WRITABLE && + proxy->terminalToSock.offset) { + ssize_t done; + size_t avail; + done =3D virNetSocketWrite(proxy->sock, + proxy->terminalToSock.data, + proxy->terminalToSock.offset); + if (done =3D=3D -2) + return; /* blocking */ + if (done < 0) { + virRemoteProxyShutdown(proxy); + return; + } + memmove(proxy->terminalToSock.data, + proxy->terminalToSock.data + done, + proxy->terminalToSock.offset - done); + proxy->terminalToSock.offset -=3D done; + + avail =3D proxy->terminalToSock.length - proxy->terminalToSock.off= set; + if (avail > 1024) { + ignore_value(VIR_REALLOC_N(proxy->terminalToSock.data, + proxy->terminalToSock.offset + 1024= )); + proxy->terminalToSock.length =3D proxy->terminalToSock.offset = + 1024; + } + } + if (!proxy->terminalToSock.offset) + virNetSocketUpdateIOCallback(proxy->sock, + VIR_EVENT_HANDLE_READABLE); + + if (events & VIR_EVENT_HANDLE_ERROR || + events & VIR_EVENT_HANDLE_HANGUP) { + virRemoteProxyShutdown(proxy); + } +} + + +static void +virRemoteProxyEventOnStdin(int watch G_GNUC_UNUSED, + int fd G_GNUC_UNUSED, + int events, + void *opaque) +{ + virRemoteProxyPtr proxy =3D opaque; + + /* we got late event after console was shutdown */ + if (!proxy->sock) + return; + + if (events & VIR_EVENT_HANDLE_READABLE) { + size_t avail =3D proxy->terminalToSock.length - + proxy->terminalToSock.offset; + int got; + + if (avail < 1024) { + if (VIR_REALLOC_N(proxy->terminalToSock.data, + proxy->terminalToSock.length + 1024) < 0) { + virRemoteProxyShutdown(proxy); + return; + } + proxy->terminalToSock.length +=3D 1024; + avail +=3D 1024; + } + + got =3D read(fd, + proxy->terminalToSock.data + + proxy->terminalToSock.offset, + avail); + if (got < 0) { + if (errno !=3D EAGAIN) { + virReportSystemError(errno, "%s", _("cannot read from stdi= n")); + virRemoteProxyShutdown(proxy); + } + return; + } + if (got =3D=3D 0) { + VIR_DEBUG("EOF on stdin, shutting down"); + virRemoteProxyShutdown(proxy); + return; + } + + proxy->terminalToSock.offset +=3D got; + if (proxy->terminalToSock.offset) + virNetSocketUpdateIOCallback(proxy->sock, + VIR_EVENT_HANDLE_READABLE | + VIR_EVENT_HANDLE_WRITABLE); + } + + if (events & VIR_EVENT_HANDLE_ERROR) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error on stdin"= )); + virRemoteProxyShutdown(proxy); + return; + } + + if (events & VIR_EVENT_HANDLE_HANGUP) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdin")); + virRemoteProxyShutdown(proxy); + return; + } +} + + +static void +virRemoteProxyEventOnStdout(int watch G_GNUC_UNUSED, + int fd, + int events, + void *opaque) +{ + virRemoteProxyPtr proxy =3D opaque; + + /* we got late event after console was shutdown */ + if (!proxy->sock) + return; + + if (events & VIR_EVENT_HANDLE_WRITABLE && + proxy->sockToTerminal.offset) { + ssize_t done; + size_t avail; + done =3D write(fd, + proxy->sockToTerminal.data, + proxy->sockToTerminal.offset); + if (done < 0) { + if (errno !=3D EAGAIN) { + virReportSystemError(errno, "%s", _("cannot write to stdou= t")); + virRemoteProxyShutdown(proxy); + } + return; + } + memmove(proxy->sockToTerminal.data, + proxy->sockToTerminal.data + done, + proxy->sockToTerminal.offset - done); + proxy->sockToTerminal.offset -=3D done; + + avail =3D proxy->sockToTerminal.length - proxy->sockToTerminal.off= set; + if (avail > 1024) { + ignore_value(VIR_REALLOC_N(proxy->sockToTerminal.data, + proxy->sockToTerminal.offset + 1024= )); + proxy->sockToTerminal.length =3D proxy->sockToTerminal.offset = + 1024; + } + } + + if (!proxy->sockToTerminal.offset) + virEventUpdateHandle(proxy->stdoutWatch, 0); + + if (events & VIR_EVENT_HANDLE_ERROR) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("IO error stdout")); + virRemoteProxyShutdown(proxy); + return; + } + + if (events & VIR_EVENT_HANDLE_HANGUP) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", _("EOF on stdout")); + virRemoteProxyShutdown(proxy); + return; + } +} + + +static int +virRemoteProxyRun(virNetSocketPtr sock) +{ + int ret =3D -1; + virRemoteProxy proxy =3D { + .sock =3D sock, + .stdinWatch =3D -1, + .stdoutWatch =3D -1, + }; + + virEventRegisterDefaultImpl(); + + if ((proxy.stdinWatch =3D virEventAddHandle(STDIN_FILENO, + VIR_EVENT_HANDLE_READABLE, + virRemoteProxyEventOnStdin, + &proxy, + NULL)) < 0) + goto cleanup; + + if ((proxy.stdoutWatch =3D virEventAddHandle(STDOUT_FILENO, + 0, + virRemoteProxyEventOnStdout, + &proxy, + NULL)) < 0) + goto cleanup; + + if (virNetSocketAddIOCallback(proxy.sock, + VIR_EVENT_HANDLE_READABLE, + virRemoteProxyEventOnSocket, + &proxy, + NULL) < 0) + goto cleanup; + + while (!proxy.quit) + virEventRunDefaultImpl(); + + if (virGetLastErrorCode() !=3D VIR_ERR_OK) + goto cleanup; + + ret =3D 0; + cleanup: + if (proxy.stdinWatch !=3D -1) + virEventRemoveHandle(proxy.stdinWatch); + if (proxy.stdoutWatch !=3D -1) + virEventRemoveHandle(proxy.stdoutWatch); + return ret; +} + +int main(int argc, char **argv) +{ + const char *uri_str =3D NULL; + g_autoptr(virURI) uri =3D NULL; + g_autofree char *driver =3D NULL; + remoteDriverTransport transport; + bool user =3D false; + bool autostart =3D false; + gboolean version =3D false; + gboolean readonly =3D false; + g_autofree char *sock_path =3D NULL; + g_autofree char *daemon_name =3D NULL; + g_autoptr(virNetSocket) sock =3D NULL; + GError *error =3D NULL; + g_autoptr(GOptionContext) context =3D NULL; + GOptionEntry entries[] =3D { + { "readonly", 'r', 0, G_OPTION_ARG_NONE, &readonly, "Connect read-= only", NULL }, + { "version", 'V', 0, G_OPTION_ARG_NONE, &version, "Display version= information", NULL }, + { NULL } + }; + + context =3D g_option_context_new("- libvirt socket proxy"); + g_option_context_add_main_entries(context, entries, PACKAGE); + if (!g_option_context_parse(context, &argc, &argv, &error)) { + g_printerr(_("option parsing failed: %s\n"), error->message); + exit(EXIT_FAILURE); + } + + if (version) { + g_print("%s (%s) %s\n", argv[0], PACKAGE_NAME, PACKAGE_VERSION); + exit(EXIT_SUCCESS); + } + + virSetErrorFunc(NULL, NULL); + virSetErrorLogPriorityFunc(NULL); + + if (virGettextInitialize() < 0 || + virErrorInitialize() < 0) { + g_printerr(_("%s: initialization failed\n"), argv[0]); + exit(EXIT_FAILURE); + } + + virFileActivateDirOverrideForProg(argv[0]); + + /* Initialize the log system */ + virLogSetFromEnv(); + + if (optind !=3D (argc - 1)) { + g_printerr("%s: expected a URI\n", argv[0]); + exit(EXIT_FAILURE); + } + + uri_str =3D argv[optind]; + VIR_DEBUG("Using URI %s", uri_str); + + if (!(uri =3D virURIParse(uri_str))) { + g_printerr(("%s: cannot parse '%s': %s\n"), + argv[0], uri_str, virGetLastErrorMessage()); + exit(EXIT_FAILURE); + } + + if (remoteSplitURIScheme(uri, &driver, &transport) < 0) { + g_printerr(_("%s: cannot parse URI transport '%s': %s\n"), + argv[0], uri_str, virGetLastErrorMessage()); + exit(EXIT_FAILURE); + } + + if (transport !=3D REMOTE_DRIVER_TRANSPORT_UNIX) { + g_printerr(_("%s: unexpected URI transport '%s'\n"), + argv[0], uri_str); + exit(EXIT_FAILURE); + } + + remoteGetURIDaemonInfo(uri, transport, &user, &autostart); + + sock_path =3D remoteGetUNIXSocket(transport, + REMOTE_DRIVER_MODE_AUTO, + driver, + !!readonly, + user, + &daemon_name); + + if (virNetSocketNewConnectUNIX(sock_path, autostart, daemon_name, &soc= k) < 0) { + g_printerr(_("%s: cannot connect to '%s': %s\n"), + argv[0], sock_path, virGetLastErrorMessage()); + exit(EXIT_FAILURE); + } + + if (virRemoteProxyRun(sock) < 0) { + g_printerr(_("%s: could not proxy traffic: %s\n"), + argv[0], virGetLastErrorMessage()); + exit(EXIT_FAILURE); + } + + exit(EXIT_SUCCESS); +} diff --git a/src/rpc/virnetsocket.h b/src/rpc/virnetsocket.h index d39b270480..3996d264fb 100644 --- a/src/rpc/virnetsocket.h +++ b/src/rpc/virnetsocket.h @@ -34,6 +34,7 @@ typedef struct _virNetSocket virNetSocket; typedef virNetSocket *virNetSocketPtr; =20 +G_DEFINE_AUTOPTR_CLEANUP_FUNC(virNetSocket, virObjectUnref); =20 typedef void (*virNetSocketIOFunc)(virNetSocketPtr sock, int events, --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) client-ip=207.211.31.120; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319884; cv=none; d=zohomail.com; s=zohoarc; b=R75hozB2mRUgh1e4edfsMM1DcgvV+S91/QF7RXvvcGp2MZoSLJhzgBS4M9s2izlWYszFeuM/2Eqqstlxq2o8K7PPK7pTlSZSnkbBuPowpCBDyy6MRJ4W8thr9R7VX5AYSjtWnTN/VEI5NM3Hn5wGAMlInASHv3UVt8fwdvoSaHQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319884; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=QGcxCew+VYQ8lMWj/EwNonhQ+e/5vjwZjAnAX73Kygs=; b=XKlrZoE9Hxysj7pNt48OKvKO3If+6cb/ki6UZa6URMPhE5WYmJl5Moc8wwXEu1B2w2kAxf7OwE2SETMuOjEVD4MAVi1Z+QO7JKqg/RH0j3loru0f86Ror4Xwkq4Sj/arVv83CDgNVvkPLpy4S8ao1+QfB85S4UuaVHuTufmAGdA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.120 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) by mx.zohomail.com with SMTPS id 1594319884241422.4047786644376; Thu, 9 Jul 2020 11:38:04 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-387-f9mx_bNUN3O5pyCA4_Mm_A-1; Thu, 09 Jul 2020 14:37:15 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 6F1CF80BCAE; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4DB0179CF3; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1C8B98435D; Thu, 9 Jul 2020 18:37:08 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069Ib7B1023486 for ; Thu, 9 Jul 2020 14:37:07 -0400 Received: by smtp.corp.redhat.com (Postfix) id 8364460F8D; Thu, 9 Jul 2020 18:37:07 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id 7E53260E1C; Thu, 9 Jul 2020 18:37:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319883; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=QGcxCew+VYQ8lMWj/EwNonhQ+e/5vjwZjAnAX73Kygs=; b=KNgyClnMudhK8G4id2pbcuIolE7q08HWUjpYCIby0det/S8WSjYxb1VR/bWet7JnQT7NJV U6DNZtY4Om+F6vbD1qsHhhSrZ1h+HMOsI3fOIi5a0cO71JNIRuBKS3RGg8yRZDyLlV2nQJ +EVMCbrP4VXEp/caKKGGKZSAclFiQeE= X-MC-Unique: f9mx_bNUN3O5pyCA4_Mm_A-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 8/9] rpc: switch order of args in virNetClientNewSSH Date: Thu, 9 Jul 2020 19:36:45 +0100 Message-Id: <20200709183646.4016586-9-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Switch keyfile and netcat parameters, since the netcat path and socket path are a logical pair that belong together. This patches the other constructors. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- src/remote/remote_driver.c | 2 +- src/rpc/virnetclient.c | 2 +- src/rpc/virnetclient.h | 4 ++-- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index c2dcf20f91..c1f7a45aab 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -1010,8 +1010,8 @@ doRemoteOpen(virConnectPtr conn, username, !tty, !verify, - netcat ? netcat : "nc", keyfile, + netcat ? netcat : "nc", sockname))) goto failed; =20 diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index aee2b52bf6..cd1bcc3ab3 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -441,8 +441,8 @@ virNetClientPtr virNetClientNewSSH(const char *nodename, const char *username, bool noTTY, bool noVerify, - const char *netcatPath, const char *keyfile, + const char *netcatPath, const char *socketPath) { virNetSocketPtr sock; diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h index 0005de46f3..6fdc370083 100644 --- a/src/rpc/virnetclient.h +++ b/src/rpc/virnetclient.h @@ -48,9 +48,9 @@ virNetClientPtr virNetClientNewSSH(const char *nodename, const char *username, bool noTTY, bool noVerify, - const char *netcat, const char *keyfile, - const char *path); + const char *netcat, + const char *socketPath); =20 virNetClientPtr virNetClientNewLibSSH2(const char *host, const char *port, --=20 2.26.2 From nobody Sun May 5 00:25:56 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) client-ip=207.211.31.81; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-1.mimecast.com; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1594319846; cv=none; d=zohomail.com; s=zohoarc; b=bDtYabsjhv0HL7jhJTHwIpCmGMrzAH9gvKffrff28+g947hzShOke7f/uil3dUwFrUuHMwi0OmpTLhVxdA5FCMfJdlC0kDDNslnbK2/IOWVJdoQD/ToDIgi+EsPaw7uGeEP3MyYQ1aDuBeV2u9AwTLBa/ixCT16ovC80MDKYpAQ= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1594319846; h=Content-Type:Content-Transfer-Encoding:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=mevR2rTpmI3I+bqKYMu3oEwim/fZrvT6VKZO1ILM5eo=; b=WmR/oCknICf9lYhfDozQW0uGCZh1S8menJLXuv3sYD/Q5YSkAbrtV1NNhkOkDCE7D04lGATUNRwqn5D/BA3o/tFpFQBbn/N4usnE0G7C8lOE6+3pS4gfz4uVrzgybeuzIZYULDgOKJPiOkxFDDSgbZuCyKYnfkfBA5wlUVN++sw= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of redhat.com designates 207.211.31.81 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass header.from= (p=none dis=none) header.from= Return-Path: Received: from us-smtp-delivery-1.mimecast.com (us-smtp-2.mimecast.com [207.211.31.81]) by mx.zohomail.com with SMTPS id 1594319845994549.7664566592036; Thu, 9 Jul 2020 11:37:25 -0700 (PDT) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-281-JirGiKeOM1qq6zCMar7s5w-1; Thu, 09 Jul 2020 14:37:22 -0400 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.phx2.redhat.com [10.5.11.23]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id D7908107BA59; Thu, 9 Jul 2020 18:37:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B67BC19D61; Thu, 9 Jul 2020 18:37:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 860D51809557; Thu, 9 Jul 2020 18:37:15 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 069IbEt9023594 for ; Thu, 9 Jul 2020 14:37:14 -0400 Received: by smtp.corp.redhat.com (Postfix) id B5B0F6111F; Thu, 9 Jul 2020 18:37:14 +0000 (UTC) Received: from localhost.localdomain.com (unknown [10.36.110.45]) by smtp.corp.redhat.com (Postfix) with ESMTP id C1CAD610AF; Thu, 9 Jul 2020 18:37:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1594319844; h=from:from:sender:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:mime-version:mime-version: content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:list-id:list-help: list-unsubscribe:list-subscribe:list-post; bh=mevR2rTpmI3I+bqKYMu3oEwim/fZrvT6VKZO1ILM5eo=; b=C/NHVFKtqe1N9Fc3FmuA52Af6G20z/DCMln51OrPLK9gF788x9vW/Eu8na6Lv5+1y2hyne TcDgMPPbRA7VraIo8vFOYFafM884N4vClM+uz8WgUJ1I2k+Pgjhla7ogDOE/5ISE9uU88I O6UxY5AgN8opjs9QmdPtBa1z62s9AVE= X-MC-Unique: JirGiKeOM1qq6zCMar7s5w-1 From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 9/9] rpc: use new virt-nc binary for remote tunnelling Date: Thu, 9 Jul 2020 19:36:46 +0100 Message-Id: <20200709183646.4016586-10-berrange@redhat.com> In-Reply-To: <20200709183646.4016586-1-berrange@redhat.com> References: <20200709183646.4016586-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-loop: libvir-list@redhat.com X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.23 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) This wires up support for using the new virt-nc binary with the ssh, libssh and libssh2 protocols. The new binary will be used preferentially if it is available in $PATH, otherwise we fall back to traditional netcat. The "proxy" URI parameter can be used to force use of netcat e.g. qemu+ssh://host/system?proxy=3Dnetcat or the disable fallback e.g. qemu+ssh://host/system?proxy=3Dvirt-nc With use of virt-nc, we can now support remote session URIs qemu+ssh://host/session and this will only use virt-nc, with no fallback. This also lets the libvirtd process be auto-started. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Michal Privoznik --- docs/uri.html.in | 18 ++++++++++ src/remote/remote_driver.c | 30 +++++++++++++++- src/remote/remote_sockets.c | 8 ----- src/rpc/virnetclient.c | 70 ++++++++++++++++++++++++++++++------- src/rpc/virnetclient.h | 30 +++++++++++++--- tests/virnetsockettest.c | 7 ++-- 6 files changed, 136 insertions(+), 27 deletions(-) diff --git a/docs/uri.html.in b/docs/uri.html.in index 49f92773f8..5311579273 100644 --- a/docs/uri.html.in +++ b/docs/uri.html.in @@ -259,6 +259,24 @@ Note that parameter values must be Example: mode=3Ddirect + + + proxy + + auto, virt, generic + +
+
auto
try virt-nc, fallback to netcat<= /dd> +
netcat
only use netcat
+
virt-nc
only use virt-nc
+
+ Can also be set in libvirt.conf as remote_pro= xy + + + + + Example: proxy=3Dvirt-nc + command diff --git a/src/remote/remote_driver.c b/src/remote/remote_driver.c index c1f7a45aab..83789a86a9 100644 --- a/src/remote/remote_driver.c +++ b/src/remote/remote_driver.c @@ -761,12 +761,14 @@ doRemoteOpen(virConnectPtr conn, g_autofree char *knownHosts =3D NULL; g_autofree char *mode_str =3D NULL; g_autofree char *daemon_name =3D NULL; + g_autofree char *proxy_str =3D NULL; bool sanity =3D true; bool verify =3D true; #ifndef WIN32 bool tty =3D true; #endif int mode; + int proxy; =20 if (inside_daemon && !conn->uri->server) { mode =3D REMOTE_DRIVER_MODE_DIRECT; @@ -774,6 +776,14 @@ doRemoteOpen(virConnectPtr conn, mode =3D REMOTE_DRIVER_MODE_AUTO; } =20 + /* Historically we didn't allow ssh tunnel with session mode, + * since we can't construct the accurate path remotely, + * so we can default to modern virt-nc */ + if (flags & VIR_DRV_OPEN_REMOTE_USER) + proxy =3D VIR_NET_CLIENT_PROXY_VIRT_NC; + else + proxy =3D VIR_NET_CLIENT_PROXY_NETCAT; + /* We handle *ALL* URIs here. The caller has rejected any * URIs we don't care about */ =20 @@ -813,6 +823,7 @@ doRemoteOpen(virConnectPtr conn, EXTRACT_URI_ARG_STR("known_hosts_verify", knownHostsVerify); EXTRACT_URI_ARG_STR("tls_priority", tls_priority); EXTRACT_URI_ARG_STR("mode", mode_str); + EXTRACT_URI_ARG_STR("proxy", proxy_str); EXTRACT_URI_ARG_BOOL("no_sanity", sanity); EXTRACT_URI_ARG_BOOL("no_verify", verify); #ifndef WIN32 @@ -865,6 +876,14 @@ doRemoteOpen(virConnectPtr conn, (mode =3D remoteDriverModeTypeFromString(mode_str)) < 0) goto failed; =20 + if (conf && !proxy_str && + virConfGetValueString(conf, "remote_proxy", &proxy_str) < 0) + goto failed; + + if (proxy_str && + (proxy =3D virNetClientProxyTypeFromString(proxy_str)) < 0) + goto failed; + /* Sanity check that nothing requested !direct mode by mistake */ if (inside_daemon && !conn->uri->server && mode !=3D REMOTE_DRIVER_MOD= E_DIRECT) { virReportError(VIR_ERR_INVALID_ARG, "%s", @@ -949,8 +968,11 @@ doRemoteOpen(virConnectPtr conn, knownHosts, knownHostsVerify, sshauth, + proxy, netcat, sockname, + name, + flags & VIR_DRV_OPEN_REMOTE_= RO, auth, conn->uri); if (!priv->client) @@ -970,8 +992,11 @@ doRemoteOpen(virConnectPtr conn, knownHosts, knownHostsVerify, sshauth, + proxy, netcat, sockname, + name, + flags & VIR_DRV_OPEN_REMOTE_R= O, auth, conn->uri); if (!priv->client) @@ -1011,8 +1036,11 @@ doRemoteOpen(virConnectPtr conn, !tty, !verify, keyfile, + proxy, netcat ? netcat : "nc", - sockname))) + sockname, + name, + flags & VIR_DRV_OPEN_REMOT= E_RO))) goto failed; =20 priv->is_secure =3D 1; diff --git a/src/remote/remote_sockets.c b/src/remote/remote_sockets.c index 854775f401..7c69ed9e7f 100644 --- a/src/remote/remote_sockets.c +++ b/src/remote/remote_sockets.c @@ -108,14 +108,6 @@ remoteGetUNIXSocketHelper(remoteDriverTransport transp= ort, g_autofree char *userdir =3D NULL; =20 if (session) { - if (transport !=3D 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; - } userdir =3D virGetUserRuntimeDirectory(); =20 sockname =3D g_strdup_printf("%s/%s-sock", userdir, sock_prefix); diff --git a/src/rpc/virnetclient.c b/src/rpc/virnetclient.c index cd1bcc3ab3..5939f74e62 100644 --- a/src/rpc/virnetclient.c +++ b/src/rpc/virnetclient.c @@ -50,6 +50,10 @@ enum { VIR_NET_CLIENT_MODE_COMPLETE, }; =20 +VIR_ENUM_IMPL(virNetClientProxy, + VIR_NET_CLIENT_PROXY_LAST, + "auto", "netcat", "virt-nc"); + struct _virNetClientCall { int mode; =20 @@ -414,20 +418,50 @@ virNetClientDoubleEscapeShell(const char *str) } =20 char * -virNetClientSSHHelperCommand(const char *netcatPath, - const char *socketPath) +virNetClientSSHHelperCommand(virNetClientProxy proxy, + const char *netcatPath, + const char *socketPath, + const char *driverURI, + bool readonly) { g_autofree char *netcatPathSafe =3D virNetClientDoubleEscapeShell(netc= atPath); + g_autofree char *driverURISafe =3D virNetClientDoubleEscapeShell(drive= rURI); + g_autofree char *nccmd =3D NULL; + g_autofree char *virtnccmd =3D NULL; =20 - return g_strdup_printf( - "sh -c " - "'if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1;= then " - "ARG=3D-q0;" + nccmd =3D g_strdup_printf( + "if '%s' -q 2>&1 | grep \"requires an argument\" >/dev/null 2>&1; = then " + "ARG=3D-q0;" "else " - "ARG=3D;" + "ARG=3D;" "fi;" - "'%s' $ARG -U %s'", + "'%s' $ARG -U %s", netcatPathSafe, netcatPathSafe, socketPath); + + virtnccmd =3D g_strdup_printf("%s '%s'", + readonly ? "virt-nc -r" : "virt-nc", + driverURISafe); + + switch (proxy) { + case VIR_NET_CLIENT_PROXY_AUTO: + return g_strdup_printf("sh -c 'which virt-nc 1>/dev/null 2>&1; " + "if test $? =3D 0; then " + " %s; " + "else" + " %s; " + "fi'", virtnccmd, nccmd); + + case VIR_NET_CLIENT_PROXY_NETCAT: + return g_strdup_printf("sh -c '%s'", nccmd); + + case VIR_NET_CLIENT_PROXY_VIRT_NC: + return g_strdup_printf("sh -c '%s'", virtnccmd); + + case VIR_NET_CLIENT_PROXY_LAST: + default: + virReportEnumRangeError(virNetClientProxy, proxy); + return NULL; + } } =20 =20 @@ -442,8 +476,11 @@ virNetClientPtr virNetClientNewSSH(const char *nodenam= e, bool noTTY, bool noVerify, const char *keyfile, + virNetClientProxy proxy, const char *netcatPath, - const char *socketPath) + const char *socketPath, + const char *driverURI, + bool readonly) { virNetSocketPtr sock; =20 @@ -451,7 +488,8 @@ virNetClientPtr virNetClientNewSSH(const char *nodename, =20 DEFAULT_VALUE(netcatPath, "nc"); =20 - command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); + command =3D virNetClientSSHHelperCommand(proxy, netcatPath, socketPath, + driverURI, readonly); =20 if (virNetSocketNewConnectSSH(nodename, service, binary, username, noT= TY, noVerify, keyfile, command, &sock) < 0) @@ -468,8 +506,11 @@ virNetClientPtr virNetClientNewLibSSH2(const char *hos= t, const char *knownHostsPath, const char *knownHostsVerify, const char *authMethods, + virNetClientProxy proxy, const char *netcatPath, const char *socketPath, + const char *driverURI, + bool readonly, virConnectAuthPtr authPtr, virURIPtr uri) { @@ -511,7 +552,8 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, DEFAULT_VALUE(netcatPath, "nc"); DEFAULT_VALUE(knownHostsVerify, "normal"); =20 - command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); + command =3D virNetClientSSHHelperCommand(proxy, netcatPath, socketPath, + driverURI, readonly); =20 if (virNetSocketNewConnectLibSSH2(host, port, family, @@ -531,8 +573,11 @@ virNetClientPtr virNetClientNewLibssh(const char *host, const char *knownHostsPath, const char *knownHostsVerify, const char *authMethods, + virNetClientProxy proxy, const char *netcatPath, const char *socketPath, + const char *driverURI, + bool readonly, virConnectAuthPtr authPtr, virURIPtr uri) { @@ -574,7 +619,8 @@ virNetClientPtr virNetClientNewLibssh(const char *host, DEFAULT_VALUE(netcatPath, "nc"); DEFAULT_VALUE(knownHostsVerify, "normal"); =20 - command =3D virNetClientSSHHelperCommand(netcatPath, socketPath); + command =3D virNetClientSSHHelperCommand(proxy, netcatPath, socketPath, + driverURI, readonly); =20 if (virNetSocketNewConnectLibssh(host, port, family, diff --git a/src/rpc/virnetclient.h b/src/rpc/virnetclient.h index 6fdc370083..76500e2c3f 100644 --- a/src/rpc/virnetclient.h +++ b/src/rpc/virnetclient.h @@ -30,9 +30,22 @@ #include "virobject.h" #include "viruri.h" =20 +typedef enum { + VIR_NET_CLIENT_PROXY_AUTO, + VIR_NET_CLIENT_PROXY_NETCAT, + VIR_NET_CLIENT_PROXY_VIRT_NC, + + VIR_NET_CLIENT_PROXY_LAST, +} virNetClientProxy; + +VIR_ENUM_DECL(virNetClientProxy); + char * -virNetClientSSHHelperCommand(const char *netcatPath, - const char *socketPath); +virNetClientSSHHelperCommand(virNetClientProxy proxy, + const char *netcatPath, + const char *socketPath, + const char *driverURI, + bool readonly); =20 virNetClientPtr virNetClientNewUNIX(const char *path, bool spawnDaemon, @@ -49,8 +62,11 @@ virNetClientPtr virNetClientNewSSH(const char *nodename, bool noTTY, bool noVerify, const char *keyfile, - const char *netcat, - const char *socketPath); + virNetClientProxy proxy, + const char *netcatPath, + const char *socketPath, + const char *driverURI, + bool readonly); =20 virNetClientPtr virNetClientNewLibSSH2(const char *host, const char *port, @@ -60,8 +76,11 @@ virNetClientPtr virNetClientNewLibSSH2(const char *host, const char *knownHostsPath, const char *knownHostsVerify, const char *authMethods, + virNetClientProxy proxy, const char *netcatPath, const char *socketPath, + const char *driverURI, + bool readonly, virConnectAuthPtr authPtr, virURIPtr uri); =20 @@ -73,8 +92,11 @@ virNetClientPtr virNetClientNewLibssh(const char *host, const char *knownHostsPath, const char *knownHostsVerify, const char *authMethods, + virNetClientProxy proxy, const char *netcatPath, const char *socketPath, + const char *driverURI, + bool readonly, virConnectAuthPtr authPtr, virURIPtr uri); =20 diff --git a/tests/virnetsockettest.c b/tests/virnetsockettest.c index 842eb1bcfc..c6fbe479d7 100644 --- a/tests/virnetsockettest.c +++ b/tests/virnetsockettest.c @@ -464,8 +464,11 @@ static int testSocketSSH(const void *opaque) virNetSocketPtr csock =3D NULL; /* Client socket */ int ret =3D -1; char buf[1024]; - g_autofree char *command =3D virNetClientSSHHelperCommand(data->netcat, - data->path); + g_autofree char *command =3D virNetClientSSHHelperCommand(VIR_NET_CLIE= NT_PROXY_AUTO, + data->netcat, + data->path, + "qemu:///sessi= on", + true); =20 if (virNetSocketNewConnectSSH(data->nodename, data->service, --=20 2.26.2