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)
47 #if defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
48 /* Just temp till we figure out the aix stuff */
58 static void sig_restore();
59 static push_signals(), pop_signals();
63 /*** Routines ****************************************************** */
65 des_read_password(k, prompt, verify)
71 char key_string[BUFSIZ];
80 ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
82 string_to_key(key_string, k);
85 memset(key_string, 0, sizeof(key_string));
89 /* good_gets is like gets except that it take a max string length and won't
90 write past the end of its input buffer. It returns a variety of negative
91 numbers in case of errors and zero if there was no characters read (a blank
92 line for instance). Otherwise it returns the length of the string read in.
100 int l; /* length of string read */
101 if (!fgets(s, max, stdin)) {
103 return EOF; /* EOF on input, nothing read */
105 return -2; /* I don't think this can happen */
108 if (l && (s[l - 1] == '\n'))
114 * This version just returns the string, doesn't map to key.
116 * Returns 0 on success, non-zero on failure.
119 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
124 read_pw_string(s, max, prompt, verify)
131 int len; /* password length */
135 struct sgttyb tty_state;
137 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
141 int (*sig) (), catch();
142 extern void setbuf();
143 extern int kill(), fclose();
146 char key_string[BUFSIZ];
152 memcpy(old_env, env, sizeof(env));
156 /* save terminal state */
157 if (ioctl(0, TIOCGETP, &tty_state) == -1)
162 tty_state.sg_flags &= ~ECHO;
163 if (ioctl(0, TIOCSETP, &tty_state) == -1) {
168 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
169 if ((fi = fopen("/dev/tty", "r+")) == NULL)
172 setbuf(fi, (char *)NULL);
173 sig = signal(SIGINT, catch);
175 (void)ioctl(fileno(fi), TCGETA, &ttyb);
178 flags = ttyb.c_lflag;
179 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
180 (void)ioctl(fileno(fi), TCSETAF, &ttyb);
188 h19line(s, sizeof(s), 0);
192 if (good_gets(s, max) <= 0) {
194 break; /* just give up */
196 continue; /* try again: blank line */
200 printf("\nVerifying, please re-enter %s", prompt);
203 h19line(key_string, sizeof(key_string), 0);
204 if (!strlen(key_string))
207 if (good_gets(key_string, sizeof(key_string)) <= 0)
210 if (strcmp(s, key_string)) {
211 printf("\n\07\07Mismatch - try again\n");
223 /* turn echo back on */
224 tty_state.sg_flags |= ECHO;
225 if (ioctl(0, TIOCSETP, &tty_state))
228 memcpy(env, old_env, sizeof(env));
230 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
231 ttyb.c_lflag = flags;
233 (void)ioctl(fileno(fi), TCSETAW, &ttyb);
234 (void)signal(SIGINT, sig);
238 (void)kill(getpid(), SIGINT);
242 memset(key_string, 0, sizeof(key_string));
243 s[max - 1] = 0; /* force termination */
244 return !ok; /* return nonzero if not okay */
249 * this can be static since we should never have more than
252 static int (*old_sigfunc[NSIG]) ();
258 for (i = 0; i < NSIG; i++)
259 old_sigfunc[i] = signal(i, sig_restore);
266 for (i = 0; i < NSIG; i++)
267 signal(i, old_sigfunc[i]);
271 sig_restore(sig, code, scp)
273 struct sigcontext *scp;
279 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)