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