~drizzle-trunk/drizzle/development

383.1.41 by Monty Taylor
Removed more mysys stuff.
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3
 *
390.1.4 by Monty Taylor
More copyright header file fixes.
4
 *  Copyright (C) 2008 Sun Microsystems, Inc.
383.1.41 by Monty Taylor
Removed more mysys stuff.
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
390.1.4 by Monty Taylor
More copyright header file fixes.
8
 *  the Free Software Foundation; version 2 of the License.
383.1.41 by Monty Taylor
Removed more mysys stuff.
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 by brian
clean slate
19
20
/*
21
** Ask for a password from tty
22
** This is an own file to avoid conflicts with curses
23
*/
383.1.44 by Monty Taylor
Renamed drizzle.h to libdrizzle.h.
24
#include "libdrizzle.h"
1 by brian
clean slate
25
390.1.6 by Monty Taylor
Oh dear god the changes. The changes. I'd tell you what they are, but I'd just be making stuff up. Suffice it to day it's mostly all around splitting files in libdrizzle into different files and removing interdepends. And whatever else I happened to see...
26
#include <string.h>
27
#include <stdlib.h>
28
1 by brian
clean slate
29
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
30
#undef HAVE_GETPASS
31
#endif
32
33
#ifdef HAVE_GETPASS
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
34
# ifdef HAVE_PWD_H
35
#   include <pwd.h>
36
# endif /* HAVE_PWD_H */
1 by brian
clean slate
37
#else /* ! HAVE_GETPASS */
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
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__ */
1 by brian
clean slate
61
#endif /* HAVE_GETPASS */
62
63
#ifdef HAVE_GETPASSPHRASE			/* For Solaris */
64
#define getpass(A) getpassphrase(A)
65
#endif
66
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
67
#if defined( __WIN__)
1 by brian
clean slate
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");
51.3.6 by Jay Pipes
Removal of DBUG from libdrizzle/ - Round 2
98
  return(my_strdup(to,MYF(MY_FAE)));
1 by brian
clean slate
99
}
100
101
#else
102
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
103
# ifndef HAVE_GETPASS
1 by brian
clean slate
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
277 by Brian Aker
Cleanup of my_bool from extra and libdrizzle
110
static void get_password(char *to,uint length,int fd, bool echo)
1 by brian
clean slate
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",stdout);
126
	  fflush(stdout);
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('*',stdout);
139
      fflush(stdout);
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
}
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
148
# endif /* ! HAVE_GETPASS */
1 by brian
clean slate
149
150
151
char *get_tty_password(const char *opt_message)
152
{
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
153
# ifdef HAVE_GETPASS
1 by brian
clean slate
154
  char *passbuff;
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
155
# else /* ! HAVE_GETPASS */
1 by brian
clean slate
156
  TERMIO org,tmp;
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
157
# endif /* HAVE_GETPASS */
1 by brian
clean slate
158
  char buff[80];
159
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
160
# ifdef HAVE_GETPASS
1 by brian
clean slate
161
  passbuff = getpass(opt_message ? opt_message : "Enter password: ");
162
390 by Monty Taylor
Removed stpncpy from source.
163
  memset(buff, 0, 80);
1 by brian
clean slate
164
  /* copy the password to buff and clear original (static) buffer */
390 by Monty Taylor
Removed stpncpy from source.
165
  strncpy(buff, passbuff, sizeof(buff) - 1);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
166
#   ifdef _PASSWORD_LEN
1 by brian
clean slate
167
  memset(passbuff, 0, _PASSWORD_LEN);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
168
#   endif
169
# else /* ! HAVE_GETPASS */
1 by brian
clean slate
170
  if (isatty(fileno(stdout)))
171
  {
172
    fputs(opt_message ? opt_message : "Enter password: ",stdout);
173
    fflush(stdout);
174
  }
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
175
#   if defined(HAVE_TERMIOS_H)
1 by brian
clean slate
176
  tcgetattr(fileno(stdin), &org);
177
  tmp = org;
178
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
179
  tmp.c_cc[VMIN] = 1;
180
  tmp.c_cc[VTIME] = 0;
181
  tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
182
  get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
183
  tcsetattr(fileno(stdin), TCSADRAIN, &org);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
184
#   elif defined(HAVE_TERMIO_H)
1 by brian
clean slate
185
  ioctl(fileno(stdin), (int) TCGETA, &org);
186
  tmp=org;
187
  tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
188
  tmp.c_cc[VMIN] = 1;
189
  tmp.c_cc[VTIME]= 0;
190
  ioctl(fileno(stdin),(int) TCSETA, &tmp);
191
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
192
  ioctl(fileno(stdin),(int) TCSETA, &org);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
193
#   else
1 by brian
clean slate
194
  gtty(fileno(stdin), &org);
195
  tmp=org;
196
  tmp.sg_flags &= ~ECHO;
197
  tmp.sg_flags |= RAW;
198
  stty(fileno(stdin), &tmp);
199
  get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
200
  stty(fileno(stdin), &org);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
201
#   endif
1 by brian
clean slate
202
  if (isatty(fileno(stdout)))
203
    fputc('\n',stdout);
206.2.1 by Jay Pipes
* Removed two files, only needed for Netware:
204
# endif /* HAVE_GETPASS */
1 by brian
clean slate
205
330 by Monty Taylor
Removed some mysys refs.
206
  return strdup(buff);
1 by brian
clean slate
207
}
208
#endif /*__WIN__*/