00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #include <my_global.h>
00025 #include <my_sys.h>
00026 #include "mysql.h"
00027 #include <m_string.h>
00028 #include <m_ctype.h>
00029
00030 #if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
00031 #undef HAVE_GETPASS
00032 #endif
00033
00034 #ifdef HAVE_GETPASS
00035 #ifdef HAVE_PWD_H
00036 #include <pwd.h>
00037 #endif
00038 #else
00039 #if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
00040 #include <sys/ioctl.h>
00041 #ifdef HAVE_TERMIOS_H
00042 #include <termios.h>
00043 #define TERMIO struct termios
00044 #else
00045 #ifdef HAVE_TERMIO_H
00046 #include <termio.h>
00047 #define TERMIO struct termio
00048 #else
00049 #include <sgtty.h>
00050 #define TERMIO struct sgttyb
00051 #endif
00052 #endif
00053 #ifdef alpha_linux_port
00054 #include <asm/ioctls.h>
00055 #include <asm/termiobits.h>
00056 #endif
00057 #else
00058 #ifndef __NETWARE__
00059 #include <conio.h>
00060 #endif
00061 #endif
00062 #endif
00063
00064 #ifdef HAVE_GETPASSPHRASE
00065 #define getpass(A) getpassphrase(A)
00066 #endif
00067
00068 #if defined( __WIN__) || defined(OS2) || defined(__NETWARE__)
00069
00070
00071 #ifdef __NETWARE__
00072 #undef _getch
00073 #undef _cputs
00074 #define _getch getcharacter
00075 #define _cputs(A) putstring(A)
00076 #endif
00077
00078 char *get_tty_password(char *opt_message)
00079 {
00080 char to[80];
00081 char *pos=to,*end=to+sizeof(to)-1;
00082 int i=0;
00083 DBUG_ENTER("get_tty_password");
00084 _cputs(opt_message ? opt_message : "Enter password: ");
00085 for (;;)
00086 {
00087 char tmp;
00088 tmp=_getch();
00089 if (tmp == '\b' || (int) tmp == 127)
00090 {
00091 if (pos != to)
00092 {
00093 _cputs("\b \b");
00094 pos--;
00095 continue;
00096 }
00097 }
00098 if (tmp == '\n' || tmp == '\r' || tmp == 3)
00099 break;
00100 if (iscntrl(tmp) || pos == end)
00101 continue;
00102 _cputs("*");
00103 *(pos++) = tmp;
00104 }
00105 while (pos != to && isspace(pos[-1]) == ' ')
00106 pos--;
00107 *pos=0;
00108 _cputs("\n");
00109 DBUG_RETURN(my_strdup(to,MYF(MY_FAE)));
00110 }
00111
00112 #else
00113
00114 #ifndef HAVE_GETPASS
00115
00116
00117
00118
00119
00120
00121 static void get_password(char *to,uint length,int fd,bool echo)
00122 {
00123 char *pos=to,*end=to+length;
00124
00125 for (;;)
00126 {
00127 char tmp;
00128 if (my_read(fd,&tmp,1,MYF(0)) != 1)
00129 break;
00130 if (tmp == '\b' || (int) tmp == 127)
00131 {
00132 if (pos != to)
00133 {
00134 if (echo)
00135 {
00136 fputs("\b \b",stdout);
00137 fflush(stdout);
00138 }
00139 pos--;
00140 continue;
00141 }
00142 }
00143 if (tmp == '\n' || tmp == '\r' || tmp == 3)
00144 break;
00145 if (iscntrl(tmp) || pos == end)
00146 continue;
00147 if (echo)
00148 {
00149 fputc('*',stdout);
00150 fflush(stdout);
00151 }
00152 *(pos++) = tmp;
00153 }
00154 while (pos != to && isspace(pos[-1]) == ' ')
00155 pos--;
00156 *pos=0;
00157 return;
00158 }
00159 #endif
00160
00161
00162 char *get_tty_password(char *opt_message)
00163 {
00164 #ifdef HAVE_GETPASS
00165 char *passbuff;
00166 #else
00167 TERMIO org,tmp;
00168 #endif
00169 char buff[80];
00170
00171 DBUG_ENTER("get_tty_password");
00172
00173 #ifdef HAVE_GETPASS
00174 passbuff = getpass(opt_message ? opt_message : "Enter password: ");
00175
00176
00177 strnmov(buff, passbuff, sizeof(buff) - 1);
00178 #ifdef _PASSWORD_LEN
00179 memset(passbuff, 0, _PASSWORD_LEN);
00180 #endif
00181 #else
00182 if (isatty(fileno(stdout)))
00183 {
00184 fputs(opt_message ? opt_message : "Enter password: ",stdout);
00185 fflush(stdout);
00186 }
00187 #if defined(HAVE_TERMIOS_H)
00188 tcgetattr(fileno(stdin), &org);
00189 tmp = org;
00190 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
00191 tmp.c_cc[VMIN] = 1;
00192 tmp.c_cc[VTIME] = 0;
00193 tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
00194 get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
00195 tcsetattr(fileno(stdin), TCSADRAIN, &org);
00196 #elif defined(HAVE_TERMIO_H)
00197 ioctl(fileno(stdin), (int) TCGETA, &org);
00198 tmp=org;
00199 tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
00200 tmp.c_cc[VMIN] = 1;
00201 tmp.c_cc[VTIME]= 0;
00202 ioctl(fileno(stdin),(int) TCSETA, &tmp);
00203 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
00204 ioctl(fileno(stdin),(int) TCSETA, &org);
00205 #else
00206 gtty(fileno(stdin), &org);
00207 tmp=org;
00208 tmp.sg_flags &= ~ECHO;
00209 tmp.sg_flags |= RAW;
00210 stty(fileno(stdin), &tmp);
00211 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
00212 stty(fileno(stdin), &org);
00213 #endif
00214 if (isatty(fileno(stdout)))
00215 fputc('\n',stdout);
00216 #endif
00217
00218 DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));
00219 }
00220 #endif