2004-12-25 17:08:02 -05:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
2003-10-16 10:09:39 -04:00
|
|
|
*
|
2015-02-02 18:30:45 -05:00
|
|
|
* Copyright (C) 1998 - 2015, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2003-10-16 10:09:39 -04:00
|
|
|
*
|
2004-12-25 17:08:02 -05:00
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2016-02-02 18:19:02 -05:00
|
|
|
* are also available at https://curl.haxx.se/docs/copyright.html.
|
2003-10-16 10:09:39 -04:00
|
|
|
*
|
2004-12-25 17:08:02 -05:00
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
2003-10-16 10:09:39 -04:00
|
|
|
*
|
2004-12-25 17:08:02 -05:00
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
2003-10-16 10:09:39 -04:00
|
|
|
*
|
2004-12-25 17:08:02 -05:00
|
|
|
***************************************************************************/
|
2012-04-06 17:35:15 -04:00
|
|
|
#include "tool_setup.h"
|
2003-10-16 10:09:39 -04:00
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifndef HAVE_GETPASS_R
|
|
|
|
/* this file is only for systems without getpass_r() */
|
2004-01-29 08:54:08 -05:00
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifdef HAVE_FCNTL_H
|
2011-10-06 11:39:00 -04:00
|
|
|
# include <fcntl.h>
|
2004-12-25 17:08:02 -05:00
|
|
|
#endif
|
2011-10-06 11:39:00 -04:00
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
2011-10-06 11:39:00 -04:00
|
|
|
# include <termios.h>
|
|
|
|
#elif defined(HAVE_TERMIO_H)
|
|
|
|
# include <termio.h>
|
2004-12-25 17:08:02 -05:00
|
|
|
#endif
|
|
|
|
|
2011-09-14 05:27:12 -04:00
|
|
|
#ifdef __VMS
|
|
|
|
# include descrip
|
|
|
|
# include starlet
|
|
|
|
# include iodef
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef WIN32
|
|
|
|
# include <conio.h>
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef NETWARE
|
|
|
|
# ifdef __NOVELL_LIBC__
|
|
|
|
# include <screen.h>
|
|
|
|
# else
|
|
|
|
# include <nwconio.h>
|
|
|
|
# endif
|
2004-12-25 17:08:02 -05:00
|
|
|
#endif
|
|
|
|
|
2015-02-03 01:57:54 -05:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2015-02-02 18:30:45 -05:00
|
|
|
#include <unistd.h>
|
2015-02-03 01:57:54 -05:00
|
|
|
#endif
|
2011-10-06 11:39:00 -04:00
|
|
|
#include "tool_getpass.h"
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2013-01-03 20:50:28 -05:00
|
|
|
#include "memdebug.h" /* keep this as LAST include */
|
2011-09-14 05:27:12 -04:00
|
|
|
|
2009-12-30 12:59:56 -05:00
|
|
|
#ifdef __VMS
|
2004-12-25 17:08:02 -05:00
|
|
|
/* VMS implementation */
|
2003-10-16 10:09:39 -04:00
|
|
|
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
|
|
|
|
{
|
2004-01-13 11:13:32 -05:00
|
|
|
long sts;
|
|
|
|
short chan;
|
2004-06-11 08:29:30 -04:00
|
|
|
|
2004-06-08 17:21:53 -04:00
|
|
|
/* MSK, 23-JAN-2004, iosbdef.h wasn't in VAX V7.2 or CC 6.4 */
|
|
|
|
/* distribution so I created this. May revert back later to */
|
|
|
|
/* struct _iosb iosb; */
|
|
|
|
struct _iosb
|
|
|
|
{
|
|
|
|
short int iosb$w_status; /* status */
|
|
|
|
short int iosb$w_bcnt; /* byte count */
|
|
|
|
int unused; /* unused */
|
|
|
|
} iosb;
|
|
|
|
|
2004-01-13 11:13:32 -05:00
|
|
|
$DESCRIPTOR(ttdesc, "TT");
|
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
buffer[0] = '\0';
|
|
|
|
sts = sys$assign(&ttdesc, &chan, 0, 0);
|
2011-04-25 16:44:39 -04:00
|
|
|
if(sts & 1) {
|
2004-01-13 11:13:32 -05:00
|
|
|
sts = sys$qiow(0, chan,
|
|
|
|
IO$_READPROMPT | IO$M_NOECHO,
|
|
|
|
&iosb, 0, 0, buffer, buflen, 0, 0,
|
|
|
|
prompt, strlen(prompt));
|
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
if((sts & 1) && (iosb.iosb$w_status & 1))
|
2004-01-13 11:13:32 -05:00
|
|
|
buffer[iosb.iosb$w_bcnt] = '\0';
|
2003-10-16 10:09:39 -04:00
|
|
|
|
2004-01-13 11:13:32 -05:00
|
|
|
sts = sys$dassgn(chan);
|
|
|
|
}
|
|
|
|
return buffer; /* we always return success */
|
2003-10-16 10:09:39 -04:00
|
|
|
}
|
2004-12-25 17:08:02 -05:00
|
|
|
#define DONE
|
2009-12-30 12:59:56 -05:00
|
|
|
#endif /* __VMS */
|
2004-12-25 17:08:02 -05:00
|
|
|
|
2008-04-22 18:53:53 -04:00
|
|
|
#ifdef __SYMBIAN32__
|
2011-10-06 11:39:00 -04:00
|
|
|
# define getch() getchar()
|
2008-04-22 18:53:53 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(WIN32) || defined(__SYMBIAN32__)
|
|
|
|
|
2004-02-02 02:24:30 -05:00
|
|
|
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
|
2003-10-16 10:09:39 -04:00
|
|
|
{
|
2004-03-10 11:03:12 -05:00
|
|
|
size_t i;
|
2004-12-25 17:08:02 -05:00
|
|
|
fputs(prompt, stderr);
|
2004-10-06 03:50:18 -04:00
|
|
|
|
2011-10-06 11:39:00 -04:00
|
|
|
for(i = 0; i < buflen; i++) {
|
2008-10-23 21:27:00 -04:00
|
|
|
buffer[i] = (char)getch();
|
2011-04-25 16:44:39 -04:00
|
|
|
if(buffer[i] == '\r' || buffer[i] == '\n') {
|
2011-10-06 11:39:00 -04:00
|
|
|
buffer[i] = '\0';
|
2003-10-16 10:09:39 -04:00
|
|
|
break;
|
|
|
|
}
|
2004-01-13 11:07:40 -05:00
|
|
|
else
|
2011-04-25 16:44:39 -04:00
|
|
|
if(buffer[i] == '\b')
|
2004-01-13 11:07:40 -05:00
|
|
|
/* remove this letter and if this is not the first key, remove the
|
|
|
|
previous one as well */
|
2014-01-27 09:52:42 -05:00
|
|
|
i = i - (i >= 1 ? 2 : 1);
|
2003-10-16 10:09:39 -04:00
|
|
|
}
|
2010-02-14 14:40:18 -05:00
|
|
|
#ifndef __SYMBIAN32__
|
2007-07-20 12:01:05 -04:00
|
|
|
/* since echo is disabled, print a newline */
|
|
|
|
fputs("\n", stderr);
|
2008-04-22 18:53:53 -04:00
|
|
|
#endif
|
2003-10-16 10:09:39 -04:00
|
|
|
/* if user didn't hit ENTER, terminate buffer */
|
2011-10-06 11:39:00 -04:00
|
|
|
if(i == buflen)
|
|
|
|
buffer[buflen-1] = '\0';
|
2003-10-16 10:09:39 -04:00
|
|
|
|
|
|
|
return buffer; /* we always return success */
|
|
|
|
}
|
2004-12-25 17:08:02 -05:00
|
|
|
#define DONE
|
2008-04-22 18:53:53 -04:00
|
|
|
#endif /* WIN32 || __SYMBIAN32__ */
|
2004-12-25 17:08:02 -05:00
|
|
|
|
2007-04-14 12:38:13 -04:00
|
|
|
#ifdef NETWARE
|
|
|
|
/* NetWare implementation */
|
2007-06-30 16:08:13 -04:00
|
|
|
#ifdef __NOVELL_LIBC__
|
2007-04-14 12:38:13 -04:00
|
|
|
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
|
|
|
|
{
|
2007-04-14 12:55:17 -04:00
|
|
|
return getpassword(prompt, buffer, buflen);
|
2007-04-14 12:38:13 -04:00
|
|
|
}
|
2007-06-30 16:08:13 -04:00
|
|
|
#else
|
|
|
|
char *getpass_r(const char *prompt, char *buffer, size_t buflen)
|
|
|
|
{
|
2007-07-06 10:58:59 -04:00
|
|
|
size_t i = 0;
|
2007-06-30 16:08:13 -04:00
|
|
|
|
|
|
|
printf("%s", prompt);
|
|
|
|
do {
|
2007-07-06 10:58:59 -04:00
|
|
|
buffer[i++] = getch();
|
2011-04-25 16:44:39 -04:00
|
|
|
if(buffer[i-1] == '\b') {
|
2007-07-06 10:58:59 -04:00
|
|
|
/* remove this letter and if this is not the first key,
|
|
|
|
remove the previous one as well */
|
2011-04-25 16:44:39 -04:00
|
|
|
if(i > 1) {
|
2007-07-06 10:58:59 -04:00
|
|
|
printf("\b \b");
|
|
|
|
i = i - 2;
|
2011-04-25 16:44:39 -04:00
|
|
|
}
|
|
|
|
else {
|
2007-07-06 10:58:59 -04:00
|
|
|
RingTheBell();
|
|
|
|
i = i - 1;
|
|
|
|
}
|
2007-06-30 16:08:13 -04:00
|
|
|
}
|
2011-04-25 16:44:39 -04:00
|
|
|
else if(buffer[i-1] != 13)
|
|
|
|
putchar('*');
|
|
|
|
|
|
|
|
} while((buffer[i-1] != 13) && (i < buflen));
|
2011-10-06 11:39:00 -04:00
|
|
|
buffer[i-1] = '\0';
|
2007-07-06 10:58:59 -04:00
|
|
|
printf("\r\n");
|
2007-06-30 16:08:13 -04:00
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
#endif /* __NOVELL_LIBC__ */
|
2007-04-14 12:38:13 -04:00
|
|
|
#define DONE
|
2007-04-14 12:45:43 -04:00
|
|
|
#endif /* NETWARE */
|
2007-04-14 12:38:13 -04:00
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifndef DONE /* not previously provided */
|
|
|
|
|
|
|
|
#ifdef HAVE_TERMIOS_H
|
2011-10-06 11:39:00 -04:00
|
|
|
# define struct_term struct termios
|
|
|
|
#elif defined(HAVE_TERMIO_H)
|
|
|
|
# define struct_term struct termio
|
2004-12-25 17:08:02 -05:00
|
|
|
#else
|
2011-10-06 11:39:00 -04:00
|
|
|
# undef struct_term
|
2003-10-16 10:09:39 -04:00
|
|
|
#endif
|
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
static bool ttyecho(bool enable, int fd)
|
|
|
|
{
|
|
|
|
#ifdef struct_term
|
|
|
|
static struct_term withecho;
|
|
|
|
static struct_term noecho;
|
|
|
|
#endif
|
|
|
|
if(!enable) {
|
2011-04-25 16:44:39 -04:00
|
|
|
/* disable echo by extracting the current 'withecho' mode and remove the
|
|
|
|
ECHO bit and set back the struct */
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
|
|
|
tcgetattr(fd, &withecho);
|
|
|
|
noecho = withecho;
|
|
|
|
noecho.c_lflag &= ~ECHO;
|
|
|
|
tcsetattr(fd, TCSANOW, &noecho);
|
2011-10-06 11:39:00 -04:00
|
|
|
#elif defined(HAVE_TERMIO_H)
|
2004-12-25 17:08:02 -05:00
|
|
|
ioctl(fd, TCGETA, &withecho);
|
|
|
|
noecho = withecho;
|
|
|
|
noecho.c_lflag &= ~ECHO;
|
|
|
|
ioctl(fd, TCSETA, &noecho);
|
2011-10-06 11:39:00 -04:00
|
|
|
#else
|
|
|
|
/* neither HAVE_TERMIO_H nor HAVE_TERMIOS_H, we can't disable echo! */
|
|
|
|
(void)fd;
|
2004-12-25 17:08:02 -05:00
|
|
|
return FALSE; /* not disabled */
|
|
|
|
#endif
|
|
|
|
return TRUE; /* disabled */
|
|
|
|
}
|
2017-03-10 08:28:37 -05:00
|
|
|
/* re-enable echo, assumes we disabled it before (and set the structs we
|
|
|
|
now use to reset the terminal status) */
|
2004-12-25 17:08:02 -05:00
|
|
|
#ifdef HAVE_TERMIOS_H
|
2017-03-10 08:28:37 -05:00
|
|
|
tcsetattr(fd, TCSAFLUSH, &withecho);
|
2011-10-06 11:39:00 -04:00
|
|
|
#elif defined(HAVE_TERMIO_H)
|
2017-03-10 08:28:37 -05:00
|
|
|
ioctl(fd, TCSETA, &withecho);
|
2004-12-25 17:08:02 -05:00
|
|
|
#else
|
2017-03-10 08:28:37 -05:00
|
|
|
return FALSE; /* not enabled */
|
2004-12-25 17:08:02 -05:00
|
|
|
#endif
|
2017-03-10 08:28:37 -05:00
|
|
|
return TRUE; /* enabled */
|
2004-12-25 17:08:02 -05:00
|
|
|
}
|
2003-10-16 10:09:39 -04:00
|
|
|
|
2004-12-25 17:08:02 -05:00
|
|
|
char *getpass_r(const char *prompt, /* prompt to display */
|
|
|
|
char *password, /* buffer to store password in */
|
|
|
|
size_t buflen) /* size of buffer to store password in */
|
2003-10-16 10:09:39 -04:00
|
|
|
{
|
2004-12-25 17:08:02 -05:00
|
|
|
ssize_t nread;
|
|
|
|
bool disabled;
|
2011-10-06 11:39:00 -04:00
|
|
|
int fd = open("/dev/tty", O_RDONLY);
|
2004-12-25 17:08:02 -05:00
|
|
|
if(-1 == fd)
|
2015-02-02 18:30:45 -05:00
|
|
|
fd = STDIN_FILENO; /* use stdin if the tty couldn't be used */
|
2004-12-25 17:08:02 -05:00
|
|
|
|
|
|
|
disabled = ttyecho(FALSE, fd); /* disable terminal echo */
|
|
|
|
|
|
|
|
fputs(prompt, stderr);
|
2011-10-06 11:39:00 -04:00
|
|
|
nread = read(fd, password, buflen);
|
2004-12-25 17:08:02 -05:00
|
|
|
if(nread > 0)
|
2011-10-06 11:39:00 -04:00
|
|
|
password[--nread] = '\0'; /* zero terminate where enter is stored */
|
2004-12-25 17:08:02 -05:00
|
|
|
else
|
2011-10-06 11:39:00 -04:00
|
|
|
password[0] = '\0'; /* got nothing */
|
2004-12-25 17:08:02 -05:00
|
|
|
|
|
|
|
if(disabled) {
|
|
|
|
/* if echo actually was disabled, add a newline */
|
|
|
|
fputs("\n", stderr);
|
2005-12-13 13:54:31 -05:00
|
|
|
(void)ttyecho(TRUE, fd); /* enable echo */
|
2004-12-25 17:08:02 -05:00
|
|
|
}
|
|
|
|
|
2015-02-02 18:30:45 -05:00
|
|
|
if(STDIN_FILENO != fd)
|
2004-12-25 17:08:02 -05:00
|
|
|
close(fd);
|
|
|
|
|
|
|
|
return password; /* return pointer to buffer */
|
2003-10-16 10:09:39 -04:00
|
|
|
}
|
2004-12-25 17:08:02 -05:00
|
|
|
|
2005-01-04 11:13:58 -05:00
|
|
|
#endif /* DONE */
|
2004-12-25 17:08:02 -05:00
|
|
|
#endif /* HAVE_GETPASS_R */
|