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 */
35
#include <sys/ioctl.h>
36
#ifdef HAVE_TERMIOS_H /* For tty-password */
38
#define TERMIO struct termios
40
#ifdef HAVE_TERMIO_H /* For tty-password */
42
#define TERMIO struct termio
45
#define TERMIO struct sgttyb
48
#ifdef alpha_linux_port
49
#include <asm/ioctls.h> /* QQ; Fix this in configure */
50
#include <asm/termiobits.h>
52
#endif /* HAVE_GETPASS */
54
#ifdef HAVE_GETPASSPHRASE /* For Solaris */
55
#define getpass(A) getpassphrase(A)
60
** Can't use fgets, because readline will get confused
61
** length is max number of chars in to, not counting \0
62
* to will not include the eol characters.
65
static void get_password(char *to,uint length,int fd, my_bool echo)
67
char *pos=to,*end=to+length;
72
if (my_read(fd,&tmp,1,MYF(0)) != 1)
74
if (tmp == '\b' || (int) tmp == 127)
80
fputs("\b \b",stderr);
87
if (tmp == '\n' || tmp == '\r' || tmp == 3)
89
if (iscntrl(tmp) || pos == end)
98
while (pos != to && isspace(pos[-1]) == ' ')
99
pos--; /* Allow dummy space at end */
104
#endif /* ! HAVE_GETPASS */
107
char *get_tty_password(const char *opt_message)
111
#else /* ! HAVE_GETPASS */
113
#endif /* HAVE_GETPASS */
116
DBUG_ENTER("get_tty_password");
119
passbuff = getpass(opt_message ? opt_message : "Enter password: ");
121
/* copy the password to buff and clear original (static) buffer */
122
strnmov(buff, passbuff, sizeof(buff) - 1);
124
memset(passbuff, 0, _PASSWORD_LEN);
127
if (isatty(fileno(stderr)))
129
fputs(opt_message ? opt_message : "Enter password: ",stderr);
132
#if defined(HAVE_TERMIOS_H)
133
tcgetattr(fileno(stdin), &org);
135
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
138
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
139
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr)));
140
tcsetattr(fileno(stdin), TCSADRAIN, &org);
141
#elif defined(HAVE_TERMIO_H)
142
ioctl(fileno(stdin), (int) TCGETA, &org);
144
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
147
ioctl(fileno(stdin),(int) TCSETA, &tmp);
148
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
149
ioctl(fileno(stdin),(int) TCSETA, &org);
151
gtty(fileno(stdin), &org);
153
tmp.sg_flags &= ~ECHO;
155
stty(fileno(stdin), &tmp);
156
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr)));
157
stty(fileno(stdin), &org);
159
if (isatty(fileno(stderr)))
161
#endif /* HAVE_GETPASS */
163
DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));