Remove NOPROTO definitions and error code comments.
[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 static void read_till_nl(FILE *);
173 static void recsig(int);
174 static void pushsig(void);
175 static void popsig(void);
176 #if defined(MSDOS) && !defined(WIN16)
177 static int noecho_fgets(char *buf, int size, FILE *tty);
178 #endif
179 #ifdef SIGACTION
180  static struct sigaction savsig[NX509_SIG];
181 #else
182 # ifndef NOPROTO
183   static void (*savsig[NX509_SIG])(int );
184 # else
185   static void (*savsig[NX509_SIG])();
186 # endif
187 #endif
188 static jmp_buf save;
189
190 int des_read_pw_string(char *buf, int length, const char *prompt,
191              int verify)
192         {
193         char buff[BUFSIZ];
194         int ret;
195
196         ret=des_read_pw(buf,buff,(length>BUFSIZ)?BUFSIZ:length,prompt,verify);
197         memset(buff,0,BUFSIZ);
198         return(ret);
199         }
200
201 #ifndef WIN16
202
203 static void read_till_nl(FILE *in)
204         {
205 #define SIZE 4
206         char buf[SIZE+1];
207
208         do      {
209                 fgets(buf,SIZE,in);
210                 } while (strchr(buf,'\n') == NULL);
211         }
212
213
214 /* return 0 if ok, 1 (or -1) otherwise */
215 int des_read_pw(char *buf, char *buff, int size, const char *prompt,
216              int verify)
217         {
218 #ifdef VMS
219         struct IOSB iosb;
220         $DESCRIPTOR(terminal,"TT");
221         long tty_orig[3], tty_new[3];
222         long status;
223         unsigned short channel = 0;
224 #else
225 #ifndef MSDOS
226         TTY_STRUCT tty_orig,tty_new;
227 #endif
228 #endif
229         int number;
230         int ok;
231         /* statics are simply to avoid warnings about longjmp clobbering
232            things */
233         static int ps;
234         int is_a_tty;
235         static FILE *tty;
236         char *p;
237
238         if (setjmp(save))
239                 {
240                 ok=0;
241                 goto error;
242                 }
243
244         number=5;
245         ok=0;
246         ps=0;
247         is_a_tty=1;
248         tty=NULL;
249
250 #ifndef MSDOS
251         if ((tty=fopen("/dev/tty","r")) == NULL)
252                 tty=stdin;
253 #else /* MSDOS */
254         if ((tty=fopen("con","r")) == NULL)
255                 tty=stdin;
256 #endif /* MSDOS */
257
258 #if defined(TTY_get) && !defined(VMS)
259         if (TTY_get(fileno(tty),&tty_orig) == -1)
260                 {
261 #ifdef ENOTTY
262                 if (errno == ENOTTY)
263                         is_a_tty=0;
264                 else
265 #endif
266 #ifdef EINVAL
267                 /* Ariel Glenn ariel@columbia.edu reports that solaris
268                  * can return EINVAL instead.  This should be ok */
269                 if (errno == EINVAL)
270                         is_a_tty=0;
271                 else
272 #endif
273                         return(-1);
274                 }
275         memcpy(&(tty_new),&(tty_orig),sizeof(tty_orig));
276 #endif
277 #ifdef VMS
278         status = SYS$ASSIGN(&terminal,&channel,0,0);
279         if (status != SS$_NORMAL)
280                 return(-1);
281         status=SYS$QIOW(0,channel,IO$_SENSEMODE,&iosb,0,0,tty_orig,12,0,0,0,0);
282         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
283                 return(-1);
284 #endif
285
286         pushsig();
287         ps=1;
288
289 #ifdef TTY_FLAGS
290         tty_new.TTY_FLAGS &= ~ECHO;
291 #endif
292
293 #if defined(TTY_set) && !defined(VMS)
294         if (is_a_tty && (TTY_set(fileno(tty),&tty_new) == -1))
295                 return(-1);
296 #endif
297 #ifdef VMS
298         tty_new[0] = tty_orig[0];
299         tty_new[1] = tty_orig[1] | TT$M_NOECHO;
300         tty_new[2] = tty_orig[2];
301         status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0,tty_new,12,0,0,0,0);
302         if ((status != SS$_NORMAL) || (iosb.iosb$w_value != SS$_NORMAL))
303                 return(-1);
304 #endif
305         ps=2;
306
307         while ((!ok) && (number--))
308                 {
309                 fputs(prompt,stderr);
310                 fflush(stderr);
311
312                 buf[0]='\0';
313                 fgets(buf,size,tty);
314                 if (feof(tty)) goto error;
315                 if (ferror(tty)) goto error;
316                 if ((p=(char *)strchr(buf,'\n')) != NULL)
317                         *p='\0';
318                 else    read_till_nl(tty);
319                 if (verify)
320                         {
321                         fprintf(stderr,"\nVerifying password - %s",prompt);
322                         fflush(stderr);
323                         buff[0]='\0';
324                         fgets(buff,size,tty);
325                         if (feof(tty)) goto error;
326                         if ((p=(char *)strchr(buff,'\n')) != NULL)
327                                 *p='\0';
328                         else    read_till_nl(tty);
329                                 
330                         if (strcmp(buf,buff) != 0)
331                                 {
332                                 fprintf(stderr,"\nVerify failure");
333                                 fflush(stderr);
334                                 break;
335                                 /* continue; */
336                                 }
337                         }
338                 ok=1;
339                 }
340
341 error:
342         fprintf(stderr,"\n");
343 #ifdef DEBUG
344         perror("fgets(tty)");
345 #endif
346         /* What can we do if there is an error? */
347 #if defined(TTY_set) && !defined(VMS) 
348         if (ps >= 2) TTY_set(fileno(tty),&tty_orig);
349 #endif
350 #ifdef VMS
351         if (ps >= 2)
352                 status = SYS$QIOW(0,channel,IO$_SETMODE,&iosb,0,0
353                         ,tty_orig,12,0,0,0,0);
354 #endif
355         
356         if (ps >= 1) popsig();
357         if (stdin != tty) fclose(tty);
358 #ifdef VMS
359         status = SYS$DASSGN(channel);
360 #endif
361         return(!ok);
362         }
363
364 #else /* WIN16 */
365
366 int des_read_pw(char *buf, char *buff, int size, char *prompt, int verify)
367         { 
368         memset(buf,0,size);
369         memset(buff,0,size);
370         return(0);
371         }
372
373 #endif
374
375 static void pushsig(void)
376         {
377         int i;
378 #ifdef SIGACTION
379         struct sigaction sa;
380
381         memset(&sa,0,sizeof sa);
382         sa.sa_handler=recsig;
383 #endif
384
385         for (i=1; i<NX509_SIG; i++)
386                 {
387 #ifdef SIGUSR1
388                 if (i == SIGUSR1)
389                         continue;
390 #endif
391 #ifdef SIGUSR2
392                 if (i == SIGUSR2)
393                         continue;
394 #endif
395 #ifdef SIGACTION
396                 sigaction(i,&sa,&savsig[i]);
397 #else
398                 savsig[i]=signal(i,recsig);
399 #endif
400                 }
401
402 #ifdef SIGWINCH
403         signal(SIGWINCH,SIG_DFL);
404 #endif
405         }
406
407 static void popsig(void)
408         {
409         int i;
410
411         for (i=1; i<NX509_SIG; i++)
412                 {
413 #ifdef SIGUSR1
414                 if (i == SIGUSR1)
415                         continue;
416 #endif
417 #ifdef SIGUSR2
418                 if (i == SIGUSR2)
419                         continue;
420 #endif
421 #ifdef SIGACTION
422                 sigaction(i,&savsig[i],NULL);
423 #else
424                 signal(i,savsig[i]);
425 #endif
426                 }
427         }
428
429 static void recsig(int i)
430         {
431         longjmp(save,1);
432 #ifdef LINT
433         i=i;
434 #endif
435         }
436
437 #if defined(MSDOS) && !defined(WIN16)
438 static int noecho_fgets(char *buf, int size, 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 #ifdef WIN_CONSOLE_BUG
466 /* Win95 has several evil console bugs: one of these is that the
467  * last character read using getch() is passed to the next read: this is
468  * usually a CR so this can be trouble. No STDIO fix seems to work but
469  * flushing the console appears to do the trick.
470  */
471                 {
472                         HANDLE inh;
473                         inh = GetStdHandle(STD_INPUT_HANDLE);
474                         FlushConsoleInputBuffer(inh);
475                 }
476 #endif
477         return(strlen(buf));
478         }
479 #endif