getpass.c revision 1.1.1.3 1 /* $NetBSD: getpass.c,v 1.1.1.3 2010/12/12 15:22:08 adam Exp $ */
2
3 /* getpass.c -- get password from user */
4 /* OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.17.2.7 2010/04/13 20:23:05 kurt Exp */
5 /* This work is part of OpenLDAP Software <http://www.openldap.org/>.
6 *
7 * Copyright 1998-2010 The OpenLDAP Foundation.
8 * Portions Copyright 1998-2003 Kurt D. Zeilenga.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted only as authorized by the OpenLDAP
13 * Public License.
14 *
15 * A copy of this license is available in the file LICENSE in the
16 * top-level directory of the distribution or, alternatively, at
17 * <http://www.OpenLDAP.org/license.html>.
18 */
19 /* Portions Copyright (c) 1992, 1993 Regents of the University of Michigan.
20 * All rights reserved.
21 *
22 * Redistribution and use in source and binary forms are permitted
23 * provided that this notice is preserved and that due credit is given
24 * to the University of Michigan at Ann Arbor. The name of the University
25 * may not be used to endorse or promote products derived from this
26 * software without specific prior written permission. This software
27 * is provided ``as is'' without express or implied warranty.
28 */
29 /* This work was originally developed by the University of Michigan
30 * and distributed as part of U-MICH LDAP. It was adapted for use in
31 * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
32 */
33
34 #include "portable.h"
35
36 #include <stdio.h>
37
38 #include <ac/stdlib.h>
39
40 #include <ac/ctype.h>
41 #include <ac/signal.h>
42 #include <ac/string.h>
43 #include <ac/termios.h>
44 #include <ac/time.h>
45 #include <ac/unistd.h>
46
47 #ifndef HAVE_GETPASSPHRASE
48
49 #ifdef HAVE_FCNTL_H
50 #include <fcntl.h>
51 #endif
52
53 #ifdef HAVE_CONIO_H
54 #include <conio.h>
55 #endif
56
57 #include <lber.h>
58 #include <ldap.h>
59
60 #include "ldap_defaults.h"
61
62 #define PBUF 512
63
64 #ifdef HAVE_WINSOCK
65 #define TTY "con:"
66 #else
67 #define TTY "/dev/tty"
68 #endif
69
70 char *
71 lutil_getpass( const char *prompt )
72 {
73 static char pbuf[PBUF];
74 FILE *fi;
75 int c;
76 unsigned i;
77 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
78 TERMIO_TYPE ttyb;
79 TERMFLAG_TYPE flags;
80 RETSIGTYPE (*sig)( int sig );
81 #endif
82
83 if( prompt == NULL ) prompt = _("Password: ");
84
85 #ifdef DEBUG
86 if (debug & D_TRACE)
87 printf("->getpass(%s)\n", prompt);
88 #endif
89
90 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
91 if ((fi = fopen(TTY, "r")) == NULL)
92 fi = stdin;
93 else
94 setbuf(fi, (char *)NULL);
95 if (fi != stdin) {
96 if (GETATTR(fileno(fi), &ttyb) < 0)
97 perror("GETATTR");
98 sig = SIGNAL (SIGINT, SIG_IGN);
99 flags = GETFLAGS( ttyb );
100 SETFLAGS( ttyb, flags & ~ECHO );
101 if (SETATTR(fileno(fi), &ttyb) < 0)
102 perror("SETATTR");
103 }
104 #else
105 fi = stdin;
106 #endif
107 fprintf(stdout, "%s", prompt);
108 fflush(stdout);
109 i = 0;
110 while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
111 if ( i < (sizeof(pbuf)-1) )
112 pbuf[i++] = c;
113 #if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
114 /* tidy up */
115 if (fi != stdin) {
116 fprintf(stdout, "\n");
117 fflush(stdout);
118 SETFLAGS( ttyb, flags );
119 if (SETATTR(fileno(fi), &ttyb) < 0)
120 perror("SETATTR");
121 (void) SIGNAL (SIGINT, sig);
122 (void) fclose(fi);
123 }
124 #endif
125 if ( c == EOF )
126 return( NULL );
127 pbuf[i] = '\0';
128 return (pbuf);
129 }
130
131 #endif /* !NEED_GETPASSPHRASE */
132