~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/get_password.c

Removed dead variable, sorted authors file.

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 <drizzled/global.h>
25
 
#include "libdrizzle.h"
26
 
#include <libdrizzle/get_password.h>
27
 
 
28
 
#include <string.h>
29
 
#include <stdio.h>
30
 
#include <stdlib.h>
31
 
#include <ctype.h>
32
 
#include <unistd.h>
33
 
 
 
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__)
34
40
#include <sys/ioctl.h>
35
41
#ifdef HAVE_TERMIOS_H                           /* For tty-password */
36
 
# include       <termios.h>
37
 
#  define TERMIO        struct termios
38
 
#else
39
 
#  ifdef HAVE_TERMIO_H                          /* For tty-password */
40
 
#    include    <termio.h>
41
 
#    define TERMIO      struct termio
42
 
#  else
43
 
#    include    <sgtty.h>
44
 
#    define TERMIO      struct sgttyb
45
 
#  endif
46
 
#endif
47
 
 
 
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
  DBUG_ENTER("get_tty_password");
 
84
  _cputs(opt_message ? opt_message : "Enter password: ");
 
85
  for (;;)
 
86
  {
 
87
    char tmp;
 
88
    tmp=_getch();
 
89
    if (tmp == '\b' || (int) tmp == 127)
 
90
    {
 
91
      if (pos != to)
 
92
      {
 
93
        _cputs("\b \b");
 
94
        pos--;
 
95
        continue;
 
96
      }
 
97
    }
 
98
    if (tmp == '\n' || tmp == '\r' || tmp == 3)
 
99
      break;
 
100
    if (iscntrl(tmp) || pos == end)
 
101
      continue;
 
102
    _cputs("*");
 
103
    *(pos++) = tmp;
 
104
  }
 
105
  while (pos != to && isspace(pos[-1]) == ' ')
 
106
    pos--;                                      /* Allow dummy space at end */
 
107
  *pos=0;
 
108
  _cputs("\n");
 
109
  DBUG_RETURN(my_strdup(to,MYF(MY_FAE)));
 
110
}
 
111
 
 
112
#else
 
113
 
 
114
#ifndef HAVE_GETPASS
48
115
/*
49
116
  Can't use fgets, because readline will get confused
50
117
  length is max number of chars in to, not counting \0
51
118
  to will not include the eol characters.
52
119
*/
53
120
 
54
 
static void get_password(char *to, uint32_t length,int fd, bool echo)
 
121
static void get_password(char *to,uint length,int fd, my_bool echo)
55
122
{
56
123
  char *pos=to,*end=to+length;
57
124
 
58
125
  for (;;)
59
126
  {
60
127
    char tmp;
61
 
    if (read(fd,&tmp,1) != 1)
 
128
    if (my_read(fd,&tmp,1,MYF(0)) != 1)
62
129
      break;
63
130
    if (tmp == '\b' || (int) tmp == 127)
64
131
    {
89
156
  *pos=0;
90
157
  return;
91
158
}
 
159
#endif /* ! HAVE_GETPASS */
92
160
 
93
161
 
94
162
char *get_tty_password(const char *opt_message)
95
163
{
 
164
#ifdef HAVE_GETPASS
 
165
  char *passbuff;
 
166
#else /* ! HAVE_GETPASS */
96
167
  TERMIO org,tmp;
 
168
#endif /* HAVE_GETPASS */
97
169
  char buff[80];
98
170
 
 
171
  DBUG_ENTER("get_tty_password");
 
172
 
 
173
#ifdef HAVE_GETPASS
 
174
  passbuff = getpass(opt_message ? opt_message : "Enter password: ");
 
175
 
 
176
  /* copy the password to buff and clear original (static) buffer */
 
177
  strnmov(buff, passbuff, sizeof(buff) - 1);
 
178
#ifdef _PASSWORD_LEN
 
179
  memset(passbuff, 0, _PASSWORD_LEN);
 
180
#endif
 
181
#else 
99
182
  if (isatty(fileno(stdout)))
100
183
  {
101
184
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
102
185
    fflush(stdout);
103
186
  }
104
 
#  if defined(HAVE_TERMIOS_H)
 
187
#if defined(HAVE_TERMIOS_H)
105
188
  tcgetattr(fileno(stdin), &org);
106
189
  tmp = org;
107
190
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
110
193
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
111
194
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
112
195
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
113
 
#  elif defined(HAVE_TERMIO_H)
 
196
#elif defined(HAVE_TERMIO_H)
114
197
  ioctl(fileno(stdin), (int) TCGETA, &org);
115
198
  tmp=org;
116
199
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
119
202
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
120
203
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
121
204
  ioctl(fileno(stdin),(int) TCSETA, &org);
122
 
#  else
 
205
#else
123
206
  gtty(fileno(stdin), &org);
124
207
  tmp=org;
125
208
  tmp.sg_flags &= ~ECHO;
127
210
  stty(fileno(stdin), &tmp);
128
211
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
129
212
  stty(fileno(stdin), &org);
130
 
#  endif
 
213
#endif
131
214
  if (isatty(fileno(stdout)))
132
215
    fputc('\n',stdout);
 
216
#endif /* HAVE_GETPASS */
133
217
 
134
 
  return strdup(buff);
 
218
  DBUG_RETURN(my_strdup(buff,MYF(MY_FAE)));
135
219
}
 
220
#endif /*__WIN__*/