1
/* Copyright (C) 2000 MySQL AB
3
This program is free software; you can redistribute it and/or modify
4
it under the terms of the GNU General Public License as published by
5
the Free Software Foundation; version 2 of the License.
7
This program is distributed in the hope that it will be useful,
8
but WITHOUT ANY WARRANTY; without even the implied warranty of
9
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
GNU General Public License for more details.
12
You should have received a copy of the GNU General Public License
13
along with this program; if not, write to the Free Software
14
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
17
** Ask for a password from tty
18
** This is an own file to avoid conflicts with curses
20
#include <my_global.h>
26
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
33
#endif /* HAVE_PWD_H */
34
#else /* ! HAVE_GETPASS */
36
#include <sys/ioctl.h>
37
#ifdef HAVE_TERMIOS_H /* For tty-password */
39
#define TERMIO struct termios
41
#ifdef HAVE_TERMIO_H /* For tty-password */
43
#define TERMIO struct termio
46
#define TERMIO struct sgttyb
49
#ifdef alpha_linux_port
50
#include <asm/ioctls.h> /* QQ; Fix this in configure */
51
#include <asm/termiobits.h>
56
#endif /* HAVE_GETPASS */
58
#ifdef HAVE_GETPASSPHRASE /* For Solaris */
59
#define getpass(A) getpassphrase(A)
63
/* were just going to fake it here and get input from
66
char *get_tty_password(const char *opt_message)
69
char *pos=to,*end=to+sizeof(to)-1;
71
DBUG_ENTER("get_tty_password");
72
_cputs(opt_message ? opt_message : "Enter password: ");
77
if (tmp == '\b' || (int) tmp == 127)
86
if (tmp == '\n' || tmp == '\r' || tmp == 3)
88
if (iscntrl(tmp) || pos == end)
93
while (pos != to && isspace(pos[-1]) == ' ')
94
pos--; /* Allow dummy space at end */
97
DBUG_RETURN(my_strdup(to,MYF(MY_FAE)));
105
** Can't use fgets, because readline will get confused
106
** length is max number of chars in to, not counting \0
107
* to will not include the eol characters.
110
static void get_password(char *to,uint length,int fd, my_bool echo)
112
char *pos=to,*end=to+length;
117
if (my_read(fd,&tmp,1,MYF(0)) != 1)
119
if (tmp == '\b' || (int) tmp == 127)
125
fputs("\b \b",stderr);
132
if (tmp == '\n' || tmp == '\r' || tmp == 3)
134
if (iscntrl(tmp) || pos == end)
143
while (pos != to && isspace(pos[-1]) == ' ')
144
pos--; /* Allow dummy space at end */
149
#endif /* ! HAVE_GETPASS */
152
char *get_tty_password(const char *opt_message)
156
#else /* ! HAVE_GETPASS */
158
#endif /* HAVE_GETPASS */
161
DBUG_ENTER("get_tty_password");
164
passbuff = getpass(opt_message ? opt_message : "Enter password: ");
166
/* copy the password to buff and clear original (static) buffer */
167
strnmov(buff, passbuff, sizeof(buff) - 1);
169
memset(passbuff, 0, _PASSWORD_LEN);
172
if (isatty(fileno(stderr)))
174
fputs(opt_message ? opt_message : "Enter password: ",stderr);
177
#if defined(HAVE_TERMIOS_H)
178
tcgetattr(fileno(stdin), &org);
180
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
183
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
184
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
185
tcsetattr(fileno(stdin), TCSADRAIN, &org);
186
#elif defined(HAVE_TERMIO_H)
187
ioctl(fileno(stdin), (int) TCGETA, &org);
189
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
192
ioctl(fileno(stdin),(int) TCSETA, &tmp);
193
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
194
ioctl(fileno(stdin),(int) TCSETA, &org);
196
gtty(fileno(stdin), &org);
198
tmp.sg_flags &= ~ECHO;
200
stty(fileno(stdin), &tmp);
201
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
202
stty(fileno(stdin), &org);
204
if (isatty(fileno(stderr)))
206
#endif /* HAVE_GETPASS */
208
DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));