mySQL 5.0.11 sources for tomato
[tomato.git] / release / src / router / mysql / libmysql / get_password.c
blob48a4dfe14bf12e7f24d3071c9033c772c36eb6d0
1 /* Copyright (c) 2000, 2011, Oracle and/or its affiliates. All rights reserved.
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.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA */
20 ** Ask for a password from tty
21 ** This is an own file to avoid conflicts with curses
23 #include <my_global.h>
24 #include <my_sys.h>
25 #include "mysql.h"
26 #include <m_string.h>
27 #include <m_ctype.h>
29 #if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
30 #undef HAVE_GETPASS
31 #endif
33 #ifdef HAVE_GETPASS
34 #ifdef HAVE_PWD_H
35 #include <pwd.h>
36 #endif /* HAVE_PWD_H */
37 #else /* ! HAVE_GETPASS */
38 #if !defined(__WIN__) && !defined(__NETWARE__)
39 #include <sys/ioctl.h>
40 #ifdef HAVE_TERMIOS_H /* For tty-password */
41 #include <termios.h>
42 #define TERMIO struct termios
43 #else
44 #ifdef HAVE_TERMIO_H /* For tty-password */
45 #include <termio.h>
46 #define TERMIO struct termio
47 #else
48 #include <sgtty.h>
49 #define TERMIO struct sgttyb
50 #endif
51 #endif
52 #ifdef alpha_linux_port
53 #include <asm/ioctls.h> /* QQ; Fix this in configure */
54 #include <asm/termiobits.h>
55 #endif
56 #else
57 #ifndef __NETWARE__
58 #include <conio.h>
59 #endif /* __NETWARE__ */
60 #endif /* __WIN__ */
61 #endif /* HAVE_GETPASS */
63 #ifdef HAVE_GETPASSPHRASE /* For Solaris */
64 #define getpass(A) getpassphrase(A)
65 #endif
67 #if defined( __WIN__) || defined(__NETWARE__)
68 /* were just going to fake it here and get input from the keyboard */
70 #ifdef __NETWARE__
71 #undef _getch
72 #undef _cputs
73 #define _getch getcharacter
74 #define _cputs(A) putstring(A)
75 #endif
77 char *get_tty_password(const char *opt_message)
79 char to[80];
80 char *pos=to,*end=to+sizeof(to)-1;
81 int i=0;
82 DBUG_ENTER("get_tty_password");
83 _cputs(opt_message ? opt_message : "Enter password: ");
84 for (;;)
86 char tmp;
87 tmp=_getch();
88 if (tmp == '\b' || (int) tmp == 127)
90 if (pos != to)
92 _cputs("\b \b");
93 pos--;
94 continue;
97 if (tmp == '\n' || tmp == '\r' || tmp == 3)
98 break;
99 if (iscntrl(tmp) || pos == end)
100 continue;
101 _cputs("*");
102 *(pos++) = tmp;
104 while (pos != to && isspace(pos[-1]) == ' ')
105 pos--; /* Allow dummy space at end */
106 *pos=0;
107 _cputs("\n");
108 DBUG_RETURN(my_strdup(to,MYF(MY_FAE)));
111 #else
113 #ifndef HAVE_GETPASS
115 Can't use fgets, because readline will get confused
116 length is max number of chars in to, not counting \0
117 to will not include the eol characters.
120 static void get_password(char *to,uint length,int fd, my_bool echo)
122 char *pos=to,*end=to+length;
124 for (;;)
126 char tmp;
127 if (my_read(fd,&tmp,1,MYF(0)) != 1)
128 break;
129 if (tmp == '\b' || (int) tmp == 127)
131 if (pos != to)
133 if (echo)
135 fputs("\b \b",stdout);
136 fflush(stdout);
138 pos--;
139 continue;
142 if (tmp == '\n' || tmp == '\r' || tmp == 3)
143 break;
144 if (iscntrl(tmp) || pos == end)
145 continue;
146 if (echo)
148 fputc('*',stdout);
149 fflush(stdout);
151 *(pos++) = tmp;
153 while (pos != to && isspace(pos[-1]) == ' ')
154 pos--; /* Allow dummy space at end */
155 *pos=0;
156 return;
158 #endif /* ! HAVE_GETPASS */
161 char *get_tty_password(const char *opt_message)
163 #ifdef HAVE_GETPASS
164 char *passbuff;
165 #else /* ! HAVE_GETPASS */
166 TERMIO org,tmp;
167 #endif /* HAVE_GETPASS */
168 char buff[80];
170 DBUG_ENTER("get_tty_password");
172 #ifdef HAVE_GETPASS
173 passbuff = getpass(opt_message ? opt_message : "Enter password: ");
175 /* copy the password to buff and clear original (static) buffer */
176 strnmov(buff, passbuff, sizeof(buff) - 1);
177 #ifdef _PASSWORD_LEN
178 memset(passbuff, 0, _PASSWORD_LEN);
179 #endif
180 #else
181 if (isatty(fileno(stdout)))
183 fputs(opt_message ? opt_message : "Enter password: ",stdout);
184 fflush(stdout);
186 #if defined(HAVE_TERMIOS_H)
187 tcgetattr(fileno(stdin), &org);
188 tmp = org;
189 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
190 tmp.c_cc[VMIN] = 1;
191 tmp.c_cc[VTIME] = 0;
192 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
193 get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
194 tcsetattr(fileno(stdin), TCSADRAIN, &org);
195 #elif defined(HAVE_TERMIO_H)
196 ioctl(fileno(stdin), (int) TCGETA, &org);
197 tmp=org;
198 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
199 tmp.c_cc[VMIN] = 1;
200 tmp.c_cc[VTIME]= 0;
201 ioctl(fileno(stdin),(int) TCSETA, &tmp);
202 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
203 ioctl(fileno(stdin),(int) TCSETA, &org);
204 #else
205 gtty(fileno(stdin), &org);
206 tmp=org;
207 tmp.sg_flags &= ~ECHO;
208 tmp.sg_flags |= RAW;
209 stty(fileno(stdin), &tmp);
210 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
211 stty(fileno(stdin), &org);
212 #endif
213 if (isatty(fileno(stdout)))
214 fputc('\n',stdout);
215 #endif /* HAVE_GETPASS */
217 DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));
219 #endif /*__WIN__*/