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