aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb5/os/read_pwd.c
blob: 0c05359d35af69740063943d927d07dd0a645c69 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
 * lib/krb5/os/read_pwd.c
 *
 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * Export of this software from the United States of America may
 *   require a specific license from the United States Government.
 *   It is the responsibility of any person or organization contemplating
 *   export to obtain such a license before exporting.
 * 
 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
 * distribute this software and its documentation for any purpose and
 * without fee is hereby granted, provided that the above copyright
 * notice appear in all copies and that both that copyright notice and
 * this permission notice appear in supporting documentation, and that
 * the name of M.I.T. not be used in advertising or publicity pertaining
 * to distribution of the software without specific, written prior
 * permission.  M.I.T. makes no representations about the suitability of
 * this software for any purpose.  It is provided "as is" without express
 * or implied warranty.
 * 
 *
 * libos: krb5_read_password for BSD 4.3
 */

#include "k5-int.h"
#ifndef _MSDOS
#include <stdio.h>
#include <errno.h>
#include <signal.h>
#include <setjmp.h>

#ifdef sun
#include <sgtty.h>
#endif

extern int errno;

#ifdef ECHO_PASSWORD
#define cleanup(errcode) (void) signal(SIGINT, ointrfunc); return errcode;
#else

/* POSIX_* are auto-magically defined in <krb5/config.h> at source
   configuration time. */

#ifdef POSIX_TERMIOS
#include <termios.h>
#else
#include <sys/ioctl.h>
#endif /* POSIX_TERMIOS */

#ifdef POSIX_TERMIOS
#define cleanup(errcode) (void) signal(SIGINT, ointrfunc); tcsetattr(fd, TCSANOW, &save_control); return errcode;
#else
#ifdef sun
#define cleanup(errcode) (void) signal(SIGINT, ointrfunc); stty(fd, (char *)&tty_savestate); return errcode;
#else /* !sun */
#define cleanup(errcode) (void) signal(SIGINT, ointrfunc); ioctl(fd, TIOCSETP, (char *)&tty_savestate); return errcode;
#endif /* sun */
#endif /* POSIX_TERMIOS */

#endif /* ECHO_PASSWORD */

static jmp_buf pwd_jump;


static krb5_sigtype
intr_routine()
{
    longjmp(pwd_jump, 1);
    /*NOTREACHED*/
}

