1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems, Inc.
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.
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.
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
21
** Ask for a password from tty
22
** This is an own file to avoid conflicts with curses
24
#include "libdrizzle.h"
29
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
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 */
42
# define TERMIO struct termios
44
# ifdef HAVE_TERMIO_H /* For tty-password */
46
# define TERMIO struct termio
49
# define TERMIO struct sgttyb
52
# ifdef alpha_linux_port
53
# include <asm/ioctls.h> /* QQ; Fix this in configure */
54
# include <asm/termiobits.h>
59
# endif /* ! __NETWARE__ */
60
# endif /* ! __WIN__ AND ! __NETWARE__ */
61
#endif /* HAVE_GETPASS */
63
#ifdef HAVE_GETPASSPHRASE /* For Solaris */
64
#define getpass(A) getpassphrase(A)
68
char *get_tty_password(const char *opt_message)
71
char *pos=to,*end=to+sizeof(to)-1;
73
_cputs(opt_message ? opt_message : "Enter password: ");
78
if (tmp == '\b' || (int) tmp == 127)
87
if (tmp == '\n' || tmp == '\r' || tmp == 3)
89
if (iscntrl(tmp) || pos == end)
94
while (pos != to && isspace(pos[-1]) == ' ')
95
pos--; /* Allow dummy space at end */
98
return(my_strdup(to,MYF(MY_FAE)));
103
# ifndef HAVE_GETPASS
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.
110
static void get_password(char *to,uint length,int fd, bool echo)
112
char *pos=to,*end=to+length;
117
if (my_read(fd,&tmp,1,MYF(0)) != 1)
119
if (tmp == '\b' || (int) tmp == 127)
125
fputs("\b \b",stdout);
132
if (tmp == '\n' || tmp == '\r' || tmp == 3)
134
if (iscntrl(tmp) || pos == end)
143
while (pos != to && isspace(pos[-1]) == ' ')
144
pos--; /* Allow dummy space at end */
148
# endif /* ! HAVE_GETPASS */
151
char *get_tty_password(const char *opt_message)
155
# else /* ! HAVE_GETPASS */
157
# endif /* HAVE_GETPASS */
161
passbuff = getpass(opt_message ? opt_message : "Enter password: ");
164
/* copy the password to buff and clear original (static) buffer */
165
strncpy(buff, passbuff, sizeof(buff) - 1);
166
# ifdef _PASSWORD_LEN
167
memset(passbuff, 0, _PASSWORD_LEN);
169
# else /* ! HAVE_GETPASS */
170
if (isatty(fileno(stdout)))
172
fputs(opt_message ? opt_message : "Enter password: ",stdout);
175
# if defined(HAVE_TERMIOS_H)
176
tcgetattr(fileno(stdin), &org);
178
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
181
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
182
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
183
tcsetattr(fileno(stdin), TCSADRAIN, &org);
184
# elif defined(HAVE_TERMIO_H)
185
ioctl(fileno(stdin), (int) TCGETA, &org);
187
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
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);
194
gtty(fileno(stdin), &org);
196
tmp.sg_flags &= ~ECHO;
198
stty(fileno(stdin), &tmp);
199
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
200
stty(fileno(stdin), &org);
202
if (isatty(fileno(stdout)))
204
# endif /* HAVE_GETPASS */