~ubuntu-branches/ubuntu/maverick/openldap/maverick-proposed

« back to all changes in this revision

Viewing changes to libraries/liblutil/getpass.c

  • Committer: Bazaar Package Importer
  • Author(s): Mathias Gug
  • Date: 2009-09-07 13:41:10 UTC
  • mto: This revision was merged to the branch mainline in revision 19.
  • Revision ID: james.westby@ubuntu.com-20090907134110-jsdrvn0atu1fex4m
Tags: upstream-2.4.18
ImportĀ upstreamĀ versionĀ 2.4.18

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/* getpass.c -- get password from user */
2
 
/* $OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.17.2.4 2009/01/22 00:00:58 kurt Exp $ */
 
2
/* $OpenLDAP: pkg/ldap/libraries/liblutil/getpass.c,v 1.17.2.6 2009/08/25 23:09:33 quanah Exp $ */
3
3
/* This work is part of OpenLDAP Software <http://www.openldap.org/>.
4
4
 *
5
5
 * Copyright 1998-2009 The OpenLDAP Foundation.
26
26
 */
27
27
/* This work was originally developed by the University of Michigan
28
28
 * and distributed as part of U-MICH LDAP.  It was adapted for use in
29
 
 * -llutil by Kurt D. Zeilenga.
 
29
 * -llutil by Kurt D. Zeilenga and subsequently rewritten by Howard Chu.
30
30
 */
31
31
 
32
32
#include "portable.h"
42
42
#include <ac/time.h>
43
43
#include <ac/unistd.h>
44
44
 
45
 
#ifdef NEED_GETPASSPHRASE
 
45
#ifndef HAVE_GETPASSPHRASE
46
46
 
47
47
#ifdef HAVE_FCNTL_H
48
48
#include <fcntl.h>
57
57
 
58
58
#include "ldap_defaults.h"
59
59
 
 
60
#define PBUF    512
 
61
 
 
62
#ifdef HAVE_WINSOCK
 
63
#define TTY "con:"
 
64
#else
 
65
#define TTY "/dev/tty"
 
66
#endif
 
67
 
