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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2001 The FreeRADIUS server project
21 * Copyright 2001 Alan DeKok <aland@ox.org>
22 * Copyright 2001 Kostas Kalevras <kkalev@noc.ntua.gr>
27 #include "libradius.h"
40 #ifdef NEEDS_GDBM_SYNC
41 # define GDBM_SYNCOPT GDBM_SYNC
43 # define GDBM_SYNCOPT 0
47 static const char rcsid[] = "$Id$";
50 * Define a structure for our module configuration.
52 * These variables do not need to be in a structure, but it's
53 * a lot cleaner to do so, and a pointer to the structure can
54 * be used as the instance handle.
56 typedef struct rlm_counter_t {
57 char *filename; /* name of the database file */
58 char *reset; /* daily, weekly, monthly */
59 char *key_name; /* User-Name */
60 char *count_attribute; /* Acct-Session-Time */
61 char *counter_name; /* Daily-Session-Time */
62 char *check_name; /* Daily-Max-Session */
63 char *service_type; /* Service-Type to search for */
70 int dict_attr; /* attribute number for the counter. */
75 * A mapping of configuration file names to internal variables.
77 * Note that the string is dynamically allocated, so it MUST
78 * be freed. When the configuration file parse re-reads the string,
79 * it free's the old one, and strdup's the new one, placing the pointer
80 * to the strdup'd string into 'config.string'. This gets around
83 static CONF_PARSER module_config[] = {
84 { "filename", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,filename), NULL, NULL },
85 { "key", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,key_name), NULL, NULL },
86 { "reset", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,reset), NULL, NULL },
87 { "count-attribute", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,count_attribute), NULL, NULL },
88 { "counter-name", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,counter_name), NULL, NULL },
89 { "check-name", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,check_name), NULL, NULL },
90 { "allowed-servicetype", PW_TYPE_STRING_PTR, offsetof(rlm_counter_t,service_type),NULL, NULL },
91 { "cache-size", PW_TYPE_INTEGER, offsetof(rlm_counter_t,cache_size), NULL, "1000" },
92 { NULL, -1, 0, NULL, NULL }
97 * See if the counter matches.
99 static int counter_cmp(void *instance, VALUE_PAIR *request, VALUE_PAIR *check,
100 VALUE_PAIR *check_pairs, VALUE_PAIR **reply_pairs)
102 rlm_counter_t *data = (rlm_counter_t *) instance;
108 check_pairs = check_pairs; /* shut the compiler up */
109 reply_pairs = reply_pairs;
112 * Find the key attribute.
114 key_vp = pairfind(request, data->key_attr);
115 if (key_vp == NULL) {
116 return RLM_MODULE_NOOP;
119 key_datum.dptr = key_vp->strvalue;
120 key_datum.dsize = key_vp->length;
122 count_datum = gdbm_fetch(data->gdbm, key_datum);
123 if (count_datum.dptr == NULL) {
126 memcpy(&counter, count_datum.dptr, sizeof(int));
127 free(count_datum.dptr);
129 return counter - check->lvalue;
133 static int find_next_reset(rlm_counter_t *data, time_t timeval)
138 tm = localtime(&timeval);
139 tm->tm_sec = tm->tm_min = 0;
141 if (strcmp(data->reset, "hourly") == 0) {
143 * Round up to the next nearest hour.
146 data->reset_time = mktime(tm);
147 } else if (strcmp(data->reset, "daily") == 0) {
149 * Round up to the next nearest day.
153 data->reset_time = mktime(tm);
154 } else if (strcmp(data->reset, "weekly") == 0) {
156 * Round up to the next nearest week.
159 tm->tm_mday += (7 - tm->tm_wday);
160 data->reset_time = mktime(tm);
161 } else if (strcmp(data->reset, "monthly") == 0) {
165 data->reset_time = mktime(tm);
167 radlog(L_ERR, "rlm_counter: Unknown reset timer \"%s\"",
177 * Do any per-module initialization that is separate to each
178 * configured instance of the module. e.g. set up connections
179 * to external databases, read configuration files, set up
180 * dictionary entries, etc.
182 * If configuration information is given in the config section
183 * that must be referenced in later calls, store a handle to it
184 * in *instance otherwise put a null pointer there.
186 static int counter_instantiate(CONF_SECTION *conf, void **instance)
195 * Set up a storage area for instance data
197 data = rad_malloc(sizeof(*data));
200 * If the configuration parameters can't be parsed, then
203 if (cf_section_parse(conf, data, module_config) < 0) {
207 cache_size = data->cache_size;
210 * Discover the attribute number of the key.
212 if (data->key_name == NULL) {
213 radlog(L_ERR, "rlm_counter: 'key' must be set.");
216 dattr = dict_attrbyname(data->key_name);
218 radlog(L_ERR, "rlm_counter: No such attribute %s",
222 data->key_attr = dattr->attr;
225 * Discover the attribute number of the counter.
227 if (data->count_attribute == NULL) {
228 radlog(L_ERR, "rlm_counter: 'count-attribute' must be set.");
231 dattr = dict_attrbyname(data->count_attribute);
233 radlog(L_ERR, "rlm_counter: No such attribute %s",
234 data->count_attribute);
237 data->count_attr = dattr->attr;
240 * Create a new attribute for the counter.
242 if (data->counter_name == NULL) {
243 radlog(L_ERR, "rlm_counter: 'counter-name' must be set.");
246 dict_addattr(data->counter_name, 0, PW_TYPE_INTEGER, -1);
247 dattr = dict_attrbyname(data->counter_name);
249 radlog(L_ERR, "rlm_counter: Failed to create counter attribute %s",
253 data->dict_attr = dattr->attr;
254 DEBUG2("rlm_counter: Counter attribute %s is number %d",
255 data->counter_name, data->dict_attr);
258 * Create a new attribute for the check item.
260 if (data->check_name == NULL) {
261 radlog(L_ERR, "rlm_counter: 'check-name' must be set.");
264 dict_addattr(data->check_name, 0, PW_TYPE_INTEGER, -1);
265 dattr = dict_attrbyname(data->check_name);
267 radlog(L_ERR, "rlm_counter: Failed to create check attribute %s",
273 * Find the attribute for the allowed protocol
275 if (data->service_type == NULL) {
276 radlog(L_ERR, "rlm_counter: 'allowed-servicetype' must be set.");
279 if (data->service_type != NULL) {
280 if ((dval = dict_valbyname(PW_SERVICE_TYPE, data->service_type)) == NULL) {
281 radlog(L_ERR, "rlm_counter: Failed to find attribute number for %s",
285 data->service_val = dval->value;
289 * Discover when next to reset the database.
291 if (data->reset == NULL) {
292 radlog(L_ERR, "rlm_counter: 'reset' must be set.");
296 data->reset_time = 0;
298 if (find_next_reset(data,now) == -1)
300 DEBUG2("rlm_counter: Next reset %d", (int)data->reset_time);
302 if (data->filename == NULL) {
303 radlog(L_ERR, "rlm_counter: 'filename' must be set.");
306 data->gdbm = gdbm_open(data->filename, sizeof(int),
307 GDBM_WRCREAT | GDBM_SYNCOPT, 0600, NULL);
308 if (data->gdbm == NULL) {
309 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
310 data->filename, strerror(errno));
313 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
314 radlog(L_ERR, "rlm_counter: Failed to set cache size");
318 * Register the counter comparison operation.
320 paircompare_register(data->dict_attr, 0, counter_cmp, data);
328 * Write accounting information to this modules database.
330 static int counter_accounting(void *instance, REQUEST *request)
332 rlm_counter_t *data = (rlm_counter_t *)instance;
335 VALUE_PAIR *key_vp, *count_vp, *proto_vp;
341 * Before doing anything else, see if we have to reset
344 if (data->reset_time && (data->reset_time <= request->timestamp)) {
345 int cache_size = data->cache_size;
347 gdbm_close(data->gdbm);
350 * Re-set the next time to clean the database.
352 data->last_reset = data->reset_time;
353 find_next_reset(data,request->timestamp);
356 * Open a completely new database.
358 data->gdbm = gdbm_open(data->filename, sizeof(int),
359 GDBM_NEWDB | GDBM_SYNCOPT, 0600, NULL);
360 if (data->gdbm == NULL) {
361 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
362 data->filename, strerror(errno));
363 return RLM_MODULE_FAIL;
365 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
366 radlog(L_ERR, "rlm_counter: Failed to set cache size");
369 * Check if we need to watch out for a specific service-type. If yes then check it
371 if (data->service_type != NULL) {
372 if ((proto_vp = pairfind(request->packet->vps, PW_SERVICE_TYPE)) == NULL)
373 return RLM_MODULE_NOOP;
374 if (proto_vp->lvalue != data->service_val)
375 return RLM_MODULE_NOOP;
381 * Look for the key. User-Name is special. It means
382 * The REAL username, after stripping.
384 key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
386 return RLM_MODULE_NOOP;
389 * Look for the attribute to use as a counter.
391 count_vp = pairfind(request->packet->vps, data->count_attr);
392 if (count_vp == NULL)
393 return RLM_MODULE_NOOP;
395 key_datum.dptr = key_vp->strvalue;
396 key_datum.dsize = key_vp->length;
398 count_datum = gdbm_fetch(data->gdbm, key_datum);
399 if (count_datum.dptr == NULL)
402 memcpy(&counter, count_datum.dptr, sizeof(int));
403 free(count_datum.dptr);
407 * if session time < diff then the user got in after the last reset. So add his session time
409 * That way if he logged in at 23:00 and we reset the daily counter at 24:00 and he logged out
410 * at 01:00 then we will only count one hour (the one in the new day). That is the right thing
413 diff = request->timestamp - data->last_reset;
414 counter += (count_vp->lvalue < diff) ? count_vp->lvalue : diff;
415 count_datum.dptr = (char *) &counter;
416 count_datum.dsize = sizeof(int);
418 rcode = gdbm_store(data->gdbm, key_datum, count_datum, GDBM_REPLACE);
420 radlog(L_ERR, "rlm_counter: Failed storing data to %s: %s",
421 data->filename, gdbm_strerror(gdbm_errno));
422 return RLM_MODULE_FAIL;
425 return RLM_MODULE_OK;
429 * Find the named user in this modules database. Create the set
430 * of attribute-value pairs to check and reply with for this user
431 * from the database. The authentication code only needs to check
432 * the password, the rest is done here.
434 static int counter_authorize(void *instance, REQUEST *request)
436 rlm_counter_t *data = (rlm_counter_t *) instance;
437 int ret=RLM_MODULE_NOOP;
443 VALUE_PAIR *key_vp, *check_vp;
444 VALUE_PAIR *reply_item;
447 /* quiet the compiler */
452 * Before doing anything else, see if we have to reset
455 if (data->reset_time && (data->reset_time <= request->timestamp)) {
456 int cache_size = data->cache_size;
458 gdbm_close(data->gdbm);
461 * Re-set the next time to clean the database.
463 data->last_reset = data->reset_time;
464 find_next_reset(data,request->timestamp);
467 * Open a completely new database.
469 data->gdbm = gdbm_open(data->filename, sizeof(int),
470 GDBM_NEWDB | GDBM_SYNCOPT, 0600, NULL);
471 if (data->gdbm == NULL) {
472 radlog(L_ERR, "rlm_counter: Failed to open file %s: %s",
473 data->filename, strerror(errno));
474 return RLM_MODULE_FAIL;
476 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
477 radlog(L_ERR, "rlm_counter: Failed to set cache size");
482 * Look for the key. User-Name is special. It means
483 * The REAL username, after stripping.
485 DEBUG2("rlm_counter: Entering module authorize code");
486 key_vp = (data->key_attr == PW_USER_NAME) ? request->username : pairfind(request->packet->vps, data->key_attr);
487 if (key_vp == NULL) {
488 DEBUG2("rlm_counter: Could not find Key value pair");
493 * Look for the check item
496 if ((dattr = dict_attrbyname(data->check_name)) == NULL)
498 if ((check_vp= pairfind(request->config_items, dattr->attr)) == NULL) {
499 DEBUG2("rlm_counter: Could not find Check item value pair");
503 key_datum.dptr = key_vp->strvalue;
504 key_datum.dsize = key_vp->length;
506 count_datum = gdbm_fetch(data->gdbm, key_datum);
507 if (count_datum.dptr != NULL){
508 memcpy(&counter, count_datum.dptr, sizeof(int));
509 free(count_datum.dptr);
514 * Check if check item > counter
516 res=check_vp->lvalue - counter;
519 * We are assuming that simultaneous-use=1. But even if that does
520 * not happen then our user could login at max for 2*max-usage-time
521 * Is that acceptable?
525 * User is allowed, but set Session-Timeout.
526 * Stolen from main/auth.c
530 * If we are near a reset then add the next limit, so that the user will
531 * not need to login again
534 if (res >= (data->reset_time - request->timestamp))
535 res += check_vp->lvalue;
537 DEBUG2("rlm_counter: (Check item - counter) is greater than zero");
538 if ((reply_item = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL) {
539 if (reply_item->lvalue > res)
540 reply_item->lvalue = res;
542 if ((reply_item = paircreate(PW_SESSION_TIMEOUT, PW_TYPE_INTEGER)) == NULL) {
543 radlog(L_ERR|L_CONS, "no memory");
544 return RLM_MODULE_NOOP;
546 reply_item->lvalue = res;
547 pairadd(&request->reply->vps, reply_item);
552 DEBUG2("rlm_counter: Authorized user %s, check_item=%d, counter=%d",
553 key_vp->strvalue,check_vp->lvalue,counter);
554 DEBUG2("rlm_counter: Sent Reply-Item for user %s, Type=Session-Timeout, value=%d",
555 key_vp->strvalue,res);
558 char module_msg[MAX_STRING_LEN];
559 VALUE_PAIR *module_msg_vp;
562 * User is denied access, send back a reply message
564 sprintf(msg, "Your maximum %s usage time has been reached", data->reset);
565 reply_item=pairmake("Reply-Message", msg, T_OP_EQ);
566 pairadd(&request->reply->vps, reply_item);
568 snprintf(module_msg, sizeof(module_msg), "rlm_counter: Maximum %s usage time reached", data->reset);
569 module_msg_vp = pairmake("Module-Message", module_msg, T_OP_EQ);
570 pairadd(&request->packet->vps, module_msg_vp);
572 ret=RLM_MODULE_REJECT;
574 DEBUG2("rlm_counter: Rejected user %s, check_item=%d, counter=%d",
575 key_vp->strvalue,check_vp->lvalue,counter);
581 static int counter_detach(void *instance)
583 rlm_counter_t *data = (rlm_counter_t *) instance;
585 paircompare_unregister(data->dict_attr, counter_cmp);
586 gdbm_close(data->gdbm);
587 free(data->filename);
589 free(data->key_name);
590 free(data->count_attribute);
591 free(data->counter_name);
598 * The module name should be the only globally exported symbol.
599 * That is, everything else should be 'static'.
601 * If the module needs to temporarily modify it's instantiation
602 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
603 * The server will then take care of ensuring that the module
604 * is single-threaded.
606 module_t rlm_counter = {
608 RLM_TYPE_THREAD_UNSAFE, /* type */
609 NULL, /* initialization */
610 counter_instantiate, /* instantiation */
612 NULL, /* authentication */
613 counter_authorize, /* authorization */
614 NULL, /* preaccounting */
615 counter_accounting, /* accounting */
616 NULL /* checksimul */
618 counter_detach, /* detach */