129088Smarkm/*-
229088Smarkm * Copyright (c) 1992, 1993
329088Smarkm *	The Regents of the University of California.  All rights reserved.
429088Smarkm *
529088Smarkm * Redistribution and use in source and binary forms, with or without
629088Smarkm * modification, are permitted provided that the following conditions
729088Smarkm * are met:
829088Smarkm * 1. Redistributions of source code must retain the above copyright
929088Smarkm *    notice, this list of conditions and the following disclaimer.
1029088Smarkm * 2. Redistributions in binary form must reproduce the above copyright
1129088Smarkm *    notice, this list of conditions and the following disclaimer in the
1229088Smarkm *    documentation and/or other materials provided with the distribution.
13351432Semaste * 3. Neither the name of the University nor the names of its contributors
1429088Smarkm *    may be used to endorse or promote products derived from this software
1529088Smarkm *    without specific prior written permission.
1629088Smarkm *
1729088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1829088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1929088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2029088Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2129088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2229088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2329088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2429088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2529088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2629088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2729088Smarkm * SUCH DAMAGE.
2829088Smarkm */
2929088Smarkm
3084305Smarkm#include <sys/cdefs.h>
3187139Smarkm
3284305Smarkm__FBSDID("$FreeBSD: stable/11/contrib/telnet/libtelnet/read_password.c 351432 2019-08-23 17:40:47Z emaste $");
3384305Smarkm
3429088Smarkm#ifndef lint
3562868Skris#if 0
3629088Smarkmstatic char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
3762868Skris#endif
3829088Smarkm#endif /* not lint */
3929088Smarkm
4029088Smarkm/*
4129088Smarkm * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
4229088Smarkm * $Author: jon $
4329088Smarkm *
4429088Smarkm * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
4529088Smarkm * of Technology.
4629088Smarkm *
4729088Smarkm * For copying and distribution information, please see the file
4829088Smarkm * <mit-copyright.h>.
4929088Smarkm *
5029088Smarkm * This routine prints the supplied string to standard
5129088Smarkm * output as a prompt, and reads a password string without
5229088Smarkm * echoing.
5329088Smarkm */
5429088Smarkm
5529088Smarkm#if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
5629088Smarkm
5729088Smarkm#include <stdio.h>
5829088Smarkm#include <strings.h>
5929088Smarkm#include <sys/ioctl.h>
6029088Smarkm#include <signal.h>
6129088Smarkm#include <setjmp.h>
6229088Smarkm
6329088Smarkmstatic jmp_buf env;
6429088Smarkm
6529088Smarkm/*** Routines ****************************************************** */
6629088Smarkm/*
6729088Smarkm * This version just returns the string, doesn't map to key.
6829088Smarkm *
6929088Smarkm * Returns 0 on success, non-zero on failure.
7029088Smarkm */
7129088Smarkm
7229088Smarkmint
7329088Smarkmlocal_des_read_pw_string(s,max,prompt,verify)
7429088Smarkm    char *s;
7529088Smarkm    int	max;
7629088Smarkm    char *prompt;
7729088Smarkm    int	verify;
7829088Smarkm{
7929088Smarkm    int ok = 0;
8029088Smarkm    char *ptr;
8129088Smarkm
8229088Smarkm    jmp_buf old_env;
8329088Smarkm    struct sgttyb tty_state;
8429088Smarkm    char key_string[BUFSIZ];
8529088Smarkm
8629088Smarkm    if (max > BUFSIZ) {
8729088Smarkm	return -1;
8829088Smarkm    }
8929088Smarkm
9029088Smarkm    /* XXX assume jmp_buf is typedef'ed to an array */
9129088Smarkm    memmove((char *)env, (char *)old_env, sizeof(env));
9229088Smarkm    if (setjmp(env))
9329088Smarkm	goto lose;
9429088Smarkm
9529088Smarkm    /* save terminal state*/
9629088Smarkm    if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
9729088Smarkm	return -1;
9829088Smarkm/*
9929088Smarkm    push_signals();
10029088Smarkm*/
10129088Smarkm    /* Turn off echo */
10229088Smarkm    tty_state.sg_flags &= ~ECHO;
10329088Smarkm    if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
10429088Smarkm	return -1;
10529088Smarkm    while (!ok) {
10662868Skris	(void) printf("%s", prompt);
10729088Smarkm	(void) fflush(stdout);
10829088Smarkm	while (!fgets(s, max, stdin));
10929088Smarkm
11029088Smarkm	if ((ptr = strchr(s, '\n')))
11129088Smarkm	    *ptr = '\0';
11229088Smarkm	if (verify) {
11329088Smarkm	    printf("\nVerifying, please re-enter %s",prompt);
11429088Smarkm	    (void) fflush(stdout);
11529088Smarkm	    if (!fgets(key_string, sizeof(key_string), stdin)) {
11629088Smarkm		clearerr(stdin);
11729088Smarkm		continue;
11829088Smarkm	    }
11929088Smarkm	    if ((ptr = strchr(key_string, '\n')))
12029088Smarkm	    *ptr = '\0';
12129088Smarkm	    if (strcmp(s,key_string)) {
12229088Smarkm		printf("\n\07\07Mismatch - try again\n");
12329088Smarkm		(void) fflush(stdout);
12429088Smarkm		continue;
12529088Smarkm	    }
12629088Smarkm	}
12729088Smarkm	ok = 1;
12829088Smarkm    }
12929088Smarkm
13029088Smarkmlose:
13129088Smarkm    if (!ok)
13229088Smarkm	memset(s, 0, max);
13329088Smarkm    printf("\n");
13429088Smarkm    /* turn echo back on */
13529088Smarkm    tty_state.sg_flags |= ECHO;
13629088Smarkm    if (ioctl(0,TIOCSETP,(char *)&tty_state))
13729088Smarkm	ok = 0;
13829088Smarkm/*
13929088Smarkm    pop_signals();
14029088Smarkm*/
14129088Smarkm    memmove((char *)old_env, (char *)env, sizeof(env));
14229088Smarkm    if (verify)
14329088Smarkm	memset(key_string, 0, sizeof (key_string));
14429088Smarkm    s[max-1] = 0;		/* force termination */
14529088Smarkm    return !ok;			/* return nonzero if not okay */
14629088Smarkm}
14729088Smarkm#endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
148