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