1
1
/* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2
2
* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
4
* Copyright (C) 2008 Sun Microsystems
4
* Copyright (C) 2008 Sun Microsystems, Inc.
6
6
* This program is free software; you can redistribute it and/or modify
7
7
* it under the terms of the GNU General Public License as published by
8
* the Free Software Foundation; either version 2 of the License, or
9
* (at your option) any later version.
8
* the Free Software Foundation; version 2 of the License.
11
10
* This program is distributed in the hope that it will be useful,
12
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
22
21
** Ask for a password from tty
23
22
** This is an own file to avoid conflicts with curses
25
#include <drizzled/global.h>
26
25
#include "libdrizzle.h"
28
#if defined(HAVE_BROKEN_GETPASS) && !defined(HAVE_GETPASSPHRASE)
35
# endif /* HAVE_PWD_H */
36
#else /* ! HAVE_GETPASS */
37
# if !defined(__WIN__) && !defined(__NETWARE__)
38
# include <sys/ioctl.h>
39
# ifdef HAVE_TERMIOS_H /* For tty-password */
41
# define TERMIO struct termios
43
# ifdef HAVE_TERMIO_H /* For tty-password */
45
# define TERMIO struct termio
48
# define TERMIO struct sgttyb
51
# ifdef alpha_linux_port
52
# include <asm/ioctls.h> /* QQ; Fix this in configure */
53
# include <asm/termiobits.h>
58
# endif /* ! __NETWARE__ */
59
# endif /* ! __WIN__ AND ! __NETWARE__ */
60
#endif /* HAVE_GETPASS */
62
#ifdef HAVE_GETPASSPHRASE /* For Solaris */
63
#define getpass(A) getpassphrase(A)
67
char *get_tty_password(const char *opt_message)
70
char *pos=to,*end=to+sizeof(to)-1;
72
_cputs(opt_message ? opt_message : "Enter password: ");
77
if (tmp == '\b' || (int) tmp == 127)
86
if (tmp == '\n' || tmp == '\r' || tmp == 3)
88
if (iscntrl(tmp) || pos == end)
93
while (pos != to && isspace(pos[-1]) == ' ')
94
pos--; /* Allow dummy space at end */
97
return(my_strdup(to,MYF(MY_FAE)));
26
#include <libdrizzle/get_password.h>
34
#include <sys/ioctl.h>
35
#ifdef HAVE_TERMIOS_H /* For tty-password */
37
# define TERMIO struct termios
39
# ifdef HAVE_TERMIO_H /* For tty-password */
41
# define TERMIO struct termio
44
# define TERMIO struct sgttyb
102
# ifndef HAVE_GETPASS
104
49
Can't use fgets, because readline will get confused
105
50
length is max number of chars in to, not counting \0
106
51
to will not include the eol characters.
109
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
56
char *pos=to,*end=to+length;
116
if (my_read(fd,&tmp,1,MYF(0)) != 1)
61
if (read(fd,&tmp,1) != 1)
118
63
if (tmp == '\b' || (int) tmp == 127)
147
# endif /* ! HAVE_GETPASS */
150
94
char *get_tty_password(const char *opt_message)
154
# else /* ! HAVE_GETPASS */
156
# endif /* HAVE_GETPASS */
160
passbuff = getpass(opt_message ? opt_message : "Enter password: ");
162
/* copy the password to buff and clear original (static) buffer */
163
stpncpy(buff, passbuff, sizeof(buff) - 1);
164
# ifdef _PASSWORD_LEN
165
memset(passbuff, 0, _PASSWORD_LEN);
167
# else /* ! HAVE_GETPASS */
168
99
if (isatty(fileno(stdout)))
170
101
fputs(opt_message ? opt_message : "Enter password: ",stdout);
173
# if defined(HAVE_TERMIOS_H)
104
# if defined(HAVE_TERMIOS_H)
174
105
tcgetattr(fileno(stdin), &org);
176
107
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
179
110
tcsetattr(fileno(stdin), TCSADRAIN, &tmp);
180
111
get_password(buff, sizeof(buff)-1, fileno(stdin), isatty(fileno(stdout)));
181
112
tcsetattr(fileno(stdin), TCSADRAIN, &org);
182
# elif defined(HAVE_TERMIO_H)
113
# elif defined(HAVE_TERMIO_H)
183
114
ioctl(fileno(stdin), (int) TCGETA, &org);
185
116
tmp.c_lflag &= ~(ECHO | ISIG | ICANON);
188
119
ioctl(fileno(stdin),(int) TCSETA, &tmp);
189
120
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
190
121
ioctl(fileno(stdin),(int) TCSETA, &org);
192
123
gtty(fileno(stdin), &org);
194
125
tmp.sg_flags &= ~ECHO;
196
127
stty(fileno(stdin), &tmp);
197
128
get_password(buff,sizeof(buff)-1,fileno(stdin),isatty(fileno(stdout)));
198
129
stty(fileno(stdin), &org);
200
131
if (isatty(fileno(stdout)))
201
132
fputc('\n',stdout);
202
# endif /* HAVE_GETPASS */
204
134
return strdup(buff);