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