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