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