From nobody Mon Feb 9 02:28:52 2026 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=1579779826; cv=none; d=zohomail.com; s=zohoarc; b=Wv5V4o3DadgaV9Mx2nefEiDJwrKRb7fpJipNY71lhZAp89kazMZaj2j43vf+I5pcuWgHq/Em4nwyXeO3Hld3l9Ufkjn+vntp3v/rj2Kd6HTaaCs8IZRWIufUpyZvZ2LQvoqcIVYLciP2sP7P5vBp1gbDi8dYE0ABicA2xiP5tBA= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1579779826; 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=baK56A43saJ3x0tN8SXmkS+0yr8/SR1JvXsQplWtIho=; b=UKgS/PYMnSK8hEcIFVCJ/Y1vT5FhvhVznPrKHLh8lWTp0vzK16ZW4tRwjpczobY1OdPYjy7vXQt5/XrsVpY7T39I76XuffV7eV4VYda3htKjrFOa8i0aL455D35xpi57E4mLodWcvVDGx4f/8byslV14/J7PLSV7SSTk76zrQx0= 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 1579779826724267.9240737485394; Thu, 23 Jan 2020 03:43:46 -0800 (PST) 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-55-zIpesGsSNC6Ohk3r2atsWA-1; Thu, 23 Jan 2020 06:43:43 -0500 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 C8D5B1005F76; Thu, 23 Jan 2020 11:43:36 +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 9DA498CCDB; Thu, 23 Jan 2020 11:43:36 +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 53DB085907; Thu, 23 Jan 2020 11:43:36 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 00NBhWfr025284 for ; Thu, 23 Jan 2020 06:43:32 -0500 Received: by smtp.corp.redhat.com (Postfix) id B69759CA3; Thu, 23 Jan 2020 11:43:32 +0000 (UTC) Received: from domokun.gsslab.fab.redhat.com (unknown [10.33.8.110]) by smtp.corp.redhat.com (Postfix) with ESMTP id 24D1C1CB; Thu, 23 Jan 2020 11:43:31 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1579779825; 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=baK56A43saJ3x0tN8SXmkS+0yr8/SR1JvXsQplWtIho=; b=ZefwzFvgCB6IljnmM7yAMe3QU69XN15spaVJcs/Dh+INDpm7OQd/UrwVQQ0IaH703p5XmH gsZmY8JkzYtIpV+FFI3GKdi8wy0JEpRjVLnAg3g84QeBkqa6tphILMkt7erguf2ALsJeF0 kZGADcjc1nJWmE5Yat75G1jZU3pt0Eo= From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= To: libvir-list@redhat.com Subject: [libvirt PATCH 03/32] util: add API for reading password from the console Date: Thu, 23 Jan 2020 11:42:56 +0000 Message-Id: <20200123114325.3860881-4-berrange@redhat.com> In-Reply-To: <20200123114325.3860881-1-berrange@redhat.com> References: <20200123114325.3860881-1-berrange@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 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-MC-Unique: zIpesGsSNC6Ohk3r2atsWA-1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @redhat.com) Content-Type: text/plain; charset="utf-8" This imports a simpler version of GNULIB's getpass() function impl for Windows. Note that GNULIB's impl was buggy as it returned a static string on UNIX, and a heap allocated string on Windows. This new impl always heap allocates. Signed-off-by: Daniel P. Berrang=C3=A9 Reviewed-by: Pavel Hrdina --- src/libvirt.c | 9 ++++----- src/libvirt_private.syms | 1 + src/util/virutil.c | 29 +++++++++++++++++++++++++++++ src/util/virutil.h | 2 ++ 4 files changed, 36 insertions(+), 5 deletions(-) diff --git a/src/libvirt.c b/src/libvirt.c index f1ffc97261..353b3a55d8 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -30,7 +30,6 @@ =20 #include #include -#include "getpass.h" =20 #ifdef WITH_CURL # include @@ -157,9 +156,9 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr c= red, if (fflush(stdout) !=3D 0) return -1; =20 - bufptr =3D getpass(""); - if (!bufptr) - return -1; + bufptr =3D virGetPassword(); + if (STREQ(bufptr, "")) + VIR_FREE(bufptr); break; =20 default: @@ -167,7 +166,7 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr c= red, } =20 if (cred[i].type !=3D VIR_CRED_EXTERNAL) { - cred[i].result =3D g_strdup(STREQ(bufptr, "") && cred[i].defre= sult ? cred[i].defresult : bufptr); + cred[i].result =3D bufptr ? bufptr : g_strdup(cred[i].defresul= t ? cred[i].defresult : ""); cred[i].resultlen =3D strlen(cred[i].result); } } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index b63feb4054..fa046051a3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3371,6 +3371,7 @@ virGetGroupList; virGetGroupName; virGetHostname; virGetHostnameQuiet; +virGetPassword; virGetSelfLastChanged; virGetSystemPageSize; virGetSystemPageSizeKB; diff --git a/src/util/virutil.c b/src/util/virutil.c index 7c2c5a78f6..87ca16c088 100644 --- a/src/util/virutil.c +++ b/src/util/virutil.c @@ -28,6 +28,10 @@ #include #include =20 +#ifdef WIN32 +# include +#endif /* WIN32 */ + #ifdef MAJOR_IN_MKDEV # include #elif MAJOR_IN_SYSMACROS @@ -1731,3 +1735,28 @@ virHostGetDRMRenderNode(void) VIR_DIR_CLOSE(driDir); return ret; } + +/* + * Get a password from the console input stream. + * The caller must free the returned password. + * + * Returns: the password, or NULL + */ +char *virGetPassword(void) +{ +#ifdef WIN32 + GString *pw =3D g_string_new(""); + + while (1) { + char c =3D _getch(); + if (c =3D=3D '\r') + break; + + g_string_append_c(pw, c); + } + + return g_string_free(pw, FALSE); +#else /* !WIN32 */ + return g_strdup(getpass("")); +#endif /* ! WIN32 */ +} diff --git a/src/util/virutil.h b/src/util/virutil.h index 1a6ae1787a..62a53f34cb 100644 --- a/src/util/virutil.h +++ b/src/util/virutil.h @@ -159,3 +159,5 @@ char *virHostGetDRMRenderNode(void) G_GNUC_NO_INLINE; */ #define VIR_ASSIGN_IS_OVERFLOW(lvalue, rvalue) \ (((lvalue) =3D (rvalue)) !=3D (rvalue)) + +char *virGetPassword(void); --=20 2.24.1