ui_openssl.c 19 KB

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