xref: /onnv-gate/usr/src/common/openssl/crypto/des/read_pwd.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /* crypto/des/read_pwd.c */
2*0Sstevel@tonic-gate /* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
3*0Sstevel@tonic-gate  * All rights reserved.
4*0Sstevel@tonic-gate  *
5*0Sstevel@tonic-gate  * This package is an SSL implementation written
6*0Sstevel@tonic-gate  * by Eric Young (eay@cryptsoft.com).
7*0Sstevel@tonic-gate  * The implementation was written so as to conform with Netscapes SSL.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * This library is free for commercial and non-commercial use as long as
10*0Sstevel@tonic-gate  * the following conditions are aheared to.  The following conditions
11*0Sstevel@tonic-gate  * apply to all code found in this distribution, be it the RC4, RSA,
12*0Sstevel@tonic-gate  * lhash, DES, etc., code; not just the SSL code.  The SSL documentation
13*0Sstevel@tonic-gate  * included with this distribution is covered by the same copyright terms
14*0Sstevel@tonic-gate  * except that the holder is Tim Hudson (tjh@cryptsoft.com).
15*0Sstevel@tonic-gate  *
16*0Sstevel@tonic-gate  * Copyright remains Eric Young's, and as such any Copyright notices in
17*0Sstevel@tonic-gate  * the code are not to be removed.
18*0Sstevel@tonic-gate  * If this package is used in a product, Eric Young should be given attribution
19*0Sstevel@tonic-gate  * as the author of the parts of the library used.
20*0Sstevel@tonic-gate  * This can be in the form of a textual message at program startup or
21*0Sstevel@tonic-gate  * in documentation (online or textual) provided with the package.
22*0Sstevel@tonic-gate  *
23*0Sstevel@tonic-gate  * Redistribution and use in source and binary forms, with or without
24*0Sstevel@tonic-gate  * modification, are permitted provided that the following conditions
25*0Sstevel@tonic-gate  * are met:
26*0Sstevel@tonic-gate  * 1. Redistributions of source code must retain the copyright
27*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer.
28*0Sstevel@tonic-gate  * 2. Redistributions in binary form must reproduce the above copyright
29*0Sstevel@tonic-gate  *    notice, this list of conditions and the following disclaimer in the
30*0Sstevel@tonic-gate  *    documentation and/or other materials provided with the distribution.
31*0Sstevel@tonic-gate  * 3. All advertising materials mentioning features or use of this software
32*0Sstevel@tonic-gate  *    must display the following acknowledgement:
33*0Sstevel@tonic-gate  *    "This product includes cryptographic software written by
34*0Sstevel@tonic-gate  *     Eric Young (eay@cryptsoft.com)"
35*0Sstevel@tonic-gate  *    The word 'cryptographic' can be left out if the rouines from the library
36*0Sstevel@tonic-gate  *    being used are not cryptographic related :-).
37*0Sstevel@tonic-gate  * 4. If you include any Windows specific code (or a derivative thereof) from
38*0Sstevel@tonic-gate  *    the apps directory (application code) you must include an acknowledgement:
39*0Sstevel@tonic-gate  *    "This product includes software written by Tim Hudson (tjh@cryptsoft.com)"
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * THIS SOFTWARE IS PROVIDED BY ERIC YOUNG ``AS IS'' AND
42*0Sstevel@tonic-gate  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43*0Sstevel@tonic-gate  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44*0Sstevel@tonic-gate  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
45*0Sstevel@tonic-gate  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46*0Sstevel@tonic-gate  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47*0Sstevel@tonic-gate  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48*0Sstevel@tonic-gate  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49*0Sstevel@tonic-gate  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50*0Sstevel@tonic-gate  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51*0Sstevel@tonic-gate  * SUCH DAMAGE.
52*0Sstevel@tonic-gate  *
53*0Sstevel@tonic-gate  * The licence and distribution terms for any publically available version or
54*0Sstevel@tonic-gate  * derivative of this code cannot be changed.  i.e. this code cannot simply be
55*0Sstevel@tonic-gate  * copied and put under another distribution licence
56*0Sstevel@tonic-gate  * [including the GNU Public Licence.]
57*0Sstevel@tonic-gate  */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate #include <openssl/e_os2.h>
60*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WIN32)
61*0Sstevel@tonic-gate #ifdef OPENSSL_UNISTD
62*0Sstevel@tonic-gate # include OPENSSL_UNISTD
63*0Sstevel@tonic-gate #else
64*0Sstevel@tonic-gate # include <unistd.h>
65*0Sstevel@tonic-gate #endif
66*0Sstevel@tonic-gate /* If unistd.h defines _POSIX_VERSION, we conclude that we
67*0Sstevel@tonic-gate  * are on a POSIX system and have sigaction and termios. */
68*0Sstevel@tonic-gate #if defined(_POSIX_VERSION)
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate # define SIGACTION
71*0Sstevel@tonic-gate # if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
72*0Sstevel@tonic-gate # define TERMIOS
73*0Sstevel@tonic-gate # endif
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate #endif
76*0Sstevel@tonic-gate #endif
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate /* #define SIGACTION */ /* Define this if you have sigaction() */
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate #ifdef WIN16TTY
81*0Sstevel@tonic-gate #undef OPENSSL_SYS_WIN16
82*0Sstevel@tonic-gate #undef _WINDOWS
83*0Sstevel@tonic-gate #include <graph.h>
84*0Sstevel@tonic-gate #endif
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /* 06-Apr-92 Luke Brennan    Support for VMS */
87*0Sstevel@tonic-gate #include "des_locl.h"
88*0Sstevel@tonic-gate #include "cryptlib.h"
89*0Sstevel@tonic-gate #include <signal.h>
90*0Sstevel@tonic-gate #include <stdio.h>
91*0Sstevel@tonic-gate #include <string.h>
92*0Sstevel@tonic-gate #include <setjmp.h>
93*0Sstevel@tonic-gate #include <errno.h>
94*0Sstevel@tonic-gate 
95*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS			/* prototypes for sys$whatever */
96*0Sstevel@tonic-gate #include <starlet.h>
97*0Sstevel@tonic-gate #ifdef __DECC
98*0Sstevel@tonic-gate #pragma message disable DOLLARID
99*0Sstevel@tonic-gate #endif
100*0Sstevel@tonic-gate #endif
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate #ifdef WIN_CONSOLE_BUG
103*0Sstevel@tonic-gate #include <windows.h>
104*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_WINCE
105*0Sstevel@tonic-gate #include <wincon.h>
106*0Sstevel@tonic-gate #endif
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate 
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /* There are 5 types of terminal interface supported,
111*0Sstevel@tonic-gate  * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate #if defined(__sgi) && !defined(TERMIOS)
115*0Sstevel@tonic-gate #define TERMIOS
116*0Sstevel@tonic-gate #undef  TERMIO
117*0Sstevel@tonic-gate #undef  SGTTY
118*0Sstevel@tonic-gate #endif
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate #if defined(linux) && !defined(TERMIO)
121*0Sstevel@tonic-gate #undef  TERMIOS
122*0Sstevel@tonic-gate #define TERMIO
123*0Sstevel@tonic-gate #undef  SGTTY
124*0Sstevel@tonic-gate #endif
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate #ifdef _LIBC
127*0Sstevel@tonic-gate #undef  TERMIOS
128*0Sstevel@tonic-gate #define TERMIO
129*0Sstevel@tonic-gate #undef  SGTTY
130*0Sstevel@tonic-gate #endif
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate #if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(MAC_OS_pre_X) && !defined(MAC_OS_GUSI_SOURCE)
133*0Sstevel@tonic-gate #undef  TERMIOS
134*0Sstevel@tonic-gate #undef  TERMIO
135*0Sstevel@tonic-gate #define SGTTY
136*0Sstevel@tonic-gate #endif
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_VXWORKS)
139*0Sstevel@tonic-gate #undef TERMIOS
140*0Sstevel@tonic-gate #undef TERMIO
141*0Sstevel@tonic-gate #undef SGTTY
142*0Sstevel@tonic-gate #endif
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate #ifdef TERMIOS
145*0Sstevel@tonic-gate #include <termios.h>
146*0Sstevel@tonic-gate #define TTY_STRUCT		struct termios
147*0Sstevel@tonic-gate #define TTY_FLAGS		c_lflag
148*0Sstevel@tonic-gate #define	TTY_get(tty,data)	tcgetattr(tty,data)
149*0Sstevel@tonic-gate #define TTY_set(tty,data)	tcsetattr(tty,TCSANOW,data)
150*0Sstevel@tonic-gate #endif
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate #ifdef TERMIO
153*0Sstevel@tonic-gate #include <termio.h>
154*0Sstevel@tonic-gate #define TTY_STRUCT		struct termio
155*0Sstevel@tonic-gate #define TTY_FLAGS		c_lflag
156*0Sstevel@tonic-gate #define TTY_get(tty,data)	ioctl(tty,TCGETA,data)
157*0Sstevel@tonic-gate #define TTY_set(tty,data)	ioctl(tty,TCSETA,data)
158*0Sstevel@tonic-gate #endif
159*0Sstevel@tonic-gate 
160*0Sstevel@tonic-gate #ifdef SGTTY
161*0Sstevel@tonic-gate #include <sgtty.h>
162*0Sstevel@tonic-gate #define TTY_STRUCT		struct sgttyb
163*0Sstevel@tonic-gate #define TTY_FLAGS		sg_flags
164*0Sstevel@tonic-gate #define TTY_get(tty,data)	ioctl(tty,TIOCGETP,data)
165*0Sstevel@tonic-gate #define TTY_set(tty,data)	ioctl(tty,TIOCSETP,data)
166*0Sstevel@tonic-gate #endif
167*0Sstevel@tonic-gate 
168*0Sstevel@tonic-gate #if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(MAC_OS_pre_X)
169*0Sstevel@tonic-gate #include <sys/ioctl.h>
170*0Sstevel@tonic-gate #endif
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_MSDOS) && !defined(__CYGWIN32__) && !defined(OPENSSL_SYS_WINCE)
173*0Sstevel@tonic-gate #include <conio.h>
174*0Sstevel@tonic-gate #define fgets(a,b,c) noecho_fgets(a,b,c)
175*0Sstevel@tonic-gate #endif
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
178*0Sstevel@tonic-gate #include <ssdef.h>
179*0Sstevel@tonic-gate #include <iodef.h>
180*0Sstevel@tonic-gate #include <ttdef.h>
181*0Sstevel@tonic-gate #include <descrip.h>
182*0Sstevel@tonic-gate struct IOSB {
183*0Sstevel@tonic-gate 	short iosb$w_value;
184*0Sstevel@tonic-gate 	short iosb$w_count;
185*0Sstevel@tonic-gate 	long  iosb$l_info;
186*0Sstevel@tonic-gate 	};
187*0Sstevel@tonic-gate #endif
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate #if defined(MAC_OS_pre_X) || defined(MAC_OS_GUSI_SOURCE)
190*0Sstevel@tonic-gate /*
191*0Sstevel@tonic-gate  * This one needs work. As a matter of fact the code is unoperational
192*0Sstevel@tonic-gate  * and this is only a trick to get it compiled.
193*0Sstevel@tonic-gate  *					<appro@fy.chalmers.se>
194*0Sstevel@tonic-gate  */
195*0Sstevel@tonic-gate #define TTY_STRUCT int
196*0Sstevel@tonic-gate #endif
197*0Sstevel@tonic-gate 
198*0Sstevel@tonic-gate #ifndef NX509_SIG
199*0Sstevel@tonic-gate #define NX509_SIG 32
200*0Sstevel@tonic-gate #endif
201*0Sstevel@tonic-gate 
202*0Sstevel@tonic-gate static void read_till_nl(FILE *);
203*0Sstevel@tonic-gate static void recsig(int);
204*0Sstevel@tonic-gate static void pushsig(void);
205*0Sstevel@tonic-gate static void popsig(void);
206*0Sstevel@tonic-gate #if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
207*0Sstevel@tonic-gate static int noecho_fgets(char *buf, int size, FILE *tty);
208*0Sstevel@tonic-gate #endif
209*0Sstevel@tonic-gate #ifdef SIGACTION
210*0Sstevel@tonic-gate  static struct sigaction savsig[NX509_SIG];
211*0Sstevel@tonic-gate #else
212*0Sstevel@tonic-gate   static void (*savsig[NX509_SIG])(int );
213*0Sstevel@tonic-gate #endif
214*0Sstevel@tonic-gate static jmp_buf save;
215*0Sstevel@tonic-gate 
des_read_pw_string(char * buf,int length,const char * prompt,int verify)216*0Sstevel@tonic-gate int des_read_pw_string(char *buf, int length, const char *prompt,
217*0Sstevel@tonic-gate 	     int verify)
218*0Sstevel@tonic-gate 	{
219*0Sstevel@tonic-gate 	char buff[BUFSIZ];
220*0Sstevel@tonic-gate 	int ret;
221*0Sstevel@tonic-gate 
222*0Sstevel@tonic-gate 	ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
223*0Sstevel@tonic-gate 	OPENSSL_cleanse(buff,BUFSIZ);
224*0Sstevel@tonic-gate 	return(ret);
225*0Sstevel@tonic-gate 	}
226*0Sstevel@tonic-gate 
227*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_WINCE
228*0Sstevel@tonic-gate 
des_read_pw(char * buf,char * buff,int size,const char * prompt,int verify)229*0Sstevel@tonic-gate int des_read_pw(char *buf, char *buff, int size, const char *prompt, int verify)
230*0Sstevel@tonic-gate 	{
231*0Sstevel@tonic-gate 	memset(buf,0,size);
232*0Sstevel@tonic-gate 	memset(buff,0,size);
233*0Sstevel@tonic-gate 	return(0);
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 
236*0Sstevel@tonic-gate #elif defined(OPENSSL_SYS_WIN16)
237*0Sstevel@tonic-gate 
des_read_pw(char * buf,char * buff,int size,char * prompt,int verify)238*0Sstevel@tonic-gate int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
239*0Sstevel@tonic-gate 	{
240*0Sstevel@tonic-gate 	memset(buf,0,size);
241*0Sstevel@tonic-gate 	memset(buff,0,size);
242*0Sstevel@tonic-gate 	return(0);
243*0Sstevel@tonic-gate 	}
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate #else /* !OPENSSL_SYS_WINCE && !OPENSSL_SYS_WIN16 */
246*0Sstevel@tonic-gate 
read_till_nl(FILE * in)247*0Sstevel@tonic-gate static void read_till_nl(FILE *in)
248*0Sstevel@tonic-gate 	{
249*0Sstevel@tonic-gate #define SIZE 4
250*0Sstevel@tonic-gate 	char buf[SIZE+1];
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	do	{
253*0Sstevel@tonic-gate 		fgets(buf,SIZE,in);
254*0Sstevel@tonic-gate 		} while (strchr(buf,'\n') == NULL);
255*0Sstevel@tonic-gate 	}
256*0Sstevel@tonic-gate 
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate /* return 0 if ok, 1 (or -1) otherwise */
des_read_pw(char * buf,char * buff,int size,const char * prompt,int verify)259*0Sstevel@tonic-gate int des_read_pw(char *buf, char *buff, int size, const char *prompt,
260*0Sstevel@tonic-gate 	     int verify)
261*0Sstevel@tonic-gate 	{
262*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
263*0Sstevel@tonic-gate 	struct IOSB iosb;
264*0Sstevel@tonic-gate 	$DESCRIPTOR(terminal,"TT");
265*0Sstevel@tonic-gate 	long tty_orig[3], tty_new[3];
266*0Sstevel@tonic-gate 	long status;
267*0Sstevel@tonic-gate 	unsigned short channel = 0;
268*0Sstevel@tonic-gate #else
269*0Sstevel@tonic-gate #if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
270*0Sstevel@tonic-gate 	TTY_STRUCT tty_orig,tty_new;
271*0Sstevel@tonic-gate #endif
272*0Sstevel@tonic-gate #endif
273*0Sstevel@tonic-gate 	int number;
274*0Sstevel@tonic-gate 	int ok;
275*0Sstevel@tonic-gate 	/* statics are simply to avoid warnings about longjmp clobbering
276*0Sstevel@tonic-gate 	   things */
277*0Sstevel@tonic-gate 	static int ps;
278*0Sstevel@tonic-gate 	int is_a_tty;
279*0Sstevel@tonic-gate 	static FILE *tty;
280*0Sstevel@tonic-gate 	char *p;
281*0Sstevel@tonic-gate 
282*0Sstevel@tonic-gate 	if (setjmp(save))
283*0Sstevel@tonic-gate 		{
284*0Sstevel@tonic-gate 		ok=0;
285*0Sstevel@tonic-gate 		goto error;
286*0Sstevel@tonic-gate 		}
287*0Sstevel@tonic-gate 
288*0Sstevel@tonic-gate 	number=5;
289*0Sstevel@tonic-gate 	ok=0;
290*0Sstevel@tonic-gate 	ps=0;
291*0Sstevel@tonic-gate 	is_a_tty=1;
292*0Sstevel@tonic-gate 	tty=NULL;
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_MSDOS
295*0Sstevel@tonic-gate 	if ((tty=fopen("con","r")) == NULL)
296*0Sstevel@tonic-gate 		tty=stdin;
297*0Sstevel@tonic-gate #elif defined(MAC_OS_pre_X) || defined(OPENSSL_SYS_VXWORKS)
298*0Sstevel@tonic-gate 	tty=stdin;
299*0Sstevel@tonic-gate #else
300*0Sstevel@tonic-gate #ifndef OPENSSL_SYS_MPE
301*0Sstevel@tonic-gate 	if ((tty=fopen("/dev/tty","r")) == NULL)
302*0Sstevel@tonic-gate #endif
303*0Sstevel@tonic-gate 		tty=stdin;
304*0Sstevel@tonic-gate #endif
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate #if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
307*0Sstevel@tonic-gate 	if (TTY_get(fileno(tty),&tty_orig) == -1)
308*0Sstevel@tonic-gate 		{
309*0Sstevel@tonic-gate #ifdef ENOTTY
310*0Sstevel@tonic-gate 		if (errno == ENOTTY)
311*0Sstevel@tonic-gate 			is_a_tty=0;
312*0Sstevel@tonic-gate 		else
313*0Sstevel@tonic-gate #endif
314*0Sstevel@tonic-gate #ifdef EINVAL
315*0Sstevel@tonic-gate 		/* Ariel Glenn ariel@columbia.edu reports that solaris
316*0Sstevel@tonic-gate 		 * can return EINVAL instead.  This should be ok */
317*0Sstevel@tonic-gate 		if (errno == EINVAL)
318*0Sstevel@tonic-gate 			is_a_tty=0;
319*0Sstevel@tonic-gate 		else
320*0Sstevel@tonic-gate #endif
321*0Sstevel@tonic-gate 			return(-1);
322*0Sstevel@tonic-gate 		}
323*0Sstevel@tonic-gate 	memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
324*0Sstevel@tonic-gate #endif
325*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
326*0Sstevel@tonic-gate 	status = sys$assign(&terminal,&channel,0,0);
327*0Sstevel@tonic-gate 	if (status != SS$_NORMAL)
328*0Sstevel@tonic-gate 		return(-1);
329*0Sstevel@tonic-gate 	status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
330*0Sstevel@tonic-gate 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
331*0Sstevel@tonic-gate 		return(-1);
332*0Sstevel@tonic-gate #endif
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	pushsig();
335*0Sstevel@tonic-gate 	ps=1;
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate #ifdef TTY_FLAGS
338*0Sstevel@tonic-gate 	tty_new.TTY_FLAGS &= ~ECHO;
339*0Sstevel@tonic-gate #endif
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
342*0Sstevel@tonic-gate 	if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
343*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_MPE
344*0Sstevel@tonic-gate 		; /* MPE lies -- echo really has been disabled */
345*0Sstevel@tonic-gate #else
346*0Sstevel@tonic-gate 		return(-1);
347*0Sstevel@tonic-gate #endif
348*0Sstevel@tonic-gate #endif
349*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
350*0Sstevel@tonic-gate 	tty_new[0] = tty_orig[0];
351*0Sstevel@tonic-gate 	tty_new[1] = tty_orig[1] | TT$M_NOECHO;
352*0Sstevel@tonic-gate 	tty_new[2] = tty_orig[2];
353*0Sstevel@tonic-gate 	status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
354*0Sstevel@tonic-gate 	if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
355*0Sstevel@tonic-gate 		return(-1);
356*0Sstevel@tonic-gate #endif
357*0Sstevel@tonic-gate 	ps=2;
358*0Sstevel@tonic-gate 
359*0Sstevel@tonic-gate 	while ((!ok) && (number--))
360*0Sstevel@tonic-gate 		{
361*0Sstevel@tonic-gate 		fputs(prompt,stderr);
362*0Sstevel@tonic-gate 		fflush(stderr);
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate 		buf[0]='\0';
365*0Sstevel@tonic-gate 		fgets(buf,size,tty);
366*0Sstevel@tonic-gate 		if (feof(tty)) goto error;
367*0Sstevel@tonic-gate 		if (ferror(tty)) goto error;
368*0Sstevel@tonic-gate 		if ((p=(char *)strchr(buf,'\n')) != NULL)
369*0Sstevel@tonic-gate 			*p='\0';
370*0Sstevel@tonic-gate 		else	read_till_nl(tty);
371*0Sstevel@tonic-gate 		if (verify)
372*0Sstevel@tonic-gate 			{
373*0Sstevel@tonic-gate 			fprintf(stderr,"\nVerifying password - %s",prompt);
374*0Sstevel@tonic-gate 			fflush(stderr);
375*0Sstevel@tonic-gate 			buff[0]='\0';
376*0Sstevel@tonic-gate 			fgets(buff,size,tty);
377*0Sstevel@tonic-gate 			if (feof(tty)) goto error;
378*0Sstevel@tonic-gate 			if ((p=(char *)strchr(buff,'\n')) != NULL)
379*0Sstevel@tonic-gate 				*p='\0';
380*0Sstevel@tonic-gate 			else	read_till_nl(tty);
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 			if (strcmp(buf,buff) != 0)
383*0Sstevel@tonic-gate 				{
384*0Sstevel@tonic-gate 				fprintf(stderr,"\nVerify failure");
385*0Sstevel@tonic-gate 				fflush(stderr);
386*0Sstevel@tonic-gate 				break;
387*0Sstevel@tonic-gate 				/* continue; */
388*0Sstevel@tonic-gate 				}
389*0Sstevel@tonic-gate 			}
390*0Sstevel@tonic-gate 		ok=1;
391*0Sstevel@tonic-gate 		}
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate error:
394*0Sstevel@tonic-gate 	fprintf(stderr,"\n");
395*0Sstevel@tonic-gate #if 0
396*0Sstevel@tonic-gate 	perror("fgets(tty)");
397*0Sstevel@tonic-gate #endif
398*0Sstevel@tonic-gate 	/* What can we do if there is an error? */
399*0Sstevel@tonic-gate #if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
400*0Sstevel@tonic-gate 	if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
401*0Sstevel@tonic-gate #endif
402*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
403*0Sstevel@tonic-gate 	if (ps >= 2)
404*0Sstevel@tonic-gate 		status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0
405*0Sstevel@tonic-gate 			,tty_orig,12,0,0,0,0);
406*0Sstevel@tonic-gate #endif
407*0Sstevel@tonic-gate 
408*0Sstevel@tonic-gate 	if (ps >= 1) popsig();
409*0Sstevel@tonic-gate 	if (stdin != tty) fclose(tty);
410*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_VMS
411*0Sstevel@tonic-gate 	status = sys$dassgn(channel);
412*0Sstevel@tonic-gate #endif
413*0Sstevel@tonic-gate 	return(!ok);
414*0Sstevel@tonic-gate 	}
415*0Sstevel@tonic-gate 
pushsig(void)416*0Sstevel@tonic-gate static void pushsig(void)
417*0Sstevel@tonic-gate 	{
418*0Sstevel@tonic-gate 	int i;
419*0Sstevel@tonic-gate #ifdef SIGACTION
420*0Sstevel@tonic-gate 	struct sigaction sa;
421*0Sstevel@tonic-gate 
422*0Sstevel@tonic-gate 	memset(&sa,0,sizeof sa);
423*0Sstevel@tonic-gate 	sa.sa_handler=recsig;
424*0Sstevel@tonic-gate #endif
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	for (i=1; i<NX509_SIG; i++)
427*0Sstevel@tonic-gate 		{
428*0Sstevel@tonic-gate #ifdef SIGUSR1
429*0Sstevel@tonic-gate 		if (i == SIGUSR1)
430*0Sstevel@tonic-gate 			continue;
431*0Sstevel@tonic-gate #endif
432*0Sstevel@tonic-gate #ifdef SIGUSR2
433*0Sstevel@tonic-gate 		if (i == SIGUSR2)
434*0Sstevel@tonic-gate 			continue;
435*0Sstevel@tonic-gate #endif
436*0Sstevel@tonic-gate #ifdef SIGACTION
437*0Sstevel@tonic-gate 		sigaction(i,&sa,&savsig[i]);
438*0Sstevel@tonic-gate #else
439*0Sstevel@tonic-gate 		savsig[i]=signal(i,recsig);
440*0Sstevel@tonic-gate #endif
441*0Sstevel@tonic-gate 		}
442*0Sstevel@tonic-gate 
443*0Sstevel@tonic-gate #ifdef SIGWINCH
444*0Sstevel@tonic-gate 	signal(SIGWINCH,SIG_DFL);
445*0Sstevel@tonic-gate #endif
446*0Sstevel@tonic-gate 	}
447*0Sstevel@tonic-gate 
popsig(void)448*0Sstevel@tonic-gate static void popsig(void)
449*0Sstevel@tonic-gate 	{
450*0Sstevel@tonic-gate 	int i;
451*0Sstevel@tonic-gate 
452*0Sstevel@tonic-gate 	for (i=1; i<NX509_SIG; i++)
453*0Sstevel@tonic-gate 		{
454*0Sstevel@tonic-gate #ifdef SIGUSR1
455*0Sstevel@tonic-gate 		if (i == SIGUSR1)
456*0Sstevel@tonic-gate 			continue;
457*0Sstevel@tonic-gate #endif
458*0Sstevel@tonic-gate #ifdef SIGUSR2
459*0Sstevel@tonic-gate 		if (i == SIGUSR2)
460*0Sstevel@tonic-gate 			continue;
461*0Sstevel@tonic-gate #endif
462*0Sstevel@tonic-gate #ifdef SIGACTION
463*0Sstevel@tonic-gate 		sigaction(i,&savsig[i],NULL);
464*0Sstevel@tonic-gate #else
465*0Sstevel@tonic-gate 		signal(i,savsig[i]);
466*0Sstevel@tonic-gate #endif
467*0Sstevel@tonic-gate 		}
468*0Sstevel@tonic-gate 	}
469*0Sstevel@tonic-gate 
recsig(int i)470*0Sstevel@tonic-gate static void recsig(int i)
471*0Sstevel@tonic-gate 	{
472*0Sstevel@tonic-gate 	longjmp(save,1);
473*0Sstevel@tonic-gate #ifdef LINT
474*0Sstevel@tonic-gate 	i=i;
475*0Sstevel@tonic-gate #endif
476*0Sstevel@tonic-gate 	}
477*0Sstevel@tonic-gate 
478*0Sstevel@tonic-gate #ifdef OPENSSL_SYS_MSDOS
noecho_fgets(char * buf,int size,FILE * tty)479*0Sstevel@tonic-gate static int noecho_fgets(char *buf, int size, FILE *tty)
480*0Sstevel@tonic-gate 	{
481*0Sstevel@tonic-gate 	int i;
482*0Sstevel@tonic-gate 	char *p;
483*0Sstevel@tonic-gate 
484*0Sstevel@tonic-gate 	p=buf;
485*0Sstevel@tonic-gate 	for (;;)
486*0Sstevel@tonic-gate 		{
487*0Sstevel@tonic-gate 		if (size == 0)
488*0Sstevel@tonic-gate 			{
489*0Sstevel@tonic-gate 			*p='\0';
490*0Sstevel@tonic-gate 			break;
491*0Sstevel@tonic-gate 			}
492*0Sstevel@tonic-gate 		size--;
493*0Sstevel@tonic-gate #ifdef WIN16TTY
494*0Sstevel@tonic-gate 		i=_inchar();
495*0Sstevel@tonic-gate #else
496*0Sstevel@tonic-gate 		i=getch();
497*0Sstevel@tonic-gate #endif
498*0Sstevel@tonic-gate 		if (i == '\r') i='\n';
499*0Sstevel@tonic-gate 		*(p++)=i;
500*0Sstevel@tonic-gate 		if (i == '\n')
501*0Sstevel@tonic-gate 			{
502*0Sstevel@tonic-gate 			*p='\0';
503*0Sstevel@tonic-gate 			break;
504*0Sstevel@tonic-gate 			}
505*0Sstevel@tonic-gate 		}
506*0Sstevel@tonic-gate #ifdef WIN_CONSOLE_BUG
507*0Sstevel@tonic-gate /* Win95 has several evil console bugs: one of these is that the
508*0Sstevel@tonic-gate  * last character read using getch() is passed to the next read: this is
509*0Sstevel@tonic-gate  * usually a CR so this can be trouble. No STDIO fix seems to work but
510*0Sstevel@tonic-gate  * flushing the console appears to do the trick.
511*0Sstevel@tonic-gate  */
512*0Sstevel@tonic-gate 		{
513*0Sstevel@tonic-gate 			HANDLE inh;
514*0Sstevel@tonic-gate 			inh = GetStdHandle(STD_INPUT_HANDLE);
515*0Sstevel@tonic-gate 			FlushConsoleInputBuffer(inh);
516*0Sstevel@tonic-gate 		}
517*0Sstevel@tonic-gate #endif
518*0Sstevel@tonic-gate 	return(strlen(buf));
519*0Sstevel@tonic-gate 	}
520*0Sstevel@tonic-gate #endif
521*0Sstevel@tonic-gate #endif /* !OPENSSL_SYS_WINCE && !WIN16 */
522