00001 /* Copyright (C) 2000 MySQL AB 00002 00003 This program is free software; you can redistribute it and/or modify 00004 it under the terms of the GNU General Public License as published by 00005 the Free Software Foundation; either version 2 of the License, or 00006 (at your option) any later version. 00007 00008 This program is distributed in the hope that it will be useful, 00009 but WITHOUT ANY WARRANTY; without even the implied warranty of 00010 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00011 GNU General Public License for more details. 00012 00013 You should have received a copy of the GNU General Public License 00014 along with this program; if not, write to the Free Software 00015 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA */ 00016 00017 /* 00018 ** Ask for a password from tty 00019 ** This is an own file to avoid conflicts with curses 00020 */ 00021 #include <my_global.h> 00022 #include <my_sys.h> 00023 #include "mysql.h" 00024 #include <m_string.h> 00025 #include <m_ctype.h> 00026 00027 #if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE) 00028 #undef HAVE_GETPASS 00029 #endif 00030 00031 #ifdef HAVE_GETPASS 00032 #ifdef HAVE_PWD_H 00033 #include <pwd.h> 00034 #endif /* HAVE_PWD_H */ 00035 #else /* ! HAVE_GETPASS */ 00036 #ifndef __WIN__ 00037 #include <sys/ioctl.h> 00038 #ifdef HAVE_TERMIOS_H /* For tty-password */ 00039 #include <termios.h> 00040 #define TERMIO struct termios 00041 #else 00042 #ifdef HAVE_TERMIO_H /* For tty-password */ 00043 #include <termio.h> 00044 #define TERMIO struct termio 00045 #else 00046 #include <sgtty.h> 00047 #define TERMIO struct sgttyb 00048 #endif 00049 #endif 00050 #ifdef alpha_linux_port 00051 #include <asm/ioctls.h> /* QQ; Fix this in configure */ 00052 #include <asm/termiobits.h> 00053 #endif 00054 #else 00055 #include <conio.h> 00056 #endif /* __WIN__ */ 00057 #endif /* HAVE_GETPASS */ 00058 00059 #ifdef HAVE_GETPASSPHRASE /* For Solaris */ 00060 #define getpass(A) getpassphrase(A) 00061 #endif 00062 00063 #ifdef __WIN__ 00064 /* were just going to fake it here and get input from 00065 the keyboard */ 00066 00067 char *get_tty_password(const char *opt_message) 00068 { 00069 char to[80]; 00070 char *pos=to,*end=to+sizeof(to)-1; 00071 int i=0; 00072 DBUG_ENTER("get_tty_password"); 00073 _cputs(opt_message ? opt_message : "Enter password: "); 00074 for (;;) 00075 { 00076 char tmp; 00077 tmp=_getch(); 00078 if (tmp == '\b' || (int) tmp == 127) 00079 { 00080 if (pos != to) 00081 { 00082 _cputs("\b \b"); 00083 pos--; 00084 continue; 00085 } 00086 } 00087 if (tmp == '\n' || tmp == '\r' || tmp == 3) 00088 break; 00089 if (iscntrl(tmp) || pos == end) 00090 continue; 00091 _cputs("*"); 00092 *(pos++) = tmp; 00093 } 00094 while (pos != to && isspace(pos[-1]) == ' ') 00095 pos--; /* Allow dummy space at end */ 00096 *pos=0; 00097 _cputs("\n"); 00098 DBUG_RETURN(my_strdup(to,MYF(MY_FAE))); 00099 } 00100 00101 #else 00102 00103 00104 #ifndef HAVE_GETPASS 00105 /* 00106 ** Can't use fgets, because readline will get confused 00107 ** length is max number of chars in to, not counting \0 00108 * to will not include the eol characters. 00109 */ 00110 00111 static void get_password(char *to,uint length,int fd,bool echo) 00112 { 00113 char *pos=to,*end=to+length; 00114 00115 for (;;) 00116 { 00117 char tmp; 00118 if (my_read(fd,&tmp,1,MYF(0)) != 1) 00119 break; 00120 if (tmp == '\b' || (int) tmp == 127) 00121 { 00122 if (pos != to) 00123 { 00124 if (echo) 00125 { 00126 fputs("\b \b",stderr); 00127 fflush(stderr); 00128 } 00129 pos--; 00130 continue; 00131 } 00132 } 00133 if (tmp == '\n' || tmp == '\r' || tmp == 3) 00134 break; 00135 if (iscntrl(tmp) || pos == end) 00136 continue; 00137 if (echo) 00138 { 00139 fputc('*',stderr); 00140 fflush(stderr); 00141 } 00142 *(pos++) = tmp; 00143 } 00144 while (pos != to && isspace(pos[-1]) == ' ') 00145 pos--; /* Allow dummy space at end */ 00146 *pos=0; 00147 return; 00148 } 00149 00150 #endif /* ! HAVE_GETPASS */ 00151 00152 00153 char *get_tty_password(const char *opt_message) 00154 { 00155 #ifdef HAVE_GETPASS 00156 char *passbuff; 00157 #else /* ! HAVE_GETPASS */ 00158 TERMIO org,tmp; 00159 #endif /* HAVE_GETPASS */ 00160 char buff[80]; 00161 00162 DBUG_ENTER("get_tty_password"); 00163 00164 #ifdef HAVE_GETPASS 00165 passbuff = getpass(opt_message ? opt_message : "Enter password: "); 00166 00167 /* copy the password to buff and clear original (static) buffer */ 00168 strnmov(buff, passbuff, sizeof(buff) - 1); 00169 #ifdef _PASSWORD_LEN 00170 memset(passbuff, 0, _PASSWORD_LEN); 00171 #endif 00172 #else 00173 if (isatty(fileno(stderr))) 00174 { 00175 fputs(opt_message ? opt_message : "Enter password: ",stderr); 00176 fflush(stderr); 00177 } 00178 #if defined(HAVE_TERMIOS_H) 00179 tcgetattr(fileno(stdin), &org); 00180 tmp = org; 00181 tmp.c_lflag &= ~(ECHO | ISIG | ICANON); 00182 tmp.c_cc[VMIN] = 1; 00183 tmp.c_cc[VTIME] = 0; 00184 tcsetattr(fileno(stdin), TCSADRAIN, &tmp); 00185 get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stderr))); 00186 tcsetattr(fileno(stdin), TCSADRAIN, &org); 00187 #elif defined(HAVE_TERMIO_H) 00188 ioctl(fileno(stdin), (int) TCGETA, &org); 00189 tmp=org; 00190 tmp.c_lflag &= ~(ECHO | ISIG | ICANON); 00191 tmp.c_cc[VMIN] = 1; 00192 tmp.c_cc[VTIME]= 0; 00193 ioctl(fileno(stdin),(int) TCSETA, &tmp); 00194 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr))); 00195 ioctl(fileno(stdin),(int) TCSETA, &org); 00196 #else 00197 gtty(fileno(stdin), &org); 00198 tmp=org; 00199 tmp.sg_flags &= ~ECHO; 00200 tmp.sg_flags |= RAW; 00201 stty(fileno(stdin), &tmp); 00202 get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stderr))); 00203 stty(fileno(stdin), &org); 00204 #endif 00205 if (isatty(fileno(stderr))) 00206 fputc('\n',stderr); 00207 #endif /* HAVE_GETPASS */ 00208 00209 DBUG_RETURN(my_strdup(buff,MYF(MY_FAE))); 00210 } 00211 00212 #endif /*__WIN__*/
1.4.7

