2 * rlm_fastusers.c authorization: Find a user in the hashed "users" file.
3 * accounting: Do nothing. Auth module only.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 * Copyright 2000 The FreeRADIUS server project
22 * Copyright 2000 Jeff Carneal <jeff@apex.net>
25 #include <freeradius-devel/autoconf.h>
27 #include <sys/socket.h>
40 #include <freeradius-devel/radiusd.h>
41 #include <freeradius-devel/modules.h>
43 struct fastuser_instance {
49 PAIR_LIST **hashtable;
58 time_t lastacctusersload;
61 /* Function declarations */
62 static int fallthrough(VALUE_PAIR *vp);
63 static int fastuser_buildhash(struct fastuser_instance *inst);
64 static int fastuser_getfile(struct fastuser_instance *inst, const char *filename,
65 PAIR_LIST **default_list, PAIR_LIST **pair_list,
67 static int fastuser_hash(const char *s, int hashtablesize);
68 static int fastuser_store(PAIR_LIST **hashtable, PAIR_LIST *entry, int idx);
69 static PAIR_LIST *fastuser_find(REQUEST *request, PAIR_LIST *user,
70 const char *username);
71 static void fastuser_tablestats(PAIR_LIST **hashtable, int size);
73 static const CONF_PARSER module_config[] = {
74 { "usersfile", PW_TYPE_FILENAME,
75 offsetof(struct fastuser_instance,usersfile), NULL, "${raddbdir}/users_fast" },
76 { "acctusersfile", PW_TYPE_FILENAME,
77 offsetof(struct fastuser_instance,acctusersfile), NULL, "${raddbdir}/acct_users" },
78 { "hashsize", PW_TYPE_INTEGER,
79 offsetof(struct fastuser_instance,hashsize), NULL, "100000" },
80 { "stats", PW_TYPE_BOOLEAN,
81 offsetof(struct fastuser_instance,stats), NULL, "no" },
82 { "compat", PW_TYPE_STRING_PTR,
83 offsetof(struct fastuser_instance,compat_mode), NULL, "cistron" },
84 { "hash_reload", PW_TYPE_INTEGER,
85 offsetof(struct fastuser_instance,hash_reload), NULL, "600" },
86 { NULL, -1, 0, NULL, NULL }
90 * See if a VALUE_PAIR list contains Fall-Through = Yes
92 static int fallthrough(VALUE_PAIR *vp)
95 tmp = pairfind(vp, PW_FALL_THROUGH);
96 return tmp ? tmp->lvalue : 0;
100 * returncheck - Check for Auth-Type = Reject and return appropriate
101 * module return code if it is found.
103 static int rad_check_return(VALUE_PAIR *list)
105 VALUE_PAIR *authtype;
108 * We check for Auth-Type = Reject here
111 authtype = pairfind(list, PW_AUTHTYPE);
112 if((authtype) && authtype->lvalue == PW_AUTHTYPE_REJECT) {
113 DEBUG2("rad_check_return: Auth-Type is Reject");
114 return RLM_MODULE_REJECT;
117 return RLM_MODULE_UPDATED;
120 static int fastuser_buildhash(struct fastuser_instance *inst) {
122 int rcode, hashindex;
123 PAIR_LIST **newhash=NULL, **oldhash=NULL;
124 PAIR_LIST *newdefaults=NULL, *newacctusers, *cur=NULL;
125 PAIR_LIST *olddefaults=NULL, *oldacctusers=NULL;
128 int reloadacctusers = 1;
131 * Allocate space for hash table here
133 memsize = sizeof(PAIR_LIST *) * inst->hashsize;
135 newhash = (PAIR_LIST **) rad_malloc(memsize);
137 memset((PAIR_LIST *)newhash, 0, memsize);
139 /* Check acct_users last modification time */
140 if ((stat(inst->acctusersfile, &statbuf) != -1)
141 && (statbuf.st_mtime <= inst->lastacctusersload)) {
142 DEBUG2("rlm_fastusers: File %s was unchanged. Not reloading.",
143 inst->acctusersfile);
148 /* Read acct_users */
149 rcode = fastuser_getfile(inst, inst->acctusersfile, NULL, &newacctusers, 1);
152 radlog(L_ERR|L_CONS, "rlm_fastusers: Errors reading %s", inst->usersfile);
156 /* Check users last modification time */
157 if ((stat(inst->usersfile, &statbuf) != -1)
158 && (statbuf.st_mtime <= inst->lastusersload)) {
159 DEBUG2("rlm_fastusers: File %s was unchanged. Not reloading.",
163 /* This was allocated earlier but will remain unused */
168 rcode = fastuser_getfile(inst, inst->usersfile, &newdefaults, newhash, 0);
171 radlog(L_ERR|L_CONS, "rlm_fastusers: Errors reading %s", inst->usersfile);
177 * We need to do this now so that users auths
178 * aren't blocked while we free the old table
181 inst->lastusersload = time(NULL);
182 oldhash = inst->hashtable;
183 inst->hashtable = newhash;
184 olddefaults = inst->defaults;
185 inst->defaults = newdefaults;
188 * When we get here, we assume the hash built properly.
189 * So we begin to tear down the old one
192 for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
193 if(oldhash[hashindex]) {
194 cur = oldhash[hashindex];
200 pairlist_free(&olddefaults);
202 if (reloadacctusers) {
203 inst->lastacctusersload = time(NULL);
204 oldacctusers = inst->acctusers;
205 inst->acctusers = newacctusers;
206 pairlist_free(&oldacctusers);
210 fastuser_tablestats(inst->hashtable, inst->hashsize);
215 static int fastuser_getfile(struct fastuser_instance *inst, const char *filename,
216 PAIR_LIST **default_list, PAIR_LIST **pair_list,
219 PAIR_LIST *users = NULL;
220 PAIR_LIST *entry=NULL, *next=NULL, *cur=NULL, *defaults=NULL, *lastdefault=NULL;
221 int compat_mode = FALSE;
224 int numdefaults = 0, numusers=0;
226 radlog(L_INFO, " fastusers: Reading %s", filename);
227 rcode = pairlist_read(filename, &users, 1);
232 if (strcmp(inst->compat_mode, "cistron") == 0) {
239 DEBUG("[%s]:%d Cistron compatibility checks for entry %s ...",
240 filename, entry->lineno, entry->name);
244 * Look for improper use of '=' in the
245 * check items. They should be using
246 * '==' for on-the-wire RADIUS attributes,
247 * and probably ':=' for server
248 * configuration items.
250 for (vp = entry->check; vp != NULL; vp = vp->next) {
252 * Ignore attributes which are set
255 if (vp->operator != T_OP_EQ)
260 * If it's a vendor attribute,
261 * or it's a wire protocol,
262 * ensure it has '=='.
264 if (((vp->attribute & ~0xffff) != 0) ||
265 (vp->attribute < 0x100)) {
267 DEBUG("[%s]:%d WARNING! Changing '%s =' to '%s =='\n\tfor comparing RADIUS attribute in check item list for user %s",
268 filename, entry->lineno, vp->name, vp->name, entry->name);
270 DEBUG("\tChanging '%s =' to '%s =='",
273 vp->operator = T_OP_CMP_EQ;
278 * Cistron Compatibility mode.
280 * Re-write selected attributes
281 * to be '+=', instead of '='.
283 * All others get set to '=='
287 * Non-wire attributes become +=
289 * On the write attributes
292 if ((vp->attribute >= 0x100) &&
293 (vp->attribute <= 0xffff) &&
294 (vp->attribute != PW_HINT) &&
295 (vp->attribute != PW_HUNTGROUP_NAME)) {
296 DEBUG("\tChanging '%s =' to '%s +='",
298 vp->operator = T_OP_ADD;
300 DEBUG("\tChanging '%s =' to '%s =='",
302 vp->operator = T_OP_CMP_EQ;
306 } /* end of loop over check items */
310 * Look for server configuration items
313 * It's a common enough mistake, that it's
316 for (vp = entry->reply; vp != NULL; vp = vp->next) {
318 * If it's NOT a vendor attribute,
319 * and it's NOT a wire protocol
320 * and we ignore Fall-Through,
321 * then bitch about it, giving a
322 * good warning message.
324 if (!(vp->attribute & ~0xffff) &&
325 (vp->attribute > 0xff) &&
326 (vp->attribute > 1000)) {
327 log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
328 "\tfound in reply item list for user \"%s\".\n"
329 "\tThis attribute MUST go on the first line"
330 " with the other check items",
331 filename, entry->lineno, vp->name,
337 * Ok, we've done all the same BS as
338 * rlm_users, so here we tear apart the
339 * linked list, and store our users in
340 * the hashtable we've built instead
343 /* Save what was next */
347 /* Save the DEFAULT entry specially */
348 if(strcmp(entry->name, "DEFAULT")==0) {
350 /* Save this as the last default we've seen */
354 /* put it at the end of the list */
356 for(cur=defaults; cur->next; cur=cur->next);
361 defaults->next = NULL;
367 /* Hash the username */
368 hashindex = fastuser_hash(entry->name, inst->hashsize);
370 /* Store the last default before this entry */
371 entry->lastdefault = lastdefault;
373 /* Store user in the hash */
374 fastuser_store(pair_list, entry, hashindex);
377 /* Restore entry to next pair_list */
380 } /* while(entry) loop */
382 if(!isacctfile && (default_list)) {
383 *default_list = defaults;
384 radlog(L_INFO, "rlm_fastusers: Loaded %d users and %d defaults",
385 numusers, numdefaults);
393 /* Hashes the username sent to it and returns index into hashtable */
394 int fastuser_hash(const char *s, int hashtablesize) {
395 unsigned int hash = 0;
398 hash = hash * 7907 + (unsigned char)*s++;
401 return (hash % hashtablesize);
404 /* Stores the username sent into the hashtable */
405 static int fastuser_store(PAIR_LIST **hashtable, PAIR_LIST *new, int idx) {
408 cur = hashtable[idx];
409 /* store new record at end of list */
411 while (cur->next != NULL)
416 new->next = hashtable[idx];
417 hashtable[idx] = new;
423 * Looks up user in hashtable. If user can't be found, returns 0.
424 * Otherwise returns a pointer to the structure for the user
426 static PAIR_LIST *fastuser_find(REQUEST *request, PAIR_LIST *user,
427 const char *username)
433 * Now we have to make sure it's the right user by
434 * comparing the check pairs
436 while((cur) && (!userfound)) {
437 if((strcmp(cur->name, username)==0) &&
438 paircompare(request, request->packet->vps, cur->check, &request->reply->vps) == 0) {
440 DEBUG2(" fastusers: Matched %s at %d", cur->name, cur->lineno);
450 return (PAIR_LIST *)0;
454 * Generate and log statistics about our hash table
456 static void fastuser_tablestats(PAIR_LIST **hashtable, int size) {
462 memset(countarray, 0, sizeof(countarray));
464 for(i=0; i<size; i++) {
466 for(cur=hashtable[i]; cur; cur=cur->next) {
478 radlog(L_INFO, "rlm_fastusers: Hash buckets with %d users: %d",
483 radlog(L_INFO, "rlm_fastusers: Hash buckets with more than 256: %d",
489 * (Re-)read the "users" file into memory.
491 static int fastuser_instantiate(CONF_SECTION *conf, void **instance)
493 struct fastuser_instance *inst=0;
495 inst = rad_malloc(sizeof *inst);
498 memset(inst, 0, sizeof(*inst));
500 if (cf_section_parse(conf, inst, module_config) < 0) {
505 inst->next_reload = time(NULL) + inst->hash_reload;
506 inst->hashtable = NULL;
507 inst->lastusersload = 0;
508 inst->lastacctusersload = 0;
509 if(fastuser_buildhash(inst) < 0) {
510 radlog(L_ERR, "rlm_fastusers: error building user hash. aborting");
515 * Need code here to read acct_users file
523 * Find the named user in the database. Create the
524 * set of attribute-value pairs to check and reply with
525 * for this user from the database. The main code only
526 * needs to check the password, the rest is done here.
528 static int fastuser_authorize(void *instance, REQUEST *request)
531 VALUE_PAIR *namepair;
532 VALUE_PAIR *check_tmp;
533 VALUE_PAIR *reply_tmp;
535 PAIR_LIST *curdefault;
540 struct fastuser_instance *inst = instance;
543 * Do we need to reload the cache?
544 * Really we should spawn a thread to do this
546 if((inst->hash_reload) && (request->timestamp > inst->next_reload)) {
547 inst->next_reload = request->timestamp + inst->hash_reload;
548 radlog(L_INFO, "rlm_fastusers: Reloading fastusers hash");
549 if(fastuser_buildhash(inst) < 0) {
550 radlog(L_ERR, "rlm_fastusers: error building user hash. aborting");
551 return RLM_MODULE_FAIL;
556 * Grab the canonical user name.
558 namepair = request->username;
559 name = namepair ? (char *) namepair->vp_strvalue : "NONE";
562 * Find the entry for the user.
564 hashidx = fastuser_hash(name, inst->hashsize);
565 user = inst->hashtable[hashidx];
566 if((user=fastuser_find(request, user, name))!=NULL) {
571 * If there's no lastdefault and we
572 * don't fallthrough, just copy the
573 * pairs for this user and return
575 if((user) && (userfound) && (user->lastdefault == NULL)) {
576 DEBUG2("rlm_fastusers: user found before DEFAULT");
578 check_tmp = paircopy(user->check);
579 pairmove(&request->config_items, &check_tmp);
580 pairfree(&check_tmp);
582 reply_tmp = paircopy(user->reply);
583 pairmove(&request->reply->vps, &reply_tmp);
584 pairfree(&reply_tmp);
586 if(!fallthrough(user->reply)) {
587 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
588 return(rad_check_return(user->check));
591 user=fastuser_find(request, user, name);
596 * When we get here, we've either found
597 * the user or not, but to preserve order
598 * we start at the top of the default
599 * list and work our way thru
600 * When we get to the user's 'lastdefault'
601 * we check to see if we should stop
604 DEBUG2("rlm_fastusers: checking defaults");
606 curdefault = inst->defaults;
608 if(paircompare(request, request->packet->vps, curdefault->check,
609 &request->reply->vps) == 0) {
610 DEBUG2(" fastusers: Matched %s at %d",
611 curdefault->name, curdefault->lineno);
614 check_tmp = paircopy(curdefault->check);
615 pairmove(&request->config_items, &check_tmp);
616 pairfree(&check_tmp);
618 reply_tmp = paircopy(curdefault->reply);
619 pairmove(&request->reply->vps, &reply_tmp);
620 pairfree(&reply_tmp);
623 * There's no fallthru on this default which
624 * is *before* we find the user in the file,
625 * so we know it's safe to quit here
627 if (!fallthrough(curdefault->reply))
633 * If we found the user, we want to stop
634 * processing once we get to 'lastdefault'
635 * This way we can process this user's entry
636 * in the order it was found in the file
638 while((userfound && (user) && (curdefault == user->lastdefault))) {
639 DEBUG2(" fastusers: found lastdefault at line %d",
642 check_tmp = paircopy(user->check);
643 pairmove(&request->config_items, &check_tmp);
644 pairfree(&check_tmp);
646 reply_tmp = paircopy(user->reply);
647 pairmove(&request->reply->vps, &reply_tmp);
648 pairfree(&reply_tmp);
650 if(!fallthrough(user->reply)) {
651 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
652 return(rad_check_return(user->check));
656 * Find next occurence of THIS user in
660 user=fastuser_find(request, user, name);
663 curdefault = curdefault->next;
666 if(userfound || defaultfound) {
667 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
668 return(rad_check_return(request->config_items));
670 DEBUG2("rlm_fastusers: user not found");
671 return RLM_MODULE_NOTFOUND;
676 * Authentication - unused.
678 static int fastuser_authenticate(void *instance, REQUEST *request)
682 return RLM_MODULE_OK;
686 * Pre-Accounting - read the acct_users file for check_items and
687 * config_items. Reply items are Not Recommended(TM) in acct_users,
688 * except for Fallthrough, which should work
690 * This function is mostly a copy of file_authorize
692 static int fastuser_preacct(void *instance, REQUEST *request)
694 VALUE_PAIR *namepair;
696 VALUE_PAIR *request_pairs;
697 VALUE_PAIR **config_pairs;
698 VALUE_PAIR *reply_pairs = NULL;
699 VALUE_PAIR *check_tmp;
700 VALUE_PAIR *reply_tmp;
701 PAIR_LIST *pl = NULL;
703 struct fastuser_instance *inst = instance;
705 namepair = request->username;
706 name = namepair ? (char *) namepair->vp_strvalue : "NONE";
707 request_pairs = request->packet->vps;
708 config_pairs = &request->config_items;
711 * Find the entry for the user.
713 for (pl = inst->acctusers; pl; pl = pl->next) {
715 if (strcmp(name, pl->name) && strcmp(pl->name, "DEFAULT"))
718 if (paircompare(request, request_pairs, pl->check, &reply_pairs) == 0) {
719 DEBUG2(" acct_users: Matched %s at %d",
720 pl->name, pl->lineno);
722 check_tmp = paircopy(pl->check);
723 reply_tmp = paircopy(pl->reply);
724 pairmove(&reply_pairs, &reply_tmp);
725 pairmove(config_pairs, &check_tmp);
726 pairfree(&reply_tmp);
727 pairfree(&check_tmp); /* should be NULL */
731 if (!fallthrough(pl->reply))
737 * See if we succeeded.
740 return RLM_MODULE_NOOP; /* on to the next module */
743 * FIXME: log a warning if there are any reply items other than
746 pairfree(&reply_pairs); /* Don't need these */
748 return RLM_MODULE_OK;
754 static int fastuser_detach(void *instance)
756 struct fastuser_instance *inst = instance;
760 /* Free hash table */
761 for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
762 if(inst->hashtable[hashindex]) {
763 cur = inst->hashtable[hashindex];
768 free(inst->compat_mode);
769 free(inst->hashtable);
770 pairlist_free(&inst->defaults);
771 pairlist_free(&inst->acctusers);
772 free(inst->usersfile);
773 free(inst->acctusersfile);
779 * This function is unused
781 static int fastuser_accounting(void *instance UNUSED, REQUEST *request UNUSED)
784 * FIXME: should re rather return RLM_MODULE_NOOP here?
786 return RLM_MODULE_FAIL;
789 /* globally exported name */
790 module_t rlm_fastusers = {
793 0, /* type: reserved */
794 fastuser_instantiate, /* instantiation */
795 fastuser_detach, /* detach */
797 fastuser_authenticate, /* authentication */
798 fastuser_authorize, /* authorization */
799 fastuser_preacct, /* preaccounting */
800 fastuser_accounting, /* accounting */
801 NULL, /* checksimul */
802 NULL, /* pre-proxy */
803 NULL, /* post-proxy */