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