Set the cache size *before* doing any operations on the gdbm files
[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;         /* The gdbm file handle */
82         pthread_mutex_t mutex;  /* A mutex to lock the gdbm file for only one reader/writer */
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 (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
200                 radlog(L_ERR, "rlm_counter: Failed to set cache size");
201         DEBUG2("rlm_counter: reset_db: Opened new database");
202
203         /*
204          * Add defaults
205          */
206         ret = add_defaults(data);
207         if (ret != RLM_MODULE_OK)
208                 return ret;     
209
210         DEBUG2("rlm_counter: reset_db ended");
211
212         return RLM_MODULE_OK;
213 }
214
215 static int find_next_reset(rlm_counter_t *data, time_t timeval)
216 {
217         int ret=0;
218         unsigned int num=1;
219         char last = 0;
220         struct tm *tm, s_tm;
221
222         tm = localtime_r(&timeval, &s_tm);
223         tm->tm_sec = tm->tm_min = 0;
224
225         if (data->reset == NULL)
226                 return -1;
227         if (isdigit((int) data->reset[0])){
228                 unsigned int len=0;
229
230                 len = strlen(data->reset);
231                 if (len == 0)
232                         return -1;
233                 last = data->reset[len - 1];
234                 if (!isalpha((int) last))
235                         last = 'd';
236                 num = atoi(data->reset);
237                 DEBUG("rlm_counter: num=%d, last=%c",num,last);
238         }
239         if (strcmp(data->reset, "hourly") == 0 || last == 'h') {
240                 /*
241                  *  Round up to the next nearest hour.
242                  */
243                 tm->tm_hour += num;
244                 data->reset_time = mktime(tm);
245         } else if (strcmp(data->reset, "daily") == 0 || last == 'd') {
246                 /*
247                  *  Round up to the next nearest day.
248                  */
249                 tm->tm_hour = 0;
250                 tm->tm_mday += num;
251                 data->reset_time = mktime(tm);
252         } else if (strcmp(data->reset, "weekly") == 0 || last == 'w') {
253                 /*
254                  *  Round up to the next nearest week.
255                  */
256                 tm->tm_hour = 0;
257                 tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
258                 data->reset_time = mktime(tm);
259         } else if (strcmp(data->reset, "monthly") == 0 || last == 'm') {
260                 tm->tm_hour = 0;
261                 tm->tm_mday = 1;
262                 tm->tm_mon += num;
263                 data->reset_time = mktime(tm);
264         } else if (strcmp(data->reset, "never") == 0) {
265                 data->reset_time = 0;
266         } else {
267                 radlog(L_ERR, "rlm_counter: Unknown reset timer \"%s\"",
268                         data->reset);
269                 return -1;
270         }
271         DEBUG2("rlm_counter: Current Time: %d, Next reset %d", 
272                 (int)timeval,(int)data->reset_time);
273
274         return ret;
275 }
276
277
278 /*
279  *      Do any per-module initialization that is separate to each
280  *      configured instance of the module.  e.g. set up connections
281  *      to external databases, read configuration files, set up
282  *      dictionary entries, etc.
283  *
284  *      If configuration information is given in the config section
285  *      that must be referenced in later calls, store a handle to it
286  *      in *instance otherwise put a null pointer there.
287  */
288 static int counter_instantiate(CONF_SECTION *conf, void **instance)
289 {
290         rlm_counter_t *data;
291         DICT_ATTR *dattr;
292         DICT_VALUE *dval;
293         ATTR_FLAGS flags;
294         time_t now;
295         int cache_size;
296         int ret;
297         datum key_datum;
298         datum time_datum;
299         const char *default1 = "DEFAULT1";
300         const char *default2 = "DEFAULT2";
301         
302         /*
303          *      Set up a storage area for instance data
304          */
305         data = rad_malloc(sizeof(*data));
306
307         /*
308          *      If the configuration parameters can't be parsed, then
309          *      fail.
310          */
311         if (cf_section_parse(conf, data, module_config) < 0) {
312                 free(data);
313                 return -1;
314         }
315         cache_size = data->cache_size;
316
317         /*
318          *      Discover the attribute number of the key. 
319          */
320         if (data->key_name == NULL) {
321                 radlog(L_ERR, "rlm_counter: 'key' must be set.");
322                 exit(0);
323         }
324         dattr = dict_attrbyname(data->key_name);
325         if (dattr == NULL) {
326                 radlog(L_ERR, "rlm_counter: No such attribute %s",
327                                 data->key_name);
328                 return -1;
329         }
330         data->key_attr = dattr->attr;
331         
332         /*
333          *      Discover the attribute number of the counter. 
334          */
335         if (data->count_attribute == NULL) {
336                 radlog(L_ERR, "rlm_counter: 'count-attribute' must be set.");
337                 exit(0);
338         }
339         dattr = dict_attrbyname(data->count_attribute);
340         if (dattr == NULL) {
341                 radlog(L_ERR, "rlm_counter: No such attribute %s",
342                                 data->count_attribute);
343                 return -1;
344         }
345         data->count_attr = dattr->attr;
346
347         /*
348          *  Create a new attribute for the counter.
349          */
350         if (data->counter_name == NULL) {
351                 radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
352                 exit(0);
353         }
354
355         memset(&flags, 0, sizeof(flags));
356         dict_addattr(data->counter_name, 0, PW_TYPE_INTEGER, -1, flags);
357         dattr = dict_attrbyname(data->counter_name);
358         if (dattr == NULL) {
359                 radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
360                                 data->counter_name);
361                 return -1;
362         }
363         data->dict_attr = dattr->attr;
364         DEBUG2("rlm_counter: Counter attribute %s is number %d",
365                         data->counter_name, data->dict_attr);
366
367         /*
368          * Create a new attribute for the check item.
369          */
370         if (data->check_name == NULL) {
371                 radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
372                 exit(0);
373         }
374         dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1, flags);
375         dattr = dict_attrbyname(data->check_name);
376         if (dattr == NULL) {
377                 radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
378                                 data->counter_name);
379                 return -1;
380         }
381
382         /*
383          * Find the attribute for the allowed protocol
384          */
385         if (data->service_type != NULL) {
386                 if ((dval = dict_valbyname(PW_SERVICE_TYPE, data->service_type)) == NULL) {
387                         radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
388                                         data->service_type);
389                         return -1;
390                 }
391                 data->service_val = dval->value;
392         }       
393
394         /*
395          * Find when to reset the database.
396          */
397         if (data->reset == NULL) {
398                 radlog(L_ERR, "rlm_counter: 'reset' must be set.");
399                 exit(0);
400         }
401         now = time(NULL);
402         data->reset_time = 0;
403         data->last_reset = now;
404
405         if (find_next_reset(data,now) == -1)
406                 return -1;
407
408         if (data->filename == NULL) {
409                 radlog(L_ERR, "rlm_counter: 'filename' must be set.");
410                 exit(0);
411         }
412         data->gdbm = gdbm_open(data->filename, sizeof(int),
413                         GDBM_WRCREAT | GDBM_COUNTER_OPTS, 0600, NULL);
414         if (data->gdbm == NULL) {
415                 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
416                                 data->filename, strerror(errno));
417                 return -1;
418         }
419         if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
420                 radlog(L_ERR, "rlm_counter: Failed to set cache size");
421
422         /*
423          * Look for the DEFAULT1 entry. This entry if it exists contains the
424          * time of the next database reset. This time is set each time we reset
425          * the database. If next_reset < now then we reset the database.
426          * That way we can overcome the problem where radiusd is down during a database
427          * reset time. If we did not keep state information in the database then the reset
428          * would be extended and that would create problems.
429          *
430          * We also store the time of the last reset in the DEFAULT2 entry.
431          *
432          * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database
433          */
434
435         key_datum.dptr = (const char *)default1;
436         key_datum.dsize = strlen(default1);
437
438         time_datum = gdbm_fetch(data->gdbm, key_datum);
439         if (time_datum.dptr != NULL){
440                 time_t next_reset = 0;
441
442                 memcpy(&next_reset, time_datum.dptr, sizeof(time_t));
443                 free(time_datum.dptr);
444                 if (next_reset <= now){
445
446                         data->last_reset = now;
447                         ret = reset_db(data);
448                         if (ret != RLM_MODULE_OK)
449                                 return -1;
450                 }
451                 else
452                         data->reset_time = next_reset;
453                 key_datum.dptr = (const char *)default2;
454                 key_datum.dsize = strlen(default2);
455
456                 time_datum = gdbm_fetch(data->gdbm, key_datum); 
457                 if (time_datum.dptr != NULL){
458                         memcpy(&data->last_reset, time_datum.dptr, sizeof(time_t));
459                         free(time_datum.dptr);
460                 }
461         }
462         else{
463                 ret = add_defaults(data);
464                 if (ret != RLM_MODULE_OK)
465                         return -1;
466         }
467
468
469         /*
470          *      Register the counter comparison operation.
471          */
472         paircompare_register(data->dict_attr, 0, counter_cmp, data);
473
474         /*
475          * Init the mutex
476          */
477         pthread_mutex_init(&data->mutex, NULL);
478
479         *instance = data;
480         
481         return 0;
482 }
483
484 /*
485  *      Write accounting information to this modules database.
486  */
487 static int counter_accounting(void *instance, REQUEST *request)
488 {
489         rlm_counter_t *data = (rlm_counter_t *)instance;
490         datum key_datum;
491         datum count_datum;
492         VALUE_PAIR *key_vp, *count_vp, *proto_vp;
493         int counter;
494         int rcode;
495         time_t diff;
496
497         /*
498          *      Before doing anything else, see if we have to reset
499          *      the counters.
500          */
501         if (data->reset_time && (data->reset_time <= request->timestamp)) {
502                 int ret;
503
504                 data->last_reset = data->reset_time;
505                 find_next_reset(data,request->timestamp);
506                 pthread_mutex_lock(&data->mutex);
507                 ret = reset_db(data);
508                 pthread_mutex_unlock(&data->mutex);
509                 if (ret != RLM_MODULE_OK)
510                         return ret;
511         }
512         /*
513          * Check if we need to watch out for a specific service-type. If yes then check it
514          */
515         if (data->service_type != NULL) {
516                 if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE)) == NULL)
517                         return RLM_MODULE_NOOP;
518                 if (proto_vp->lvalue != data->service_val)
519                         return RLM_MODULE_NOOP;
520
521         }       
522         
523
524         /*
525          *      Look for the key.  User-Name is special.  It means
526          *      The REAL username, after stripping.
527          */
528         key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
529         if (key_vp == NULL)
530                 return RLM_MODULE_NOOP;
531
532         /*
533          *      Look for the attribute to use as a counter.
534          */
535         count_vp = pairfind(request->packet->vps, data->count_attr);
536         if (count_vp == NULL)
537                 return RLM_MODULE_NOOP;
538
539         key_datum.dptr = key_vp->strvalue;
540         key_datum.dsize = key_vp->length;
541
542         pthread_mutex_lock(&data->mutex);
543         count_datum = gdbm_fetch(data->gdbm, key_datum);
544         pthread_mutex_unlock(&data->mutex);
545         if (count_datum.dptr == NULL)
546                 counter = 0;
547         else{
548                 memcpy(&counter, count_datum.dptr, sizeof(int));
549                 free(count_datum.dptr);
550         }
551
552         if (count_vp->type == PW_TYPE_DATE) {
553                 /*
554                  *      If session time < diff then the user got in after the
555                  *      last reset. So add his session time, otherwise add the
556                  *      diff.
557                  *
558                  *      That way if he logged in at 23:00 and we reset the
559                  *      daily counter at 24:00 and he logged out at 01:00
560                  *      then we will only count one hour (the one in the new
561                  *      day). That is the right thing
562                  */
563                 diff = request->timestamp - data->last_reset;
564                 counter += (count_vp->lvalue < diff) ? count_vp->lvalue : diff;
565
566         } else if (count_vp->type == PW_TYPE_INTEGER) {
567                 /*
568                  *      Integers get counted, without worrying about
569                  *      reset dates.
570                  */
571                 counter += count_vp->lvalue;
572
573         } else {
574                 /*
575                  *      The attribute is NOT an integer, just count once
576                  *      more that we've seen it.
577                  */
578                 counter++;
579         }
580         count_datum.dptr = (char *) &counter;
581         count_datum.dsize = sizeof(int);
582
583         pthread_mutex_lock(&data->mutex);
584         rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);
585         pthread_mutex_unlock(&data->mutex);
586         if (rcode < 0) {
587                 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
588                                 data->filename, gdbm_strerror(gdbm_errno));
589                 return RLM_MODULE_FAIL;
590         }
591
592         return RLM_MODULE_OK;
593 }
594
595 /*
596  *      Find the named user in this modules database.  Create the set
597  *      of attribute-value pairs to check and reply with for this user
598  *      from the database. The authentication code only needs to check
599  *      the password, the rest is done here.
600  */
601 static int counter_authorize(void *instance, REQUEST *request)
602 {
603         rlm_counter_t *data = (rlm_counter_t *) instance;
604         int ret=RLM_MODULE_NOOP;
605         datum key_datum;
606         datum count_datum;
607         int counter=0;
608         int res=0;
609         DICT_ATTR *dattr;
610         VALUE_PAIR *key_vp, *check_vp;
611         VALUE_PAIR *reply_item;
612         char msg[128];
613
614         /* quiet the compiler */
615         instance = instance;
616         request = request;
617
618         /*
619          *      Before doing anything else, see if we have to reset
620          *      the counters.
621          */
622         if (data->reset_time && (data->reset_time <= request->timestamp)) {
623                 int ret2;
624
625                 data->last_reset = data->reset_time;
626                 find_next_reset(data,request->timestamp);
627                 pthread_mutex_lock(&data->mutex);
628                 ret2 = reset_db(data);
629                 pthread_mutex_unlock(&data->mutex);
630                 if (ret2 != RLM_MODULE_OK)
631                         return ret2;
632         }
633
634
635         /*
636          *      Look for the key.  User-Name is special.  It means
637          *      The REAL username, after stripping.
638          */
639         DEBUG2("rlm_counter: Entering module authorize code");
640         key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
641         if (key_vp == NULL) {
642                 DEBUG2("rlm_counter: Could not find Key value pair");
643                 return ret;
644         }
645
646         /*
647          *      Look for the check item
648          */
649         if ((dattr = dict_attrbyname(data->check_name)) == NULL) {
650                 return ret;
651         }
652         if ((check_vp= pairfind(request->config_items, dattr->attr)) == NULL) {
653                 DEBUG2("rlm_counter: Could not find Check item value pair");
654                 return ret;
655         }
656
657         key_datum.dptr = key_vp->strvalue;
658         key_datum.dsize = key_vp->length;
659         
660         pthread_mutex_lock(&data->mutex);
661         count_datum = gdbm_fetch(data->gdbm, key_datum);
662         pthread_mutex_unlock(&data->mutex);
663         if (count_datum.dptr != NULL){
664                 memcpy(&counter, count_datum.dptr, sizeof(int));
665                 free(count_datum.dptr);
666         }
667
668         /*
669          * Check if check item > counter
670          */
671         res=check_vp->lvalue - counter;
672         if (res > 0) {
673                 /*
674                  *      We are assuming that simultaneous-use=1. But
675                  *      even if that does not happen then our user
676                  *      could login at max for 2*max-usage-time Is
677                  *      that acceptable?
678                  */
679
680                 /*
681                  *      User is allowed, but set Session-Timeout.
682                  *      Stolen from main/auth.c
683                  */
684
685                 /*
686                  *      If we are near a reset then add the next
687                  *      limit, so that the user will not need to
688                  *      login again
689                  */
690                 if (data->reset_time && (
691                         res >= (data->reset_time - request->timestamp))) {
692                         res += check_vp->lvalue;
693                 }
694
695                 DEBUG2("rlm_counter: (Check item - counter) is greater than zero");
696                 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
697                         if (reply_item->lvalue > res)
698                                 reply_item->lvalue = res;
699                 } else {
700                         if ((reply_item = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
701                                 radlog(L_ERR|L_CONS, "no memory");
702                                 return RLM_MODULE_NOOP;
703                         }
704                         reply_item->lvalue = res;
705                         pairadd(&request->reply->vps, reply_item);
706                 }
707
708                 ret=RLM_MODULE_OK;
709
710                 DEBUG2("rlm_counter: Authorized user %s, check_item=%d, counter=%d",
711                                 key_vp->strvalue,check_vp->lvalue,counter);
712                 DEBUG2("rlm_counter: Sent Reply-Item for user %s, Type=Session-Timeout, value=%d",
713                                 key_vp->strvalue,res);
714         }
715         else{
716                 char module_fmsg[MAX_STRING_LEN];
717                 VALUE_PAIR *module_fmsg_vp;
718
719                 /*
720                  * User is denied access, send back a reply message
721                 */
722                 sprintf(msg, "Your maximum %s usage time has been reached", data->reset);
723                 reply_item=pairmake("Reply-Message", msg, T_OP_EQ);
724                 pairadd(&request->reply->vps, reply_item);
725
726                 snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", data->reset);
727                 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
728                 pairadd(&request->packet->vps, module_fmsg_vp); 
729
730                 ret=RLM_MODULE_REJECT;
731
732                 DEBUG2("rlm_counter: Rejected user %s, check_item=%d, counter=%d",
733                                 key_vp->strvalue,check_vp->lvalue,counter);
734         }
735
736         return ret;
737 }
738
739 static int counter_detach(void *instance)
740 {
741         rlm_counter_t *data = (rlm_counter_t *) instance;
742
743         paircompare_unregister(data->dict_attr, counter_cmp);
744         gdbm_close(data->gdbm);
745         free(data->filename);
746         free(data->reset);
747         free(data->key_name);
748         free(data->count_attribute);
749         free(data->counter_name);
750         pthread_mutex_destroy(&data->mutex);
751
752         free(instance);
753         return 0;
754 }
755
756 /*
757  *      The module name should be the only globally exported symbol.
758  *      That is, everything else should be 'static'.
759  *
760  *      If the module needs to temporarily modify it's instantiation
761  *      data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
762  *      The server will then take care of ensuring that the module
763  *      is single-threaded.
764  */
765 module_t rlm_counter = {
766         "Counter",      
767         RLM_TYPE_THREAD_SAFE,           /* type */
768         NULL,                           /* initialization */
769         counter_instantiate,            /* instantiation */
770         {
771                 NULL,                   /* authentication */
772                 counter_authorize,      /* authorization */
773                 NULL,                   /* preaccounting */
774                 counter_accounting,     /* accounting */
775                 NULL,                   /* checksimul */
776                 NULL,                   /* pre-proxy */
777                 NULL,                   /* post-proxy */
778                 NULL                    /* post-auth */
779         },
780         counter_detach,                 /* detach */
781         NULL,                           /* destroy */
782 };