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.
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.
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
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>
25 #include <freeradius-devel/ident.h>
29 #include <freeradius-devel/autoconf.h>
36 #include <freeradius-devel/radiusd.h>
37 #include <freeradius-devel/modules.h>
41 #ifdef NEEDS_GDBM_SYNC
42 # define GDBM_SYNCOPT GDBM_SYNC
44 # define GDBM_SYNCOPT 0
48 #define GDBM_COUNTER_OPTS (GDBM_SYNCOPT | GDBM_NOLOCK)
50 #define GDBM_COUNTER_OPTS (GDBM_SYNCOPT)
53 #ifndef HAVE_GDBM_FDESC
54 #define gdbm_fdesc(foo) (-1)
57 #define UNIQUEID_MAX_LEN 32
60 * Define a structure for our module configuration.
62 * These variables do not need to be in a structure, but it's
63 * a lot cleaner to do so, and a pointer to the structure can
64 * be used as the instance handle.
66 typedef struct rlm_counter_t {
67 char *filename; /* name of the database file */
68 char *reset; /* daily, weekly, monthly, never or user defined */
69 char *key_name; /* User-Name */
70 char *count_attribute; /* Acct-Session-Time */
71 char *counter_name; /* Daily-Session-Time */
72 char *check_name; /* Daily-Max-Session */
73 char *reply_name; /* Session-Timeout */
74 char *service_type; /* Service-Type to search for */
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 */
86 pthread_mutex_t mutex; /* A mutex to lock the gdbm file for only one reader/writer */
90 #ifndef HAVE_PTHREAD_H
92 * This is a lot simpler than putting ifdef's around
93 * every use of the pthread functions.
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)
101 typedef struct rad_counter {
102 unsigned int user_counter;
103 char uniqueid[UNIQUEID_MAX_LEN];
107 * A mapping of configuration file names to internal variables.
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
115 static const 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 { "reply-name", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,reply_name), NULL, NULL },
123 { "allowed-servicetype", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,service_type),NULL, NULL },
124 { "cache-size", PW_TYPE_INTEGER, offsetof(rlm_counter_t,cache_size), NULL, "1000" },
125 { NULL, -1, 0, NULL, NULL }
128 static int counter_detach(void *instance);
132 * See if the counter matches.
134 static int counter_cmp(void *instance,
136 VALUE_PAIR *request, VALUE_PAIR *check,
137 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
139 rlm_counter_t *data = (rlm_counter_t *) instance;
145 check_pairs = check_pairs; /* shut the compiler up */
146 reply_pairs = reply_pairs;
150 * Find the key attribute.
152 key_vp = pairfind(request, data->key_attr);
153 if (key_vp == NULL) {
154 return RLM_MODULE_NOOP;
157 key_datum.dptr = key_vp->vp_strvalue;
158 key_datum.dsize = key_vp->length;
160 count_datum = gdbm_fetch(data->gdbm, key_datum);
162 if (count_datum.dptr == NULL) {
165 memcpy(&counter, count_datum.dptr, sizeof(rad_counter));
166 free(count_datum.dptr);
168 return counter.user_counter - check->lvalue;
172 static int add_defaults(rlm_counter_t *data)
176 const char *default1 = "DEFAULT1";
177 const char *default2 = "DEFAULT2";
179 DEBUG2("rlm_counter: add_defaults: Start");
181 key_datum.dptr = (char *) default1;
182 key_datum.dsize = strlen(default1);
183 time_datum.dptr = (char *) &data->reset_time;
184 time_datum.dsize = sizeof(time_t);
186 if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
187 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
188 data->filename, gdbm_strerror(gdbm_errno));
189 return RLM_MODULE_FAIL;
191 DEBUG2("rlm_counter: DEFAULT1 set to %d",(int)data->reset_time);
193 key_datum.dptr = (char *) default2;
194 key_datum.dsize = strlen(default2);
195 time_datum.dptr = (char *) &data->last_reset;
196 time_datum.dsize = sizeof(time_t);
198 if (gdbm_store(data->gdbm, key_datum, time_datum, GDBM_REPLACE) < 0){
199 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
200 data->filename, gdbm_strerror(gdbm_errno));
201 return RLM_MODULE_FAIL;
203 DEBUG2("rlm_counter: DEFAULT2 set to %d",(int)data->last_reset);
204 DEBUG2("rlm_counter: add_defaults: End");
206 return RLM_MODULE_OK;
209 static int reset_db(rlm_counter_t *data)
211 int cache_size = data->cache_size;
214 DEBUG2("rlm_counter: reset_db: Closing database");
215 gdbm_close(data->gdbm);
218 * Open a completely new database.
220 data->gdbm = gdbm_open(data->filename, sizeof(int),
221 GDBM_NEWDB | GDBM_COUNTER_OPTS, 0600, NULL);
222 if (data->gdbm == NULL) {
223 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
224 data->filename, strerror(errno));
225 return RLM_MODULE_FAIL;
227 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
228 radlog(L_ERR, "rlm_counter: Failed to set cache size");
229 DEBUG2("rlm_counter: reset_db: Opened new database");
234 ret = add_defaults(data);
235 if (ret != RLM_MODULE_OK)
238 DEBUG2("rlm_counter: reset_db ended");
240 return RLM_MODULE_OK;
243 static int find_next_reset(rlm_counter_t *data, time_t timeval)
247 unsigned int num = 1;
250 char sCurrentTime[40], sNextTime[40];
252 tm = localtime_r(&timeval, &s_tm);
253 len = strftime(sCurrentTime, sizeof(sCurrentTime), "%Y-%m-%d %H:%M:%S", tm);
254 if (len == 0) *sCurrentTime = '\0';
255 tm->tm_sec = tm->tm_min = 0;
257 if (data->reset == NULL)
259 if (isdigit((int) data->reset[0])){
260 len = strlen(data->reset);
263 last = data->reset[len - 1];
264 if (!isalpha((int) last))
266 num = atoi(data->reset);
267 DEBUG("rlm_counter: num=%d, last=%c",num,last);
269 if (strcmp(data->reset, "hourly") == 0 || last == 'h') {
271 * Round up to the next nearest hour.
274 data->reset_time = mktime(tm);
275 } else if (strcmp(data->reset, "daily") == 0 || last == 'd') {
277 * Round up to the next nearest day.
281 data->reset_time = mktime(tm);
282 } else if (strcmp(data->reset, "weekly") == 0 || last == 'w') {
284 * Round up to the next nearest week.
287 tm->tm_mday += (7 - tm->tm_wday) +(7*(num-1));
288 data->reset_time = mktime(tm);
289 } else if (strcmp(data->reset, "monthly") == 0 || last == 'm') {
293 data->reset_time = mktime(tm);
294 } else if (strcmp(data->reset, "never") == 0) {
295 data->reset_time = 0;
297 radlog(L_ERR, "rlm_counter: Unknown reset timer \"%s\"",
302 len = strftime(sNextTime, sizeof(sNextTime), "%Y-%m-%d %H:%M:%S", tm);
303 if (len == 0) *sNextTime = '\0';
304 DEBUG2("rlm_counter: Current Time: %li [%s], Next reset %li [%s]",
305 timeval, sCurrentTime, data->reset_time, sNextTime);
312 * Do any per-module initialization that is separate to each
313 * configured instance of the module. e.g. set up connections
314 * to external databases, read configuration files, set up
315 * dictionary entries, etc.
317 * If configuration information is given in the config section
318 * that must be referenced in later calls, store a handle to it
319 * in *instance otherwise put a null pointer there.
321 static int counter_instantiate(CONF_SECTION *conf, void **instance)
332 const char *default1 = "DEFAULT1";
333 const char *default2 = "DEFAULT2";
336 * Set up a storage area for instance data
338 data = rad_malloc(sizeof(*data));
340 radlog(L_ERR, "rlm_counter: rad_malloc() failed.");
343 memset(data, 0, sizeof(*data));
346 * If the configuration parameters can't be parsed, then
349 if (cf_section_parse(conf, data, module_config) < 0) {
353 cache_size = data->cache_size;
356 * Discover the attribute number of the key.
358 if (data->key_name == NULL) {
359 radlog(L_ERR, "rlm_counter: 'key' must be set.");
360 counter_detach(data);
363 dattr = dict_attrbyname(data->key_name);
365 radlog(L_ERR, "rlm_counter: No such attribute %s",
367 counter_detach(data);
370 data->key_attr = dattr->attr;
373 * Discover the attribute number of the counter.
375 if (data->count_attribute == NULL) {
376 radlog(L_ERR, "rlm_counter: 'count-attribute' must be set.");
377 counter_detach(data);
380 dattr = dict_attrbyname(data->count_attribute);
382 radlog(L_ERR, "rlm_counter: No such attribute %s",
383 data->count_attribute);
384 counter_detach(data);
387 data->count_attr = dattr->attr;
390 * Discover the attribute number of the reply attribute.
392 if (data->reply_name != NULL) {
393 dattr = dict_attrbyname(data->reply_name);
395 radlog(L_ERR, "rlm_counter: No such attribute %s",
397 counter_detach(data);
400 if (dattr->type != PW_TYPE_INTEGER) {
401 radlog(L_ERR, "rlm_counter: Reply attribute %s is not of type integer",
403 counter_detach(data);
406 data->reply_attr = dattr->attr;
411 * Create a new attribute for the counter.
413 if (data->counter_name == NULL) {
414 radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
415 counter_detach(data);
419 memset(&flags, 0, sizeof(flags));
420 dict_addattr(data->counter_name, 0, PW_TYPE_INTEGER, -1, flags);
421 dattr = dict_attrbyname(data->counter_name);
423 radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
425 counter_detach(data);
428 data->dict_attr = dattr->attr;
429 DEBUG2("rlm_counter: Counter attribute %s is number %d",
430 data->counter_name, data->dict_attr);
433 * Create a new attribute for the check item.
435 if (data->check_name == NULL) {
436 radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
437 counter_detach(data);
440 dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1, flags);
441 dattr = dict_attrbyname(data->check_name);
443 radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
445 counter_detach(data);
448 data->check_attr = dattr->attr;
451 * Find the attribute for the allowed protocol
453 if (data->service_type != NULL) {
454 if ((dval = dict_valbyname(PW_SERVICE_TYPE, data->service_type)) == NULL) {
455 radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
457 counter_detach(data);
460 data->service_val = dval->value;
464 * Find when to reset the database.
466 if (data->reset == NULL) {
467 radlog(L_ERR, "rlm_counter: 'reset' must be set.");
468 counter_detach(data);
472 data->reset_time = 0;
473 data->last_reset = now;
475 if (find_next_reset(data,now) == -1){
476 radlog(L_ERR, "rlm_counter: find_next_reset() returned -1. Exiting.");
477 counter_detach(data);
481 if (data->filename == NULL) {
482 radlog(L_ERR, "rlm_counter: 'filename' must be set.");
483 counter_detach(data);
486 data->gdbm = gdbm_open(data->filename, sizeof(int),
487 GDBM_WRCREAT | GDBM_COUNTER_OPTS, 0600, NULL);
488 if (data->gdbm == NULL) {
489 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
490 data->filename, strerror(errno));
491 counter_detach(data);
494 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
495 radlog(L_ERR, "rlm_counter: Failed to set cache size");
498 * Look for the DEFAULT1 entry. This entry if it exists contains the
499 * time of the next database reset. This time is set each time we reset
500 * the database. If next_reset < now then we reset the database.
501 * That way we can overcome the problem where radiusd is down during a database
502 * reset time. If we did not keep state information in the database then the reset
503 * would be extended and that would create problems.
505 * We also store the time of the last reset in the DEFAULT2 entry.
507 * If DEFAULT1 and DEFAULT2 do not exist (new database) we add them to the database
510 key_datum.dptr = (char *)default1;
511 key_datum.dsize = strlen(default1);
513 time_datum = gdbm_fetch(data->gdbm, key_datum);
514 if (time_datum.dptr != NULL){
515 time_t next_reset = 0;
517 memcpy(&next_reset, time_datum.dptr, sizeof(time_t));
518 free(time_datum.dptr);
519 if (next_reset && next_reset <= now){
521 data->last_reset = now;
522 ret = reset_db(data);
523 if (ret != RLM_MODULE_OK){
524 radlog(L_ERR, "rlm_counter: reset_db() failed");
525 counter_detach(data);
530 data->reset_time = next_reset;
531 key_datum.dptr = (char *)default2;
532 key_datum.dsize = strlen(default2);
534 time_datum = gdbm_fetch(data->gdbm, key_datum);
535 if (time_datum.dptr != NULL){
536 memcpy(&data->last_reset, time_datum.dptr, sizeof(time_t));
537 free(time_datum.dptr);
541 ret = add_defaults(data);
542 if (ret != RLM_MODULE_OK){
543 radlog(L_ERR, "rlm_counter: add_defaults() failed");
544 counter_detach(data);
551 * Register the counter comparison operation.
553 paircompare_register(data->dict_attr, 0, counter_cmp, data);
558 pthread_mutex_init(&data->mutex, NULL);
566 * Write accounting information to this modules database.
568 static int counter_accounting(void *instance, REQUEST *request)
570 rlm_counter_t *data = (rlm_counter_t *)instance;
573 VALUE_PAIR *key_vp, *count_vp, *proto_vp, *uniqueid_vp;
576 int acctstatustype = 0;
579 if ((key_vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL)
580 acctstatustype = key_vp->lvalue;
582 DEBUG("rlm_counter: Could not find account status type in packet.");
583 return RLM_MODULE_NOOP;
585 if (acctstatustype != PW_STATUS_STOP){
586 DEBUG("rlm_counter: We only run on Accounting-Stop packets.");
587 return RLM_MODULE_NOOP;
589 uniqueid_vp = pairfind(request->packet->vps, PW_ACCT_UNIQUE_SESSION_ID);
590 if (uniqueid_vp != NULL)
591 DEBUG("rlm_counter: Packet Unique ID = '%s'",uniqueid_vp->vp_strvalue);
594 * Before doing anything else, see if we have to reset
597 if (data->reset_time && (data->reset_time <= request->timestamp)) {
600 DEBUG("rlm_counter: Time to reset the database.");
601 data->last_reset = data->reset_time;
602 find_next_reset(data,request->timestamp);
603 pthread_mutex_lock(&data->mutex);
604 ret = reset_db(data);
605 pthread_mutex_unlock(&data->mutex);
606 if (ret != RLM_MODULE_OK)
610 * Check if we need to watch out for a specific service-type. If yes then check it
612 if (data->service_type != NULL) {
613 if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE)) == NULL){
614 DEBUG("rlm_counter: Could not find Service-Type attribute in the request. Returning NOOP.");
615 return RLM_MODULE_NOOP;
617 if ((unsigned)proto_vp->lvalue != data->service_val){
618 DEBUG("rlm_counter: This Service-Type is not allowed. Returning NOOP.");
619 return RLM_MODULE_NOOP;
623 * Check if request->timestamp - {Acct-Delay-Time} < last_reset
624 * If yes reject the packet since it is very old
626 key_vp = pairfind(request->packet->vps, PW_ACCT_DELAY_TIME);
628 if (key_vp->lvalue != 0 &&
629 (request->timestamp - key_vp->lvalue) < data->last_reset){
630 DEBUG("rlm_counter: This packet is too old. Returning NOOP.");
631 return RLM_MODULE_NOOP;
638 * Look for the key. User-Name is special. It means
639 * The REAL username, after stripping.
641 key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
643 DEBUG("rlm_counter: Could not find the key-attribute in the request. Returning NOOP.");
644 return RLM_MODULE_NOOP;
648 * Look for the attribute to use as a counter.
650 count_vp = pairfind(request->packet->vps, data->count_attr);
651 if (count_vp == NULL){
652 DEBUG("rlm_counter: Could not find the count-attribute in the request.");
653 return RLM_MODULE_NOOP;
656 key_datum.dptr = key_vp->vp_strvalue;
657 key_datum.dsize = key_vp->length;
659 DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->vp_strvalue);
660 pthread_mutex_lock(&data->mutex);
661 count_datum = gdbm_fetch(data->gdbm, key_datum);
662 pthread_mutex_unlock(&data->mutex);
663 if (count_datum.dptr == NULL){
664 DEBUG("rlm_counter: Could not find the requested key in the database.");
665 counter.user_counter = 0;
666 if (uniqueid_vp != NULL)
667 strlcpy(counter.uniqueid,uniqueid_vp->vp_strvalue,UNIQUEID_MAX_LEN - 1);
669 memset((char *)counter.uniqueid,0,UNIQUEID_MAX_LEN);
672 DEBUG("rlm_counter: Key found.");
673 memcpy(&counter, count_datum.dptr, sizeof(rad_counter));
674 free(count_datum.dptr);
675 if (counter.uniqueid)
676 DEBUG("rlm_counter: Counter Unique ID = '%s'",counter.uniqueid);
677 if (uniqueid_vp != NULL){
678 if (counter.uniqueid != NULL &&
679 strncmp(uniqueid_vp->vp_strvalue,counter.uniqueid, UNIQUEID_MAX_LEN - 1) == 0){
680 DEBUG("rlm_counter: Unique IDs for user match. Droping the request.");
681 return RLM_MODULE_NOOP;
683 strlcpy(counter.uniqueid,uniqueid_vp->vp_strvalue,UNIQUEID_MAX_LEN - 1);
685 DEBUG("rlm_counter: User=%s, Counter=%d.",request->username->vp_strvalue,counter.user_counter);
688 if (data->count_attr == PW_ACCT_SESSION_TIME) {
690 * If session time < diff then the user got in after the
691 * last reset. So add his session time, otherwise add the
694 * That way if he logged in at 23:00 and we reset the
695 * daily counter at 24:00 and he logged out at 01:00
696 * then we will only count one hour (the one in the new
697 * day). That is the right thing
699 diff = request->timestamp - data->last_reset;
700 counter.user_counter += (count_vp->lvalue < diff) ? count_vp->lvalue : diff;
702 } else if (count_vp->type == PW_TYPE_INTEGER) {
704 * Integers get counted, without worrying about
707 counter.user_counter += count_vp->lvalue;
711 * The attribute is NOT an integer, just count once
712 * more that we've seen it.
714 counter.user_counter++;
717 DEBUG("rlm_counter: User=%s, New Counter=%d.",request->username->vp_strvalue,counter.user_counter);
718 count_datum.dptr = (char *) &counter;
719 count_datum.dsize = sizeof(rad_counter);
721 DEBUG("rlm_counter: Storing new value in database.");
722 pthread_mutex_lock(&data->mutex);
723 rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);
724 pthread_mutex_unlock(&data->mutex);
726 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
727 data->filename, gdbm_strerror(gdbm_errno));
728 return RLM_MODULE_FAIL;
730 DEBUG("rlm_counter: New value stored successfully.");
732 return RLM_MODULE_OK;
736 * Find the named user in this modules database. Create the set
737 * of attribute-value pairs to check and reply with for this user
738 * from the database. The authentication code only needs to check
739 * the password, the rest is done here.
741 static int counter_authorize(void *instance, REQUEST *request)
743 rlm_counter_t *data = (rlm_counter_t *) instance;
744 int ret=RLM_MODULE_NOOP;
749 VALUE_PAIR *key_vp, *check_vp;
750 VALUE_PAIR *reply_item;
753 /* quiet the compiler */
758 * Before doing anything else, see if we have to reset
761 if (data->reset_time && (data->reset_time <= request->timestamp)) {
764 data->last_reset = data->reset_time;
765 find_next_reset(data,request->timestamp);
766 pthread_mutex_lock(&data->mutex);
767 ret2 = reset_db(data);
768 pthread_mutex_unlock(&data->mutex);
769 if (ret2 != RLM_MODULE_OK)
775 * Look for the key. User-Name is special. It means
776 * The REAL username, after stripping.
778 DEBUG2("rlm_counter: Entering module authorize code");
779 key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
780 if (key_vp == NULL) {
781 DEBUG2("rlm_counter: Could not find Key value pair");
786 * Look for the check item
788 if ((check_vp= pairfind(request->config_items, data->check_attr)) == NULL) {
789 DEBUG2("rlm_counter: Could not find Check item value pair");
793 key_datum.dptr = key_vp->vp_strvalue;
794 key_datum.dsize = key_vp->length;
801 counter.user_counter = 0;
803 DEBUG("rlm_counter: Searching the database for key '%s'",key_vp->vp_strvalue);
804 pthread_mutex_lock(&data->mutex);
805 count_datum = gdbm_fetch(data->gdbm, key_datum);
806 pthread_mutex_unlock(&data->mutex);
807 if (count_datum.dptr != NULL){
808 DEBUG("rlm_counter: Key Found.");
809 memcpy(&counter, count_datum.dptr, sizeof(rad_counter));
810 free(count_datum.dptr);
813 DEBUG("rlm_counter: Could not find the requested key in the database.");
816 * Check if check item > counter
818 DEBUG("rlm_counter: Check item = %d, Count = %d",check_vp->lvalue,counter.user_counter);
819 res=check_vp->lvalue - counter.user_counter;
821 DEBUG("rlm_counter: res is greater than zero");
822 if (data->count_attr == PW_ACCT_SESSION_TIME) {
824 * Do the following only if the count attribute is
829 * We are assuming that simultaneous-use=1. But
830 * even if that does not happen then our user
831 * could login at max for 2*max-usage-time Is
836 * User is allowed, but set Session-Timeout.
837 * Stolen from main/auth.c
841 * If we are near a reset then add the next
842 * limit, so that the user will not need to
844 * Before that set the return value to the time
845 * remaining to next reset
847 if (data->reset_time && (
848 res >= (data->reset_time - request->timestamp))) {
849 res = data->reset_time - request->timestamp;
850 res += check_vp->lvalue;
853 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
854 if (reply_item->lvalue > res)
855 reply_item->lvalue = res;
857 if ((reply_item = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
858 radlog(L_ERR|L_CONS, "no memory");
859 return RLM_MODULE_NOOP;
861 reply_item->lvalue = res;
862 pairadd(&request->reply->vps, reply_item);
865 else if (data->reply_attr) {
866 if ((reply_item = pairfind(request->reply->vps, data->reply_attr)) != NULL) {
867 if (reply_item->lvalue > res)
868 reply_item->lvalue = res;
871 if ((reply_item = paircreate(data->reply_attr, PW_TYPE_INTEGER)) == NULL) {
872 radlog(L_ERR|L_CONS, "no memory");
873 return RLM_MODULE_NOOP;
875 reply_item->lvalue = res;
876 pairadd(&request->reply->vps, reply_item);
882 DEBUG2("rlm_counter: (Check item - counter) is greater than zero");
883 DEBUG2("rlm_counter: Authorized user %s, check_item=%d, counter=%d",
884 key_vp->vp_strvalue,check_vp->lvalue,counter.user_counter);
885 DEBUG2("rlm_counter: Sent Reply-Item for user %s, Type=Session-Timeout, value=%d",
886 key_vp->vp_strvalue,res);
889 char module_fmsg[MAX_STRING_LEN];
890 VALUE_PAIR *module_fmsg_vp;
893 * User is denied access, send back a reply message
895 sprintf(msg, "Your maximum %s usage time has been reached", data->reset);
896 reply_item=pairmake("Reply-Message", msg, T_OP_EQ);
897 pairadd(&request->reply->vps, reply_item);
899 snprintf(module_fmsg,sizeof(module_fmsg), "rlm_counter: Maximum %s usage time reached", data->reset);
900 module_fmsg_vp = pairmake("Module-Failure-Message", module_fmsg, T_OP_EQ);
901 pairadd(&request->packet->vps, module_fmsg_vp);
903 ret=RLM_MODULE_REJECT;
905 DEBUG2("rlm_counter: Rejected user %s, check_item=%d, counter=%d",
906 key_vp->vp_strvalue,check_vp->lvalue,counter.user_counter);
912 static int counter_detach(void *instance)
914 rlm_counter_t *data = (rlm_counter_t *) instance;
916 paircompare_unregister(data->dict_attr, counter_cmp);
918 gdbm_close(data->gdbm);
919 pthread_mutex_destroy(&data->mutex);
926 * The module name should be the only globally exported symbol.
927 * That is, everything else should be 'static'.
929 * If the module needs to temporarily modify it's instantiation
930 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
931 * The server will then take care of ensuring that the module
932 * is single-threaded.
934 module_t rlm_counter = {
937 RLM_TYPE_THREAD_SAFE, /* type */
938 counter_instantiate, /* instantiation */
939 counter_detach, /* detach */
941 NULL, /* authentication */
942 counter_authorize, /* authorization */
943 NULL, /* preaccounting */
944 counter_accounting, /* accounting */
945 NULL, /* checksimul */
946 NULL, /* pre-proxy */
947 NULL, /* post-proxy */