File: passphrase.c

package info (click to toggle)
donkey 0.5-18
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 252 kB
  • ctags: 365
  • sloc: ansic: 1,963; makefile: 54; sh: 29
file content (182 lines) | stat: -rw-r--r-- 3,698 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
/*
 * passphrase.c
 *		 Copyright (C) 1995 Kazuhiko Yamamoto
 *	      Kazuhiko Yamamoto <kazu@is.aist-nara.ac.jp>
 */


#include "config.h"
#include "donkey.h"

private void turnecho PROTO((FILE *fp, int on));
private RETSIGTYPE  get_sigint(); /* signal */

FILE *fp, *outfp;

private void turnecho(fp, on)
FILE *fp;
int on;
{
#ifdef HAVE_TERMIOS_H
  static struct termios orig, noecho, *tp;
#else
#  ifdef HAVE_TERMIO_H
     static struct termio orig, noecho, *tp;
#  else
     static struct sgttyb orig, noecho, *tp;
#  endif
#endif
	static int state = 0;
	int fd = fileno(fp);

	if (!isatty(fd))
		return; /* return void */

	if (state == 0) {
#ifdef HAVE_TERMIOS_H
		if (tcgetattr(fd, &orig) < 0)
			perror("tcgetattr");
		noecho = orig;
		noecho.c_lflag &= ~ECHO;
#else
#  ifdef HAVE_TERMIO_H
		if (ioctl(fd, TCGETA, &orig) < 0)
			perror("ioctl");
		noecho = orig;
		noecho.c_lflag &= ~ECHO;
#  else
		if (ioctl(fd, TIOCGETP, &orig) < 0)
			perror("ioctl");
		noecho = orig;
		noecho.sg_flags &= ~ECHO;
#  endif
#endif
		state = 1;
	}
	tp = NULL;
	if (on && state == 2) {
		/* Turn echo back on. */
		tp = &orig;
		state = 1;
	} else if (!on && state == 1) {
		/* Turn echo off. */
		tp = &noecho;
		state = 2;
	}
	if (tp != NULL) {
#ifdef HAVE_TERMIOS_H
		if (tcsetattr(fd, TCSANOW, tp) < 0)
			perror("tcsetattr");
#else
#  ifdef HAVE_TERMIO_H
		if (ioctl(fd, TCSETA, tp) < 0)
			perror("ioctl");
#  else
		if (ioctl(fd, TIOCSETP, tp) < 0)
			perror("ioctl");
#  endif
#endif	/* !HAVE_TERMIOS_H */
	}
}

private RETSIGTYPE get_sigint()
{
	(void) rewind(outfp);

	turnecho(fp, 1);
	
	(void) fputc('\n', outfp);

	if (fp != stdin)
		(void)fclose(fp);
	exit(0);
}

/*
 * read size-free passphrase without echo
 */

public int passphrase(prompt, phrase, size, reject_short)
char *prompt, *phrase;
int size, reject_short;
{
	sigtype oldintr;
#ifdef SIGTSTP
	sigtype oldtstp;
#endif

	/*
	 * read and write to /dev/tty if possible; else read from
	 * stdin and write to stderr.
	 */
  	if ((outfp = fp = fopen("/dev/tty", "r+")) == NULL) {
  		outfp = stderr;
  		fp = stdin;
  	}

	/* 
	 * We don't want to return with echoing off.
	 */
	oldintr = signal(SIGINT, get_sigint);

	/*
	 * Ignore suspend.
	 */
#ifdef SIGTSTP
	oldtstp = signal(SIGTSTP, SIG_IGN);
#endif

	turnecho(fp, 0);
	(void) fputs(prompt, outfp);
	(void) fflush(outfp);
	(void) rewind(outfp);
	phrase[0] = '\0';
	if (fgets(phrase, size, fp) != NULL)
		phrase[strlen(phrase) - 1] = '\0';
	(void) fflush(fp);

	if (reject_short)
		while (strlen(phrase) < PASS_PHRASE_MIN_LEN) {
			(void) rewind(outfp);
			
			(void) fputs("\nYour passphrase is too short. Enter longer passphrase.\n", outfp);
			(void) fputs(prompt, outfp);
			(void) fflush(outfp);
			(void) rewind(outfp);
			
			phrase[0] = '\0';		
			if (fgets(phrase, size, fp) != NULL)
				phrase[strlen(phrase) - 1] = '\0';
			(void) fflush(fp);
		}
	
	(void) rewind(outfp);

	turnecho(fp, 1);
	
	(void) fputc('\n', outfp);

	(void) signal(SIGINT, oldintr);
#ifdef SIGTSTP
	(void) signal(SIGTSTP, oldtstp);
#endif

	if (fp != stdin)
		(void)fclose(fp);
	return(RET_SUCCESS);
}

/*
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 * Copyright (c) 1995 Mike Gleason, NCEMRSoft.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms are permitted provided
 * that: (1) source distributions retain this entire copyright notice and
 * comment, and (2) distributions may not be sold for profit on physical
 * media such as disks, tapes, and CD-ROMS, without expressed written
 * permission.
 */