Coverage Report

Created: 2018-08-29 13:53

/src/openssl/crypto/ui/ui_openssl.c
Line
Count
Source (jump to first uncovered line)
1
/*
2
 * Copyright 2001-2018 The OpenSSL Project Authors. All Rights Reserved.
3
 *
4
 * Licensed under the OpenSSL license (the "License").  You may not use
5
 * this file except in compliance with the License.  You can obtain a copy
6
 * in the file LICENSE in the source distribution or at
7
 * https://www.openssl.org/source/license.html
8
 */
9
10
#include "e_os.h"
11
#include <openssl/e_os2.h>
12
#include <openssl/err.h>
13
#include <openssl/ui.h>
14
15
#ifndef OPENSSL_NO_UI_CONSOLE
16
/*
17
 * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
18
 * [maybe others?], because it masks interfaces not discussed in standard,
19
 * sigaction and fileno included. -pedantic would be more appropriate for the
20
 * intended purposes, but we can't prevent users from adding -ansi.
21
 */
22
# if defined(OPENSSL_SYS_VXWORKS)
23
#  include <sys/types.h>
24
# endif
25
26
# if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
27
#  ifndef _POSIX_C_SOURCE
28
#   define _POSIX_C_SOURCE 2
29
#  endif
30
# endif
31
# include <signal.h>
32
# include <stdio.h>
33
# include <string.h>
34
# include <errno.h>
35
36
# if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
37
#  ifdef OPENSSL_UNISTD
38
#   include OPENSSL_UNISTD
39
#  else
40
#   include <unistd.h>
41
#  endif
42
/*
43
 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
44
 * system and have sigaction and termios.
45
 */
46
#  if defined(_POSIX_VERSION) && _POSIX_VERSION>=199309L
47
48
#   define SIGACTION
49
#   if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
50
#    define TERMIOS
51
#   endif
52
53
#  endif
54
# endif
55
56
# include "ui_locl.h"
57
# include "internal/cryptlib.h"
58
59
# ifdef OPENSSL_SYS_VMS          /* prototypes for sys$whatever */
60
#  include <starlet.h>
61
#  ifdef __DECC
62
#   pragma message disable DOLLARID
63
#  endif
64
# endif
65
66
# ifdef WIN_CONSOLE_BUG
67
#  include <windows.h>
68
#  ifndef OPENSSL_SYS_WINCE
69
#   include <wincon.h>
70
#  endif
71
# endif
72
73
/*
74
 * There are 6 types of terminal interface supported, TERMIO, TERMIOS, VMS,
75
 * MSDOS, WIN32 Console and SGTTY.
76
 *
77
 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
78
 * remain respected.  Otherwise, we default to TERMIOS except for a few
79
 * systems that require something different.
80
 *
81
 * Note: we do not use SGTTY unless it's defined by the configuration.  We
82
 * may eventually opt to remove it's use entirely.
83
 */
84
85
# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
86
87
#  if defined(_LIBC)
88
#   undef  TERMIOS
89
#   define TERMIO
90
#   undef  SGTTY
91
/*
92
 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
93
 */
94
#  elif !defined(OPENSSL_SYS_VMS) \
95
  && !defined(OPENSSL_SYS_MSDOS) \
96
  && !defined(OPENSSL_SYS_VXWORKS)
