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 2002 Kostas Kalevras <kkalev@noc.ntua.gr>
23 * March 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
25 * April 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
26 * - Add support for the Pool-Name attribute
27 * May 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
28 * - Check the return value of a gdbm_fetch() we didn't check
29 * - Change the nas entry in the ippool_key structure from uint32 to string[64]
30 * That should allow us to also use the NAS-Identifier attribute
31 * Sep 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
32 * - Move from authorize to post-auth
33 * - Use mutex locks when accessing the gdbm files
34 * - Fail if we don't find nas port information
35 * Oct 2002, Kostas Kalevras <kkalev@noc.ntua.gr>
36 * - Do a memset(0) on the key.nas before doing searches. Nusty bug
37 * Jul 2003, Kostas Kalevras <kkalev@noc.ntua.gr>
38 * - Make Multilink work this time
39 * - Instead of locking file operations, lock transactions. That means we only keep
40 * one big transaction lock instead of per file locks (mutexes).
41 * Sep 2003, Kostas Kalevras <kkalev@noc.ntua.gr>
42 * - Fix postauth to not leak ip's
43 * Add an extra attribute in each entry <char extra> signifying if we need to delete this
44 * entry in the accounting phase. This is only true in case we are doing MPPP
45 * Various other code changes. Code comments should explain things
46 * Highly experimental at this phase.
47 * Mar 2004, Kostas Kalevras <kkalev@noc.ntua.gr>
48 * - Add a timestamp and a timeout attribute in ippool_info. When we assign an ip we set timestamp
49 * to request->timestamp and timeout to %{Session-Timeout:-0}. When we search for a free entry
50 * we check if timeout has expired. If it has then we free the entry. We also add a maximum
51 * timeout configuration directive. If it is non zero then we also use that one to free entries.
62 #ifdef HAVE_SYS_TYPES_H
63 #include <sys/types.h>
70 #ifdef HAVE_INTTYPES_H
74 #ifdef HAVE_NETINET_IN_H
75 #include <netinet/in.h>
85 #ifdef NEEDS_GDBM_SYNC
86 # define GDBM_SYNCOPT GDBM_SYNC
88 # define GDBM_SYNCOPT 0
92 #define GDBM_IPPOOL_OPTS (GDBM_SYNCOPT | GDBM_NOLOCK)
94 #define GDBM_IPPOOL_OPTS (GDBM_SYNCOPT)
97 #define MAX_NAS_NAME_SIZE 64
99 static const char rcsid[] = "$Id$";
102 * Define a structure for our module configuration.
104 * These variables do not need to be in a structure, but it's
105 * a lot cleaner to do so, and a pointer to the structure can
106 * be used as the instance handle.
108 typedef struct rlm_ippool_t {
112 uint32_t range_start;
120 #ifdef HAVE_PTHREAD_H
121 pthread_mutex_t op_mutex;
125 #ifndef HAVE_PTHREAD_H
127 * This is easier than ifdef's throughout the code.
129 #define pthread_mutex_init(_x, _y)
130 #define pthread_mutex_destroy(_x)
131 #define pthread_mutex_lock(_x)
132 #define pthread_mutex_unlock(_x)
135 typedef struct ippool_info {
144 typedef struct ippool_key {
145 char nas[MAX_NAS_NAME_SIZE];
150 * A mapping of configuration file names to internal variables.
152 * Note that the string is dynamically allocated, so it MUST
153 * be freed. When the configuration file parse re-reads the string,
154 * it free's the old one, and strdup's the new one, placing the pointer
155 * to the strdup'd string into 'config.string'. This gets around
158 static CONF_PARSER module_config[] = {
159 { "session-db", PW_TYPE_STRING_PTR, offsetof(rlm_ippool_t,session_db), NULL, NULL },
160 { "ip-index", PW_TYPE_STRING_PTR, offsetof(rlm_ippool_t,ip_index), NULL, NULL },
161 { "range-start", PW_TYPE_IPADDR, offsetof(rlm_ippool_t,range_start), NULL, "0" },
162 { "range-stop", PW_TYPE_IPADDR, offsetof(rlm_ippool_t,range_stop), NULL, "0" },
163 { "netmask", PW_TYPE_IPADDR, offsetof(rlm_ippool_t,netmask), NULL, "0" },
164 { "cache-size", PW_TYPE_INTEGER, offsetof(rlm_ippool_t,cache_size), NULL, "1000" },
165 { "override", PW_TYPE_BOOLEAN, offsetof(rlm_ippool_t,override), NULL, "no" },
166 { "maximum-timeout", PW_TYPE_INTEGER, offsetof(rlm_ippool_t,max_timeout), NULL, "0" },
167 { NULL, -1, 0, NULL, NULL }
172 * Do any per-module initialization that is separate to each
173 * configured instance of the module. e.g. set up connections
174 * to external databases, read configuration files, set up
175 * dictionary entries, etc.
177 * If configuration information is given in the config section
178 * that must be referenced in later calls, store a handle to it
179 * in *instance otherwise put a null pointer there.
181 static int ippool_instantiate(CONF_SECTION *conf, void **instance)
191 const char *cli = "0";
192 char *pool_name = NULL;
195 * Set up a storage area for instance data
197 data = rad_malloc(sizeof(*data));
201 memset(data, 0, sizeof(*data));
204 * If the configuration parameters can't be parsed, then
207 if (cf_section_parse(conf, data, module_config) < 0) {
211 cache_size = data->cache_size;
213 if (data->session_db == NULL) {
214 radlog(L_ERR, "rlm_ippool: 'session-db' must be set.");
218 if (data->ip_index == NULL) {
219 radlog(L_ERR, "rlm_ippool: 'ip-index' must be set.");
223 data->range_start = htonl(data->range_start);
224 data->range_stop = htonl(data->range_stop);
225 data->netmask = htonl(data->netmask);
226 if (data->range_start == 0 || data->range_stop == 0 || \
227 data->range_start >= data->range_stop ) {
228 radlog(L_ERR, "rlm_ippool: Invalid configuration data given.");
233 data->gdbm = gdbm_open(data->session_db, sizeof(int),
234 GDBM_WRCREAT | GDBM_IPPOOL_OPTS, 0600, NULL);
235 if (data->gdbm == NULL) {
236 radlog(L_ERR, "rlm_ippool: Failed to open file %s: %s",
237 data->session_db, strerror(errno));
240 data->ip = gdbm_open(data->ip_index, sizeof(int),
241 GDBM_WRCREAT | GDBM_IPPOOL_OPTS, 0600, NULL);
242 if (data->ip == NULL) {
243 radlog(L_ERR, "rlm_ippool: Failed to open file %s: %s",
244 data->ip_index, strerror(errno));
247 if (gdbm_setopt(data->gdbm, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
248 radlog(L_ERR, "rlm_ippool: Failed to set cache size");
249 if (gdbm_setopt(data->ip, GDBM_CACHESIZE, &cache_size, sizeof(int)) == -1)
250 radlog(L_ERR, "rlm_ippool: Failed to set cache size");
252 key_datum = gdbm_firstkey(data->gdbm);
253 if (key_datum.dptr == NULL){
255 * If the database does not exist initialize it.
256 * We set the nas/port pairs to not existent values and
262 const char *nas_init = "NOT_EXIST";
264 DEBUG("rlm_ippool: Initializing database");
265 for(i=data->range_start,j=~0;i<=data->range_stop;i++,j--){
268 * Net and Broadcast addresses are excluded
270 or_result = i | data->netmask;
271 if (~data->netmask != 0 &&
272 (or_result == data->netmask ||
273 (~or_result == 0))) {
274 DEBUG("rlm_ippool: IP %s excluded",
275 ip_ntoa(str, ntohl(i)));
279 strcpy(key.nas, nas_init);
281 key_datum.dptr = (char *) &key;
282 key_datum.dsize = sizeof(ippool_key);
284 entry.ipaddr = ntohl(i);
289 strcpy(entry.cli,cli);
291 data_datum.dptr = (char *) &entry;
292 data_datum.dsize = sizeof(ippool_info);
294 rcode = gdbm_store(data->gdbm, key_datum, data_datum, GDBM_REPLACE);
296 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
297 data->session_db, gdbm_strerror(gdbm_errno));
299 gdbm_close(data->gdbm);
300 gdbm_close(data->ip);
306 free(key_datum.dptr);
308 /* Add the ip pool name */
310 pool_name = cf_section_name2(conf);
311 if (pool_name != NULL)
312 data->name = strdup(pool_name);
314 pthread_mutex_init(&data->op_mutex, NULL);
322 * Check for an Accounting-Stop
323 * If we find one and we have allocated an IP to this nas/port combination, deallocate it.
325 static int ippool_accounting(void *instance, REQUEST *request)
327 rlm_ippool_t *data = (rlm_ippool_t *)instance;
331 int acctstatustype = 0;
332 unsigned int port = ~0;
334 char nas[MAX_NAS_NAME_SIZE];
342 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) != NULL)
343 acctstatustype = vp->lvalue;
345 DEBUG("rlm_ippool: Could not find account status type in packet. Return NOOP.");
346 return RLM_MODULE_NOOP;
348 switch(acctstatustype){
350 if ((vp = pairfind(request->packet->vps, PW_NAS_PORT)) != NULL)
353 DEBUG("rlm_ippool: Could not find port number in packet. Return NOOP.");
354 return RLM_MODULE_NOOP;
356 if ((vp = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS)) != NULL)
357 strncpy(nas, vp->strvalue, MAX_NAS_NAME_SIZE - 1);
359 if ((vp = pairfind(request->packet->vps, PW_NAS_IDENTIFIER)) != NULL)
360 strncpy(nas, vp->strvalue, MAX_NAS_NAME_SIZE - 1);
362 DEBUG("rlm_ippool: Could not find nas information in packet. Return NOOP.");
363 return RLM_MODULE_NOOP;
368 /* We don't care about any other accounting packet */
369 DEBUG("rlm_ippool: This is not an Accounting-Stop. Return NOOP.");
371 return RLM_MODULE_NOOP;
374 memset(key.nas,0,MAX_NAS_NAME_SIZE);
375 strncpy(key.nas,nas,MAX_NAS_NAME_SIZE -1 );
377 DEBUG("rlm_ippool: Searching for an entry for nas/port: %s/%u",key.nas,key.port);
378 key_datum.dptr = (char *) &key;
379 key_datum.dsize = sizeof(ippool_key);
381 pthread_mutex_lock(&data->op_mutex);
382 data_datum = gdbm_fetch(data->gdbm, key_datum);
383 if (data_datum.dptr != NULL){
386 * If the entry was found set active to zero
388 memcpy(&entry, data_datum.dptr, sizeof(ippool_info));
389 free(data_datum.dptr);
390 DEBUG("rlm_ippool: Deallocated entry for ip/port: %s/%u",ip_ntoa(str,entry.ipaddr),port);
396 * Save the reference to the entry
398 save_datum.dptr = key_datum.dptr;
399 save_datum.dsize = key_datum.dsize;
401 data_datum.dptr = (char *) &entry;
402 data_datum.dsize = sizeof(ippool_info);
404 rcode = gdbm_store(data->gdbm, key_datum, data_datum, GDBM_REPLACE);
406 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
407 data->session_db, gdbm_strerror(gdbm_errno));
408 pthread_mutex_unlock(&data->op_mutex);
409 return RLM_MODULE_FAIL;
413 * Decrease allocated count from the ip index
415 key_datum.dptr = (char *) &entry.ipaddr;
416 key_datum.dsize = sizeof(uint32_t);
417 data_datum = gdbm_fetch(data->ip, key_datum);
418 if (data_datum.dptr != NULL){
419 memcpy(&num, data_datum.dptr, sizeof(int));
420 free(data_datum.dptr);
423 DEBUG("rlm_ippool: num: %d",num);
424 data_datum.dptr = (char *) #
425 data_datum.dsize = sizeof(int);
426 rcode = gdbm_store(data->ip, key_datum, data_datum, GDBM_REPLACE);
428 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
429 data->ip_index, gdbm_strerror(gdbm_errno));
430 pthread_mutex_unlock(&data->op_mutex);
431 return RLM_MODULE_FAIL;
433 if (num >0 && entry.extra == 1){
435 * We are doing MPPP and we still have nas/port entries referencing
436 * this ip. Delete this entry so that eventually we only keep one
437 * reference to this ip.
439 gdbm_delete(data->gdbm,save_datum);
443 pthread_mutex_unlock(&data->op_mutex);
446 pthread_mutex_unlock(&data->op_mutex);
447 DEBUG("rlm_ippool: Entry not found");
450 return RLM_MODULE_OK;
453 static int ippool_postauth(void *instance, REQUEST *request)
455 rlm_ippool_t *data = (rlm_ippool_t *) instance;
456 unsigned int port = 0;
463 char nas[MAX_NAS_NAME_SIZE];
475 /* quiet the compiler */
479 /* Check if Pool-Name attribute exists. If it exists check our name and
480 * run only if they match
482 if ((vp = pairfind(request->config_items, PW_POOL_NAME)) != NULL){
483 if (data->name == NULL || strcmp(data->name,vp->strvalue))
484 return RLM_MODULE_NOOP;
486 DEBUG("rlm_ippool: Could not find Pool-Name attribute.");
487 return RLM_MODULE_NOOP;
491 * Get the nas ip address
494 if ((vp = pairfind(request->packet->vps, PW_NAS_IP_ADDRESS)) != NULL)
495 strncpy(nas, vp->strvalue, MAX_NAS_NAME_SIZE - 1);
497 if ((vp = pairfind(request->packet->vps, PW_NAS_IDENTIFIER)) != NULL)
498 strncpy(nas, vp->strvalue, MAX_NAS_NAME_SIZE - 1);
500 DEBUG("rlm_ippool: Could not find nas information. Return NOOP.");
501 return RLM_MODULE_NOOP;
508 if ((vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) != NULL)
515 if ((vp = pairfind(request->packet->vps, PW_NAS_PORT)) != NULL)
518 DEBUG("rlm_ippool: Could not find nas port information. Return NOOP.");
519 return RLM_MODULE_NOOP;
522 memset(key.nas,0,MAX_NAS_NAME_SIZE);
523 strncpy(key.nas,nas,MAX_NAS_NAME_SIZE -1 );
525 DEBUG("rlm_ippool: Searching for an entry for nas/port: %s/%u",key.nas,key.port);
526 key_datum.dptr = (char *) &key;
527 key_datum.dsize = sizeof(ippool_key);
529 pthread_mutex_lock(&data->op_mutex);
530 data_datum = gdbm_fetch(data->gdbm, key_datum);
531 if (data_datum.dptr != NULL){
533 * If there is a corresponding entry in the database with active=1 it is stale.
537 memcpy(&entry, data_datum.dptr, sizeof(ippool_info));
538 free(data_datum.dptr);
540 DEBUG("rlm_ippool: Found a stale entry for ip/port: %s/%u",ip_ntoa(str,entry.ipaddr),port);
546 * Save the reference to the entry
548 save_datum.dptr = key_datum.dptr;
549 save_datum.dsize = key_datum.dsize;
551 data_datum.dptr = (char *) &entry;
552 data_datum.dsize = sizeof(ippool_info);
554 rcode = gdbm_store(data->gdbm, key_datum, data_datum, GDBM_REPLACE);
556 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
557 data->session_db, gdbm_strerror(gdbm_errno));
558 pthread_mutex_unlock(&data->op_mutex);
559 return RLM_MODULE_FAIL;
561 /* Decrease allocated count from the ip index */
563 key_datum.dptr = (char *) &entry.ipaddr;
564 key_datum.dsize = sizeof(uint32_t);
565 data_datum = gdbm_fetch(data->ip, key_datum);
566 if (data_datum.dptr != NULL){
567 memcpy(&num, data_datum.dptr, sizeof(int));
568 free(data_datum.dptr);
571 DEBUG("rlm_ippool: num: %d",num);
572 data_datum.dptr = (char *) #
573 data_datum.dsize = sizeof(int);
574 rcode = gdbm_store(data->ip, key_datum, data_datum, GDBM_REPLACE);
576 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
577 data->ip_index, gdbm_strerror(gdbm_errno));
578 pthread_mutex_unlock(&data->op_mutex);
579 return RLM_MODULE_FAIL;
581 if (num >0 && entry.extra == 1){
583 * We are doing MPPP and we still have nas/port entries referencing
584 * this ip. Delete this entry so that eventually we only keep one
585 * reference to this ip.
587 gdbm_delete(data->gdbm,save_datum);
594 pthread_mutex_unlock(&data->op_mutex);
597 * If there is a Framed-IP-Address attribute in the reply, check for override
599 if (pairfind(request->reply->vps, PW_FRAMED_IP_ADDRESS) != NULL) {
600 DEBUG("rlm_ippool: Found Framed-IP-Address attribute in reply attribute list.");
603 /* Override supplied Framed-IP-Address */
604 DEBUG("rlm_ippool: override is set to yes. Override the existing Framed-IP-Address attribute.");
605 pairdelete(&request->reply->vps, PW_FRAMED_IP_ADDRESS);
608 DEBUG("rlm_ippool: override is set to no. Return NOOP.");
609 return RLM_MODULE_NOOP;
614 * Walk through the database searching for an active=0 entry.
615 * We search twice. Once to see if we have an active entry with the same callerid
616 * so that MPPP can work ok and then once again to find a free entry.
619 pthread_mutex_lock(&data->op_mutex);
621 key_datum.dptr = NULL;
623 key_datum = gdbm_firstkey(data->gdbm);
624 while(key_datum.dptr){
625 data_datum = gdbm_fetch(data->gdbm, key_datum);
626 if (data_datum.dptr){
627 memcpy(&entry,data_datum.dptr, sizeof(ippool_info));
628 free(data_datum.dptr);
630 * If we find an entry for the same caller-id and nas with active=1
631 * then we use that for multilink (MPPP) to work properly.
633 if (strcmp(entry.cli,cli) == 0 && entry.active){
634 memcpy(&key,key_datum.dptr,sizeof(ippool_key));
635 if (!strcmp(key.nas,nas)){
641 nextkey = gdbm_nextkey(data->gdbm, key_datum);
642 free(key_datum.dptr);
647 if (key_datum.dptr == NULL){
648 key_datum = gdbm_firstkey(data->gdbm);
649 while(key_datum.dptr){
650 data_datum = gdbm_fetch(data->gdbm, key_datum);
651 if (data_datum.dptr){
652 memcpy(&entry,data_datum.dptr, sizeof(ippool_info));
653 free(data_datum.dptr);
656 * Find an entry with active == 0
657 * or an entry that has expired
659 if (entry.active == 0 || (entry.timestamp && ((entry.timeout &&
660 request->timestamp >= (entry.timestamp + entry.timeout)) ||
661 (data->max_timeout && request->timestamp >= (entry.timestamp + data->max_timeout))))){
664 tmp.dptr = (char *) &entry.ipaddr;
665 tmp.dsize = sizeof(uint32_t);
666 data_datum = gdbm_fetch(data->ip, tmp);
669 * If we find an entry in the ip index and the number is zero (meaning
670 * that we haven't allocated the same ip address to another nas/port pair)
671 * or if we don't find an entry then delete the session entry so
672 * that we can change the key (nas/port)
673 * Else we don't delete the session entry since we haven't yet deallocated the
674 * corresponding ip address and we continue our search.
677 if (data_datum.dptr){
678 memcpy(&num,data_datum.dptr, sizeof(int));
679 free(data_datum.dptr);
691 nextkey = gdbm_nextkey(data->gdbm, key_datum);
692 free(key_datum.dptr);
697 * If we have found a free entry set active to 1 then add a Framed-IP-Address attribute to
699 * We keep the operation mutex locked until after we have set the corresponding entry active
704 * Found == 1 means we have the nas/port combination entry in our database
705 * We exchange the ip address between the nas/port entry and the free entry
706 * Afterwards we will save the free ip address to the nas/port entry.
708 * ---------------------------------------------
709 * - NAS/PORT Entry |||| Free Entry ||| Time
710 * - IP1 IP2(Free) BEFORE
711 * - IP2(Free) IP1 AFTER
712 * ---------------------------------------------
714 * We only do this if we are NOT doing MPPP
718 datum data_datum_tmp;
721 memset(key_tmp.nas,0,MAX_NAS_NAME_SIZE);
722 strncpy(key_tmp.nas,nas,MAX_NAS_NAME_SIZE -1 );
724 DEBUG("rlm_ippool: Searching for an entry for nas/port: %s/%u",key_tmp.nas,key_tmp.port);
725 key_datum_tmp.dptr = (char *) &key_tmp;
726 key_datum_tmp.dsize = sizeof(ippool_key);
728 data_datum_tmp = gdbm_fetch(data->gdbm, key_datum_tmp);
729 if (data_datum_tmp.dptr != NULL){
731 rcode = gdbm_store(data->gdbm, key_datum, data_datum_tmp, GDBM_REPLACE);
733 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
734 data->session_db, gdbm_strerror(gdbm_errno));
735 pthread_mutex_unlock(&data->op_mutex);
736 return RLM_MODULE_FAIL;
738 free(data_datum_tmp.dptr);
743 * We have not found the nas/port combination
747 * Delete the entry so that we can change the key
748 * All is well. We delete one entry and we add one entry
750 gdbm_delete(data->gdbm, key_datum);
754 * We are doing MPPP. (mppp should be 1)
755 * We don't do anything.
756 * We will create an extra not needed entry in the database in this case
757 * but we don't really care since we always also use the ip_index database
758 * when we search for a free entry.
759 * We will also delete that entry on the accounting section so that we only
760 * have one nas/port entry referencing each ip
765 radlog(L_ERR, "rlm_ippool: mppp is not one. Please report this behaviour.");
768 free(key_datum.dptr);
770 entry.timestamp = request->timestamp;
771 if ((vp = pairfind(request->reply->vps, PW_SESSION_TIMEOUT)) != NULL)
772 entry.timeout = (time_t) vp->lvalue;
777 data_datum.dptr = (char *) &entry;
778 data_datum.dsize = sizeof(ippool_info);
779 memset(key.nas,0,MAX_NAS_NAME_SIZE);
780 strncpy(key.nas,nas,MAX_NAS_NAME_SIZE - 1);
782 key_datum.dptr = (char *) &key;
783 key_datum.dsize = sizeof(ippool_key);
785 DEBUG2("rlm_ippool: Allocating ip to nas/port: %s/%u",key.nas,key.port);
786 rcode = gdbm_store(data->gdbm, key_datum, data_datum, GDBM_REPLACE);
788 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
789 data->session_db, gdbm_strerror(gdbm_errno));
790 pthread_mutex_unlock(&data->op_mutex);
791 return RLM_MODULE_FAIL;
794 /* Increase the ip index count */
795 key_datum.dptr = (char *) &entry.ipaddr;
796 key_datum.dsize = sizeof(uint32_t);
797 data_datum = gdbm_fetch(data->ip, key_datum);
798 if (data_datum.dptr){
799 memcpy(&num,data_datum.dptr,sizeof(int));
800 free(data_datum.dptr);
804 DEBUG("rlm_ippool: num: %d",num);
805 data_datum.dptr = (char *) #
806 data_datum.dsize = sizeof(int);
807 rcode = gdbm_store(data->ip, key_datum, data_datum, GDBM_REPLACE);
809 radlog(L_ERR, "rlm_ippool: Failed storing data to %s: %s",
810 data->ip_index, gdbm_strerror(gdbm_errno));
811 pthread_mutex_unlock(&data->op_mutex);
812 return RLM_MODULE_FAIL;
814 pthread_mutex_unlock(&data->op_mutex);
817 DEBUG("rlm_ippool: Allocated ip %s to client on nas %s,port %u",ip_ntoa(str,entry.ipaddr),
819 if ((vp = paircreate(PW_FRAMED_IP_ADDRESS, PW_TYPE_IPADDR)) == NULL) {
820 radlog(L_ERR|L_CONS, "no memory");
821 return RLM_MODULE_FAIL;
823 vp->lvalue = entry.ipaddr;
824 ip_ntoa(vp->strvalue, vp->lvalue);
825 pairadd(&request->reply->vps, vp);
828 * If there is no Framed-Netmask attribute in the
831 if (pairfind(request->reply->vps, PW_FRAMED_IP_NETMASK) == NULL) {
832 if ((vp = paircreate(PW_FRAMED_IP_NETMASK, PW_TYPE_IPADDR)) == NULL)
833 radlog(L_ERR|L_CONS, "no memory");
835 vp->lvalue = ntohl(data->netmask);
836 ip_ntoa(vp->strvalue, vp->lvalue);
837 pairadd(&request->reply->vps, vp);
843 pthread_mutex_unlock(&data->op_mutex);
844 DEBUG("rlm_ippool: No available ip addresses in pool.");
845 return RLM_MODULE_NOTFOUND;
848 return RLM_MODULE_OK;
851 static int ippool_detach(void *instance)
853 rlm_ippool_t *data = (rlm_ippool_t *) instance;
855 gdbm_close(data->gdbm);
856 gdbm_close(data->ip);
857 free(data->session_db);
858 free(data->ip_index);
859 pthread_mutex_destroy(&data->op_mutex);
866 * The module name should be the only globally exported symbol.
867 * That is, everything else should be 'static'.
869 * If the module needs to temporarily modify it's instantiation
870 * data, the type should be changed to RLM_TYPE_THREAD_UNSAFE.
871 * The server will then take care of ensuring that the module
872 * is single-threaded.
874 module_t rlm_ippool = {
876 RLM_TYPE_THREAD_SAFE, /* type */
877 NULL, /* initialization */
878 ippool_instantiate, /* instantiation */
880 NULL, /* authentication */
881 NULL, /* authorization */
882 NULL, /* preaccounting */
883 ippool_accounting, /* accounting */
884 NULL, /* checksimul */
885 NULL, /* pre-proxy */
886 NULL, /* post-proxy */
887 ippool_postauth /* post-auth */
889 ippool_detach, /* detach */