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>
26 #include "libradius.h"
28 #include <sys/socket.h>
44 struct fastuser_instance {
50 PAIR_LIST **hashtable;
59 time_t lastacctusersload;
62 /* Function declarations */
63 static int fallthrough(VALUE_PAIR *vp);
64 static int fastuser_buildhash(struct fastuser_instance *inst);
65 static int fastuser_getfile(struct fastuser_instance *inst, const char *filename,
66 PAIR_LIST **default_list, PAIR_LIST **pair_list,
68 static int fastuser_hash(const char *s, long hashtablesize);
69 static int fastuser_store(PAIR_LIST **hashtable, PAIR_LIST *entry, int idx);
70 static PAIR_LIST *fastuser_find(REQUEST *request, PAIR_LIST *user,
71 const char *username);
72 static void fastuser_tablestats(PAIR_LIST **hashtable, long size);
73 static int fastuser_passcheck(REQUEST *request, PAIR_LIST *user, const char *name);
75 static CONF_PARSER module_config[] = {
76 { "usersfile", PW_TYPE_STRING_PTR,
77 offsetof(struct fastuser_instance,usersfile), NULL, "${raddbdir}/users_fast" },
78 { "acctusersfile", PW_TYPE_STRING_PTR,
79 offsetof(struct fastuser_instance,acctusersfile), NULL, "${raddbdir}/acct_users" },
80 { "hashsize", PW_TYPE_INTEGER,
81 offsetof(struct fastuser_instance,hashsize), NULL, "100000" },
82 { "stats", PW_TYPE_BOOLEAN,
83 offsetof(struct fastuser_instance,stats), NULL, "no" },
84 { "compat", PW_TYPE_STRING_PTR,
85 offsetof(struct fastuser_instance,compat_mode), NULL, "cistron" },
86 { "hash_reload", PW_TYPE_INTEGER,
87 offsetof(struct fastuser_instance,hash_reload), NULL, "600" },
88 { NULL, -1, 0, NULL, NULL }
92 * See if a VALUE_PAIR list contains Fall-Through = Yes
94 static int fallthrough(VALUE_PAIR *vp)
97 tmp = pairfind(vp, PW_FALL_THROUGH);
98 return tmp ? tmp->lvalue : 0;
102 * returncheck - Check for Auth-Type = Reject and return appropriate
103 * module return code if it is found.
105 static int rad_check_return(VALUE_PAIR *list)
107 VALUE_PAIR *authtype;
110 * We check for Auth-Type = Reject here
113 authtype = pairfind(list, PW_AUTHTYPE);
114 if((authtype) && authtype->lvalue == PW_AUTHTYPE_REJECT) {
115 DEBUG2("rad_check_return: Auth-Type is Reject");
116 return RLM_MODULE_REJECT;
119 return RLM_MODULE_UPDATED;
122 static int fastuser_buildhash(struct fastuser_instance *inst) {
124 int rcode, hashindex;
125 PAIR_LIST **newhash=NULL, **oldhash=NULL;
126 PAIR_LIST *newdefaults=NULL, *newacctusers, *cur=NULL;
127 PAIR_LIST *olddefaults=NULL, *oldacctusers=NULL;
130 int reloadacctusers = 1;
133 * Allocate space for hash table here
135 memsize = sizeof(PAIR_LIST *) * inst->hashsize;
137 newhash = (PAIR_LIST **) rad_malloc(memsize);
139 memset((PAIR_LIST *)newhash, 0, memsize);
141 /* Check acct_users last modification time */
142 if ((stat(inst->acctusersfile, &statbuf) != -1)
143 && (statbuf.st_mtime <= inst->lastacctusersload)) {
144 DEBUG2("rlm_fastusers: File %s was unchanged. Not reloading.",
145 inst->acctusersfile);
150 /* Read acct_users */
151 rcode = fastuser_getfile(inst, inst->acctusersfile, NULL, &newacctusers, 1);
154 radlog(L_ERR|L_CONS, "rlm_fastusers: Errors reading %s", inst->usersfile);
158 /* Check users last modification time */
159 if ((stat(inst->usersfile, &statbuf) != -1)
160 && (statbuf.st_mtime <= inst->lastusersload)) {
161 DEBUG2("rlm_fastusers: File %s was unchanged. Not reloading.",
165 /* This was allocated earlier but will remain unused */
170 rcode = fastuser_getfile(inst, inst->usersfile, &newdefaults, newhash, 0);
173 radlog(L_ERR|L_CONS, "rlm_fastusers: Errors reading %s", inst->usersfile);
179 * We need to do this now so that users auths
180 * aren't blocked while we free the old table
183 inst->lastusersload = time(NULL);
184 oldhash = inst->hashtable;
185 inst->hashtable = newhash;
186 olddefaults = inst->defaults;
187 inst->defaults = newdefaults;
190 * When we get here, we assume the hash built properly.
191 * So we begin to tear down the old one
194 for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
195 if(oldhash[hashindex]) {
196 cur = oldhash[hashindex];
202 pairlist_free(&olddefaults);
204 if (reloadacctusers) {
205 inst->lastacctusersload = time(NULL);
206 oldacctusers = inst->acctusers;
207 inst->acctusers = newacctusers;
208 pairlist_free(&oldacctusers);
212 fastuser_tablestats(inst->hashtable, inst->hashsize);
217 static int fastuser_getfile(struct fastuser_instance *inst, const char *filename,
218 PAIR_LIST **default_list, PAIR_LIST **pair_list,
221 PAIR_LIST *users = NULL;
222 PAIR_LIST *entry=NULL, *next=NULL, *cur=NULL, *defaults=NULL, *lastdefault=NULL;
223 int compat_mode = FALSE;
226 long numdefaults = 0, numusers=0;
228 radlog(L_INFO, " fastusers: Reading %s", filename);
229 rcode = pairlist_read(filename, &users, 1);
234 if (strcmp(inst->compat_mode, "cistron") == 0) {
241 DEBUG("[%s]:%d Cistron compatibility checks for entry %s ...",
242 filename, entry->lineno, entry->name);
246 * Look for improper use of '=' in the
247 * check items. They should be using
248 * '==' for on-the-wire RADIUS attributes,
249 * and probably ':=' for server
250 * configuration items.
252 for (vp = entry->check; vp != NULL; vp = vp->next) {
254 * Ignore attributes which are set
257 if (vp->operator != T_OP_EQ)
262 * If it's a vendor attribute,
263 * or it's a wire protocol,
264 * ensure it has '=='.
266 if (((vp->attribute & ~0xffff) != 0) ||
267 (vp->attribute < 0x100)) {
269 DEBUG("[%s]:%d WARNING! Changing '%s =' to '%s =='\n\tfor comparing RADIUS attribute in check item list for user %s",
270 filename, entry->lineno, vp->name, vp->name, entry->name);
272 DEBUG("\tChanging '%s =' to '%s =='",
275 vp->operator = T_OP_CMP_EQ;
280 * Cistron Compatibility mode.
282 * Re-write selected attributes
283 * to be '+=', instead of '='.
285 * All others get set to '=='
289 * Non-wire attributes become +=
291 * On the write attributes
294 if ((vp->attribute >= 0x100) &&
295 (vp->attribute <= 0xffff) &&
296 (vp->attribute != PW_HINT) &&
297 (vp->attribute != PW_HUNTGROUP_NAME)) {
298 DEBUG("\tChanging '%s =' to '%s +='",
300 vp->operator = T_OP_ADD;
302 DEBUG("\tChanging '%s =' to '%s =='",
304 vp->operator = T_OP_CMP_EQ;
308 } /* end of loop over check items */
312 * Look for server configuration items
315 * It's a common enough mistake, that it's
318 for (vp = entry->reply; vp != NULL; vp = vp->next) {
320 * If it's NOT a vendor attribute,
321 * and it's NOT a wire protocol
322 * and we ignore Fall-Through,
323 * then bitch about it, giving a
324 * good warning message.
326 if (!(vp->attribute & ~0xffff) &&
327 (vp->attribute > 0xff) &&
328 (vp->attribute > 1000)) {
329 log_debug("[%s]:%d WARNING! Check item \"%s\"\n"
330 "\tfound in reply item list for user \"%s\".\n"
331 "\tThis attribute MUST go on the first line"
332 " with the other check items",
333 filename, entry->lineno, vp->name,
339 * Ok, we've done all the same BS as
340 * rlm_users, so here we tear apart the
341 * linked list, and store our users in
342 * the hashtable we've built instead
345 /* Save what was next */
349 /* Save the DEFAULT entry specially */
350 if(strcmp(entry->name, "DEFAULT")==0) {
352 /* Save this as the last default we've seen */
356 /* put it at the end of the list */
358 for(cur=defaults; cur->next; cur=cur->next);
363 defaults->next = NULL;
369 /* Hash the username */
370 hashindex = fastuser_hash(entry->name, inst->hashsize);
372 /* Store the last default before this entry */
373 entry->lastdefault = lastdefault;
375 /* Store user in the hash */
376 fastuser_store(pair_list, entry, hashindex);
379 /* Restore entry to next pair_list */
382 } /* while(entry) loop */
384 if(!isacctfile && (default_list)) {
385 *default_list = defaults;
386 radlog(L_INFO, "rlm_fastusers: Loaded %ld users and %ld defaults",
387 numusers, numdefaults);
395 /* Hashes the username sent to it and returns index into hashtable */
396 int fastuser_hash(const char *s, long hashtablesize) {
397 unsigned long hash = 0;
400 hash = hash * 7907 + (unsigned char)*s++;
403 return (hash % hashtablesize);
406 /* Stores the username sent into the hashtable */
407 static int fastuser_store(PAIR_LIST **hashtable, PAIR_LIST *new, int idx) {
410 cur = hashtable[idx];
411 /* store new record at end of list */
413 while (cur->next != NULL)
418 new->next = hashtable[idx];
419 hashtable[idx] = new;
425 * Looks up user in hashtable. If user can't be found, returns 0.
426 * Otherwise returns a pointer to the structure for the user
428 static PAIR_LIST *fastuser_find(REQUEST *request, PAIR_LIST *user,
429 const char *username)
435 * Now we have to make sure it's the right user by
436 * comparing the check pairs
438 while((cur) && (!userfound)) {
439 if((strcmp(cur->name, username)==0) &&
440 paircmp(request, request->packet->vps, cur->check, &request->reply->vps) == 0) {
442 * Usercollide means we have to compare check pairs
445 if(mainconfig.do_usercollide) {
446 if((userfound = fastuser_passcheck(request, cur, username))==0) {
452 DEBUG2(" fastusers: Matched %s at %d", cur->name, cur->lineno);
463 return (PAIR_LIST *)0;
467 * Generate and log statistics about our hash table
469 static void fastuser_tablestats(PAIR_LIST **hashtable, long size) {
475 memset(countarray, 0, sizeof(countarray));
477 for(i=0; i<size; i++) {
479 for(cur=hashtable[i]; cur; cur=cur->next) {
491 radlog(L_INFO, "rlm_fastusers: Hash buckets with %d users: %d",
496 radlog(L_INFO, "rlm_fastusers: Hash buckets with more than 256: %d",
501 static int fastuser_passcheck(REQUEST *request, PAIR_LIST *user,
502 const char *name UNUSED)
505 VALUE_PAIR *check_save;
508 * We check for REJECT specially here or a REJECT
509 * user will never match
511 check_save = pairfind(user->check, PW_AUTHTYPE);
512 if((check_save) && check_save->lvalue == PW_AUTHTYPE_REJECT) {
513 DEBUG2(" fastusers(uc): User '%s' line %d is Auth-Type Reject, but usercollide match",
514 user->name, user->lineno);
518 /* Save the orginal config items */
519 check_save = request->config_items;
520 request->config_items = NULL;
522 DEBUG2(" fastusers(uc): Checking %s at %d", user->name, user->lineno);
524 /* Copy this users check pairs to the request */
525 request->config_items = paircopy(user->check);
527 /* Check the req to see if we matched */
528 if(rad_check_password(request)==0) {
529 DEBUG2(" fastusers(uc): Matched %s at %d", user->name, user->lineno);
533 /* Restore check items */
534 pairfree(&request->config_items);
535 request->config_items = check_save;
541 * (Re-)read the "users" file into memory.
543 static int fastuser_instantiate(CONF_SECTION *conf, void **instance)
545 struct fastuser_instance *inst=0;
547 inst = rad_malloc(sizeof *inst);
550 memset(inst, 0, sizeof(*inst));
552 if (cf_section_parse(conf, inst, module_config) < 0) {
557 inst->next_reload = time(NULL) + inst->hash_reload;
558 inst->hashtable = NULL;
559 inst->lastusersload = 0;
560 inst->lastacctusersload = 0;
561 if(fastuser_buildhash(inst) < 0) {
562 radlog(L_ERR, "rlm_fastusers: error building user hash. aborting");
567 * Need code here to read acct_users file
575 * Find the named user in the database. Create the
576 * set of attribute-value pairs to check and reply with
577 * for this user from the database. The main code only
578 * needs to check the password, the rest is done here.
580 static int fastuser_authorize(void *instance, REQUEST *request)
583 VALUE_PAIR *namepair;
584 VALUE_PAIR *check_tmp;
585 VALUE_PAIR *reply_tmp;
587 PAIR_LIST *curdefault;
592 struct fastuser_instance *inst = instance;
595 * Do we need to reload the cache?
596 * Really we should spawn a thread to do this
598 if((inst->hash_reload) && (request->timestamp > inst->next_reload)) {
599 inst->next_reload = request->timestamp + inst->hash_reload;
600 radlog(L_INFO, "rlm_fastusers: Reloading fastusers hash");
601 if(fastuser_buildhash(inst) < 0) {
602 radlog(L_ERR, "rlm_fastusers: error building user hash. aborting");
608 * Grab the canonical user name.
610 namepair = request->username;
611 name = namepair ? (char *) namepair->strvalue : "NONE";
614 * Find the entry for the user.
616 hashidx = fastuser_hash(name, inst->hashsize);
617 user = inst->hashtable[hashidx];
618 if((user=fastuser_find(request, user, name))!=NULL) {
623 * If there's no lastdefault and we
624 * don't fallthrough, just copy the
625 * pairs for this user and return
627 if((user) && (userfound) && (user->lastdefault == NULL)) {
628 DEBUG2("rlm_fastusers: user found before DEFAULT");
630 check_tmp = paircopy(user->check);
631 pairmove(&request->config_items, &check_tmp);
632 pairfree(&check_tmp);
634 reply_tmp = paircopy(user->reply);
635 pairmove(&request->reply->vps, &reply_tmp);
636 pairfree(&reply_tmp);
638 if(!fallthrough(user->reply)) {
639 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
640 return(rad_check_return(user->check));
643 user=fastuser_find(request, user, name);
648 * When we get here, we've either found
649 * the user or not, but to preserve order
650 * we start at the top of the default
651 * list and work our way thru
652 * When we get to the user's 'lastdefault'
653 * we check to see if we should stop
656 DEBUG2("rlm_fastusers: checking defaults");
658 curdefault = inst->defaults;
660 if(paircmp(request, request->packet->vps, curdefault->check,
661 &request->reply->vps) == 0) {
662 DEBUG2(" fastusers: Matched %s at %d",
663 curdefault->name, curdefault->lineno);
666 check_tmp = paircopy(curdefault->check);
667 pairmove(&request->config_items, &check_tmp);
668 pairfree(&check_tmp);
670 reply_tmp = paircopy(curdefault->reply);
671 pairmove(&request->reply->vps, &reply_tmp);
672 pairfree(&reply_tmp);
675 * There's no fallthru on this default which
676 * is *before* we find the user in the file,
677 * so we know it's safe to quit here
679 if (!fallthrough(curdefault->reply))
685 * If we found the user, we want to stop
686 * processing once we get to 'lastdefault'
687 * This way we can process this user's entry
688 * in the order it was found in the file
690 while((userfound && (user) && (curdefault == user->lastdefault))) {
691 DEBUG2(" fastusers: found lastdefault at line %d",
694 check_tmp = paircopy(user->check);
695 pairmove(&request->config_items, &check_tmp);
696 pairfree(&check_tmp);
698 reply_tmp = paircopy(user->reply);
699 pairmove(&request->reply->vps, &reply_tmp);
700 pairfree(&reply_tmp);
702 if(!fallthrough(user->reply)) {
703 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
704 return(rad_check_return(user->check));
708 * Find next occurence of THIS user in
712 user=fastuser_find(request, user, name);
715 curdefault = curdefault->next;
718 if(userfound || defaultfound) {
719 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
720 return(rad_check_return(request->config_items));
722 DEBUG2("rlm_fastusers: user not found");
723 return RLM_MODULE_NOTFOUND;
728 * Authentication - unused.
730 static int fastuser_authenticate(void *instance, REQUEST *request)
734 return RLM_MODULE_OK;
738 * Pre-Accounting - read the acct_users file for check_items and
739 * config_items. Reply items are Not Recommended(TM) in acct_users,
740 * except for Fallthrough, which should work
742 * This function is mostly a copy of file_authorize
744 static int fastuser_preacct(void *instance, REQUEST *request)
746 VALUE_PAIR *namepair;
748 VALUE_PAIR *request_pairs;
749 VALUE_PAIR **config_pairs;
750 VALUE_PAIR *reply_pairs = NULL;
751 VALUE_PAIR *check_tmp;
752 VALUE_PAIR *reply_tmp;
753 PAIR_LIST *pl = NULL;
755 struct fastuser_instance *inst = instance;
757 namepair = request->username;
758 name = namepair ? (char *) namepair->strvalue : "NONE";
759 request_pairs = request->packet->vps;
760 config_pairs = &request->config_items;
763 * Find the entry for the user.
765 for (pl = inst->acctusers; pl; pl = pl->next) {
767 if (strcmp(name, pl->name) && strcmp(pl->name, "DEFAULT"))
770 if (paircmp(request, request_pairs, pl->check, &reply_pairs) == 0) {
771 DEBUG2(" acct_users: Matched %s at %d",
772 pl->name, pl->lineno);
774 check_tmp = paircopy(pl->check);
775 reply_tmp = paircopy(pl->reply);
776 pairmove(&reply_pairs, &reply_tmp);
777 pairmove(config_pairs, &check_tmp);
778 pairfree(&reply_tmp);
779 pairfree(&check_tmp); /* should be NULL */
783 if (!fallthrough(pl->reply))
789 * See if we succeeded.
792 return RLM_MODULE_NOOP; /* on to the next module */
795 * FIXME: log a warning if there are any reply items other than
798 pairfree(&reply_pairs); /* Don't need these */
800 return RLM_MODULE_OK;
806 static int fastuser_detach(void *instance)
808 struct fastuser_instance *inst = instance;
812 /* Free hash table */
813 for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
814 if(inst->hashtable[hashindex]) {
815 cur = inst->hashtable[hashindex];
820 free(inst->compat_mode);
821 free(inst->hashtable);
822 pairlist_free(&inst->defaults);
823 pairlist_free(&inst->acctusers);
824 free(inst->usersfile);
825 free(inst->acctusersfile);
831 * This function is unused
833 static int fastuser_accounting(void *instance UNUSED, REQUEST *request UNUSED)
835 return RLM_MODULE_FAIL;
838 /* globally exported name */
839 module_t rlm_fastusers = {
841 0, /* type: reserved */
842 NULL, /* initialization */
843 fastuser_instantiate, /* instantiation */
845 fastuser_authenticate, /* authentication */
846 fastuser_authorize, /* authorization */
847 fastuser_preacct, /* preaccounting */
848 fastuser_accounting, /* accounting */
849 NULL, /* checksimul */
850 NULL, /* pre-proxy */
851 NULL, /* post-proxy */
854 fastuser_detach, /* detach */