source: heimdal/trunk/lib/hcrypto/ui.c@ 5

Last change on this file since 5 was 5, checked in by Paul Smedley, 9 years ago

Use win32 codepath for password prompt to prevent password echoing - fixes #1

File size: 4.8 KB
Line 
1/*
2 * Copyright (c) 1997 - 2000, 2005 Kungliga Tekniska Högskolan
3 * (Royal Institute of Technology, Stockholm, Sweden).
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 *
17 * 3. Neither the name of the Institute 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 INSTITUTE 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 INSTITUTE 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.
32 */
33
34#include <config.h>
35
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
39#include <signal.h>
40#ifdef HAVE_TERMIOS_H
41#include <termios.h>
42#endif
43#include <roken.h>
44
45#ifdef __OS2__
46#define HAVE_CONIO_H
47#endif
48
49#include <ui.h>
50#ifdef HAVE_CONIO_H
51#include <conio.h>
52#endif
53
54static sig_atomic_t intr_flag;
55
56static void
57intr(int sig)
58{
59 intr_flag++;
60}
61
62#ifdef HAVE_CONIO_H
63
64/*
65 * Windows does console slightly different then then unix case.
66 */
67
68static int
69read_string(const char *preprompt, const char *prompt,
70 char *buf, size_t len, int echo)
71{
72 int of = 0;
73 int c;
74 char *p;
75 void (*oldsigintr)(int);
76
77#ifndef __OS2__
78 _cprintf("%s%s", preprompt, prompt);
79#else
80 fprintf(stderr, "%s%s", preprompt, prompt);
81 fflush(stderr);
82#endif
83
84 oldsigintr = signal(SIGINT, intr);
85
86 p = buf;
87 while(intr_flag == 0){
88#ifndef __OS2__
89 c = ((echo)? _getche(): _getch());
90#else
91 c = ((echo)? getche(): getch());
92#endif
93 if(c == '\n' || c == '\r')
94 break;
95 if(of == 0)
96 *p++ = c;
97 of = (p == buf + len);
98 }
99 if(of)
100 p--;
101 *p = 0;
102
103 if(echo == 0){
104 printf("\n");
105 }
106
107 signal(SIGINT, oldsigintr);
108
109 if(intr_flag)
110 return -2;
111 if(of)
112 return -1;
113 return 0;
114}
115
116#else /* !HAVE_CONIO_H */
117
118#ifndef NSIG
119#define NSIG 47
120#endif
121
122static int
123read_string(const char *preprompt, const char *prompt,
124 char *buf, size_t len, int echo)
125{
126 struct sigaction sigs[NSIG];
127 int oksigs[NSIG];
128 struct sigaction sa;
129 FILE *tty;
130 int ret = 0;
131 int of = 0;
132 int i;
133 int c;
134 char *p;
135
136 struct termios t_new, t_old;
137
138 memset(&oksigs, 0, sizeof(oksigs));
139
140 memset(&sa, 0, sizeof(sa));
141 sa.sa_handler = intr;
142 sigemptyset(&sa.sa_mask);
143 sa.sa_flags = 0;
144 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
145 if (i != SIGALRM)
146 if (sigaction(i, &sa, &sigs[i]) == 0)
147 oksigs[i] = 1;
148
149 if((tty = fopen("/dev/tty", "r")) != NULL)
150 rk_cloexec_file(tty);
151 else
152 tty = stdin;
153
154 fprintf(stderr, "%s%s", preprompt, prompt);
155 fflush(stderr);
156
157 if(echo == 0){
158 tcgetattr(fileno(tty), &t_old);
159 memcpy(&t_new, &t_old, sizeof(t_new));
160 t_new.c_lflag &= ~ECHO;
161 tcsetattr(fileno(tty), TCSANOW, &t_new);
162 }
163 intr_flag = 0;
164 p = buf;
165 while(intr_flag == 0){
166 c = getc(tty);
167 if(c == EOF){
168 if(!ferror(tty))
169 ret = 1;
170 break;
171 }
172 if(c == '\n')
173 break;
174 if(of == 0)
175 *p++ = c;
176 of = (p == buf + len);
177 }
178 if(of)
179 p--;
180 *p = 0;
181
182 if(echo == 0){
183 fprintf(stderr, "\n");
184 tcsetattr(fileno(tty), TCSANOW, &t_old);
185 }
186
187 if(tty != stdin)
188 fclose(tty);
189
190 for(i = 1; i < sizeof(sigs) / sizeof(sigs[0]); i++)
191 if (oksigs[i])
192 sigaction(i, &sigs[i], NULL);
193
194 if(ret)
195 return -3;
196 if(intr_flag)
197 return -2;
198 if(of)
199 return -1;
200 return 0;
201}
202
203#endif /* HAVE_CONIO_H */
204
205int
206UI_UTIL_read_pw_string(char *buf, int length, const char *prompt, int verify)
207{
208 int ret;
209
210 ret = read_string("", prompt, buf, length, 0);
211 if (ret)
212 return ret;
213
214 if (verify) {
215 char *buf2;
216 buf2 = malloc(length);
217 if (buf2 == NULL)
218 return 1;
219
220 ret = read_string("Verify password - ", prompt, buf2, length, 0);
221 if (ret) {
222 free(buf2);
223 return ret;
224 }
225 if (strcmp(buf2, buf) != 0)
226 ret = 1;
227 free(buf2);
228 }
229 return ret;
230}
Note: See TracBrowser for help on using the repository browser.