1 /* getpass.c -- get password from user */
3 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
5 * Copyright 1998-2007 The OpenLDAP Foundation.
6 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted only as authorized by the OpenLDAP
13 * A copy of this license is available in the file LICENSE in the
14 * top-level directory of the distribution or, alternatively, at
15 * <http://www.OpenLDAP.org/license.html>.
17 /* Portions Copyright (c) 1992, 1993 Regents of the University of Michigan.
18 * All rights reserved.
20 * Redistribution and use in source and binary forms are permitted
21 * provided that this notice is preserved and that due credit is given
22 * to the University of Michigan at Ann Arbor. The name of the University
23 * may not be used to endorse or promote products derived from this
24 * software without specific prior written permission. This software
25 * is provided ``as is'' without express or implied warranty.
27 /* This work was originally developed by the University of Michigan
28 * and distributed as part of U-MICH LDAP. It was adapted for use in
29 * -llutil by Kurt D. Zeilenga.
36 #include <ac/stdlib.h>
39 #include <ac/signal.h>
40 #include <ac/string.h>
41 #include <ac/termios.h>
43 #include <ac/unistd.h>
45 #ifdef NEED_GETPASSPHRASE
58 #include "ldap_defaults.h"
61 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' )
87 static char pbuf[513];
91 RETSIGTYPE (*sig)( int sig );
93 if( prompt == NULL ) prompt = _("Password: ");
97 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)
111 setbuf(fi, (char *)NULL);
112 sig = SIGNAL (SIGINT, SIG_IGN);
114 if (GETATTR(fileno(fi), &ttyb) < 0)
117 flags = GETFLAGS( ttyb );
118 SETFLAGS( ttyb, flags & ~ECHO );
120 if (SETATTR(fileno(fi), &ttyb) < 0)
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 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");
161 SETFLAGS( ttyb, flags );
163 if (SETATTR(fileno(fi), &ttyb) < 0)
166 (void) SIGNAL (SIGINT, sig);
177 #endif /* !NEED_GETPASSPHRASE */