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