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