ui_openssl.c 18 KB

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