97
#   define TERMIOS
98
#   undef  TERMIO
99
#   undef  SGTTY
100
#  endif
101
102
# endif
103
104
# ifdef TERMIOS
105
#  include <termios.h>
106
#  define TTY_STRUCT             struct termios
107
0
#  define TTY_FLAGS              c_lflag
108
0
#  define TTY_get(tty,data)      tcgetattr(tty,data)
109
0
#  define TTY_set(tty,data)      tcsetattr(tty,TCSANOW,data)
110
# endif
111
112
# ifdef TERMIO
113
#  include <termio.h>
114
#  define TTY_STRUCT             struct termio
115
#  define TTY_FLAGS              c_lflag
116
#  define TTY_get(tty,data)      ioctl(tty,TCGETA,data)
117
#  define TTY_set(tty,data)      ioctl(tty,TCSETA,data)
118
# endif
119
120
# ifdef SGTTY
121
#  include <sgtty.h>
122
#  define TTY_STRUCT             struct sgttyb
123
#  define TTY_FLAGS              sg_flags
124
#  define TTY_get(tty,data)      ioctl(tty,TIOCGETP,data)
125
#  define TTY_set(tty,data)      ioctl(tty,TIOCSETP,data)
126
# endif
127
128
# if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
129
#  include <sys/ioctl.h>
130
# endif
131
132
# ifdef OPENSSL_SYS_MSDOS
133
#  include <conio.h>
134
# endif
135
136
# ifdef OPENSSL_SYS_VMS
137
#  include <ssdef.h>
138
#  include <iodef.h>
139
#  include <ttdef.h>
140
#  include <descrip.h>
141
struct IOSB {
142
    short iosb$w_value;
143
    short iosb$w_count;
144
    long iosb$l_info;
145
};
146
# endif
147
148
# ifndef NX509_SIG
149
0
#  define NX509_SIG 32
150
# endif
151
152
/* Define globals.  They are protected by a lock */
153
# ifdef SIGACTION
154
static struct sigaction savsig[NX509_SIG];
155
# else
156
static void (*savsig[NX509_SIG]) (int);
157
# endif
158
159
# ifdef OPENSSL_SYS_VMS
160
static struct IOSB iosb;
161
static $DESCRIPTOR(terminal, "TT");
162
static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
163
                                      * will always suffice for the actual
164
                                      * structures? */
165
static long status;
166
static unsigned short channel = 0;
167
# elif defined(_WIN32) && !defined(_WIN32_WCE)
168
static DWORD tty_orig, tty_new;
169
# else
170
#  if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
171
static TTY_STRUCT tty_orig, tty_new;
172
#  endif
173
# endif
174
static FILE *tty_in, *tty_out;
175
static int is_a_tty;
176
177
/* Declare static functions */
178
# if !defined(OPENSSL_SYS_WINCE)
179
static int read_till_nl(FILE *);
180
static void recsig(int);
181
static void pushsig(void);
182
static void popsig(void);
183
# endif
184
# if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
185
static int noecho_fgets(char *buf, int size, FILE *tty);
186
# endif
187
static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
188
189
static int read_string(UI *ui, UI_STRING *uis);
190
static int write_string(UI *ui, UI_STRING *uis);
191
192
static int open_console(UI *ui);
193
static int echo_console(UI *ui);
194
static int noecho_console(UI *ui);
195
static int close_console(UI *ui);
196
197
/*
198
 * The following function makes sure that info and error strings are printed
199
 * before any prompt.
200
 */
201
static int write_string(UI *ui, UI_STRING *uis)
202
0
{
203
0
    switch (UI_get_string_type(uis)) {
204
0
    case UIT_ERROR:
205
0
    case UIT_INFO:
206
0
        fputs(UI_get0_output_string(uis), tty_out);
207
0
        fflush(tty_out);
208
0
        break;
209
0
    case UIT_NONE:
210
0
    case UIT_PROMPT:
211
0
    case UIT_VERIFY:
212
0
    case UIT_BOOLEAN:
213
0
        break;
214
0
    }
215
0
    return 1;
216
0
}
217
218
static int read_string(UI *ui, UI_STRING *uis)
219
{
220
    int ok = 0;
221
222
    switch (UI_get_string_type(uis)) {
223
    case UIT_BOOLEAN:
224
        fputs(UI_get0_output_string(uis), tty_out);
225
        fputs(UI_get0_action_string(uis), tty_out);
226
        fflush(tty_out);
227
        return read_string_inner(ui, uis,
228
                                 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
229
                                 0);
230
    case UIT_PROMPT:
231
        fputs(UI_get0_output_string(uis), tty_out);
232
        fflush(tty_out);
233
        return read_string_inner(ui, uis,
234
                                 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
235
                                 1);
236
    case UIT_VERIFY:
237
        fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
238
        fflush(tty_out);
239
        if ((ok = read_string_inner(ui, uis,
240
                                    UI_get_input_flags(uis) &
241
                                    UI_INPUT_FLAG_ECHO, 1)) <= 0)
242
            return ok;
243
        if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
244
            fprintf(tty_out, "Verify failure\n");
245
            fflush(tty_out);
246
            return 0;
247
        }
248
        break;
249
    case UIT_NONE:
250
    case UIT_INFO:
251
    case UIT_ERROR:
252
        break;
253
    }
