get_password.c

Go to the documentation of this file.
00001 /* Copyright (C) 2000-2004 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.
00006 
00007    There are special exceptions to the terms and conditions of the GPL as it
00008    is applied to this software. View the full text of the exception in file
00009    EXCEPTIONS-CLIENT in the directory of this software distribution.
00010 
00011    This program is distributed in the hope that it will be useful,
00012    but WITHOUT ANY WARRANTY; without even the implied warranty of
00013    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014    GNU General Public License for more details.
00015 
00016    You should have received a copy of the GNU General Public License
00017    along with this program; if not, write to the Free Software
00018    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
00019 
00020 /*
00021 ** Ask for a password from tty
00022 ** This is an own file to avoid conflicts with curses
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 /* HAVE_PWD_H */
00038 #else /* ! HAVE_GETPASS */
00039 #if !defined(__WIN__) && !defined(OS2) && !defined(__NETWARE__)
00040 #include <sys/ioctl.h>
00041 #ifdef HAVE_TERMIOS_H                           /* For tty-password */
00042 #include        <termios.h>
00043 #define TERMIO  struct termios
00044 #else
00045 #ifdef HAVE_TERMIO_H                            /* For tty-password */
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>                         /* QQ; Fix this in configure */
00055 #include <asm/termiobits.h>
00056 #endif
00057 #else
00058 #ifndef __NETWARE__
00059 #include <conio.h>
00060 #endif /* __NETWARE__ */
00061 #endif /* __WIN__ */
00062 #endif /* HAVE_GETPASS */
00063 
00064 #ifdef HAVE_GETPASSPHRASE                       /* For Solaris */
00065 #define getpass(A) getpassphrase(A)
00066 #endif
00067 
00068 #if defined( __WIN__) || defined(OS2) || defined(__NETWARE__)
00069 /* were just going to fake it here and get input from the keyboard */
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--;                                      /* Allow dummy space at end */
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   Can't use fgets, because readline will get confused
00117   length is max number of chars in to, not counting \0
00118   to will not include the eol characters.
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--;                                      /* Allow dummy space at end */
00156   *pos=0;
00157   return;
00158 }
00159 #endif /* ! HAVE_GETPASS */
00160 
00161 
00162 char *get_tty_password(char *opt_message)
00163 {
00164 #ifdef HAVE_GETPASS
00165   char *passbuff;
00166 #else /* ! HAVE_GETPASS */
00167   TERMIO org,tmp;
00168 #endif /* HAVE_GETPASS */
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   /* copy the password to buff and clear original (static) buffer */
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 /* HAVE_GETPASS */
00217 
00218   DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));
00219 }
00220 #endif /*__WIN__*/

Generated on Wed Jul 20 21:04:21 2005 for MySQL 5.0.9 Beta by  doxygen 1.4.3