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