254
    return 1;
255
}
256
257
# if !defined(OPENSSL_SYS_WINCE)
258
/* Internal functions to read a string without echoing */
259
static int read_till_nl(FILE *in)
260
0
{
261
0
#  define SIZE 4
262
0
    char buf[SIZE + 1];
263
0
264
0
    do {
265
0
        if (!fgets(buf, SIZE, in))
266
0
            return 0;
267
0
    } while (strchr(buf, '\n') == NULL);
268
0
    return 1;
269
0
}
270
271
static volatile sig_atomic_t intr_signal;
272
# endif
273
274
static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
275
0
{
276
0
    static int ps;
277
0
    int ok;
278
0
    char result[BUFSIZ];
279
0
    int maxsize = BUFSIZ - 1;
280
0
# if !defined(OPENSSL_SYS_WINCE)
281
0
    char *p = NULL;
282
0
    int echo_eol = !echo;
283
0
284
0
    intr_signal = 0;
285
0
    ok = 0;
286
0
    ps = 0;
287
0
288
0
    pushsig();
289
0
    ps = 1;
290
0
291
0
    if (!echo && !noecho_console(ui))
292
0
        goto error;
293
0
    ps = 2;
294
0
295
0
    result[0] = '\0';
296
#  if defined(_WIN32)
297
    if (is_a_tty) {
298
        DWORD numread;
299
#   if defined(CP_UTF8)
300
        if (GetEnvironmentVariableW(L"OPENSSL_WIN32_UTF8", NULL, 0) != 0) {
301
            WCHAR wresult[BUFSIZ];
302
303
            if (ReadConsoleW(GetStdHandle(STD_INPUT_HANDLE),
304
                         wresult, maxsize, &numread, NULL)) {
305
                if (numread >= 2 &&
306
                    wresult[numread-2] == L'\r' &&
307
                    wresult[numread-1] == L'\n') {
308
                    wresult[numread-2] = L'\n';
309
                    numread--;
310
                }
311
                wresult[numread] = '\0';
312
                if (WideCharToMultiByte(CP_UTF8, 0, wresult, -1,
313
                                        result, sizeof(result), NULL, 0) > 0)
314
                    p = result;
315
316
                OPENSSL_cleanse(wresult, sizeof(wresult));
317
            }
318
        } else
319
#   endif
320
        if (ReadConsoleA(GetStdHandle(STD_INPUT_HANDLE),
321
                         result, maxsize, &numread, NULL)) {
322
            if (numread >= 2 &&
323
                result[numread-2] == '\r' && result[numread-1] == '\n') {
324
                result[numread-2] = '\n';
325
                numread--;
326
            }
327
            result[numread] = '\0';
328
            p = result;
329
        }
330
    } else
331
#  elif defined(OPENSSL_SYS_MSDOS)
332
    if (!echo) {
333
        noecho_fgets(result, maxsize, tty_in);
334
        p = result;             /* FIXME: noecho_fgets doesn't return errors */
335
    } else
336
#  endif
337
    p = fgets(result, maxsize, tty_in);
338
0
    if (p == NULL)
339
0
        goto error;
340
0
    if (feof(tty_in))
341
0
        goto error;
342
0
    if (ferror(tty_in))
343
0
        goto error;
344
0
    if ((p = (char *)strchr(result, '\n')) != NULL) {
345
0
        if (strip_nl)
346
0
            *p = '\0';
347
0
    } else if (!read_till_nl(tty_in))
348
0
        goto error;
349
0
    if (UI_set_result(ui, uis, result) >= 0)
350
0
        ok = 1;
351
0
352
0
 error:
353
0
    if (intr_signal == SIGINT)
354
0
        ok = -1;
355
0
    if (echo_eol)
356
0
        fprintf(tty_out, "\n");
357
0
    if (ps >= 2 && !echo && !echo_console(ui))
358
0
        ok = 0;
359
0
360
0
    if (ps >= 1)
361
0
        popsig();
362
# else
363
    ok = 1;
364
# endif
365
366
0
    OPENSSL_cleanse(result, BUFSIZ);
367
0
    return ok;
368
0
}
369
370
/* Internal functions to open, handle and close a channel to the console.  */
371
static int open_console(UI *ui)
372
0
{
373
0
    CRYPTO_THREAD_write_lock(ui->lock);
374
0
    is_a_tty = 1;
375
0
376
# if defined(OPENSSL_SYS_VXWORKS)
377
    tty_in = stdin;
378
    tty_out = stderr;
379
# elif defined(_WIN32) && !defined(_WIN32_WCE)
380
    if ((tty_out = fopen("conout$", "w")) == NULL)
381
        tty_out = stderr;
382
383
    if (GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &tty_orig)) {
384
        tty_in = stdin;
385
    } else {
386
        is_a_tty = 0;
387
        if ((tty_in = fopen("conin$", "r")) == NULL)
388
            tty_in = stdin;
389
    }
