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