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