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)
42 #include <sys/ttold.h>
47 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
56 #if defined(AFS_SGI_ENV) || defined (AFS_AIX_ENV) /*|| defined (AFS_HPUX_ENV) || defined(AFS_SUN5_ENV)*/
71 static sigtype sig_restore();
72 static push_signals(), pop_signals();
73 int des_read_pw_string();
76 /*** Routines ****************************************************** */
78 des_read_password(k,prompt,verify)
84 char key_string[BUFSIZ];
93 ok = des_read_pw_string(key_string, BUFSIZ, prompt, verify);
95 des_string_to_key(key_string, k);
98 bzero(key_string, sizeof (key_string));
102 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_LINUX20_ENV)
103 static void catch(int);
106 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV))
111 * This version just returns the string, doesn't map to key.
113 * Returns 0 on success, non-zero on failure.
116 des_read_pw_string(s,maxa,prompt,verify)
126 struct sigaction newsig, oldsig;
127 struct termios save_ttyb, ttyb;
129 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
131 struct sigaction osa, sa;
136 struct sgttyb tty_state, echo_off_tty_state;
139 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
144 extern void setbuf();
145 extern int kill(), fclose();
150 DWORD oldConMode, newConMode;
151 BOOL resetConMode = FALSE;
153 char key_string[BUFSIZ];
160 if ((fi = fopen("/dev/tty", "r")) == NULL)
162 setbuf(fi, (char *)NULL); /* We don't want any buffering for our i/o. */
164 * Install signal handler for SIGINT so that we can restore
165 * the tty settings after we change them. The handler merely
166 * increments the variable "intrupt" to tell us that an
167 * interrupt signal was received.
169 newsig.sa_handler = catch;
170 sigemptyset(&newsig.sa_mask);
172 sigaction(SIGINT, &newsig, &oldsig);
176 * Get the terminal characters (save for later restoration) and
177 * reset them so that echo is off
180 tcgetattr(fno, &ttyb);
182 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
183 tcsetattr(fno, TCSAFLUSH, &ttyb);
185 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
186 if((fi = fopen("/dev/tty", "r")) == NULL) {
190 setbuf(fi, (char*)NULL);
191 sa.sa_handler = catch;
193 sa.sa_flags = SA_INTERRUPT;
194 (void) sigaction(SIGINT, &sa, &osa);
196 (void) ioctl(fileno(fi), TCGETS, &ttyb);
197 flags = ttyb.c_lflag;
198 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
199 (void) ioctl(fileno(fi), TCSETSF, &ttyb);
202 /* XXX assume jmp_buf is typedef'ed to an array */
203 bcopy((char *)old_env, (char *)env, sizeof(env));
206 /* save terminal state*/
207 if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
211 bcopy (&tty_state, &echo_off_tty_state, sizeof (tty_state));
212 echo_off_tty_state.sg_flags &= ~ECHO;
213 if (ioctl(0,TIOCSETP,(char *)&echo_off_tty_state) == -1)
216 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
217 if((fi = fopen("/dev/tty", "r+")) == NULL)
220 setbuf(fi, (char*)NULL);
221 sig = signal(SIGINT, catch);
223 (void) ioctl(fileno(fi), TCGETA, &ttyb);
226 flags = ttyb.c_lflag;
227 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
228 (void) ioctl(fileno(fi), TCSETAF, &ttyb);
231 /* turn off console input echoing */
232 if ((hConStdin = GetStdHandle(STD_INPUT_HANDLE)) != INVALID_HANDLE_VALUE) {
233 if (GetConsoleMode(hConStdin, &oldConMode)) {
234 newConMode = (oldConMode & ~(ENABLE_ECHO_INPUT));
235 if (SetConsoleMode(hConStdin, newConMode)) {
246 (void) printf(prompt);
247 (void) fflush(stdout);
249 h19line(s,sizeof(s),0);
253 if (!fgets(s, maxa, stdin)) {
258 * Otherwise hitting ctrl-d will always leave us inside this loop forever!
264 if ((ptr = index(s, '\n')))
268 printf("\nVerifying, please re-enter %s",prompt);
269 (void) fflush(stdout);
271 h19line(key_string,sizeof(key_string),0);
272 if (!strlen(key_string))
275 if (!fgets(key_string, sizeof(key_string), stdin)) {
279 if ((ptr = index(key_string, '\n')))
282 if (strcmp(s,key_string)) {
283 printf("\n\07\07Mismatch - try again\n");
284 (void) fflush(stdout);
297 * Restore the terminal to its previous characteristics.
298 * Restore the old signal handler for SIGINT.
300 tcsetattr(fno, TCSANOW, &save_ttyb);
301 sigaction(SIGINT, &oldsig, (struct sigaction *)0);
306 * If we got a SIGINT while we were doing things, send the SIGINT
307 * to ourselves so that the calling program receives it (since we
308 * were intercepting it for a period of time.)
311 kill(getpid(), SIGINT);
313 #if defined(AFS_SUN_ENV) && !defined(AFS_SUN5_ENV)
314 ttyb.c_lflag = flags;
315 (void) ioctl(fileno(fi), TCSETSW, &ttyb);
316 (void) sigaction(SIGINT, &osa, (struct sigaction *)NULL);
321 if (ioctl(0,TIOCSETP,(char *)&tty_state))
324 bcopy((char *)env, (char *)old_env, sizeof(env));
326 #if defined (AFS_AIX_ENV) /*|| defined (AFS_HPUX_ENV)*/ || defined(AFS_SGI_ENV) || defined(AFS_LINUX20_ENV)
327 ttyb.c_lflag = flags;
329 (void) ioctl(fileno(fi), TCSETAW, &ttyb);
330 (void) signal(SIGINT, sig);
334 (void) kill(getpid(), SIGINT);
337 /* restore console to original mode settings */
339 (void)SetConsoleMode(hConStdin, oldConMode);
347 bzero(key_string, sizeof (key_string));
348 s[maxa-1] = 0; /* force termination */
349 return !ok; /* return nonzero if not okay */
354 * this can be static since we should never have more than
358 void static (*old_sigfunc[NSIG])();
360 static sigtype (*old_sigfunc[NSIG])();
363 static push_signals()
366 for (i = 0; i < NSIG; i++)
367 old_sigfunc[i] = signal(i,sig_restore);
373 for (i = 0; i < NSIG; i++)
374 (void) signal(i,old_sigfunc[i]);
385 #if defined (AFS_AIX_ENV) || defined (AFS_HPUX_ENV) || defined(AFS_SGI_ENV) || defined(AFS_SUN_ENV) || defined(AFS_LINUX20_ENV)