2 * $Id: config.c,v 1.1 2004/11/14 07:26:26 paulus Exp $
4 * Copyright (C) 1995,1996,1997 Lars Fenneberg
6 * Copyright 1992 Livingston Enterprises, Inc.
8 * Copyright 1992,1993, 1994,1995 The Regents of the University of Michigan
9 * and Merit Network, Inc. All Rights Reserved
11 * See the file COPYRIGHT for the respective terms and conditions.
12 * If the file is missing contact me at lf@elemental.net
13 * and I'll send you a copy.
18 #include <radiusclient.h>
21 static int test_config(char *);
24 * Function: find_option
26 * Purpose: find an option in the option list
28 * Returns: pointer to option on success, NULL otherwise
31 static OPTION *find_option(char *optname, unsigned int type)
35 /* there're so few options that a binary search seems not necessary */
36 for (i = 0; i < num_options; i++) {
37 if (!strcmp(config_options[i].name, optname) &&
38 (config_options[i].type & type))
39 return &config_options[i];
46 * Function: set_option_...
48 * Purpose: set a specific option doing type conversions
50 * Returns: 0 on success, -1 on failure
53 static int set_option_str(char *filename, int line, OPTION *option, char *p)
56 option->val = (void *) strdup(p);
63 static int set_option_int(char *filename, int line, OPTION *option, char *p)
68 error("%s: line %d: bogus option value", filename, line);
72 if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
78 option->val = (void *) iptr;
83 static int set_option_srv(char *filename, int line, OPTION *option, char *p)
91 error("%s: line %d: bogus option value", filename, line);
95 serv = (SERVER *) option->val;
97 for (i = 0; i < serv->max; i++) {
102 while ((p = strtok(p, ", \t")) != NULL) {
104 if ((q = strchr(p,':')) != NULL) {
107 serv->port[serv->max] = atoi(q);
109 if (!strcmp(option->name,"authserver"))
110 if ((svp = getservbyname ("radius", "udp")) == NULL)
111 serv->port[serv->max] = PW_AUTH_UDP_PORT;
113 serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
114 else if (!strcmp(option->name, "acctserver"))
115 if ((svp = getservbyname ("radacct", "udp")) == NULL)
116 serv->port[serv->max] = PW_ACCT_UDP_PORT;
118 serv->port[serv->max] = ntohs ((unsigned int) svp->s_port);
120 error("%s: line %d: no default port for %s", filename, line, option->name);
125 serv->name[serv->max++] = strdup(p);
133 static int set_option_auo(char *filename, int line, OPTION *option, char *p)
138 warn("%s: line %d: bogus option value", filename, line);
142 if ((iptr = (int *) malloc(sizeof(iptr))) == NULL) {
148 p = strtok(p, ", \t");
150 if (!strncmp(p, "local", 5))
151 *iptr = AUTH_LOCAL_FST;
152 else if (!strncmp(p, "radius", 6))
153 *iptr = AUTH_RADIUS_FST;
155 error("%s: auth_order: unknown keyword: %s", filename, p);
159 p = strtok(NULL, ", \t");
161 if (p && (*p != '\0')) {
162 if ((*iptr & AUTH_RADIUS_FST) && !strcmp(p, "local"))
163 *iptr = (*iptr) | AUTH_LOCAL_SND;
164 else if ((*iptr & AUTH_LOCAL_FST) && !strcmp(p, "radius"))
165 *iptr = (*iptr) | AUTH_RADIUS_SND;
167 error("%s: auth_order: unknown or unexpected keyword: %s", filename, p);
172 option->val = (void *) iptr;
179 * Function: rc_read_config
181 * Purpose: read the global config file
183 * Returns: 0 on success, -1 when failure
186 int rc_read_config(char *filename)
189 char buffer[512], *p;
193 if ((configfd = fopen(filename,"r")) == NULL)
195 error("rc_read_config: can't open %s: %m", filename);
200 while ((fgets(buffer, sizeof(buffer), configfd) != NULL))
205 if ((*p == '\n') || (*p == '#') || (*p == '\0'))
208 p[strlen(p)-1] = '\0';
211 if ((pos = strcspn(p, "\t ")) == 0) {
212 error("%s: line %d: bogus format: %s", filename, line, p);
218 if ((option = find_option(p, OT_ANY)) == NULL) {
219 error("%s: line %d: unrecognized keyword: %s", filename, line, p);
223 if (option->status != ST_UNDEF) {
224 error("%s: line %d: duplicate option line: %s", filename, line, p);
232 switch (option->type) {
234 if (set_option_str(filename, line, option, p) < 0)
238 if (set_option_int(filename, line, option, p) < 0)
242 if (set_option_srv(filename, line, option, p) < 0)
246 if (set_option_auo(filename, line, option, p) < 0)
250 fatal("rc_read_config: impossible case branch!");
256 return test_config(filename);
260 * Function: rc_conf_str, rc_conf_int, rc_conf_src
262 * Purpose: get the value of a config option
264 * Returns: config option value
267 char *rc_conf_str(char *optname)
271 option = find_option(optname, OT_STR);
274 fatal("rc_conf_str: unkown config option requested: %s", optname);
275 return (char *)option->val;
278 int rc_conf_int(char *optname)
282 option = find_option(optname, OT_INT|OT_AUO);
285 fatal("rc_conf_int: unkown config option requested: %s", optname);
286 return *((int *)option->val);
289 SERVER *rc_conf_srv(char *optname)
293 option = find_option(optname, OT_SRV);
296 fatal("rc_conf_srv: unkown config option requested: %s", optname);
297 return (SERVER *)option->val;
301 * Function: test_config
303 * Purpose: test the configuration the user supplied
305 * Returns: 0 on success, -1 when failure
308 static int test_config(char *filename)
315 if (!(rc_conf_srv("authserver")->max))
317 error("%s: no authserver specified", filename);
320 if (!(rc_conf_srv("acctserver")->max))
322 error("%s: no acctserver specified", filename);
325 if (!rc_conf_str("servers"))
327 error("%s: no servers file specified", filename);
330 if (!rc_conf_str("dictionary"))
332 error("%s: no dictionary specified", filename);
336 if (rc_conf_int("radius_timeout") <= 0)
338 error("%s: radius_timeout <= 0 is illegal", filename);
341 if (rc_conf_int("radius_retries") <= 0)
343 error("%s: radius_retries <= 0 is illegal", filename);
348 file = rc_conf_str("login_local");
349 if (stat(file, &st) == 0)
351 if (!S_ISREG(st.st_mode)) {
352 error("%s: not a regular file: %s", filename, file);
356 error("%s: file not found: %s", filename, file);
359 file = rc_conf_str("login_radius");
360 if (stat(file, &st) == 0)
362 if (!S_ISREG(st.st_mode)) {
363 error("%s: not a regular file: %s", filename, file);
367 error("%s: file not found: %s", filename, file);
372 if (rc_conf_int("login_tries") <= 0)
374 error("%s: login_tries <= 0 is illegal", filename);
377 if (rc_conf_str("seqfile") == NULL)
379 error("%s: seqfile not specified", filename);
382 if (rc_conf_int("login_timeout") <= 0)
384 error("%s: login_timeout <= 0 is illegal", filename);
387 if (rc_conf_str("mapfile") == NULL)
389 error("%s: mapfile not specified", filename);
392 if (rc_conf_str("nologin") == NULL)
394 error("%s: nologin not specified", filename);
402 * Function: rc_find_match
404 * Purpose: see if ip_addr is one of the ip addresses of hostname
406 * Returns: 0 on success, -1 when failure
410 static int find_match (UINT4 *ip_addr, char *hostname)
416 if (rc_good_ipaddr (hostname) == 0)
418 if (*ip_addr == ntohl(inet_addr (hostname)))
425 if ((hp = gethostbyname (hostname)) == (struct hostent *) NULL)
429 for (paddr = hp->h_addr_list; *paddr; paddr++)
431 addr = ** (UINT4 **) paddr;
432 if (ntohl(addr) == *ip_addr)
442 * Function: rc_find_server
444 * Purpose: search a server in the servers file
446 * Returns: 0 on success, -1 on failure
450 int rc_find_server (char *server_name, UINT4 *ip_addr, char *secret)
460 char hostnm[AUTH_ID_LEN + 1];
462 /* Get the IP address of the authentication server */
463 if ((*ip_addr = rc_get_ipaddr (server_name)) == (UINT4) 0)
466 if ((clientfd = fopen (rc_conf_str("servers"), "r")) == (FILE *) NULL)
468 error("rc_find_server: couldn't open file: %m: %s", rc_conf_str("servers"));
472 myipaddr = rc_own_ipaddress();
475 while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL)
480 if ((h = strtok (buffer, " \t\n")) == NULL) /* first hostname */
483 memset (hostnm, '\0', AUTH_ID_LEN);
485 if (len > AUTH_ID_LEN)
489 strncpy (hostnm, h, (size_t) len);
490 hostnm[AUTH_ID_LEN] = '\0';
492 if ((s = strtok (NULL, " \t\n")) == NULL) /* and secret field */
495 memset (secret, '\0', MAX_SECRET_LENGTH);
497 if (len > MAX_SECRET_LENGTH)
499 len = MAX_SECRET_LENGTH;
501 strncpy (secret, s, (size_t) len);
502 secret[MAX_SECRET_LENGTH] = '\0';
504 if (!strchr (hostnm, '/')) /* If single name form */
506 if (find_match (ip_addr, hostnm) == 0)
512 else /* <name1>/<name2> "paired" form */
514 strtok (hostnm, "/");
515 if (find_match (&myipaddr, hostnm) == 0)
516 { /* If we're the 1st name, target is 2nd */
517 host2 = strtok (NULL, " ");
518 if (find_match (ip_addr, host2) == 0)
524 else /* If we were 2nd name, target is 1st name */
526 if (find_match (ip_addr, hostnm) == 0)
537 memset (buffer, '\0', sizeof (buffer));
538 memset (secret, '\0', sizeof (secret));
539 error("rc_find_server: couldn't find RADIUS server %s in %s",
540 server_name, rc_conf_str("servers"));