diff --git a/src/libvirt.c b/src/libvirt.c index 04f9fd7ab1..2d02808a89 100644 --- a/src/libvirt.c +++ b/src/libvirt.c @@ -30,7 +30,6 @@ #include #include -#include "getpass.h" #ifdef WITH_CURL # include @@ -157,9 +156,9 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr cred, if (fflush(stdout) != 0) return -1; - bufptr = getpass(""); - if (!bufptr) - return -1; + bufptr = virGetPassword(); + if (STREQ(bufptr, "")) + VIR_FREE(bufptr); break; default: @@ -167,7 +166,7 @@ virConnectAuthCallbackDefault(virConnectCredentialPtr cred, } if (cred[i].type != VIR_CRED_EXTERNAL) { - cred[i].result = g_strdup(STREQ(bufptr, "") && cred[i].defresult ? cred[i].defresult : bufptr); + cred[i].result = bufptr ? bufptr : g_strdup(cred[i].defresult ? cred[i].defresult : ""); cred[i].resultlen = strlen(cred[i].result); } } diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 970f412527..2a61eba3e1 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -3380,6 +3380,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 +#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 = g_string_new(""); + + while (1) { + char c = _getch(); + if (c == '\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) = (rvalue)) != (rvalue)) + +char *virGetPassword(void); diff --git a/tools/virsh-secret.c b/tools/virsh-secret.c index 00a434e997..be4adc416e 100644 --- a/tools/virsh-secret.c +++ b/tools/virsh-secret.c @@ -250,7 +250,7 @@ cmdSecretSetValue(vshControl *ctl, const vshCmd *cmd) vshPrint(ctl, "%s", _("Enter new value for secret:")); fflush(stdout); - if (!(file_buf = getpass(""))) { + if (!(file_buf = virGetPassword())) { vshError(ctl, "%s", _("Failed to read secret")); return false; }