3 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
6 * For copying and distribution information, please see the file
9 * This routine prints the supplied string to standard
10 * output as a prompt, and reads a password string without
14 #include <afsconfig.h>
15 #include <afs/param.h>
19 #include <mit-cpyright.h>
36 #include <sys/ioctl.h>
51 #include <sys/ttold.h>
65 #if defined(AFS_SGI_ENV) || defined (AFS_AIX_ENV) || defined(AFS_FBSD_ENV) /*|| defined (AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)*/
80 static sigtype sig_restore();
81 static push_signals(), pop_signals();
84 int des_read_pw_string(char *, int, char *, int);
85 int des_string_to_key(char *, des_cblock *);
87 /*** Routines ****************************************************** */
89 des_read_password(k,prompt,verify)
95 char key_string[BUFSIZ];
104 ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
106 des_string_to_key(key_string, k);
111 memset(key_string, 0, sizeof (key_string));
115 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
116 static void catch(int);
119 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV))
124 * This version just returns the string, doesn't map to key.
126 * Returns 0 on success, non-zero on failure.
129 des_read_pw_string(s,maxa,prompt,verify)
137 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
139 struct sigaction newsig, oldsig;
140 struct termios save_ttyb, ttyb;
142 #if defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
145 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
147 struct sigaction osa, sa;
152 struct sgttyb tty_state, echo_off_tty_state;
155 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
160 extern void setbuf();
161 extern int kill(), fclose();
166 DWORD oldConMode, newConMode;
167 BOOL resetConMode = FALSE;
169 char key_string[BUFSIZ];
175 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)
176 if ((fi = fopen("/dev/tty", "r")) == NULL)
178 setbuf(fi, (char *)NULL); /* We don't want any buffering for our i/o. */
180 * Install signal handler for SIGINT so that we can restore
181 * the tty settings after we change them. The handler merely
182 * increments the variable "intrupt" to tell us that an
183 * interrupt signal was received.
185 newsig.sa_handler = catch;
186 sigemptyset(&newsig.sa_mask);
188 sigaction(SIGINT, &newsig, &oldsig);
192 * Get the terminal characters (save for later restoration) and
193 * reset them so that echo is off
196 tcgetattr(fno, &ttyb);
198 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
199 tcsetattr(fno, TCSAFLUSH, &ttyb);
201 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
202 if((fi = fopen("/dev/tty", "r")) == NULL) {
206 setbuf(fi, (char*)NULL);
207 sa.sa_handler = catch;
209 sa.sa_flags = SA_INTERRUPT;
210 (void) sigaction(SIGINT, &sa, &osa);
212 (void) ioctl(fileno(fi), TCGETS, &ttyb);
213 flags = ttyb.c_lflag;
214 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
215 (void) ioctl(fileno(fi), TCSETSF, &ttyb);
218 /* XXX assume jmp_buf is typedef'ed to an array */
219 memcpy((char *)env, (char *)old_env, sizeof(env));
222 /* save terminal state*/
223 if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
227 memcpy(&echo_off_tty_state, &tty_state, sizeof (tty_state));
228 echo_off_tty_state.sg_flags &= ~ECHO;
229 if (ioctl(0,TIOCSETP,(char *)&echo_off_tty_state) == -1)
232 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
233 if((fi = fopen("/dev/tty", "r+")) == NULL)
236 setbuf(fi, (char*)NULL);
237 sig = signal(SIGINT, catch);
239 (void) ioctl(fileno(fi), TCGETA, &ttyb);
242 flags = ttyb.c_lflag;
243 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
244 (void) ioctl(fileno(fi), TCSETAF, &ttyb);
247 /* turn off console input echoing */
248 if ((hConStdin = GetStdHandle(STD_INPUT_HANDLE)) != INVALID_HANDLE_VALUE) {
249 if (GetConsoleMode(hConStdin, &oldConMode)) {
250 newConMode = (oldConMode & ~(ENABLE_ECHO_INPUT));
251 if (SetConsoleMode(hConStdin, newConMode)) {
262 (void) printf(prompt);
263 (void) fflush(stdout);
265 h19line(s,sizeof(s),0);
269 if (!fgets(s, maxa, stdin)) {
274 * Otherwise hitting ctrl-d will always leave us inside this loop forever!
280 if ((ptr = strchr(s, '\n')))
284 printf("\nVerifying, please re-enter %s",prompt);
285 (void) fflush(stdout);
287 h19line(key_string,sizeof(key_string),0);
288 if (!strlen(key_string))
291 if (!fgets(key_string, sizeof(key_string), stdin)) {
295 if ((ptr = strchr(key_string, '\n')))
298 if (strcmp(s,key_string)) {
299 printf("\n\07\07Mismatch - try again\n");
300 (void) fflush(stdout);
313 #if defined(AFS_HPUX_ENV) || defined(AFS_FBSD_ENV)
315 * Restore the terminal to its previous characteristics.
316 * Restore the old signal handler for SIGINT.
318 tcsetattr(fno, TCSANOW, &save_ttyb);
319 sigaction(SIGINT, &oldsig, (struct sigaction *)0);
324 * If we got a SIGINT while we were doing things, send the SIGINT
325 * to ourselves so that the calling program receives it (since we
326 * were intercepting it for a period of time.)
329 kill(getpid(), SIGINT);
331 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
332 ttyb.c_lflag = flags;
333 (void) ioctl(fileno(fi), TCSETSW, &ttyb);
334 (void) sigaction(SIGINT, &osa, (struct sigaction *)NULL);
339 if (ioctl(0,TIOCSETP,(char *)&tty_state))
342 memcpy((char *)old_env, (char *)env, sizeof(env));
344 #if defined (AFS_AIX_ENV) /*|| defined (AFS_HPUX_ENV)*/ || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
345 ttyb.c_lflag = flags;
347 (void) ioctl(fileno(fi), TCSETAW, &ttyb);
348 (void) signal(SIGINT, sig);
352 (void) kill(getpid(), SIGINT);
355 /* restore console to original mode settings */
357 (void)SetConsoleMode(hConStdin, oldConMode);
365 memset(key_string, 0, sizeof (key_string));
366 s[maxa-1] = 0; /* force termination */
367 return !ok; /* return nonzero if not okay */
372 * this can be static since we should never have more than
376 void static (*old_sigfunc[NSIG])();
378 static sigtype (*old_sigfunc[NSIG])();
381 static push_signals()
384 for (i = 0; i < NSIG; i++)
385 old_sigfunc[i] = signal(i,sig_restore);
391 for (i = 0; i < NSIG; i++)
392 (void) signal(i,old_sigfunc[i]);
403 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV) || defined(AFS_FBSD_ENV)