2 * Copyright 2001-2016 The OpenSSL Project Authors. All Rights Reserved.
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
10 #include <openssl/e_os2.h>
13 * need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
14 * [maybe others?], because it masks interfaces not discussed in standard,
15 * sigaction and fileno included. -pedantic would be more appropriate for the
16 * intended purposes, but we can't prevent users from adding -ansi.
18 #if defined(OPENSSL_SYS_VXWORKS)
19 # include <sys/types.h>
22 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
23 # ifndef _POSIX_C_SOURCE
24 # define _POSIX_C_SOURCE 2
32 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
33 # ifdef OPENSSL_UNISTD
34 # include OPENSSL_UNISTD
39 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
40 * system and have sigaction and termios.
42 # if defined(_POSIX_VERSION)
45 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
52 /* 06-Apr-92 Luke Brennan Support for VMS */
54 #include "internal/cryptlib.h"
56 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
59 # pragma message disable DOLLARID
63 #ifdef WIN_CONSOLE_BUG
65 # ifndef OPENSSL_SYS_WINCE
71 * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
74 * If someone defines one of the macros TERMIO, TERMIOS or SGTTY, it will
75 * remain respected. Otherwise, we default to TERMIOS except for a few
76 * systems that require something different.
78 * Note: we do not use SGTTY unless it's defined by the configuration. We
79 * may eventually opt to remove it's use entirely.
82 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
89 * We know that VMS, MSDOS, VXWORKS, use entirely other mechanisms.
90 * MAC_OS_GUSI_SOURCE should probably go away, but that needs to be confirmed.
92 # elif !defined(OPENSSL_SYS_VMS) \
93 && !defined(OPENSSL_SYS_MSDOS) \
94 && !defined(MAC_OS_GUSI_SOURCE) \
95 && !defined(OPENSSL_SYS_VXWORKS)
104 # include <termios.h>
105 # define TTY_STRUCT struct termios
106 # define TTY_FLAGS c_lflag
107 # define TTY_get(tty,data) tcgetattr(tty,data)
108 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
113 # define TTY_STRUCT struct termio
114 # define TTY_FLAGS c_lflag
115 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
116 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
121 # define TTY_STRUCT struct sgttyb
122 # define TTY_FLAGS sg_flags
123 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
124 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
127 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
128 # include <sys/ioctl.h>
131 #ifdef OPENSSL_SYS_MSDOS
135 #ifdef OPENSSL_SYS_VMS
139 # include <descrip.h>
147 #if defined(MAC_OS_GUSI_SOURCE)
149 * This one needs work. As a matter of fact the code is unoperational
150 * and this is only a trick to get it compiled.
151 * <appro@fy.chalmers.se>
153 # define TTY_STRUCT int
157 # define NX509_SIG 32
160 /* Define globals. They are protected by a lock */
162 static struct sigaction savsig[NX509_SIG];
164 static void (*savsig[NX509_SIG]) (int);
167 #ifdef OPENSSL_SYS_VMS
168 static struct IOSB iosb;
169 static $DESCRIPTOR(terminal, "TT");
170 static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this
171 * will always suffice for the actual
174 static unsigned short channel = 0;
176 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
177 static TTY_STRUCT tty_orig, tty_new;
180 static FILE *tty_in, *tty_out;
183 /* Declare static functions */
184 #if !defined(OPENSSL_SYS_WINCE)
185 static int read_till_nl(FILE *);
186 static void recsig(int);
187 static void pushsig(void);
188 static void popsig(void);
190 #if defined(OPENSSL_SYS_MSDOS)
191 static int noecho_fgets(char *buf, int size, FILE *tty);
193 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
195 static int read_string(UI *ui, UI_STRING *uis);
196 static int write_string(UI *ui, UI_STRING *uis);
198 static int open_console(UI *ui);
199 static int echo_console(UI *ui);
200 static int noecho_console(UI *ui);
201 static int close_console(UI *ui);
203 static UI_METHOD ui_openssl = {
204 "OpenSSL default user interface",
207 NULL, /* No flusher is needed for command lines */
213 /* The method with all the built-in thingies */
214 UI_METHOD *UI_OpenSSL(void)
220 * The following function makes sure that info and error strings are printed
223 static int write_string(UI *ui, UI_STRING *uis)
225 switch (UI_get_string_type(uis)) {
228 fputs(UI_get0_output_string(uis), tty_out);
237 static int read_string(UI *ui, UI_STRING *uis)
241 switch (UI_get_string_type(uis)) {
243 fputs(UI_get0_output_string(uis), tty_out);
244 fputs(UI_get0_action_string(uis), tty_out);
246 return read_string_inner(ui, uis,
247 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
250 fputs(UI_get0_output_string(uis), tty_out);
252 return read_string_inner(ui, uis,
253 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
256 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
258 if ((ok = read_string_inner(ui, uis,
259 UI_get_input_flags(uis) &
260 UI_INPUT_FLAG_ECHO, 1)) <= 0)
262 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
263 fprintf(tty_out, "Verify failure\n");
274 #if !defined(OPENSSL_SYS_WINCE)
275 /* Internal functions to read a string without echoing */
276 static int read_till_nl(FILE *in)
282 if (!fgets(buf, SIZE, in))
284 } while (strchr(buf, '\n') == NULL);
288 static volatile sig_atomic_t intr_signal;
291 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
296 int maxsize = BUFSIZ - 1;
297 #if !defined(OPENSSL_SYS_WINCE)
307 if (!echo && !noecho_console(ui))
312 # ifdef OPENSSL_SYS_MSDOS
314 noecho_fgets(result, maxsize, tty_in);
315 p = result; /* FIXME: noecho_fgets doesn't return errors */
317 p = fgets(result, maxsize, tty_in);
319 p = fgets(result, maxsize, tty_in);
327 if ((p = (char *)strchr(result, '\n')) != NULL) {
330 } else if (!read_till_nl(tty_in))
332 if (UI_set_result(ui, uis, result) >= 0)
336 if (intr_signal == SIGINT)
339 fprintf(tty_out, "\n");
340 if (ps >= 2 && !echo && !echo_console(ui))
349 OPENSSL_cleanse(result, BUFSIZ);
353 /* Internal functions to open, handle and close a channel to the console. */
354 static int open_console(UI *ui)
356 CRYPTO_THREAD_write_lock(ui->lock);
359 #if defined(OPENSSL_SYS_VXWORKS)
363 # ifdef OPENSSL_SYS_MSDOS
364 # define DEV_TTY "con"
366 # define DEV_TTY "/dev/tty"
368 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
370 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
374 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
375 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
383 * Ariel Glenn ariel@columbia.edu reports that solaris can return
384 * EINVAL instead. This should be ok
393 #ifdef OPENSSL_SYS_VMS
394 status = sys$assign(&terminal, &channel, 0, 0);
395 if (status != SS$_NORMAL)
398 sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
400 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
406 static int noecho_console(UI *ui)
409 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
410 tty_new.TTY_FLAGS &= ~ECHO;
413 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
414 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
417 #ifdef OPENSSL_SYS_VMS
418 tty_new[0] = tty_orig[0];
419 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
420 tty_new[2] = tty_orig[2];
422 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
424 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
430 static int echo_console(UI *ui)
432 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
433 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
434 tty_new.TTY_FLAGS |= ECHO;
437 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
438 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
441 #ifdef OPENSSL_SYS_VMS
442 tty_new[0] = tty_orig[0];
443 tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
444 tty_new[2] = tty_orig[2];
446 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
448 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
454 static int close_console(UI *ui)
458 if (tty_out != stderr)
460 #ifdef OPENSSL_SYS_VMS
461 status = sys$dassgn(channel);
463 CRYPTO_THREAD_unlock(ui->lock);
468 #if !defined(OPENSSL_SYS_WINCE)
469 /* Internal functions to handle signals and act on them */
470 static void pushsig(void)
472 # ifndef OPENSSL_SYS_WIN32
478 memset(&sa, 0, sizeof(sa));
479 sa.sa_handler = recsig;
482 # ifdef OPENSSL_SYS_WIN32
483 savsig[SIGABRT] = signal(SIGABRT, recsig);
484 savsig[SIGFPE] = signal(SIGFPE, recsig);
485 savsig[SIGILL] = signal(SIGILL, recsig);
486 savsig[SIGINT] = signal(SIGINT, recsig);
487 savsig[SIGSEGV] = signal(SIGSEGV, recsig);
488 savsig[SIGTERM] = signal(SIGTERM, recsig);
490 for (i = 1; i < NX509_SIG; i++) {
500 if (i == SIGKILL) /* We can't make any action on that. */
504 sigaction(i, &sa, &savsig[i]);
506 savsig[i] = signal(i, recsig);
512 signal(SIGWINCH, SIG_DFL);
516 static void popsig(void)
518 # ifdef OPENSSL_SYS_WIN32
519 signal(SIGABRT, savsig[SIGABRT]);
520 signal(SIGFPE, savsig[SIGFPE]);
521 signal(SIGILL, savsig[SIGILL]);
522 signal(SIGINT, savsig[SIGINT]);
523 signal(SIGSEGV, savsig[SIGSEGV]);
524 signal(SIGTERM, savsig[SIGTERM]);
527 for (i = 1; i < NX509_SIG; i++) {
537 sigaction(i, &savsig[i], NULL);
539 signal(i, savsig[i]);
545 static void recsig(int i)
551 /* Internal functions specific for Windows */
552 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WINCE)
553 static int noecho_fgets(char *buf, int size, FILE *tty)
578 # ifdef WIN_CONSOLE_BUG
580 * Win95 has several evil console bugs: one of these is that the last
581 * character read using getch() is passed to the next read: this is
582 * usually a CR so this can be trouble. No STDIO fix seems to work but
583 * flushing the console appears to do the trick.
587 inh = GetStdHandle(STD_INPUT_HANDLE);
588 FlushConsoleInputBuffer(inh);
591 return (strlen(buf));