60
68
char *
61
69
lutil_getpass( const char *prompt )
62
70
{
63
 
#if !defined(HAVE_TERMIOS_H) && !defined(HAVE_SGTTY_H)
64
 
        static char buf[256];
65
 
        int i, c;
66
 
 
67
 
        if( prompt == NULL ) prompt = _("Password: ");
68
 
 
69
 
#ifdef DEBUG
70
 
        if (debug & D_TRACE)
71
 
                printf("->getpass(%s)\n", prompt);
72
 
#endif
73
 
 
74
 
        printf("%s", prompt);
75
 
        i = 0;
76
 
        while ( (c = getch()) != EOF && c != '\n' && c != '\r' )
77
 
                buf[i++] = c;
78
 
        if ( c == EOF )
79
 
                return( NULL );
80
 
        buf[i] = '\0';
81
 
        return (buf);
82
 
#else
83
 
        int no_pass = 0;
84
 
        char i, j, k;
 
71
        static char pbuf[PBUF];
 
72
        FILE *fi;
 
73
        int c;
 
74
        unsigned i;
 
75
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
85
76
        TERMIO_TYPE ttyb;
86
77
        TERMFLAG_TYPE flags;
87
 
        static char pbuf[513];
88
 
        register char *p;
89
 
        register int c;
90
 
        FILE *fi;
91
78
        RETSIGTYPE (*sig)( int sig );
 
79
#endif
92
80
 
93
81
        if( prompt == NULL ) prompt = _("Password: ");
94
82
 
96
84
        if (debug & D_TRACE)
97
85
                printf("->getpass(%s)\n", prompt);
98
86
#endif
99
 
        /*
100
 
         *  Stolen from the getpass() routine.  Can't use the plain
101
 
         *  getpass() for two reasons.  One is that LDAP passwords
102
 
         *  can be really, really long - much longer than 8 chars.
103
 
         *  The second is that we like to make this client available
104
 
         *  out of inetd via a Merit asynch port, and we need to be
105
 
         *  able to do telnet control codes to turn on and off line
106
 
         *  blanking.
107
 
         */
108
 
        if ((fi = fdopen(open("/dev/tty", 2), "r")) == NULL)
 
87
 
 
88
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
 
89
        if ((fi = fopen(TTY, "r")) == NULL)
109
90
                fi = stdin;
110
91
        else
111
92
                setbuf(fi, (char *)NULL);
112
 
        sig = SIGNAL (SIGINT, SIG_IGN);
113
93
        if (fi != stdin) {
114
94
                if (GETATTR(fileno(fi), &ttyb) < 0)
115
95
                        perror("GETATTR");
116
 
        }
117
 
        flags = GETFLAGS( ttyb );
118
 
        SETFLAGS( ttyb, flags & ~ECHO );
119
 
        if (fi != stdin) {
 
96
                sig = SIGNAL (SIGINT, SIG_IGN);
 
97
                flags = GETFLAGS( ttyb );
 
98
                SETFLAGS( ttyb, flags & ~ECHO );
120
99
                if (SETATTR(fileno(fi), &ttyb) < 0)
121
100
                        perror("SETATTR");
122
101
        }
123
 
 
124
 
        /*  blank the line if through Merit */
125
 
        if (fi == stdin) {
126
 
                printf("%c%c%c", 255, 251, 1);
127
 
                fflush(stdout);
128
 
                (void) scanf("%c%c%c", &i, &j, &k);
129
 
                fflush(stdin);
130
 
        }
131
 
 
132
 
        /* fetch the password */
 
102
#else
 
103
        fi = stdin;
 
104
#endif
133
105
        fprintf(stdout, "%s", prompt); 
134
106
        fflush(stdout);
135
 
        for (p=pbuf; (c = getc(fi))!='\n' && c!=EOF;) {
136
 
                if (c == '\r')
137
 
                        break;
138
 
                if (p < &pbuf[512])
139
 
                        *p++ = c;
140
 
        }
141
 
        if (c == EOF)
142
 
                no_pass = 1;
143
 
        else {
144
 
                *p = '\0';
145
 
                if (*(p - 1) == '\r')
146
 
                        *(p - 1) = '\0';
147
 
        }
148
 
 
149
 
        /*  unblank the line if through Merit */
150
 
        if (fi == stdin) {
151
 
                printf("%c%c%c", 255, 252, 1);
152
 
                fflush(stdout);
153
 
                (void) scanf("%c%c%c", &i, &j, &k);
154
 
                fflush(stdin);
155
 
                printf("\n"); fflush(stdout);
156
 
        }
157
 
        fprintf(stdout, "\n"); 
158
 
        fflush(stdout);
159
 
 
 
107
        i = 0;
 
108
        while ( (c = getc(fi)) != EOF && c != '\n' && c != '\r' )
 
109
                if ( i < (sizeof(pbuf)-1) )
 
110
                        pbuf[i++] = c;
 
111
#if defined(HAVE_TERMIOS_H) || defined(HAVE_SGTTY_H)
160
112
        /* tidy up */
161
 
        SETFLAGS( ttyb, flags );
162
113
        if (fi != stdin) {
 
114
                fprintf(stdout, "\n"); 
 
115
                fflush(stdout);
 
116
                SETFLAGS( ttyb, flags );
163
117
                if (SETATTR(fileno(fi), &ttyb) < 0)
164
118
                        perror("SETATTR");
165
 
        }
166
 
        (void) SIGNAL (SIGINT, sig);
167
 
        if (fi != stdin)
 
119
                (void) SIGNAL (SIGINT, sig);
168
120
                (void) fclose(fi);
169
 
        else
170
 
                i = getchar();
171
 
        if (no_pass)
172
 
                return(NULL);
173
 
        return(pbuf);
 
121
        }
174
122
#endif
 
123
        if ( c == EOF )
 
124
                return( NULL );
 
125
        pbuf[i] = '\0';
 
126
        return (pbuf);
175
127
}
176
128
 
177
129
#endif /* !NEED_GETPASSPHRASE */