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