390
# else
391
#  ifdef OPENSSL_SYS_MSDOS
392
#   define DEV_TTY "con"
393
#  else
394
0
#   define DEV_TTY "/dev/tty"
395
0
#  endif
396
0
    if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
397
0
        tty_in = stdin;
398
0
    if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
399
0
        tty_out = stderr;
400
0
# endif
401
0
402
0
# if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
403
0
    if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
404
0
#  ifdef ENOTTY
405
0
        if (errno == ENOTTY)
406
0
            is_a_tty = 0;
407
0
        else
408
0
#  endif
409
0
#  ifdef EINVAL
410
0
            /*
411
0
             * Ariel Glenn reports that solaris can return EINVAL instead.
412
0
             * This should be ok
413
0
             */
414
0
        if (errno == EINVAL)
415
0
            is_a_tty = 0;
416
0
        else
417
0
#  endif
418
0
#  ifdef ENODEV
419
0
            /*
420
0
             * MacOS X returns ENODEV (Operation not supported by device),
421
0
             * which seems appropriate.
422
0
             */
423
0
        if (errno == ENODEV)
424
0
            is_a_tty = 0;
425
0
        else
426
0
#  endif
427
0
            {
428
0
                char tmp_num[10];
429
0
                BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%d", errno);
430
0
                UIerr(UI_F_OPEN_CONSOLE, UI_R_UNKNOWN_TTYGET_ERRNO_VALUE);
431
0
                ERR_add_error_data(2, "errno=", tmp_num);
432
0
433
0
                return 0;
434
0
            }
435
0
    }
436
0
# endif
437
# ifdef OPENSSL_SYS_VMS
438
    status = sys$assign(&terminal, &channel, 0, 0);
439
440
    /* if there isn't a TT device, something is very wrong */
441
    if (status != SS$_NORMAL) {
442
        char tmp_num[12];
443
444
        BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
445
        UIerr(UI_F_OPEN_CONSOLE, UI_R_SYSASSIGN_ERROR);
446
        ERR_add_error_data(2, "status=", tmp_num);
447
        return 0;
448
    }
449
450
    status = sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12,
451
                      0, 0, 0, 0);
452
453
    /* If IO$_SENSEMODE doesn't work, this is not a terminal device */
454
    if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
455
        is_a_tty = 0;
456
# endif
457
0
    return 1;
458
0
}
459
460
static int noecho_console(UI *ui)
461
0
{
462
0
# ifdef TTY_FLAGS
463
0
    memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
464
0
    tty_new.TTY_FLAGS &= ~ECHO;
465
0
# endif
466
0
467
0
# if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
468
0
    if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
469
0
        return 0;
470
0
# endif
471
# ifdef OPENSSL_SYS_VMS
472
    if (is_a_tty) {
473
        tty_new[0] = tty_orig[0];
474
        tty_new[1] = tty_orig[1] | TT$M_NOECHO;
475
        tty_new[2] = tty_orig[2];
476
        status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
477
                          0, 0, 0, 0);
478
        if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
479
            char tmp_num[2][12];
480
481
            BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
482
                         status);
