~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/get_password.c

  • Committer: Patrick Galbraith
  • Date: 2008-07-24 16:57:40 UTC
  • mto: (202.2.4 rename-mysql-to-drizzle)
  • mto: This revision was merged to the branch mainline in revision 212.
  • Revision ID: patg@ishvara-20080724165740-x58yw6zs6d9o17lf
Most everything working with client rename
mysqlslap test still fails... can't connect to the server

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 
 *
4
 
 *  Copyright (C) 2008 Sun Microsystems, Inc.
5
 
 *
6
 
 *  This program is free software; you can redistribute it and/or modify
7
 
 *  it under the terms of the GNU General Public License as published by
8
 
 *  the Free Software Foundation; version 2 of the License.
9
 
 *
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.
14
 
 *
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 St, Fifth Floor, Boston, MA  02110-1301  USA
18
 
 */
 
1
/* Copyright (C) 2000-2004 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.
 
6
 
 
7
   There are special exceptions to the terms and conditions of the GPL as it
 
8
   is applied to this software. View the full text of the exception in file
 
9
   EXCEPTIONS-CLIENT in the directory of this software distribution.
 
10
 
 
11
   This program is distributed in the hope that it will be useful,
 
12
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
   GNU General Public License for more details.
 
15
 
 
16
   You should have received a copy of the GNU General Public License
 
17
   along with this program; if not, write to the Free Software
 
18
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
19
19
 
20
20
/*
21
21
** Ask for a password from tty
22
22
** This is an own file to avoid conflicts with curses
23
23
*/
24
 
#include "libdrizzle.h"
25
 
 
26
 
#include <string.h>
27
 
#include <stdio.h>
28
 
#include <stdlib.h>
29
 
#include <ctype.h>
30
 
 
 
24
#include <my_global.h>
 
25
#include <my_sys.h>
 
26
#include "drizzle.h"
 
27
#include <m_string.h>
 
28
#include <m_ctype.h>
 
29
 
 
30
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
 
31
#undef HAVE_GETPASS
 
32
#endif
 
33
 
 
34
#ifdef HAVE_GETPASS
 
35
#ifdef HAVE_PWD_H
 
36
#include <pwd.h>
 
37
#endif /* HAVE_PWD_H */
 
38
#else /* ! HAVE_GETPASS */
 
39
#if !defined(__WIN__) && !defined(__NETWARE__)
31
40
#include <sys/ioctl.h>
32
41
#ifdef HAVE_TERMIOS_H                           /* For tty-password */
33
 
# include       <termios.h>
34
 
#  define TERMIO        struct termios
35
 
#else
36
 
#  ifdef HAVE_TERMIO_H                          /* For tty-password */
37
 
#    include    <termio.h>
38
 
#    define TERMIO      struct termio
39
 
#  else
40
 
#    include    <sgtty.h>
41
 
#    define TERMIO      struct sgttyb
42
 
#  endif
43
 
#endif
44
 
 
 
42
#include        <termios.h>
 
43
#define TERMIO  struct termios
 
44
#else
 
45
#ifdef HAVE_TERMIO_H                            /* For tty-password */
 
46
#include        <termio.h>
 
47
#define TERMIO  struct termio
 
48
#else
 
49
#include        <sgtty.h>
 
50
#define TERMIO  struct sgttyb
 
51
#endif
 
52
#endif
 
53
#ifdef alpha_linux_port
 
54
#include <asm/ioctls.h>                         /* QQ; Fix this in configure */
 
55
#include <asm/termiobits.h>
 
56
#endif
 
57
#else
 
58
#ifndef __NETWARE__
 
59
#include <conio.h>
 
60
#endif /* __NETWARE__ */
 
61
#endif /* __WIN__ */
 
62
#endif /* HAVE_GETPASS */
 
63
 
 
64
#ifdef HAVE_GETPASSPHRASE                       /* For Solaris */
 
65
#define getpass(A) getpassphrase(A)
 
66
#endif
 
67
 
 
68
#if defined( __WIN__) || defined(__NETWARE__)
 
69
/* were just going to fake it here and get input from the keyboard */
 
70
 
 
71
#ifdef __NETWARE__
 
72
#undef _getch
 
73
#undef _cputs
 
74
#define _getch getcharacter
 
75
#define _cputs(A) putstring(A)
 
76
#endif
 
77
 
 
78
char *get_tty_password(const char *opt_message)
 
