]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright 2001-2020 The OpenSSL Project Authors. All Rights Reserved. | |
3 | * | |
4 | * Licensed under the Apache License 2.0 (the "License"). You may not use | |
5 | * this file except in compliance with the License. You can obtain a copy | |
6 | * in the file LICENSE in the source distribution or at | |
7 | * https://www.openssl.org/source/license.html | |
8 | */ | |
9 | ||
10 | #include "e_os.h" | |
11 | #include <openssl/e_os2.h> | |
12 | #include <openssl/err.h> | |
13 | #include <openssl/ui.h> | |
14 | ||
15 | #ifndef OPENSSL_NO_UI_CONSOLE | |
16 | /* | |
17 | * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc | |
18 | * [maybe others?], because it masks interfaces not discussed in standard, | |
19 | * sigaction and fileno included. -pedantic would be more appropriate for the | |
20 | * intended purposes, but we can't prevent users from adding -ansi. | |
21 | */ | |
22 | # if defined(OPENSSL_SYS_VXWORKS) | |
23 | # include <sys/types.h> | |
24 | # endif | |
25 | ||
26 | # if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS) | |
27 | # ifndef _POSIX_C_SOURCE | |
28 | # define _POSIX_C_SOURCE 2 | |
29 | # endif | |
30 | # endif | |
31 | # include <signal.h> | |
32 | # include <stdio.h> | |
33 | # include <string.h> | |
34 | # include <errno.h> | |
35 | ||
36 | # if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) | |
37 | # include <unistd.h> | |
38 | /* | |
39 | * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX | |
40 | * system and have sigaction and termios. | |
41 | */ | |
42 | # if defined(_POSIX_VERSION) && _POSIX_VERSION>=199309L | |
43 | ||
44 | # define SIGACTION | |
45 | # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) | |
46 | # define TERMIOS | |
47 | # endif | |
48 | ||
49 | # endif | |
50 | # endif | |
51 | ||
52 | # include "ui_local.h" | |
53 | # include "internal/cryptlib.h" | |
54 | ||
55 | # ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */ | |
56 | # include <starlet.h> | |
57 | # ifdef __DECC | |
58 | # pragma message disable DOLLARID | |
59 | # endif | |
60 | # endif | |
61 | ||
62 | # ifdef WIN_CONSOLE_BUG | |
63 | # include <windows.h> | |
64 | # ifndef OPENSSL_SYS_WINCE | |
65 | # include <wincon.h> | |
66 | # endif | |
67 | # endif | |
68 | ||
69 | /* | |
70 | * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS, | |
71 | * MSDOS, WIN32 Console and SGTTY. | |
72 | * | |
73 | * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will | |
74 | * remain respected. Otherwise, we default to TERMIOS except for a few | |
75 | * systems that require something different. | |
76 | * | |
77 | * Note: we do not use SGTTY unless it's defined by the configuration. We | |
78 | * may eventually opt to remove its use entirely. | |
79 | */ | |
80 | ||
81 | # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) | |
82 | ||
83 | # if defined(_LIBC) | |
84 | # undef TERMIOS | |
85 | # define TERMIO | |
86 | # undef SGTTY | |
87 | /* | |
88 | * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms. | |
89 | */ | |
90 | # elif !defined(OPENSSL_SYS_VMS) \ | |
91 | && !defined(OPENSSL_SYS_MSDOS) \ | |
92 | && !defined(OPENSSL_SYS_VXWORKS) | |
93 | # define TERMIOS | |
94 | # undef TERMIO | |
95 | # undef SGTTY | |
96 | # endif | |
97 | ||
98 | # endif | |
99 | ||
100 | # if defined(OPENSSL_SYS_VXWORKS) | |
101 | # undef TERMIOS | |
102 | # undef TERMIO | |
103 | # undef SGTTY | |
104 | # endif | |
105 | ||
106 | # ifdef TERMIOS | |
107 | # include <termios.h> | |
108 | # define TTY_STRUCT struct termios | |
109 | # define TTY_FLAGS c_lflag | |
110 | # define TTY_get(tty,data) tcgetattr(tty,data) | |
111 | # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data) | |
112 | # endif | |
113 | ||
114 | # ifdef TERMIO | |
115 | # include <termio.h> | |
116 | # define TTY_STRUCT struct termio | |
117 | # define TTY_FLAGS c_lflag | |
118 | # define TTY_get(tty,data) ioctl(tty,TCGETA,data) | |
119 | # define TTY_set(tty,data) ioctl(tty,TCSETA,data) | |
120 | # endif | |
121 | ||
122 | # ifdef SGTTY | |
123 | # include <sgtty.h> | |
124 | # define TTY_STRUCT struct sgttyb | |
125 | # define TTY_FLAGS sg_flags | |
126 | # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data) | |
127 | # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data) | |
128 | # endif | |
129 | ||
130 | # if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) | |
131 | # include <sys/ioctl.h> | |
132 | # endif | |
133 | ||
134 | # ifdef OPENSSL_SYS_MSDOS | |
135 | # include <conio.h> | |
136 | # endif | |
137 | ||
138 | # ifdef OPENSSL_SYS_VMS | |
139 | # include <ssdef.h> | |
140 | # include <iodef.h> | |
141 | # include <ttdef.h> | |
142 | # include <descrip.h> | |
143 | struct IOSB { | |
144 | short iosb$w_value; | |
145 | short iosb$w_count; | |
146 | long iosb$l_info; | |
147 | }; | |
148 | # endif | |
149 | ||
150 | # ifndef NX509_SIG | |
151 | # define NX509_SIG 32 | |
152 | # endif | |
153 | ||
154 | /* Define globals. They are protected by a lock */ | |
155 | # ifdef SIGACTION | |
156 | static struct sigaction savsig[NX509_SIG]; | |
157 | # else | |
158 | static void (*savsig[NX509_SIG]) (int); | |
159 | # endif | |
160 | ||
161 | # ifdef OPENSSL_SYS_VMS | |
162 | static struct IOSB iosb; | |
163 | static $DESCRIPTOR(terminal, "TT"); | |
164 | static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this | |
165 | * will always suffice for the actual | |
166 | * structures? */ | |
167 | static long status; | |
168 | static unsigned short channel = 0; | |
169 | # elif defined(_WIN32) && !defined(_WIN32_WCE) | |
170 | static DWORD tty_orig, tty_new; | |
171 | # else | |
172 | # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__) | |
173 | static TTY_STRUCT tty_orig, tty_new; | |
174 | # endif | |
175 | # endif | |
176 | static FILE *tty_in, *tty_out; | |
177 | static int is_a_tty; | |
178 | ||
179 | /* Declare static functions */ | |
180 | # if !defined(OPENSSL_SYS_WINCE) | |
181 | static int read_till_nl(FILE *); | |
182 | static void recsig(int); | |
183 | static void pushsig(void); | |
184 | static void popsig(void); | |
185 | # endif | |
186 | # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) | |
187 | static int noecho_fgets(char *buf, int size, FILE *tty); | |
188 | # endif | |
189 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl); | |
190 | ||
191 | static int read_string(UI *ui, UI_STRING *uis); | |
192 | static int write_string(UI *ui, UI_STRING *uis); | |
193 | ||
194 | static int open_console(UI *ui); | |
195 | static int echo_console(UI *ui); | |
196 | static int noecho_console(UI *ui); | |
197 | static int close_console(UI *ui); | |
198 | ||
199 | /* | |
200 | * The following function makes sure that info and error strings are printed | |
201 | * before any prompt. | |
202 | */ | |
203 | static int write_string(UI *ui, UI_STRING *uis) | |
204 | { | |
205 | switch (UI_get_string_type(uis)) { | |
206 | case UIT_ERROR: | |
207 | case UIT_INFO: | |
208 | fputs(UI_get0_output_string(uis), tty_out); | |
209 | fflush(tty_out); | |
210 | break; | |
211 | case UIT_NONE: | |
212 | case UIT_PROMPT: | |
213 | case UIT_VERIFY: | |
214 | case UIT_BOOLEAN: | |
215 | break; | |
216 | } | |
217 | return 1; | |
218 | } | |
219 | ||
220 | static int read_string(UI *ui, UI_STRING *uis) | |
221 | { | |
222 | int ok = 0; | |
223 | ||
224 | switch (UI_get_string_type(uis)) { | |
225 | case UIT_BOOLEAN: | |
226 | fputs(UI_get0_output_string(uis), tty_out); | |
227 | fputs(UI_get0_action_string(uis), tty_out); | |
228 | fflush(tty_out); | |
229 | return read_string_inner(ui, uis, | |
230 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, | |
231 | 0); | |
232 | case UIT_PROMPT: | |
233 | fputs(UI_get0_output_string(uis), tty_out); | |
234 | fflush(tty_out); | |
235 | return read_string_inner(ui, uis, | |
236 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, | |
237 | 1); | |
238 | case UIT_VERIFY: | |
239 | fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis)); | |
240 | fflush(tty_out); | |
241 | if ((ok = read_string_inner(ui, uis, | |
242 | UI_get_input_flags(uis) & | |
243 | UI_INPUT_FLAG_ECHO, 1)) <= 0) | |
244 | return ok; | |
245 | if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) { | |
246 | fprintf(tty_out, "Verify failure\n"); | |
247 | fflush(tty_out); | |
248 | return 0; | |
249 | } | |
250 | break; | |
251 | case UIT_NONE: | |
252 | case UIT_INFO: | |
253 | case UIT_ERROR: | |
254 | break; | |
255 | } | |
256 | return 1; | |
257 | } | |
258 | ||
259 | # if !defined(OPENSSL_SYS_WINCE) | |
260 | /* Internal functions to read a string without echoing */ | |
261 | static int read_till_nl(FILE *in) | |
262 | { | |
263 | # define SIZE 4 | |
264 | char buf[SIZE + 1]; | |
265 | ||
266 | do { | |
267 | if (!fgets(buf, SIZE, in)) | |
268 | return 0; | |
269 | } while (strchr(buf, '\n') == NULL); | |
270 | return 1; | |
271 | } | |
272 | ||
273 | static volatile sig_atomic_t intr_signal; | |
274 | # endif | |
275 | ||
276 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl) | |
277 | { | |
278 | static int ps; | |
279 | int ok; | |
280 | char result[BUFSIZ]; | |
281 | int maxsize = BUFSIZ - 1; | |
282 | # if !defined(OPENSSL_SYS_WINCE) | |
283 | char *p = NULL; | |
284 | int echo_eol = !echo; | |
285 | ||
286 | intr_signal = 0; | |
287 | ok = 0; | |
288 | ps = 0; | |
289 | ||
290 | pushsig(); | |
291 | ps = 1; | |
292 | ||
293 | if (!echo && !noecho_console(ui)) | |
294 | goto error; | |
295 | ps = 2; | |
296 | ||
297 | result[0] = '\0'; | |
298 | # if defined(_WIN32) | |
299 | if (is_a_tty) { | |
300 | DWORD numread; | |
301 | # if defined(CP_UTF8) | |
302 | if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) { | |
303 | WCHAR wresult[BUFSIZ]; | |
304 | ||
305 | if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE), | |
306 | wresult, maxsize, &numread, NULL)) { | |
307 | if (numread >= 2 && | |
308 | wresult[numread-2] == L'\r' && | |
309 | wresult[numread-1] == L'\n') { | |
310 | wresult[numread-2] = L'\n'; | |
311 | numread--; | |
312 | } | |
313 | wresult[numread] = '\0'; | |
314 | if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1, | |
315 | result, sizeof(result), NULL, 0) > 0) | |
316 | p = result; | |
317 | ||
318 | OPENSSL_cleanse(wresult, sizeof(wresult)); | |
319 | } | |
320 | } else | |
321 | # endif | |
322 | if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE), | |
323 | result, maxsize, &numread, NULL)) { | |
324 | if (numread >= 2 && | |
325 | result[numread-2] == '\r' && result[numread-1] == '\n') { | |
326 | result[numread-2] = '\n'; | |
327 | numread--; | |
328 | } | |
329 | result[numread] = '\0'; | |
330 | p = result; | |
331 | } | |
332 | } else | |
333 | # elif defined(OPENSSL_SYS_MSDOS) | |
334 | if (!echo) { | |
335 | noecho_fgets(result, maxsize, tty_in); | |
336 | p = result; /* FIXME: noecho_fgets doesn't return errors */ | |
337 | } else | |
338 | # endif | |
339 | p = fgets(result, maxsize, tty_in); | |
340 | if (p == NULL) | |
341 | goto error; | |
342 | if (feof(tty_in)) | |
343 | goto error; | |
344 | if (ferror(tty_in)) | |
345 | goto error; | |
346 | if ((p = (char *)strchr(result, '\n')) != NULL) { | |
347 | if (strip_nl) | |
348 | *p = '\0'; | |
349 | } else if (!read_till_nl(tty_in)) | |
350 | goto error; | |
351 | if (UI_set_result(ui, uis, result) >= 0) | |
352 | ok = 1; | |
353 | ||
354 | error: | |
355 | if (intr_signal == SIGINT) | |
356 | ok = -1; | |
357 | if (echo_eol) | |
358 | fprintf(tty_out, "\n"); | |
359 | if (ps >= 2 && !echo && !echo_console(ui)) | |
360 | ok = 0; | |
361 | ||
362 | if (ps >= 1) | |
363 | popsig(); | |
364 | # else | |
365 | ok = 1; | |
366 | # endif | |
367 | ||
368 | OPENSSL_cleanse(result, BUFSIZ); | |
369 | return ok; | |
370 | } | |
371 | ||
372 | /* Internal functions to open, handle and close a channel to the console. */ | |
373 | static int open_console(UI *ui) | |
374 | { | |
375 | CRYPTO_THREAD_write_lock(ui->lock); | |
376 | is_a_tty = 1; | |
377 | ||
378 | # if defined(OPENSSL_SYS_VXWORKS) | |
379 | tty_in = stdin; | |
380 | tty_out = stderr; | |
381 | # elif defined(_WIN32) && !defined(_WIN32_WCE) | |
382 | if ((tty_out = fopen("conout$", "w")) == NULL) | |
383 | tty_out = stderr; | |
384 | ||
385 | if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) { | |
386 | tty_in = stdin; | |
387 | } else { | |
388 | is_a_tty = 0; | |
389 | if ((tty_in = fopen("conin$", "r")) == NULL) | |
390 | tty_in = stdin; | |
391 | } | |
392 | # else | |
393 | # ifdef OPENSSL_SYS_MSDOS | |
394 | # define DEV_TTY "con" | |
395 | # else | |
396 | # define DEV_TTY "/dev/tty" | |
397 | # endif | |
398 | if ((tty_in = fopen(DEV_TTY, "r")) == NULL) | |
399 | tty_in = stdin; | |
400 | if ((tty_out = fopen(DEV_TTY, "w")) == NULL) | |
401 | tty_out = stderr; | |
402 | # endif | |
403 | ||
404 | # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) | |
405 | if (TTY_get(fileno(tty_in), &tty_orig) == -1) { | |
406 | # ifdef ENOTTY | |
407 | if (errno == ENOTTY) | |
408 | is_a_tty = 0; | |
409 | else | |
410 | # endif | |
411 | # ifdef EINVAL | |
412 | /* | |
413 | * Ariel Glenn reports that solaris can return EINVAL instead. | |
414 | * This should be ok | |
415 | */ | |
416 | if (errno == EINVAL) | |
417 | is_a_tty = 0; | |
418 | else | |
419 | # endif | |
420 | # ifdef ENXIO | |
421 | /* | |
422 | * Solaris can return ENXIO. | |
423 | * This should be ok | |
424 | */ | |
425 | if (errno == ENXIO) | |
426 | is_a_tty = 0; | |
427 | else | |
428 | # endif | |
429 | # ifdef EIO | |
430 | /* | |
431 | * Linux can return EIO. | |
432 | * This should be ok | |
433 | */ | |
434 | if (errno == EIO) | |
435 | is_a_tty = 0; | |
436 | else | |
437 | # endif | |
438 | # ifdef EPERM | |
439 | /* | |
440 | * Linux can return EPERM (Operation not permitted), | |
441 | * e.g. if a daemon executes openssl via fork()+execve() | |
442 | * This should be ok | |
443 | */ | |
444 | if (errno == EPERM) | |
445 | is_a_tty = 0; | |
446 | else | |
447 | # endif | |
448 | # ifdef ENODEV | |
449 | /* | |
450 | * MacOS X returns ENODEV (Operation not supported by device), | |
451 | * which seems appropriate. | |
452 | */ | |
453 | if (errno == ENODEV) | |
454 | is_a_tty = 0; | |
455 | else | |
456 | # endif | |
457 | { | |
458 | char tmp_num[10]; | |
459 | BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno); | |
460 | UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE); | |
461 | ERR_add_error_data(2, "errno=", tmp_num); | |
462 | ||
463 | return 0; | |
464 | } | |
465 | } | |
466 | # endif | |
467 | # ifdef OPENSSL_SYS_VMS | |
468 | status = sys$assign(&terminal, &channel, 0, 0); | |
469 | ||
470 | /* if there isn't a TT device, something is very wrong */ | |
471 | if (status != SS$_NORMAL) { | |
472 | char tmp_num[12]; | |
473 | ||
474 | BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status); | |
475 | UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR); | |
476 | ERR_add_error_data(2, "status=", tmp_num); | |
477 | return 0; | |
478 | } | |
479 | ||
480 | status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, | |
481 | 0, 0, 0, 0); | |
482 | ||
483 | /* If IO$_SENSEMODE doesn't work, this is not a terminal device */ | |
484 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
485 | is_a_tty = 0; | |
486 | # endif | |
487 | return 1; | |
488 | } | |
489 | ||
490 | static int noecho_console(UI *ui) | |
491 | { | |
492 | # ifdef TTY_FLAGS | |
493 | memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig)); | |
494 | tty_new.TTY_FLAGS &= ~ECHO; | |
495 | # endif | |
496 | ||
497 | # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) | |
498 | if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) | |
499 | return 0; | |
500 | # endif | |
501 | # ifdef OPENSSL_SYS_VMS | |
502 | if (is_a_tty) { | |
503 | tty_new[0] = tty_orig[0]; | |
504 | tty_new[1] = tty_orig[1] | TT$M_NOECHO; | |
505 | tty_new[2] = tty_orig[2]; | |
506 | status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, | |
507 | 0, 0, 0, 0); | |
508 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) { | |
509 | char tmp_num[2][12]; | |
510 | ||
511 | BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X", | |
512 | status); | |
513 | BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X", | |
514 | iosb.iosb$w_value); | |
515 | UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR); | |
516 | ERR_add_error_data(5, "status=", tmp_num[0], | |
517 | ",", "iosb.iosb$w_value=", tmp_num[1]); | |
518 | return 0; | |
519 | } | |
520 | } | |
521 | # endif | |
522 | # if defined(_WIN32) && !defined(_WIN32_WCE) | |
523 | if (is_a_tty) { | |
524 | tty_new = tty_orig; | |
525 | tty_new &= ~ENABLE_ECHO_INPUT; | |
526 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new); | |
527 | } | |
528 | # endif | |
529 | return 1; | |
530 | } | |
531 | ||
532 | static int echo_console(UI *ui) | |
533 | { | |
534 | # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) | |
535 | memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig)); | |
536 | if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1)) | |
537 | return 0; | |
538 | # endif | |
539 | # ifdef OPENSSL_SYS_VMS | |
540 | if (is_a_tty) { | |
541 | tty_new[0] = tty_orig[0]; | |
542 | tty_new[1] = tty_orig[1]; | |
543 | tty_new[2] = tty_orig[2]; | |
544 | status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, | |
545 | 0, 0, 0, 0); | |
546 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) { | |
547 | char tmp_num[2][12]; | |
548 | ||
549 | BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X", | |
550 | status); | |
551 | BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X", | |
552 | iosb.iosb$w_value); | |
553 | UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR); | |
554 | ERR_add_error_data(5, "status=", tmp_num[0], | |
555 | ",", "iosb.iosb$w_value=", tmp_num[1]); | |
556 | return 0; | |
557 | } | |
558 | } | |
559 | # endif | |
560 | # if defined(_WIN32) && !defined(_WIN32_WCE) | |
561 | if (is_a_tty) { | |
562 | tty_new = tty_orig; | |
563 | SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new); | |
564 | } | |
565 | # endif | |
566 | return 1; | |
567 | } | |
568 | ||
569 | static int close_console(UI *ui) | |
570 | { | |
571 | if (tty_in != stdin) | |
572 | fclose(tty_in); | |
573 | if (tty_out != stderr) | |
574 | fclose(tty_out); | |
575 | # ifdef OPENSSL_SYS_VMS | |
576 | status = sys$dassgn(channel); | |
577 | if (status != SS$_NORMAL) { | |
578 | char tmp_num[12]; | |
579 | ||
580 | BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status); | |
581 | UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR); | |
582 | ERR_add_error_data(2, "status=", tmp_num); | |
583 | return 0; | |
584 | } | |
585 | # endif | |
586 | CRYPTO_THREAD_unlock(ui->lock); | |
587 | ||
588 | return 1; | |
589 | } | |
590 | ||
591 | # if !defined(OPENSSL_SYS_WINCE) | |
592 | /* Internal functions to handle signals and act on them */ | |
593 | static void pushsig(void) | |
594 | { | |
595 | # ifndef OPENSSL_SYS_WIN32 | |
596 | int i; | |
597 | # endif | |
598 | # ifdef SIGACTION | |
599 | struct sigaction sa; | |
600 | ||
601 | memset(&sa, 0, sizeof(sa)); | |
602 | sa.sa_handler = recsig; | |
603 | # endif | |
604 | ||
605 | # ifdef OPENSSL_SYS_WIN32 | |
606 | savsig[SIGABRT] = signal(SIGABRT, recsig); | |
607 | savsig[SIGFPE] = signal(SIGFPE, recsig); | |
608 | savsig[SIGILL] = signal(SIGILL, recsig); | |
609 | savsig[SIGINT] = signal(SIGINT, recsig); | |
610 | savsig[SIGSEGV] = signal(SIGSEGV, recsig); | |
611 | savsig[SIGTERM] = signal(SIGTERM, recsig); | |
612 | # else | |
613 | for (i = 1; i < NX509_SIG; i++) { | |
614 | # ifdef SIGUSR1 | |
615 | if (i == SIGUSR1) | |
616 | continue; | |
617 | # endif | |
618 | # ifdef SIGUSR2 | |
619 | if (i == SIGUSR2) | |
620 | continue; | |
621 | # endif | |
622 | # ifdef SIGKILL | |
623 | if (i == SIGKILL) /* We can't make any action on that. */ | |
624 | continue; | |
625 | # endif | |
626 | # ifdef SIGACTION | |
627 | sigaction(i, &sa, &savsig[i]); | |
628 | # else | |
629 | savsig[i] = signal(i, recsig); | |
630 | # endif | |
631 | } | |
632 | # endif | |
633 | ||
634 | # ifdef SIGWINCH | |
635 | signal(SIGWINCH, SIG_DFL); | |
636 | # endif | |
637 | } | |
638 | ||
639 | static void popsig(void) | |
640 | { | |
641 | # ifdef OPENSSL_SYS_WIN32 | |
642 | signal(SIGABRT, savsig[SIGABRT]); | |
643 | signal(SIGFPE, savsig[SIGFPE]); | |
644 | signal(SIGILL, savsig[SIGILL]); | |
645 | signal(SIGINT, savsig[SIGINT]); | |
646 | signal(SIGSEGV, savsig[SIGSEGV]); | |
647 | signal(SIGTERM, savsig[SIGTERM]); | |
648 | # else | |
649 | int i; | |
650 | for (i = 1; i < NX509_SIG; i++) { | |
651 | # ifdef SIGUSR1 | |
652 | if (i == SIGUSR1) | |
653 | continue; | |
654 | # endif | |
655 | # ifdef SIGUSR2 | |
656 | if (i == SIGUSR2) | |
657 | continue; | |
658 | # endif | |
659 | # ifdef SIGACTION | |
660 | sigaction(i, &savsig[i], NULL); | |
661 | # else | |
662 | signal(i, savsig[i]); | |
663 | # endif | |
664 | } | |
665 | # endif | |
666 | } | |
667 | ||
668 | static void recsig(int i) | |
669 | { | |
670 | intr_signal = i; | |
671 | } | |
672 | # endif | |
673 | ||
674 | /* Internal functions specific for Windows */ | |
675 | # if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32) | |
676 | static int noecho_fgets(char *buf, int size, FILE *tty) | |
677 | { | |
678 | int i; | |
679 | char *p; | |
680 | ||
681 | p = buf; | |
682 | for (;;) { | |
683 | if (size == 0) { | |
684 | *p = '\0'; | |
685 | break; | |
686 | } | |
687 | size--; | |
688 | # if defined(_WIN32) | |
689 | i = _getch(); | |
690 | # else | |
691 | i = getch(); | |
692 | # endif | |
693 | if (i == '\r') | |
694 | i = '\n'; | |
695 | *(p++) = i; | |
696 | if (i == '\n') { | |
697 | *p = '\0'; | |
698 | break; | |
699 | } | |
700 | } | |
701 | # ifdef WIN_CONSOLE_BUG | |
702 | /* | |
703 | * Win95 has several evil console bugs: one of these is that the last | |
704 | * character read using getch() is passed to the next read: this is | |
705 | * usually a CR so this can be trouble. No STDIO fix seems to work but | |
706 | * flushing the console appears to do the trick. | |
707 | */ | |
708 | { | |
709 | HANDLE inh; | |
710 | inh = GetStdHandle(STD_INPUT_HANDLE); | |
711 | FlushConsoleInputBuffer(inh); | |
712 | } | |
713 | # endif | |
714 | return strlen(buf); | |
715 | } | |
716 | # endif | |
717 | ||
718 | static UI_METHOD ui_openssl = { | |
719 | "OpenSSL default user interface", | |
720 | open_console, | |
721 | write_string, | |
722 | NULL, /* No flusher is needed for command lines */ | |
723 | read_string, | |
724 | close_console, | |
725 | NULL | |
726 | }; | |
727 | ||
728 | /* The method with all the built-in console thingies */ | |
729 | UI_METHOD *UI_OpenSSL(void) | |
730 | { | |
731 | return &ui_openssl; | |
732 | } | |
733 | ||
734 | static const UI_METHOD *default_UI_meth = &ui_openssl; | |
735 | ||
736 | #else | |
737 | ||
738 | static const UI_METHOD *default_UI_meth = NULL; | |
739 | ||
740 | #endif | |
741 | ||
742 | void UI_set_default_method(const UI_METHOD *meth) | |
743 | { | |
744 | default_UI_meth = meth; | |
745 | } | |
746 | ||
747 | const UI_METHOD *UI_get_default_method(void) | |
748 | { | |
749 | return default_UI_meth; | |
750 | } |