~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to libdrizzle/get_password.c

enable remaining subselect tests, merge with latest from the trunk

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 */
 
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
 */
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
24
#include <drizzled/global.h>
25
 
#include "drizzle.h"
26
 
#include <mystrings/m_string.h>
27
 
#include <mystrings/m_ctype.h>
28
 
 
29
 
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
30
 
#undef HAVE_GETPASS
31
 
#endif
32
 
 
33
 
#ifdef HAVE_GETPASS
34
 
# ifdef HAVE_PWD_H
35
 
#   include <pwd.h>
36
 
# endif /* HAVE_PWD_H */
37
 
#else /* ! HAVE_GETPASS */
38
 
# if !defined(__WIN__) && !defined(__NETWARE__)
39
 
#   include <sys/ioctl.h>
40
 
#   ifdef HAVE_TERMIOS_H                                /* For tty-password */
41
 
#     include   <termios.h>
42
 
#     define TERMIO     struct termios
43
 
#   else
44
 
#     ifdef HAVE_TERMIO_H                               /* For tty-password */
45
 
#       include <termio.h>
46
 
#       define TERMIO   struct termio
47
 
#     else
48
 
#       include <sgtty.h>
49
 
#       define TERMIO   struct sgttyb
50
 
#     endif
51
 
#   endif
52
 
#   ifdef alpha_linux_port
53
 
#     include <asm/ioctls.h>                            /* QQ; Fix this in configure */
54
 
#     include <asm/termiobits.h>
55
 
#   endif
56
 
# else
57
 
#   ifndef __NETWARE__
58
 
#     include <conio.h>
59
 
#   endif /* ! __NETWARE__ */
60
 
# endif /* ! __WIN__ AND ! __NETWARE__ */
61
 
#endif /* HAVE_GETPASS */
62
 
 
63
 
#ifdef HAVE_GETPASSPHRASE                       /* For Solaris */
64
 
#define getpass(A) getpassphrase(A)
65
 
#endif
66
 
 
67
 
#if defined( __WIN__)
68
 
char *get_tty_password(const char *opt_message)
69
 
{
70
 
  char to[80];
71
 
  char *pos=to,*end=to+sizeof(to)-1;
72
 
  int i=0;
73
 
  _cputs(opt_message ? opt_message : "Enter password: ");
74
 
  for (;;)
75
 
  {
76
 
    char tmp;
77
 
    tmp=_getch();
78
 
    if (tmp == '\b' || (int) tmp == 127)
79
 
    {
80
 
      if (pos != to)
81
 
      {
82
 
        _cputs("\b \b");
83
 
        pos--;
84
 
        continue;
85
 
      }
86
 
    }
87
 
    if (tmp == '\n' || tmp == '\r' || tmp == 3)
88
 
      break;
89
 
    if (iscntrl(tmp) || pos == end)
90
 
      continue;
91
 
    _cputs("*");
92
 
    *(pos++) = tmp;
93
 
  }
94
 
  while (pos != to && isspace(pos[-1]) == ' ')
95
 
    pos--;                                      /* Allow dummy space at end */
96
 
  *pos=0;
97
 
  _cputs("\n");
98
 
  return(my_strdup(to,MYF(MY_FAE)));
99
 
}
100
 
 
 
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
 
 
34
#include <sys/ioctl.h>
 
35
#ifdef HAVE_TERMIOS_H                           /* For tty-password */
 
36
# include       <termios.h>
 
37
#  define TERMIO        struct termios
101
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
102
47
 
103
 
# ifndef HAVE_GETPASS
104
48
/*
105
49
  Can't use fgets, because readline will get confused
106
50
  length is max number of chars in to, not counting \0
107
51
  to will not include the eol characters.
108
52
*/
109
53
 
110
 
static void get_password(char *to,uint length,int fd, bool echo)
 
54
static void get_password(char *to, uint32_t length,int fd, bool echo)
111
55
{
112
56
  char *pos=to,*end=to+length;
113
57
 
114
58
  for (;;)
115
59
  {
116
60
    char tmp;
117
 
    if (my_read(fd,&tmp,1,MYF(0)) != 1)
 
61
    if (read(fd,&tmp,1) != 1)
118
62
      break;
119
63
    if (tmp == '\b' || (int) tmp == 127)
120
64
    {
145
89
  *pos=0;
146
90
  return;
147
91
}
148
 
# endif /* ! HAVE_GETPASS */
149
92
 
150
93
 
151
94
char *get_tty_password(const char *opt_message)
152
95
{
153
 
# ifdef HAVE_GETPASS
154
 
  char *passbuff;
155
 
# else /* ! HAVE_GETPASS */
156
96
  TERMIO org,tmp;
157
 
# endif /* HAVE_GETPASS */
158
97
  char buff[80];
159
98
 
160
 
# ifdef HAVE_GETPASS
161
 
  passbuff = getpass(opt_message ? opt_message : "Enter password: ");
162
 
 
163
 
  /* copy the password to buff and clear original (static) buffer */
164
 
  stpncpy(buff, passbuff, sizeof(buff) - 1);
165
 
#   ifdef _PASSWORD_LEN
166
 
  memset(passbuff, 0, _PASSWORD_LEN);
167
 
#   endif
168
 
# else /* ! HAVE_GETPASS */
169
99
  if (isatty(fileno(stdout)))
170
100
  {
171
101
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
172
102
    fflush(stdout);
173
103
  }
174
 
#   if defined(HAVE_TERMIOS_H)
 
104
#  if defined(HAVE_TERMIOS_H)
175
105
  tcgetattr(fileno(stdin), &org);
176
106
  tmp = org;
177
107
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
180
110
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
181
111
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
182
112
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
183
 
#   elif defined(HAVE_TERMIO_H)
 
113
#  elif defined(HAVE_TERMIO_H)
184
114
  ioctl(fileno(stdin), (int) TCGETA, &org);
185
115
  tmp=org;
186
116
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
189
119
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
190
120
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
191
121
  ioctl(fileno(stdin),(int) TCSETA, &org);
192
 
#   else
 
122
#  else
193
123
  gtty(fileno(stdin), &org);
194
124
  tmp=org;
195
125
  tmp.sg_flags &= ~ECHO;
197
127
  stty(fileno(stdin), &tmp);
198
128
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
199
129
  stty(fileno(stdin), &org);
200
 
#   endif
 
130
#  endif
201
131
  if (isatty(fileno(stdout)))
202
132
    fputc('\n',stdout);
203
 
# endif /* HAVE_GETPASS */
204
133
 
205
134
  return strdup(buff);
206
135
}
207
 
#endif /*__WIN__*/