rxgen, kauth: Set but not used variables
[openafs.git] / src / kauth / read_passwd.c
1 /*
2  * Copyright 1985, 1986, 1987, 1988 by the Massachusetts Institute
3  * of Technology.
4  *
5  * For copying and distribution information, please see the file
6  * <mit-copyright.h>.
7  *
8  * This routine prints the supplied string to standard
9  * output as a prompt, and reads a password string without
10  * echoing.
11  */
12
13 #include <afsconfig.h>
14 #include <afs/param.h>
15
16 #include <mit-cpyright.h>
17
18 #include <stdio.h>
19 #include <des.h>
20 #include <des_prototypes.h>
21 #ifdef  BSDUNIX
22 #include <strings.h>
23 #include <sys/ioctl.h>
24 #include <signal.h>
25 #include <setjmp.h>
26 #else
27 #include <string.h>
28 #endif
29 #if defined(AFS_AIX_ENV)
30 #include <signal.h>
31 #endif
32 #if defined(AFS_SGI_ENV)
33 #include <signal.h>
34 #endif
35 #include <string.h>
36
37 #if     defined (AFS_AIX_ENV) || defined(AFS_SGI_ENV)
38 /* Just temp till we figure out the aix stuff */
39 #undef  BSDUNIX
40 static int intrupt;
41 #endif
42
43 #ifdef  BSDUNIX
44 static jmp_buf env;
45 #endif
46
47 #ifdef BSDUNIX
48 static void sig_restore();
49 static push_signals(), pop_signals();
50 int read_pw_string();
51 #endif
52
53 /*** Routines ****************************************************** */
54 int
55 des_read_password(C_Block *k, char *prompt, int verify)
56 {
57     int ok;
58     char key_string[BUFSIZ];
59
60 #ifdef BSDUNIX
61     if (setjmp(env)) {
62         ok = -1;
63         goto lose;
64     }
65 #endif
66
67     ok = read_pw_string(key_string, BUFSIZ, prompt, verify);
68     if (ok == 0)
69         string_to_key(key_string, k);
70 #ifdef BSDUNIX
71   lose:
72 #endif
73     memset(key_string, 0, sizeof(key_string));
74     return ok;
75 }
76
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.
81    */
82
83 static int
84 good_gets(char *s, int max)
85 {
86     int l;                      /* length of string read */
87     if (!fgets(s, max, stdin)) {
88         if (feof(stdin))
89             return EOF;         /* EOF on input, nothing read */
90         else
91             return -2;          /* I don't think this can happen */
92     }
93     l = strlen(s);
94     if (l && (s[l - 1] == '\n'))
95         s[--l] = 0;
96     return l;
97 }
98
99 /*
100  * This version just returns the string, doesn't map to key.
101  *
102  * Returns 0 on success, non-zero on failure.
103  */
104
105 #if     !defined(BSDUNIX) && (defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV))
106 #include <termio.h>
107 #endif
108
109 int
110 read_pw_string(char *s, int max, char *prompt, int verify)
111 {
112     int ok = 0;
113
114 #ifdef  BSDUNIX
115     jmp_buf old_env;
116     struct sgttyb tty_state;
117 #else
118 #if     defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
119     struct termio ttyb;
120     FILE *fi;
121     char savel, flags;
122     int (*sig) (), catch();
123     extern void setbuf();
124     extern int kill(), fclose();
125 #endif
126 #endif
127     char key_string[BUFSIZ];
128
129     if (max > BUFSIZ) {
130         return -1;
131     }
132 #ifdef  BSDUNIX
133     memcpy(old_env, env, sizeof(env));
134     if (setjmp(env))
135         goto lose;
136
137     /* save terminal state */
138     if (ioctl(0, TIOCGETP, &tty_state) == -1)
139         return -1;
140
141     push_signals();
142     /* Turn off echo */
143     tty_state.sg_flags &= ~ECHO;
144     if (ioctl(0, TIOCSETP, &tty_state) == -1) {
145         pop_signals();
146         return -1;
147     }
148 #else
149 #if     defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
150     if ((fi = fopen("/dev/tty", "r+")) == NULL)
151         return (-1);
152     else
153         setbuf(fi, (char *)NULL);
154     sig = signal(SIGINT, catch);
155     intrupt = 0;
156     (void)ioctl(fileno(fi), TCGETA, &ttyb);
157     savel = ttyb.c_line;
158     ttyb.c_line = 0;
159     flags = ttyb.c_lflag;
160     ttyb.c_lflag &= ~(ECHO | ECHOE | ECHOK | ECHONL);
161     (void)ioctl(fileno(fi), TCSETAF, &ttyb);
162 #endif
163 #endif
164
165     while (!ok) {
166         printf("%s", prompt);
167         fflush(stdout);
168 #ifdef  CROSSMSDOS
169         h19line(s, sizeof(s), 0);
170         if (!strlen(s))
171             continue;
172 #else
173         if (good_gets(s, max) <= 0) {
174             if (feof(stdin))
175                 break;          /* just give up */
176             else
177                 continue;       /* try again: blank line */
178         }
179 #endif
180         if (verify) {
181             printf("\nVerifying, please re-enter %s", prompt);
182             fflush(stdout);
183 #ifdef CROSSMSDOS
184             h19line(key_string, sizeof(key_string), 0);
185             if (!strlen(key_string))
186                 continue;
187 #else
188             if (good_gets(key_string, sizeof(key_string)) <= 0)
189                 continue;
190 #endif
191             if (strcmp(s, key_string)) {
192                 printf("\n\07\07Mismatch - try again\n");
193                 fflush(stdout);
194                 continue;
195             }
196         }
197         ok = 1;
198     }
199
200 #ifdef BSDUNIX
201   lose:
202 #endif
203     if (!ok)
204         memset(s, 0, max);
205 #ifdef  BSDUNIX
206     /* turn echo back on */
207     tty_state.sg_flags |= ECHO;
208     if (ioctl(0, TIOCSETP, &tty_state))
209         ok = 0;
210     pop_signals();
211     memcpy(env, old_env, sizeof(env));
212 #else
213 #if     defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
214     ttyb.c_lflag = flags;
215     ttyb.c_line = savel;
216     (void)ioctl(fileno(fi), TCSETAW, &ttyb);
217     (void)signal(SIGINT, sig);
218     if (fi != stdin)
219         (void)fclose(fi);
220     if (intrupt)
221         (void)kill(getpid(), SIGINT);
222 #endif
223 #endif
224     if (verify)
225         memset(key_string, 0, sizeof(key_string));
226     s[max - 1] = 0;             /* force termination */
227     return !ok;                 /* return nonzero if not okay */
228 }
229
230 #ifdef  BSDUNIX
231 /*
232  * this can be static since we should never have more than
233  * one set saved....
234  */
235 static int (*old_sigfunc[NSIG]) (int);
236
237 static
238 push_signals(void)
239 {
240     int i;
241     for (i = 0; i < NSIG; i++)
242         old_sigfunc[i] = signal(i, sig_restore);
243 }
244
245 static
246 pop_signals(void)
247 {
248     int i;
249     for (i = 0; i < NSIG; i++)
250         signal(i, old_sigfunc[i]);
251 }
252
253 static void
254 sig_restore(int sig, int code, struct sigcontext *scp)
255 {
256     longjmp(env, 1);
257 }
258 #endif
259
260 #if     defined(AFS_AIX_ENV) || defined(AFS_SGI_ENV)
261 static int
262 catch(void)
263 {
264     ++intrupt;
265 }
266 #endif