]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/des/read_pwd.c
WinCE patches
[thirdparty/openssl.git] / crypto / des / read_pwd.c
CommitLineData
d02b48c6 1/* crypto/des/read_pwd.c */
58964a49 2/* Copyright (C) 1995-1998 Eric Young (eay@cryptsoft.com)
d02b48c6
RE
3 * All rights reserved.
4 *
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.
8 *
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).
15 *
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.
22 *
23 * Redistribution and use in source and binary forms, with or without
24 * modification, are permitted provided that the following conditions
25 * are met:
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)"
40 *
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
51 * SUCH DAMAGE.
52 *
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.]
57 */
58
a5bc1e85 59#include <openssl/e_os2.h>
7ab1a391 60#if !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_WIN32)
404fb714
AP
61#ifdef OPENSSL_UNISTD
62# include OPENSSL_UNISTD
63#else
64# include <unistd.h>
65#endif
fa3da3cf
BM
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)
69
70# define SIGACTION
71# if !defined(TERMIOS) && !defined(TERMIO) && !defined(SGTTY)
72# define TERMIOS
73# endif
74
75#endif
76#endif
77
58964a49 78/* #define SIGACTION */ /* Define this if you have sigaction() */
fa3da3cf 79
d02b48c6 80#ifdef WIN16TTY
bc36ee62 81#undef OPENSSL_SYS_WIN16
d02b48c6
RE
82#undef _WINDOWS
83#include <graph.h>
84#endif
85
86/* 06-Apr-92 Luke Brennan Support for VMS */
87#include "des_locl.h"
6e064240 88#include "cryptlib.h"
d02b48c6 89#include <signal.h>
410aa522 90#include <stdio.h>
d02b48c6
RE
91#include <string.h>
92#include <setjmp.h>
93#include <errno.h>
94
bc36ee62 95#ifdef OPENSSL_SYS_VMS /* prototypes for sys$whatever */
7d7d2cbc
UM
96#include <starlet.h>
97#ifdef __DECC
98#pragma message disable DOLLARID
99#endif
100#endif
101
d00b7aad
DSH
102#ifdef WIN_CONSOLE_BUG
103#include <windows.h>
0bf23d9b 104#ifndef OPENSSL_SYS_WINCE
d00b7aad
DSH
105#include <wincon.h>
106#endif
0bf23d9b 107#endif
d00b7aad
DSH
108
109
d02b48c6
RE
110/* There are 5 types of terminal interface supported,
111 * TERMIO, TERMIOS, VMS, MSDOS and SGTTY
112 */
113
114#if defined(__sgi) && !defined(TERMIOS)
115#define TERMIOS
116#undef TERMIO
117#undef SGTTY
118#endif
119
120#if defined(linux) && !defined(TERMIO)
121#undef TERMIOS
122#define TERMIO
123#undef SGTTY
124#endif
125
126#ifdef _LIBC
127#undef TERMIOS
128#define TERMIO
129#undef SGTTY
130#endif
131
bc36ee62 132#if !defined(TERMIO) && !defined(TERMIOS) && !defined(OPENSSL_SYS_VMS) && !defined(OPENSSL_SYS_MSDOS) && !defined(MAC_OS_pre_X) && !defined(MAC_OS_GUSI_SOURCE)
d02b48c6
RE
133#undef TERMIOS
134#undef TERMIO
135#define SGTTY
136#endif
137
6a89a25c 138#if defined(OPENSSL_SYS_VXWORKS)
3e83e686
RL
139#undef TERMIOS
140#undef TERMIO
141#undef SGTTY
142#endif
143
d02b48c6
RE
144#ifdef TERMIOS
145#include <termios.h>
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)
150#endif
151
152#ifdef TERMIO
153#include <termio.h>
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)
158#endif
159
160#ifdef SGTTY
161#include <sgtty.h>
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)
166#endif
167
bc36ee62 168#if !defined(_LIBC) && !defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_VMS) && !defined(MAC_OS_pre_X)
d02b48c6
RE
169#include <sys/ioctl.h>
170#endif
171
0bf23d9b 172#if defined(OPENSSL_SYS_MSDOS) && !defined(__CYGWIN32__) && !defined(OPENSSL_SYS_WINCE)
d02b48c6
RE
173#include <conio.h>
174#define fgets(a,b,c) noecho_fgets(a,b,c)
175#endif
176
bc36ee62 177#ifdef OPENSSL_SYS_VMS
d02b48c6
RE
178#include <ssdef.h>
179#include <iodef.h>
180#include <ttdef.h>
181#include <descrip.h>
182struct IOSB {
183 short iosb$w_value;
184 short iosb$w_count;
185 long iosb$l_info;
186 };
187#endif
188
76997b7d 189#if defined(MAC_OS_pre_X) || defined(MAC_OS_GUSI_SOURCE)
17f389bb
AP
190/*
191 * This one needs work. As a matter of fact the code is unoperational
192 * and this is only a trick to get it compiled.
193 * <appro@fy.chalmers.se>
194 */
195#define TTY_STRUCT int
196#endif
197
d02b48c6
RE
198#ifndef NX509_SIG
199#define NX509_SIG 32
200#endif
201
d02b48c6
RE
202static void read_till_nl(FILE *);
203static void recsig(int);
204static void pushsig(void);
205static void popsig(void);
bc36ee62 206#if defined(OPENSSL_SYS_MSDOS) && !defined(OPENSSL_SYS_WIN16)
d02b48c6
RE
207static int noecho_fgets(char *buf, int size, FILE *tty);
208#endif
58964a49
RE
209#ifdef SIGACTION
210 static struct sigaction savsig[NX509_SIG];
d02b48c6 211#else
58964a49 212 static void (*savsig[NX509_SIG])(int );
d02b48c6
RE
213#endif
214static jmp_buf save;
215
6b691a5c
UM
216int des_read_pw_string(char *buf, int length, const char *prompt,
217 int verify)
d02b48c6
RE
218 {
219 char buff[BUFSIZ];
220 int ret;
221
222 ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
223 memset(buff,0,BUFSIZ);
224 return(ret);
225 }
226
0bf23d9b
RL
227#ifdef OPENSSL_SYS_WINCE
228
229int des_read_pw(char *buf, char *buff, int size, const char *prompt, int verify)
230 {
231 memset(buf,0,size);
232 memset(buff,0,size);
233 return(0);
234 }
235
236#elif defined(OPENSSL_SYS_WIN16)
237
238int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
239 {
240 memset(buf,0,size);
241 memset(buff,0,size);
242 return(0);
243 }
244
245#else /* !OPENSSL_SYS_WINCE && !OPENSSL_SYS_WIN16 */
d02b48c6 246
6b691a5c 247static void read_till_nl(FILE *in)
d02b48c6
RE
248 {
249#define SIZE 4
250 char buf[SIZE+1];
251
252 do {
253 fgets(buf,SIZE,in);
254 } while (strchr(buf,'\n') == NULL);
255 }
256
257
258/* return 0 if ok, 1 (or -1) otherwise */
6b691a5c
UM
259int des_read_pw(char *buf, char *buff, int size, const char *prompt,
260 int verify)
d02b48c6 261 {
bc36ee62 262#ifdef OPENSSL_SYS_VMS
d02b48c6
RE
263 struct IOSB iosb;
264 $DESCRIPTOR(terminal,"TT");
265 long tty_orig[3], tty_new[3];
266 long status;
267 unsigned short channel = 0;
268#else
451dc18f 269#if !defined(OPENSSL_SYS_MSDOS) || defined(__DJGPP__)
d02b48c6
RE
270 TTY_STRUCT tty_orig,tty_new;
271#endif
272#endif
15799403
BL
273 int number;
274 int ok;
275 /* statics are simply to avoid warnings about longjmp clobbering
276 things */
277 static int ps;
278 int is_a_tty;
279 static FILE *tty;
d02b48c6
RE
280 char *p;
281
15799403
BL
282 if (setjmp(save))
283 {
284 ok=0;
285 goto error;
286 }
287
288 number=5;
289 ok=0;
290 ps=0;
291 is_a_tty=1;
292 tty=NULL;
293
bc36ee62 294#ifdef OPENSSL_SYS_MSDOS
d02b48c6
RE
295 if ((tty=fopen("con","r")) == NULL)
296 tty=stdin;
6a89a25c 297#elif defined(MAC_OS_pre_X) || defined(OPENSSL_SYS_VXWORKS)
991f0706
UM
298 tty=stdin;
299#else
cf1b7d96 300#ifndef OPENSSL_SYS_MPE
991f0706 301 if ((tty=fopen("/dev/tty","r")) == NULL)
c5f8bbbc 302#endif
991f0706
UM
303 tty=stdin;
304#endif
d02b48c6 305
bc36ee62 306#if defined(TTY_get) && !defined(OPENSSL_SYS_VMS)
d02b48c6
RE
307 if (TTY_get(fileno(tty),&tty_orig) == -1)
308 {
309#ifdef ENOTTY
310 if (errno == ENOTTY)
311 is_a_tty=0;
312 else
58964a49
RE
313#endif
314#ifdef EINVAL
315 /* Ariel Glenn ariel@columbia.edu reports that solaris
316 * can return EINVAL instead. This should be ok */
317 if (errno == EINVAL)
318 is_a_tty=0;
319 else
d02b48c6
RE
320#endif
321 return(-1);
322 }
323 memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
324#endif
bc36ee62 325#ifdef OPENSSL_SYS_VMS
7d7d2cbc 326 status = sys$assign(&terminal,&channel,0,0);
d02b48c6
RE
327 if (status != SS$_NORMAL)
328 return(-1);
7d7d2cbc 329 status=sys$qiow(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
d02b48c6
RE
330 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
331 return(-1);
332#endif
333
d02b48c6
RE
334 pushsig();
335 ps=1;
336
337#ifdef TTY_FLAGS
338 tty_new.TTY_FLAGS &= ~ECHO;
339#endif
340
bc36ee62 341#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
d02b48c6 342 if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
cf1b7d96 343#ifdef OPENSSL_SYS_MPE
c5f8bbbc
RL
344 ; /* MPE lies -- echo really has been disabled */
345#else
d02b48c6
RE
346 return(-1);
347#endif
c5f8bbbc 348#endif
bc36ee62 349#ifdef OPENSSL_SYS_VMS
d02b48c6
RE
350 tty_new[0] = tty_orig[0];
351 tty_new[1] = tty_orig[1] | TT$M_NOECHO;
352 tty_new[2] = tty_orig[2];
7d7d2cbc 353 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
d02b48c6
RE
354 if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
355 return(-1);
356#endif
357 ps=2;
358
359 while ((!ok) && (number--))
360 {
361 fputs(prompt,stderr);
362 fflush(stderr);
363
364 buf[0]='\0';
365 fgets(buf,size,tty);
366 if (feof(tty)) goto error;
367 if (ferror(tty)) goto error;
368 if ((p=(char *)strchr(buf,'\n')) != NULL)
369 *p='\0';
370 else read_till_nl(tty);
371 if (verify)
372 {
373 fprintf(stderr,"\nVerifying password - %s",prompt);
374 fflush(stderr);
375 buff[0]='\0';
376 fgets(buff,size,tty);
377 if (feof(tty)) goto error;
378 if ((p=(char *)strchr(buff,'\n')) != NULL)
379 *p='\0';
380 else read_till_nl(tty);
381
382 if (strcmp(buf,buff) != 0)
383 {
384 fprintf(stderr,"\nVerify failure");
385 fflush(stderr);
386 break;
387 /* continue; */
388 }
389 }
390 ok=1;
391 }
392
393error:
394 fprintf(stderr,"\n");
59dbdb51 395#if 0
d02b48c6
RE
396 perror("fgets(tty)");
397#endif
398 /* What can we do if there is an error? */
bc36ee62 399#if defined(TTY_set) && !defined(OPENSSL_SYS_VMS)
d02b48c6
RE
400 if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
401#endif
bc36ee62 402#ifdef OPENSSL_SYS_VMS
d02b48c6 403 if (ps >= 2)
7d7d2cbc 404 status = sys$qiow(0,channel,IO$_SETMODE,&iosb,0,0
d02b48c6
RE
405 ,tty_orig,12,0,0,0,0);
406#endif
407
408 if (ps >= 1) popsig();
409 if (stdin != tty) fclose(tty);
bc36ee62 410#ifdef OPENSSL_SYS_VMS
7d7d2cbc 411 status = sys$dassgn(channel);
d02b48c6
RE
412#endif
413 return(!ok);
414 }
415
6b691a5c 416static void pushsig(void)
d02b48c6
RE
417 {
418 int i;
215c24fc
BL
419#ifdef SIGACTION
420 struct sigaction sa;
421
422 memset(&sa,0,sizeof sa);
423 sa.sa_handler=recsig;
424#endif
d02b48c6
RE
425
426 for (i=1; i<NX509_SIG; i++)
58964a49
RE
427 {
428#ifdef SIGUSR1
429 if (i == SIGUSR1)
430 continue;
431#endif
432#ifdef SIGUSR2
433 if (i == SIGUSR2)
434 continue;
435#endif
436#ifdef SIGACTION
215c24fc 437 sigaction(i,&sa,&savsig[i]);
58964a49 438#else
d02b48c6 439 savsig[i]=signal(i,recsig);
58964a49
RE
440#endif
441 }
d02b48c6
RE
442
443#ifdef SIGWINCH
444 signal(SIGWINCH,SIG_DFL);
445#endif
446 }
447
6b691a5c 448static void popsig(void)
d02b48c6
RE
449 {
450 int i;
451
452 for (i=1; i<NX509_SIG; i++)
58964a49
RE
453 {
454#ifdef SIGUSR1
455 if (i == SIGUSR1)
456 continue;
457#endif
458#ifdef SIGUSR2
459 if (i == SIGUSR2)
460 continue;
461#endif
462#ifdef SIGACTION
463 sigaction(i,&savsig[i],NULL);
464#else
d02b48c6 465 signal(i,savsig[i]);
58964a49
RE
466#endif
467 }
d02b48c6
RE
468 }
469
6b691a5c 470static void recsig(int i)
d02b48c6
RE
471 {
472 longjmp(save,1);
473#ifdef LINT
474 i=i;
475#endif
476 }
477
0bf23d9b 478#ifdef OPENSSL_SYS_MSDOS
6b691a5c 479static int noecho_fgets(char *buf, int size, FILE *tty)
d02b48c6
RE
480 {
481 int i;
482 char *p;
483
484 p=buf;
485 for (;;)
486 {
487 if (size == 0)
488 {
489 *p='\0';
490 break;
491 }
492 size--;
493#ifdef WIN16TTY
494 i=_inchar();
495#else
496 i=getch();
497#endif
498 if (i == '\r') i='\n';
499 *(p++)=i;
500 if (i == '\n')
501 {
502 *p='\0';
503 break;
504 }
505 }
d00b7aad
DSH
506#ifdef WIN_CONSOLE_BUG
507/* Win95 has several evil console bugs: one of these is that the
508 * last character read using getch() is passed to the next read: this is
509 * usually a CR so this can be trouble. No STDIO fix seems to work but
510 * flushing the console appears to do the trick.
511 */
b1c4fe36
BM
512 {
513 HANDLE inh;
514 inh = GetStdHandle(STD_INPUT_HANDLE);
515 FlushConsoleInputBuffer(inh);
516 }
d00b7aad 517#endif
d02b48c6
RE
518 return(strlen(buf));
519 }
520#endif
0bf23d9b 521#endif /* !OPENSSL_SYS_WINCE && !WIN16 */