2 * rlm_files.c authorization: Find a user in the "users" file.
3 * accounting: Write the "detail" files.
9 static const char rcsid[] = "$Id$";
30 struct file_instance {
43 * See if a VALUE_PAIR list contains Fall-Through = Yes
45 static int fallthrough(VALUE_PAIR *vp)
49 tmp = pairfind(vp, PW_FALL_THROUGH);
51 return tmp ? tmp->lvalue : 0;
55 * A temporary holding area for config values to be extracted
56 * into, before they are copied into the instance data
58 static struct file_instance config;
60 static CONF_PARSER module_config[] = {
61 { "usersfile", PW_TYPE_STRING_PTR, &config.usersfile, RADIUS_USERS },
62 { "acctusersfile", PW_TYPE_STRING_PTR, &config.acctusersfile, RADIUS_ACCT_USERS },
63 { "compat", PW_TYPE_STRING_PTR, &config.compat_mode, "cistron" },
64 { NULL, -1, NULL, NULL }
67 static int getusersfile(const char *filename, PAIR_LIST **pair_list)
70 PAIR_LIST *users = NULL;
72 rcode = pairlist_read(filename, &users, 1);
78 * Walk through the 'users' file list, if we're debugging,
79 * or if we're in compat_mode.
82 (strcmp(config.compat_mode, "cistron") == 0)) {
85 int compat_mode = FALSE;
87 if (strcmp(config.compat_mode, "cistron") == 0) {
94 DEBUG("[%s]:%d Cistron compatibility checks for entry %s ...",
95 filename, entry->lineno,
100 * Look for improper use of '=' in the
101 * check items. They should be using
102 * '==' for on-the-wire RADIUS attributes,
103 * and probably ':=' for server
104 * configuration items.
106 for (vp = entry->check; vp != NULL; vp = vp->next) {
108 * Ignore attributes which are set
111 if (vp->operator != T_OP_EQ) {
116 * If it's a vendor attribute,
117 * or it's a wire protocol,
118 * ensure it has '=='.
120 if (((vp->attribute & ~0xffff) != 0) ||
121 (vp->attribute < 0x100)) {
123 DEBUG("[%s]:%d WARNING! Changing '%s =' to '%s =='\n\tfor comparing RADIUS attribute in check item list for user %s",
124 filename, entry->lineno,
128 DEBUG("\tChanging '%s =' to '%s =='",
131 vp->operator = T_OP_CMP_EQ;
136 * Cistron Compatibility mode.
138 * Re-write selected attributes
139 * to be '+=', instead of '='.
141 * All others get set to '=='
145 * Non-wire attributes become +=
147 * On the write attributes
150 if ((vp->attribute >= 0x100) &&
151 (vp->attribute <= 0xffff) &&
152 (vp->attribute != PW_HINT) &&
153 (vp->attribute != PW_HUNTGROUP_NAME)) {
154 DEBUG("\tChanging '%s =' to '%s +='",
156 vp->operator = T_OP_ADD;
158 DEBUG("\tChanging '%s =' to '%s =='",
160 vp->operator = T_OP_CMP_EQ;
164 } /* end of loop over check items */
168 * Look for server configuration items
171 * It's a common enough mistake, that it's
174 for (vp = entry->reply; vp != NULL; vp = vp->next) {
176 * If it's NOT a vendor attribute,
177 * and it's NOT a wire protocol
178 * and we ignore Fall-Through,
179 * then bitch about it, giving a
180 * good warning message.
182 if (!(vp->attribute & ~0xffff) &&
183 (vp->attribute > 0xff) &&
184 (vp->attribute > 1000)) {
185 log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
186 "\tfound in reply item list for user \"%s\".\n"
187 "\tThis attribute MUST go on the first line"
188 " with the other check items",
189 filename, entry->lineno, vp->name,
204 * (Re-)read the "users" file into memory.
206 static int file_instantiate(CONF_SECTION *conf, void **instance)
208 struct file_instance *inst;
211 inst = malloc(sizeof *inst);
213 radlog(L_ERR|L_CONS, "Out of memory\n");
217 if (cf_section_parse(conf, module_config) < 0) {
222 inst->usersfile = config.usersfile;
223 inst->acctusersfile = config.acctusersfile;
224 config.usersfile = NULL;
225 config.acctusersfile = NULL;
227 rcode = getusersfile(inst->usersfile, &inst->users);
229 radlog(L_ERR|L_CONS, "Errors reading %s", inst->usersfile);
230 free(inst->usersfile);
231 free(inst->acctusersfile);
236 rcode = getusersfile(inst->acctusersfile, &inst->acctusers);
238 radlog(L_ERR|L_CONS, "Errors reading %s", inst->acctusersfile);
239 pairlist_free(&inst->users);
240 free(inst->usersfile);
241 free(inst->acctusersfile);
251 * Find the named user in the database. Create the
252 * set of attribute-value pairs to check and reply with
253 * for this user from the database. The main code only
254 * needs to check the password, the rest is done here.
256 static int file_authorize(void *instance, REQUEST *request)
258 VALUE_PAIR *namepair;
259 VALUE_PAIR *request_pairs;
260 VALUE_PAIR *check_tmp;
261 VALUE_PAIR *reply_tmp;
265 struct file_instance *inst = instance;
266 VALUE_PAIR **check_pairs, **reply_pairs;
267 VALUE_PAIR *check_save;
269 request_pairs = request->packet->vps;
270 check_pairs = &request->config_items;
271 reply_pairs = &request->reply->vps;
274 * Grab the canonical user name.
276 namepair = request->username;
277 name = namepair ? (char *) namepair->strvalue : "NONE";
280 * Find the entry for the user.
282 for(pl = inst->users; pl; pl = pl->next) {
284 * If the current entry is NOT a default,
285 * AND the name does NOT match the current entry,
286 * then skip to the next entry.
288 if ((strcmp(pl->name, "DEFAULT") != 0) &&
289 (strcmp(name, pl->name) != 0)) {
294 * If the current request matches against the
295 * check pairs, then add the reply pairs from the
296 * entry to the current list of reply pairs.
298 if ((paircmp(request_pairs, pl->check, reply_pairs) == 0)) {
300 if((mainconfig.do_usercollide) && (strcmp(pl->name, "DEFAULT"))) {
303 * We have to make sure the password
307 /* Save the orginal config items */
308 check_save = paircopy(request->config_items);
310 /* Copy this users check pairs to the request */
311 check_tmp = paircopy(pl->check);
312 pairmove(check_pairs, &check_tmp);
315 DEBUG2(" users: Checking %s at %d", pl->name, pl->lineno);
316 /* Check the req to see if we matched */
317 if (rad_check_password(request)==0) {
318 DEBUG2(" users: Matched %s at %d", pl->name, pl->lineno);
322 /* Free our saved config items */
323 pairfree(check_save);
326 * Already copied check items, so
327 * just copy reply here
329 reply_tmp = paircopy(pl->reply);
330 pairmove(reply_pairs, &reply_tmp);
333 /* We didn't match here */
335 /* Restore check items */
336 pairfree(request->config_items);
337 request->config_items = paircopy(check_save);
338 check_pairs = &request->config_items;
345 DEBUG2(" users: Matched %s at %d", pl->name, pl->lineno);
347 check_tmp = paircopy(pl->check);
348 reply_tmp = paircopy(pl->reply);
349 pairmove(reply_pairs, &reply_tmp);
350 pairmove(check_pairs, &check_tmp);
352 pairfree(check_tmp); /* should be NULL */
357 if (!fallthrough(pl->reply))
363 * See if we succeeded. If we didn't find the user,
364 * then exit from the module.
367 return RLM_MODULE_NOTFOUND;
370 * Remove server internal parameters.
372 pairdelete(reply_pairs, PW_FALL_THROUGH);
374 return RLM_MODULE_OK;
378 * Pre-Accounting - read the acct_users file for check_items and
379 * config_items. Reply items are Not Recommended(TM) in acct_users,
380 * except for Fallthrough, which should work
382 * This function is mostly a copy of file_authorize
384 static int file_preacct(void *instance, REQUEST *request)
386 VALUE_PAIR *namepair;
388 VALUE_PAIR *request_pairs;
389 VALUE_PAIR **config_pairs;
390 VALUE_PAIR *reply_pairs = NULL;
391 VALUE_PAIR *check_tmp;
392 VALUE_PAIR *reply_tmp;
395 struct file_instance *inst = instance;
397 namepair = request->username;
398 name = namepair ? (char *) namepair->strvalue : "NONE";
399 request_pairs = request->packet->vps;
400 config_pairs = &request->config_items;
403 * Find the entry for the user.
405 for (pl = inst->acctusers; pl; pl = pl->next) {
407 if (strcmp(name, pl->name) && strcmp(pl->name, "DEFAULT"))
410 if (paircmp(request_pairs, pl->check, &reply_pairs) == 0) {
411 DEBUG2(" acct_users: Matched %s at %d",
412 pl->name, pl->lineno);
414 check_tmp = paircopy(pl->check);
415 reply_tmp = paircopy(pl->reply);
416 pairmove(&reply_pairs, &reply_tmp);
417 pairmove(config_pairs, &check_tmp);
419 pairfree(check_tmp); /* should be NULL */
423 if (!fallthrough(pl->reply))
429 * See if we succeeded.
432 return RLM_MODULE_NOOP; /* on to the next module */
435 * FIXME: log a warning if there are any reply items other than
438 pairfree(reply_pairs); /* Don't need these */
440 return RLM_MODULE_OK;
446 static int file_detach(void *instance)
448 struct file_instance *inst = instance;
449 pairlist_free(&inst->users);
450 pairlist_free(&inst->acctusers);
451 free(inst->usersfile);
452 free(inst->acctusersfile);
458 /* globally exported name */
459 module_t rlm_files = {
461 0, /* type: reserved */
462 NULL, /* initialization */
463 file_instantiate, /* instantiation */
464 file_authorize, /* authorization */
465 NULL, /* authentication */
466 file_preacct, /* preaccounting */
467 NULL, /* accounting */
468 NULL, /* checksimul */
469 file_detach, /* detach */