kernel - support dummy reallocblks in devfs
[dragonfly.git] / lib / libtelnet / read_password.c
bloba3317c55aceb14d390ac4d8c23d48c562d82f053
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#)read_password.c 8.3 (Berkeley) 5/30/95
30 * $FreeBSD: src/crypto/telnet/libtelnet/read_password.c,v 1.1.1.1.8.2 2002/04/13 10:59:07 markm Exp $
34 * $Source: /mit/kerberos/src/lib/des/RCS/read_password.c,v $
35 * $Author: jon $
37 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
38 * of Technology.
40 * For copying and distribution information, please see the file
41 * <mit-copyright.h>.
43 * This routine prints the supplied string to standard
44 * output as a prompt, and reads a password string without
45 * echoing.
48 #if defined(RSA_ENCPWD) || defined(KRB4_ENCPWD)
50 #include <stdio.h>
51 #include <strings.h>
52 #include <sys/ioctl.h>
53 #include <signal.h>
54 #include <setjmp.h>
56 static jmp_buf env;
58 /*** Routines ****************************************************** */
60 * This version just returns the string, doesn't map to key.
62 * Returns 0 on success, non-zero on failure.
65 int
66 local_des_read_pw_string(s,max,prompt,verify)
67 char *s;
68 int max;
69 char *prompt;
70 int verify;
72 int ok = 0;
73 char *ptr;
75 jmp_buf old_env;
76 struct sgttyb tty_state;
77 char key_string[BUFSIZ];
79 if (max > BUFSIZ) {
80 return -1;
83 /* XXX assume jmp_buf is typedef'ed to an array */
84 memmove((char *)env, (char *)old_env, sizeof(env));
85 if (setjmp(env))
86 goto lose;
88 /* save terminal state*/
89 if (ioctl(0,TIOCGETP,(char *)&tty_state) == -1)
90 return -1;
92 push_signals();
94 /* Turn off echo */
95 tty_state.sg_flags &= ~ECHO;
96 if (ioctl(0,TIOCSETP,(char *)&tty_state) == -1)
97 return -1;
98 while (!ok) {
99 (void) printf("%s", prompt);
100 (void) fflush(stdout);
101 while (!fgets(s, max, stdin));
103 if ((ptr = strchr(s, '\n')))
104 *ptr = '\0';
105 if (verify) {
106 printf("\nVerifying, please re-enter %s",prompt);
107 (void) fflush(stdout);
108 if (!fgets(key_string, sizeof(key_string), stdin)) {
109 clearerr(stdin);
110 continue;
112 if ((ptr = strchr(key_string, '\n')))
113 *ptr = '\0';
114 if (strcmp(s,key_string)) {
115 printf("\n\07\07Mismatch - try again\n");
116 (void) fflush(stdout);
117 continue;
120 ok = 1;
123 lose:
124 if (!ok)
125 memset(s, 0, max);
126 printf("\n");
127 /* turn echo back on */
128 tty_state.sg_flags |= ECHO;
129 if (ioctl(0,TIOCSETP,(char *)&tty_state))
130 ok = 0;
132 pop_signals();
134 memmove((char *)old_env, (char *)env, sizeof(env));
135 if (verify)
136 memset(key_string, 0, sizeof (key_string));
137 s[max-1] = 0; /* force termination */
138 return !ok; /* return nonzero if not okay */
140 #endif /* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */