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