]>
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 | 119 | |
2ee67f1d RL |
120 | #define _POSIX_C_SOURCE 1 |
121 | #include <signal.h> | |
122 | #include <stdio.h> | |
123 | #undef _POSIX_C_SOURCE | |
124 | #include <string.h> | |
125 | #include <errno.h> | |
126 | ||
02790299 RL |
127 | #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) |
128 | # ifdef OPENSSL_UNISTD | |
129 | # include OPENSSL_UNISTD | |
130 | # else | |
131 | # include <unistd.h> | |
132 | # endif | |
a63d5eaa RL |
133 | /* If unistd.h defines _POSIX_VERSION, we conclude that we |
134 | * are on a POSIX system and have sigaction and termios. */ | |
02790299 | 135 | # if defined(_POSIX_VERSION) |
a63d5eaa | 136 | |
02790299 RL |
137 | # define SIGACTION |
138 | # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY) | |
139 | # define TERMIOS | |
140 | # endif | |
a63d5eaa | 141 | |
02790299 | 142 | # endif |
a63d5eaa | 143 | #endif |
a63d5eaa RL |
144 | |
145 | #ifdef WIN16TTY | |
02790299 RL |
146 | # undef OPENSSL_SYS_WIN16 |
147 | # undef WIN16 | |
148 | # undef _WINDOWS | |
149 | # include <graph.h> | |
a63d5eaa RL |
150 | #endif |
151 | ||
152 | /* 06-Apr-92 Luke Brennan Support for VMS */ | |
153 | #include "ui_locl.h" | |
154 | #include "cryptlib.h" | |
a63d5eaa | 155 | |
02790299 RL |
156 | #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */ |
157 | # include <starlet.h> | |
158 | # ifdef __DECC | |
159 | # pragma message disable DOLLARID | |
160 | # endif | |
a63d5eaa RL |
161 | #endif |
162 | ||
163 | #ifdef WIN_CONSOLE_BUG | |
02790299 | 164 | # include <windows.h> |
0bf23d9b | 165 | #ifndef OPENSSL_SYS_WINCE |
02790299 | 166 | # include <wincon.h> |
a63d5eaa | 167 | #endif |
0bf23d9b | 168 | #endif |
a63d5eaa RL |
169 | |
170 | ||
171 | /* There are 5 types of terminal interface supported, | |
172 | * TERMIO, TERMIOS, VMS, MSDOS and SGTTY | |
173 | */ | |
174 | ||
175 | #if defined(__sgi) && !defined(TERMIOS) | |
02790299 RL |
176 | # define TERMIOS |
177 | # undef TERMIO | |
178 | # undef SGTTY | |
a63d5eaa RL |
179 | #endif |
180 | ||
181 | #if defined(linux) && !defined(TERMIO) | |
02790299 RL |
182 | # undef TERMIOS |
183 | # define TERMIO | |
184 | # undef SGTTY | |
a63d5eaa RL |
185 | #endif |
186 | ||
187 | #ifdef _LIBC | |
02790299 RL |
188 | # undef TERMIOS |
189 | # define TERMIO | |
190 | # undef SGTTY | |
a63d5eaa RL |
191 | #endif |
192 | ||
02790299 RL |
193 | #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE) |
194 | # undef TERMIOS | |
195 | # undef TERMIO | |
196 | # define SGTTY | |
a63d5eaa RL |
197 | #endif |
198 | ||
6a89a25c | 199 | #if defined(OPENSSL_SYS_VXWORKS) |
3e83e686 RL |
200 | #undef TERMIOS |
201 | #undef TERMIO | |
202 | #undef SGTTY | |
203 | #endif | |
204 | ||
a63d5eaa | 205 | #ifdef TERMIOS |
02790299 RL |
206 | # include <termios.h> |
207 | # define TTY_STRUCT struct termios | |
208 | # define TTY_FLAGS c_lflag | |
209 | # define TTY_get(tty,data) tcgetattr(tty,data) | |
210 | # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data) | |
a63d5eaa RL |
211 | #endif |
212 | ||
213 | #ifdef TERMIO | |
02790299 RL |
214 | # include <termio.h> |
215 | # define TTY_STRUCT struct termio | |
216 | # define TTY_FLAGS c_lflag | |
217 | # define TTY_get(tty,data) ioctl(tty,TCGETA,data) | |
218 | # define TTY_set(tty,data) ioctl(tty,TCSETA,data) | |
a63d5eaa RL |
219 | #endif |
220 | ||
221 | #ifdef SGTTY | |
02790299 RL |
222 | # include <sgtty.h> |
223 | # define TTY_STRUCT struct sgttyb | |
224 | # define TTY_FLAGS sg_flags | |
225 | # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data) | |
226 | # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data) | |
a63d5eaa RL |
227 | #endif |
228 | ||
0709385d | 229 | #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS) |
02790299 | 230 | # include <sys/ioctl.h> |
a63d5eaa RL |
231 | #endif |
232 | ||
02790299 RL |
233 | #ifdef OPENSSL_SYS_MSDOS |
234 | # include <conio.h> | |
a63d5eaa RL |
235 | #endif |
236 | ||
02790299 RL |
237 | #ifdef OPENSSL_SYS_VMS |
238 | # include <ssdef.h> | |
239 | # include <iodef.h> | |
240 | # include <ttdef.h> | |
241 | # include <descrip.h> | |
a63d5eaa RL |
242 | struct IOSB { |
243 | short iosb$w_value; | |
244 | short iosb$w_count; | |
245 | long iosb$l_info; | |
246 | }; | |
247 | #endif | |
248 | ||
0709385d RL |
249 | #ifdef OPENSSL_SYS_SUNOS |
250 | typedef int sig_atomic_t; | |
251 | #endif | |
252 | ||
02790299 | 253 | #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) |
a63d5eaa RL |
254 | /* |
255 | * This one needs work. As a matter of fact the code is unoperational | |
256 | * and this is only a trick to get it compiled. | |
257 | * <appro@fy.chalmers.se> | |
258 | */ | |
02790299 | 259 | # define TTY_STRUCT int |
a63d5eaa RL |
260 | #endif |
261 | ||
262 | #ifndef NX509_SIG | |
02790299 | 263 | # define NX509_SIG 32 |
a63d5eaa RL |
264 | #endif |
265 | ||
266 | ||
267 | /* Define globals. They are protected by a lock */ | |
268 | #ifdef SIGACTION | |
269 | static struct sigaction savsig[NX509_SIG]; | |
270 | #else | |
271 | static void (*savsig[NX509_SIG])(int ); | |
272 | #endif | |
a63d5eaa | 273 | |
02790299 | 274 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
275 | static struct IOSB iosb; |
276 | static $DESCRIPTOR(terminal,"TT"); | |
200bc9e3 | 277 | static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this will always suffice for the actual structures? */ |
a63d5eaa RL |
278 | static long status; |
279 | static unsigned short channel = 0; | |
280 | #else | |
451dc18f | 281 | #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__) |
a63d5eaa RL |
282 | static TTY_STRUCT tty_orig,tty_new; |
283 | #endif | |
284 | #endif | |
200bc9e3 | 285 | static FILE *tty_in, *tty_out; |
a63d5eaa RL |
286 | static int is_a_tty; |
287 | ||
288 | /* Declare static functions */ | |
0bf23d9b | 289 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
290 | static void read_till_nl(FILE *); |
291 | static void recsig(int); | |
292 | static void pushsig(void); | |
293 | static void popsig(void); | |
0bf23d9b | 294 | #endif |
29fb08c2 | 295 | #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) |
a63d5eaa RL |
296 | static int noecho_fgets(char *buf, int size, FILE *tty); |
297 | #endif | |
b589977b | 298 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl); |
a63d5eaa RL |
299 | |
300 | static int read_string(UI *ui, UI_STRING *uis); | |
9ad0f681 | 301 | static int write_string(UI *ui, UI_STRING *uis); |
a63d5eaa RL |
302 | |
303 | static int open_console(UI *ui); | |
304 | static int echo_console(UI *ui); | |
305 | static int noecho_console(UI *ui); | |
306 | static int close_console(UI *ui); | |
307 | ||
308 | static UI_METHOD ui_openssl = | |
309 | { | |
310 | "OpenSSL default user interface", | |
311 | open_console, | |
9ad0f681 RL |
312 | write_string, |
313 | NULL, /* No flusher is needed for command lines */ | |
a63d5eaa | 314 | read_string, |
a63d5eaa | 315 | close_console, |
9ad0f681 | 316 | NULL |
a63d5eaa RL |
317 | }; |
318 | ||
319 | /* The method with all the built-in thingies */ | |
320 | UI_METHOD *UI_OpenSSL(void) | |
321 | { | |
322 | return &ui_openssl; | |
323 | } | |
324 | ||
9ad0f681 RL |
325 | /* The following function makes sure that info and error strings are printed |
326 | before any prompt. */ | |
327 | static int write_string(UI *ui, UI_STRING *uis) | |
a63d5eaa RL |
328 | { |
329 | switch (UI_get_string_type(uis)) | |
330 | { | |
b589977b RL |
331 | case UIT_ERROR: |
332 | case UIT_INFO: | |
9ad0f681 | 333 | fputs(UI_get0_output_string(uis), tty_out); |
200bc9e3 | 334 | fflush(tty_out); |
a63d5eaa | 335 | break; |
b589977b RL |
336 | default: |
337 | break; | |
9ad0f681 RL |
338 | } |
339 | return 1; | |
340 | } | |
341 | ||
342 | static int read_string(UI *ui, UI_STRING *uis) | |
343 | { | |
344 | int ok = 0; | |
345 | ||
346 | switch (UI_get_string_type(uis)) | |
347 | { | |
b589977b RL |
348 | case UIT_BOOLEAN: |
349 | fputs(UI_get0_output_string(uis), tty_out); | |
350 | fputs(UI_get0_action_string(uis), tty_out); | |
351 | fflush(tty_out); | |
352 | return read_string_inner(ui, uis, | |
353 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0); | |
9ad0f681 RL |
354 | case UIT_PROMPT: |
355 | fputs(UI_get0_output_string(uis), tty_out); | |
356 | fflush(tty_out); | |
357 | return read_string_inner(ui, uis, | |
b589977b | 358 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1); |
9ad0f681 | 359 | case UIT_VERIFY: |
200bc9e3 | 360 | fprintf(tty_out,"Verifying - %s", |
a63d5eaa | 361 | UI_get0_output_string(uis)); |
200bc9e3 | 362 | fflush(tty_out); |
9ad0f681 | 363 | if ((ok = read_string_inner(ui, uis, |
b589977b | 364 | UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0) |
9ad0f681 | 365 | return ok; |
a63d5eaa RL |
366 | if (strcmp(UI_get0_result_string(uis), |
367 | UI_get0_test_string(uis)) != 0) | |
368 | { | |
200bc9e3 BM |
369 | fprintf(tty_out,"Verify failure\n"); |
370 | fflush(tty_out); | |
a63d5eaa RL |
371 | return 0; |
372 | } | |
373 | break; | |
a63d5eaa | 374 | default: |
a63d5eaa RL |
375 | break; |
376 | } | |
377 | return 1; | |
378 | } | |
379 | ||
380 | ||
0bf23d9b | 381 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
382 | /* Internal functions to read a string without echoing */ |
383 | static void read_till_nl(FILE *in) | |
384 | { | |
385 | #define SIZE 4 | |
386 | char buf[SIZE+1]; | |
387 | ||
388 | do { | |
389 | fgets(buf,SIZE,in); | |
390 | } while (strchr(buf,'\n') == NULL); | |
391 | } | |
392 | ||
94960c84 | 393 | static volatile sig_atomic_t intr_signal; |
0bf23d9b | 394 | #endif |
7c517a04 | 395 | |
b589977b | 396 | static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl) |
a63d5eaa RL |
397 | { |
398 | static int ps; | |
399 | int ok; | |
b589977b | 400 | char result[BUFSIZ]; |
a63d5eaa | 401 | int maxsize = BUFSIZ-1; |
0bf23d9b | 402 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
b589977b | 403 | char *p; |
a63d5eaa | 404 | |
7c517a04 | 405 | intr_signal=0; |
a63d5eaa RL |
406 | ok=0; |
407 | ps=0; | |
408 | ||
409 | pushsig(); | |
410 | ps=1; | |
411 | ||
7c517a04 BL |
412 | if (!echo && !noecho_console(ui)) |
413 | goto error; | |
a63d5eaa RL |
414 | ps=2; |
415 | ||
b589977b | 416 | result[0]='\0'; |
02790299 | 417 | #ifdef OPENSSL_SYS_MSDOS |
b589977b | 418 | if (!echo) |
7c517a04 | 419 | { |
b589977b | 420 | noecho_fgets(result,maxsize,tty_in); |
7c517a04 BL |
421 | p=result; /* FIXME: noecho_fgets doesn't return errors */ |
422 | } | |
b589977b | 423 | else |
7c517a04 | 424 | p=fgets(result,maxsize,tty_in); |
b589977b | 425 | #else |
7c517a04 | 426 | p=fgets(result,maxsize,tty_in); |
02790299 | 427 | #endif |
7c517a04 BL |
428 | if(!p) |
429 | goto error; | |
b589977b RL |
430 | if (feof(tty_in)) goto error; |
431 | if (ferror(tty_in)) goto error; | |
432 | if ((p=(char *)strchr(result,'\n')) != NULL) | |
433 | { | |
434 | if (strip_nl) | |
a63d5eaa | 435 | *p='\0'; |
a63d5eaa | 436 | } |
b589977b RL |
437 | else |
438 | read_till_nl(tty_in); | |
439 | if (UI_set_result(ui, uis, result) >= 0) | |
440 | ok=1; | |
a63d5eaa RL |
441 | |
442 | error: | |
7c517a04 BL |
443 | if (intr_signal == SIGINT) |
444 | ok=-1; | |
200bc9e3 | 445 | if (!echo) fprintf(tty_out,"\n"); |
7c517a04 BL |
446 | if (ps >= 2 && !echo && !echo_console(ui)) |
447 | ok=0; | |
a63d5eaa RL |
448 | |
449 | if (ps >= 1) | |
450 | popsig(); | |
451 | #else | |
a63d5eaa RL |
452 | ok=1; |
453 | #endif | |
454 | ||
4579924b | 455 | OPENSSL_cleanse(result,BUFSIZ); |
a63d5eaa RL |
456 | return ok; |
457 | } | |
458 | ||
459 | ||
460 | /* Internal functions to open, handle and close a channel to the console. */ | |
461 | static int open_console(UI *ui) | |
462 | { | |
463 | CRYPTO_w_lock(CRYPTO_LOCK_UI); | |
464 | is_a_tty = 1; | |
465 | ||
6a89a25c | 466 | #if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) |
200bc9e3 BM |
467 | tty_in=stdin; |
468 | tty_out=stderr; | |
a63d5eaa | 469 | #else |
508f15cd BM |
470 | # ifdef OPENSSL_SYS_MSDOS |
471 | # define DEV_TTY "con" | |
472 | # else | |
473 | # define DEV_TTY "/dev/tty" | |
474 | # endif | |
475 | if ((tty_in=fopen(DEV_TTY,"r")) == NULL) | |
200bc9e3 | 476 | tty_in=stdin; |
508f15cd | 477 | if ((tty_out=fopen(DEV_TTY,"w")) == NULL) |
200bc9e3 | 478 | tty_out=stderr; |
a63d5eaa RL |
479 | #endif |
480 | ||
c5597592 | 481 | #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS) |
2ee67f1d | 482 | if (TTY_get(fileno(tty_in),&tty_orig) == -1) |
a63d5eaa RL |
483 | { |
484 | #ifdef ENOTTY | |
485 | if (errno == ENOTTY) | |
486 | is_a_tty=0; | |
487 | else | |
488 | #endif | |
489 | #ifdef EINVAL | |
490 | /* Ariel Glenn ariel@columbia.edu reports that solaris | |
491 | * can return EINVAL instead. This should be ok */ | |
492 | if (errno == EINVAL) | |
493 | is_a_tty=0; | |
494 | else | |
495 | #endif | |
496 | return 0; | |
497 | } | |
498 | #endif | |
02790299 | 499 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
500 | status = sys$assign(&terminal,&channel,0,0); |
501 | if (status != SS$_NORMAL) | |
502 | return 0; | |
503 | status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0); | |
504 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
505 | return 0; | |
506 | #endif | |
507 | return 1; | |
508 | } | |
509 | ||
510 | static int noecho_console(UI *ui) | |
511 | { | |
512 | #ifdef TTY_FLAGS | |
513 | memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); | |
514 | tty_new.TTY_FLAGS &= ~ECHO; | |
515 | #endif | |
516 | ||
02790299 | 517 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
200bc9e3 | 518 | if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) |
a63d5eaa RL |
519 | return 0; |
520 | #endif | |
02790299 | 521 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
522 | tty_new[0] = tty_orig[0]; |
523 | tty_new[1] = tty_orig[1] | TT$M_NOECHO; | |
524 | tty_new[2] = tty_orig[2]; | |
525 | status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0); | |
526 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
527 | return 0; | |
528 | #endif | |
529 | return 1; | |
530 | } | |
531 | ||
532 | static int echo_console(UI *ui) | |
533 | { | |
02790299 | 534 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
a63d5eaa RL |
535 | memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig)); |
536 | tty_new.TTY_FLAGS |= ECHO; | |
537 | #endif | |
538 | ||
02790299 | 539 | #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS) |
200bc9e3 | 540 | if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1)) |
a63d5eaa RL |
541 | return 0; |
542 | #endif | |
02790299 | 543 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
544 | tty_new[0] = tty_orig[0]; |
545 | tty_new[1] = tty_orig[1] & ~TT$M_NOECHO; | |
546 | tty_new[2] = tty_orig[2]; | |
547 | status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0); | |
548 | if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL)) | |
549 | return 0; | |
550 | #endif | |
551 | return 1; | |
552 | } | |
553 | ||
554 | static int close_console(UI *ui) | |
555 | { | |
caa7b2ad | 556 | if (tty_in != stdin) fclose(tty_in); |
200bc9e3 | 557 | if (tty_out != stderr) fclose(tty_out); |
02790299 | 558 | #ifdef OPENSSL_SYS_VMS |
a63d5eaa RL |
559 | status = sys$dassgn(channel); |
560 | #endif | |
561 | CRYPTO_w_unlock(CRYPTO_LOCK_UI); | |
562 | ||
563 | return 1; | |
564 | } | |
565 | ||
566 | ||
0bf23d9b | 567 | #if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
568 | /* Internal functions to handle signals and act on them */ |
569 | static void pushsig(void) | |
570 | { | |
571 | int i; | |
572 | #ifdef SIGACTION | |
573 | struct sigaction sa; | |
574 | ||
575 | memset(&sa,0,sizeof sa); | |
576 | sa.sa_handler=recsig; | |
577 | #endif | |
578 | ||
579 | for (i=1; i<NX509_SIG; i++) | |
580 | { | |
581 | #ifdef SIGUSR1 | |
582 | if (i == SIGUSR1) | |
583 | continue; | |
584 | #endif | |
585 | #ifdef SIGUSR2 | |
586 | if (i == SIGUSR2) | |
587 | continue; | |
588 | #endif | |
589 | #ifdef SIGKILL | |
590 | if (i == SIGKILL) /* We can't make any action on that. */ | |
591 | continue; | |
592 | #endif | |
593 | #ifdef SIGACTION | |
594 | sigaction(i,&sa,&savsig[i]); | |
595 | #else | |
596 | savsig[i]=signal(i,recsig); | |
597 | #endif | |
598 | } | |
599 | ||
600 | #ifdef SIGWINCH | |
601 | signal(SIGWINCH,SIG_DFL); | |
602 | #endif | |
603 | } | |
604 | ||
605 | static void popsig(void) | |
606 | { | |
607 | int i; | |
608 | ||
609 | for (i=1; i<NX509_SIG; i++) | |
610 | { | |
611 | #ifdef SIGUSR1 | |
612 | if (i == SIGUSR1) | |
613 | continue; | |
614 | #endif | |
615 | #ifdef SIGUSR2 | |
616 | if (i == SIGUSR2) | |
617 | continue; | |
618 | #endif | |
619 | #ifdef SIGACTION | |
620 | sigaction(i,&savsig[i],NULL); | |
621 | #else | |
622 | signal(i,savsig[i]); | |
623 | #endif | |
624 | } | |
625 | } | |
626 | ||
627 | static void recsig(int i) | |
628 | { | |
7c517a04 | 629 | intr_signal=i; |
a63d5eaa | 630 | } |
0bf23d9b | 631 | #endif |
a63d5eaa | 632 | |
a63d5eaa | 633 | /* Internal functions specific for Windows */ |
0bf23d9b | 634 | #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE) |
a63d5eaa RL |
635 | static int noecho_fgets(char *buf, int size, FILE *tty) |
636 | { | |
637 | int i; | |
638 | char *p; | |
639 | ||
640 | p=buf; | |
641 | for (;;) | |
642 | { | |
643 | if (size == 0) | |
644 | { | |
645 | *p='\0'; | |
646 | break; | |
647 | } | |
648 | size--; | |
649 | #ifdef WIN16TTY | |
650 | i=_inchar(); | |
651 | #else | |
652 | i=getch(); | |
653 | #endif | |
654 | if (i == '\r') i='\n'; | |
655 | *(p++)=i; | |
656 | if (i == '\n') | |
657 | { | |
658 | *p='\0'; | |
659 | break; | |
660 | } | |
661 | } | |
662 | #ifdef WIN_CONSOLE_BUG | |
663 | /* Win95 has several evil console bugs: one of these is that the | |
664 | * last character read using getch() is passed to the next read: this is | |
665 | * usually a CR so this can be trouble. No STDIO fix seems to work but | |
666 | * flushing the console appears to do the trick. | |
667 | */ | |
668 | { | |
669 | HANDLE inh; | |
670 | inh = GetStdHandle(STD_INPUT_HANDLE); | |
671 | FlushConsoleInputBuffer(inh); | |
672 | } | |
673 | #endif | |
674 | return(strlen(buf)); | |
675 | } | |
676 | #endif |