ui_openssl.c 18 KB

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