1
/* Copyright (C) 2000-2004 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.
7
There are special exceptions to the terms and conditions of the GPL as it
8
is applied to this software. View the full text of the exception in file
9
EXCEPTIONS-CLIENT in the directory of this software distribution.
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
GNU General Public License for more details.
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */
21
** Ask for a password from tty
22
** This is an own file to avoid conflicts with curses
24
#include <my_global.h>
30
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
37
#endif /* HAVE_PWD_H */
38
#else /* ! HAVE_GETPASS */
39
#if !defined(__WIN__) && !defined(__NETWARE__)
40
#include <sys/ioctl.h>
41
#ifdef HAVE_TERMIOS_H /* For tty-password */
43
#define TERMIO struct termios
45
#ifdef HAVE_TERMIO_H /* For tty-password */
47
#define TERMIO struct termio
50
#define TERMIO struct sgttyb
53
#ifdef alpha_linux_port
54
#include <asm/ioctls.h> /* QQ; Fix this in configure */
55
#include <asm/termiobits.h>
60
#endif /* __NETWARE__ */
62
#endif /* HAVE_GETPASS */
64
#ifdef HAVE_GETPASSPHRASE /* For Solaris */
65
#define getpass(A) getpassphrase(A)
68
#if defined( __WIN__) || defined(__NETWARE__)
69
/* were just going to fake it here and get input from the keyboard */
74
#define _getch getcharacter
75
#define _cputs(A) putstring(A)
78
char *get_tty_password(const char *opt_message)
81
char *pos=to,*end=to+sizeof(to)-1;
83
DBUG_ENTER("get_tty_password");
84
_cputs(opt_message ? opt_message : "Enter password: ");
89
if (tmp == '\b' || (int) tmp == 127)
98
if (tmp == '\n' || tmp == '\r' || tmp == 3)
100
if (iscntrl(tmp) || pos == end)
105
while (pos != to && isspace(pos[-1]) == ' ')
106
pos--; /* Allow dummy space at end */
109
DBUG_RETURN(my_strdup(to,MYF(MY_FAE)));
116
Can't use fgets, because readline will get confused
117
length is max number of chars in to, not counting \0
118
to will not include the eol characters.
121
static void get_password(char *to,uint length,int fd, my_bool echo)
123
char *pos=to,*end=to+length;
128
if (my_read(fd,&tmp,1,MYF(0)) != 1)
130
if (tmp == '\b' || (int) tmp == 127)
136
fputs("\b \b",stdout);
143
if (tmp == '\n' || tmp == '\r' || tmp == 3)
145
if (iscntrl(tmp) || pos == end)
154
while (pos != to && isspace(pos[-1]) == ' ')
155
pos--; /* Allow dummy space at end */
159
#endif /* ! HAVE_GETPASS */
162
char *get_tty_password(const char *opt_message)
166
#else /* ! HAVE_GETPASS */
168
#endif /* HAVE_GETPASS */
171
DBUG_ENTER("get_tty_password");
174
passbuff = getpass(opt_message ? opt_message : "Enter password: ");
176
/* copy the password to buff and clear original (static) buffer */
177
strnmov(buff, passbuff, sizeof(buff) - 1);
179
memset(passbuff, 0, _PASSWORD_LEN);
182
if (isatty(fileno(stdout)))
184
fputs(opt_message ? opt_message : "Enter password: ",stdout);
187
#if defined(HAVE_TERMIOS_H)
188
tcgetattr(fileno(stdin), &org);
190
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
193
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
194
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
195
tcsetattr(fileno(stdin), TCSADRAIN, &org);
196
#elif defined(HAVE_TERMIO_H)
197
ioctl(fileno(stdin), (int) TCGETA, &org);
199
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
202
ioctl(fileno(stdin),(int) TCSETA, &tmp);
203
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
204
ioctl(fileno(stdin),(int) TCSETA, &org);
206
gtty(fileno(stdin), &org);
208
tmp.sg_flags &= ~ECHO;
210
stty(fileno(stdin), &tmp);
211
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
212
stty(fileno(stdin), &org);
214
if (isatty(fileno(stdout)))
216
#endif /* HAVE_GETPASS */
218
DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));