krb5_error_code
krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
    krb5_context context;
    char *prompt;
    char *prompt2;
    char *return_pwd;
    int *size_return;
{
    /* adapted from Kerberos v4 des/read_password.c */
#if defined(__STDC__) || defined(mips)
    /* readin_string is used after a longjmp, so must be volatile */
    volatile
#endif
      char *readin_string = 0;
    register char *ptr;
    int scratchchar;
    krb5_sigtype (*ointrfunc)();
#ifndef ECHO_PASSWORD
#ifdef POSIX_TERMIOS
    struct termios echo_control, save_control;
    int fd;

    /* get the file descriptor associated with stdin */
    fd=fileno(stdin);

#ifdef notdef
    /* don't want to read password from anything but a terminal */
    if (!isatty(fd)) {
        fprintf(stderr,"Can only read password from a tty\n"); /* XXX */
        errno=ENOTTY; /* say innapropriate ioctl for device */
	return errno;
    }
#endif /* notdef */

    if (tcgetattr(fd, &echo_control) == -1)
	return errno;

    save_control = echo_control;
    echo_control.c_lflag &= ~(ECHO|ECHONL);
    
    if (tcsetattr(fd, TCSANOW, &echo_control) == -1)
	return errno;
#else
    /* 4.3BSD style */
    struct sgttyb tty_state, tty_savestate;
    int fd;

    /* get the file descriptor associated with stdin */
    fd=fileno(stdin);

#ifdef notdef
    /* don't want to read password from anything but a terminal */
    if (!isatty(fd)) {
        fprintf(stderr,"Can only read password from a tty\n"); /* XXX */
        errno=ENOTTY; /* say innapropriate ioctl for device */
	return errno;
    }
#endif /* notdef */

    /* save terminal state */
    if (
#ifdef sun
	gtty(fd,(char *)&tty_savestate)
#else
	ioctl(fd,TIOCGETP,(char *)&tty_savestate)
#endif
	== -1) 
	return errno;

    tty_state = tty_savestate;

    tty_state.sg_flags &= ~ECHO;
    if (
#ifdef sun
	stty(fd,(char *)&tty_state)
#else
	ioctl(fd,TIOCSETP,(char *)&tty_state)
#endif
	== -1)
	return errno;
#endif

#endif /* ECHO_PASSWORD */

    if (setjmp(pwd_jump)) {
	/* interrupted */
	if (readin_string) {
	    (void) memset((char *)readin_string, 0, *size_return);
	    krb5_xfree(readin_string);
	}
	(void) memset(return_pwd, 0, *size_return);
	cleanup(KRB5_LIBOS_PWDINTR);
    }
    /* save intrfunc */
    ointrfunc = signal(SIGINT, intr_routine);

    /* put out the prompt */
    (void) fputs(prompt,stdout);
    (void) fflush(stdout);
    (void) memset(return_pwd, 0, *size_return);

    if (fgets(return_pwd, *size_return, stdin) == NULL) {
	/* error */
	(void) putchar('\n');
	(void) memset(return_pwd, 0, *size_return);
	cleanup(KRB5_LIBOS_CANTREADPWD);
    }
    (void) putchar('\n');
    /* fgets always null-terminates the returned string */

    /* replace newline with null */
    if (ptr = strchr(return_pwd, '\n'))
	*ptr = '\0';
    else /* flush rest of input line */
	do {
	    scratchchar = getchar();
	} while (scratchchar != EOF && scratchchar != '\n');

    if (prompt2) {
	/* put out the prompt */
	(void) fputs(prompt2,stdout);
	(void) fflush(stdout);
	readin_string = malloc(*size_return);
	if (!readin_string) {
	    (void) memset(return_pwd, 0, *size_return);
	    cleanup(ENOMEM);
	}
	(void) memset((char *)readin_string, 0, *size_return);
	if (fgets((char *)readin_string, *size_return, stdin) == NULL) {
	    /* error */
	    (void) putchar('\n');
	    (void) memset((char *)readin_string, 0, *size_return);
	    (void) memset(return_pwd, 0, *size_return);
	    krb5_xfree(readin_string);
	    cleanup(KRB5_LIBOS_CANTREADPWD);
	}
	(void) putchar('\n');

	if (ptr = strchr((char *)readin_string, '\n'))
	    *ptr = '\0';
        else /* need to flush */
	    do {
		scratchchar = getchar();
	    } while (scratchchar != EOF && scratchchar != '\n');
	    
	/* compare */
	if (strncmp(return_pwd, (char *)readin_string, *size_return)) {
	    (void) memset((char *)readin_string, 0, *size_return);
	    (void) memset(return_pwd, 0, *size_return);
	    krb5_xfree(readin_string);
	    cleanup(KRB5_LIBOS_BADPWDMATCH);
	}
	(void) memset((char *)readin_string, 0, *size_return);
	krb5_xfree(readin_string);
    }
    
    /* reset intrfunc */
    (void) signal(SIGINT, ointrfunc);

#ifndef ECHO_PASSWORD
#ifdef POSIX_TERMIOS
    if (tcsetattr(fd, TCSANOW, &save_control) == -1)
	return errno;
#else
    if (
#ifdef sun
	stty(fd, (char *)&tty_savestate)
#else
	ioctl(fd, TIOCSETP, (char *)&tty_savestate)
#endif
	== -1)
	return errno;
#endif
#endif /* ECHO_PASSWORD */
    *size_return = strlen(return_pwd);

    return 0;
}
#else /* MSDOS */
/* Don't expect to be called, just define it for sanity and the linker.
*/

krb5_error_code INTERFACE
krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
    krb5_context context;
    char *prompt;
    char *prompt2;
    char *return_pwd;
    int *size_return;
{
   *size_return = 0;
   return 0;
}
#endif   /* !MSDOS */