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