1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/ossrv/ssl/libcrypto/src/crypto/ui/ui_openssl.c Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,759 @@
1.4 +/* crypto/ui/ui_openssl.c -*- mode:C; c-file-style: "eay" -*- */
1.5 +/* Written by Richard Levitte (richard@levitte.org) and others
1.6 + * for the OpenSSL project 2001.
1.7 + */
1.8 +/* ====================================================================
1.9 + * Copyright (c) 2001 The OpenSSL Project. All rights reserved.
1.10 + *
1.11 + * Redistribution and use in source and binary forms, with or without
1.12 + * modification, are permitted provided that the following conditions
1.13 + * are met:
1.14 + *
1.15 + * 1. Redistributions of source code must retain the above copyright
1.16 + * notice, this list of conditions and the following disclaimer.
1.17 + *
1.18 + * 2. Redistributions in binary form must reproduce the above copyright
1.19 + * notice, this list of conditions and the following disclaimer in
1.20 + * the documentation and/or other materials provided with the
1.21 + * distribution.
1.22 + *
1.23 + * 3. All advertising materials mentioning features or use of this
1.24 + * software must display the following acknowledgment:
1.25 + * "This product includes software developed by the OpenSSL Project
1.26 + * for use in the OpenSSL Toolkit. (http://www.openssl.org/)"
1.27 + *
1.28 + * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
1.29 + * endorse or promote products derived from this software without
1.30 + * prior written permission. For written permission, please contact
1.31 + * openssl-core@openssl.org.
1.32 + *
1.33 + * 5. Products derived from this software may not be called "OpenSSL"
1.34 + * nor may "OpenSSL" appear in their names without prior written
1.35 + * permission of the OpenSSL Project.
1.36 + *
1.37 + * 6. Redistributions of any form whatsoever must retain the following
1.38 + * acknowledgment:
1.39 + * "This product includes software developed by the OpenSSL Project
1.40 + * for use in the OpenSSL Toolkit (http://www.openssl.org/)"
1.41 + *
1.42 + * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
1.43 + * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.44 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
1.45 + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
1.46 + * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
1.47 + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1.48 + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
1.49 + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.50 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
1.51 + * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
1.52 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
1.53 + * OF THE POSSIBILITY OF SUCH DAMAGE.
1.54 + * ====================================================================
1.55 + *
1.56 + * This product includes cryptographic software written by Eric Young
1.57 + * (eay@cryptsoft.com). This product includes software written by Tim
1.58 + * Hudson (tjh@cryptsoft.com).
1.59 + *
1.60 + */
1.61 +
1.62 +/* The lowest level part of this file was previously in crypto/des/read_pwd.c,
1.63 + * Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
1.64 + * All rights reserved.
1.65 + *
1.66 + * This package is an SSL implementation written
1.67 + * by Eric Young (eay@cryptsoft.com).
1.68 + * The implementation was written so as to conform with Netscapes SSL.
1.69 + *
1.70 + * This library is free for commercial and non-commercial use as long as
1.71 + * the following conditions are aheared to. The following conditions
1.72 + * apply to all code found in this distribution, be it the RC4, RSA,
1.73 + * lhash, DES, etc., code; not just the SSL code. The SSL documentation
1.74 + * included with this distribution is covered by the same copyright terms
1.75 + * except that the holder is Tim Hudson (tjh@cryptsoft.com).
1.76 + *
1.77 + * Copyright remains Eric Young's, and as such any Copyright notices in
1.78 + * the code are not to be removed.
1.79 + * If this package is used in a product, Eric Young should be given attribution
1.80 + * as the author of the parts of the library used.
1.81 + * This can be in the form of a textual message at program startup or
1.82 + * in documentation (online or textual) provided with the package.
1.83 + *
1.84 + * Redistribution and use in source and binary forms, with or without
1.85 + * modification, are permitted provided that the following conditions
1.86 + * are met:
1.87 + * 1. Redistributions of source code must retain the copyright
1.88 + * notice, this list of conditions and the following disclaimer.
1.89 + * 2. Redistributions in binary form must reproduce the above copyright
1.90 + * notice, this list of conditions and the following disclaimer in the
1.91 + * documentation and/or other materials provided with the distribution.
1.92 + * 3. All advertising materials mentioning features or use of this software
1.93 + * must display the following acknowledgement:
1.94 + * "This product includes cryptographic software written by
1.95 + * Eric Young (eay@cryptsoft.com)"
1.96 + * The word 'cryptographic' can be left out if the rouines from the library
1.97 + * being used are not cryptographic related :-).
1.98 + * 4. If you include any Windows specific code (or a derivative thereof) from
1.99 + * the apps directory (application code) you must include an acknowledgement:
1.100 + * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
1.101 + *
1.102 + * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
1.103 + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1.104 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
1.105 + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
1.106 + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
1.107 + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
1.108 + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
1.109 + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
1.110 + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
1.111 + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
1.112 + * SUCH DAMAGE.
1.113 + *
1.114 + * The licence and distribution terms for any publically available version or
1.115 + * derivative of this code cannot be changed. i.e. this code cannot simply be
1.116 + * copied and put under another distribution licence
1.117 + * [including the GNU Public Licence.]
1.118 + */
1.119 +/*
1.120 + © Portions copyright (c) 2006 Nokia Corporation. All rights reserved.
1.121 + */
1.122 +
1.123 +
1.124 +#include <openssl/e_os2.h>
1.125 +
1.126 +/* need for #define _POSIX_C_SOURCE arises whenever you pass -ansi to gcc
1.127 + * [maybe others?], because it masks interfaces not discussed in standard,
1.128 + * sigaction and fileno included. -pedantic would be more appropriate for
1.129 + * the intended purposes, but we can't prevent users from adding -ansi.
1.130 + */
1.131 +
1.132 +#define _POSIX_C_SOURCE 1
1.133 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.134 +#undef _POSIX_C_SOURCE
1.135 +#endif
1.136 +#ifndef SYMBIAN
1.137 +#include <signal.h>
1.138 +#endif
1.139 +#include <stdio.h>
1.140 +#include <string.h>
1.141 +#include <errno.h>
1.142 +
1.143 +
1.144 +#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
1.145 +# ifdef OPENSSL_UNISTD
1.146 +# include OPENSSL_UNISTD
1.147 +# else
1.148 +# include <unistd.h>
1.149 +# endif
1.150 +/* If unistd.h defines _POSIX_VERSION, we conclude that we
1.151 + * are on a POSIX system and have sigaction and termios. */
1.152 +# if defined(_POSIX_VERSION) && !defined(SYMBIAN)
1.153 +# define SIGACTION
1.154 +# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
1.155 +# define TERMIOS
1.156 +# endif
1.157 +
1.158 +# endif
1.159 +#endif
1.160 +
1.161 +#ifdef WIN16TTY
1.162 +# undef OPENSSL_SYS_WIN16
1.163 +# undef WIN16
1.164 +# undef _WINDOWS
1.165 +# include <graph.h>
1.166 +#endif
1.167 +
1.168 +/* 06-Apr-92 Luke Brennan Support for VMS */
1.169 +#include "ui_locl.h"
1.170 +#include "cryptlib.h"
1.171 +
1.172 +#ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
1.173 +# include <starlet.h>
1.174 +# ifdef __DECC
1.175 +# pragma message disable DOLLARID
1.176 +# endif
1.177 +#endif
1.178 +
1.179 +#ifndef SYMBIAN
1.180 +#ifdef WIN_CONSOLE_BUG
1.181 +# include <windows.h>
1.182 +#ifndef OPENSSL_SYS_WINCE
1.183 +# include <wincon.h>
1.184 +#endif
1.185 +#endif
1.186 +#endif
1.187 +
1.188 +/* There are 5 types of terminal interface supported,
1.189 + * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
1.190 + */
1.191 +
1.192 +#if defined(__sgi) && !defined(TERMIOS)
1.193 +# define TERMIOS
1.194 +# undef TERMIO
1.195 +# undef SGTTY
1.196 +#endif
1.197 +
1.198 +#if defined(linux) && !defined(TERMIO)
1.199 +# undef TERMIOS
1.200 +# define TERMIO
1.201 +# undef SGTTY
1.202 +#endif
1.203 +
1.204 +#ifdef _LIBC
1.205 +# undef TERMIOS
1.206 +# define TERMIO
1.207 +# undef SGTTY
1.208 +#endif
1.209 +
1.210 +#if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(MAC_OS_GUSI_SOURCE)
1.211 +# undef TERMIOS
1.212 +# undef TERMIO
1.213 +# define SGTTY
1.214 +#endif
1.215 +
1.216 +#if defined(OPENSSL_SYS_VXWORKS)
1.217 +#undef TERMIOS
1.218 +#undef TERMIO
1.219 +#undef SGTTY
1.220 +#endif
1.221 +
1.222 +#if defined(OPENSSL_SYS_NETWARE)
1.223 +#undef TERMIOS
1.224 +#undef TERMIO
1.225 +#undef SGTTY
1.226 +#endif
1.227 +
1.228 +#if defined(SYMBIAN)
1.229 +#undef TERMIOS
1.230 +#undef TERMIO
1.231 +#undef SGTTY
1.232 +#endif
1.233 +
1.234 +#ifdef TERMIOS
1.235 +# include <termios.h>
1.236 +# define TTY_STRUCT struct termios
1.237 +# define TTY_FLAGS c_lflag
1.238 +# define TTY_get(tty,data) tcgetattr(tty,data)
1.239 +# define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
1.240 +#endif
1.241 +
1.242 +#ifdef TERMIO
1.243 +# include <termio.h>
1.244 +# define TTY_STRUCT struct termio
1.245 +# define TTY_FLAGS c_lflag
1.246 +# define TTY_get(tty,data) ioctl(tty,TCGETA,data)
1.247 +# define TTY_set(tty,data) ioctl(tty,TCSETA,data)
1.248 +#endif
1.249 +
1.250 +#ifdef SGTTY
1.251 +# include <sgtty.h>
1.252 +# define TTY_STRUCT struct sgttyb
1.253 +# define TTY_FLAGS sg_flags
1.254 +# define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
1.255 +# define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
1.256 +#endif
1.257 +
1.258 +#if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MACINTOSH_CLASSIC) && !defined(OPENSSL_SYS_SUNOS)
1.259 +# include <sys/ioctl.h>
1.260 +#endif
1.261 +
1.262 +#ifdef OPENSSL_SYS_MSDOS
1.263 +# include <conio.h>
1.264 +#endif
1.265 +
1.266 +#ifdef OPENSSL_SYS_VMS
1.267 +# include <ssdef.h>
1.268 +# include <iodef.h>
1.269 +# include <ttdef.h>
1.270 +# include <descrip.h>
1.271 +struct IOSB {
1.272 + short iosb$w_value;
1.273 + short iosb$w_count;
1.274 + long iosb$l_info;
1.275 + };
1.276 +#endif
1.277 +#if (defined(SYMBIAN) && (defined(__WINSCW__) || defined(__WINS__)))
1.278 +#include "libcrypto_wsd_macros.h"
1.279 +#include "libcrypto_wsd.h"
1.280 +#endif
1.281 +
1.282 +#ifdef OPENSSL_SYS_SUNOS
1.283 + typedef int sig_atomic_t;
1.284 +#endif
1.285 +
1.286 +#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(MAC_OS_GUSI_SOURCE) || defined(OPENSSL_SYS_NETWARE)
1.287 +/*
1.288 + * This one needs work. As a matter of fact the code is unoperational
1.289 + * and this is only a trick to get it compiled.
1.290 + * <appro@fy.chalmers.se>
1.291 + */
1.292 +# define TTY_STRUCT int
1.293 +#endif
1.294 +
1.295 +#ifndef NX509_SIG
1.296 +# define NX509_SIG 32
1.297 +#endif
1.298 +
1.299 +
1.300 +/* Define globals. They are protected by a lock */
1.301 +#ifdef SIGACTION
1.302 +static struct sigaction savsig[NX509_SIG];
1.303 +#else
1.304 +static void (*savsig[NX509_SIG])(int );
1.305 +#endif
1.306 +
1.307 +#ifdef OPENSSL_SYS_VMS
1.308 +static struct IOSB iosb;
1.309 +static $DESCRIPTOR(terminal,"TT");
1.310 +static long tty_orig[3], tty_new[3]; /* XXX Is there any guarantee that this will always suffice for the actual structures? */
1.311 +static long status;
1.312 +static unsigned short channel = 0;
1.313 +#else
1.314 +#ifndef SYMBIAN
1.315 +#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
1.316 +static TTY_STRUCT tty_orig,tty_new;
1.317 +#endif
1.318 +#endif
1.319 +#endif
1.320 +#ifndef EMULATOR
1.321 +static FILE *tty_in, *tty_out;
1.322 +static int is_a_tty;
1.323 +#else
1.324 +GET_STATIC_VAR_FROM_TLS(tty_in,ui_openssl,FILE *)
1.325 +#define tty_in (*GET_WSD_VAR_NAME(tty_in,ui_openssl, s)())
1.326 +
1.327 +GET_STATIC_VAR_FROM_TLS(tty_out,ui_openssl,FILE *)
1.328 +#define tty_out (*GET_WSD_VAR_NAME(tty_out,ui_openssl, s)())
1.329 +
1.330 +GET_STATIC_VAR_FROM_TLS(is_a_tty,ui_openssl,int)
1.331 +#define is_a_tty (*GET_WSD_VAR_NAME(is_a_tty,ui_openssl, s)())
1.332 +
1.333 +#endif
1.334 +/* Declare static functions */
1.335 +#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
1.336 +static void read_till_nl(FILE *);
1.337 +static void recsig(int);
1.338 +static void pushsig(void);
1.339 +static void popsig(void);
1.340 +#endif
1.341 +#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
1.342 +static int noecho_fgets(char *buf, int size, FILE *tty);
1.343 +#endif
1.344 +static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl);
1.345 +
1.346 +static int read_string(UI *ui, UI_STRING *uis);
1.347 +static int write_string(UI *ui, UI_STRING *uis);
1.348 +
1.349 +static int open_console(UI *ui);
1.350 +static int echo_console(UI *ui);
1.351 +static int noecho_console(UI *ui);
1.352 +static int close_console(UI *ui);
1.353 +
1.354 +#ifndef EMULATOR
1.355 +static UI_METHOD ui_openssl =
1.356 + {
1.357 + "OpenSSL default user interface",
1.358 + open_console,
1.359 + write_string,
1.360 + NULL, /* No flusher is needed for command lines */
1.361 + read_string,
1.362 + close_console,
1.363 + NULL
1.364 + };
1.365 +#else
1.366 +GET_STATIC_VAR_FROM_TLS(ui_openssl,ui_openssl,UI_METHOD)
1.367 +#define ui_openssl (*GET_WSD_VAR_NAME(ui_openssl,ui_openssl, s)())
1.368 +const UI_METHOD temp_s_ui_openssl =
1.369 + {
1.370 + "OpenSSL default user interface",
1.371 + open_console,
1.372 + write_string,
1.373 + NULL, /* No flusher is needed for command lines */
1.374 + read_string,
1.375 + close_console,
1.376 + NULL
1.377 + };
1.378 +
1.379 +#endif
1.380 +/* The method with all the built-in thingies */
1.381 +EXPORT_C UI_METHOD *UI_OpenSSL(void)
1.382 + {
1.383 + return &ui_openssl;
1.384 + }
1.385 +
1.386 +/* The following function makes sure that info and error strings are printed
1.387 + before any prompt. */
1.388 +static int write_string(UI *ui, UI_STRING *uis)
1.389 + {
1.390 + switch (UI_get_string_type(uis))
1.391 + {
1.392 + case UIT_ERROR:
1.393 + case UIT_INFO:
1.394 + fputs(UI_get0_output_string(uis), tty_out);
1.395 + fflush(tty_out);
1.396 + break;
1.397 + default:
1.398 + break;
1.399 + }
1.400 + return 1;
1.401 + }
1.402 +
1.403 +static int read_string(UI *ui, UI_STRING *uis)
1.404 + {
1.405 + int ok = 0;
1.406 +
1.407 + switch (UI_get_string_type(uis))
1.408 + {
1.409 + case UIT_BOOLEAN:
1.410 + fputs(UI_get0_output_string(uis), tty_out);
1.411 + fputs(UI_get0_action_string(uis), tty_out);
1.412 + fflush(tty_out);
1.413 + return read_string_inner(ui, uis,
1.414 + UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 0);
1.415 + case UIT_PROMPT:
1.416 + fputs(UI_get0_output_string(uis), tty_out);
1.417 + fflush(tty_out);
1.418 + return read_string_inner(ui, uis,
1.419 + UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1);
1.420 + case UIT_VERIFY:
1.421 + fprintf(tty_out,"Verifying - %s",
1.422 + UI_get0_output_string(uis));
1.423 + fflush(tty_out);
1.424 + if ((ok = read_string_inner(ui, uis,
1.425 + UI_get_input_flags(uis) & UI_INPUT_FLAG_ECHO, 1)) <= 0)
1.426 + return ok;
1.427 + if (strcmp(UI_get0_result_string(uis),
1.428 + UI_get0_test_string(uis)) != 0)
1.429 + {
1.430 + fprintf(tty_out,"Verify failure\n");
1.431 + fflush(tty_out);
1.432 + return 0;
1.433 + }
1.434 + break;
1.435 + default:
1.436 + break;
1.437 + }
1.438 + return 1;
1.439 + }
1.440 +
1.441 +#ifndef SYMBIAN
1.442 +#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
1.443 +/* Internal functions to read a string without echoing */
1.444 +static void read_till_nl(FILE *in)
1.445 + {
1.446 +#define SIZE 4
1.447 + char buf[SIZE+1];
1.448 +
1.449 + do {
1.450 + fgets(buf,SIZE,in);
1.451 + } while (strchr(buf,'\n') == NULL);
1.452 + }
1.453 +
1.454 +static volatile sig_atomic_t intr_signal;
1.455 +#endif
1.456 +#endif
1.457 +static int read_string_inner(UI *ui, UI_STRING *uis, int echo, int strip_nl)
1.458 + {
1.459 + static int ps;
1.460 + int ok;
1.461 + char result[BUFSIZ];
1.462 + int maxsize = BUFSIZ-1;
1.463 +#ifndef SYMBIAN
1.464 +#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
1.465 + char *p;
1.466 +
1.467 + intr_signal=0;
1.468 + ok=0;
1.469 + ps=0;
1.470 +
1.471 + pushsig();
1.472 + ps=1;
1.473 +
1.474 + if (!echo && !noecho_console(ui))
1.475 + goto error;
1.476 + ps=2;
1.477 +
1.478 + result[0]='\0';
1.479 +#ifdef OPENSSL_SYS_MSDOS
1.480 + if (!echo)
1.481 + {
1.482 + noecho_fgets(result,maxsize,tty_in);
1.483 + p=result; /* FIXME: noecho_fgets doesn't return errors */
1.484 + }
1.485 + else
1.486 + p=fgets(result,maxsize,tty_in);
1.487 +#else
1.488 + p=fgets(result,maxsize,tty_in);
1.489 +#endif
1.490 + if(!p)
1.491 + goto error;
1.492 + if (feof(tty_in)) goto error;
1.493 + if (ferror(tty_in)) goto error;
1.494 + if ((p=(char *)strchr(result,'\n')) != NULL)
1.495 + {
1.496 + if (strip_nl)
1.497 + *p='\0';
1.498 + }
1.499 + else
1.500 + read_till_nl(tty_in);
1.501 + if (UI_set_result(ui, uis, result) >= 0)
1.502 + ok=1;
1.503 +
1.504 +error:
1.505 + if (intr_signal == SIGINT)
1.506 + ok=-1;
1.507 + if (!echo) fprintf(tty_out,"\n");
1.508 + if (ps >= 2 && !echo && !echo_console(ui))
1.509 + ok=0;
1.510 +
1.511 + if (ps >= 1)
1.512 + popsig();
1.513 +#else
1.514 + ok=1;
1.515 +#endif
1.516 +#endif
1.517 + OPENSSL_cleanse(result,BUFSIZ);
1.518 + return ok;
1.519 + }
1.520 +
1.521 +
1.522 +/* Internal functions to open, handle and close a channel to the console. */
1.523 +static int open_console(UI *ui)
1.524 + {
1.525 + CRYPTO_w_lock(CRYPTO_LOCK_UI);
1.526 + is_a_tty = 1;
1.527 +
1.528 +#if defined(OPENSSL_SYS_MACINTOSH_CLASSIC) || defined(OPENSSL_SYS_VXWORKS) || defined(OPENSSL_SYS_NETWARE)
1.529 + tty_in=stdin;
1.530 + tty_out=stderr;
1.531 +#else
1.532 +# ifdef OPENSSL_SYS_MSDOS
1.533 +# define DEV_TTY "con"
1.534 +# else
1.535 +# define DEV_TTY "/dev/tty"
1.536 +# endif
1.537 + if ((tty_in=fopen(DEV_TTY,"r")) == NULL)
1.538 + tty_in=stdin;
1.539 + if ((tty_out=fopen(DEV_TTY,"w")) == NULL)
1.540 + tty_out=stderr;
1.541 +#endif
1.542 +
1.543 +#if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
1.544 + if (TTY_get(fileno(tty_in),&tty_orig) == -1)
1.545 + {
1.546 +#ifdef ENOTTY
1.547 + if (errno == ENOTTY)
1.548 + is_a_tty=0;
1.549 + else
1.550 +#endif
1.551 +#ifdef EINVAL
1.552 + /* Ariel Glenn ariel@columbia.edu reports that solaris
1.553 + * can return EINVAL instead. This should be ok */
1.554 + if (errno == EINVAL)
1.555 + is_a_tty=0;
1.556 + else
1.557 +#endif
1.558 + return 0;
1.559 + }
1.560 +#endif
1.561 +#ifdef OPENSSL_SYS_VMS
1.562 + status = sys$assign(&terminal,&channel,0,0);
1.563 + if (status != SS$_NORMAL)
1.564 + return 0;
1.565 + status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
1.566 + if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
1.567 + return 0;
1.568 +#endif
1.569 + return 1;
1.570 + }
1.571 +
1.572 +static int noecho_console(UI *ui)
1.573 + {
1.574 +#ifdef TTY_FLAGS
1.575 + memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
1.576 + tty_new.TTY_FLAGS &= ~ECHO;
1.577 +#endif
1.578 +
1.579 +#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
1.580 + if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
1.581 + return 0;
1.582 +#endif
1.583 +#ifdef OPENSSL_SYS_VMS
1.584 + tty_new[0] = tty_orig[0];
1.585 + tty_new[1] = tty_orig[1] | TT$M_NOECHO;
1.586 + tty_new[2] = tty_orig[2];
1.587 + status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
1.588 + if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
1.589 + return 0;
1.590 +#endif
1.591 + return 1;
1.592 + }
1.593 +
1.594 +static int echo_console(UI *ui)
1.595 + {
1.596 +#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
1.597 + memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
1.598 + tty_new.TTY_FLAGS |= ECHO;
1.599 +#endif
1.600 +
1.601 +#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
1.602 + if (is_a_tty && (TTY_set(fileno(tty_in),&tty_new) == -1))
1.603 + return 0;
1.604 +#endif
1.605 +#ifdef OPENSSL_SYS_VMS
1.606 + tty_new[0] = tty_orig[0];
1.607 + tty_new[1] = tty_orig[1] & ~TT$M_NOECHO;
1.608 + tty_new[2] = tty_orig[2];
1.609 + status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
1.610 + if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
1.611 + return 0;
1.612 +#endif
1.613 + return 1;
1.614 + }
1.615 +
1.616 +static int close_console(UI *ui)
1.617 + {
1.618 + if (tty_in != stdin) fclose(tty_in);
1.619 + if (tty_out != stderr) fclose(tty_out);
1.620 +#ifdef OPENSSL_SYS_VMS
1.621 + status = sys$dassgn(channel);
1.622 +#endif
1.623 + CRYPTO_w_unlock(CRYPTO_LOCK_UI);
1.624 +
1.625 + return 1;
1.626 + }
1.627 +
1.628 +
1.629 +#ifndef SYMBIAN
1.630 +#if !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
1.631 +/* Internal functions to handle signals and act on them */
1.632 +static void pushsig(void)
1.633 + {
1.634 +#ifndef OPENSSL_SYS_WIN32
1.635 + int i;
1.636 +#endif
1.637 +#ifdef SIGACTION
1.638 + struct sigaction sa;
1.639 +
1.640 + memset(&sa,0,sizeof sa);
1.641 + sa.sa_handler=recsig;
1.642 +#endif
1.643 +
1.644 +#ifdef OPENSSL_SYS_WIN32
1.645 + savsig[SIGABRT]=signal(SIGABRT,recsig);
1.646 + savsig[SIGFPE]=signal(SIGFPE,recsig);
1.647 + savsig[SIGILL]=signal(SIGILL,recsig);
1.648 + savsig[SIGINT]=signal(SIGINT,recsig);
1.649 + savsig[SIGSEGV]=signal(SIGSEGV,recsig);
1.650 + savsig[SIGTERM]=signal(SIGTERM,recsig);
1.651 +#else
1.652 + for (i=1; i<NX509_SIG; i++)
1.653 + {
1.654 +#ifdef SIGUSR1
1.655 + if (i == SIGUSR1)
1.656 + continue;
1.657 +#endif
1.658 +#ifdef SIGUSR2
1.659 + if (i == SIGUSR2)
1.660 + continue;
1.661 +#endif
1.662 +#ifdef SIGKILL
1.663 + if (i == SIGKILL) /* We can't make any action on that. */
1.664 + continue;
1.665 +#endif
1.666 +#ifdef SIGACTION
1.667 + sigaction(i,&sa,&savsig[i]);
1.668 +#else
1.669 + savsig[i]=signal(i,recsig);
1.670 +#endif
1.671 + }
1.672 +#endif
1.673 +
1.674 +#ifdef SIGWINCH
1.675 + signal(SIGWINCH,SIG_DFL);
1.676 +#endif
1.677 + }
1.678 +
1.679 +static void popsig(void)
1.680 + {
1.681 +#ifdef OPENSSL_SYS_WIN32
1.682 + signal(SIGABRT,savsig[SIGABRT]);
1.683 + signal(SIGFPE,savsig[SIGFPE]);
1.684 + signal(SIGILL,savsig[SIGILL]);
1.685 + signal(SIGINT,savsig[SIGINT]);
1.686 + signal(SIGSEGV,savsig[SIGSEGV]);
1.687 + signal(SIGTERM,savsig[SIGTERM]);
1.688 +#else
1.689 + int i;
1.690 + for (i=1; i<NX509_SIG; i++)
1.691 + {
1.692 +#ifdef SIGUSR1
1.693 + if (i == SIGUSR1)
1.694 + continue;
1.695 +#endif
1.696 +#ifdef SIGUSR2
1.697 + if (i == SIGUSR2)
1.698 + continue;
1.699 +#endif
1.700 +#ifdef SIGACTION
1.701 + sigaction(i,&savsig[i],NULL);
1.702 +#else
1.703 + signal(i,savsig[i]);
1.704 +#endif
1.705 + }
1.706 +#endif
1.707 + }
1.708 +
1.709 +static void recsig(int i)
1.710 + {
1.711 + intr_signal=i;
1.712 + }
1.713 +#endif
1.714 +#endif
1.715 +
1.716 +/* Internal functions specific for Windows */
1.717 +#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16) && !defined(OPENSSL_SYS_WINCE)
1.718 +static int noecho_fgets(char *buf, int size, FILE *tty)
1.719 + {
1.720 + int i;
1.721 + char *p;
1.722 +
1.723 + p=buf;
1.724 + for (;;)
1.725 + {
1.726 + if (size == 0)
1.727 + {
1.728 + *p='\0';
1.729 + break;
1.730 + }
1.731 + size--;
1.732 +#ifdef WIN16TTY
1.733 + i=_inchar();
1.734 +#else
1.735 + i=getch();
1.736 +#endif
1.737 + if (i == '\r') i='\n';
1.738 + *(p++)=i;
1.739 + if (i == '\n')
1.740 + {
1.741 + *p='\0';
1.742 + break;
1.743 + }
1.744 + }
1.745 +#ifndef SYMBIAN
1.746 +#ifdef WIN_CONSOLE_BUG
1.747 +/* Win95 has several evil console bugs: one of these is that the
1.748 + * last character read using getch() is passed to the next read: this is
1.749 + * usually a CR so this can be trouble. No STDIO fix seems to work but
1.750 + * flushing the console appears to do the trick.
1.751 + */
1.752 + {
1.753 + HANDLE inh;
1.754 + inh = GetStdHandle(STD_INPUT_HANDLE);
1.755 + FlushConsoleInputBuffer(inh);
1.756 + }
1.757 +#endif
1.758 +#endif
1.759 + return(strlen(buf));
1.760 + }
1.761 +#endif
1.762 +