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>
16 #include <mit-cpyright.h>
20 #include <des_prototypes.h>
23 #include <sys/ioctl.h>
29 #if defined(AFS_AIX_ENV)
32 #if defined(AFS_SGI_ENV)
37 #if defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
38 /* Just temp till we figure out the aix stuff */
48 static void sig_restore();
49 static push_signals(), pop_signals();
53 /*** Routines ****************************************************** */
55 des_read_password(C_Block *k, char *prompt, int verify)
58 char key_string[BUFSIZ];
67 ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
69 string_to_key(key_string, k);
73 memset(key_string, 0, sizeof(key_string));
77 /* good_gets is like gets except that it take a max string length and won't
78 write past the end of its input buffer. It returns a variety of negative
79 numbers in case of errors and zero if there was no characters read (a blank
80 line for instance). Otherwise it returns the length of the string read in.
84 good_gets(char *s, int max)
86 int l; /* length of string read */
87 if (!fgets(s, max, stdin)) {
89 return EOF; /* EOF on input, nothing read */
91 return -2; /* I don't think this can happen */
94 if (l && (s[l - 1] == '\n'))
100 * This version just returns the string, doesn't map to key.
102 * Returns 0 on success, non-zero on failure.
105 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
110 read_pw_string(char *s, int max, char *prompt, int verify)
116 struct sgttyb tty_state;
118 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
122 int (*sig) (), catch();
123 extern void setbuf();
124 extern int kill(), fclose();
127 char key_string[BUFSIZ];
133 memcpy(old_env, env, sizeof(env));
137 /* save terminal state */
138 if (ioctl(0, TIOCGETP, &tty_state) == -1)
143 tty_state.sg_flags &= ~ECHO;
144 if (ioctl(0, TIOCSETP, &tty_state) == -1) {
149 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
150 if ((fi = fopen("/dev/tty", "r+")) == NULL)
153 setbuf(fi, (char *)NULL);
154 sig = signal(SIGINT, catch);
156 (void)ioctl(fileno(fi), TCGETA, &ttyb);
159 flags = ttyb.c_lflag;
160 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
161 (void)ioctl(fileno(fi), TCSETAF, &ttyb);
166 printf("%s", prompt);
169 h19line(s, sizeof(s), 0);
173 if (good_gets(s, max) <= 0) {
175 break; /* just give up */
177 continue; /* try again: blank line */
181 printf("\nVerifying, please re-enter %s", prompt);
184 h19line(key_string, sizeof(key_string), 0);
185 if (!strlen(key_string))
188 if (good_gets(key_string, sizeof(key_string)) <= 0)
191 if (strcmp(s, key_string)) {
192 printf("\n\07\07Mismatch - try again\n");
206 /* turn echo back on */
207 tty_state.sg_flags |= ECHO;
208 if (ioctl(0, TIOCSETP, &tty_state))
211 memcpy(env, old_env, sizeof(env));
213 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
214 ttyb.c_lflag = flags;
216 (void)ioctl(fileno(fi), TCSETAW, &ttyb);
217 (void)signal(SIGINT, sig);
221 (void)kill(getpid(), SIGINT);
225 memset(key_string, 0, sizeof(key_string));
226 s[max - 1] = 0; /* force termination */
227 return !ok; /* return nonzero if not okay */
232 * this can be static since we should never have more than
235 static int (*old_sigfunc[NSIG]) (int);
241 for (i = 0; i < NSIG; i++)
242 old_sigfunc[i] = signal(i, sig_restore);
249 for (i = 0; i < NSIG; i++)
250 signal(i, old_sigfunc[i]);
254 sig_restore(int sig, int code, struct sigcontext *scp)
260 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)