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