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