]> git.ipfire.org Git - thirdparty/openssl.git/blob - crypto/ui/ui_openssl.c
Copyright consolidation 05/10
[thirdparty/openssl.git] / crypto / ui / ui_openssl.c
1 /*
2 * Copyright 2001-2016 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 <openssl/e_os2.h>
11
12 /*
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.
17 */
18 #if defined(OPENSSL_SYS_VXWORKS)
19 # include <sys/types.h>
20 #endif
21
22 #if !defined(_POSIX_C_SOURCE) && defined(OPENSSL_SYS_VMS)
23 # ifndef _POSIX_C_SOURCE
24 # define _POSIX_C_SOURCE 2
25 # endif
26 #endif
27 #include <signal.h>
28 #include <stdio.h>
29 #include <string.h>
30 #include <errno.h>
31
32 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
33 # ifdef OPENSSL_UNISTD
34 # include OPENSSL_UNISTD
35 # else
36 # include <unistd.h>
37 # endif
38 /*
39 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
40 * system and have sigaction and termios.
41 */
42 # if defined(_POSIX_VERSION)
43
44 # define SIGACTION
45 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
46 # define TERMIOS
47 # endif
48
49 # endif
50 #endif
51
52 /* 06-Apr-92 Luke Brennan Support for VMS */
53 #include "ui_locl.h"
54 #include "internal/cryptlib.h"
55
56 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
57 # include <starlet.h>
58 # ifdef __DECC
59 # pragma message disable DOLLARID
60 # endif
61 #endif
62
63 #ifdef WIN_CONSOLE_BUG
64 # include <windows.h>
65 # ifndef OPENSSL_SYS_WINCE
66 # include <wincon.h>
67 # endif
68 #endif
69
70 /*
71 * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
72 * MSDOS and SGTTY.
73 *
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.
77 *
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.
80 */
81
82 #if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
83
84 # if defined(_LIBC)
85 # undef TERMIOS
86 # define TERMIO
87 # undef SGTTY
88 /*
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.
91 */
92 # elif !defined(OPENSSL_SYS_VMS) \
93 && !defined(OPENSSL_SYS_MSDOS) \
94 && !defined(MAC_OS_GUSI_SOURCE) \
95 && !defined(OPENSSL_SYS_VXWORKS)
96 # define TERMIOS
97 # undef TERMIO
98 # undef SGTTY
99 # endif
100
101 #endif
102
103 #ifdef TERMIOS
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)
109 #endif
110
111 #ifdef TERMIO
112 # include <termio.h>
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)
117 #endif
118
119 #ifdef SGTTY
120 # include <sgtty.h>
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)
125 #endif
126
127 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
128 # include <sys/ioctl.h>
129 #endif
130
131 #ifdef OPENSSL_SYS_MSDOS
132 # include <conio.h>
133 #endif
134
135 #ifdef OPENSSL_SYS_VMS
136 # include <ssdef.h>
137 # include <iodef.h>
138 # include <ttdef.h>
139 # include <descrip.h>
140 struct IOSB {
141 short iosb$w_value;
142 short iosb$w_count;
143 long iosb$l_info;
144 };
145 #endif
146
147 #if defined(MAC_OS_GUSI_SOURCE)
148 /*
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>
152 */
153 # define TTY_STRUCT int
154 #endif
155
156 #ifndef NX509_SIG
157 # define NX509_SIG 32
158 #endif
159
160 /* Define globals. They are protected by a lock */
161 #ifdef SIGACTION
162 static struct sigaction savsig[NX509_SIG];
163 #else
164 static void (*savsig[NX509_SIG]) (int);
165 #endif
166
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
172 * structures? */
173 static long status;
174 static unsigned short channel = 0;
175 #else
176 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
177 static TTY_STRUCT tty_orig, tty_new;
178 # endif
179 #endif
180 static FILE *tty_in, *tty_out;
181 static int is_a_tty;
182
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);
189 #endif
190 #if defined(OPENSSL_SYS_MSDOS)
191 static int noecho_fgets(char *buf, int size, FILE *tty);
192 #endif
193 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
194
195 static int read_string(UI *ui, UI_STRING *uis);
196 static int write_string(UI *ui, UI_STRING *uis);
197
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);
202
203 static UI_METHOD ui_openssl = {
204 "OpenSSL default user interface",
205 open_console,
206 write_string,
207 NULL, /* No flusher is needed for command lines */
208 read_string,
209 close_console,
210 NULL
211 };
212
213 /* The method with all the built-in thingies */
214 UI_METHOD *UI_OpenSSL(void)
215 {
216 return &ui_openssl;
217 }
218
219 /*
220 * The following function makes sure that info and error strings are printed
221 * before any prompt.
222 */
223 static int write_string(UI *ui, UI_STRING *uis)
224 {
225 switch (UI_get_string_type(uis)) {
226 case UIT_ERROR:
227 case UIT_INFO:
228 fputs(UI_get0_output_string(uis), tty_out);
229 fflush(tty_out);
230 break;
231 default:
232 break;
233 }
234 return 1;
235 }
236
237 static int read_string(UI *ui, UI_STRING *uis)
238 {
239 int ok = 0;
240
241 switch (UI_get_string_type(uis)) {
242 case UIT_BOOLEAN:
243 fputs(UI_get0_output_string(uis), tty_out);
244 fputs(UI_get0_action_string(uis), tty_out);
245 fflush(tty_out);
246 return read_string_inner(ui, uis,
247 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
248 0);
249 case UIT_PROMPT:
250 fputs(UI_get0_output_string(uis), tty_out);
251 fflush(tty_out);
252 return read_string_inner(ui, uis,
253 UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO,
254 1);
255 case UIT_VERIFY:
256 fprintf(tty_out, "Verifying - %s", UI_get0_output_string(uis));
257 fflush(tty_out);
258 if ((ok = read_string_inner(ui, uis,
259 UI_get_input_flags(uis) &
260 UI_INPUT_FLAG_ECHO, 1)) <= 0)
261 return ok;
262 if (strcmp(UI_get0_result_string(uis), UI_get0_test_string(uis)) != 0) {
263 fprintf(tty_out, "Verify failure\n");
264 fflush(tty_out);
265 return 0;
266 }
267 break;
268 default:
269 break;
270 }
271 return 1;
272 }
273
274 #if !defined(OPENSSL_SYS_WINCE)
275 /* Internal functions to read a string without echoing */
276 static int read_till_nl(FILE *in)
277 {
278 # define SIZE 4
279 char buf[SIZE + 1];
280
281 do {
282 if (!fgets(buf, SIZE, in))
283 return 0;
284 } while (strchr(buf, '\n') == NULL);
285 return 1;
286 }
287
288 static volatile sig_atomic_t intr_signal;
289 #endif
290
291 static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
292 {
293 static int ps;
294 int ok;
295 char result[BUFSIZ];
296 int maxsize = BUFSIZ - 1;
297 #if !defined(OPENSSL_SYS_WINCE)
298 char *p;
299
300 intr_signal = 0;
301 ok = 0;
302 ps = 0;
303
304 pushsig();
305 ps = 1;
306
307 if (!echo && !noecho_console(ui))
308 goto error;
309 ps = 2;
310
311 result[0] = '\0';
312 # ifdef OPENSSL_SYS_MSDOS
313 if (!echo) {
314 noecho_fgets(result, maxsize, tty_in);
315 p = result; /* FIXME: noecho_fgets doesn't return errors */
316 } else
317 p = fgets(result, maxsize, tty_in);
318 # else
319 p = fgets(result, maxsize, tty_in);
320 # endif
321 if (!p)
322 goto error;
323 if (feof(tty_in))
324 goto error;
325 if (ferror(tty_in))
326 goto error;
327 if ((p = (char *)strchr(result, '\n')) != NULL) {
328 if (strip_nl)
329 *p = '\0';
330 } else if (!read_till_nl(tty_in))
331 goto error;
332 if (UI_set_result(ui, uis, result) >= 0)
333 ok = 1;
334
335 error:
336 if (intr_signal == SIGINT)
337 ok = -1;
338 if (!echo)
339 fprintf(tty_out, "\n");
340 if (ps >= 2 && !echo && !echo_console(ui))
341 ok = 0;
342
343 if (ps >= 1)
344 popsig();
345 #else
346 ok = 1;
347 #endif
348
349 OPENSSL_cleanse(result, BUFSIZ);
350 return ok;
351 }
352
353 /* Internal functions to open, handle and close a channel to the console. */
354 static int open_console(UI *ui)
355 {
356 CRYPTO_THREAD_write_lock(ui->lock);
357 is_a_tty = 1;
358
359 #if defined(OPENSSL_SYS_VXWORKS)
360 tty_in = stdin;
361 tty_out = stderr;
362 #else
363 # ifdef OPENSSL_SYS_MSDOS
364 # define DEV_TTY "con"
365 # else
366 # define DEV_TTY "/dev/tty"
367 # endif
368 if ((tty_in = fopen(DEV_TTY, "r")) == NULL)
369 tty_in = stdin;
370 if ((tty_out = fopen(DEV_TTY, "w")) == NULL)
371 tty_out = stderr;
372 #endif
373
374 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
375 if (TTY_get(fileno(tty_in), &tty_orig) == -1) {
376 # ifdef ENOTTY
377 if (errno == ENOTTY)
378 is_a_tty = 0;
379 else
380 # endif
381 # ifdef EINVAL
382 /*
383 * Ariel Glenn ariel@columbia.edu reports that solaris can return
384 * EINVAL instead. This should be ok
385 */
386 if (errno == EINVAL)
387 is_a_tty = 0;
388 else
389 # endif
390 return 0;
391 }
392 #endif
393 #ifdef OPENSSL_SYS_VMS
394 status = sys$assign(&terminal, &channel, 0, 0);
395 if (status != SS$_NORMAL)
396 return 0;
397 status =
398 sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
399 0, 0);
400 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
401 return 0;
402 #endif
403 return 1;
404 }
405
406 static int noecho_console(UI *ui)
407 {
408 #ifdef TTY_FLAGS
409 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
410 tty_new.TTY_FLAGS &= ~ECHO;
411 #endif
412
413 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
414 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
415 return 0;
416 #endif
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];
421 status =
422 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
423 0);
424 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
425 return 0;
426 #endif
427 return 1;
428 }
429
430 static int echo_console(UI *ui)
431 {
432 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
433 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
434 tty_new.TTY_FLAGS |= ECHO;
435 #endif
436
437 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
438 if (is_a_tty && (TTY_set(fileno(tty_in), &tty_new) == -1))
439 return 0;
440 #endif
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];
445 status =
446 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
447 0);
448 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
449 return 0;
450 #endif
451 return 1;
452 }
453
454 static int close_console(UI *ui)
455 {
456 if (tty_in != stdin)
457 fclose(tty_in);
458 if (tty_out != stderr)
459 fclose(tty_out);
460 #ifdef OPENSSL_SYS_VMS
461 status = sys$dassgn(channel);
462 #endif
463 CRYPTO_THREAD_unlock(ui->lock);
464
465 return 1;
466 }
467
468 #if !defined(OPENSSL_SYS_WINCE)
469 /* Internal functions to handle signals and act on them */
470 static void pushsig(void)
471 {
472 # ifndef OPENSSL_SYS_WIN32
473 int i;
474 # endif
475 # ifdef SIGACTION
476 struct sigaction sa;
477
478 memset(&sa, 0, sizeof(sa));
479 sa.sa_handler = recsig;
480 # endif
481
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);
489 # else
490 for (i = 1; i < NX509_SIG; i++) {
491 # ifdef SIGUSR1
492 if (i == SIGUSR1)
493 continue;
494 # endif
495 # ifdef SIGUSR2
496 if (i == SIGUSR2)
497 continue;
498 # endif
499 # ifdef SIGKILL
500 if (i == SIGKILL) /* We can't make any action on that. */
501 continue;
502 # endif
503 # ifdef SIGACTION
504 sigaction(i, &sa, &savsig[i]);
505 # else
506 savsig[i] = signal(i, recsig);
507 # endif
508 }
509 # endif
510
511 # ifdef SIGWINCH
512 signal(SIGWINCH, SIG_DFL);
513 # endif
514 }
515
516 static void popsig(void)
517 {
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]);
525 # else
526 int i;
527 for (i = 1; i < NX509_SIG; i++) {
528 # ifdef SIGUSR1
529 if (i == SIGUSR1)
530 continue;
531 # endif
532 # ifdef SIGUSR2
533 if (i == SIGUSR2)
534 continue;
535 # endif
536 # ifdef SIGACTION
537 sigaction(i, &savsig[i], NULL);
538 # else
539 signal(i, savsig[i]);
540 # endif
541 }
542 # endif
543 }
544
545 static void recsig(int i)
546 {
547 intr_signal = i;
548 }
549 #endif
550
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)
554 {
555 int i;
556 char *p;
557
558 p = buf;
559 for (;;) {
560 if (size == 0) {
561 *p = '\0';
562 break;
563 }
564 size--;
565 # if defined(_WIN32)
566 i = _getch();
567 # else
568 i = getch();
569 # endif
570 if (i == '\r')
571 i = '\n';
572 *(p++) = i;
573 if (i == '\n') {
574 *p = '\0';
575 break;
576 }
577 }
578 # ifdef WIN_CONSOLE_BUG
579 /*
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.
584 */
585 {
586 HANDLE inh;
587 inh = GetStdHandle(STD_INPUT_HANDLE);
588 FlushConsoleInputBuffer(inh);
589 }
590 # endif
591 return (strlen(buf));
592 }
593 #endif