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)
62 # include OPENSSL_UNISTD
66 /* If unistd.h defines _POSIX_VERSION, we conclude that we
67 * are on a POSIX system and have sigaction and termios. */
68 #if defined(_POSIX_VERSION)
71 # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
78 /* #define SIGACTION */ /* Define this if you have sigaction() */
81 #undef OPENSSL_SYS_WIN16
86 /* 06-Apr-92 Luke Brennan Support for VMS */
95 #ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
98 #pragma message disable DOLLARID
102 #ifdef WIN_CONSOLE_BUG
104 #ifndef OPENSSL_SYS_WINCE
110 /* There are 5 types of terminal interface supported,
111 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
114 #if defined(__sgi) && !defined(TERMIOS)
120 #if defined(linux) && !defined(TERMIO)
132 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS)
138 #if defined(OPENSSL_SYS_VXWORKS)
146 #define TTY_STRUCT struct termios
147 #define TTY_FLAGS c_lflag
148 #define TTY_get(tty,data) tcgetattr(tty,data)
149 #define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
154 #define TTY_STRUCT struct termio
155 #define TTY_FLAGS c_lflag
156 #define TTY_get(tty,data) ioctl(tty,TCGETA,data)
157 #define TTY_set(tty,data) ioctl(tty,TCSETA,data)
162 #define TTY_STRUCT struct sgttyb
163 #define TTY_FLAGS sg_flags
164 #define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
165 #define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
168 #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS)
169 #include <sys/ioctl.h>
172 #if defined(OPENSSL_SYS_MSDOS) && !defined(__CYGWIN32__) && !defined(OPENSSL_SYS_WINCE)
174 #define fgets(a,b,c) noecho_fgets(a,b,c)
177 #ifdef OPENSSL_SYS_VMS
194 static void read_till_nl(FILE *);
195 static void recsig(int);
196 static void pushsig(void);
197 static void popsig(void);
198 #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
199 static int noecho_fgets(char *buf, int size, FILE *tty);
202 static struct sigaction savsig[NX509_SIG];
204 static void (*savsig[NX509_SIG])(int );
208 int des_read_pw_string(char *buf, int length, const char *prompt,
214 ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
215 OPENSSL_cleanse(buff,BUFSIZ);
219 #ifdef OPENSSL_SYS_WINCE
221 int des_read_pw(char *buf, char *buff, int size, const char *prompt, int verify)
228 #elif defined(OPENSSL_SYS_WIN16)
230 int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
237 #else /* !OPENSSL_SYS_WINCE && !OPENSSL_SYS_WIN16 */
239 static void read_till_nl(FILE *in)
246 } while (strchr(buf,'\n') == NULL);
250 /* return 0 if ok, 1 (or -1) otherwise */
251 int des_read_pw(char *buf, char *buff, int size, const char *prompt,
254 #ifdef OPENSSL_SYS_VMS
256 $DESCRIPTOR(terminal,"TT");
257 long tty_orig[3], tty_new[3];
259 unsigned short channel = 0;
261 #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
262 TTY_STRUCT tty_orig,tty_new;
267 /* statics are simply to avoid warnings about longjmp clobbering
286 #ifdef OPENSSL_SYS_MSDOS
287 if ((tty=fopen("con","r")) == NULL)
289 #elif defined(OPENSSL_SYS_VXWORKS)
292 if ((tty=fopen("/dev/tty","r")) == NULL)
296 #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
297 if (TTY_get(fileno(tty),&tty_orig) == -1)
305 /* Ariel Glenn ariel@columbia.edu reports that solaris
306 * can return EINVAL instead. This should be ok */
313 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
315 #ifdef OPENSSL_SYS_VMS
316 status = sys$assign(&terminal,&channel,0,0);
317 if (status != SS$_NORMAL)
319 status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
320 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
328 tty_new.TTY_FLAGS &= ~ECHO;
331 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
332 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
335 #ifdef OPENSSL_SYS_VMS
336 tty_new[0] = tty_orig[0];
337 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
338 tty_new[2] = tty_orig[2];
339 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
340 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
345 while ((!ok) && (number--))
347 fputs(prompt,stderr);
352 if (feof(tty)) goto error;
353 if (ferror(tty)) goto error;
354 if ((p=(char *)strchr(buf,'\n')) != NULL)
356 else read_till_nl(tty);
359 fprintf(stderr,"\nVerifying password - %s",prompt);
362 fgets(buff,size,tty);
363 if (feof(tty)) goto error;
364 if ((p=(char *)strchr(buff,'\n')) != NULL)
366 else read_till_nl(tty);
368 if (strcmp(buf,buff) != 0)
370 fprintf(stderr,"\nVerify failure");
380 fprintf(stderr,"\n");
382 perror("fgets(tty)");
384 /* What can we do if there is an error? */
385 #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
386 if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
388 #ifdef OPENSSL_SYS_VMS
390 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0
391 ,tty_orig,12,0,0,0,0);
394 if (ps >= 1) popsig();
395 if (stdin != tty) fclose(tty);
396 #ifdef OPENSSL_SYS_VMS
397 status = sys$dassgn(channel);
402 static void pushsig(void)
408 memset(&sa,0,sizeof sa);
409 sa.sa_handler=recsig;
412 for (i=1; i<NX509_SIG; i++)
423 sigaction(i,&sa,&savsig[i]);
425 savsig[i]=signal(i,recsig);
430 signal(SIGWINCH,SIG_DFL);
434 static void popsig(void)
438 for (i=1; i<NX509_SIG; i++)
449 sigaction(i,&savsig[i],NULL);
456 static void recsig(int i)
461 #ifdef OPENSSL_SYS_MSDOS
462 static int noecho_fgets(char *buf, int size, FILE *tty)
481 if (i == '\r') i='\n';
489 #ifdef WIN_CONSOLE_BUG
490 /* Win95 has several evil console bugs: one of these is that the
491 * last character read using getch() is passed to the next read: this is
492 * usually a CR so this can be trouble. No STDIO fix seems to work but
493 * flushing the console appears to do the trick.
497 inh = GetStdHandle(STD_INPUT_HANDLE);
498 FlushConsoleInputBuffer(inh);
504 #endif /* !OPENSSL_SYS_WINCE && !WIN16 */