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