]> xenbits.xensource.com Git - libvirt.git/commitdiff
util/virutil: Use readpassphrase when libbsd is available
authorJakub Palacky <jpalacky@redhat.com>
Wed, 11 Sep 2024 12:36:40 +0000 (14:36 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Thu, 12 Sep 2024 11:12:47 +0000 (13:12 +0200)
When libbsd is available, use the preferred readpassphrase() function isntead of getpass()
as the getpass() function has been marked as obsolete and shouldnt be used

Signed-off-by: Jakub Palacky <jpalacky@redhat.com>
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
meson.build
src/util/meson.build
src/util/virutil.c

index 297fbfae48337491d47943dfaf7c8a6d6129dd3a..4c6e5a088e3e65dcb1458d2adda7407cd245e9bd 100644 (file)
@@ -954,6 +954,11 @@ if blkid_dep.found()
   conf.set('WITH_BLKID', 1)
 endif
 
+libbsd_dep = dependency('libbsd', required: false)
+if libbsd_dep.found()
+  conf.set('WITH_LIBBSD', 1)
+endif
+
 capng_dep = dependency('libcap-ng', required: get_option('capng'))
 if capng_dep.found()
   conf.set('WITH_CAPNG', 1)
@@ -2335,6 +2340,7 @@ libs_summary = {
   'dlopen': dlopen_dep.found(),
   'fuse': fuse_dep.found(),
   'glusterfs': glusterfs_dep.found(),
+  'libbsd': libbsd_dep.found(),
   'libiscsi': libiscsi_dep.found(),
   'libkvm': libkvm_dep.found(),
   'libnbd': libnbd_dep.found(),
index 896c79515034e40483aca7274c1330299c482e33..54db542957d14a218537d7700d9cc5d31b74e421 100644 (file)
@@ -194,6 +194,7 @@ virt_util_lib = static_library(
     devmapper_dep,
     gnutls_dep,
     intl_dep,
+    libbsd_dep,
     libm_dep,
     libnl_dep,
     libutil_dep,
index 6c89a48e5112b9731800fc6e6eaf550946ec2465..bf6008fdfbdf0a8255a1dd53477b9a95730273cf 100644 (file)
 # include <conio.h>
 #endif /* WIN32 */
 
+#ifdef WITH_LIBBSD
+# include <bsd/readpassphrase.h>
+#endif
+
 #ifdef __linux__
 # include <sys/sysmacros.h>
 #endif
@@ -1773,6 +1777,19 @@ char *virGetPassword(void)
     }
 
     return g_string_free(pw, FALSE);
+#elif WITH_LIBBSD /* !WIN32 */
+# ifndef PASS_MAX
+#  define PASS_MAX 1024
+# endif
+    char *pass = NULL;
+    g_autofree char *buffer = g_new0(char, PASS_MAX);
+
+    pass = readpassphrase("", buffer, PASS_MAX, 0);
+    if (pass == NULL) {
+        return NULL;
+    }
+
+    return g_steal_pointer(&buffer);
 #else /* !WIN32 */
     return g_strdup(getpass(""));
 #endif /* ! WIN32 */