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