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