Add a tor_getpass to read passphrases. Needs better backend.

This commit is contained in:
Nick Mathewson 2015-03-01 15:35:36 +01:00
parent 1b52e95028
commit cbdf2c5d8f
3 changed files with 38 additions and 0 deletions

View File

@ -386,6 +386,7 @@ AC_CHECK_FUNCS(
ftime \ ftime \
getaddrinfo \ getaddrinfo \
getifaddrs \ getifaddrs \
getpass \
getrlimit \ getrlimit \
gettimeofday \ gettimeofday \
gmtime_r \ gmtime_r \
@ -399,6 +400,7 @@ AC_CHECK_FUNCS(
pipe \ pipe \
pipe2 \ pipe2 \
prctl \ prctl \
readpassphrase \
rint \ rint \
sigaction \ sigaction \
socketpair \ socketpair \
@ -926,6 +928,7 @@ AC_CHECK_HEADERS(
netinet/in.h \ netinet/in.h \
netinet/in6.h \ netinet/in6.h \
pwd.h \ pwd.h \
readpassphrase.h \
stdint.h \ stdint.h \
sys/eventfd.h \ sys/eventfd.h \
sys/file.h \ sys/file.h \

View File

@ -67,6 +67,9 @@
#ifdef HAVE_CRT_EXTERNS_H #ifdef HAVE_CRT_EXTERNS_H
#include <crt_externs.h> #include <crt_externs.h>
#endif #endif
#ifdef HAVE_READPASSPHRASE_H
#include <readpassphrase.h>
#endif
#ifndef HAVE_GETTIMEOFDAY #ifndef HAVE_GETTIMEOFDAY
#ifdef HAVE_FTIME #ifdef HAVE_FTIME
@ -3242,3 +3245,33 @@ tor_sleep_msec(int msec)
} }
#endif #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
}

View File

@ -708,6 +708,8 @@ STATIC int tor_ersatz_socketpair(int family, int type, int protocol,
#endif #endif
#endif #endif
ssize_t tor_getpass(const char *prompt, char *output, size_t buflen);
/* This needs some of the declarations above so we include it here. */ /* This needs some of the declarations above so we include it here. */
#include "compat_threads.h" #include "compat_threads.h"