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