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.
1329088Smarkm * 3. All advertising materials mentioning features or use of this software
1429088Smarkm *    must display the following acknowledgement:
1529088Smarkm *	This product includes software developed by the University of
1629088Smarkm *	California, Berkeley and its contributors.
1729088Smarkm * 4. Neither the name of the University nor the names of its contributors
1829088Smarkm *    may be used to endorse or promote products derived from this software
1929088Smarkm *    without specific prior written permission.
2029088Smarkm *
2129088Smarkm * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
2229088Smarkm * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
2329088Smarkm * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2429088Smarkm * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2529088Smarkm * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2629088Smarkm * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2729088Smarkm * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2829088Smarkm * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2929088Smarkm * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
3029088Smarkm * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
3129088Smarkm * SUCH DAMAGE.
3229088Smarkm */
3329088Smarkm
3484305Smarkm#include <sys/cdefs.h>
3587139Smarkm
3684305Smarkm__FBSDID("$FreeBSD$");
3784305Smarkm
3829088Smarkm#ifndef lint
3962868Skris#if 0
4029088Smarkmstatic char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
4162868Skris#endif
4229088Smarkm#endif /* not lint */
4329088Smarkm
4429088Smarkm/*
4529088Smarkm * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
4629088Smarkm * $Author: jon $
4729088Smarkm *
4829088Smarkm * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
4929088Smarkm * of Technology.
5029088Smarkm *
5129088Smarkm * For copying and distribution information, please see the file
5229088Smarkm * <mit-copyright.h>.
5329088Smarkm *
5429088Smarkm * This routine prints the supplied string to standard
5529088Smarkm * output as a prompt, and reads a password string without
5629088Smarkm * echoing.
5729088Smarkm */
5829088Smarkm
5929088Smarkm#if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
6029088Smarkm
6129088Smarkm#include <stdio.h>
6229088Smarkm#include <strings.h>
6329088Smarkm#include <sys/ioctl.h>
6429088Smarkm#include <signal.h>
6529088Smarkm#include <setjmp.h>
6629088Smarkm
6729088Smarkmstatic jmp_buf env;
6829088Smarkm
6929088Smarkm/*** Routines ****************************************************** */
7029088Smarkm/*
7129088Smarkm * This version just returns the string, doesn't map to key.
7229088Smarkm *
7329088Smarkm * Returns 0 on success, non-zero on failure.
7429088Smarkm */
7529088Smarkm
7629088Smarkmint
7729088Smarkmlocal_des_read_pw_string(s,max,prompt,verify)
7829088Smarkm    char *s;
7929088Smarkm    int	max;
8029088Smarkm    char *prompt;
8129088Smarkm    int	verify;
8229088Smarkm{
8329088Smarkm    int ok = 0;
8429088Smarkm    char *ptr;
8529088Smarkm
8629088Smarkm    jmp_buf old_env;
8729088Smarkm    struct sgttyb tty_state;
8829088Smarkm    char key_string[BUFSIZ];
8929088Smarkm
9029088Smarkm    if (max > BUFSIZ) {
9129088Smarkm	return -1;
9229088Smarkm    }
9329088Smarkm
9429088Smarkm    /* XXX assume jmp_buf is typedef'ed to an array */
9529088Smarkm    memmove((char *)env, (char *)old_env, sizeof(env));
9629088Smarkm    if (setjmp(env))
9729088Smarkm	goto lose;
9829088Smarkm
9929088Smarkm    /* save terminal state*/
10029088Smarkm    if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
10129088Smarkm	return -1;
10229088Smarkm/*
10329088Smarkm    push_signals();
10429088Smarkm*/
10529088Smarkm    /* Turn off echo */
10629088Smarkm    tty_state.sg_flags &= ~ECHO;
10729088Smarkm    if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
10829088Smarkm	return -1;
10929088Smarkm    while (!ok) {
11062868Skris	(void) printf("%s", prompt);
11129088Smarkm	(void) fflush(stdout);
11229088Smarkm	while (!fgets(s, max, stdin));
11329088Smarkm
11429088Smarkm	if ((ptr = strchr(s, '\n')))
11529088Smarkm	    *ptr = '\0';
11629088Smarkm	if (verify) {
11729088Smarkm	    printf("\nVerifying, please re-enter %s",prompt);
11829088Smarkm	    (void) fflush(stdout);
11929088Smarkm	    if (!fgets(key_string, sizeof(key_string), stdin)) {
12029088Smarkm		clearerr(stdin);
12129088Smarkm		continue;
12229088Smarkm	    }
12329088Smarkm	    if ((ptr = strchr(key_string, '\n')))
12429088Smarkm	    *ptr = '\0';
12529088Smarkm	    if (strcmp(s,key_string)) {
12629088Smarkm		printf("\n\07\07Mismatch - try again\n");
12729088Smarkm		(void) fflush(stdout);
12829088Smarkm		continue;
12929088Smarkm	    }
13029088Smarkm	}
13129088Smarkm	ok = 1;
13229088Smarkm    }
13329088Smarkm
13429088Smarkmlose:
13529088Smarkm    if (!ok)
13629088Smarkm	memset(s, 0, max);
13729088Smarkm    printf("\n");
13829088Smarkm    /* turn echo back on */
13929088Smarkm    tty_state.sg_flags |= ECHO;
14029088Smarkm    if (ioctl(0,TIOCSETP,(char *)&tty_state))
14129088Smarkm	ok = 0;
14229088Smarkm/*
14329088Smarkm    pop_signals();
14429088Smarkm*/
14529088Smarkm    memmove((char *)old_env, (char *)env, sizeof(env));
14629088Smarkm    if (verify)
14729088Smarkm	memset(key_string, 0, sizeof (key_string));
14829088Smarkm    s[max-1] = 0;		/* force termination */
14929088Smarkm    return !ok;			/* return nonzero if not okay */
15029088Smarkm}
15129088Smarkm#endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
152