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