xref: /freebsd/contrib/telnet/libtelnet/read_password.c (revision a3c858005cae175e277f6f6735ca9eaea7eaf3c3)
181cb6ddcSMark Murray /*-
281cb6ddcSMark Murray  * Copyright (c) 1992, 1993
381cb6ddcSMark Murray  *	The Regents of the University of California.  All rights reserved.
481cb6ddcSMark Murray  *
581cb6ddcSMark Murray  * Redistribution and use in source and binary forms, with or without
681cb6ddcSMark Murray  * modification, are permitted provided that the following conditions
781cb6ddcSMark Murray  * are met:
881cb6ddcSMark Murray  * 1. Redistributions of source code must retain the above copyright
981cb6ddcSMark Murray  *    notice, this list of conditions and the following disclaimer.
1081cb6ddcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
1181cb6ddcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
1281cb6ddcSMark Murray  *    documentation and/or other materials provided with the distribution.
13*83129c0bSEd Maste  * 3. Neither the name of the University nor the names of its contributors
1481cb6ddcSMark Murray  *    may be used to endorse or promote products derived from this software
1581cb6ddcSMark Murray  *    without specific prior written permission.
1681cb6ddcSMark Murray  *
1781cb6ddcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
1881cb6ddcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1981cb6ddcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2081cb6ddcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
2181cb6ddcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2281cb6ddcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2381cb6ddcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2481cb6ddcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2581cb6ddcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2681cb6ddcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2781cb6ddcSMark Murray  * SUCH DAMAGE.
2881cb6ddcSMark Murray  */
2981cb6ddcSMark Murray 
3081cb6ddcSMark Murray #ifndef lint
3119a32101SKris Kennaway #if 0
3281cb6ddcSMark Murray static char sccsid[] = "@(#)read_password.c	8.3 (Berkeley) 5/30/95";
3319a32101SKris Kennaway #endif
3481cb6ddcSMark Murray #endif /* not lint */
3581cb6ddcSMark Murray 
3681cb6ddcSMark Murray /*
3781cb6ddcSMark Murray  * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
3881cb6ddcSMark Murray  * $Author: jon $
3981cb6ddcSMark Murray  *
4081cb6ddcSMark Murray  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
4181cb6ddcSMark Murray  * of Technology.
4281cb6ddcSMark Murray  *
4381cb6ddcSMark Murray  * For copying and distribution information, please see the file
4481cb6ddcSMark Murray  * <mit-copyright.h>.
4581cb6ddcSMark Murray  *
4681cb6ddcSMark Murray  * This routine prints the supplied string to standard
4781cb6ddcSMark Murray  * output as a prompt, and reads a password string without
4881cb6ddcSMark Murray  * echoing.
4981cb6ddcSMark Murray  */
5081cb6ddcSMark Murray 
5181cb6ddcSMark Murray #if	defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
5281cb6ddcSMark Murray 
5381cb6ddcSMark Murray #include <stdio.h>
5481cb6ddcSMark Murray #include <strings.h>
5581cb6ddcSMark Murray #include <sys/ioctl.h>
5681cb6ddcSMark Murray #include <signal.h>
5781cb6ddcSMark Murray #include <setjmp.h>
5881cb6ddcSMark Murray 
5981cb6ddcSMark Murray static jmp_buf env;
6081cb6ddcSMark Murray 
6181cb6ddcSMark Murray /*** Routines ****************************************************** */
6281cb6ddcSMark Murray /*
6381cb6ddcSMark Murray  * This version just returns the string, doesn't map to key.
6481cb6ddcSMark Murray  *
6581cb6ddcSMark Murray  * Returns 0 on success, non-zero on failure.
6681cb6ddcSMark Murray  */
6781cb6ddcSMark Murray 
6881cb6ddcSMark Murray int
local_des_read_pw_string(s,max,prompt,verify)6981cb6ddcSMark Murray local_des_read_pw_string(s,max,prompt,verify)
7081cb6ddcSMark Murray     char *s;
7181cb6ddcSMark Murray     int	max;
7281cb6ddcSMark Murray     char *prompt;
7381cb6ddcSMark Murray     int	verify;
7481cb6ddcSMark Murray {
7581cb6ddcSMark Murray     int ok = 0;
7681cb6ddcSMark Murray     char *ptr;
7781cb6ddcSMark Murray 
7881cb6ddcSMark Murray     jmp_buf old_env;
7981cb6ddcSMark Murray     struct sgttyb tty_state;
8081cb6ddcSMark Murray     char key_string[BUFSIZ];
8181cb6ddcSMark Murray 
8281cb6ddcSMark Murray     if (max > BUFSIZ) {
8381cb6ddcSMark Murray 	return -1;
8481cb6ddcSMark Murray     }
8581cb6ddcSMark Murray 
8681cb6ddcSMark Murray     /* XXX assume jmp_buf is typedef'ed to an array */
8781cb6ddcSMark Murray     memmove((char *)env, (char *)old_env, sizeof(env));
8881cb6ddcSMark Murray     if (setjmp(env))
8981cb6ddcSMark Murray 	goto lose;
9081cb6ddcSMark Murray 
9181cb6ddcSMark Murray     /* save terminal state*/
9281cb6ddcSMark Murray     if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
9381cb6ddcSMark Murray 	return -1;
9481cb6ddcSMark Murray /*
9581cb6ddcSMark Murray     push_signals();
9681cb6ddcSMark Murray */
9781cb6ddcSMark Murray     /* Turn off echo */
9881cb6ddcSMark Murray     tty_state.sg_flags &= ~ECHO;
9981cb6ddcSMark Murray     if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
10081cb6ddcSMark Murray 	return -1;
10181cb6ddcSMark Murray     while (!ok) {
10219a32101SKris Kennaway 	(void) printf("%s", prompt);
10381cb6ddcSMark Murray 	(void) fflush(stdout);
10481cb6ddcSMark Murray 	while (!fgets(s, max, stdin));
10581cb6ddcSMark Murray 
10681cb6ddcSMark Murray 	if ((ptr = strchr(s, '\n')))
10781cb6ddcSMark Murray 	    *ptr = '\0';
10881cb6ddcSMark Murray 	if (verify) {
10981cb6ddcSMark Murray 	    printf("\nVerifying, please re-enter %s",prompt);
11081cb6ddcSMark Murray 	    (void) fflush(stdout);
11181cb6ddcSMark Murray 	    if (!fgets(key_string, sizeof(key_string), stdin)) {
11281cb6ddcSMark Murray 		clearerr(stdin);
11381cb6ddcSMark Murray 		continue;
11481cb6ddcSMark Murray 	    }
11581cb6ddcSMark Murray 	    if ((ptr = strchr(key_string, '\n')))
11681cb6ddcSMark Murray 	    *ptr = '\0';
11781cb6ddcSMark Murray 	    if (strcmp(s,key_string)) {
11881cb6ddcSMark Murray 		printf("\n\07\07Mismatch - try again\n");
11981cb6ddcSMark Murray 		(void) fflush(stdout);
12081cb6ddcSMark Murray 		continue;
12181cb6ddcSMark Murray 	    }
12281cb6ddcSMark Murray 	}
12381cb6ddcSMark Murray 	ok = 1;
12481cb6ddcSMark Murray     }
12581cb6ddcSMark Murray 
12681cb6ddcSMark Murray lose:
12781cb6ddcSMark Murray     if (!ok)
12881cb6ddcSMark Murray 	memset(s, 0, max);
12981cb6ddcSMark Murray     printf("\n");
13081cb6ddcSMark Murray     /* turn echo back on */
13181cb6ddcSMark Murray     tty_state.sg_flags |= ECHO;
13281cb6ddcSMark Murray     if (ioctl(0,TIOCSETP,(char *)&tty_state))
13381cb6ddcSMark Murray 	ok = 0;
13481cb6ddcSMark Murray /*
13581cb6ddcSMark Murray     pop_signals();
13681cb6ddcSMark Murray */
13781cb6ddcSMark Murray     memmove((char *)old_env, (char *)env, sizeof(env));
13881cb6ddcSMark Murray     if (verify)
13981cb6ddcSMark Murray 	memset(key_string, 0, sizeof (key_string));
14081cb6ddcSMark Murray     s[max-1] = 0;		/* force termination */
14181cb6ddcSMark Murray     return !ok;			/* return nonzero if not okay */
14281cb6ddcSMark Murray }
14381cb6ddcSMark Murray #endif	/* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */
144