483
            BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
484
                         iosb.iosb$w_value);
485
            UIerr(UI_F_NOECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
486
            ERR_add_error_data(5, "status=", tmp_num[0],
487
                               ",", "iosb.iosb$w_value=", tmp_num[1]);
488
            return 0;
489
        }
490
    }
491
# endif
492
# if defined(_WIN32) && !defined(_WIN32_WCE)
493
    if (is_a_tty) {
494
        tty_new = tty_orig;
495
        tty_new &= ~ENABLE_ECHO_INPUT;
496
        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
497
    }
498
# endif
499
0
    return 1;
500
0
}
501
502
static int echo_console(UI *ui)
503
0
{
504
0
# if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
505
0
    memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
506
0
    if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
507
0
        return 0;
508
0
# endif
509
# ifdef OPENSSL_SYS_VMS
510
    if (is_a_tty) {
511
        tty_new[0] = tty_orig[0];
512
        tty_new[1] = tty_orig[1];
513
        tty_new[2] = tty_orig[2];
514
        status = sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12,
515
                          0, 0, 0, 0);
516
        if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) {
517
            char tmp_num[2][12];
518
519
            BIO_snprintf(tmp_num[0], sizeof(tmp_num[0]) - 1, "%%X%08X",
520
                         status);
521
            BIO_snprintf(tmp_num[1], sizeof(tmp_num[1]) - 1, "%%X%08X",
522
                         iosb.iosb$w_value);
523
            UIerr(UI_F_ECHO_CONSOLE, UI_R_SYSQIOW_ERROR);
524
            ERR_add_error_data(5, "status=", tmp_num[0],
525
                               ",", "iosb.iosb$w_value=", tmp_num[1]);
526
            return 0;
527
        }
528
    }
529
# endif
530
# if defined(_WIN32) && !defined(_WIN32_WCE)
531
    if (is_a_tty) {
532
        tty_new = tty_orig;
533
        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), tty_new);
534
    }
535
# endif
536
0
    return 1;
