xref: /freebsd/contrib/telnet/libtelnet/read_password.c (revision d0b2dbfa0ecf2bbc9709efc5e20baf8e4b44bbbf)
1 /*-
2  * Copyright (c) 1992, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 #include <sys/cdefs.h>
31 
32 __FBSDID("$FreeBSD$");
33 
34 #ifndef lint
35 #if 0
36 static char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
37 #endif
38 #endif /* not lint */
39 
40 /*
41  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
42  * $Author: jon $
43  *
44  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
45  * of Technology.
46  *
47  * For copying and distribution information, please see the file
48  * <mit-copyright.h>.
49  *
50  * This routine prints the supplied string to standard
51  * output as a prompt, and reads a password string without
52  * echoing.
53  */
54 
55 #if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
56 
57 #include <stdio.h>
58 #include <strings.h>
59 #include <sys/ioctl.h>
60 #include <signal.h>
61 #include <setjmp.h>
62 
63 static jmp_buf env;
64 
65 /*** Routines ****************************************************** */
66 /*
67  * This version just returns the string, doesn't map to key.
68  *
69  * Returns 0 on success, non-zero on failure.
70  */
71 
72 int
73 local_des_read_pw_string(s,max,prompt,verify)
74     char *s;
75     int	max;
76     char *prompt;
77     int	verify;
78 {
79     int ok = 0;
80     char *ptr;
81 
82     jmp_buf old_env;
83     struct sgttyb tty_state;
84     char key_string[BUFSIZ];
85 
86     if (max > BUFSIZ) {
87 	return -1;
88     }
89 
90     /* XXX assume jmp_buf is typedef'ed to an array */
91     memmove((char *)env, (char *)old_env, sizeof(env));
92     if (setjmp(env))
93 	goto lose;
94 
95     /* save terminal state*/
96     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
97 	return -1;
98 /*
99     push_signals();
100 */
101     /* Turn off echo */
102     tty_state.sg_flags &= ~ECHO;
103     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
104 	return -1;
105     while (!ok) {
106 	(void) printf("%s", prompt);
107 	(void) fflush(stdout);
108 	while (!fgets(s, max, stdin));
109 
110 	if ((ptr = strchr(s, '\n')))
111 	    *ptr = '\0';
112 	if (verify) {
113 	    printf("\nVerifying, please re-enter %s",prompt);
114 	    (void) fflush(stdout);
115 	    if (!fgets(key_string, sizeof(key_string), stdin)) {
116 		clearerr(stdin);
117 		continue;
118 	    }
119 	    if ((ptr = strchr(key_string, '\n')))
120 	    *ptr = '\0';
121 	    if (strcmp(s,key_string)) {
122 		printf("\n\07\07Mismatch - try again\n");
123 		(void) fflush(stdout);
124 		continue;
125 	    }
126 	}
127 	ok = 1;
128     }
129 
130 lose:
131     if (!ok)
132 	memset(s, 0, max);
133     printf("\n");
134     /* turn echo back on */
135     tty_state.sg_flags |= ECHO;
136     if (ioctl(0,TIOCSETP,(char *)&tty_state))
137 	ok = 0;
138 /*
139     pop_signals();
140 */
141     memmove((char *)old_env, (char *)env, sizeof(env));
142     if (verify)
143 	memset(key_string, 0, sizeof (key_string));
144     s[max-1] = 0;		/* force termination */
145     return !ok;			/* return nonzero if not okay */
146 }
147 #endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
148