BSD 4_4 release
[unix-history] / usr / src / lib / libtelnet / read_password.c
CommitLineData
640d6f08 1/*-
11794364
KB
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
640d6f08 4 *
ad787160
C
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.
640d6f08
DB
32 */
33
34#ifndef lint
ad787160 35static char sccsid[] = "@(#)read_password.c 8.1 (Berkeley) 6/4/93";
640d6f08
DB
36#endif /* not lint */
37
38/*
39 * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
40 * $Author: jon $
41 *
42 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
43 * of Technology.
44 *
45 * For copying and distribution information, please see the file
46 * <mit-copyright.h>.
47 *
48 * This routine prints the supplied string to standard
49 * output as a prompt, and reads a password string without
50 * echoing.
51 */
52
53#include <stdio.h>
54#include <strings.h>
55#include <sys/ioctl.h>
56#include <signal.h>
57#include <setjmp.h>
58
59static jmp_buf env;
60
61/*** Routines ****************************************************** */
62/*
63 * This version just returns the string, doesn't map to key.
64 *
65 * Returns 0 on success, non-zero on failure.
66 */
67
68int
69local_des_read_pw_string(s,max,prompt,verify)
70 char *s;
71 int max;
72 char *prompt;
73 int verify;
74{
75 int ok = 0;
76 char *ptr;
77
78 jmp_buf old_env;
79 struct sgttyb tty_state;
80 char key_string[BUFSIZ];
81
82 if (max > BUFSIZ) {
83 return -1;
84 }
85
86 /* XXX assume jmp_buf is typedef'ed to an array */
87 bcopy((char *)old_env, (char *)env, sizeof(env));
88 if (setjmp(env))
89 goto lose;
90
91 /* save terminal state*/
92 if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
93 return -1;
94/*
95 push_signals();
96*/
97 /* Turn off echo */
98 tty_state.sg_flags &= ~ECHO;
99 if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
100 return -1;
101 while (!ok) {
102 (void) printf(prompt);
103 (void) fflush(stdout);
104 while (!fgets(s, max, stdin));
105
106 if ((ptr = index(s, '\n')))
107 *ptr = '\0';
108 if (verify) {
109 printf("\nVerifying, please re-enter %s",prompt);
110 (void) fflush(stdout);
111 if (!fgets(key_string, sizeof(key_string), stdin)) {
112 clearerr(stdin);
113 continue;
114 }
115 if ((ptr = index(key_string, '\n')))
116 *ptr = '\0';
117 if (strcmp(s,key_string)) {
118 printf("\n\07\07Mismatch - try again\n");
119 (void) fflush(stdout);
120 continue;
121 }
122 }
123 ok = 1;
124 }
125
126lose:
127 if (!ok)
128 bzero(s, max);
129 printf("\n");
130 /* turn echo back on */
131 tty_state.sg_flags |= ECHO;
132 if (ioctl(0,TIOCSETP,(char *)&tty_state))
133 ok = 0;
134/*
135 pop_signals();
136*/
137 bcopy((char *)env, (char *)old_env, sizeof(env));
138 if (verify)
139 bzero(key_string, sizeof (key_string));
140 s[max-1] = 0; /* force termination */
141 return !ok; /* return nonzero if not okay */
142}