1 /* crypto/des/read_pwd.c */
2 /* Copyright (C) 1995-1997 Eric Young (eay@mincom.oz.au)
5 * This package is an SSL implementation written
6 * by Eric Young (eay@mincom.oz.au).
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@mincom.oz.au).
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@mincom.oz.au)"
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@mincom.oz.au)"
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.]
65 /* 06-Apr-92 Luke Brennan Support for VMS */
72 /* There are 5 types of terminal interface supported,
73 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
76 #if defined(__sgi) && !defined(TERMIOS)
82 #if defined(linux) && !defined(TERMIO)
92 #if !defined(TERMIO) && !defined(TERMIOS) && !defined(VMS) && !defined(MSDOS)
96 #if defined(HAVE_SGTTY_H) && defined(__NeXT__)
102 #define TTY_STRUCT struct termios
103 #define TTY_FLAGS c_lflag
104 #define TTY_get(tty,data) tcgetattr(tty,data)
105 #define TTY_set(tty,data) tcsetattr(tty,TCSANOW,data)
110 #define TTY_STRUCT struct termio
111 #define TTY_FLAGS c_lflag
112 #define TTY_get(tty,data) ioctl(tty,TCGETA,data)
113 #define TTY_set(tty,data) ioctl(tty,TCSETA,data)
118 #define TTY_STRUCT struct sgttyb
119 #define TTY_FLAGS sg_flags
120 #define TTY_get(tty,data) ioctl(tty,TIOCGETP,data)
121 #define TTY_set(tty,data) ioctl(tty,TIOCSETP,data)
124 #if !defined(_LIBC) && !defined(MSDOS) && !defined(VMS)
125 #include <sys/ioctl.h>
130 #define fgets(a,b,c) noecho_fgets(a,b,c)
150 static void read_till_nl(FILE *);
151 static int read_pw(char *buf
, char *buff
, int size
, char *prompt
, int verify
);
152 static void recsig(int);
153 static void pushsig(void);
154 static void popsig(void);
155 #if defined(MSDOS) && !defined(WIN16)
156 static int noecho_fgets(char *buf
, int size
, FILE *tty
);
159 static void read_till_nl();
160 static int read_pw();
161 static void recsig();
162 static void pushsig();
163 static void popsig();
164 #if defined(MSDOS) && !defined(WIN16)
165 static int noecho_fgets();
170 static void (*savsig
[NX509_SIG
])(int );
172 static void (*savsig
[NX509_SIG
])();
176 int des_read_password(key
, prompt
, verify
)
182 char buf
[BUFSIZ
],buff
[BUFSIZ
];
184 if ((ok
=read_pw(buf
,buff
,BUFSIZ
,prompt
,verify
)) == 0)
185 des_string_to_key(buf
,key
);
186 memset(buf
,0,BUFSIZ
);
187 memset(buff
,0,BUFSIZ
);
191 int des_read_2passwords(key1
, key2
, prompt
, verify
)
198 char buf
[BUFSIZ
],buff
[BUFSIZ
];
200 if ((ok
=read_pw(buf
,buff
,BUFSIZ
,prompt
,verify
)) == 0)
201 des_string_to_2keys(buf
,key1
,key2
);
202 memset(buf
,0,BUFSIZ
);
203 memset(buff
,0,BUFSIZ
);
207 int des_read_pw_string(buf
, length
, prompt
, verify
)
216 ret
=read_pw(buf
,buff
,(length
>BUFSIZ
)?BUFSIZ
:length
,prompt
,verify
);
217 memset(buff
,0,BUFSIZ
);
223 static void read_till_nl(in
)
231 } while (strchr(buf
,'\n') == NULL
);
235 /* return 0 if ok, 1 (or -1) otherwise */
236 static int read_pw(buf
, buff
, size
, prompt
, verify
)
245 $
DESCRIPTOR(terminal
,"TT");
246 long tty_orig
[3], tty_new
[3];
248 unsigned short channel
= 0;
251 TTY_STRUCT tty_orig
,tty_new
;
264 #elif !defined(MSDOS)
265 if ((tty
=fopen("/dev/tty","r")) == NULL
)
268 if ((tty
=fopen("con","r")) == NULL
)
272 #if defined(TTY_get) && !defined(VMS)
273 if (TTY_get(fileno(tty
),&tty_orig
) == -1)
282 memcpy(&(tty_new
),&(tty_orig
),sizeof(tty_orig
));
285 status
= SYS$
ASSIGN(&terminal
,&channel
,0,0);
286 if (status
!= SS$_NORMAL
)
288 status
=SYS$
QIOW(0,channel
,IO$_SENSEMODE
,&iosb
,0,0,tty_orig
,12,0,0,0,0);
289 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
))
302 tty_new
.TTY_FLAGS
&= ~ECHO
;
305 #if defined(TTY_set) && !defined(VMS)
306 if (is_a_tty
&& (TTY_set(fileno(tty
),&tty_new
) == -1))
310 tty_new
[0] = tty_orig
[0];
311 tty_new
[1] = tty_orig
[1] | TT$M_NOECHO
;
312 tty_new
[2] = tty_orig
[2];
313 status
= SYS$
QIOW(0,channel
,IO$_SETMODE
,&iosb
,0,0,tty_new
,12,0,0,0,0);
314 if ((status
!= SS$_NORMAL
) || (iosb
.iosb$w_value
!= SS$_NORMAL
))
319 while ((!ok
) && (number
--))
321 fputs(prompt
,stderr
);
326 if (feof(tty
)) goto error
;
327 if (ferror(tty
)) goto error
;
328 if ((p
=(char *)strchr(buf
,'\n')) != NULL
)
330 else read_till_nl(tty
);
333 fprintf(stderr
,"\nVerifying password - %s",prompt
);
336 fgets(buff
,size
,tty
);
337 if (feof(tty
)) goto error
;
338 if ((p
=(char *)strchr(buff
,'\n')) != NULL
)
340 else read_till_nl(tty
);
342 if (strcmp(buf
,buff
) != 0)
344 fprintf(stderr
,"\nVerify failure");
354 fprintf(stderr
,"\n");
356 perror("fgets(tty)");
358 /* What can we do if there is an error? */
359 #if defined(TTY_set) && !defined(VMS)
360 if (ps
>= 2) TTY_set(fileno(tty
),&tty_orig
);
364 status
= SYS$
QIOW(0,channel
,IO$_SETMODE
,&iosb
,0,0
365 ,tty_orig
,12,0,0,0,0);
368 if (ps
>= 1) popsig();
369 if (stdin
!= tty
) fclose(tty
);
371 status
= SYS$
DASSGN(channel
);
378 static int read_pw(buf
, buff
, size
, prompt
, verify
)
392 static void pushsig()
396 for (i
=1; i
<NX509_SIG
; i
++)
397 savsig
[i
]=signal(i
,recsig
);
400 signal(SIGWINCH
,SIG_DFL
);
408 for (i
=1; i
<NX509_SIG
; i
++)
412 static void recsig(i
)
421 #if defined(MSDOS) && !defined(WIN16)
422 static int noecho_fgets(buf
,size
,tty
)
444 if (i
== '\r') i
='\n';