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