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>
17 #include <mit-cpyright.h>
23 #include <sys/ioctl.h>
30 #if defined(AFS_AIX_ENV)
33 #if defined(AFS_SGI_ENV)
38 #if defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
39 /* Just temp till we figure out the aix stuff */
49 static void sig_restore();
50 static push_signals(), pop_signals();
54 /*** Routines ****************************************************** */
56 des_read_password(k, prompt, verify)
62 char key_string[BUFSIZ];
71 ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
73 string_to_key(key_string, k);
76 memset(key_string, 0, sizeof(key_string));
80 /* good_gets is like gets except that it take a max string length and won't
81 write past the end of its input buffer. It returns a variety of negative
82 numbers in case of errors and zero if there was no characters read (a blank
83 line for instance). Otherwise it returns the length of the string read in.
91 int l; /* length of string read */
92 if (!fgets(s, max, stdin)) {
94 return EOF; /* EOF on input, nothing read */
96 return -2; /* I don't think this can happen */
99 if (l && (s[l - 1] == '\n'))
105 * This version just returns the string, doesn't map to key.
107 * Returns 0 on success, non-zero on failure.
110 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
115 read_pw_string(s, max, prompt, verify)
122 int len; /* password length */
126 struct sgttyb tty_state;
128 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
132 int (*sig) (), catch();
133 extern void setbuf();
134 extern int kill(), fclose();
137 char key_string[BUFSIZ];
143 memcpy(old_env, env, sizeof(env));
147 /* save terminal state */
148 if (ioctl(0, TIOCGETP, &tty_state) == -1)
153 tty_state.sg_flags &= ~ECHO;
154 if (ioctl(0, TIOCSETP, &tty_state) == -1) {
159 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
160 if ((fi = fopen("/dev/tty", "r+")) == NULL)
163 setbuf(fi, (char *)NULL);
164 sig = signal(SIGINT, catch);
166 (void)ioctl(fileno(fi), TCGETA, &ttyb);
169 flags = ttyb.c_lflag;
170 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
171 (void)ioctl(fileno(fi), TCSETAF, &ttyb);
179 h19line(s, sizeof(s), 0);
183 if (good_gets(s, max) <= 0) {
185 break; /* just give up */
187 continue; /* try again: blank line */
191 printf("\nVerifying, please re-enter %s", prompt);
194 h19line(key_string, sizeof(key_string), 0);
195 if (!strlen(key_string))
198 if (good_gets(key_string, sizeof(key_string)) <= 0)
201 if (strcmp(s, key_string)) {
202 printf("\n\07\07Mismatch - try again\n");
214 /* turn echo back on */
215 tty_state.sg_flags |= ECHO;
216 if (ioctl(0, TIOCSETP, &tty_state))
219 memcpy(env, old_env, sizeof(env));
221 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
222 ttyb.c_lflag = flags;
224 (void)ioctl(fileno(fi), TCSETAW, &ttyb);
225 (void)signal(SIGINT, sig);
229 (void)kill(getpid(), SIGINT);
233 memset(key_string, 0, sizeof(key_string));
234 s[max - 1] = 0; /* force termination */
235 return !ok; /* return nonzero if not okay */
240 * this can be static since we should never have more than
243 static int (*old_sigfunc[NSIG]) ();
249 for (i = 0; i < NSIG; i++)
250 old_sigfunc[i] = signal(i, sig_restore);
257 for (i = 0; i < NSIG; i++)
258 signal(i, old_sigfunc[i]);
262 sig_restore(sig, code, scp)
264 struct sigcontext *scp;
270 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)