1 /* crypto/des/read_pwd.c */
2 /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@cryptsoft.com).
7 * The implementation was written so as to conform with Netscapes SSL.
9 * This library is free for commercial and non-commercial use as long as
10 * the following conditions are aheared to. The following conditions
11 * apply to all code found in this distribution, be it the RC4, RSA,
12 * lhash, DES, etc., code; not just the SSL code. The SSL documentation
13 * included with this distribution is covered by the same copyright terms
14 * except that the holder is Tim Hudson (tjh@cryptsoft.com).
16 * Copyright remains Eric Young's, and as such any Copyright notices in
17 * the code are not to be removed.
18 * If this package is used in a product, Eric Young should be given attribution
19 * as the author of the parts of the library used.
20 * This can be in the form of a textual message at program startup or
21 * in documentation (online or textual) provided with the package.
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
26 * 1. Redistributions of source code must retain the copyright
27 * notice, this list of conditions and the following disclaimer.
28 * 2. Redistributions in binary form must reproduce the above copyright
29 * notice, this list of conditions and the following disclaimer in the
30 * documentation and/or other materials provided with the distribution.
31 * 3. All advertising materials mentioning features or use of this software
32 * must display the following acknowledgement:
33 * "This product includes cryptographic software written by
34 * Eric Young (eay@cryptsoft.com)"
35 * The word 'cryptographic' can be left out if the rouines from the library
36 * being used are not cryptographic related :-).
37 * 4. If you include any Windows specific code (or a derivative thereof) from
38 * the apps directory (application code) you must include an acknowledgement:
39 * "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
41 * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
53 * The licence and distribution terms for any publically available version or
54 * derivative of this code cannot be changed. i.e. this code cannot simply be
55 * copied and put under another distribution licence
56 * [including the GNU Public Licence.]
59 #include <openssl/e_os2.h>
60 #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WIN32)
61 # ifdef OPENSSL_UNISTD
62 # include OPENSSL_UNISTD
67 * If unistd.h defines _POSIX_VERSION, we conclude that we are on a POSIX
68 * system and have sigaction and termios.
70 # if defined(_POSIX_VERSION)
73 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
80 /* Define this if you have sigaction() */
81 /* #define SIGACTION */
83 /* 06-Apr-92 Luke Brennan Support for VMS */
92 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
95 # pragma message disable DOLLARID
99 #ifdef WIN_CONSOLE_BUG
100 # include <windows.h>
101 # ifndef OPENSSL_SYS_WINCE
107 * There are 5 types of terminal interface supported, TERMIO, TERMIOS, VMS,
111 #if defined(__sgi) && !defined(TERMIOS)
117 #if defined(linux) && !defined(TERMIO)
129 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS)
135 #if defined(OPENSSL_SYS_VXWORKS)
142 # include <termios.h>
143 # define TTY_STRUCT struct termios
144 # define TTY_FLAGS c_lflag
145 # define TTY_get(tty,data) tcgetattr(tty,data)
146 # define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
151 # define TTY_STRUCT struct termio
152 # define TTY_FLAGS c_lflag
153 # define TTY_get(tty,data) ioctl(tty,TCGETA,data)
154 # define TTY_set(tty,data) ioctl(tty,TCSETA,data)
159 # define TTY_STRUCT struct sgttyb
160 # define TTY_FLAGS sg_flags
161 # define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
162 # define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
165 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
166 # include <sys/ioctl.h>
169 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WINCE)
171 # define fgets(a,b,c) noecho_fgets(a,b,c)
174 #ifdef OPENSSL_SYS_VMS
178 # include <descrip.h>
187 # define NX509_SIG 32
190 static void read_till_nl(FILE *);
191 static void recsig(int);
192 static void pushsig(void);
193 static void popsig(void);
194 #if defined(OPENSSL_SYS_MSDOS)
195 static int noecho_fgets(char *buf, int size, FILE *tty);
198 static struct sigaction savsig[NX509_SIG];
200 static void (*savsig[NX509_SIG]) (int);
204 int des_read_pw_string(char *buf, int length, const char *prompt, int verify)
210 des_read_pw(buf, buff, (length > BUFSIZ) ? BUFSIZ : length, prompt,
212 OPENSSL_cleanse(buff, BUFSIZ);
216 #ifdef OPENSSL_SYS_WINCE
218 int des_read_pw(char *buf, char *buff, int size, const char *prompt,
221 memset(buf, 0, size);
222 memset(buff, 0, size);
226 #else /* !OPENSSL_SYS_WINCE */
228 static void read_till_nl(FILE *in)
234 fgets(buf, SIZE, in);
235 } while (strchr(buf, '\n') == NULL);
238 /* return 0 if ok, 1 (or -1) otherwise */
239 int des_read_pw(char *buf, char *buff, int size, const char *prompt,
242 # ifdef OPENSSL_SYS_VMS
244 $DESCRIPTOR(terminal, "TT");
245 long tty_orig[3], tty_new[3];
247 unsigned short channel = 0;
249 # if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
250 TTY_STRUCT tty_orig, tty_new;
256 * statics are simply to avoid warnings about longjmp clobbering things
274 # ifdef OPENSSL_SYS_MSDOS
275 if ((tty = fopen("con", "r")) == NULL)
277 # elif defined(OPENSSL_SYS_VXWORKS)
280 if ((tty = fopen("/dev/tty", "r")) == NULL)
284 # if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
285 if (TTY_get(fileno(tty), &tty_orig) == -1) {
293 * Ariel Glenn ariel@columbia.edu reports that solaris can return
294 * EINVAL instead. This should be ok
302 memcpy(&(tty_new), &(tty_orig), sizeof(tty_orig));
304 # ifdef OPENSSL_SYS_VMS
305 status = sys$assign(&terminal, &channel, 0, 0);
306 if (status != SS$_NORMAL)
309 sys$qiow(0, channel, IO$_SENSEMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
311 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
319 tty_new.TTY_FLAGS &= ~ECHO;
322 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
323 if (is_a_tty && (TTY_set(fileno(tty), &tty_new) == -1))
326 # ifdef OPENSSL_SYS_VMS
327 tty_new[0] = tty_orig[0];
328 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
329 tty_new[2] = tty_orig[2];
331 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_new, 12, 0, 0, 0,
333 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
338 while ((!ok) && (number--)) {
339 fputs(prompt, stderr);
343 fgets(buf, size, tty);
348 if ((p = (char *)strchr(buf, '\n')) != NULL)
353 fprintf(stderr, "\nVerifying password - %s", prompt);
356 fgets(buff, size, tty);
359 if ((p = (char *)strchr(buff, '\n')) != NULL)
364 if (strcmp(buf, buff) != 0) {
365 fprintf(stderr, "\nVerify failure");
375 fprintf(stderr, "\n");
376 /* What can we do if there is an error? */
377 # if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
379 TTY_set(fileno(tty), &tty_orig);
381 # ifdef OPENSSL_SYS_VMS
384 sys$qiow(0, channel, IO$_SETMODE, &iosb, 0, 0, tty_orig, 12, 0, 0,
392 # ifdef OPENSSL_SYS_VMS
393 status = sys$dassgn(channel);
398 static void pushsig(void)
404 memset(&sa, 0, sizeof sa);
405 sa.sa_handler = recsig;
408 for (i = 1; i < NX509_SIG; i++) {
418 sigaction(i, &sa, &savsig[i]);
420 savsig[i] = signal(i, recsig);
425 signal(SIGWINCH, SIG_DFL);
429 static void popsig(void)
433 for (i = 1; i < NX509_SIG; i++) {
443 sigaction(i, &savsig[i], NULL);
445 signal(i, savsig[i]);
450 static void recsig(int i)
455 # ifdef OPENSSL_SYS_MSDOS
456 static int noecho_fgets(char *buf, int size, FILE *tty)
477 # ifdef WIN_CONSOLE_BUG
479 * Win95 has several evil console bugs: one of these is that the last
480 * character read using getch() is passed to the next read: this is
481 * usually a CR so this can be trouble. No STDIO fix seems to work but
482 * flushing the console appears to do the trick.
486 inh = GetStdHandle(STD_INPUT_HANDLE);
487 FlushConsoleInputBuffer(inh);
490 return (strlen(buf));
493 #endif /* !OPENSSL_SYS_WINCE */