Add more NULL's to module data structures, in preparation for
[freeradius.git] / src / modules / rlm_counter / rlm_counter.c
1 /*
2  * rlm_counter.c
3  *
4  * Version:  $Id$
5  *
6  *   This program is free software; you can redistribute it and/or modify
7  *   it under the terms of the GNU General Public License as published by
8  *   the Free Software Foundation; either version 2 of the License, or
9  *   (at your option) any later version.
10  *
11  *   This program is distributed in the hope that it will be useful,
12  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
13  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  *   GNU General Public License for more details.
15  *
16  *   You should have received a copy of the GNU General Public License
17  *   along with this program; if not, write to the Free Software
18  *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  * Copyright 2001  The FreeRADIUS server project
21  * Copyright 2001  Alan DeKok <aland@ox.org>
22  * Copyright 2001,2002  Kostas Kalevras <kkalev@noc.ntua.gr>
23  */
24
25 #include "config.h"
26 #include "autoconf.h"
27 #include "libradius.h"
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <ctype.h>
33
34 #include "radiusd.h"
35 #include "modules.h"
36 #include "conffile.h"
37
38 #include <gdbm.h>
39 #include <time.h>
40
41 #ifdef NEEDS_GDBM_SYNC
42 #       define GDBM_SYNCOPT GDBM_SYNC
43 #else
44 #       define GDBM_SYNCOPT 0
45 #endif
46
47 #ifdef GDBM_NOLOCK
48 #define GDBM_COUNTER_OPTS (GDBM_SYNCOPT | GDBM_NOLOCK)
49 #else
50 #define GDBM_COUNTER_OPTS (GDBM_SYNCOPT)
51 #endif
52
53 #ifndef HAVE_GDBM_FDESC
54 #define gdbm_fdesc(foo) (-1)
55 #endif
56
57 static const char rcsid[] = "$Id$";
58
59 /*
60  *      Define a structure for our module configuration.
61  *
62  *      These variables do not need to be in a structure, but it's
63  *      a lot cleaner to do so, and a pointer to the structure can
64  *      be used as the instance handle.
65  */
66 typedef struct rlm_counter_t {
67         char *filename;  /* name of the database file */
68         char *reset;  /* daily, weekly, monthly, never or user defined */
69         char *key_name;  /* User-Name */
70         char *count_attribute;  /* Acct-Session-Time */
71         char *counter_name;  /* Daily-Session-Time */
72         char *check_name;  /* Daily-Max-Session */
73         char *service_type;  /* Service-Type to search for */
74         int cache_size;
75         int service_val;
76         int key_attr;
77         int count_attr;
78         time_t reset_time;  /* The time of the next reset. */
79         time_t last_reset;  /* The time of the last reset. */
80         int dict_attr;  /* attribute number for the counter. */
81         GDBM_FILE gdbm;
82         int fd;
83 } rlm_counter_t;
84
85 /*
86  *      A mapping of configuration file names to internal variables.
87  *
88  *      Note that the string is dynamically allocated, so it MUST
89  *      be freed.  When the configuration file parse re-reads the string,
90  *      it free's the old one, and strdup's the new one, placing the pointer
91  *      to the strdup'd string into 'config.string'.  This gets around
92  *      buffer over-flows.
93  */
94 static CONF_PARSER module_config[] = {
95   { "filename", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,filename), NULL, NULL },
96   { "key", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,key_name), NULL, NULL },
97   { "reset", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,reset), NULL,  NULL },
98   { "count-attribute", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,count_attribute), NULL, NULL },
99   { "counter-name", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,counter_name), NULL,  NULL },
100   { "check-name", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,check_name), NULL, NULL },
101   { "allowed-servicetype", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,service_type),NULL, NULL },
102   { "cache-size", PW_TYPE_INTEGER, offsetof(rlm_counter_t,cache_size), NULL, "1000" },
103   { NULL, -1, 0, NULL, NULL }
104 };
105
106
107 /*
108  *      See if the counter matches.
109  */
110 static int counter_cmp(void *instance, REQUEST *req, VALUE_PAIR *request, VALUE_PAIR *check,
111                 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
112 {
113         rlm_counter_t *data = (rlm_counter_t *) instance;
114         datum key_datum;
115         datum count_datum;
116         VALUE_PAIR *key_vp;
117         int counter;
118
119         check_pairs = check_pairs; /* shut the compiler up */
120         reply_pairs = reply_pairs;
121
122         /*
123          *      Find the key attribute.
124          */
125         key_vp = pairfind(request, data->key_attr);
126         if (key_vp == NULL) {
127                 return RLM_MODULE_NOOP;
128         }
129
130         key_datum.dptr = key_vp->strvalue;
131         key_datum.dsize = key_vp->length;
132
133         count_datum = gdbm_fetch(data->gdbm, key_datum);
134
135         if (count_datum.dptr == NULL) {
136                 return -1;
137         }
138         memcpy(&counter, count_datum.dptr, sizeof(int));
139         free(count_datum.dptr);
140
141         return counter - check->lvalue;
142 }
143
144 static int add_defaults(rlm_counter_t *data)
145 {
146         datum key_datum;
147         datum time_datum;
148         const char *default1 = "DEFAULT1";
149         const char *default2 = "DEFAULT2";
150         
151         DEBUG2("rlm_counter: add_defaults: Start");
152
153         key_datum.dptr = (const char *) default1;
154         key_datum.dsize = strlen(default1);
155         time_datum.dptr = (char *) &data->reset_time;
156         time_datum.dsize = sizeof(time_t);
157
158         if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
159                 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
160                                 data->filename, gdbm_strerror(gdbm_errno));
161                 return RLM_MODULE_FAIL;
162         }
163         DEBUG2("rlm_counter: DEFAULT1 set to %d",(int)data->reset_time);
164
165         key_datum.dptr = (const char *) default2;
166         key_datum.dsize = strlen(default2);
167         time_datum.dptr = (char *) &data->last_reset;
168         time_datum.dsize = sizeof(time_t);
169
170         if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
171                 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
172                                 data->filename, gdbm_strerror(gdbm_errno));
173                 return RLM_MODULE_FAIL;
174         }
175         DEBUG2("rlm_counter: DEFAULT2 set to %d",(int)data->last_reset);
176         DEBUG2("rlm_counter: add_defaults: End");
177
178         return RLM_MODULE_OK;
179 }
180
181 static int reset_db(rlm_counter_t *data)
182 {
183         int cache_size = data->cache_size;
184         int ret;
185
186         DEBUG2("rlm_counter: reset_db: Closing database");
187         gdbm_close(data->gdbm);
188
189         /*
190          *      Open a completely new database.
191          */
192         data->gdbm = gdbm_open(data->filename, sizeof(int),
193                         GDBM_NEWDB | GDBM_COUNTER_OPTS, 0600, NULL);
194         if (data->gdbm == NULL) {
195                 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
196                                 data->filename, strerror(errno));
197                 return RLM_MODULE_FAIL;
198         }
199         if (data->fd >= 0) data->fd = gdbm_fdesc(data->gdbm);
200         if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
201                 radlog(L_ERR, "rlm_counter: Failed to set cache size");
202         DEBUG2("rlm_counter: reset_db: Opened new database");
203
204         /*
205          * Add defaults
206          */
207         ret = add_defaults(data);
208         if (ret != RLM_MODULE_OK)
209                 return ret;     
210
211         DEBUG2("rlm_counter: reset_db ended");
212
213         return RLM_MODULE_OK;
214 }
215
216 static int find_next_reset(rlm_counter_t *data, time_t timeval)
217 {
218         int ret=0;
219         unsigned int num=1;
220         char last = 0;
221         struct tm *tm, s_tm;
222
223         tm = localtime_r(&timeval, &s_tm);
224         tm->tm_sec = tm->tm_min = 0;
225
226         if (data->reset == NULL)
227                 return -1;
228         if (isdigit((int) data->reset[0])){
229                 unsigned int len=0;
230
231                 len = strlen(data->reset);
232                 if (len == 0)
233                         return -1;
234                 last = data->reset[len - 1];
235                 if (!isalpha((int) last))
236                         last = 'd';
237                 num = atoi(data->reset);
238                 DEBUG("rlm_counter: num=%d, last=%c",num,last);
239         }
240         if (strcmp(data->reset, "hourly") == 0 || last == 'h') {
241                 /*
242                  *  Round up to the next nearest hour.
243                  */
244                 tm->tm_hour += num;
245                 data->reset_time = mktime(tm);
246         } else if (strcmp(data->reset, "daily") == 0 || last == 'd') {
247                 /*
248                  *  Round up to the next nearest day.
249                  */
250                 tm->tm_hour = 0;
251                 tm->tm_mday += num;
252                 data->reset_time = mktime(tm);
253         } else if (strcmp(data->reset, "weekly") == 0 || last == 'w') {
254                 /*
255                  *  Round up to the next nearest week.
256                  */
257                 tm->tm_hour = 0;
258                 tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
259                 data->reset_time = mktime(tm);
260         } else if (strcmp(data->reset, "monthly") == 0 || last == 'm') {
261                 tm->tm_hour = 0;
262                 tm->tm_mday = 1;
263                 tm->tm_mon += num;
264                 data->reset_time = mktime(tm);
265         } else if (strcmp(data->reset, "never") == 0) {
266                 data->reset_time = 0;
267         } else {
268                 radlog(L_ERR, "rlm_counter: Unknown reset timer \"%s\"",
269                         data->reset);
270                 return -1;
271         }
272         DEBUG2("rlm_counter: Current Time: %d, Next reset %d", 
273                 (int)timeval,(int)data->reset_time);
274
275         return ret;
276 }
277
278
279 /*
280  *      Do any per-module initialization that is separate to each
281  *      configured instance of the module.  e.g. set up connections
282  *      to external databases, read configuration files, set up
283  *      dictionary entries, etc.
284  *
285  *      If configuration information is given in the config section
286  *      that must be referenced in later calls, store a handle to it
287  *      in *instance otherwise put a null pointer there.
288  */
289 static int counter_instantiate(CONF_SECTION *conf, void **instance)
290 {
291         rlm_counter_t *data;
292         DICT_ATTR *dattr;
293         DICT_VALUE *dval;
294         ATTR_FLAGS flags;
295         time_t now;
296         int cache_size;
297         int ret;
298         datum key_datum;
299         datum time_datum;
300         const char *default1 = "DEFAULT1";
301         const char *default2 = "DEFAULT2";
302         
303         /*
304          *      Set up a storage area for instance data
305          */
306         data = rad_malloc(sizeof(*data));
307
308         /*
309          *      If the configuration parameters can't be parsed, then
310          *      fail.
311          */
312         if (cf_section_parse(conf, data, module_config) < 0) {
313                 free(data);
314                 return -1;
315         }
316         cache_size = data->cache_size;
317
318         /*
319          *      Discover the attribute number of the key. 
320          */
321         if (data->key_name == NULL) {
322                 radlog(L_ERR, "rlm_counter: 'key' must be set.");
323                 exit(0);
324         }
325         dattr = dict_attrbyname(data->key_name);
326         if (dattr == NULL) {
327                 radlog(L_ERR, "rlm_counter: No such attribute %s",
328                                 data->key_name);
329                 return -1;
330         }
331         data->key_attr = dattr->attr;
332         
333         /*
334          *      Discover the attribute number of the counter. 
335          */
336         if (data->count_attribute == NULL) {
337                 radlog(L_ERR, "rlm_counter: 'count-attribute' must be set.");
338                 exit(0);
339         }
340         dattr = dict_attrbyname(data->count_attribute);
341         if (dattr == NULL) {
342                 radlog(L_ERR, "rlm_counter: No such attribute %s",
343                                 data->count_attribute);
344                 return -1;
345         }
346         data->count_attr = dattr->attr;
347
348         /*
349          *  Create a new attribute for the counter.
350          */
351         if (data->counter_name == NULL) {
352                 radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
353                 exit(0);
354         }
355
356         memset(&flags, 0, sizeof(flags));
357         dict_addattr(data->counter_name, 0, PW_TYPE_INTEGER, -1, flags);
358         dattr = dict_attrbyname(data->counter_name);
359         if (dattr == NULL) {
360                 radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
361                                 data->counter_name);
362                 return -1;
363         }
364         data->dict_attr = dattr->attr;
365         DEBUG2("rlm_counter: Counter attribute %s is number %d",
366                         data->counter_name, data->dict_attr);
367
368         /*
369          * Create a new attribute for the check item.
370          */
371         if (data->check_name == NULL) {
372                 radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
373                 exit(0);
374         }
375         dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1, flags);
376         dattr = dict_attrbyname(data->check_name);
377         if (dattr == NULL) {
378                 radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
379                                 data->counter_name);
380                 return -1;
381         }
382
383         /*
384          * Find the attribute for the allowed protocol
385          */
386         if (data->service_type != NULL) {
387                 if ((dval = dict_valbyname(PW_SERVICE_TYPE, data->service_type)) == NULL) {
388                         radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
389                                         data->service_type);
390                         return -1;
391                 }
392                 data->service_val = dval->value;
393         }       
394
395         /*
396          * Find when to reset the database.
397          */
398         if (data->reset == NULL) {
399                 radlog(L_ERR, "rlm_counter: 'reset' must be set.");
400                 exit(0);
401         }
402         now = time(NULL);
403         data->reset_time = 0;
404         data->last_reset = now;
405
406         if (find_next_reset(data,now) == -1)
407                 return -1;
408
409         if (data->filename == NULL) {
410                 radlog(L_ERR, "rlm_counter: 'filename' must be set.");
411                 exit(0);
412         }
413         data->gdbm = gdbm_open(data->filename, sizeof(int),
414                         GDBM_WRCREAT | GDBM_COUNTER_OPTS, 0600, NULL);
415         if (data->gdbm == NULL) {
416                 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
417                                 data->filename, strerror(errno));
418                 return -1;
419         }
420         /*
421          * Look for the DEFAULT1 entry. This entry if it exists contains the
422          * time of the next database reset. This time is set each time we reset
423          * the database. If next_reset < now then we reset the database.
424          * That way we can overcome the problem where radiusd is down during a database
425          * reset time. If we did not keep state information in the database then the reset
426          * would be extended and that would create problems.
427          *
428          * We also store the time of the last reset in the DEFAULT2 entry.
429          *
430          * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database
431          */
432
433         key_datum.dptr = (const char *)default1;
434         key_datum.dsize = strlen(default1);
435
436         time_datum = gdbm_fetch(data->gdbm, key_datum);
437         if (time_datum.dptr != NULL){
438                 time_t next_reset = 0;
439
440                 memcpy(&next_reset, time_datum.dptr, sizeof(time_t));
441                 free(time_datum.dptr);
442                 if (next_reset <= now){
443
444                         data->last_reset = now;
445                         ret = reset_db(data);
446                         if (ret != RLM_MODULE_OK)
447                                 return -1;
448                 }
449                 else
450                         data->reset_time = next_reset;
451                 key_datum.dptr = (const char *)default2;
452                 key_datum.dsize = strlen(default2);
453
454                 time_datum = gdbm_fetch(data->gdbm, key_datum); 
455                 if (time_datum.dptr != NULL){
456                         memcpy(&data->last_reset, time_datum.dptr, sizeof(time_t));
457                         free(time_datum.dptr);
458                 }
459         }
460         else{
461                 ret = add_defaults(data);
462                 if (ret != RLM_MODULE_OK)
463                         return -1;
464         }
465
466         if (data->fd >= 0) data->fd = gdbm_fdesc(data->gdbm);
467
468         if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
469                 radlog(L_ERR, "rlm_counter: Failed to set cache size");
470
471
472         /*
473          *      Register the counter comparison operation.
474          */
475         paircompare_register(data->dict_attr, 0, counter_cmp, data);
476
477         *instance = data;
478         
479         return 0;
480 }
481
482 /*
483  *      Write accounting information to this modules database.
484  */
485 static int counter_accounting(void *instance, REQUEST *request)
486 {
487         rlm_counter_t *data = (rlm_counter_t *)instance;
488         datum key_datum;
489         datum count_datum;
490         VALUE_PAIR *key_vp, *count_vp, *proto_vp;
491         int counter;
492         int rcode;
493         time_t diff;
494
495         /*
496          *      Before doing anything else, see if we have to reset
497          *      the counters.
498          */
499         if (data->reset_time && (data->reset_time <= request->timestamp)) {
500                 int ret;
501
502                 data->last_reset = data->reset_time;
503                 find_next_reset(data,request->timestamp);
504                 ret = reset_db(data);
505                 if (ret != RLM_MODULE_OK)
506                         return ret;
507         }
508         /*
509          * Check if we need to watch out for a specific service-type. If yes then check it
510          */
511         if (data->service_type != NULL) {
512                 if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE)) == NULL)
513                         return RLM_MODULE_NOOP;
514                 if (proto_vp->lvalue != data->service_val)
515                         return RLM_MODULE_NOOP;
516
517         }       
518         
519
520         /*
521          *      Look for the key.  User-Name is special.  It means
522          *      The REAL username, after stripping.
523          */
524         key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
525         if (key_vp == NULL)
526                 return RLM_MODULE_NOOP;
527
528         /*
529          *      Look for the attribute to use as a counter.
530          */
531         count_vp = pairfind(request->packet->vps, data->count_attr);
532         if (count_vp == NULL)
533                 return RLM_MODULE_NOOP;
534
535         key_datum.dptr = key_vp->strvalue;
536         key_datum.dsize = key_vp->length;
537
538         count_datum = gdbm_fetch(data->gdbm, key_datum);
539         if (count_datum.dptr == NULL)
540                 counter = 0;
541         else{
542                 memcpy(&counter, count_datum.dptr, sizeof(int));
543                 free(count_datum.dptr);
544         }
545
546         if (count_vp->type == PW_TYPE_DATE) {
547                 /*
548                  *      If session time < diff then the user got in after the
549                  *      last reset. So add his session time, otherwise add the
550                  *      diff.
551                  *
552                  *      That way if he logged in at 23:00 and we reset the
553                  *      daily counter at 24:00 and he logged out at 01:00
554                  *      then we will only count one hour (the one in the new
555                  *      day). That is the right thing
556                  */
557                 diff = request->timestamp - data->last_reset;
558                 counter += (count_vp->lvalue < diff) ? count_vp->lvalue : diff;
559
560         } else if (count_vp->type == PW_TYPE_INTEGER) {
561                 /*
562                  *      Integers get counted, without worrying about
563                  *      reset dates.
564                  */
565                 counter += count_vp->lvalue;
566
567         } else {
568                 /*
569                  *      The attribute is NOT an integer, just count once
570                  *      more that we've seen it.
571                  */
572                 counter++;
573         }
574         count_datum.dptr = (char *) &counter;
575         count_datum.dsize = sizeof(int);
576
577         rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);
578         if (rcode < 0) {
579                 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
580                                 data->filename, gdbm_strerror(gdbm_errno));
581                 return RLM_MODULE_FAIL;
582         }
583
584         return RLM_MODULE_OK;
585 }
586
587 /*
588  *      Find the named user in this modules database.  Create the set
589  *      of attribute-value pairs to check and reply with for this user
590  *      from the database. The authentication code only needs to check
591  *      the password, the rest is done here.
592  */
593 static int counter_authorize(void *instance, REQUEST *request)
594 {
595         rlm_counter_t *data = (rlm_counter_t *) instance;
596         int ret=RLM_MODULE_NOOP;
597         datum key_datum;
598         datum count_datum;
599         int counter=0;
600         int res=0;
601         DICT_ATTR *dattr;
602         VALUE_PAIR *key_vp, *check_vp;
603         VALUE_PAIR *reply_item;
604         char msg[128];
605
606         /* quiet the compiler */
607         instance = instance;
608         request = request;
609
610         /*
611          *      Before doing anything else, see if we have to reset
612          *      the counters.
613          */
614         if (data->reset_time && (data->reset_time <= request->timestamp)) {
615                 int ret2;
616
617                 data->last_reset = data->reset_time;
618                 find_next_reset(data,request->timestamp);
619                 ret2 = reset_db(data);
620                 if (ret2 != RLM_MODULE_OK)
621                         return ret2;
622         }
623
624
625         /*
626          *      Look for the key.  User-Name is special.  It means
627          *      The REAL username, after stripping.
628          */
629         DEBUG2("rlm_counter: Entering module authorize code");
630         key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
631         if (key_vp == NULL) {
632                 DEBUG2("rlm_counter: Could not find Key value pair");
633                 return ret;
634         }
635
636         /*
637          *      Look for the check item
638          */
639         if ((dattr = dict_attrbyname(data->check_name)) == NULL) {
640                 return ret;
641         }
642         if ((check_vp= pairfind(request->config_items, dattr->attr)) == NULL) {
643                 DEBUG2("rlm_counter: Could not find Check item value pair");
644                 return ret;
645         }
646
647         key_datum.dptr = key_vp->strvalue;
648         key_datum.dsize = key_vp->length;
649         
650         count_datum = gdbm_fetch(data->gdbm, key_datum);
651         if (count_datum.dptr != NULL){
652                 memcpy(&counter, count_datum.dptr, sizeof(int));
653                 free(count_datum.dptr);
654         }
655
656         /*
657          * Check if check item > counter
658          */
659         res=check_vp->lvalue - counter;
660         if (res > 0) {
661                 /*
662                  *      We are assuming that simultaneous-use=1. But
663                  *      even if that does not happen then our user
664                  *      could login at max for 2*max-usage-time Is
665                  *      that acceptable?
666                  */
667
668                 /*
669                  *      User is allowed, but set Session-Timeout.
670                  *      Stolen from main/auth.c
671                  */
672
673                 /*
674                  *      If we are near a reset then add the next
675                  *      limit, so that the user will not need to
676                  *      login again
677                  */
678                 if (data->reset_time && (
679                         res >= (data->reset_time - request->timestamp))) {
680                         res += check_vp->lvalue;
681                 }
682
683                 DEBUG2("rlm_counter: (Check item - counter) is greater than zero");
684                 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
685                         if (reply_item->lvalue > res)
686                                 reply_item->lvalue = res;
687                 } else {
688                         if ((reply_item = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
689                                 radlog(L_ERR|L_CONS, "no memory");
690                                 return RLM_MODULE_NOOP;
691                         }
692                         reply_item->lvalue = res;
693                         pairadd(&request->reply->vps, reply_item);
694                 }
695
696                 ret=RLM_MODULE_OK;
697
698                 DEBUG2("rlm_counter: Authorized user %s, check_item=%d, counter=%d",
699                                 key_vp->strvalue,check_vp->lvalue,counter);
700                 DEBUG2("rlm_counter: Sent Reply-Item for user %s, Type=Session-Timeout, value=%d",
701                                 key_vp->strvalue,res);
702         }
703         else{
704                 char module_fmsg[MAX_STRING_LEN];
705                 VALUE_PAIR *module_fmsg_vp;
706
707                 /*
708                  * User is denied access, send back a reply message
709                 */
710                 sprintf(msg, "Your maximum %s usage time has been reached", data->reset);
711                 reply_item=pairmake("Reply-Message", msg, T_OP_EQ);
712                 pairadd(&request->reply->vps, reply_item);
713
714                 snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", data->reset);
715                 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
716                 pairadd(&request->packet->vps, module_fmsg_vp); 
717
718                 ret=RLM_MODULE_REJECT;
719
720                 DEBUG2("rlm_counter: Rejected user %s, check_item=%d, counter=%d",
721                                 key_vp->strvalue,check_vp->lvalue,counter);
722         }
723
724         return ret;
725 }
726
727 static int counter_detach(void *instance)
728 {
729         rlm_counter_t *data = (rlm_counter_t *) instance;
730
731         paircompare_unregister(data->dict_attr, counter_cmp);
732         gdbm_close(data->gdbm);
733         free(data->filename);
734         free(data->reset);
735         free(data->key_name);
736         free(data->count_attribute);
737         free(data->counter_name);
738
739         free(instance);
740         return 0;
741 }
742
743 /*
744  *      The module name should be the only globally exported symbol.
745  *      That is, everything else should be 'static'.
746  *
747  *      If the module needs to temporarily modify it's instantiation
748  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
749  *      The server will then take care of ensuring that the module
750  *      is single-threaded.
751  */
752 module_t rlm_counter = {
753         "Counter",      
754         RLM_TYPE_THREAD_UNSAFE,         /* type */
755         NULL,                           /* initialization */
756         counter_instantiate,            /* instantiation */
757         {
758                 NULL,                   /* authentication */
759                 counter_authorize,      /* authorization */
760                 NULL,                   /* preaccounting */
761                 counter_accounting,     /* accounting */
762                 NULL,                   /* checksimul */
763                 NULL,                   /* pre-proxy */
764                 NULL,                   /* post-proxy */
765                 NULL                    /* post-auth */
766         },
767         counter_detach,                 /* detach */
768         NULL,                           /* destroy */
769 };