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 <afs/param.h>
14 #include <afsconfig.h>
18 #include <mit-cpyright.h>
24 #include <sys/ioctl.h>
31 #if defined(AFS_AIX_ENV)
34 #if defined(AFS_SGI_ENV)
39 #if defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
40 /* Just temp till we figure out the aix stuff */
50 static void sig_restore();
51 static push_signals(), pop_signals();
55 /*** Routines ****************************************************** */
57 des_read_password(k,prompt,verify)
63 char key_string[BUFSIZ];
72 ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
74 string_to_key(key_string, k);
77 bzero(key_string, sizeof (key_string));
81 /* good_gets is like gets except that it take a max string length and won't
82 write past the end of its input buffer. It returns a variety of negative
83 numbers in case of errors and zero if there was no characters read (a blank
84 line for instance). Otherwise it returns the length of the string read in.
87 static int good_gets (s, max)
90 { int l; /* length of string read */
91 if (!fgets (s, max, stdin)) {
92 if (feof(stdin)) return EOF; /* EOF on input, nothing read */
93 else return -2; /* I don't think this can happen */
96 if (l && (s[l-1] == '\n')) s[--l] = 0;
101 * This version just returns the string, doesn't map to key.
103 * Returns 0 on success, non-zero on failure.
106 #if !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
111 read_pw_string(s,max,prompt,verify)
118 int len; /* password length */
122 struct sgttyb tty_state;
124 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
128 int (*sig)(), catch();
129 extern void setbuf();
130 extern int kill(), fclose();
133 char key_string[BUFSIZ];
140 bcopy(env, old_env, sizeof(env));
144 /* save terminal state*/
145 if (ioctl(0,TIOCGETP,&tty_state) == -1)
150 tty_state.sg_flags &= ~ECHO;
151 if (ioctl(0,TIOCSETP,&tty_state) == -1) {
156 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
157 if((fi = fopen("/dev/tty", "r+")) == NULL)
160 setbuf(fi, (char*)NULL);
161 sig = signal(SIGINT, catch);
163 (void) ioctl(fileno(fi), TCGETA, &ttyb);
166 flags = ttyb.c_lflag;
167 ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
168 (void) ioctl(fileno(fi), TCSETAF, &ttyb);
176 h19line(s,sizeof(s),0);
180 if (good_gets(s, max) <= 0) {
181 if (feof (stdin)) break; /* just give up */
182 else continue; /* try again: blank line */
186 printf("\nVerifying, please re-enter %s",prompt);
189 h19line(key_string,sizeof(key_string),0);
190 if (!strlen(key_string))
193 if (good_gets(key_string, sizeof(key_string)) <= 0)
196 if (strcmp(s,key_string)) {
197 printf("\n\07\07Mismatch - try again\n");
209 /* turn echo back on */
210 tty_state.sg_flags |= ECHO;
211 if (ioctl(0,TIOCSETP,&tty_state))
214 bcopy(old_env, env, sizeof(env));
216 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
217 ttyb.c_lflag = flags;
219 (void) ioctl(fileno(fi), TCSETAW, &ttyb);
220 (void) signal(SIGINT, sig);
224 (void) kill(getpid(), SIGINT);
228 bzero(key_string, sizeof (key_string));
229 s[max-1] = 0; /* force termination */
230 return !ok; /* return nonzero if not okay */
235 * this can be static since we should never have more than
238 static int (*old_sigfunc[NSIG])();
240 static push_signals()
243 for (i = 0; i < NSIG; i++)
244 old_sigfunc[i] = signal(i,sig_restore);
250 for (i = 0; i < NSIG; i++)
251 signal(i,old_sigfunc[i]);
254 static void sig_restore(sig,code,scp)
256 struct sigcontext *scp;
262 #if defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)