ui_openssl.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  1. /* crypto/ui/ui_openssl.c */
  2. /*
  3. * Written by Richard Levitte ([email protected]) and others for the
  4. * OpenSSL project 2001.
  5. */
  6. /* ====================================================================
  7. * Copyright (c) 2001-2018 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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  56. * Hudson ([email protected]).
  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 ([email protected])
  62. * All rights reserved.
  63. *
  64. * This package is an SSL implementation written
  65. * by Eric Young ([email protected]).
  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 ([email protected]).
  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 ([email protected])"
  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 ([email protected])"
  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. * <[email protected]>
  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 [email protected] 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 ENXIO
  465. /*
  466. * Solaris can return ENXIO.
  467. * This should be ok
  468. */
  469. if (errno == ENXIO)
  470. is_a_tty = 0;
  471. else
  472. # endif
  473. # ifdef EIO
  474. /*
  475. * Linux can return EIO.
  476. * This should be ok
  477. */
  478. if (errno == EIO)
  479. is_a_tty = 0;
  480. else
  481. # endif
  482. # ifdef ENODEV
  483. /*
  484. * MacOS X returns ENODEV (Operation not supported by device),
  485. * which seems appropriate.
  486. */
  487. if (errno == ENODEV)
  488. is_a_tty = 0;
  489. else
  490. # endif
  491. return 0;
  492. }
  493. #endif
  494. #ifdef OPENSSL_SYS_VMS
  495. status = sys$assign(&terminal, &channel, 0, 0);
  496. /* if there isn't a TT device, something is very wrong */
  497. if (status != SS$_NORMAL)
  498. return 0;
  499. status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
  500. 0, 0, 0, 0);
  501. /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
  502. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  503. is_a_tty = 0;
  504. #endif
  505. return 1;
  506. }
  507. static int noecho_console(UI *ui)
  508. {
  509. #ifdef TTY_FLAGS
  510. memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
  511. tty_new.TTY_FLAGS &= ~ECHO;
  512. #endif
  513. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  514. if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
  515. return 0;
  516. #endif
  517. #ifdef OPENSSL_SYS_VMS
  518. if (is_a_tty) {
  519. tty_new[0] = tty_orig[0];
  520. tty_new[1] = tty_orig[1] | TT$M_NOECHO;
  521. tty_new[2] = tty_orig[2];
  522. status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
  523. 0, 0, 0, 0);
  524. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  525. return 0;
  526. }
  527. #endif
  528. return 1;
  529. }
  530. static int echo_console(UI *ui)
  531. {
  532. #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
  533. memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
  534. if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
  535. return 0;
  536. #endif
  537. #ifdef OPENSSL_SYS_VMS
  538. if (is_a_tty) {
  539. tty_new[0] = tty_orig[0];
  540. tty_new[1] = tty_orig[1];
  541. tty_new[2] = tty_orig[2];
  542. status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
  543. 0, 0, 0, 0);
  544. if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
  545. return 0;
  546. }
  547. #endif
  548. return 1;
  549. }
  550. static int close_console(UI *ui)
  551. {
  552. if (tty_in != stdin)
  553. fclose(tty_in);
  554. if (tty_out != stderr)
  555. fclose(tty_out);
  556. #ifdef OPENSSL_SYS_VMS
  557. status = sys$dassgn(channel);
  558. if (status != SS$_NORMAL)
  559. return 0;
  560. #endif
  561. CRYPTO_w_unlock(CRYPTO_LOCK_UI);
  562. return 1;
  563. }
  564. #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  565. /* Internal functions to handle signals and act on them */
  566. static void pushsig(void)
  567. {
  568. # ifndef OPENSSL_SYS_WIN32
  569. int i;
  570. # endif
  571. # ifdef SIGACTION
  572. struct sigaction sa;
  573. memset(&sa, 0, sizeof(sa));
  574. sa.sa_handler = recsig;
  575. # endif
  576. # ifdef OPENSSL_SYS_WIN32
  577. savsig[SIGABRT] = signal(SIGABRT, recsig);
  578. savsig[SIGFPE] = signal(SIGFPE, recsig);
  579. savsig[SIGILL] = signal(SIGILL, recsig);
  580. savsig[SIGINT] = signal(SIGINT, recsig);
  581. savsig[SIGSEGV] = signal(SIGSEGV, recsig);
  582. savsig[SIGTERM] = signal(SIGTERM, recsig);
  583. # else
  584. for (i = 1; i < NX509_SIG; i++) {
  585. # ifdef SIGUSR1
  586. if (i == SIGUSR1)
  587. continue;
  588. # endif
  589. # ifdef SIGUSR2
  590. if (i == SIGUSR2)
  591. continue;
  592. # endif
  593. # ifdef SIGKILL
  594. if (i == SIGKILL) /* We can't make any action on that. */
  595. continue;
  596. # endif
  597. # ifdef SIGACTION
  598. sigaction(i, &sa, &savsig[i]);
  599. # else
  600. savsig[i] = signal(i, recsig);
  601. # endif
  602. }
  603. # endif
  604. # ifdef SIGWINCH
  605. signal(SIGWINCH, SIG_DFL);
  606. # endif
  607. }
  608. static void popsig(void)
  609. {
  610. # ifdef OPENSSL_SYS_WIN32
  611. signal(SIGABRT, savsig[SIGABRT]);
  612. signal(SIGFPE, savsig[SIGFPE]);
  613. signal(SIGILL, savsig[SIGILL]);
  614. signal(SIGINT, savsig[SIGINT]);
  615. signal(SIGSEGV, savsig[SIGSEGV]);
  616. signal(SIGTERM, savsig[SIGTERM]);
  617. # else
  618. int i;
  619. for (i = 1; i < NX509_SIG; i++) {
  620. # ifdef SIGUSR1
  621. if (i == SIGUSR1)
  622. continue;
  623. # endif
  624. # ifdef SIGUSR2
  625. if (i == SIGUSR2)
  626. continue;
  627. # endif
  628. # ifdef SIGACTION
  629. sigaction(i, &savsig[i], NULL);
  630. # else
  631. signal(i, savsig[i]);
  632. # endif
  633. }
  634. # endif
  635. }
  636. static void recsig(int i)
  637. {
  638. intr_signal = i;
  639. }
  640. #endif
  641. /* Internal functions specific for Windows */
  642. #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
  643. static int noecho_fgets(char *buf, int size, FILE *tty)
  644. {
  645. int i;
  646. char *p;
  647. p = buf;
  648. for (;;) {
  649. if (size == 0) {
  650. *p = '\0';
  651. break;
  652. }
  653. size--;
  654. # ifdef WIN16TTY
  655. i = _inchar();
  656. # elif defined(_WIN32)
  657. i = _getch();
  658. # else
  659. i = getch();
  660. # endif
  661. if (i == '\r')
  662. i = '\n';
  663. *(p++) = i;
  664. if (i == '\n') {
  665. *p = '\0';
  666. break;
  667. }
  668. }
  669. # ifdef WIN_CONSOLE_BUG
  670. /*
  671. * Win95 has several evil console bugs: one of these is that the last
  672. * character read using getch() is passed to the next read: this is
  673. * usually a CR so this can be trouble. No STDIO fix seems to work but
  674. * flushing the console appears to do the trick.
  675. */
  676. {
  677. HANDLE inh;
  678. inh = GetStdHandle(STD_INPUT_HANDLE);
  679. FlushConsoleInputBuffer(inh);
  680. }
  681. # endif
  682. return (strlen(buf));
  683. }
  684. #endif