]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getpass.c
c5cc6b0e32245598cd85b4aaf8cb05c65bdaab74
[openldap] / libraries / liblutil / getpass.c
1 /*
2  * Copyright (c) 1992, 1993  Regents of the University of Michigan.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that this notice is preserved and that due credit is given
7  * to the University of Michigan at Ann Arbor. The name of the University
8  * may not be used to endorse or promote products derived from this
9  * software without specific prior written permission. This software
10  * is provided ``as is'' without express or implied warranty.
11  */
12
13 #include "portable.h"
14
15 #ifndef HAVE_GETPASS
16
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include <ac/ctype.h>
21 #include <ac/errno.h>
22 #include <ac/signal.h>
23 #include <ac/string.h>
24 #include <ac/termios.h>
25 #include <ac/time.h>
26 #include <ac/unistd.h>
27
28 #ifdef HAVE_FCNTL_H
29 #include <fcntl.h>
30 #endif
31
32 #ifdef HAVE_CONIO_H
33 #include <conio.h>
34 #endif
35
36 #include <lber.h>
37 #include <ldap.h>
38 #include <ldapconfig.h>
39
40 char *
41 getpass( const char *prompt )
42 {
43 #if !defined(HAVE_POSIX_TERMIOS) && !defined(HAVE_SGTTY_H)
44         static char buf[256];
45         int i, c;
46
47 #ifdef DEBUG
48         if (debug & D_TRACE)
49                 printf("->getpass(%s)\n", prompt);
50 #endif
51         printf("%s", prompt);
52         i = 0;
53         while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
54                 buf[i++] = c;
55         if ( c == EOF )
56                 return( NULL );
57         buf[i] = '\0';
58         return (buf);
59 #else
60         int no_pass = 0;
61         char i, j, k;
62         TERMIO_TYPE ttyb;
63         TERMFLAG_TYPE flags;
64         static char pbuf[513];
65         register char *p;
66         register int c;
67         FILE *fi;
68         RETSIGTYPE (*sig)( int sig );
69
70 #ifdef DEBUG
71         if (debug & D_TRACE)
72                 printf("->getpass(%s)\n", prompt);
73 #endif
74         /*
75          *  Stolen from the getpass() routine.  Can't use the plain
76          *  getpass() for two reasons.  One is that LDAP passwords
77          *  can be really, really long - much longer than 8 chars.
78          *  The second is that we like to make this client available
79          *  out of inetd via a Merit asynch port, and we need to be
80          *  able to do telnet control codes to turn on and off line
81          *  blanking.
82          */
83         if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
84                 fi = stdin;
85         else
86                 setbuf(fi, (char *)NULL);
87         sig = SIGNAL (SIGINT, SIG_IGN);
88         if (fi != stdin) {
89                 if (GETATTR(fileno(fi), &ttyb) < 0)
90                         perror("GETATTR");
91         }
92         flags = GETFLAGS( ttyb );
93         SETFLAGS( ttyb, flags & ~ECHO );
94         if (fi != stdin) {
95                 if (SETATTR(fileno(fi), &ttyb) < 0)
96                         perror("SETATTR");
97         }
98
99         /*  blank the line if through Merit */
100         if (fi == stdin) {
101                 printf("%c%c%c", 255, 251, 1);
102                 fflush(stdout);
103                 (void) scanf("%c%c%c", &i, &j, &k);
104                 fflush(stdin);
105         }
106
107         /* fetch the password */
108         fprintf(stdout, "%s", prompt); 
109         fflush(stdout);
110         for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
111                 if (c == '\r')
112                         break;
113                 if (p < &pbuf[512])
114                         *p++ = c;
115         }
116         if (c == EOF)
117                 no_pass = 1;
118         else {
119                 *p = '\0';
120                 if (*(p - 1) == '\r')
121                         *(p - 1) = '\0';
122         }
123
124         /*  unblank the line if through Merit */
125         if (fi == stdin) {
126                 printf("%c%c%c", 255, 252, 1);
127                 fflush(stdout);
128                 (void) scanf("%c%c%c", &i, &j, &k);
129                 fflush(stdin);
130                 printf("\n"); fflush(stdout);
131         }
132         fprintf(stdout, "\n"); 
133         fflush(stdout);
134
135         /* tidy up */
136         SETFLAGS( ttyb, flags );
137         if (fi != stdin) {
138                 if (SETATTR(fileno(fi), &ttyb) < 0)
139                         perror("SETATTR");
140         }
141         (void) SIGNAL (SIGINT, sig);
142         if (fi != stdin)
143                 (void) fclose(fi);
144         else
145                 i = getchar();
146         if (no_pass)
147                 return(NULL);
148         return(pbuf);
149 #endif /* DOS */
150 }
151
152 #endif /* !HAVE_GETPASS */