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