1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char sccsid[] = "@(#)read_password.c	8.2 (Berkeley) 12/15/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 #if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
28 
29 #include <stdio.h>
30 #include <strings.h>
31 #include <sys/ioctl.h>
32 #include <signal.h>
33 #include <setjmp.h>
34 
35 static jmp_buf env;
36 
37 /*** Routines ****************************************************** */
38 /*
39  * This version just returns the string, doesn't map to key.
40  *
41  * Returns 0 on success, non-zero on failure.
42  */
43 
44 int
45 local_des_read_pw_string(s,max,prompt,verify)
46     char *s;
47     int	max;
48     char *prompt;
49     int	verify;
50 {
51     int ok = 0;
52     char *ptr;
53 
54     jmp_buf old_env;
55     struct sgttyb tty_state;
56     char key_string[BUFSIZ];
57 
58     if (max > BUFSIZ) {
59 	return -1;
60     }
61 
62     /* XXX assume jmp_buf is typedef'ed to an array */
63     bcopy((char *)old_env, (char *)env, sizeof(env));
64     if (setjmp(env))
65 	goto lose;
66 
67     /* save terminal state*/
68     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
69 	return -1;
70 /*
71     push_signals();
72 */
73     /* Turn off echo */
74     tty_state.sg_flags &= ~ECHO;
75     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
76 	return -1;
77     while (!ok) {
78 	(void) printf(prompt);
79 	(void) fflush(stdout);
80 	while (!fgets(s, max, stdin));
81 
82 	if ((ptr = index(s, '\n')))
83 	    *ptr = '\0';
84 	if (verify) {
85 	    printf("\nVerifying, please re-enter %s",prompt);
86 	    (void) fflush(stdout);
87 	    if (!fgets(key_string, sizeof(key_string), stdin)) {
88 		clearerr(stdin);
89 		continue;
90 	    }
91             if ((ptr = index(key_string, '\n')))
92 	    *ptr = '\0';
93 	    if (strcmp(s,key_string)) {
94 		printf("\n\07\07Mismatch - try again\n");
95 		(void) fflush(stdout);
96 		continue;
97 	    }
98 	}
99 	ok = 1;
100     }
101 
102 lose:
103     if (!ok)
104 	bzero(s, max);
105     printf("\n");
106     /* turn echo back on */
107     tty_state.sg_flags |= ECHO;
108     if (ioctl(0,TIOCSETP,(char *)&tty_state))
109 	ok = 0;
110 /*
111     pop_signals();
112 */
113     bcopy((char *)env, (char *)old_env, sizeof(env));
114     if (verify)
115 	bzero(key_string, sizeof (key_string));
116     s[max-1] = 0;		/* force termination */
117     return !ok;			/* return nonzero if not okay */
118 }
119 #endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
120