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