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 <afs/param.h>
15 #include <mit-cpyright.h>
21 #if defined(AFS_SUN5_ENV) || defined(AFS_NT40_ENV)
31 #include <sys/ioctl.h>
36 #if defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV) || defined(AFS_DARWIN_ENV)
42 #include <sys/ttold.h>
47 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
59 #if defined(AFS_SGI_ENV) || defined (AFS_AIX_ENV) /*|| defined (AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)*/
74 static sigtype sig_restore();
75 static push_signals(), pop_signals();
76 int des_read_pw_string();
79 /*** Routines ****************************************************** */
81 des_read_password(k,prompt,verify)
87 char key_string[BUFSIZ];
96 ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
98 des_string_to_key(key_string, k);
101 bzero(key_string, sizeof (key_string));
105 #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)
106 static void catch(int);
109 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV))
114 * This version just returns the string, doesn't map to key.
116 * Returns 0 on success, non-zero on failure.
119 des_read_pw_string(s,maxa,prompt,verify)
127 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN_ENV)
129 struct sigaction newsig, oldsig;
130 struct termios save_ttyb, ttyb;
132 #if defined(AFS_DARWIN_ENV)
135 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
137 struct sigaction osa, sa;
142 struct sgttyb tty_state, echo_off_tty_state;
145 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
150 extern void setbuf();
151 extern int kill(), fclose();
156 DWORD oldConMode, newConMode;
157 BOOL resetConMode = FALSE;
159 char key_string[BUFSIZ];
165 #if defined(AFS_HPUX_ENV) || defined(AFS_DARWIN_ENV)
166 if ((fi = fopen("/dev/tty", "r")) == NULL)
168 setbuf(fi, (char *)NULL); /* We don't want any buffering for our i/o. */
170 * Install signal handler for SIGINT so that we can restore
171 * the tty settings after we change them. The handler merely
172 * increments the variable "intrupt" to tell us that an
173 * interrupt signal was received.
175 newsig.sa_handler = catch;
176 sigemptyset(&newsig.sa_mask);
178 sigaction(SIGINT, &newsig, &oldsig);
182 * Get the terminal characters (save for later restoration) and
183 * reset them so that echo is off
186 tcgetattr(fno, &ttyb);
188 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
189 tcsetattr(fno, TCSAFLUSH, &ttyb);
191 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
192 if((fi = fopen("/dev/tty", "r")) == NULL) {
196 setbuf(fi, (char*)NULL);
197 sa.sa_handler = catch;
199 sa.sa_flags = SA_INTERRUPT;
200 (void) sigaction(SIGINT, &sa, &osa);
202 (void) ioctl(fileno(fi), TCGETS, &ttyb);
203 flags = ttyb.c_lflag;
204 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
205 (void) ioctl(fileno(fi), TCSETSF, &ttyb);
208 /* XXX assume jmp_buf is typedef'ed to an array */
209 bcopy((char *)old_env, (char *)env, sizeof(env));
212 /* save terminal state*/
213 if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
217 bcopy (&tty_state, &echo_off_tty_state, sizeof (tty_state));
218 echo_off_tty_state.sg_flags &= ~ECHO;
219 if (ioctl(0,TIOCSETP,(char *)&echo_off_tty_state) == -1)
222 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
223 if((fi = fopen("/dev/tty", "r+")) == NULL)
226 setbuf(fi, (char*)NULL);
227 sig = signal(SIGINT, catch);
229 (void) ioctl(fileno(fi), TCGETA, &ttyb);
232 flags = ttyb.c_lflag;
233 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
234 (void) ioctl(fileno(fi), TCSETAF, &ttyb);
237 /* turn off console input echoing */
238 if ((hConStdin = GetStdHandle(STD_INPUT_HANDLE)) != INVALID_HANDLE_VALUE) {
239 if (GetConsoleMode(hConStdin, &oldConMode)) {
240 newConMode = (oldConMode & ~(ENABLE_ECHO_INPUT));
241 if (SetConsoleMode(hConStdin, newConMode)) {
252 (void) printf(prompt);
253 (void) fflush(stdout);
255 h19line(s,sizeof(s),0);
259 if (!fgets(s, maxa, stdin)) {
264 * Otherwise hitting ctrl-d will always leave us inside this loop forever!
270 if ((ptr = index(s, '\n')))
274 printf("\nVerifying, please re-enter %s",prompt);
275 (void) fflush(stdout);
277 h19line(key_string,sizeof(key_string),0);
278 if (!strlen(key_string))
281 if (!fgets(key_string, sizeof(key_string), stdin)) {
285 if ((ptr = index(key_string, '\n')))
288 if (strcmp(s,key_string)) {
289 printf("\n\07\07Mismatch - try again\n");
290 (void) fflush(stdout);
303 * Restore the terminal to its previous characteristics.
304 * Restore the old signal handler for SIGINT.
306 tcsetattr(fno, TCSANOW, &save_ttyb);
307 sigaction(SIGINT, &oldsig, (struct sigaction *)0);
312 * If we got a SIGINT while we were doing things, send the SIGINT
313 * to ourselves so that the calling program receives it (since we
314 * were intercepting it for a period of time.)
317 kill(getpid(), SIGINT);
319 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
320 ttyb.c_lflag = flags;
321 (void) ioctl(fileno(fi), TCSETSW, &ttyb);
322 (void) sigaction(SIGINT, &osa, (struct sigaction *)NULL);
327 if (ioctl(0,TIOCSETP,(char *)&tty_state))
330 bcopy((char *)env, (char *)old_env, sizeof(env));
332 #if defined (AFS_AIX_ENV) /*|| defined (AFS_HPUX_ENV)*/ || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
333 ttyb.c_lflag = flags;
335 (void) ioctl(fileno(fi), TCSETAW, &ttyb);
336 (void) signal(SIGINT, sig);
340 (void) kill(getpid(), SIGINT);
343 /* restore console to original mode settings */
345 (void)SetConsoleMode(hConStdin, oldConMode);
353 bzero(key_string, sizeof (key_string));
354 s[maxa-1] = 0; /* force termination */
355 return !ok; /* return nonzero if not okay */
360 * this can be static since we should never have more than
364 void static (*old_sigfunc[NSIG])();
366 static sigtype (*old_sigfunc[NSIG])();
369 static push_signals()
372 for (i = 0; i < NSIG; i++)
373 old_sigfunc[i] = signal(i,sig_restore);
379 for (i = 0; i < NSIG; i++)
380 (void) signal(i,old_sigfunc[i]);
391 #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)