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