~ubuntu-branches/ubuntu/precise/mysql-5.1/precise

« back to all changes in this revision

Viewing changes to client/get_password.c

  • Committer: Bazaar Package Importer
  • Author(s): Norbert Tretkowski
  • Date: 2010-03-17 14:56:02 UTC
  • Revision ID: james.westby@ubuntu.com-20100317145602-x7e30l1b2sb5s6w6
Tags: upstream-5.1.45
ImportĀ upstreamĀ versionĀ 5.1.45

Show diffs side-by-side

added added

removed removed

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