79
{
 
80
  char to[80];
 
81
  char *pos=to,*end=to+sizeof(to)-1;
 
82
  int i=0;
 
83
  _cputs(opt_message ? opt_message : "Enter password: ");
 
84
  for (;;)
 
85
  {
 
86
    char tmp;
 
87
    tmp=_getch();
 
88
    if (tmp == '\b' || (int) tmp == 127)
 
89
    {
 
90
      if (pos != to)
 
91
      {
 
92
        _cputs("\b \b");
 
93
        pos--;
 
94
        continue;
 
95
      }
 
96
    }
 
97
    if (tmp == '\n' || tmp == '\r' || tmp == 3)
 
98
      break;
 
99
    if (iscntrl(tmp) || pos == end)
 
100
      continue;
 
101
    _cputs("*");
 
102
    *(pos++) = tmp;
 
103
  }
 
104
  while (pos != to && isspace(pos[-1]) == ' ')
 
105
    pos--;                                      /* Allow dummy space at end */
 
106
  *pos=0;
 
107
  _cputs("\n");
 
108
  return(my_strdup(to,MYF(MY_FAE)));
 
109
}
 
110
 
 
111
#else
 
112
 
 
113
#ifndef HAVE_GETPASS
45
114
/*
46
115
  Can't use fgets, because readline will get confused
47
116
  length is max number of chars in to, not counting \0
48
117
  to will not include the eol characters.
49
118
*/
50
119
 
51
 
static void get_password(char *to, uint32_t length,int fd, bool echo)
 
120
static void get_password(char *to,uint length,int fd, my_bool echo)
52
121
{
53
122
  char *pos=to,*end=to+length;
54
123
 
55
124
  for (;;)
56
125
  {
57
126
    char tmp;
58
 
    if (read(fd,&tmp,1) != 1)
 
127
    if (my_read(fd,&tmp,1,MYF(0)) != 1)
59
128
      break;
60
129
    if (tmp == '\b' || (int) tmp == 127)
61
130
    {
86
155
  *pos=0;
87
156
  return;
88
157
}
 
158
#endif /* ! HAVE_GETPASS */
89
159
 
90
160
 
91
161
char *get_tty_password(const char *opt_message)
92
162
{
 
163
#ifdef HAVE_GETPASS
 
164
  char *passbuff;
 
165
#else /* ! HAVE_GETPASS */
93
166
  TERMIO org,tmp;
 
167
#endif /* HAVE_GETPASS */
94
168
  char buff[80];
95
169
 
 
170
#ifdef HAVE_GETPASS
 
171
  passbuff = getpass(opt_message ? opt_message : "Enter password: ");
 
172
 
 
173
  /* copy the password to buff and clear original (static) buffer */
 
174
  strnmov(buff, passbuff, sizeof(buff) - 1);
 
175
#ifdef _PASSWORD_LEN
 
176
  memset(passbuff, 0, _PASSWORD_LEN);
 
177
#endif
 
178
#else 
96
179
  if (isatty(fileno(stdout)))
97
180
  {
98
181
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
99
182
    fflush(stdout);
100
183
  }
101
 
#  if defined(HAVE_TERMIOS_H)
 
184
#if defined(HAVE_TERMIOS_H)
102
185
  tcgetattr(fileno(stdin), &org);
103
186
  tmp = org;
104
187
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
107
190
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
108
191
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
109
192
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
110
 
#  elif defined(HAVE_TERMIO_H)
 
193
#elif defined(HAVE_TERMIO_H)
111
194
  ioctl(fileno(stdin), (int) TCGETA, &org);
112
195
  tmp=org;
113
196
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
116
199
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
117
200
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
118
201
  ioctl(fileno(stdin),(int) TCSETA, &org);
119
 
#  else
 
202
#else
120
203
  gtty(fileno(stdin), &org);
121
204
  tmp=org;
122
205
  tmp.sg_flags &= ~ECHO;
124
207
  stty(fileno(stdin), &tmp);
125
208
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
126
209
  stty(fileno(stdin), &org);
127
 
#  endif
 
210
#endif
128
211
  if (isatty(fileno(stdout)))
129
212
    fputc('\n',stdout);
 
213
#endif /* HAVE_GETPASS */
130
214
 
131
 
  return strdup(buff);
 
215
  return(my_strdup(buff,MYF(MY_FAE)));
132
216
}
 
217
#endif /*__WIN__*/