1 /*-
2  * Copyright (c) 1992 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)read_password.c	5.1 (Berkeley) 01/20/93";
10 #endif /* not lint */
11 
12 /*
13  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
14  * $Author: jon $
15  *
16  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
17  * of Technology.
18  *
19  * For copying and distribution information, please see the file
20  * <mit-copyright.h>.
21  *
22  * This routine prints the supplied string to standard
23  * output as a prompt, and reads a password string without
24  * echoing.
25  */
26 
27 #include <stdio.h>
28 #include <strings.h>
29 #include <sys/ioctl.h>
30 #include <signal.h>
31 #include <setjmp.h>
32 
33 static jmp_buf env;
34 
35 /*** Routines ****************************************************** */
36 /*
37  * This version just returns the string, doesn't map to key.
38  *
39  * Returns 0 on success, non-zero on failure.
40  */
41 
42 int
43 local_des_read_pw_string(s,max,prompt,verify)
44     char *s;
45     int	max;
46     char *prompt;
47     int	verify;
48 {
49     int ok = 0;
50     char *ptr;
51 
52     jmp_buf old_env;
53     struct sgttyb tty_state;
54     char key_string[BUFSIZ];
55 
56     if (max > BUFSIZ) {
57 	return -1;
58     }
59 
60     /* XXX assume jmp_buf is typedef'ed to an array */
61     bcopy((char *)old_env, (char *)env, sizeof(env));
62     if (setjmp(env))
63 	goto lose;
64 
65     /* save terminal state*/
66     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
67 	return -1;
68 /*
69     push_signals();
70 */
71     /* Turn off echo */
72     tty_state.sg_flags &= ~ECHO;
73     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
74 	return -1;
75     while (!ok) {
76 	(void) printf(prompt);
77 	(void) fflush(stdout);
78 	while (!fgets(s, max, stdin));
79 
80 	if ((ptr = index(s, '\n')))
81 	    *ptr = '\0';
82 	if (verify) {
83 	    printf("\nVerifying, please re-enter %s",prompt);
84 	    (void) fflush(stdout);
85 	    if (!fgets(key_string, sizeof(key_string), stdin)) {
86 		clearerr(stdin);
87 		continue;
88 	    }
89             if ((ptr = index(key_string, '\n')))
90 	    *ptr = '\0';
91 	    if (strcmp(s,key_string)) {
92 		printf("\n\07\07Mismatch - try again\n");
93 		(void) fflush(stdout);
94 		continue;
95 	    }
96 	}
97 	ok = 1;
98     }
99 
100 lose:
101     if (!ok)
102 	bzero(s, max);
103     printf("\n");
104     /* turn echo back on */
105     tty_state.sg_flags |= ECHO;
106     if (ioctl(0,TIOCSETP,(char *)&tty_state))
107 	ok = 0;
108 /*
109     pop_signals();
110 */
111     bcopy((char *)env, (char *)old_env, sizeof(env));
112     if (verify)
113 	bzero(key_string, sizeof (key_string));
114     s[max-1] = 0;		/* force termination */
115     return !ok;			/* return nonzero if not okay */
116 }
117