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);
160 p = strtok(NULL, ", \t");
162 if (p && (*p != '\0')) {
163 if ((*iptr & AUTH_RADIUS_FST) && !strcmp(p, "local"))
164 *iptr = (*iptr) | AUTH_LOCAL_SND;
165 else if ((*iptr & AUTH_LOCAL_FST) && !strcmp(p, "radius"))
166 *iptr = (*iptr) | AUTH_RADIUS_SND;
168 error("%s: auth_order: unknown or unexpected keyword: %s", filename, p);
174 option->val = (void *) iptr;
181 * Function: rc_read_config
183 * Purpose: read the global config file
185 * Returns: 0 on success, -1 when failure
188 int rc_read_config(char *filename)
191 char buffer[512], *p;
195 if ((configfd = fopen(filename,"r")) == NULL)
197 error("rc_read_config: can't open %s: %m", filename);
202 while ((fgets(buffer, sizeof(buffer), configfd) != NULL))
207 if ((*p == '\n') || (*p == '#') || (*p == '\0'))
210 p[strlen(p)-1] = '\0';
213 if ((pos = strcspn(p, "\t ")) == 0) {
214 error("%s: line %d: bogus format: %s", filename, line, p);
220 if ((option = find_option(p, OT_ANY)) == NULL) {
221 warn("%s: line %d: unrecognized keyword: %s", filename, line, p);
225 if (option->status != ST_UNDEF) {
226 error("%s: line %d: duplicate option line: %s", filename, line, p);
234 switch (option->type) {
236 if (set_option_str(filename, line, option, p) < 0)
240 if (set_option_int(filename, line, option, p) < 0)
244 if (set_option_srv(filename, line, option, p) < 0)
248 if (set_option_auo(filename, line, option, p) < 0)
252 fatal("rc_read_config: impossible case branch!");
258 return test_config(filename);
262 * Function: rc_conf_str, rc_conf_int, rc_conf_src
264 * Purpose: get the value of a config option
266 * Returns: config option value
269 char *rc_conf_str(char *optname)
273 option = find_option(optname, OT_STR);
276 fatal("rc_conf_str: unkown config option requested: %s", optname);
277 return (char *)option->val;
280 int rc_conf_int(char *optname)
284 option = find_option(optname, OT_INT|OT_AUO);
287 fatal("rc_conf_int: unkown config option requested: %s", optname);
288 return *((int *)option->val);
291 SERVER *rc_conf_srv(char *optname)
295 option = find_option(optname, OT_SRV);
298 fatal("rc_conf_srv: unkown config option requested: %s", optname);
299 return (SERVER *)option->val;
303 * Function: test_config
305 * Purpose: test the configuration the user supplied
307 * Returns: 0 on success, -1 when failure
310 static int test_config(char *filename)
317 if (!(rc_conf_srv("authserver")->max))
319 error("%s: no authserver specified", filename);
322 if (!(rc_conf_srv("acctserver")->max))
324 error("%s: no acctserver specified", filename);
327 if (!rc_conf_str("servers"))
329 error("%s: no servers file specified", filename);
332 if (!rc_conf_str("dictionary"))
334 error("%s: no dictionary specified", filename);
338 if (rc_conf_int("radius_timeout") <= 0)
340 error("%s: radius_timeout <= 0 is illegal", filename);
343 if (rc_conf_int("radius_retries") <= 0)
345 error("%s: radius_retries <= 0 is illegal", filename);
350 file = rc_conf_str("login_local");
351 if (stat(file, &st) == 0)
353 if (!S_ISREG(st.st_mode)) {
354 error("%s: not a regular file: %s", filename, file);
358 error("%s: file not found: %s", filename, file);
361 file = rc_conf_str("login_radius");
362 if (stat(file, &st) == 0)
364 if (!S_ISREG(st.st_mode)) {
365 error("%s: not a regular file: %s", filename, file);
369 error("%s: file not found: %s", filename, file);
374 if (rc_conf_int("login_tries") <= 0)
376 error("%s: login_tries <= 0 is illegal", filename);
379 if (rc_conf_str("seqfile") == NULL)
381 error("%s: seqfile not specified", filename);
384 if (rc_conf_int("login_timeout") <= 0)
386 error("%s: login_timeout <= 0 is illegal", filename);
389 if (rc_conf_str("mapfile") == NULL)
391 error("%s: mapfile not specified", filename);
394 if (rc_conf_str("nologin") == NULL)
396 error("%s: nologin not specified", filename);
404 * Function: rc_find_match
406 * Purpose: see if ip_addr is one of the ip addresses of hostname
408 * Returns: 0 on success, -1 when failure
412 static int find_match (UINT4 *ip_addr, char *hostname)
418 if (rc_good_ipaddr (hostname) == 0)
420 if (*ip_addr == ntohl(inet_addr (hostname)))
427 if ((hp = gethostbyname (hostname)) == (struct hostent *) NULL)
431 for (paddr = hp->h_addr_list; *paddr; paddr++)
433 addr = ** (UINT4 **) paddr;
434 if (ntohl(addr) == *ip_addr)
444 * Function: rc_find_server
446 * Purpose: search a server in the servers file
448 * Returns: 0 on success, -1 on failure
452 int rc_find_server (char *server_name, UINT4 *ip_addr, char *secret)
462 char hostnm[AUTH_ID_LEN + 1];
464 /* Get the IP address of the authentication server */
465 if ((*ip_addr = rc_get_ipaddr (server_name)) == (UINT4) 0)
468 if ((clientfd = fopen (rc_conf_str("servers"), "r")) == (FILE *) NULL)
470 error("rc_find_server: couldn't open file: %m: %s", rc_conf_str("servers"));
474 myipaddr = rc_own_ipaddress();
477 while (fgets (buffer, sizeof (buffer), clientfd) != (char *) NULL)
482 if ((h = strtok (buffer, " \t\n")) == NULL) /* first hostname */
485 memset (hostnm, '\0', AUTH_ID_LEN + 1);
486 strlcpy (hostnm, h, AUTH_ID_LEN + 1);
488 if ((s = strtok (NULL, " \t\n")) == NULL) /* and secret field */
491 memset (secret, '\0', MAX_SECRET_LENGTH + 1);
492 strlcpy (secret, s, MAX_SECRET_LENGTH + 1);
494 if (!strchr (hostnm, '/')) /* If single name form */
496 if (find_match (ip_addr, hostnm) == 0)
502 else /* <name1>/<name2> "paired" form */
504 strtok (hostnm, "/");
505 if (find_match (&myipaddr, hostnm) == 0)
506 { /* If we're the 1st name, target is 2nd */
507 host2 = strtok (NULL, " ");
508 if (find_match (ip_addr, host2) == 0)
514 else /* If we were 2nd name, target is 1st name */
516 if (find_match (ip_addr, hostnm) == 0)
527 memset (buffer, '\0', sizeof (buffer));
528 memset (secret, '\0', sizeof (secret));
529 error("rc_find_server: couldn't find RADIUS server %s in %s",
530 server_name, rc_conf_str("servers"));