]>
Commit | Line | Data |
---|---|---|
a63d5eaa | 1 | /* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */ |
219a3580 RL |
2 | /* Written by Richard Levitte (richard@levitte.org) and others |
3 | * for the OpenSSL project 2001. | |
a63d5eaa RL |
4 | */ |
5 | /* ==================================================================== | |
219a3580 | 6 | * Copyright (c) 2001 The OpenSSL Project. All rights reserved. |
a63d5eaa RL |
7 | * |
8 | * Redistribution and use in source and binary forms, with or without | |
9 | * modification, are permitted provided that the following conditions | |
10 | * are met: | |
11 | * | |
12 | * 1. Redistributions of source code must retain the above copyright | |
13 | * notice, this list of conditions and the following disclaimer. | |
14 | * | |
15 | * 2. Redistributions in binary form must reproduce the above copyright | |
16 | * notice, this list of conditions and the following disclaimer in | |
17 | * the documentation and/or other materials provided with the | |
18 | * distribution. | |
19 | * | |
20 | * 3. All advertising materials mentioning features or use of this | |
21 | * software must display the following acknowledgment: | |
22 | * "This product includes software developed by the OpenSSL Project | |
23 | * for use in the OpenSSL Toolkit. (http://www.openssl.org/)" | |
24 | * | |
25 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to | |
26 | * endorse or promote products derived from this software without | |
27 | * prior written permission. For written permission, please contact | |
28 | * openssl-core@openssl.org. | |
29 | * | |
30 | * 5. Products derived from this software may not be called "OpenSSL" | |
31 | * nor may "OpenSSL" appear in their names without prior written | |
32 | * permission of the OpenSSL Project. | |
33 | * | |
34 | * 6. Redistributions of any form whatsoever must retain the following | |
35 | * acknowledgment: | |
36 | * "This product includes software developed by the OpenSSL Project | |
37 | * for use in the OpenSSL Toolkit (http://www.openssl.org/)" | |
38 | * | |
39 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY | |
40 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
41 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | |
42 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR | |
43 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | |
44 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT | |
45 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
46 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
47 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
48 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
49 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
50 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
51 | * ==================================================================== | |
52 | * | |
53 | * This product includes cryptographic software written by Eric Young | |
54 | * (eay@cryptsoft.com). This product includes software written by Tim | |
55 | * Hudson (tjh@cryptsoft.com). | |
56 | * | |
57 | */ | |
58 | ||
02790299 RL |
59 | /* The lowest level part of this file was previously in crypto/des/read_pwd.c, |
60 | * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com) | |
61 | * All rights reserved. | |
62 | * | |
63 | * This package is an SSL implementation written | |
64 | * by Eric Young (eay@cryptsoft.com). | |
65 | * The implementation was written so as to conform with Netscapes SSL. | |
66 | * | |
67 | * This library is free for commercial and non-commercial use as long as | |
68 | * the following conditions are aheared to. The following conditions | |
69 | * apply to all code found in this distribution, be it the RC4, RSA, | |
70 | * lhash, DES, etc., code; not just the SSL code. The SSL documentation | |
71 | * included with this distribution is covered by the same copyright terms | |
72 | * except that the holder is Tim Hudson (tjh@cryptsoft.com). | |
73 | * | |
74 | * Copyright remains Eric Young's, and as such any Copyright notices in | |
75 | * the code are not to be removed. | |
76 | * If this package is used in a product, Eric Young should be given attribution | |
77 | * as the author of the parts of the library used. | |
78 | * This can be in the form of a textual message at program startup or | |
79 | * in documentation (online or textual) provided with the package. | |
80 | * | |
81 | * Redistribution and use in source and binary forms, with or without | |
82 | * modification, are permitted provided that the following conditions | |
83 | * are met: | |
84 | * 1. Redistributions of source code must retain the copyright | |
85 | * notice, this list of conditions and the following disclaimer. | |
86 | * 2. Redistributions in binary form must reproduce the above copyright | |
87 | * notice, this list of conditions and the following disclaimer in the | |
88 | * documentation and/or other materials provided with the distribution. | |
89 | * 3. All advertising materials mentioning features or use of this software | |
90 | * must display the following acknowledgement: | |
91 | * "This product includes cryptographic software written by | |
92 | * Eric Young (eay@cryptsoft.com)" | |
93 | * The word 'cryptographic' can be left out if the rouines from the library | |
94 | * being used are not cryptographic related :-). | |
95 | * 4. If you include any Windows specific code (or a derivative thereof) from | |
96 | * the apps directory (application code) you must include an acknowledgement: | |
97 | * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)" | |
98 | * | |
99 | * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND | |
100 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | |
101 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | |
102 | * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE | |
103 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL | |
104 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS | |
105 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
106 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT | |
107 | * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY | |
108 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
109 | * SUCH DAMAGE. | |
110 | * | |
111 | * The licence and distribution terms for any publically available version or | |
112 | * derivative of this code cannot be changed. i.e. this code cannot simply be | |
113 | * copied and put under another distribution licence | |
114 | * [including the GNU Public Licence.] | |
115 | */ | |
116 | ||
117 | ||
7babdf20 | 118 | #include <openssl/e_os2.h> |
02790299 | 119 | |
6df617a5 AP |
120 | /* need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc |
121 | * [maybe others?], because it masks interfaces not discussed in standard, | |
122 | * sigaction and fileno included. -pedantic would be more appropriate for | |
123 | * the intended purposes, but we can't prevent users from adding -ansi. | |
124 | */ | |
83d8fa7d DSH |
125 | #ifndef _POSIX_C_SOURCE |
126 | #define _POSIX_C_SOURCE 2 | |
127 | #endif | |
2ee67f1d RL |
128 | #include <signal.h> |
129 | #include <stdio.h> | |
2ee67f1d RL |
130 | #include <string.h> |
131 | #include <errno.h> | |
132 | ||
02790299 RL |
133 | #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) |
134 | # ifdef OPENSSL_UNISTD | |
135 | # include OPENSSL_UNISTD | |
136 | # else | |
137 | # include <unistd.h> | |
138 | # endif | |
a63d5eaa RL |
139 | /* If unistd.h defines _POSIX_VERSION, we conclude that we |
140 | * are on a POSIX system and have sigaction and termios. */ | |
02790299 | 141 | # if defined(_POSIX_VERSION) |
a63d5eaa | 142 | |
02790299 RL |
143 | # define SIGACTION |
144 | # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) | |
145 | # define TERMIOS | |
146 | # endif | |
a63d5eaa | 147 | |
02790299 | 148 | # endif |
a63d5eaa | 149 | #endif |
a63d5eaa RL |
150 | |
151 | #ifdef WIN16TTY | |
02790299 RL |
152 | # undef OPENSSL_SYS_WIN16 |
153 | # undef WIN16 | |
154 | # undef _WINDOWS | |
155 | # include <graph.h> | |
a63d5eaa RL |
156 | #endif |
157 | ||
158 | /* 06-Apr-92 Luke Brennan Support for VMS */ | |
159 | #include "ui_locl.h" | |
160 | #include "cryptlib.h" | |
a63d5eaa | 161 | |
02790299 RL |
162 | #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */ |
163 | # include <starlet.h> | |
164 | # ifdef __DECC | |
165 | # pragma message disable DOLLARID | |
166 | # endif | |
a63d5eaa RL |
167 | #endif |
168 | ||
169 | #ifdef WIN_CONSOLE_BUG | |
02790299 | 170 | # include <windows.h> |
0bf23d9b | 171 | #ifndef OPENSSL_SYS_WINCE |
02790299 | 172 | # include <wincon.h> |
a63d5eaa | 173 | #endif |
0bf23d9b | 174 | #endif |
a63d5eaa RL |
175 | |
176 | ||
177 | /* There are 5 types of terminal interface supported, | |
178 | * TERMIO, TERMIOS, VMS, MSDOS and SGTTY | |
179 | */ | |
180 | ||
181 | #if defined(__sgi) && !defined(TERMIOS) | |
02790299 RL |
182 | # define TERMIOS |
183 | # undef TERMIO | |
184 | # undef SGTTY | |
a63d5eaa RL |
185 | #endif |
186 | ||
187 | #if defined(linux) && !defined(TERMIO) | |
02790299 RL |
188 | # undef TERMIOS |
189 | # define TERMIO | |
190 | # undef SGTTY | |
a63d5eaa RL |
191 | #endif |
192 | ||
193 | #ifdef _LIBC | |
02790299 RL |
194 | # undef TERMIOS |
195 | # define TERMIO | |
196 | # undef SGTTY | |
a63d5eaa RL |
197 | #endif |
198 | ||
02790299 RL |
199 | #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE) |
200 | # undef TERMIOS | |
201 | # undef TERMIO | |
202 | # define SGTTY | |
a63d5eaa RL |
203 | #endif |
204 | ||
6a89a25c | 205 | #if defined(OPENSSL_SYS_VXWORKS) |
3e83e686 RL |
206 | #undef TERMIOS |
207 | #undef TERMIO | |
208 | #undef SGTTY | |
209 | #endif | |
210 | ||
4d8743f4 RL |
211 | #if defined(OPENSSL_SYS_NETWARE) |
212 | #undef TERMIOS | |
213 | #undef TERMIO | |
214 | #undef SGTTY | |
215 | #endif | |
216 | ||
a63d5eaa | 217 | #ifdef TERMIOS |
02790299 RL |
218 | # include <termios.h> |
219 | # define TTY_STRUCT struct termios | |
220 | # define TTY_FLAGS c_lflag | |
221 | # define TTY_get(tty,data) tcgetattr(tty,data) | |
222 | # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data) | |
a63d5eaa RL |
223 | #endif |
224 | ||
225 | #ifdef TERMIO | |
02790299 RL |
226 | # include <termio.h> |
227 | # define TTY_STRUCT struct termio | |
228 | # define TTY_FLAGS c_lflag | |
229 | # define TTY_get(tty,data) ioctl(tty,TCGETA,data) | |
230 | # define TTY_set(tty,data) ioctl(tty,TCSETA,data) | |
a63d5eaa RL |
231 | #endif |
232 | ||
233 | #ifdef SGTTY | |
02790299 RL |
234 | # include <sgtty.h> |
235 | # define TTY_STRUCT struct sgttyb | |
236 | # define TTY_FLAGS sg_flags | |
237 | # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data) | |
238 | # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data) | |
a63d5eaa RL |
239 | #endif |
240 | ||
0709385d | 241 | #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS) |
02790299 | 242 | # include <sys/ioctl.h> |
a63d5eaa RL |
243 | #endif |
244 | ||
02790299 RL |
245 | #ifdef OPENSSL_SYS_MSDOS |
246 | # include <conio.h> | |
a63d5eaa RL |
247 | #endif |
248 | ||
02790299 RL |
249 | #ifdef OPENSSL_SYS_VMS |
250 | # include <ssdef.h> | |
251 | # include <iodef.h> | |
252 | # include <ttdef.h> | |
253 | # include <descrip.h> | |
a63d5eaa RL |
254 | struct IOSB { |
255 | short iosb$w_value; | |
256 | short iosb$w_count; | |
257 | long iosb$l_info; | |
258 | }; | |
259 | #endif | |
260 | ||
0709385d RL |
261 | #ifdef OPENSSL_SYS_SUNOS |
262 | typedef int sig_atomic_t; | |
263 | #endif | |
264 | ||
4d8743f4 | 265 | #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE) |
a63d5eaa RL |
266 | /* |
267 | * This one needs work. As a matter of fact the code is unoperational | |
268 | * and this is only a trick to get it compiled. | |
269 | * <appro@fy.chalmers.se> | |
270 | */ | |
02790299 | 271 | # define TTY_STRUCT int |
a63d5eaa RL |
272 | #endif |
273 | ||
274 | #ifndef NX509_SIG | |
02790299 | 275 | # define NX509_SIG 32 |
a63d5eaa RL |
276 | #endif |
277 | ||
278 | ||
279 | /* Define globals. They are protected by a lock */ | |
280 | #ifdef SIGACTION | |
281 | static struct sigaction savsig[NX509_SIG]; | |
282 | #else | |
283 | static void (*savsig[NX509_SIG])(int ); | |
284 | #endif | |
a63d5eaa | 285 | |
02790299 | 286 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
287 | static struct IOSB iosb; |
288 | static $DESCRIPTOR(terminal,"TT"); | |
200bc9e3 | 289 | static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this will always suffice for the actual structures? */ |
a63d5eaa RL |
290 | static long status; |
291 | static unsigned short channel = 0; | |
292 | #else | |
451dc18f | 293 | #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__) |
a63d5eaa RL |
294 | static TTY_STRUCT tty_orig,tty_new; |
295 | #endif | |
296 | #endif | |
200bc9e3 | 297 | static FILE *tty_in, *tty_out; |
a63d5eaa RL |
298 | static int is_a_tty; |
299 | ||
300 | /* Declare static functions */ | |
0bf23d9b | 301 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
0e039aa7 | 302 | static int read_till_nl(FILE *); |
a63d5eaa RL |
303 | static void recsig(int); |
304 | static void pushsig(void); | |
305 | static void popsig(void); | |
0bf23d9b | 306 | #endif |
29fb08c2 | 307 | #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) |
a63d5eaa RL |
308 | static int noecho_fgets(char *buf, int size, FILE *tty); |
309 | #endif | |
b589977b | 310 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl); |
a63d5eaa RL |
311 | |
312 | static int read_string(UI *ui, UI_STRING *uis); | |
9ad0f681 | 313 | static int write_string(UI *ui, UI_STRING *uis); |
a63d5eaa RL |
314 | |
315 | static int open_console(UI *ui); | |
316 | static int echo_console(UI *ui); | |
317 | static int noecho_console(UI *ui); | |
318 | static int close_console(UI *ui); | |
319 | ||
320 | static UI_METHOD ui_openssl = | |
321 | { | |
322 | "OpenSSL default user interface", | |
323 | open_console, | |
9ad0f681 RL |
324 | write_string, |
325 | NULL, /* No flusher is needed for command lines */ | |
a63d5eaa | 326 | read_string, |
a63d5eaa | 327 | close_console, |
9ad0f681 | 328 | NULL |
a63d5eaa RL |
329 | }; |
330 | ||
331 | /* The method with all the built-in thingies */ | |
332 | UI_METHOD *UI_OpenSSL(void) | |
333 | { | |
334 | return &ui_openssl; | |
335 | } | |
336 | ||
9ad0f681 RL |
337 | /* The following function makes sure that info and error strings are printed |
338 | before any prompt. */ | |
339 | static int write_string(UI *ui, UI_STRING *uis) | |
a63d5eaa RL |
340 | { |
341 | switch (UI_get_string_type(uis)) | |
342 | { | |
b589977b RL |
343 | case UIT_ERROR: |
344 | case UIT_INFO: | |
9ad0f681 | 345 | fputs(UI_get0_output_string(uis), tty_out); |
200bc9e3 | 346 | fflush(tty_out); |
a63d5eaa | 347 | break; |
b589977b RL |
348 | default: |
349 | break; | |
9ad0f681 RL |
350 | } |
351 | return 1; | |
352 | } | |
353 | ||
354 | static int read_string(UI *ui, UI_STRING *uis) | |
355 | { | |
356 | int ok = 0; | |
357 | ||
358 | switch (UI_get_string_type(uis)) | |
359 | { | |
b589977b RL |
360 | case UIT_BOOLEAN: |
361 | fputs(UI_get0_output_string(uis), tty_out); | |
362 | fputs(UI_get0_action_string(uis), tty_out); | |
363 | fflush(tty_out); | |
364 | return read_string_inner(ui, uis, | |
365 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0); | |
9ad0f681 RL |
366 | case UIT_PROMPT: |
367 | fputs(UI_get0_output_string(uis), tty_out); | |
368 | fflush(tty_out); | |
369 | return read_string_inner(ui, uis, | |
b589977b | 370 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1); |
9ad0f681 | 371 | case UIT_VERIFY: |
200bc9e3 | 372 | fprintf(tty_out,"Verifying - %s", |
a63d5eaa | 373 | UI_get0_output_string(uis)); |
200bc9e3 | 374 | fflush(tty_out); |
9ad0f681 | 375 | if ((ok = read_string_inner(ui, uis, |
b589977b | 376 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0) |
9ad0f681 | 377 | return ok; |
a63d5eaa RL |
378 | if (strcmp(UI_get0_result_string(uis), |
379 | UI_get0_test_string(uis)) != 0) | |
380 | { | |
200bc9e3 BM |
381 | fprintf(tty_out,"Verify failure\n"); |
382 | fflush(tty_out); | |
a63d5eaa RL |
383 | return 0; |
384 | } | |
385 | break; | |
a63d5eaa | 386 | default: |
a63d5eaa RL |
387 | break; |
388 | } | |
389 | return 1; | |
390 | } | |
391 | ||
392 | ||
0bf23d9b | 393 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa | 394 | /* Internal functions to read a string without echoing */ |
0e039aa7 | 395 | static int read_till_nl(FILE *in) |
a63d5eaa RL |
396 | { |
397 | #define SIZE 4 | |
398 | char buf[SIZE+1]; | |
399 | ||
400 | do { | |
0e039aa7 DSH |
401 | if (!fgets(buf,SIZE,in)) |
402 | return 0; | |
a63d5eaa | 403 | } while (strchr(buf,'\n') == NULL); |
0e039aa7 | 404 | return 1; |
a63d5eaa RL |
405 | } |
406 | ||
94960c84 | 407 | static volatile sig_atomic_t intr_signal; |
0bf23d9b | 408 | #endif |
7c517a04 | 409 | |
b589977b | 410 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl) |
a63d5eaa RL |
411 | { |
412 | static int ps; | |
413 | int ok; | |
b589977b | 414 | char result[BUFSIZ]; |
a63d5eaa | 415 | int maxsize = BUFSIZ-1; |
0bf23d9b | 416 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
b589977b | 417 | char *p; |
a63d5eaa | 418 | |
7c517a04 | 419 | intr_signal=0; |
a63d5eaa RL |
420 | ok=0; |
421 | ps=0; | |
422 | ||
423 | pushsig(); | |
424 | ps=1; | |
425 | ||
7c517a04 BL |
426 | if (!echo && !noecho_console(ui)) |
427 | goto error; | |
a63d5eaa RL |
428 | ps=2; |
429 | ||
b589977b | 430 | result[0]='\0'; |
02790299 | 431 | #ifdef OPENSSL_SYS_MSDOS |
b589977b | 432 | if (!echo) |
7c517a04 | 433 | { |
b589977b | 434 | noecho_fgets(result,maxsize,tty_in); |
7c517a04 BL |
435 | p=result; /* FIXME: noecho_fgets doesn't return errors */ |
436 | } | |
b589977b | 437 | else |
7c517a04 | 438 | p=fgets(result,maxsize,tty_in); |
b589977b | 439 | #else |
7c517a04 | 440 | p=fgets(result,maxsize,tty_in); |
02790299 | 441 | #endif |
7c517a04 BL |
442 | if(!p) |
443 | goto error; | |
b589977b RL |
444 | if (feof(tty_in)) goto error; |
445 | if (ferror(tty_in)) goto error; | |
446 | if ((p=(char *)strchr(result,'\n')) != NULL) | |
447 | { | |
448 | if (strip_nl) | |
a63d5eaa | 449 | *p='\0'; |
a63d5eaa | 450 | } |
b589977b | 451 | else |
0e039aa7 DSH |
452 | if (!read_till_nl(tty_in)) |
453 | goto error; | |
b589977b RL |
454 | if (UI_set_result(ui, uis, result) >= 0) |
455 | ok=1; | |
a63d5eaa RL |
456 | |
457 | error: | |
7c517a04 BL |
458 | if (intr_signal == SIGINT) |
459 | ok=-1; | |
200bc9e3 | 460 | if (!echo) fprintf(tty_out,"\n"); |
7c517a04 BL |
461 | if (ps >= 2 && !echo && !echo_console(ui)) |
462 | ok=0; | |
a63d5eaa RL |
463 | |
464 | if (ps >= 1) | |
465 | popsig(); | |
466 | #else | |
a63d5eaa RL |
467 | ok=1; |
468 | #endif | |
469 | ||
4579924b | 470 | OPENSSL_cleanse(result,BUFSIZ); |
a63d5eaa RL |
471 | return ok; |
472 | } | |
473 | ||
474 | ||
475 | /* Internal functions to open, handle and close a channel to the console. */ | |
476 | static int open_console(UI *ui) | |
477 | { | |
478 | CRYPTO_w_lock(CRYPTO_LOCK_UI); | |
479 | is_a_tty = 1; | |
480 | ||
4700aea9 | 481 | #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS) |
200bc9e3 BM |
482 | tty_in=stdin; |
483 | tty_out=stderr; | |
a63d5eaa | 484 | #else |
508f15cd BM |
485 | # ifdef OPENSSL_SYS_MSDOS |
486 | # define DEV_TTY "con" | |
487 | # else | |
488 | # define DEV_TTY "/dev/tty" | |
489 | # endif | |
490 | if ((tty_in=fopen(DEV_TTY,"r")) == NULL) | |
200bc9e3 | 491 | tty_in=stdin; |
508f15cd | 492 | if ((tty_out=fopen(DEV_TTY,"w")) == NULL) |
200bc9e3 | 493 | tty_out=stderr; |
a63d5eaa RL |
494 | #endif |
495 | ||
c5597592 | 496 | #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) |
2ee67f1d | 497 | if (TTY_get(fileno(tty_in),&tty_orig) == -1) |
a63d5eaa RL |
498 | { |
499 | #ifdef ENOTTY | |
500 | if (errno == ENOTTY) | |
501 | is_a_tty=0; | |
502 | else | |
503 | #endif | |
504 | #ifdef EINVAL | |
505 | /* Ariel Glenn ariel@columbia.edu reports that solaris | |
506 | * can return EINVAL instead. This should be ok */ | |
507 | if (errno == EINVAL) | |
508 | is_a_tty=0; | |
509 | else | |
510 | #endif | |
511 | return 0; | |
512 | } | |
513 | #endif | |
02790299 | 514 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
515 | status = sys$assign(&terminal,&channel,0,0); |
516 | if (status != SS$_NORMAL) | |
517 | return 0; | |
518 | status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0); | |
519 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
520 | return 0; | |
521 | #endif | |
522 | return 1; | |
523 | } | |
524 | ||
525 | static int noecho_console(UI *ui) | |
526 | { | |
527 | #ifdef TTY_FLAGS | |
528 | memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); | |
529 | tty_new.TTY_FLAGS &= ~ECHO; | |
530 | #endif | |
531 | ||
02790299 | 532 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
200bc9e3 | 533 | if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) |
a63d5eaa RL |
534 | return 0; |
535 | #endif | |
02790299 | 536 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
537 | tty_new[0] = tty_orig[0]; |
538 | tty_new[1] = tty_orig[1] | TT$M_NOECHO; | |
539 | tty_new[2] = tty_orig[2]; | |
540 | status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0); | |
541 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
542 | return 0; | |
543 | #endif | |
544 | return 1; | |
545 | } | |
546 | ||
547 | static int echo_console(UI *ui) | |
548 | { | |
02790299 | 549 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
a63d5eaa RL |
550 | memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); |
551 | tty_new.TTY_FLAGS |= ECHO; | |
552 | #endif | |
553 | ||
02790299 | 554 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
200bc9e3 | 555 | if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) |
a63d5eaa RL |
556 | return 0; |
557 | #endif | |
02790299 | 558 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
559 | tty_new[0] = tty_orig[0]; |
560 | tty_new[1] = tty_orig[1] & ~TT$M_NOECHO; | |
561 | tty_new[2] = tty_orig[2]; | |
562 | status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0); | |
563 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
564 | return 0; | |
565 | #endif | |
566 | return 1; | |
567 | } | |
568 | ||
569 | static int close_console(UI *ui) | |
570 | { | |
caa7b2ad | 571 | if (tty_in != stdin) fclose(tty_in); |
200bc9e3 | 572 | if (tty_out != stderr) fclose(tty_out); |
02790299 | 573 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
574 | status = sys$dassgn(channel); |
575 | #endif | |
576 | CRYPTO_w_unlock(CRYPTO_LOCK_UI); | |
577 | ||
578 | return 1; | |
579 | } | |
580 | ||
581 | ||
0bf23d9b | 582 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
583 | /* Internal functions to handle signals and act on them */ |
584 | static void pushsig(void) | |
585 | { | |
bfa4b8c5 | 586 | #ifndef OPENSSL_SYS_WIN32 |
a63d5eaa | 587 | int i; |
bfa4b8c5 | 588 | #endif |
a63d5eaa RL |
589 | #ifdef SIGACTION |
590 | struct sigaction sa; | |
591 | ||
592 | memset(&sa,0,sizeof sa); | |
593 | sa.sa_handler=recsig; | |
594 | #endif | |
595 | ||
6d00101e AP |
596 | #ifdef OPENSSL_SYS_WIN32 |
597 | savsig[SIGABRT]=signal(SIGABRT,recsig); | |
598 | savsig[SIGFPE]=signal(SIGFPE,recsig); | |
599 | savsig[SIGILL]=signal(SIGILL,recsig); | |
600 | savsig[SIGINT]=signal(SIGINT,recsig); | |
601 | savsig[SIGSEGV]=signal(SIGSEGV,recsig); | |
602 | savsig[SIGTERM]=signal(SIGTERM,recsig); | |
603 | #else | |
a63d5eaa RL |
604 | for (i=1; i<NX509_SIG; i++) |
605 | { | |
606 | #ifdef SIGUSR1 | |
607 | if (i == SIGUSR1) | |
608 | continue; | |
609 | #endif | |
610 | #ifdef SIGUSR2 | |
611 | if (i == SIGUSR2) | |
612 | continue; | |
613 | #endif | |
614 | #ifdef SIGKILL | |
615 | if (i == SIGKILL) /* We can't make any action on that. */ | |
616 | continue; | |
617 | #endif | |
618 | #ifdef SIGACTION | |
619 | sigaction(i,&sa,&savsig[i]); | |
620 | #else | |
621 | savsig[i]=signal(i,recsig); | |
622 | #endif | |
623 | } | |
6d00101e | 624 | #endif |
a63d5eaa RL |
625 | |
626 | #ifdef SIGWINCH | |
627 | signal(SIGWINCH,SIG_DFL); | |
628 | #endif | |
629 | } | |
630 | ||
631 | static void popsig(void) | |
632 | { | |
6d00101e AP |
633 | #ifdef OPENSSL_SYS_WIN32 |
634 | signal(SIGABRT,savsig[SIGABRT]); | |
635 | signal(SIGFPE,savsig[SIGFPE]); | |
636 | signal(SIGILL,savsig[SIGILL]); | |
637 | signal(SIGINT,savsig[SIGINT]); | |
638 | signal(SIGSEGV,savsig[SIGSEGV]); | |
639 | signal(SIGTERM,savsig[SIGTERM]); | |
640 | #else | |
bfa4b8c5 | 641 | int i; |
a63d5eaa RL |
642 | for (i=1; i<NX509_SIG; i++) |
643 | { | |
644 | #ifdef SIGUSR1 | |
645 | if (i == SIGUSR1) | |
646 | continue; | |
647 | #endif | |
648 | #ifdef SIGUSR2 | |
649 | if (i == SIGUSR2) | |
650 | continue; | |
651 | #endif | |
652 | #ifdef SIGACTION | |
653 | sigaction(i,&savsig[i],NULL); | |
654 | #else | |
655 | signal(i,savsig[i]); | |
656 | #endif | |
657 | } | |
6d00101e | 658 | #endif |
a63d5eaa RL |
659 | } |
660 | ||
661 | static void recsig(int i) | |
662 | { | |
7c517a04 | 663 | intr_signal=i; |
a63d5eaa | 664 | } |
0bf23d9b | 665 | #endif |
a63d5eaa | 666 | |
a63d5eaa | 667 | /* Internal functions specific for Windows */ |
0bf23d9b | 668 | #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
669 | static int noecho_fgets(char *buf, int size, FILE *tty) |
670 | { | |
671 | int i; | |
672 | char *p; | |
673 | ||
674 | p=buf; | |
675 | for (;;) | |
676 | { | |
677 | if (size == 0) | |
678 | { | |
679 | *p='\0'; | |
680 | break; | |
681 | } | |
682 | size--; | |
683 | #ifdef WIN16TTY | |
684 | i=_inchar(); | |
e527201f AP |
685 | #elif defined(_WIN32) |
686 | i=_getch(); | |
a63d5eaa RL |
687 | #else |
688 | i=getch(); | |
689 | #endif | |
690 | if (i == '\r') i='\n'; | |
691 | *(p++)=i; | |
692 | if (i == '\n') | |
693 | { | |
694 | *p='\0'; | |
695 | break; | |
696 | } | |
697 | } | |
698 | #ifdef WIN_CONSOLE_BUG | |
699 | /* Win95 has several evil console bugs: one of these is that the | |
700 | * last character read using getch() is passed to the next read: this is | |
701 | * usually a CR so this can be trouble. No STDIO fix seems to work but | |
702 | * flushing the console appears to do the trick. | |
703 | */ | |
704 | { | |
705 | HANDLE inh; | |
706 | inh = GetStdHandle(STD_INPUT_HANDLE); | |
707 | FlushConsoleInputBuffer(inh); | |
708 | } | |
709 | #endif | |
710 | return(strlen(buf)); | |
711 | } | |
712 | #endif |