ui_openssl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. /* crypto/ui/ui_openssl.c */
  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. #if defined(OPENSSL_SYSNAME_VXWORKS)
  125. # include <sys/types.h>
  126. #endif
  127. #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
  128. # ifndef _POSIX_C_SOURCE
  129. # define _POSIX_C_SOURCE 2
  130. # endif
  131. #endif
  132. #include <signal.h>
  133. #include <stdio.h>
  134. #include <string.h>
  135. #include <errno.h>
  136. #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
  137. # ifdef OPENSSL_UNISTD
  138. # include OPENSSL_UNISTD
  139. # else
  140. # include <unistd.h>
  141. # endif
  142. /*
  143. * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
  144. * system and have sigaction and termios.
  145. */
  146. # if defined(_POSIX_VERSION)
  147. # define SIGACTION
  148. # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
  149. # define TERMIOS
  150. # endif
  151. # endif
  152. #endif
  153. #ifdef WIN16TTY
  154. # undef OPENSSL_SYS_WIN16
  155. # undef WIN16
  156. # undef _WINDOWS
  157. # include <graph.h>
  158. #endif
  159. /* 06-Apr-92 Luke Brennan Support for VMS */
  160. #include "ui_locl.h"
  161. #include "cryptlib.h"
  162. #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
  163. # include <starlet.h>
  164. # ifdef __DECC
  165. # pragma message disable DOLLARID
  166. # endif
  167. #endif
  168. #ifdef WIN_CONSOLE_BUG
  169. # include <windows.h>
  170. # ifndef OPENSSL_SYS_WINCE
  171. # include <wincon.h>
  172. # endif
  173. #endif
  174. /*
  175. * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
  176. * MSDOS and SGTTY.
  177. *
  178. * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
  179. * remain respected. Otherwise, we default to TERMIOS except for a few
  180. * systems that require something different.
  181. *
  182. * Note: we do not use SGTTY unless it's defined by the configuration. We
  183. * may eventually opt to remove it's use entirely.
  184. */
  185. #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
  186. # if defined(_LIBC)
  187. # undef TERMIOS
  188. # define TERMIO
  189. # undef SGTTY
  190. /*
  191. * We know that VMS, MSDOS, VXWORKS, NETWARE use entirely other mechanisms.
  192. * MAC_OS_GUSI_SOURCE should probably go away, but that needs to be confirmed.
  193. */
  194. # elif !defined(OPENSSL_SYS_VMS) \
  195. && !defined(OPENSSL_SYS_MSDOS) \
  196. && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) \
  197. && !defined(MAC_OS_GUSI_SOURCE) \
  198. && !defined(OPENSSL_SYS_VXWORKS) \
  199. && !defined(OPENSSL_SYS_NETWARE)
  200. # define TERMIOS
  201. # undef TERMIO
  202. # undef SGTTY
  203. # endif
  204. #endif
  205. #ifdef TERMIOS
  206. # include <termios.h>
  207. # define TTY_STRUCT struct termios
  208. # define TTY_FLAGS c_lflag
  209. # define TTY_get(tty,data) tcgetattr(tty,data)
  210. # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
  211. #endif
  212. #ifdef TERMIO
  213. # include <termio.h>
  214. # define TTY_STRUCT struct termio
  215. # define TTY_FLAGS c_lflag
  216. # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
  217. # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
  218. #endif
  219. #ifdef SGTTY
  220. # include <sgtty.h>
  221. # define TTY_STRUCT struct sgttyb
  222. # define TTY_FLAGS sg_flags
  223. # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
  224. # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
  225. #endif
  226. #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
  227. # include <sys/ioctl.h>
  228. #endif
  229. #ifdef OPENSSL_SYS_MSDOS
  230. # include <conio.h>
  231. #endif
  232. #ifdef OPENSSL_SYS_VMS
  233. # include <ssdef.h>
  234. # include <iodef.h>
  235. # include <ttdef.h>
  236. # include <descrip.h>
  237. struct IOSB {
  238. short iosb$w_value;
  239. short iosb$w_count;
  240. long iosb$l_info;
  241. };
  242. #endif
  243. #ifdef OPENSSL_SYS_SUNOS
  244. typedef int sig_atomic_t;
  245. #endif
  246. #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
  247. /*
  248. * This one needs work. As a matter of fact the code is unoperational
  249. * and this is only a trick to get it compiled.
  250. * <appro@fy.chalmers.se>
  251. */
  252. # define TTY_STRUCT int
  253. #endif
  254. #ifndef NX509_SIG
  255. # define NX509_SIG 32
  256. #endif
  257. /* Define globals. They are protected by a lock */
  258. #ifdef SIGACTION
  259. static struct sigaction savsig[NX509_SIG];
  260. #else
  261. static void (*savsig[NX509_SIG]) (int);
  262. #endif
  263. #ifdef OPENSSL_SYS_VMS
  264. static struct IOSB iosb;
  265. static $DESCRIPTOR(terminal, "TT");
  266. static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
  267. * will always suffice for the actual
  268. * structures? */
  269. static long status;
  270. static unsigned short channel = 0;
  271. #else
  272. # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
  273. static TTY_STRUCT tty_orig, tty_new;
  274. # endif
  275. #endif
  276. static FILE *tty_in, *tty_out;
  277. static int is_a_tty;
  278. /* Declare static functions */
  279. #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  280. static int read_till_nl(FILE *);
  281. static void recsig(int);
  282. static void pushsig(void);
  283. static void popsig(void);
  284. #endif
  285. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
  286. static int noecho_fgets(char *buf, int size, FILE *tty);
  287. #endif
  288. static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
  289. static int read_string(UI *ui, UI_STRING *uis);
  290. static int write_string(UI *ui, UI_STRING *uis);
  291. static int open_console(UI *ui);
  292. static int echo_console(UI *ui);
  293. static int noecho_console(UI *ui);
  294. static int close_console(UI *ui);
  295. static UI_METHOD ui_openssl = {
  296. "OpenSSL default user interface",
  297. open_console,
  298. write_string,
  299. NULL, /* No flusher is needed for command lines */
  300. read_string,
  301. close_console,
  302. NULL
  303. };
  304. /* The method with all the built-in thingies */
  305. UI_METHOD *UI_OpenSSL(void)
  306. {
  307. return &ui_openssl;
  308. }
  309. /*
  310. * The following function makes sure that info and error strings are printed
  311. * before any prompt.
  312. */
  313. static int write_string(UI *ui, UI_STRING *uis)
  314. {
  315. switch (UI_get_string_type(uis)) {
  316. case UIT_ERROR:
  317. case UIT_INFO:
  318. fputs(UI_get0_output_string(uis), tty_out);
  319. fflush(tty_out);
  320. break;
  321. default:
  322. break;
  323. }
  324. return 1;
  325. }
  326. static int read_string(UI *ui, UI_STRING *uis)
  327. {
  328. int ok = 0;
  329. switch (UI_get_string_type(uis)) {
  330. case UIT_BOOLEAN:
  331. fputs(UI_get0_output_string(uis), tty_out);
  332. fputs(UI_get0_action_string(uis), tty_out);
  333. fflush(tty_out);
  334. return read_string_inner(ui, uis,
  335. UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
  336. 0);
  337. case UIT_PROMPT:
  338. fputs(UI_get0_output_string(uis), tty_out);
  339. fflush(tty_out);
  340. return read_string_inner(ui, uis,
  341. UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
  342. 1);
  343. case UIT_VERIFY:
  344. fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
  345. fflush(tty_out);
  346. if ((ok = read_string_inner(ui, uis,
  347. UI_get_input_flags(uis) &
  348. UI_INPUT_FLAG_ECHO, 1)) <= 0)
  349. return ok;
  350. if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
  351. fprintf(tty_out, "Verify failure\n");
  352. fflush(tty_out);
  353. return 0;
  354. }
  355. break;
  356. default:
  357. break;
  358. }
  359. return 1;
  360. }
  361. #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  362. /* Internal functions to read a string without echoing */
  363. static int read_till_nl(FILE *in)
  364. {
  365. # define SIZE 4
  366. char buf[SIZE + 1];
  367. do {
  368. if (!fgets(buf, SIZE, in))
  369. return 0;
  370. } while (strchr(buf, '\n') == NULL);
  371. return 1;
  372. }
  373. static volatile sig_atomic_t intr_signal;
  374. #endif
  375. static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
  376. {
  377. static int ps;
  378. int ok;
  379. char result[BUFSIZ];
  380. int maxsize = BUFSIZ - 1;
  381. #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  382. char *p;
  383. intr_signal = 0;
  384. ok = 0;
  385. ps = 0;
  386. pushsig();
  387. ps = 1;
  388. if (!echo && !noecho_console(ui))
  389. goto error;
  390. ps = 2;
  391. result[0] = '\0';
  392. # ifdef OPENSSL_SYS_MSDOS
  393. if (!echo) {
  394. noecho_fgets(result, maxsize, tty_in);
  395. p = result; /* FIXME: noecho_fgets doesn't return errors */
  396. } else
  397. p = fgets(result, maxsize, tty_in);
  398. # else
  399. p = fgets(result, maxsize, tty_in);
  400. # endif
  401. if (p == NULL)
  402. goto error;
  403. if (feof(tty_in))
  404. goto error;
  405. if (ferror(tty_in))
  406. goto error;
  407. if ((p = (char *)strchr(result, '\n')) != NULL) {
  408. if (strip_nl)
  409. *p = '\0';
  410. } else if (!read_till_nl(tty_in))
  411. goto error;
  412. if (UI_set_result(ui, uis, result) >= 0)
  413. ok = 1;
  414. error:
  415. if (intr_signal == SIGINT)
  416. ok = -1;
  417. if (!echo)
  418. fprintf(tty_out, "\n");
  419. if (ps >= 2 && !echo && !echo_console(ui))
  420. ok = 0;
  421. if (ps >= 1)
  422. popsig();
  423. #else
  424. ok = 1;
  425. #endif
  426. OPENSSL_cleanse(result, BUFSIZ);
  427. return ok;
  428. }
  429. /* Internal functions to open, handle and close a channel to the console. */
  430. static int open_console(UI *ui)
  431. {
  432. CRYPTO_w_lock(CRYPTO_LOCK_UI);
  433. is_a_tty = 1;
  434. #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE) || defined(OPENSSL_SYS_BEOS)
  435. tty_in = stdin;
  436. tty_out = stderr;
  437. #else
  438. # ifdef OPENSSL_SYS_MSDOS
  439. # define DEV_TTY "con"
  440. # else
  441. # define DEV_TTY "/dev/tty"
  442. # endif
  443. if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
  444. tty_in = stdin;
  445. if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
  446. tty_out = stderr;
  447. #endif
  448. #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
  449. if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
  450. # ifdef ENOTTY
  451. if (errno == ENOTTY)
  452. is_a_tty = 0;
  453. else
  454. # endif
  455. # ifdef EINVAL
  456. /*
  457. * Ariel Glenn ariel@columbia.edu reports that solaris can return
  458. * EINVAL instead. This should be ok
  459. */
  460. if (errno == EINVAL)
  461. is_a_tty = 0;
  462. else
  463. # endif
  464. # ifdef ENODEV
  465. /*
  466. * MacOS X returns ENODEV (Operation not supported by device),
  467. * which seems appropriate.
  468. */
  469. if (errno == ENODEV)
  470. is_a_tty = 0;
  471. else
  472. # endif
  473. return 0;
  474. }
  475. #endif
  476. #ifdef OPENSSL_SYS_VMS
  477. status = sys$assign(&terminal, &channel, 0, 0);
  478. /* if there isn't a TT device, something is very wrong */
  479. if (status != SS$_NORMAL)
  480. return 0;
  481. status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
  482. 0, 0, 0, 0);
  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. static int noecho_console(UI *ui)
  490. {
  491. #ifdef TTY_FLAGS
  492. memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
  493. tty_new.TTY_FLAGS &= ~ECHO;
  494. #endif
  495. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  496. if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
  497. return 0;
  498. #endif
  499. #ifdef OPENSSL_SYS_VMS
  500. if (is_a_tty) {
  501. tty_new[0] = tty_orig[0];
  502. tty_new[1] = tty_orig[1] | TT$M_NOECHO;
  503. tty_new[2] = tty_orig[2];
  504. status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
  505. 0, 0, 0, 0);
  506. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  507. return 0;
  508. }
  509. #endif
  510. return 1;
  511. }
  512. static int echo_console(UI *ui)
  513. {
  514. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  515. memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
  516. tty_new.TTY_FLAGS |= ECHO;
  517. #endif
  518. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  519. if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
  520. return 0;
  521. #endif
  522. #ifdef OPENSSL_SYS_VMS
  523. if (is_a_tty) {
  524. tty_new[0] = tty_orig[0];
  525. tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
  526. tty_new[2] = tty_orig[2];
  527. status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
  528. 0, 0, 0, 0);
  529. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  530. return 0;
  531. }
  532. #endif
  533. return 1;
  534. }
  535. static int close_console(UI *ui)
  536. {
  537. if (tty_in != stdin)
  538. fclose(tty_in);
  539. if (tty_out != stderr)
  540. fclose(tty_out);
  541. #ifdef OPENSSL_SYS_VMS
  542. status = sys$dassgn(channel);
  543. if (status != SS$_NORMAL)
  544. return 0;
  545. #endif
  546. CRYPTO_w_unlock(CRYPTO_LOCK_UI);
  547. return 1;
  548. }
  549. #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  550. /* Internal functions to handle signals and act on them */
  551. static void pushsig(void)
  552. {
  553. # ifndef OPENSSL_SYS_WIN32
  554. int i;
  555. # endif
  556. # ifdef SIGACTION
  557. struct sigaction sa;
  558. memset(&sa, 0, sizeof sa);
  559. sa.sa_handler = recsig;
  560. # endif
  561. # ifdef OPENSSL_SYS_WIN32
  562. savsig[SIGABRT] = signal(SIGABRT, recsig);
  563. savsig[SIGFPE] = signal(SIGFPE, recsig);
  564. savsig[SIGILL] = signal(SIGILL, recsig);
  565. savsig[SIGINT] = signal(SIGINT, recsig);
  566. savsig[SIGSEGV] = signal(SIGSEGV, recsig);
  567. savsig[SIGTERM] = signal(SIGTERM, recsig);
  568. # else
  569. for (i = 1; i < NX509_SIG; i++) {
  570. # ifdef SIGUSR1
  571. if (i == SIGUSR1)
  572. continue;
  573. # endif
  574. # ifdef SIGUSR2
  575. if (i == SIGUSR2)
  576. continue;
  577. # endif
  578. # ifdef SIGKILL
  579. if (i == SIGKILL) /* We can't make any action on that. */
  580. continue;
  581. # endif
  582. # ifdef SIGACTION
  583. sigaction(i, &sa, &savsig[i]);
  584. # else
  585. savsig[i] = signal(i, recsig);
  586. # endif
  587. }
  588. # endif
  589. # ifdef SIGWINCH
  590. signal(SIGWINCH, SIG_DFL);
  591. # endif
  592. }
  593. static void popsig(void)
  594. {
  595. # ifdef OPENSSL_SYS_WIN32
  596. signal(SIGABRT, savsig[SIGABRT]);
  597. signal(SIGFPE, savsig[SIGFPE]);
  598. signal(SIGILL, savsig[SIGILL]);
  599. signal(SIGINT, savsig[SIGINT]);
  600. signal(SIGSEGV, savsig[SIGSEGV]);
  601. signal(SIGTERM, savsig[SIGTERM]);
  602. # else
  603. int i;
  604. for (i = 1; i < NX509_SIG; i++) {
  605. # ifdef SIGUSR1
  606. if (i == SIGUSR1)
  607. continue;
  608. # endif
  609. # ifdef SIGUSR2
  610. if (i == SIGUSR2)
  611. continue;
  612. # endif
  613. # ifdef SIGACTION
  614. sigaction(i, &savsig[i], NULL);
  615. # else
  616. signal(i, savsig[i]);
  617. # endif
  618. }
  619. # endif
  620. }
  621. static void recsig(int i)
  622. {
  623. intr_signal = i;
  624. }
  625. #endif
  626. /* Internal functions specific for Windows */
  627. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  628. static int noecho_fgets(char *buf, int size, FILE *tty)
  629. {
  630. int i;
  631. char *p;
  632. p = buf;
  633. for (;;) {
  634. if (size == 0) {
  635. *p = '\0';
  636. break;
  637. }
  638. size--;
  639. # ifdef WIN16TTY
  640. i = _inchar();
  641. # elif defined(_WIN32)
  642. i = _getch();
  643. # else
  644. i = getch();
  645. # endif
  646. if (i == '\r')
  647. i = '\n';
  648. *(p++) = i;
  649. if (i == '\n') {
  650. *p = '\0';
  651. break;
  652. }
  653. }
  654. # ifdef WIN_CONSOLE_BUG
  655. /*
  656. * Win95 has several evil console bugs: one of these is that the last
  657. * character read using getch() is passed to the next read: this is
  658. * usually a CR so this can be trouble. No STDIO fix seems to work but
  659. * flushing the console appears to do the trick.
  660. */
  661. {
  662. HANDLE inh;
  663. inh = GetStdHandle(STD_INPUT_HANDLE);
  664. FlushConsoleInputBuffer(inh);
  665. }
  666. # endif
  667. return (strlen(buf));
  668. }
  669. #endif