1
by brian
clean slate |
1 |
/* Copyright (C) 2000 MySQL AB
|
2 |
||
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.
|
|
6 |
||
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.
|
|
11 |
||
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 */
|
|
15 |
||
16 |
/*
|
|
17 |
** Ask for a password from tty
|
|
18 |
** This is an own file to avoid conflicts with curses
|
|
19 |
*/
|
|
20 |
#include <my_global.h> |
|
21 |
#include <my_sys.h> |
|
22 |
#include "mysql.h" |
|
23 |
#include <m_string.h> |
|
24 |
#include <m_ctype.h> |
|
25 |
||
26 |
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
|
|
27 |
#undef HAVE_GETPASS
|
|
28 |
#endif
|
|
29 |
||
30 |
#ifdef HAVE_GETPASS
|
|
31 |
#ifdef HAVE_PWD_H
|
|
32 |
#include <pwd.h> |
|
33 |
#endif /* HAVE_PWD_H */ |
|
34 |
#else /* ! HAVE_GETPASS */ |
|
12.2.5
by Stewart Smith
remove more #ifdef __WIN__ code |
35 |
#include <sys/ioctl.h> |
36 |
#ifdef HAVE_TERMIOS_H /* For tty-password */ |
|
37 |
#include <termios.h> |
|
38 |
#define TERMIO struct termios
|
|
39 |
#else
|
|
40 |
#ifdef HAVE_TERMIO_H /* For tty-password */ |
|
41 |
#include <termio.h> |
|
42 |
#define TERMIO struct termio
|
|
43 |
#else
|
|
44 |
#include <sgtty.h> |
|
45 |
#define TERMIO struct sgttyb
|
|
46 |
#endif
|
|
47 |
#endif
|
|
48 |
#ifdef alpha_linux_port
|
|
49 |
#include <asm/ioctls.h> /* QQ; Fix this in configure */ |
|
50 |
#include <asm/termiobits.h> |
|
51 |
#endif
|
|
1
by brian
clean slate |
52 |
#endif /* HAVE_GETPASS */ |
53 |
||
54 |
#ifdef HAVE_GETPASSPHRASE /* For Solaris */ |
|
55 |
#define getpass(A) getpassphrase(A)
|
|
56 |
#endif
|
|
57 |
||
58 |
#ifndef HAVE_GETPASS
|
|
59 |
/*
|
|
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.
|
|
63 |
*/
|
|
64 |
||
65 |
static void get_password(char *to,uint length,int fd, my_bool echo) |
|
66 |
{
|
|
67 |
char *pos=to,*end=to+length; |
|
68 |
||
69 |
for (;;) |
|
70 |
{
|
|
71 |
char tmp; |
|
72 |
if (my_read(fd,&tmp,1,MYF(0)) != 1) |
|
73 |
break; |
|
74 |
if (tmp == '\b' || (int) tmp == 127) |
|
75 |
{
|
|
76 |
if (pos != to) |
|
77 |
{
|
|
78 |
if (echo) |
|
79 |
{
|
|
80 |
fputs("\b \b",stderr); |
|
81 |
fflush(stderr); |
|
82 |
}
|
|
83 |
pos--; |
|
84 |
continue; |
|
85 |
}
|
|
86 |
}
|
|
87 |
if (tmp == '\n' || tmp == '\r' || tmp == 3) |
|
88 |
break; |
|
89 |
if (iscntrl(tmp) || pos == end) |
|
90 |
continue; |
|
91 |
if (echo) |
|
92 |
{
|
|
93 |
fputc('*',stderr); |
|
94 |
fflush(stderr); |
|
95 |
}
|
|
96 |
*(pos++) = tmp; |
|
97 |
}
|
|
98 |
while (pos != to && isspace(pos[-1]) == ' ') |
|
99 |
pos--; /* Allow dummy space at end */ |
|
100 |
*pos=0; |
|
101 |
return; |
|
102 |
}
|
|
103 |
||
104 |
#endif /* ! HAVE_GETPASS */ |
|
105 |
||
106 |
||
107 |
char *get_tty_password(const char *opt_message) |
|
108 |
{
|
|
109 |
#ifdef HAVE_GETPASS
|
|
110 |
char *passbuff; |
|
111 |
#else /* ! HAVE_GETPASS */ |
|
112 |
TERMIO org,tmp; |
|
113 |
#endif /* HAVE_GETPASS */ |
|
114 |
char buff[80]; |
|
115 |
||
116 |
DBUG_ENTER("get_tty_password"); |
|
117 |
||
118 |
#ifdef HAVE_GETPASS
|
|
119 |
passbuff = getpass(opt_message ? opt_message : "Enter password: "); |
|
120 |
||
121 |
/* copy the password to buff and clear original (static) buffer */
|
|
122 |
strnmov(buff, passbuff, sizeof(buff) - 1); |
|
123 |
#ifdef _PASSWORD_LEN
|
|
124 |
memset(passbuff, 0, _PASSWORD_LEN); |
|
125 |
#endif
|
|
126 |
#else
|
|
127 |
if (isatty(fileno(stderr))) |
|
128 |
{
|
|
129 |
fputs(opt_message ? opt_message : "Enter password: ",stderr); |
|
130 |
fflush(stderr); |
|
131 |
}
|
|
132 |
#if defined(HAVE_TERMIOS_H)
|
|
133 |
tcgetattr(fileno(stdin), &org); |
|
134 |
tmp = org; |
|
135 |
tmp.c_lflag &= ~(ECHO | ISIG | ICANON); |
|
136 |
tmp.c_cc[VMIN] = 1; |
|
137 |
tmp.c_cc[VTIME] = 0; |
|
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); |
|
143 |
tmp=org; |
|
144 |
tmp.c_lflag &= ~(ECHO | ISIG | ICANON); |
|
145 |
tmp.c_cc[VMIN] = 1; |
|
146 |
tmp.c_cc[VTIME]= 0; |
|
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); |
|
150 |
#else
|
|
151 |
gtty(fileno(stdin), &org); |
|
152 |
tmp=org; |
|
153 |
tmp.sg_flags &= ~ECHO; |
|
154 |
tmp.sg_flags |= RAW; |
|
155 |
stty(fileno(stdin), &tmp); |
|
156 |
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr))); |
|
157 |
stty(fileno(stdin), &org); |
|
158 |
#endif
|
|
159 |
if (isatty(fileno(stderr))) |
|
160 |
fputc('\n',stderr); |
|
161 |
#endif /* HAVE_GETPASS */ |
|
162 |
||
163 |
DBUG_RETURN(my_strdup(buff,MYF(MY_FAE))); |
|
164 |
}
|
|
165 |