2 * realm.c Read and process realm file.
5 * Version: @(#) realm.c 16-Jul-1998 miquels@cistron.nl
10 "@(#)realm.c 1.0 Copyright 1998 Cistron Internet Services B.V.";
12 #include <sys/types.h>
13 #include <sys/socket.h>
15 #include <netinet/in.h>
33 static struct tpl tpl[] = {
34 { "realm", offsetof(CONF, realm) },
35 { "radwtmp", offsetof(CONF, radwtmp) },
36 { "radutmp", offsetof(CONF, radutmp) },
37 { "acctdir", offsetof(CONF, acctdir) },
38 { "acctdir2", offsetof(CONF, acctdir2) },
39 { "authproxy", offsetof(CONF, authproxy) },
40 { "acctproxy", offsetof(CONF, acctproxy) },
41 { "striprealm", offsetof(CONF, striprealm) },
46 * Initialize the config file structs.
48 static CONF *initconf(void)
50 conf = (CONF *)malloc(sizeof(CONF));
51 memset(conf, 0, sizeof(CONF));
53 strcpy(conf->radutmp, RADUTMP);
54 strcpy(conf->radutmp, RADWTMP);
55 strcpy(conf->confdir, RADIUS_DIR);
56 strcpy(conf->acctdir, RADACCT_DIR);
57 strcpy(conf->logdir, RADLOG_DIR);
58 strcpy(conf->pidfile, RADIUS_PID);
59 strcpy(conf->checkrad, CHECKRAD1);
60 strcpy(conf->checkrad2, CHECKRAD2);
61 strcpy(conf->striprealm, "yes");
67 * Read the config file.
69 int readconf(char *conffile)
81 if ((fp = fopen(conffile, "r")) == NULL) {
82 log(L_ERR, "%s: %s", conffile, strerror(errno));
88 * Read config file line by line.
90 while(fgets(buf, sizeof(buf), fp)) {
93 * Skip comments and empty lines, and split
94 * the rest up in key/value pairs.
96 if (buf[0] == '#' || buf[0] == '\n' || buf[0] == 0)
98 key = strtok(buf, " \t");
99 val = strtok(NULL, "\n");
100 if (key == NULL || key[0] == 0 ||
101 val == NULL || val[0] == 0) {
102 log(L_ERR, "%s[%d]: syntax error", conffile, lineno);
107 * The "realm" key is special, we allocate a new
108 * CONF now _unless_ the "realm" keyword is the
109 * first keyword in the file.
111 if (strcmp(key, "realm") == 0 && !first) {
112 cf2 = (CONF *)malloc(sizeof(CONF));
113 memcpy(cf2, cf, sizeof(CONF));
114 strcpy(cf2->striprealm, "no");
121 * Find the key in our keyword list and
122 * calculate the offset into the CONF struct
125 for(i = 0; tpl[i].name; i++) {
126 if (strcmp(tpl[i].name, key) == 0)
129 if (tpl[i].name == NULL) {
130 log(L_ERR, "%s[%d]: unknown keyword %s",
131 conffile, lineno, key);
134 strcpy((char *)cf + tpl[i].offset, val);
142 * Find the configuration for a certain realm.
143 * We modify the username in-place, so this function should
144 * only be called once.
146 CONF *getconf(char *username)
152 strncpy(buf, username, 128);
156 * Split username and realm. We support both
157 * realm\username and username@realm.
159 if ((realm = strchr(buf, '@')) != NULL) {
162 } else if ((user = strchr(buf, '\\')) != NULL) {
169 * Find this realm in the conffile, if not found use
170 * the default (local) realm and do not strip the realm,
171 * If found strip realm from username if needed.
173 for (cf = conf; cf; cf = cf->next)
174 if (strcasecmp(realm, cf->realm) == 0)
179 if (strcmp(cf->striprealm, "yes") == 0)
180 strcpy(username, user);