Debian Codegraph @

ax25/access.c

raw
1 #include <stdio.h>
2 #include <ctype.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7 #include <time.h>
8 #include <limits.h>
9 #include <pwd.h>
10 #include <fcntl.h>
11
12 #include "access.h"
13 #include "md5.h"
14 #include "axspawn.h"
15
16 #define CONV_RAND_MAX 0x7fffffff
17
18 #define SYSTEMPW 0
19 #define USERPW 1
20
21 static long seed = 1L;
22
23 /*--------------------------------------------------------------------------*/
24
25 static int conv_rand(void)
26 {
27 seed = (1103515245L * seed + 12345) & CONV_RAND_MAX;
28 return (int) (seed & 077777);
29 }
30
31 /*--------------------------------------------------------------------------*/
32
33 static void conv_randomize(void)
34 {
35 seed = (time(NULL) & CONV_RAND_MAX);
36 }
37
38 /*--------------------------------------------------------------------------*/
39
40 static int conv_random(int num, int base)
41 {
42 return ((long) (conv_rand() * time(NULL)) & CONV_RAND_MAX) % num + base;
43 }
44
45 /*--------------------------------------------------------------------------*/
46
47 static void char_to_hex(char *c, char *h, int n)
48 {
49
50 int i;
51 static char *hextable = "0123456789abcdef";
52
53 for (i = 0; i < n; i++) {
54 *h++ = hextable[(*c>>4)&0xf];
55 *h++ = hextable[*c++ &0xf];
56 }
57 *h = '\0';
58 }
59
60 /*--------------------------------------------------------------------------*/
61
62 static char *generate_rand_pw(int len)
63 {
64 static char pass[PASSSIZE+1];
65 int i, j;
66
67 pass[0] = 0;
68
69 if (seed == 1L)
70 conv_randomize();
71
72 if (len < 1)
73 return pass;
74
75 if (len > PASSSIZE)
76 len = PASSSIZE;
77
78 for (i = 0; i < len; i++) {
79 j = conv_random(10+26*2, 0);
80 if (j < 10) {
81 pass[i] = j + '0';
82 continue;
83 }
84 j -= 10;
85 if (j < 26) {
86 pass[i] = j + 'A';
87 continue;
88 }
89 j -= 26;
90 pass[i] = j + 'a';
91 }
92 pass[len] = 0;
93
94 return pass;
95 }
96
97 /*--------------------------------------------------------------------------*/
98
99 static void calc_md5_pw (const char *MD5prompt, const char *MD5pw,
100 char *MD5result)
101 {
102 MD5_CTX context;
103 short i, n, len;
104 char buffer[1024];
105
106 strncpy(buffer, MD5prompt, 10);
107 buffer[10] = 0;
108 strcat(buffer, MD5pw);
109
110 MD5Init(&context);
111
112 len = strlen(buffer);
113 for (i= 0; i < len; i += 16) {
114 n = (len - i) > 16 ? 16 : (len - i);
115 MD5Update(&context, (unsigned char *) buffer+i, n);
116 }
117
118 MD5Final(&context);
119
120 MD5result[0] = '\0';
121 for (i = 0; i < 16; i++) {
122 MD5result[i] = context.digest[i];
123 }
124 }
125
126 /*--------------------------------------------------------------------------*/
127
128 void ask_pw_sys(char *prompt, char *pass_want, char *pw)
129 {
130 char buffer[2048];
131 int five_digits[5];
132 int pwlen;
133 int i, j;
134
135 pass_want[0]= 0;
136
137 if (!pw || !*pw)
138 return;
139
140 pwlen = strlen(pw);
141
142 if (seed == 1L)
143 conv_randomize();
144
145 for (i = 0; i < 5; i++) {
146 int k;
147 again:
148 j = conv_random(pwlen, 0);
149 /* store generated request-numbers */
150 five_digits[i] = j+1; /* pos0 refers as 1 */
151 /* same number again? */
152 for (k = 0; k < i; k++) {
153 if (five_digits[k] == five_digits[i])
154 goto again;
155 }
156 /* store expected string in cp->passwd */
157 pass_want[i] = pw[j];
158 }
159 /* and terminate the string */
160 pass_want[i] = 0;
161
162 sprintf(buffer, "\r%s> %d %d %d %d %d\r", prompt, five_digits[0], five_digits[1], five_digits[2], five_digits[3], five_digits[4]);
163 write_ax25(buffer, strlen(buffer), 1);
164 }
165
166 /*--------------------------------------------------------------------------*/
167
168 void ask_pw_md5(char *prompt, char *pass_want, char *pw)
169 {
170 #define SALT_LEN 10
171 char buffer[2048];
172 char cipher[16];
173 char key[256];
174 char *challenge;
175
176 pass_want[0]= 0;
177
178 if (!pw || !*pw)
179 return;
180
181 if (seed == 1L)
182 conv_randomize();
183
184 strncpy(key, pw, sizeof(key));
185 key[sizeof(key)-1] = 0;
186
187 /* compute random salt */
188 challenge = generate_rand_pw(SALT_LEN);
189
190 /* ask for proper response to this challenge: */
191 sprintf(buffer, "\r%s> [%s]\r", prompt, challenge);
192 write_ax25(buffer, strlen(buffer), 1);
193 /* compute md5 challenge */
194 calc_md5_pw(challenge, key, cipher);
195 /* store expected answer */
196 char_to_hex(cipher, pass_want, 16);
197 }
198
199 /*--------------------------------------------------------------------------*/
200
201 static void write_example_passwd(char *pwfile, char pwlocation,
202 struct passwd *pw)
203 {
204 FILE * f;
205 int i;
206
207 i = open(pwfile, O_CREAT | O_WRONLY | O_TRUNC,
208 (S_IRUSR | S_IWUSR | (pwlocation == SYSTEMPW ? (S_IRGRP /* | S_IWGRP */) : 0)));
209 if (i == -1)
210 return;
211 fchown(i, (pwlocation == SYSTEMPW ? 0 : pw->pw_uid), (pwlocation == SYSTEMPW ? 0 : pw->pw_gid));
212 close(i);
213 f = fopen(pwfile, "w");
214 if (!f)
215 return;
216 fprintf(f, "# %s Password file for axspawn\n", (pwlocation == SYSTEMPW ? "System" : "User"));
217 if (pwlocation == SYSTEMPW) {
218 fprintf(f, "# disable user self-administered passwords in $HOME/.%s\n", PWFILE);
219 fprintf(f, "# with the line \"systempasswordonly\"\n");
220 fprintf(f, "# systempasswordonly\n");
221 }
222 fprintf(f, "# Examples (sys and md5 passwords may differ):\n");
223 fprintf(f, "# md5 standard (secure) - length: >= %d and <= %d characters\n", MINPWLEN_MD5, PASSSIZE);
224 fprintf(f, "# %smd5:%s\n", (pwlocation == SYSTEMPW ? "username:" : ""), generate_rand_pw(MINPWLEN_MD5));
225 fprintf(f, "# sys/baycom standard (not very secure) - length: >= %d and <= %d characters\n", MINPWLEN_SYS, PASSSIZE);
226 fprintf(f, "# %ssys:%s\n", (pwlocation == SYSTEMPW ? "username:" : ""), generate_rand_pw(MINPWLEN_SYS));
227 fprintf(f, "# unix standard (plaintext): no password is read here. Your password is looked\n");
228 fprintf(f, "# up during login in the system password table /etc/passwd or /etc/shadow\n");
229 fprintf(f, "# unix\n");
230 fclose(f);
231 }
232
233 /*--------------------------------------------------------------------------*/
234
235 char *read_pwd (struct passwd *pw, int *pwtype)
236 {
237 FILE * f = NULL;
238 struct stat statbuf;
239 char pwfile[PATH_MAX + 1];
240 int len;
241 char pwlocation;
242 char buf[2048];
243 int only_systempw = 0;
244 char *pass = NULL;
245 char *p_buf;
246 char *p;
247
248
249 for (pwlocation = 0; pwlocation < 2; pwlocation++) {
250
251 if (pwlocation == SYSTEMPW) {
252 sprintf(pwfile, "/%s/%s", AX25_SYSCONFDIR, PWFILE);
253 if (stat(pwfile, &statbuf)) {
254 write_example_passwd(pwfile, pwlocation, pw);
255 continue;
256 }
257 if (!S_ISREG(statbuf.st_mode) || (statbuf.st_mode & (S_IROTH | S_IWOTH)))
258 continue;
259 f = fopen(pwfile, "r");
260 if (!f)
261 continue;
262 } else {
263 if (only_systempw)
264 goto end;
265 snprintf(pwfile, sizeof(pwfile), "%s/.%s", pw->pw_dir, PWFILE);
266 pwfile[sizeof(pwfile)-1] = 0;
267
268 if (stat(pwfile, &statbuf)) {
269 sprintf(buf, "Notice: No .%s file found in your homedirectory (for more secure\r", PWFILE);
270 write_ax25(buf, strlen(buf), 1);
271 sprintf(buf, " password authentication than plaintext). Generating example file,\r");
272 write_ax25(buf, strlen(buf), 1);
273 sprintf(buf, " with unique passwords (which may be changed).\r");
274 write_ax25(buf, strlen(buf), 1);
275 sprintf(buf, " Please edit ~/.%s, and enable your preferred authentication type;\r", PWFILE);
276 write_ax25(buf, strlen(buf), 1);
277 sprintf(buf, " MD5 is recommended.\r");
278 write_ax25(buf, strlen(buf), 1);
279 write_example_passwd(pwfile, pwlocation, pw);
280 goto end;
281 }
282 if (!S_ISREG(statbuf.st_mode)) {
283 sprintf(buf, "Error: password file .%s should be a regular file. Skipping..\r", PWFILE);
284 write_ax25(buf, strlen(buf), 1);
285 goto end;
286 }
287 if (statbuf.st_uid != 0 && statbuf.st_uid != pw->pw_uid) {
288 sprintf(buf, "Error: your password file .%s is not owned by you. Skipping..\r", PWFILE);
289 write_ax25(buf, strlen(buf), 1);
290 goto end;
291 }
292 if ((statbuf.st_mode & (S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH))) {
293 sprintf(buf, "WARNING: your password file .%s has wrong permissions.\r", PWFILE);
294 write_ax25(buf, strlen(buf), 1);
295 sprintf(buf, " Please change it with\r");
296 write_ax25(buf, strlen(buf), 1);
297 sprintf(buf, " chmod 600 .%s\r", PWFILE);
298 write_ax25(buf, strlen(buf), 1);
299 sprintf(buf, " and don't forget to change your password stored in .%s\r", PWFILE);
300 write_ax25(buf, strlen(buf), 1);
301 sprintf(buf, " because it may be compromised.\r");
302 write_ax25(buf, strlen(buf), 1);
303 /* go on.. if user takes no action, he always gets this message */
304 }
305 f = fopen(pwfile, "r");
306 if (!f)
307 goto end;
308 }
309
310
311 for (;;) {
312 if (!fgets(buf, sizeof(buf), f)) {
313 fclose(f);
314 if (pwlocation == SYSTEMPW)
315 break;
316 /* perhaps this is too irritating for the user
317 when the message occurs always. Thus only
318 write the notice, if cleartext fallback
319 is disabled by administrative configuration.
320 */
321 if (!((*pwtype) & PW_CLEARTEXT)) {
322 sprintf(buf, "Failed to find a suitable password in %s\r", pwfile);
323 write_ax25(buf, strlen(buf), 1);
324 }
325 goto end;
326 }
327 p = strchr(buf, '\n');
328 if (p)
329 *p = 0;
330 if (!*buf || isspace(*buf & 0xff))
331 continue;
332 if (*buf == '#')
333 continue;
334 if (pwlocation == SYSTEMPW) {
335 if (!Strcasecmp(buf, "systempasswordonly")) {
336 only_systempw = 1;
337 continue;
338 }
339 p = strchr(buf, ':');
340 if (!p)
341 continue;
342 *p++ = 0;
343 if (strcmp(pw->pw_name, buf))
344 continue;
345 p_buf = p;
346 } else {
347 p_buf = buf;
348 }
349
350 if (!Strcasecmp(p_buf, "unix")) {
351 pass = p_buf;
352 } else {
353 pass = strchr(p_buf, ':');
354 if (!pass)
355 continue;
356 *pass++ = 0;
357 }
358
359 while (*pass && isspace(*pass & 0xff))
360 pass++;
361 for (p = pass; *p && !isspace(*p & 0xff); p++) ;
362 *p = 0;
363
364 if ( (*pwtype & PW_MD5) && !Strcasecmp(p_buf, "md5") ) {
365 fclose(f);
366 *pwtype = PW_MD5;
367 goto found;
368 } else if ( (*pwtype & PW_SYS) && (!Strcasecmp(p_buf, "sys") || !strcmp(p_buf, "baycom")) ) {
369 fclose(f);
370 *pwtype = PW_SYS;
371 goto found;
372 } else if ( (*pwtype & PW_UNIX) && (!Strcasecmp(p_buf, "unix") ) ) {
373 fclose(f);
374 *pwtype = PW_UNIX;
375 return NULL;
376 }
377 }
378 }
379 found:
380
381 if (!pass || !*pwtype)
382 goto end;
383
384 len = strlen(pass);
385
386 if ((*pwtype == PW_SYS && len < MINPWLEN_SYS) || (*pwtype == PW_MD5 && len < MINPWLEN_MD5)) {
387 sprintf(buf, "Password in password file too short\r");
388 write_ax25(buf, strlen(buf), 1);
389 goto end;
390 }
391 if (strlen(pass) > PASSSIZE)
392 pass[PASSSIZE] = 0;
393
394 return strdup(pass);
395
396 end:
397 *pwtype = (((*pwtype) & PW_CLEARTEXT) ? PW_CLEARTEXT : 0);
398 /* ^ may allow cleartext? - then cleartext, else deny */
399 return NULL;
400 }