summaryrefslogtreecommitdiff
path: root/src/common/compat.c
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2015-03-01 15:35:36 +0100
committerNick Mathewson <nickm@torproject.org>2015-06-17 10:11:18 -0400
commitcbdf2c5d8f6fcce432e2355f406ca9e3c2340a5b (patch)
tree3a353cccbff8b5997c68177f7ea497209038edb3 /src/common/compat.c
parent1b52e95028e0d84b7a112e4b8f2e393261dbb19c (diff)
downloadtor-cbdf2c5d8f6fcce432e2355f406ca9e3c2340a5b.tar.gz
tor-cbdf2c5d8f6fcce432e2355f406ca9e3c2340a5b.zip
Add a tor_getpass to read passphrases. Needs better backend.
Diffstat (limited to 'src/common/compat.c')
-rw-r--r--src/common/compat.c33
1 files changed, 33 insertions, 0 deletions
diff --git a/src/common/compat.c b/src/common/compat.c
index 8da7ef3f69..701027523e 100644
--- a/src/common/compat.c
+++ b/src/common/compat.c
@@ -67,6 +67,9 @@
#ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h>
#endif
+#ifdef HAVE_READPASSPHRASE_H
+#include <readpassphrase.h>
+#endif
#ifndef HAVE_GETTIMEOFDAY
#ifdef HAVE_FTIME
@@ -3242,3 +3245,33 @@ tor_sleep_msec(int msec)
}
#endif
+/** Emit the password prompt <b>prompt</b>, then read up to <b>buflen</b>
+ * characters of passphrase into <b>output</b>. */
+ssize_t
+tor_getpass(const char *prompt, char *output, size_t buflen)
+{
+ tor_assert(buflen <= SSIZE_MAX);
+#if defined(HAVE_READPASSPHRASE)
+ char *pwd = readpassphrase(prompt, output, buflen, RPP_ECHO_OFF);
+ if (pwd == NULL)
+ return -1;
+ return strlen(pwd);
+#elif defined(HAVE_GETPASS)
+ /* XXX We shouldn't actually use this; it's deprecated to hell and back */
+ memset(output, 0, buflen);
+ char *pwd = getpass(prompt);
+ if (pwd == NULL)
+ return -1;
+ ssize_t len = (ssize_t)strlen(pwd);
+ strlcpy(output, pwd, buflen);
+ memset(pwd, 0, len);
+ return len;
+#else
+ /* XXX This is even worse. */
+ puts(prompt);
+ ssize_t n = read(STDIN_FILENO, output, buflen);
+ if (n < 0)
+ return -1;
+ return n;
+#endif
+}