]> git.sur5r.net Git - openldap/blob - libraries/liblutil/getpass.c
if urls is NULL, default to ldap:///
[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
19 #include <ac/stdlib.h>
20
21 #include <ac/ctype.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
39 #include "ldap_defaults.h"
40
41 char *
42 getpass( const char *prompt )
43 {
44 #if !defined(HAVE_POSIX_TERMIOS) && !defined(HAVE_SGTTY_H)
45         static char buf[256];
46         int i, c;
47
48 #ifdef DEBUG
49         if (debug & D_TRACE)
50                 printf("->getpass(%s)\n", prompt);
51 #endif
52         printf("%s", prompt);
53         i = 0;
54         while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
55                 buf[i++] = c;
56         if ( c == EOF )
57                 return( NULL );
58         buf[i] = '\0';
59         return (buf);
60 #else
61         int no_pass = 0;
62         char i, j, k;
63         TERMIO_TYPE ttyb;
64         TERMFLAG_TYPE flags;
65         static char pbuf[513];
66         register char *p;
67         register int c;
68         FILE *fi;
69         RETSIGTYPE (*sig)( int sig );
70
71 #ifdef DEBUG
72         if (debug & D_TRACE)
73                 printf("->getpass(%s)\n", prompt);
74 #endif
75         /*
76          *  Stolen from the getpass() routine.  Can't use the plain
77          *  getpass() for two reasons.  One is that LDAP passwords
78          *  can be really, really long - much longer than 8 chars.
79          *  The second is that we like to make this client available
80          *  out of inetd via a Merit asynch port, and we need to be
81          *  able to do telnet control codes to turn on and off line
82          *  blanking.
83          */
84         if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
85                 fi = stdin;
86         else
87                 setbuf(fi, (char *)NULL);
88         sig = SIGNAL (SIGINT, SIG_IGN);
89         if (fi != stdin) {
90                 if (GETATTR(fileno(fi), &ttyb) < 0)
91                         perror("GETATTR");
92         }
93         flags = GETFLAGS( ttyb );
94         SETFLAGS( ttyb, flags & ~ECHO );
95         if (fi != stdin) {
96                 if (SETATTR(fileno(fi), &ttyb) < 0)
97                         perror("SETATTR");
98         }
99
100         /*  blank the line if through Merit */
101         if (fi == stdin) {
102                 printf("%c%c%c", 255, 251, 1);
103                 fflush(stdout);
104                 (void) scanf("%c%c%c", &i, &j, &k);
105                 fflush(stdin);
106         }
107
108         /* fetch the password */
109         fprintf(stdout, "%s", prompt); 
110         fflush(stdout);
111         for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
112                 if (c == '\r')
113                         break;
114                 if (p < &pbuf[512])
115                         *p++ = c;
116         }
117         if (c == EOF)
118                 no_pass = 1;
119         else {
120                 *p = '\0';
121                 if (*(p - 1) == '\r')
122                         *(p - 1) = '\0';
123         }
124
125         /*  unblank the line if through Merit */
126         if (fi == stdin) {
127                 printf("%c%c%c", 255, 252, 1);
128                 fflush(stdout);
129                 (void) scanf("%c%c%c", &i, &j, &k);
130                 fflush(stdin);
131                 printf("\n"); fflush(stdout);
132         }
133         fprintf(stdout, "\n"); 
134         fflush(stdout);
135
136         /* tidy up */
137         SETFLAGS( ttyb, flags );
138         if (fi != stdin) {
139                 if (SETATTR(fileno(fi), &ttyb) < 0)
140                         perror("SETATTR");
141         }
142         (void) SIGNAL (SIGINT, sig);
143         if (fi != stdin)
144                 (void) fclose(fi);
145         else
146                 i = getchar();
147         if (no_pass)
148                 return(NULL);
149         return(pbuf);
150 #endif
151 }
152
153 #endif /* !HAVE_GETPASS */