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