537
0
}
538
539
static int close_console(UI *ui)
540
0
{
541
0
    if (tty_in != stdin)
542
0
        fclose(tty_in);
543
0
    if (tty_out != stderr)
544
0
        fclose(tty_out);
545
# ifdef OPENSSL_SYS_VMS
546
    status = sys$dassgn(channel);
547
    if (status != SS$_NORMAL) {
548
        char tmp_num[12];
549
550
        BIO_snprintf(tmp_num, sizeof(tmp_num) - 1, "%%X%08X", status);
551
        UIerr(UI_F_CLOSE_CONSOLE, UI_R_SYSDASSGN_ERROR);
552
        ERR_add_error_data(2, "status=", tmp_num);
553
        return 0;
554
    }
555
# endif
556
    CRYPTO_THREAD_unlock(ui->lock);
557
0
558
0
    return 1;
559
0
}
560
561
# if !defined(OPENSSL_SYS_WINCE)
562
/* Internal functions to handle signals and act on them */
563
static void pushsig(void)
564
0
{
565
0
#  ifndef OPENSSL_SYS_WIN32
566
0
    int i;
567
0
#  endif
568
0
#  ifdef SIGACTION
569
0
    struct sigaction sa;
570
0
571
0
    memset(&sa, 0, sizeof(sa));
572
0
    sa.sa_handler = recsig;
573
0
#  endif
574
0
575
#  ifdef OPENSSL_SYS_WIN32
576
    savsig[SIGABRT] = signal(SIGABRT, recsig);
577
    savsig[SIGFPE] = signal(SIGFPE, recsig);
578
    savsig[SIGILL] = signal(SIGILL, recsig);
579
    savsig[SIGINT] = signal(SIGINT, recsig);
580
    savsig[SIGSEGV] = signal(SIGSEGV, recsig);
581
    savsig[SIGTERM] = signal(SIGTERM, recsig);
582
#  else
583
0
    for (i = 1; i < NX509_SIG; i++) {
584
0
#   ifdef SIGUSR1
585
0
        if (i == SIGUSR1)
586
0
            continue;
587
0
#   endif
588
0
#   ifdef SIGUSR2
589
0
        if (i == SIGUSR2)
590
0
            continue;
591
0
#   endif
592
0
#   ifdef SIGKILL
593
0
        if (i == SIGKILL)       /* We can't make any action on that. */
594
0
            continue;
595
0
#   endif
596
0
#   ifdef SIGACTION
597
0
        sigaction(i, &sa, &savsig[i]);
598
#   else
599
        savsig[i] = signal(i, recsig);
600
#   endif
601
    }
602
0
#  endif
603
0
604
0
#  ifdef SIGWINCH
605
0
    signal(SIGWINCH, SIG_DFL);
606
0
#  endif
607
0
}
608
609
static void popsig(void)
610
0
{
611
#  ifdef OPENSSL_SYS_WIN32
612
    signal(SIGABRT, savsig[SIGABRT]);
613
    signal(SIGFPE, savsig[SIGFPE]);
614
    signal(SIGILL, savsig[SIGILL]);
615
    signal(SIGINT, savsig[SIGINT]);
616
    signal(SIGSEGV, savsig[SIGSEGV]);
617
    signal(SIGTERM, savsig[SIGTERM]);
618
#  else
619
    int i;
620
0
    for (i = 1; i < NX509_SIG; i++) {
621
0
#   ifdef SIGUSR1
622
0
        if (i == SIGUSR1)
623
0
            continue;
624
0
#   endif
625
0
#   ifdef SIGUSR2
626
0
        if (i == SIGUSR2)
627
0
            continue;
628
0
#   endif
629
0
#   ifdef SIGACTION
630
0
        sigaction(i, &savsig[i], NULL);
631
#   else
632
        signal(i, savsig[i]);
633
#   endif
634
    }
635
0
#  endif
636
0
}
637
638
static void recsig(int i)
639
0
{
640
0
    intr_signal = i;
641
0
}
642
# endif
643
644
/* Internal functions specific for Windows */
645
# if defined(OPENSSL_SYS_MSDOS) && !defined(_WIN32)
646
static int noecho_fgets(char *buf, int size, FILE *tty)
647
{
648
    int i;
649
    char *p;
650
651
    p = buf;
652
    for (;;) {
653
        if (size == 0) {
654
            *p = '\0';
655
            break;
656
        }
657
        size--;
658
#  if defined(_WIN32)
659
        i = _getch();
660
#  else
661
        i = getch();
662
#  endif
663
        if (i == '\r')
664
            i = '\n';
665
        *(p++) = i;
666
        if (i == '\n') {
667
            *p = '\0';
668
            break;
669
        }
670
    }
671
#  ifdef WIN_CONSOLE_BUG
672
    /*
673
     * Win95 has several evil console bugs: one of these is that the last
674
     * character read using getch() is passed to the next read: this is
675
     * usually a CR so this can be trouble. No STDIO fix seems to work but
676
     * flushing the console appears to do the trick.
677
     */
678
    {
679
        HANDLE inh;
680
        inh = GetStdHandle(STD_INPUT_HANDLE);
681
        FlushConsoleInputBuffer(inh);
682
    }
683
#  endif
684
    return strlen(buf);
685
}
686
# endif
687
688
static UI_METHOD ui_openssl = {
689
    "OpenSSL default user interface",
690
    open_console,
691
    write_string,
692
    NULL,                       /* No flusher is needed for command lines */
693
    read_string,
694
    close_console,
695
    NULL
696
};
697
698
/* The method with all the built-in console thingies */
699
UI_METHOD *UI_OpenSSL(void)
700
0
{
701
0
    return &ui_openssl;
702
0
}
703
704
static const UI_METHOD *default_UI_meth = &ui_openssl;
705
706
#else
707
708
static const UI_METHOD *default_UI_meth = NULL;
709
710
#endif
711
712
void UI_set_default_method(const UI_METHOD *meth)
713
0
{
714
0
    default_UI_meth = meth;
715
0
}
716
717
const UI_METHOD *UI_get_default_method(void)
718
0
{
719
0
    return default_UI_meth;
720
0
}