read_password.c revision 87139
174011Sjhb/*-
268685Sjhb * Copyright (c) 1992, 1993
368685Sjhb *	The Regents of the University of California.  All rights reserved.
468685Sjhb *
568685Sjhb * Redistribution and use in source and binary forms, with or without
668685Sjhb * modification, are permitted provided that the following conditions
768685Sjhb * are met:
868685Sjhb * 1. Redistributions of source code must retain the above copyright
968685Sjhb *    notice, this list of conditions and the following disclaimer.
1068685Sjhb * 2. Redistributions in binary form must reproduce the above copyright
1168685Sjhb *    notice, this list of conditions and the following disclaimer in the
1268685Sjhb *    documentation and/or other materials provided with the distribution.
1368685Sjhb * 3. All advertising materials mentioning features or use of this software
1468685Sjhb *    must display the following acknowledgement:
1568685Sjhb *	This product includes software developed by the University of
1668685Sjhb *	California, Berkeley and its contributors.
1768685Sjhb * 4. Neither the name of the University nor the names of its contributors
1868685Sjhb *    may be used to endorse or promote products derived from this software
1968685Sjhb *    without specific prior written permission.
2068685Sjhb *
2168685Sjhb * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2268685Sjhb * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2368685Sjhb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2468685Sjhb * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2568685Sjhb * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26254617Sjkim * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27206622Suqs * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2868685Sjhb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2968685Sjhb * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3068685Sjhb * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3168685Sjhb * SUCH DAMAGE.
3268685Sjhb */
33150628Sjhb
3468685Sjhb#include <sys/cdefs.h>
3568685Sjhb
3668685Sjhb__FBSDID("$FreeBSD: head/contrib/telnet/libtelnet/read_password.c 87139 2001-11-30 21:06:38Z markm $");
3768685Sjhb
3868685Sjhb#ifndef lint
3968685Sjhb#if 0
4068685Sjhbstatic char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
4184306Sru#endif
4284306Sru#endif /* not lint */
4368685Sjhb
4489462Sru/*
4568685Sjhb * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
4689462Sru * $Author: jon $
4768685Sjhb *
4889462Sru * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
4989462Sru * of Technology.
5089462Sru *
5189462Sru * For copying and distribution information, please see the file
5268685Sjhb * <mit-copyright.h>.
5389462Sru *
54150628Sjhb * This routine prints the supplied string to standard
55150628Sjhb * output as a prompt, and reads a password string without
5689462Sru * echoing.
5789462Sru */
5889462Sru
5968685Sjhb#if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
6089462Sru
6168685Sjhb#include <stdio.h>
6289462Sru#include <strings.h>
6368685Sjhb#include <sys/ioctl.h>
6489462Sru#include <signal.h>
65254617Sjkim#include <setjmp.h>
66254617Sjkim
67254617Sjkimstatic jmp_buf env;
68254617Sjkim
6968685Sjhb/*** Routines ****************************************************** */
7068685Sjhb/*
7168685Sjhb * This version just returns the string, doesn't map to key.
7268685Sjhb *
7368685Sjhb * Returns 0 on success, non-zero on failure.
7468685Sjhb */
7589192Sru
76115440Shmpint
7768685Sjhblocal_des_read_pw_string(s,max,prompt,verify)
7868685Sjhb    char *s;
7989192Sru    int	max;
8089192Sru    char *prompt;
8189192Sru    int	verify;
8268685Sjhb{
8389192Sru    int ok = 0;
8468685Sjhb    char *ptr;
8589192Sru
8668685Sjhb    jmp_buf old_env;
8789192Sru    struct sgttyb tty_state;
8868685Sjhb    char key_string[BUFSIZ];
8989192Sru
9068685Sjhb    if (max > BUFSIZ) {
9168685Sjhb	return -1;
9268685Sjhb    }
9368685Sjhb
9468685Sjhb    /* XXX assume jmp_buf is typedef'ed to an array */
9587999Sjake    memmove((char *)env, (char *)old_env, sizeof(env));
9689192Sru    if (setjmp(env))
9789192Sru	goto lose;
9889192Sru
9989192Sru    /* save terminal state*/
10089192Sru    if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
10187999Sjake	return -1;
10289192Sru/*
10387999Sjake    push_signals();
10489192Sru*/
10587999Sjake    /* Turn off echo */
10689192Sru    tty_state.sg_flags &= ~ECHO;
10787999Sjake    if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
10887999Sjake	return -1;
10987999Sjake    while (!ok) {
11087999Sjake	(void) printf("%s", prompt);
11187999Sjake	(void) fflush(stdout);
11268685Sjhb	while (!fgets(s, max, stdin));
11389192Sru
11468685Sjhb	if ((ptr = strchr(s, '\n')))
11568685Sjhb	    *ptr = '\0';
11668685Sjhb	if (verify) {
11789192Sru	    printf("\nVerifying, please re-enter %s",prompt);
11868685Sjhb	    (void) fflush(stdout);
11968685Sjhb	    if (!fgets(key_string, sizeof(key_string), stdin)) {
12068685Sjhb		clearerr(stdin);
12168685Sjhb		continue;
12268685Sjhb	    }
12389192Sru	    if ((ptr = strchr(key_string, '\n')))
12468685Sjhb	    *ptr = '\0';
12568685Sjhb	    if (strcmp(s,key_string)) {
12668685Sjhb		printf("\n\07\07Mismatch - try again\n");
12768685Sjhb		(void) fflush(stdout);
12868685Sjhb		continue;
12968685Sjhb	    }
13068685Sjhb	}
13168685Sjhb	ok = 1;
13289192Sru    }
13368685Sjhb
134115440Shmplose:
13568685Sjhb    if (!ok)
13668685Sjhb	memset(s, 0, max);
13768685Sjhb    printf("\n");
13868685Sjhb    /* turn echo back on */
13968685Sjhb    tty_state.sg_flags |= ECHO;
14068685Sjhb    if (ioctl(0,TIOCSETP,(char *)&tty_state))
14168685Sjhb	ok = 0;
14268685Sjhb/*
14368685Sjhb    pop_signals();
14468685Sjhb*/
14568685Sjhb    memmove((char *)old_env, (char *)env, sizeof(env));
14689192Sru    if (verify)
14768685Sjhb	memset(key_string, 0, sizeof (key_string));
148115440Shmp    s[max-1] = 0;		/* force termination */
14968685Sjhb    return !ok;			/* return nonzero if not okay */
15068685Sjhb}
15168685Sjhb#endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
15268685Sjhb