2
0

ui_openssl.c 20 KB

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