Remove crap function from server core, and put it into
[freeradius.git] / src / modules / rlm_fastusers / rlm_fastusers.c
1 /*
2  * rlm_fastusers.c      authorization: Find a user in the hashed "users" file.
3  *                      accounting:    Do nothing.  Auth module only.
4  *
5  * Version:     $Id$
6  *
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.
11  *
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.
16  *
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
20  *
21  * Copyright 2000  The FreeRADIUS server project
22  * Copyright 2000  Jeff Carneal <jeff@apex.net>
23  */
24
25 #include        "autoconf.h"
26 #include        "libradius.h"
27
28 #include        <sys/socket.h>
29 #include        <sys/time.h>
30 #include        <sys/stat.h>
31
32 #include        <stdio.h>
33 #include        <stdlib.h>
34 #include        <string.h>
35 #include        <pwd.h>
36 #include        <grp.h>
37 #include        <ctype.h>
38 #include        <fcntl.h>
39 #include        <limits.h>
40
41 #include        "radiusd.h"
42 #include        "modules.h"
43
44 struct fastuser_instance {
45         char *compat_mode;
46         int hash_reload;
47
48         /* hash table */
49         long hashsize;
50         PAIR_LIST **hashtable;
51         PAIR_LIST *defaults;
52         PAIR_LIST *acctusers;
53         int stats;
54
55         char *usersfile;
56         char *acctusersfile;
57         time_t next_reload;
58         time_t lastusersload;
59         time_t lastacctusersload;
60 };
61
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, 
67                                                                                                                 int isacctfile);
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);
74
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 }
89 };
90
91 /*
92  * See if a VALUE_PAIR list contains Fall-Through = Yes
93  */
94 static int fallthrough(VALUE_PAIR *vp)
95 {
96         VALUE_PAIR *tmp;
97         tmp = pairfind(vp, PW_FALL_THROUGH);
98         return tmp ? tmp->lvalue : 0;
99 }
100
101 /*
102  *       returncheck - Check for Auth-Type = Reject and return appropriate
103  *                     module return code if it is found.
104  */
105 static int rad_check_return(VALUE_PAIR *list)
106 {
107       VALUE_PAIR      *authtype;
108
109       /* 
110        * We check for Auth-Type = Reject here
111        */
112
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;
117       }
118
119       return RLM_MODULE_UPDATED;
120 }
121
122 static int fastuser_buildhash(struct fastuser_instance *inst) {
123         long memsize=0;
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;
128         struct stat statbuf;
129         int reloadusers = 1;
130         int reloadacctusers = 1;
131
132         /* 
133          * Allocate space for hash table here
134          */
135         memsize = sizeof(PAIR_LIST *) * inst->hashsize;
136
137         newhash = (PAIR_LIST **) rad_malloc(memsize);
138
139         memset((PAIR_LIST *)newhash, 0, memsize);
140
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);
146                 reloadacctusers = 0;
147                 rcode = 0;
148         }
149         else
150         /* Read acct_users */
151         rcode = fastuser_getfile(inst, inst->acctusersfile, NULL, &newacctusers, 1);
152
153         if (rcode != 0) {
154                 radlog(L_ERR|L_CONS, "rlm_fastusers:  Errors reading %s", inst->usersfile);
155                 return -1;
156         }
157
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.",
162                         inst->usersfile);
163                 reloadusers = 0;
164                 rcode = 0;
165                 /* This was allocated earlier but will remain unused */
166                 free(newhash);
167         }
168         else
169         /* Read users */
170         rcode = fastuser_getfile(inst, inst->usersfile, &newdefaults, newhash, 0);
171
172         if (rcode != 0) {
173                 radlog(L_ERR|L_CONS, "rlm_fastusers:  Errors reading %s", inst->usersfile);
174                 return -1;
175         }
176
177         if (reloadusers) {
178                 /*
179                  * We need to do this now so that users auths
180                  * aren't blocked while we free the old table
181                  * below
182                  */
183                 inst->lastusersload = time(NULL);
184                 oldhash = inst->hashtable;
185                 inst->hashtable = newhash;
186                 olddefaults = inst->defaults;
187                 inst->defaults = newdefaults;
188
189                 /*
190                  * When we get here, we assume the hash built properly.
191                  * So we begin to tear down the old one
192                  */
193                 if (oldhash) {
194                         for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
195                                 if(oldhash[hashindex]) {
196                                         cur = oldhash[hashindex];
197                                         pairlist_free(&cur);
198                                 }
199                         } 
200                         free(oldhash);
201                 }
202                 pairlist_free(&olddefaults);
203         }
204         if (reloadacctusers) {
205                 inst->lastacctusersload = time(NULL);
206                 oldacctusers = inst->acctusers;
207                 inst->acctusers = newacctusers;
208                 pairlist_free(&oldacctusers);
209         }
210
211         if(inst->stats) 
212                 fastuser_tablestats(inst->hashtable, inst->hashsize);
213
214         return 0;       
215 }
216
217 static int fastuser_getfile(struct fastuser_instance *inst, const char *filename, 
218                                                                                                                 PAIR_LIST **default_list, PAIR_LIST **pair_list, 
219                                                                                                                 int isacctfile) {
220         int rcode;
221         PAIR_LIST *users = NULL;
222         PAIR_LIST *entry=NULL, *next=NULL, *cur=NULL, *defaults=NULL, *lastdefault=NULL;
223         int compat_mode = FALSE;
224         VALUE_PAIR *vp=NULL;
225         int hashindex = 0;
226         long numdefaults = 0, numusers=0;
227
228         radlog(L_INFO, " fastusers:  Reading %s", filename);
229         rcode = pairlist_read(filename, &users, 1);
230         if (rcode < 0) {
231                 return -1;
232         }
233
234         if (strcmp(inst->compat_mode, "cistron") == 0) {
235                 compat_mode = TRUE;
236         }
237         
238         entry = users;
239         while (entry) {
240                 if (compat_mode) {
241                         DEBUG("[%s]:%d Cistron compatibility checks for entry %s ...",
242                                 filename, entry->lineno, entry->name);
243                 }
244
245                 /*
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.
251                  */
252                 for (vp = entry->check; vp != NULL; vp = vp->next) {
253                         /*
254                          *      Ignore attributes which are set
255                          *      properly.
256                          */
257                         if (vp->operator != T_OP_EQ) 
258                                 continue;
259                                 
260
261                         /*
262                          *      If it's a vendor attribute,
263                          *      or it's a wire protocol, 
264                          *      ensure it has '=='.
265                          */
266                         if (((vp->attribute & ~0xffff) != 0) ||
267                                 (vp->attribute < 0x100)) {
268                                 if (!compat_mode) {
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);
271                                 } else {
272                                         DEBUG("\tChanging '%s =' to '%s =='",
273                                                 vp->name, vp->name);
274                                 }
275                                 vp->operator = T_OP_CMP_EQ;
276                                 continue;
277                         }
278                                 
279                         /*
280                          *      Cistron Compatibility mode.
281                          *
282                          *      Re-write selected attributes
283                          *      to be '+=', instead of '='.
284                          *
285                          *      All others get set to '=='
286                          */
287                         if (compat_mode) {
288                                 /*
289                                  *      Non-wire attributes become +=
290                                  *
291                                  *      On the write attributes
292                                  *      become ==
293                                  */
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 +='",
299                                                 vp->name, vp->name);
300                                                 vp->operator = T_OP_ADD;
301                                 } else {
302                                         DEBUG("\tChanging '%s =' to '%s =='",
303                                                 vp->name, vp->name);
304                                         vp->operator = T_OP_CMP_EQ;
305                                 }
306                         }
307                                 
308                 } /* end of loop over check items */
309                 
310                 
311                 /*
312                  *      Look for server configuration items
313                  *      in the reply list.
314                  *
315                  *      It's a common enough mistake, that it's
316                  *      worth doing.
317                  */
318                 for (vp = entry->reply; vp != NULL; vp = vp->next) {
319                         /*
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.
325                          */
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,
334                                         entry->name);
335                         }
336                 }
337
338                 /*
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
343                  */
344
345                 /* Save what was next */
346                 next = entry->next;
347
348                 if(!isacctfile) {
349                         /* Save the DEFAULT entry specially */
350                         if(strcmp(entry->name, "DEFAULT")==0) {
351                                 
352                                 /* Save this as the last default we've seen */
353                                 lastdefault = entry;
354                                 numdefaults++;
355         
356                                 /* put it at the end of the list */
357                                 if(defaults) {
358                                         for(cur=defaults; cur->next; cur=cur->next);
359                                         cur->next = entry;
360                                         entry->next = NULL;
361                                 } else {
362                                         defaults = entry;
363                                         defaults->next = NULL; 
364                                 }
365         
366                         } else {
367                                 numusers++;
368         
369                                 /* Hash the username */
370                                 hashindex = fastuser_hash(entry->name, inst->hashsize);
371         
372                                 /* Store the last default before this entry */
373                                 entry->lastdefault = lastdefault;
374         
375                                 /* Store user in the hash */
376                                 fastuser_store(pair_list, entry, hashindex);
377                         }
378                 }
379                 /* Restore entry to next pair_list */
380                 entry = next;
381
382         } /* while(entry) loop */
383
384         if(!isacctfile && (default_list)) {
385                 *default_list = defaults;
386                 radlog(L_INFO, "rlm_fastusers:  Loaded %ld users and %ld defaults",
387                                         numusers, numdefaults);
388         } else {
389                 *pair_list = users;
390         }
391
392         return 0;
393 }
394
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;
398
399         while (*s != '\0') {
400                 hash = hash * 7907 + (unsigned char)*s++;
401         }
402
403         return (hash % hashtablesize);
404 }
405
406 /* Stores the username sent into the hashtable */
407 static int fastuser_store(PAIR_LIST **hashtable, PAIR_LIST *new, int idx) {
408         PAIR_LIST *cur;
409
410         cur = hashtable[idx];
411         /* store new record at end of list */
412         if(cur) {
413                 while (cur->next != NULL) 
414                         cur=cur->next;
415                 cur->next = new;
416                 new->next = NULL;
417         } else {
418                 new->next = hashtable[idx];
419                 hashtable[idx] = new;
420         }
421         return 1;
422 }
423
424 /*
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
427  */
428 static PAIR_LIST *fastuser_find(REQUEST *request, PAIR_LIST *user, 
429                                             const char *username)
430 {
431         PAIR_LIST *cur=user;
432         int userfound = 0;
433
434         /*
435          * Now we have to make sure it's the right user by
436          * comparing the check pairs
437          */
438         while((cur) && (!userfound)) {
439                 if((strcmp(cur->name, username)==0) &&
440                                 paircmp(request, request->packet->vps, cur->check, &request->reply->vps) == 0) {
441                         /*
442                          * Usercollide means we have to compare check pairs
443                          * AND the password
444                          */
445                         if(mainconfig.do_usercollide) {
446                                 if((userfound = fastuser_passcheck(request, cur, username))==0) {
447                                         cur = cur->next;
448                                 } 
449
450                         } else {
451                                 userfound = 1;
452                                 DEBUG2("  fastusers: Matched %s at %d", cur->name, cur->lineno);
453                         }
454                 } else {
455                         cur = cur->next;
456                 }
457         }
458
459         if(cur) {
460                 return cur;
461         }
462
463         return (PAIR_LIST *)0;
464 }
465
466 /*
467  * Generate and log statistics about our hash table
468  */
469 static void fastuser_tablestats(PAIR_LIST **hashtable, long size) {
470         int i, count;
471         int countarray[256];
472         int toomany=0;
473         PAIR_LIST *cur;
474
475         memset(countarray, 0, sizeof(countarray));
476
477         for(i=0; i<size; i++) {
478                 count = 0;
479                 for(cur=hashtable[i]; cur; cur=cur->next) {
480                         count++;
481                 }
482                 if(count<256) {
483                         countarray[count]++;
484                 } else {
485                         toomany++;
486                 }
487         }
488
489         for(i=0; i<256; i++) 
490                 if(countarray[i]) {
491                         radlog(L_INFO, "rlm_fastusers:  Hash buckets with %d users:  %d",
492                                                 i, countarray[i]);
493                 }
494
495         if(toomany) {
496                 radlog(L_INFO, "rlm_fastusers:  Hash buckets with more than 256:  %d", 
497                                         toomany);
498         }
499 }
500
501 static int fastuser_passcheck(REQUEST *request, PAIR_LIST *user, const char *name)
502 {
503         int found=0;
504         VALUE_PAIR      *check_save;
505
506         /* 
507          * We check for REJECT specially here or a REJECT
508          * user will never match
509          */
510         check_save = pairfind(user->check, PW_AUTHTYPE);
511         if((check_save) && check_save->lvalue == PW_AUTHTYPE_REJECT)  {
512                 DEBUG2("  fastusers(uc):  User '%s' line %d is Auth-Type Reject, but usercollide match", 
513                                         user->name, user->lineno);
514                 return 1;
515         }
516
517         /* Save the orginal config items */
518         check_save = request->config_items;
519         request->config_items = NULL;
520         
521         DEBUG2("  fastusers(uc): Checking %s at %d", user->name, user->lineno);
522
523         /* Copy this users check pairs to the request */
524         request->config_items = paircopy(user->check);
525
526         /* Check the req to see if we matched */
527         if(rad_check_password(request)==0) {
528                 DEBUG2("  fastusers(uc): Matched %s at %d", user->name, user->lineno);
529                 found = 1;
530         }
531
532         /* Restore check items */
533         pairfree(&request->config_items); 
534         request->config_items = check_save;
535
536         return found;
537 }
538
539 /*
540  *      (Re-)read the "users" file into memory.
541  */
542 static int fastuser_instantiate(CONF_SECTION *conf, void **instance)
543 {
544         struct fastuser_instance *inst=0;
545
546         inst = rad_malloc(sizeof *inst);
547
548         memset(inst, 0, sizeof(*inst));
549
550         if (cf_section_parse(conf, inst, module_config) < 0) {
551                 free(inst);
552                 return -1;
553         }
554
555         inst->next_reload = time(NULL) + inst->hash_reload;
556         inst->hashtable = NULL;
557         inst->lastusersload = 0;
558         inst->lastacctusersload = 0;
559         if(fastuser_buildhash(inst) < 0) {
560                 radlog(L_ERR, "rlm_fastusers:  error building user hash.  aborting");
561                 return -1;
562         }
563
564         /*
565          * Need code here to read acct_users file
566          */
567
568         *instance = inst;
569         return 0;
570 }
571
572 /*
573  *      Find the named user in the database.  Create the
574  *      set of attribute-value pairs to check and reply with
575  *      for this user from the database. The main code only
576  *      needs to check the password, the rest is done here.
577  */
578 static int fastuser_authorize(void *instance, REQUEST *request)
579 {
580
581         VALUE_PAIR      *namepair;
582         VALUE_PAIR      *check_tmp;
583         VALUE_PAIR      *reply_tmp;
584         PAIR_LIST               *user;
585         PAIR_LIST               *curdefault;
586         const char      *name;
587         int                     userfound=0;
588         int                     defaultfound=0;
589         int                     hashidx=0;
590         struct fastuser_instance *inst = instance;
591
592         /*
593          * Do we need to reload the cache?
594          * Really we should spawn a thread to do this
595          */
596         if((inst->hash_reload) && (request->timestamp > inst->next_reload)) {
597                 inst->next_reload = request->timestamp + inst->hash_reload;
598                 radlog(L_INFO, "rlm_fastusers:  Reloading fastusers hash");
599                 if(fastuser_buildhash(inst) < 0) {
600                         radlog(L_ERR, "rlm_fastusers:  error building user hash.  aborting");
601                         exit(1);
602                 }
603         }
604
605         /*
606          *      Grab the canonical user name.
607          */
608         namepair = request->username;
609         name = namepair ? (char *) namepair->strvalue : "NONE";
610
611         /*
612          *      Find the entry for the user.
613          */
614         hashidx = fastuser_hash(name, inst->hashsize);
615         user = inst->hashtable[hashidx];
616         if((user=fastuser_find(request, user, name))!=NULL) {
617                 userfound = 1;          
618         }
619
620         /* 
621          * If there's no lastdefault and we
622          * don't fallthrough, just copy the
623          * pairs for this user and return
624          */
625         if((user) && (userfound) && (user->lastdefault == NULL)) {
626                 DEBUG2("rlm_fastusers:  user found before DEFAULT");
627
628                 check_tmp = paircopy(user->check);
629                 pairmove(&request->config_items, &check_tmp);
630                 pairfree(&check_tmp); 
631
632                 reply_tmp = paircopy(user->reply);
633                 pairmove(&request->reply->vps, &reply_tmp);
634                 pairfree(&reply_tmp);
635
636                 if(!fallthrough(user->reply)) {
637                         pairdelete(&request->reply->vps, PW_FALL_THROUGH);
638                         return(rad_check_return(user->check));
639                 } else {
640                         user=user->next;
641                         user=fastuser_find(request, user, name);
642                 }
643         }
644
645         /* 
646          * When we get here, we've either found 
647          * the user or not, but to preserve order
648          * we start at the top of the default
649          * list and work our way thru
650          * When we get to the user's 'lastdefault'
651          * we check to see if we should stop
652          * and return
653          */
654         DEBUG2("rlm_fastusers:  checking defaults");
655                         
656         curdefault = inst->defaults;
657         while(curdefault) {
658                 if(paircmp(request, request->packet->vps, curdefault->check, 
659                                                         &request->reply->vps) == 0) {
660                         DEBUG2("  fastusers: Matched %s at %d", 
661                                                         curdefault->name, curdefault->lineno);
662                         defaultfound = 1;
663
664                         check_tmp = paircopy(curdefault->check);
665                         pairmove(&request->config_items, &check_tmp);
666                         pairfree(&check_tmp); 
667
668                         reply_tmp = paircopy(curdefault->reply);
669                         pairmove(&request->reply->vps, &reply_tmp);
670                         pairfree(&reply_tmp);
671
672                 }
673
674                 /* 
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
678                  */
679                 if (!fallthrough(curdefault->reply))
680                         break;
681
682                 /*
683                  * If we found the user, we want to stop
684                  * processing once we get to 'lastdefault'
685                  * This way we can process this user's entry
686                  * in the order it was found in the file
687                  */
688                 while((userfound && (user) && (curdefault == user->lastdefault))) {
689                                 DEBUG2("  fastusers:  found lastdefault at line %d",
690                                                    curdefault->lineno);
691
692                         check_tmp = paircopy(user->check);
693                         pairmove(&request->config_items, &check_tmp);
694                         pairfree(&check_tmp); 
695
696                         reply_tmp = paircopy(user->reply);
697                         pairmove(&request->reply->vps, &reply_tmp);
698                         pairfree(&reply_tmp);
699
700                         if(!fallthrough(user->reply)) {
701                                 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
702                                 return(rad_check_return(user->check));
703                         }
704
705                         /* 
706                          * Find next occurence of THIS user in
707                          * the users file
708                          */
709                         user=user->next;
710                         user=fastuser_find(request, user, name);
711                 } 
712
713                 curdefault = curdefault->next;
714         }
715
716         if(userfound || defaultfound) {
717                 pairdelete(&request->reply->vps, PW_FALL_THROUGH);
718                 return(rad_check_return(request->config_items));
719         } else {
720                 DEBUG2("rlm_fastusers:  user not found");
721                 return RLM_MODULE_NOTFOUND;
722         }
723 }
724
725 /*
726  *      Authentication - unused.
727  */
728 static int fastuser_authenticate(void *instance, REQUEST *request)
729 {
730         instance = instance;
731         request = request;
732         return RLM_MODULE_OK;
733 }
734
735 /*
736  *      Pre-Accounting - read the acct_users file for check_items and
737  *      config_items. Reply items are Not Recommended(TM) in acct_users,
738  *      except for Fallthrough, which should work
739  *
740  *      This function is mostly a copy of file_authorize
741  */
742 static int fastuser_preacct(void *instance, REQUEST *request)
743 {
744         VALUE_PAIR      *namepair;
745         const char      *name;
746         VALUE_PAIR      *request_pairs;
747         VALUE_PAIR      **config_pairs;
748         VALUE_PAIR      *reply_pairs = NULL;
749         VALUE_PAIR      *check_tmp;
750         VALUE_PAIR      *reply_tmp;
751         PAIR_LIST       *pl = NULL;
752         int             found = 0;
753         struct fastuser_instance *inst = instance;
754
755         namepair = request->username;
756         name = namepair ? (char *) namepair->strvalue : "NONE";
757         request_pairs = request->packet->vps;
758         config_pairs = &request->config_items;
759         
760         /*
761          *      Find the entry for the user.
762          */
763         for (pl = inst->acctusers; pl; pl = pl->next) {
764
765                 if (strcmp(name, pl->name) && strcmp(pl->name, "DEFAULT"))
766                         continue;
767
768                 if (paircmp(request, request_pairs, pl->check, &reply_pairs) == 0) {
769                         DEBUG2("  acct_users: Matched %s at %d",
770                                pl->name, pl->lineno);
771                         found = 1;
772                         check_tmp = paircopy(pl->check);
773                         reply_tmp = paircopy(pl->reply);
774                         pairmove(&reply_pairs, &reply_tmp);
775                         pairmove(config_pairs, &check_tmp);
776                         pairfree(&reply_tmp);
777                         pairfree(&check_tmp); /* should be NULL */
778                         /*
779                          *      Fallthrough?
780                          */
781                         if (!fallthrough(pl->reply))
782                                 break;
783                 }
784         }
785
786         /*
787          *      See if we succeeded.
788          */
789         if (!found)
790                 return RLM_MODULE_NOOP; /* on to the next module */
791
792         /*
793          *      FIXME: log a warning if there are any reply items other than
794          *      Fallthrough
795          */
796         pairfree(&reply_pairs); /* Don't need these */
797
798         return RLM_MODULE_OK;
799 }
800
801 /*
802  *  Clean up.
803  */
804 static int fastuser_detach(void *instance)
805 {
806         struct fastuser_instance *inst = instance;
807         int hashindex;
808         PAIR_LIST *cur;
809
810         /* Free hash table */
811         for(hashindex=0; hashindex<inst->hashsize; hashindex++) {
812                 if(inst->hashtable[hashindex]) {
813                         cur = inst->hashtable[hashindex];
814                         pairlist_free(&cur);
815                 }
816         } 
817
818         free(inst->compat_mode);
819         free(inst->hashtable);
820         pairlist_free(&inst->defaults);
821         pairlist_free(&inst->acctusers);
822         free(inst->usersfile);
823         free(inst->acctusersfile);
824         free(inst);
825         return 0;
826 }
827
828 /*
829  *      This function is unused
830  */
831 static int fastuser_accounting(void *instance, REQUEST *request)
832 {
833         return RLM_MODULE_FAIL;
834 }
835
836 /* globally exported name */
837 module_t rlm_fastusers = {
838         "fastusers",
839         0,                              /* type: reserved */
840         NULL,                           /* initialization */
841         fastuser_instantiate,           /* instantiation */
842         {
843                 fastuser_authenticate,  /* authentication */
844                 fastuser_authorize,     /* authorization */
845                 fastuser_preacct,       /* preaccounting */
846                 fastuser_accounting,    /* accounting */
847                 NULL,                   /* checksimul */
848                 NULL,                   /* pre-proxy */
849                 NULL,                   /* post-proxy */
850                 NULL                    /* post-auth */
851         },
852         fastuser_detach,                /* detach */
853         NULL                            /* destroy */
854 };
855