1
1
/* getpass.c -- get password from user */
2
/* $OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.17.2.4 2009/01/22 00:00:58 kurt Exp $ */
2
/* $OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.17.2.6 2009/08/25 23:09:33 quanah Exp $ */
3
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5
5
* Copyright 1998-2009 The OpenLDAP Foundation.
58
58
#include "ldap_defaults.h"
65
#define TTY "/dev/tty"
61
69
lutil_getpass( const char *prompt )
63
#if !defined(HAVE_TERMIOS_H) && !defined(HAVE_SGTTY_H)
67
if( prompt == NULL ) prompt = _("Password: ");
71
printf("->getpass(%s)\n", prompt);
76
while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
71
static char pbuf[PBUF];
75
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
86
77
TERMFLAG_TYPE flags;
87
static char pbuf[513];
91
78
RETSIGTYPE (*sig)( int sig );
93
81
if( prompt == NULL ) prompt = _("Password: ");
96
84
if (debug & D_TRACE)
97
85
printf("->getpass(%s)\n", prompt);
100
* Stolen from the getpass() routine. Can't use the plain
101
* getpass() for two reasons. One is that LDAP passwords
102
* can be really, really long - much longer than 8 chars.
103
* The second is that we like to make this client available
104
* out of inetd via a Merit asynch port, and we need to be
105
* able to do telnet control codes to turn on and off line
108
if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
88
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
89
if ((fi = fopen(TTY, "r")) == NULL)
111
92
setbuf(fi, (char *)NULL);
112
sig = SIGNAL (SIGINT, SIG_IGN);
113
93
if (fi != stdin) {
114
94
if (GETATTR(fileno(fi), &ttyb) < 0)
115
95
perror("GETATTR");
117
flags = GETFLAGS( ttyb );
118
SETFLAGS( ttyb, flags & ~ECHO );
96
sig = SIGNAL (SIGINT, SIG_IGN);
97
flags = GETFLAGS( ttyb );
98
SETFLAGS( ttyb, flags & ~ECHO );
120
99
if (SETATTR(fileno(fi), &ttyb) < 0)
121
100
perror("SETATTR");
124
/* blank the line if through Merit */
126
printf("%c%c%c", 255, 251, 1);
128
(void) scanf("%c%c%c", &i, &j, &k);
132
/* fetch the password */
133
105
fprintf(stdout, "%s", prompt);
135
for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
145
if (*(p - 1) == '\r')
149
/* unblank the line if through Merit */
151
printf("%c%c%c", 255, 252, 1);
153
(void) scanf("%c%c%c", &i, &j, &k);
155
printf("\n"); fflush(stdout);
157
fprintf(stdout, "\n");
108
while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
109
if ( i < (sizeof(pbuf)-1) )
111
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
161
SETFLAGS( ttyb, flags );
162
113
if (fi != stdin) {
114
fprintf(stdout, "\n");
116
SETFLAGS( ttyb, flags );
163
117
if (SETATTR(fileno(fi), &ttyb) < 0)
164
118
perror("SETATTR");
166
(void) SIGNAL (SIGINT, sig);
119
(void) SIGNAL (SIGINT, sig);
168
120
(void) fclose(fi);
177
129
#endif /* !NEED_GETPASSPHRASE */