72d85eefcd336e4eadb75a4976d6ccdb9179950b
[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(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)
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 #if defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
269 /*
270  * This one needs work. As a matter of fact the code is unoperational
271  * and this is only a trick to get it compiled.
272  *                                      <appro@fy.chalmers.se>
273  */
274 # define TTY_STRUCT int
275 #endif
276
277 #ifndef NX509_SIG
278 # define NX509_SIG 32
279 #endif
280
281
282 /* Define globals.  They are protected by a lock */
283 #ifdef SIGACTION
284 static struct sigaction savsig[NX509_SIG];
285 #else
286 static void (*savsig[NX509_SIG])(int );
287 #endif
288
289 #ifdef OPENSSL_SYS_VMS
290 static struct IOSB iosb;
291 static $DESCRIPTOR(terminal,"TT");
292 static long tty_orig[3], tty_new[3]; /* XXX   Is there any guarantee that this will always suffice for the actual structures? */
293 static long status;
294 static unsigned short channel = 0;
295 #else
296 #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
297 static TTY_STRUCT tty_orig,tty_new;
298 #endif
299 #endif
300 static FILE *tty_in, *tty_out;
301 static int is_a_tty;
302
303 /* Declare static functions */
304 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
305 static int read_till_nl(FILE *);
306 static void recsig(int);
307 static void pushsig(void);
308 static void popsig(void);
309 #endif
310 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
311 static int noecho_fgets(char *buf, int size, FILE *tty);
312 #endif
313 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
314
315 static int read_string(UI *ui, UI_STRING *uis);
316 static int write_string(UI *ui, UI_STRING *uis);
317
318 static int open_console(UI *ui);
319 static int echo_console(UI *ui);
320 static int noecho_console(UI *ui);
321 static int close_console(UI *ui);
322
323 static UI_METHOD ui_openssl =
324         {
325         "OpenSSL default user interface",
326         open_console,
327         write_string,
328         NULL,                   /* No flusher is needed for command lines */
329         read_string,
330         close_console,
331         NULL
332         };
333
334 /* The method with all the built-in thingies */
335 UI_METHOD *UI_OpenSSL(void)
336         {
337         return &ui_openssl;
338         }
339
340 /* The following function makes sure that info and error strings are printed
341    before any prompt. */
342 static int write_string(UI *ui, UI_STRING *uis)
343         {
344         switch (UI_get_string_type(uis))
345                 {
346         case UIT_ERROR:
347         case UIT_INFO:
348                 fputs(UI_get0_output_string(uis), tty_out);
349                 fflush(tty_out);
350                 break;
351         default:
352                 break;
353                 }
354         return 1;
355         }
356
357 static int read_string(UI *ui, UI_STRING *uis)
358         {
359         int ok = 0;
360
361         switch (UI_get_string_type(uis))
362                 {
363         case UIT_BOOLEAN:
364                 fputs(UI_get0_output_string(uis), tty_out);
365                 fputs(UI_get0_action_string(uis), tty_out);
366                 fflush(tty_out);
367                 return read_string_inner(ui, uis,
368                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
369         case UIT_PROMPT:
370                 fputs(UI_get0_output_string(uis), tty_out);
371                 fflush(tty_out);
372                 return read_string_inner(ui, uis,
373                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
374         case UIT_VERIFY:
375                 fprintf(tty_out,"Verifying - %s",
376                         UI_get0_output_string(uis));
377                 fflush(tty_out);
378                 if ((ok = read_string_inner(ui, uis,
379                         UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
380                         return ok;
381                 if (strcmp(UI_get0_result_string(uis),
382                         UI_get0_test_string(uis)) != 0)
383                         {
384                         fprintf(tty_out,"Verify failure\n");
385                         fflush(tty_out);
386                         return 0;
387                         }
388                 break;
389         default:
390                 break;
391                 }
392         return 1;
393         }
394
395
396 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
397 /* Internal functions to read a string without echoing */
398 static int read_till_nl(FILE *in)
399         {
400 #define SIZE 4
401         char buf[SIZE+1];
402
403         do      {
404                 if (!fgets(buf,SIZE,in))
405                         return 0;
406                 } while (strchr(buf,'\n') == NULL);
407         return 1;
408         }
409
410 static volatile sig_atomic_t intr_signal;
411 #endif
412
413 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
414         {
415         static int ps;
416         int ok;
417         char result[BUFSIZ];
418         int maxsize = BUFSIZ-1;
419 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
420         char *p;
421
422         intr_signal=0;
423         ok=0;
424         ps=0;
425
426         pushsig();
427         ps=1;
428
429         if (!echo && !noecho_console(ui))
430                 goto error;
431         ps=2;
432
433         result[0]='\0';
434 #ifdef OPENSSL_SYS_MSDOS
435         if (!echo)
436                 {
437                 noecho_fgets(result,maxsize,tty_in);
438                 p=result; /* FIXME: noecho_fgets doesn't return errors */
439                 }
440         else
441                 p=fgets(result,maxsize,tty_in);
442 #else
443         p=fgets(result,maxsize,tty_in);
444 #endif
445         if(!p)
446                 goto error;
447         if (feof(tty_in)) goto error;
448         if (ferror(tty_in)) goto error;
449         if ((p=(char *)strchr(result,'\n')) != NULL)
450                 {
451                 if (strip_nl)
452                         *p='\0';
453                 }
454         else
455                 if (!read_till_nl(tty_in))
456                         goto error;
457         if (UI_set_result(ui, uis, result) >= 0)
458                 ok=1;
459
460 error:
461         if (intr_signal == SIGINT)
462                 ok=-1;
463         if (!echo) fprintf(tty_out,"\n");
464         if (ps >= 2 && !echo && !echo_console(ui))
465                 ok=0;
466
467         if (ps >= 1)
468                 popsig();
469 #else
470         ok=1;
471 #endif
472
473         OPENSSL_cleanse(result,BUFSIZ);
474         return ok;
475         }
476
477
478 /* Internal functions to open, handle and close a channel to the console.  */
479 static int open_console(UI *ui)
480         {
481         CRYPTO_w_lock(CRYPTO_LOCK_UI);
482         is_a_tty = 1;
483
484 #if defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
485         tty_in=stdin;
486         tty_out=stderr;
487 #else
488 #  ifdef OPENSSL_SYS_MSDOS
489 #    define DEV_TTY "con"
490 #  else
491 #    define DEV_TTY "/dev/tty"
492 #  endif
493         if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
494                 tty_in=stdin;
495         if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
496                 tty_out=stderr;
497 #endif
498
499 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
500         if (TTY_get(fileno(tty_in),&tty_orig) == -1)
501                 {
502 #ifdef ENOTTY
503                 if (errno == ENOTTY)
504                         is_a_tty=0;
505                 else
506 #endif
507 #ifdef EINVAL
508                 /* Ariel Glenn ariel@columbia.edu reports that solaris
509                  * can return EINVAL instead.  This should be ok */
510                 if (errno == EINVAL)
511                         is_a_tty=0;
512                 else
513 #endif
514                         return 0;
515                 }
516 #endif
517 #ifdef OPENSSL_SYS_VMS
518         status = sys$assign(&terminal,&channel,0,0);
519         if (status != SS$_NORMAL)
520                 return 0;
521         status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
522         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
523                 return 0;
524 #endif
525         return 1;
526         }
527
528 static int noecho_console(UI *ui)
529         {
530 #ifdef TTY_FLAGS
531         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
532         tty_new.TTY_FLAGS &= ~ECHO;
533 #endif
534
535 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
536         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
537                 return 0;
538 #endif
539 #ifdef OPENSSL_SYS_VMS
540         tty_new[0] = tty_orig[0];
541         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
542         tty_new[2] = tty_orig[2];
543         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
544         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
545                 return 0;
546 #endif
547         return 1;
548         }
549
550 static int echo_console(UI *ui)
551         {
552 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
553         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
554         tty_new.TTY_FLAGS |= ECHO;
555 #endif
556
557 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
558         if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
559                 return 0;
560 #endif
561 #ifdef OPENSSL_SYS_VMS
562         tty_new[0] = tty_orig[0];
563         tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
564         tty_new[2] = tty_orig[2];
565         status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
566         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
567                 return 0;
568 #endif
569         return 1;
570         }
571
572 static int close_console(UI *ui)
573         {
574         if (tty_in != stdin) fclose(tty_in);
575         if (tty_out != stderr) fclose(tty_out);
576 #ifdef OPENSSL_SYS_VMS
577         status = sys$dassgn(channel);
578 #endif
579         CRYPTO_w_unlock(CRYPTO_LOCK_UI);
580
581         return 1;
582         }
583
584
585 #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
586 /* Internal functions to handle signals and act on them */
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                 {
609 #ifdef SIGUSR1
610                 if (i == SIGUSR1)
611                         continue;
612 #endif
613 #ifdef SIGUSR2
614                 if (i == SIGUSR2)
615                         continue;
616 #endif
617 #ifdef SIGKILL
618                 if (i == SIGKILL) /* We can't make any action on that. */
619                         continue;
620 #endif
621 #ifdef SIGACTION
622                 sigaction(i,&sa,&savsig[i]);
623 #else
624                 savsig[i]=signal(i,recsig);
625 #endif
626                 }
627 #endif
628
629 #ifdef SIGWINCH
630         signal(SIGWINCH,SIG_DFL);
631 #endif
632         }
633
634 static void popsig(void)
635         {
636 #ifdef OPENSSL_SYS_WIN32
637         signal(SIGABRT,savsig[SIGABRT]);
638         signal(SIGFPE,savsig[SIGFPE]);
639         signal(SIGILL,savsig[SIGILL]);
640         signal(SIGINT,savsig[SIGINT]);
641         signal(SIGSEGV,savsig[SIGSEGV]);
642         signal(SIGTERM,savsig[SIGTERM]);
643 #else
644         int i;
645         for (i=1; i<NX509_SIG; i++)
646                 {
647 #ifdef SIGUSR1
648                 if (i == SIGUSR1)
649                         continue;
650 #endif
651 #ifdef SIGUSR2
652                 if (i == SIGUSR2)
653                         continue;
654 #endif
655 #ifdef SIGACTION
656                 sigaction(i,&savsig[i],NULL);
657 #else
658                 signal(i,savsig[i]);
659 #endif
660                 }
661 #endif
662         }
663
664 static void recsig(int i)
665         {
666         intr_signal=i;
667         }
668 #endif
669
670 /* Internal functions specific for Windows */
671 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
672 static int noecho_fgets(char *buf, int size, FILE *tty)
673         {
674         int i;
675         char *p;
676
677         p=buf;
678         for (;;)
679                 {
680                 if (size == 0)
681                         {
682                         *p='\0';
683                         break;
684                         }
685                 size--;
686 #ifdef WIN16TTY
687                 i=_inchar();
688 #elif defined(_WIN32)
689                 i=_getch();
690 #else
691                 i=getch();
692 #endif
693                 if (i == '\r') i='\n';
694                 *(p++)=i;
695                 if (i == '\n')
696                         {
697                         *p='\0';
698                         break;
699                         }
700                 }
701 #ifdef WIN_CONSOLE_BUG
702 /* Win95 has several evil console bugs: one of these is that the
703  * last character read using getch() is passed to the next read: this is
704  * usually a CR so this can be trouble. No STDIO fix seems to work but
705  * flushing the console appears to do the trick.
706  */
707                 {
708                         HANDLE inh;
709                         inh = GetStdHandle(STD_INPUT_HANDLE);
710                         FlushConsoleInputBuffer(inh);
711                 }
712 #endif
713         return(strlen(buf));
714         }
715 #endif