xref: /freebsd/contrib/telnet/libtelnet/read_password.c (revision 2e3f49888ec8851bafb22011533217487764fdb0)
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 #ifndef lint
31 #if 0
32 static char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
33 #endif
34 #endif /* not lint */
35 
36 /*
37  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
38  * $Author: jon $
39  *
40  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
41  * of Technology.
42  *
43  * For copying and distribution information, please see the file
44  * <mit-copyright.h>.
45  *
46  * This routine prints the supplied string to standard
47  * output as a prompt, and reads a password string without
48  * echoing.
49  */
50 
51 #if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
52 
53 #include <stdio.h>
54 #include <strings.h>
55 #include <sys/ioctl.h>
56 #include <signal.h>
57 #include <setjmp.h>
58 
59 static jmp_buf env;
60 
61 /*** Routines ****************************************************** */
62 /*
63  * This version just returns the string, doesn't map to key.
64  *
65  * Returns 0 on success, non-zero on failure.
66  */
67 
68 int
69 local_des_read_pw_string(s,max,prompt,verify)
70     char *s;
71     int	max;
72     char *prompt;
73     int	verify;
74 {
75     int ok = 0;
76     char *ptr;
77 
78     jmp_buf old_env;
79     struct sgttyb tty_state;
80     char key_string[BUFSIZ];
81 
82     if (max > BUFSIZ) {
83 	return -1;
84     }
85 
86     /* XXX assume jmp_buf is typedef'ed to an array */
87     memmove((char *)env, (char *)old_env, sizeof(env));
88     if (setjmp(env))
89 	goto lose;
90 
91     /* save terminal state*/
92     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
93 	return -1;
94 /*
95     push_signals();
96 */
97     /* Turn off echo */
98     tty_state.sg_flags &= ~ECHO;
99     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
100 	return -1;
101     while (!ok) {
102 	(void) printf("%s", prompt);
103 	(void) fflush(stdout);
104 	while (!fgets(s, max, stdin));
105 
106 	if ((ptr = strchr(s, '\n')))
107 	    *ptr = '\0';
108 	if (verify) {
109 	    printf("\nVerifying, please re-enter %s",prompt);
110 	    (void) fflush(stdout);
111 	    if (!fgets(key_string, sizeof(key_string), stdin)) {
112 		clearerr(stdin);
113 		continue;
114 	    }
115 	    if ((ptr = strchr(key_string, '\n')))
116 	    *ptr = '\0';
117 	    if (strcmp(s,key_string)) {
118 		printf("\n\07\07Mismatch - try again\n");
119 		(void) fflush(stdout);
120 		continue;
121 	    }
122 	}
123 	ok = 1;
124     }
125 
126 lose:
127     if (!ok)
128 	memset(s, 0, max);
129     printf("\n");
130     /* turn echo back on */
131     tty_state.sg_flags |= ECHO;
132     if (ioctl(0,TIOCSETP,(char *)&tty_state))
133 	ok = 0;
134 /*
135     pop_signals();
136 */
137     memmove((char *)old_env, (char *)env, sizeof(env));
138     if (verify)
139 	memset(key_string, 0, sizeof (key_string));
140     s[max-1] = 0;		/* force termination */
141     return !ok;			/* return nonzero if not okay */
142 }
143 #endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
144