ui_openssl.c 20 KB

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