Debian Codegraph @

libtelnet/read_passwd.c

raw
1 /*
2 Copyright (C) 1997-2026 Free Software Foundation, Inc.
3
4 This file is part of GNU Inetutils.
5
6 GNU Inetutils is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or (at
9 your option) any later version.
10
11 GNU Inetutils is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see `http://www.gnu.org/licenses/'. */
18
19 /*
20 * Copyright (c) 1992, 1993
21 * The Regents of the University of California. All rights reserved.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
26 * 1. Redistributions of source code must retain the above copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. Neither the name of the University nor the names of its contributors
32 * may be used to endorse or promote products derived from this software
33 * without specific prior written permission.
34 *
35 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
36 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
37 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
38 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
39 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
40 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
41 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
42 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
43 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
44 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
45 * SUCH DAMAGE.
46 */
47
48 /*
49 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
50 * of Technology.
51 *
52 * For copying and distribution information, please see the file
53 * <mit-copyright.h>.
54 *
55 * This routine prints the supplied string to standard
56 * output as a prompt, and reads a password string without
57 * echoing.
58 */
59
60 #include <config.h>
61
62 #if defined RSA_ENCPWD || defined KRB4_ENCPWD
63
64 # include <stdio.h>
65 # include <sys/ioctl.h>
66 # include <setjmp.h>
67
68 static jmp_buf env;
69
70 /*** Routines ****************************************************** */
71 /*
72 * This version just returns the string, doesn't map to key.
73 *
74 * Returns 0 on success, non-zero on failure.
75 */
76
77 int
78 local_des_read_pw_string (s, max, prompt, verify)
79 char *s;
80 int max;
81 char *prompt;
82 int verify;
83 {
84 int ok = 0;
85 char *ptr;
86
87 jmp_buf old_env;
88 struct sgttyb tty_state;
89 char key_string[BUFSIZ];
90
91 if (max > BUFSIZ)
92 {
93 return -1;
94 }
95
96 /* XXX assume jmp_buf is typedef'ed to an array */
97 memmove ((char *) env, (char *) old_env, sizeof (env));
98 if (setjmp (env))
99 goto lose;
100
101 /* save terminal state */
102 if (ioctl (0, TIOCGETP, (char *) &tty_state) == -1)
103 return -1;
104 /*
105 push_signals();
106 */
107 /* Turn off echo */
108 tty_state.sg_flags &= ~ECHO;
109 if (ioctl (0, TIOCSETP, (char *) &tty_state) == -1)
110 return -1;
111 while (!ok)
112 {
113 printf ("%s", prompt);
114 fflush (stdout);
115 while (!fgets (s, max, stdin));
116
117 if ((ptr = strchr (s, '\n')))
118 *ptr = '\0';
119 if (verify)
120 {
121 printf ("\nVerifying, please re-enter %s", prompt);
122 fflush (stdout);
123 if (!fgets (key_string, sizeof (key_string), stdin))
124 {
125 clearerr (stdin);
126 continue;
127 }
128 if ((ptr = strchr (key_string, '\n')))
129 *ptr = '\0';
130 if (strcmp (s, key_string))
131 {
132 printf ("\n\07\07Mismatch - try again\n");
133 fflush (stdout);
134 continue;
135 }
136 }
137 ok = 1;
138 }
139
140 lose:
141 if (!ok)
142 memset (s, 0, max);
143 printf ("\n");
144 /* turn echo back on */
145 tty_state.sg_flags |= ECHO;
146 if (ioctl (0, TIOCSETP, (char *) &tty_state))
147 ok = 0;
148 /*
149 pop_signals();
150 */
151 memmove ((char *) old_env, (char *) env, sizeof (env));
152 if (verify)
153 memset (key_string, 0, sizeof (key_string));
154 s[max - 1] = 0; /* force termination */
155 return !ok; /* return nonzero if not okay */
156 }
157 #endif /* defined(RSA_ENCPWD) || defined(KRB4_ENCPWD) */