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