Debian Codegraph @

pppd/plugins/radius/config.c

raw
1 /*
2 * Copyright (C) 1995,1996,1997 Lars Fenneberg
3 *
4 * Copyright 1992 Livingston Enterprises, Inc.
5 *
6 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
7 * and Merit Network, Inc. All Rights Reserved
8 *
9 * See the file COPYRIGHT for the respective terms and conditions.
10 * If the file is missing contact me at [email protected]
11 * and I'll send you a copy.
12 *
13 */
14
15 #include <includes.h>
16 #include <radiusclient.h>
17 #include <options.h>
18
19 static int test_config(char *);
20
21 /*
22 * Function: find_option
23 *
24 * Purpose: find an option in the option list
25 *
26 * Returns: pointer to option on success, NULL otherwise
27 */
28
29 static OPTION *find_option(char *optname, unsigned int type)
30 {
31 int i;
32
33 /* there're so few options that a binary search seems not necessary */
34 for (i = 0; i < num_options; i++) {
35 if (!strcmp(config_options[i].name, optname) &&
36 (config_options[i].type & type))
37 return &config_options[i];
38 }
39
40 return NULL;
41 }
42
43 /*
44 * Function: set_option_...
45 *
46 * Purpose: set a specific option doing type conversions
47 *
48 * Returns: 0 on success, -1 on failure
49 */
50
51 static int set_option_str(char *filename, int line, OPTION *option, char *p)
52 {
53 if (p)
54 option->val = (void *) strdup(p);
55 else
56 option->val = NULL;
57
58 return 0;
59 }
60
61 static int set_option_int(char *filename, int line, OPTION *option, char *p)
62 {
63 int *iptr;
64
65 if (p == NULL) {
66 error("%s: line %d: bogus option value", filename, line);
67 return (-1);
68 }
69
70 if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
71 novm("read_config");
72 return (-1);
73 }
74
75 *iptr = atoi(p);
76 option->val = (void *) iptr;
77
78 return 0;
79 }
80
81 static int set_option_srv(char *filename, int line, OPTION *option, char *p)
82 {
83 SERVER *serv;
84 char *q;
85 struct servent *svp;
86 int i;
87
88 if (p == NULL) {
89 error("%s: line %d: bogus option value", filename, line);
90 return (-1);
91 }
92
93 serv = (SERVER *) option->val;
94
95 for (i = 0; i < serv->max; i++) {
96 free(serv->name[i]);
97 }
98 serv->max = 0;
99
100 while ((p = strtok(p, ", \t")) != NULL) {
101
102 if ((q = strchr(p,':')) != NULL) {
103 *q = '\0';
104 q++;
105 serv->port[serv->max] = atoi(q);
106 } else {
107 if (!strcmp(option->name,"authserver"))
108 if ((svp = getservbyname ("radius", "udp")) == NULL)
109 serv->port[serv->max] = PW_AUTH_UDP_PORT;
110 else
111 serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
112 else if (!strcmp(option->name, "acctserver"))
113 if ((svp = getservbyname ("radacct", "udp")) == NULL)
114 serv->port[serv->max] = PW_ACCT_UDP_PORT;
115 else
116 serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
117 else {
118 error("%s: line %d: no default port for %s", filename, line, option->name);
119 return (-1);
120 }
121 }
122
123 serv->name[serv->max++] = strdup(p);
124
125 p = NULL;
126 }
127
128 return 0;
129 }
130
131 static int set_option_auo(char *filename, int line, OPTION *option, char *p)
132 {
133 int *iptr;
134
135 if (p == NULL) {
136 warn("%s: line %d: bogus option value", filename, line);
137 return (-1);
138 }
139
140 if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
141 novm("read_config");
142 return (-1);
143 }
144
145 *iptr = 0;
146 p = strtok(p, ", \t");
147
148 if (!strncmp(p, "local", 5))
149 *iptr = AUTH_LOCAL_FST;
150 else if (!strncmp(p, "radius", 6))
151 *iptr = AUTH_RADIUS_FST;
152 else {
153 error("%s: auth_order: unknown keyword: %s", filename, p);
154 free(iptr);
155 return (-1);
156 }
157
158 p = strtok(NULL, ", \t");
159
160 if (p && (*p != '\0')) {
161 if ((*iptr & AUTH_RADIUS_FST) && !strcmp(p, "local"))
162 *iptr = (*iptr) | AUTH_LOCAL_SND;
163 else if ((*iptr & AUTH_LOCAL_FST) && !strcmp(p, "radius"))
164 *iptr = (*iptr) | AUTH_RADIUS_SND;
165 else {
166 error("%s: auth_order: unknown or unexpected keyword: %s", filename, p);
167 free(iptr);
168 return (-1);
169 }
170 }
171
172 option->val = (void *) iptr;
173
174 return 0;
175 }
176
177
178 /*
179 * Function: rc_read_config
180 *
181 * Purpose: read the global config file
182 *
183 * Returns: 0 on success, -1 when failure
184 */
185
186 int rc_read_config(char *filename)
187 {
188 FILE *configfd;
189 char buffer[512], *p;
190 OPTION *option;
191 int line, pos;
192
193 if ((configfd = fopen(filename,"r")) == NULL)
194 {
195 error("rc_read_config: can't open %s: %m", filename);
196 return (-1);
197 }
198
199 line = 0;
200 while ((fgets(buffer, sizeof(buffer), configfd) != NULL))
201 {
202 line++;
203 p = buffer;
204
205 if ((*p == '\n') || (*p == '#') || (*p == '\0'))
206 continue;
207
208 p[strlen(p)-1] = '\0';
209
210
211 if ((pos = strcspn(p, "\t ")) == 0) {
212 error("%s: line %d: bogus format: %s", filename, line, p);
213 fclose(configfd);
214 return (-1);
215 }
216
217 p[pos] = '\0';
218
219 if ((option = find_option(p, OT_ANY)) == NULL) {
220 warn("%s: line %d: unrecognized keyword: %s", filename, line, p);
221 continue;
222 }
223
224 if (option->status != ST_UNDEF) {
225 error("%s: line %d: duplicate option line: %s", filename, line, p);
226 fclose(configfd);
227 return (-1);
228 }
229
230 p += pos+1;
231 while (isspace(*p))
232 p++;
233
234 switch (option->type) {
235 case OT_STR:
236 if (set_option_str(filename, line, option, p) < 0) {
237 fclose(configfd);
238 return (-1);
239 }
240 break;
241 case OT_INT:
242 if (set_option_int(filename, line, option, p) < 0) {
243 fclose(configfd);
244 return (-1);
245 }
246 break;
247 case OT_SRV:
248 if (set_option_srv(filename, line, option, p) < 0) {
249 fclose(configfd);
250 return (-1);
251 }
252 break;
253 case OT_AUO:
254 if (set_option_auo(filename, line, option, p) < 0) {
255 fclose(configfd);
256 return (-1);
257 }
258 break;
259 default:
260 fatal("rc_read_config: impossible case branch!");
261 abort();
262 }
263 }
264 fclose(configfd);
265
266 return test_config(filename);
267 }
268
269 /*
270 * Function: rc_conf_str, rc_conf_int, rc_conf_src
271 *
272 * Purpose: get the value of a config option
273 *
274 * Returns: config option value
275 */
276
277 char *rc_conf_str(char *optname)
278 {
279 OPTION *option;
280
281 option = find_option(optname, OT_STR);
282
283 if (option == NULL)
284 fatal("rc_conf_str: unknown config option requested: %s", optname);
285 return (char *)option->val;
286 }
287
288 int rc_conf_int(char *optname)
289 {
290 OPTION *option;
291
292 option = find_option(optname, OT_INT|OT_AUO);
293
294 if (option == NULL)
295 fatal("rc_conf_int: unknown config option requested: %s", optname);
296 return *((int *)option->val);
297 }
298
299 SERVER *rc_conf_srv(char *optname)
300 {
301 OPTION *option;
302
303 option = find_option(optname, OT_SRV);
304
305 if (option == NULL)
306 fatal("rc_conf_srv: unknown config option requested: %s", optname);
307 return (SERVER *)option->val;
308 }
309
310 /*
311 * Function: test_config
312 *
313 * Purpose: test the configuration the user supplied
314 *
315 * Returns: 0 on success, -1 when failure
316 */
317
318 static int test_config(char *filename)
319 {
320 #if 0
321 struct stat st;
322 char *file;
323 #endif
324
325 if (!(rc_conf_srv("authserver")->max))
326 {
327 error("%s: no authserver specified", filename);
328 return (-1);
329 }
330 if (!(rc_conf_srv("acctserver")->max))
331 {
332 error("%s: no acctserver specified", filename);
333 return (-1);
334 }
335 if (!rc_conf_str("servers"))
336 {
337 error("%s: no servers file specified", filename);
338 return (-1);
339 }
340 if (!rc_conf_str("dictionary"))
341 {
342 error("%s: no dictionary specified", filename);
343 return (-1);
344 }
345
346 if (rc_conf_int("radius_timeout") <= 0)
347 {
348 error("%s: radius_timeout <= 0 is illegal", filename);
349 return (-1);
350 }
351 if (rc_conf_int("radius_retries") <= 0)
352 {
353 error("%s: radius_retries <= 0 is illegal", filename);
354 return (-1);
355 }
356
357 #if 0
358 file = rc_conf_str("login_local");
359 if (stat(file, &st) == 0)
360 {
361 if (!S_ISREG(st.st_mode)) {
362 error("%s: not a regular file: %s", filename, file);
363 return (-1);
364 }
365 } else {
366 error("%s: file not found: %s", filename, file);
367 return (-1);
368 }
369 file = rc_conf_str("login_radius");
370 if (stat(file, &st) == 0)
371 {
372 if (!S_ISREG(st.st_mode)) {
373 error("%s: not a regular file: %s", filename, file);
374 return (-1);
375 }
376 } else {
377 error("%s: file not found: %s", filename, file);
378 return (-1);
379 }
380 #endif
381
382 if (rc_conf_int("login_tries") <= 0)
383 {
384 error("%s: login_tries <= 0 is illegal", filename);
385 return (-1);
386 }
387 if (rc_conf_str("seqfile") == NULL)
388 {
389 error("%s: seqfile not specified", filename);
390 return (-1);
391 }
392 if (rc_conf_int("login_timeout") <= 0)
393 {
394 error("%s: login_timeout <= 0 is illegal", filename);
395 return (-1);
396 }
397 if (rc_conf_str("mapfile") == NULL)
398 {
399 error("%s: mapfile not specified", filename);
400 return (-1);
401 }
402 if (rc_conf_str("nologin") == NULL)
403 {
404 error("%s: nologin not specified", filename);
405 return (-1);
406 }
407
408 return 0;
409 }
410
411 /*
412 * Function: rc_find_match
413 *
414 * Purpose: see if ip_addr is one of the ip addresses of hostname
415 *
416 * Returns: 0 on success, -1 when failure
417 *
418 */
419
420 static int find_match (UINT4 *ip_addr, char *hostname)
421 {
422 UINT4 addr;
423 char **paddr;
424 struct hostent *hp;
425
426 if (rc_good_ipaddr (hostname) == 0)
427 {
428 if (*ip_addr == ntohl(inet_addr (hostname)))
429 {
430 return (0);
431 }
432 }
433 else
434 {
435 if ((hp = gethostbyname (hostname)) == (struct hostent *) NULL)
436 {
437 return (-1);
438 }
439 for (paddr = hp->h_addr_list; *paddr; paddr++)
440 {
441 addr = ** (UINT4 **) paddr;
442 if (ntohl(addr) == *ip_addr)
443 {
444 return (0);
445 }
446 }
447 }
448 return (-1);
449 }
450
451 /*
452 * Function: rc_find_server
453 *
454 * Purpose: search a server in the servers file
455 *
456 * Returns: 0 on success, -1 on failure
457 *
458 */
459
460 int rc_find_server (char *server_name, UINT4 *ip_addr, char *secret)
461 {
462 UINT4 myipaddr = 0;
463 int len;
464 int result;
465 FILE *clientfd;
466 char *h;
467 char *s;
468 char *host2;
469 char buffer[128];
470 char hostnm[AUTH_ID_LEN + 1];
471
472 /* Get the IP address of the authentication server */
473 if ((*ip_addr = rc_get_ipaddr (server_name)) == (UINT4) 0)
474 return (-1);
475
476 if ((clientfd = fopen (rc_conf_str("servers"), "r")) == (FILE *) NULL)
477 {
478 error("rc_find_server: couldn't open file: %m: %s", rc_conf_str("servers"));
479 return (-1);
480 }
481
482 myipaddr = rc_own_ipaddress();
483
484 result = 0;
485 while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL)
486 {
487 if (*buffer == '#')
488 continue;
489
490 if ((h = strtok (buffer, " \t\n")) == NULL) /* first hostname */
491 continue;
492
493 memset (hostnm, '\0', AUTH_ID_LEN + 1);
494 strlcpy (hostnm, h, AUTH_ID_LEN + 1);
495
496 if ((s = strtok (NULL, " \t\n")) == NULL) /* and secret field */
497 continue;
498
499 memset (secret, '\0', MAX_SECRET_LENGTH + 1);
500 strlcpy (secret, s, MAX_SECRET_LENGTH + 1);
501
502 if (!strchr (hostnm, '/')) /* If single name form */
503 {
504 if (find_match (ip_addr, hostnm) == 0)
505 {
506 result++;
507 break;
508 }
509 }
510 else /* <name1>/<name2> "paired" form */
511 {
512 strtok (hostnm, "/");
513 if (find_match (&myipaddr, hostnm) == 0)
514 { /* If we're the 1st name, target is 2nd */
515 host2 = strtok (NULL, " ");
516 if (find_match (ip_addr, host2) == 0)
517 {
518 result++;
519 break;
520 }
521 }
522 else /* If we were 2nd name, target is 1st name */
523 {
524 if (find_match (ip_addr, hostnm) == 0)
525 {
526 result++;
527 break;
528 }
529 }
530 }
531 }
532 fclose (clientfd);
533 if (result == 0)
534 {
535 memset (buffer, '\0', sizeof (buffer));
536 error("rc_find_server: couldn't find RADIUS server %s in %s",
537 server_name, rc_conf_str("servers"));
538 return (-1);
539 }
540 return 0;
541 }