2 * lib/krb5/os/read_pwd.c
4 * Copyright 1990,1991 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. M.I.T. makes no representations about the suitability of
20 * this software for any purpose. It is provided "as is" without express
21 * or implied warranty.
24 * libos: krb5_read_password for BSD 4.3
41 #define cleanup(errcode) (void) signal(SIGINT, ointrfunc); return errcode;
44 /* POSIX_* are auto-magically defined in <krb5/config.h> at source
45 configuration time. */
50 #include <sys/ioctl.h>
51 #endif /* POSIX_TERMIOS */
54 #define cleanup(errcode) (void) signal(SIGINT, ointrfunc); tcsetattr(fd, TCSANOW, &save_control); return errcode;
57 #define cleanup(errcode) (void) signal(SIGINT, ointrfunc); stty(fd, (char *)&tty_savestate); return errcode;
59 #define cleanup(errcode) (void) signal(SIGINT, ointrfunc); ioctl(fd, TIOCSETP, (char *)&tty_savestate); return errcode;
61 #endif /* POSIX_TERMIOS */
63 #endif /* ECHO_PASSWORD */
65 static jmp_buf pwd_jump;
76 krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
83 /* adapted from Kerberos v4 des/read_password.c */
84 #if defined(__STDC__) || defined(mips)
85 /* readin_string is used after a longjmp, so must be volatile */
88 char *readin_string = 0;
91 krb5_sigtype (*ointrfunc)();
94 struct termios echo_control, save_control;
97 /* get the file descriptor associated with stdin */
101 /* don't want to read password from anything but a terminal */
103 fprintf(stderr,"Can only read password from a tty\n"); /* XXX */
104 errno=ENOTTY; /* say innapropriate ioctl for device */
109 if (tcgetattr(fd, &echo_control) == -1)
112 save_control = echo_control;
113 echo_control.c_lflag &= ~(ECHO|ECHONL);
115 if (tcsetattr(fd, TCSANOW, &echo_control) == -1)
119 struct sgttyb tty_state, tty_savestate;
122 /* get the file descriptor associated with stdin */
126 /* don't want to read password from anything but a terminal */
128 fprintf(stderr,"Can only read password from a tty\n"); /* XXX */
129 errno=ENOTTY; /* say innapropriate ioctl for device */
134 /* save terminal state */
137 gtty(fd,(char *)&tty_savestate)
139 ioctl(fd,TIOCGETP,(char *)&tty_savestate)
144 tty_state = tty_savestate;
146 tty_state.sg_flags &= ~ECHO;
149 stty(fd,(char *)&tty_state)
151 ioctl(fd,TIOCSETP,(char *)&tty_state)
157 #endif /* ECHO_PASSWORD */
159 if (setjmp(pwd_jump)) {
162 (void) memset((char *)readin_string, 0, *size_return);
163 krb5_xfree(readin_string);
165 (void) memset(return_pwd, 0, *size_return);
166 cleanup(KRB5_LIBOS_PWDINTR);
169 ointrfunc = signal(SIGINT, intr_routine);
171 /* put out the prompt */
172 (void) fputs(prompt,stdout);
173 (void) fflush(stdout);
174 (void) memset(return_pwd, 0, *size_return);
176 if (fgets(return_pwd, *size_return, stdin) == NULL) {
178 (void) putchar('\n');
179 (void) memset(return_pwd, 0, *size_return);
180 cleanup(KRB5_LIBOS_CANTREADPWD);
182 (void) putchar('\n');
183 /* fgets always null-terminates the returned string */
185 /* replace newline with null */
186 if (ptr = strchr(return_pwd, '\n'))
188 else /* flush rest of input line */
190 scratchchar = getchar();
191 } while (scratchchar != EOF && scratchchar != '\n');
194 /* put out the prompt */
195 (void) fputs(prompt2,stdout);
196 (void) fflush(stdout);
197 readin_string = malloc(*size_return);
198 if (!readin_string) {
199 (void) memset(return_pwd, 0, *size_return);
202 (void) memset((char *)readin_string, 0, *size_return);
203 if (fgets((char *)readin_string, *size_return, stdin) == NULL) {
205 (void) putchar('\n');
206 (void) memset((char *)readin_string, 0, *size_return);
207 (void) memset(return_pwd, 0, *size_return);
208 krb5_xfree(readin_string);
209 cleanup(KRB5_LIBOS_CANTREADPWD);
211 (void) putchar('\n');
213 if (ptr = strchr((char *)readin_string, '\n'))
215 else /* need to flush */
217 scratchchar = getchar();
218 } while (scratchchar != EOF && scratchchar != '\n');
221 if (strncmp(return_pwd, (char *)readin_string, *size_return)) {
222 (void) memset((char *)readin_string, 0, *size_return);
223 (void) memset(return_pwd, 0, *size_return);
224 krb5_xfree(readin_string);
225 cleanup(KRB5_LIBOS_BADPWDMATCH);
227 (void) memset((char *)readin_string, 0, *size_return);
228 krb5_xfree(readin_string);
232 (void) signal(SIGINT, ointrfunc);
234 #ifndef ECHO_PASSWORD
236 if (tcsetattr(fd, TCSANOW, &save_control) == -1)
241 stty(fd, (char *)&tty_savestate)
243 ioctl(fd, TIOCSETP, (char *)&tty_savestate)
248 #endif /* ECHO_PASSWORD */
249 *size_return = strlen(return_pwd);
254 /* Don't expect to be called, just define it for sanity and the linker.
257 krb5_error_code INTERFACE
258 krb5_read_password(context, prompt, prompt2, return_pwd, size_return)
259 krb5_context context;