#include "apr_private.h"
#include "apr_strings.h"
#include "apr_lib.h"
#include "apr_errno.h"
#if APR_HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if APR_HAVE_ERRNO_H
#include <errno.h>
#endif
#if APR_HAVE_UNISTD_H
#include <unistd.h>
#endif
#if APR_HAVE_CONIO_H
#ifdef _MSC_VER
#pragma warning(disable: 4032)
#include <conio.h>
#pragma warning(default: 4032)
#else
#include <conio.h>
#endif
#endif
#if APR_HAVE_STDLIB_H
#include <stdlib.h>
#endif
#if APR_HAVE_STRING_H
#include <string.h>
#endif
#if APR_HAVE_STRINGS_H
#include <strings.h>
#endif
#if defined(HAVE_GETPASS) && \
(defined(PASS_MAX) && PASS_MAX < 32) || defined(__hpux) || defined(__hpux__)
#undef HAVE_GETPASS
#endif
#if defined(HAVE_TERMIOS_H) && !defined(HAVE_GETPASS)
#include <termios.h>
#endif
#if !APR_CHARSET_EBCDIC
#define LF 10
#define CR 13
#else
#define LF '\n'
#define CR '\r'
#endif
#define MAX_STRING_LEN 256
#define ERR_OVERFLOW 5
#if !defined(HAVE_GETPASS) && !defined(HAVE_GETPASSPHRASE) && !defined(HAVE_GETPASS_R)
#if !defined(HAVE_TERMIOS_H) && !defined(WIN32) && !defined(NETWARE)
static char *get_password(const char *prompt)
{
static char password[MAX_STRING_LEN];
fputs(prompt, stderr);
fgets((char *) &password, sizeof(password), stdin);
return (char *) &password;
}
#elif defined (HAVE_TERMIOS_H)
#include <stdio.h>
static char *get_password(const char *prompt)
{
struct termios attr;
static char password[MAX_STRING_LEN];
int n=0;
fputs(prompt, stderr);
fflush(stderr);
if (tcgetattr(STDIN_FILENO, &attr) != 0)
return NULL;
attr.c_lflag &= ~(ECHO);
if (tcsetattr(STDIN_FILENO, TCSAFLUSH, &attr) != 0)
return NULL;
while ((password[n] = getchar()) != '\n') {
if (n < sizeof(password) - 1 && password[n] >= ' ' && password[n] <= '~') {
n++;
} else {
fprintf(stderr,"\n");
fputs(prompt, stderr);
fflush(stderr);
n = 0;
}
}
password[n] = '\0';
printf("\n");
if (n > (MAX_STRING_LEN - 1)) {
password[MAX_STRING_LEN - 1] = '\0';
}
attr.c_lflag |= ECHO;
tcsetattr(STDIN_FILENO, TCSANOW, &attr);
return (char*) &password;
}
#else
static char *get_password(const char *prompt)
{
#ifdef _WIN32_WCE
return NULL;
#else
static char password[128];
int n = 0;
int ch;
fputs(prompt, stderr);
while ((ch = _getch()) != '\r') {
if (ch == EOF) {
fputs("[EOF]\n", stderr);
return NULL;
}
else if (ch == 0 || ch == 0xE0) {
ch = (ch << 4) | _getch();
if ((ch == 0xE53 || ch == 0xE4B || ch == 0x053 || ch == 0x04b) && n) {
password[--n] = '\0';
fputs("\b \b", stderr);
}
else {
fputc('\a', stderr);
}
}
else if ((ch == '\b' || ch == 127) && n) {
password[--n] = '\0';
fputs("\b \b", stderr);
}
else if (ch == 3) {
fputs("^C\n", stderr);
exit(-1);
}
else if (ch == 26) {
fputs("^Z\n", stderr);
return NULL;
}
else if (ch == 27) {
fputc('\n', stderr);
fputs(prompt, stderr);
n = 0;
}
else if ((n < sizeof(password) - 1) && !apr_iscntrl(ch)) {
password[n++] = ch;
fputc('*', stderr);
}
else {
fputc('\a', stderr);
}
}
fputc('\n', stderr);
password[n] = '\0';
return password;
#endif
}
#endif
#endif
APR_DECLARE(apr_status_t) apr_password_get(const char *prompt, char *pwbuf, apr_size_t *bufsiz)
{
apr_status_t rv = APR_SUCCESS;
#if defined(HAVE_GETPASS_R)
if (getpass_r(prompt, pwbuf, *bufsiz) == NULL)
return APR_EINVAL;
#else
#if defined(HAVE_GETPASSPHRASE)
char *pw_got = getpassphrase(prompt);
#elif defined(HAVE_GETPASS)
char *pw_got = getpass(prompt);
#else
char *pw_got = get_password(prompt);
#endif
if (!pw_got)
return APR_EINVAL;
if (strlen(pw_got) >= *bufsiz) {
rv = APR_ENAMETOOLONG;
}
apr_cpystrn(pwbuf, pw_got, *bufsiz);
memset(pw_got, 0, strlen(pw_got));
#endif
return rv;
}