2 * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
5 * For copying and distribution information, please see the file
8 * This routine prints the supplied string to standard
9 * output as a prompt, and reads a password string without
13 #include <afsconfig.h>
14 #include <afs/param.h>
19 #include <mit-cpyright.h>
25 #include <sys/ioctl.h>
32 #if defined(AFS_AIX_ENV)
35 #if defined(AFS_SGI_ENV)
40 #if defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
41 /* Just temp till we figure out the aix stuff */
51 static void sig_restore();
52 static push_signals(), pop_signals();
56 /*** Routines ****************************************************** */
58 des_read_password(k, prompt, verify)
64 char key_string[BUFSIZ];
73 ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
75 string_to_key(key_string, k);
78 memset(key_string, 0, sizeof(key_string));
82 /* good_gets is like gets except that it take a max string length and won't
83 write past the end of its input buffer. It returns a variety of negative
84 numbers in case of errors and zero if there was no characters read (a blank
85 line for instance). Otherwise it returns the length of the string read in.
93 int l; /* length of string read */
94 if (!fgets(s, max, stdin)) {
96 return EOF; /* EOF on input, nothing read */
98 return -2; /* I don't think this can happen */
101 if (l && (s[l - 1] == '\n'))
107 * This version just returns the string, doesn't map to key.
109 * Returns 0 on success, non-zero on failure.
112 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
117 read_pw_string(s, max, prompt, verify)
124 int len; /* password length */
128 struct sgttyb tty_state;
130 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
134 int (*sig) (), catch();
135 extern void setbuf();
136 extern int kill(), fclose();
139 char key_string[BUFSIZ];
145 memcpy(old_env, env, sizeof(env));
149 /* save terminal state */
150 if (ioctl(0, TIOCGETP, &tty_state) == -1)
155 tty_state.sg_flags &= ~ECHO;
156 if (ioctl(0, TIOCSETP, &tty_state) == -1) {
161 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
162 if ((fi = fopen("/dev/tty", "r+")) == NULL)
165 setbuf(fi, (char *)NULL);
166 sig = signal(SIGINT, catch);
168 (void)ioctl(fileno(fi), TCGETA, &ttyb);
171 flags = ttyb.c_lflag;
172 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
173 (void)ioctl(fileno(fi), TCSETAF, &ttyb);
181 h19line(s, sizeof(s), 0);
185 if (good_gets(s, max) <= 0) {
187 break; /* just give up */
189 continue; /* try again: blank line */
193 printf("\nVerifying, please re-enter %s", prompt);
196 h19line(key_string, sizeof(key_string), 0);
197 if (!strlen(key_string))
200 if (good_gets(key_string, sizeof(key_string)) <= 0)
203 if (strcmp(s, key_string)) {
204 printf("\n\07\07Mismatch - try again\n");
216 /* turn echo back on */
217 tty_state.sg_flags |= ECHO;
218 if (ioctl(0, TIOCSETP, &tty_state))
221 memcpy(env, old_env, sizeof(env));
223 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
224 ttyb.c_lflag = flags;
226 (void)ioctl(fileno(fi), TCSETAW, &ttyb);
227 (void)signal(SIGINT, sig);
231 (void)kill(getpid(), SIGINT);
235 memset(key_string, 0, sizeof(key_string));
236 s[max - 1] = 0; /* force termination */
237 return !ok; /* return nonzero if not okay */
242 * this can be static since we should never have more than
245 static int (*old_sigfunc[NSIG]) ();
251 for (i = 0; i < NSIG; i++)
252 old_sigfunc[i] = signal(i, sig_restore);
259 for (i = 0; i < NSIG; i++)
260 signal(i, old_sigfunc[i]);
264 sig_restore(sig, code, scp)
266 struct sigcontext *scp;
272 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)