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 2000,2001,2002,2003,2004,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/autoconf.h>
28 #include <sys/types.h>
40 #include <freeradius-devel/radiusd.h>
41 #include <freeradius-devel/radutmp.h>
42 #include <freeradius-devel/modules.h>
43 #include <freeradius-devel/rad_assert.h>
45 #define LOCK_LEN sizeof(struct radutmp)
47 static const char porttypes[] = "ASITX";
50 * Used for caching radutmp lookups in the accounting
51 * component. The session (checksimul) component doesn't use it,
52 * but probably should, though we're not sure how...
54 * The intent here is to keep this structure as small as
55 * possible, so that it doesn't take up too much memory.
57 typedef struct nas_port {
59 unsigned int nas_port;
62 struct nas_port *next; /* for the free list */
67 * Per-file information.
69 * Hmm... having multiple filenames managed by one instance
70 * of the module makes it difficult for the module to do
71 * simultaneous-use checking, without more code edits.
73 typedef struct radutmp_cache_t {
74 const char *filename; /* for future reference */
75 time_t last_used; /* for future reference */
78 NAS_PORT *free_offsets;
83 pthread_mutex_t mutex;
89 * We cache the users, too, so that we only have to read radutmp
92 typedef struct radutmp_simul_t {
93 char login[sizeof(((struct radutmp *) NULL)->login) + 1];
99 * Data we store per module.
101 typedef struct rlm_radutmp_t {
109 rbtree_t *user_tree; /* for simultaneous-use */
112 * As the filenames can be dynamically translated,
113 * we want to keep track of them in a separate data
114 * structure, so that we can have per-file caches.
116 radutmp_cache_t cache;
119 #ifndef HAVE_PTHREAD_H
121 * This is easier than ifdef's throughout the code.
123 #define pthread_mutex_init(_x, _y)
124 #define pthread_mutex_destroy(_x)
125 #define pthread_mutex_lock(_x)
126 #define pthread_mutex_unlock(_x)
129 static const CONF_PARSER module_config[] = {
130 { "filename", PW_TYPE_STRING_PTR,
131 offsetof(rlm_radutmp_t,filename), NULL, RADUTMP },
132 { "username", PW_TYPE_STRING_PTR,
133 offsetof(rlm_radutmp_t,username), NULL, "%{User-Name}"},
134 { "case_sensitive", PW_TYPE_BOOLEAN,
135 offsetof(rlm_radutmp_t,case_sensitive), NULL, "yes"},
136 { "check_with_nas", PW_TYPE_BOOLEAN,
137 offsetof(rlm_radutmp_t,check_nas), NULL, "yes"},
138 { "perm", PW_TYPE_INTEGER,
139 offsetof(rlm_radutmp_t,permission), NULL, "0644" },
140 { "callerid", PW_TYPE_BOOLEAN,
141 offsetof(rlm_radutmp_t,callerid_ok), NULL, "no" },
142 { NULL, -1, 0, NULL, NULL } /* end the list */
149 static int nas_port_cmp(const void *a, const void *b)
151 const NAS_PORT *one = a;
152 const NAS_PORT *two = b;
154 if (one->nas_address < two->nas_address) return -1;
155 if (one->nas_address > two->nas_address) return +1;
157 if (one->nas_port < two->nas_port) return -1;
158 if (one->nas_port > two->nas_port) return +1;
165 * Compare two user names.
167 static int user_cmp(const void *a, const void *b)
169 const radutmp_simul_t *one = a;
170 const radutmp_simul_t *two = b;
172 return strcmp(one->login, two->login);
177 * Compare two user names, case insensitive.
179 static int user_case_cmp(const void *a, const void *b)
181 const radutmp_simul_t *one = a;
182 const radutmp_simul_t *two = b;
184 return strcasecmp(one->login, two->login);
191 static int radutmp_detach(void *instance)
193 NAS_PORT *this, *next;
194 rlm_radutmp_t *inst = instance;
196 rbtree_free(inst->cache.nas_ports);
198 for (this = inst->cache.free_offsets;
205 if (inst->cache.filename) free(inst->cache.filename);
207 pthread_mutex_destroy(&(inst->cache.mutex));
210 rbtree_free(inst->user_tree);
220 static int radutmp_instantiate(CONF_SECTION *conf, void **instance)
224 inst = rad_malloc(sizeof(*inst));
228 memset(inst, 0, sizeof(*inst));
230 if (cf_section_parse(conf, inst, module_config)) {
231 radutmp_detach(inst);
235 inst->cache.nas_ports = rbtree_create(nas_port_cmp, free, 0);
236 if (!inst->cache.nas_ports) {
237 radlog(L_ERR, "rlm_radutmp: Failed to create nas tree");
238 radutmp_detach(inst);
242 pthread_mutex_init(&(inst->cache.mutex), NULL);
243 inst->cache.permission = inst->permission;
245 if (inst->case_sensitive) {
246 inst->user_tree = rbtree_create(user_cmp, free, 0);
248 inst->user_tree = rbtree_create(user_case_cmp, free, 0);
250 if (!inst->user_tree) {
251 radlog(L_ERR, "rlm_radutmp: Failed to create user tree");
252 radutmp_detach(inst);
262 * Reset the cached entries.
264 static int cache_reset(rlm_radutmp_t *inst, radutmp_cache_t *cache)
266 NAS_PORT *this, *next;
269 * Cache is already reset, do nothing.
271 if ((rbtree_num_elements(cache->nas_ports) == 0) &&
272 (cache->free_offsets == NULL)) {
273 DEBUG2(" rlm_radutmp: Not resetting the cache");
276 DEBUG2(" rlm_radutmp: Resetting the cache");
278 pthread_mutex_lock(&cache->mutex);
280 rbtree_free(inst->user_tree);
282 rbtree_free(cache->nas_ports);
284 for (this = cache->free_offsets;
290 cache->free_offsets = NULL;
293 * Re-create the caches.
295 cache->nas_ports = rbtree_create(nas_port_cmp, free, 0);
296 if (!cache->nas_ports) {
297 pthread_mutex_unlock(&cache->mutex);
298 radlog(L_ERR, "rlm_radutmp: No memory");
302 cache->max_offset = 0;
304 cache->cached_file = 1;
306 if (inst->case_sensitive) {
307 inst->user_tree = rbtree_create(user_cmp, free, 0);
309 inst->user_tree = rbtree_create(user_case_cmp, free, 0);
311 if (!inst->user_tree) {
312 pthread_mutex_unlock(&cache->mutex);
313 radlog(L_ERR, "rlm_radutmp: No memory");
317 pthread_mutex_unlock(&cache->mutex);
324 * Compare two offsets in a tree.
326 static int offset_cmp(const void *a, const void *b)
328 const NAS_PORT *one = a;
329 const NAS_PORT *two = b;
331 if (one->offset < two->offset) return -1;
332 if (one->offset > two->offset) return +1;
339 * Data structure to use when walking the trees, for zap.
341 typedef struct offset_walk_t {
343 radutmp_cache_t *cache;
344 rbtree_t *offset_tree;
345 uint32_t nas_address;
352 * Walk over the cache, finding entries with the matching NAS IP address.
354 static int nas_port_walk(void *context, void *data)
356 offset_walk_t *walk = context;
357 NAS_PORT *nas_port = data;
360 * Doesn't match, keep going.
362 if (walk->nas_address != nas_port->nas_address) return 0;
365 * Insert it into the offset tree, for later deletion.
367 if (rbtree_insert(walk->offset_tree, nas_port) != 1) {
368 DEBUG2(" rlm_radumtp: Insertion failed in nas port walk.");
377 * Walk through the offset tree, operating on the cache
379 static int offset_walk(void *context, void *data)
381 offset_walk_t *walk = context;
382 NAS_PORT *nas_port = data;
384 radutmp_simul_t *user, myUser;
387 * Seek to the entry, and possibly re-write it.
389 if (lseek(walk->fd, nas_port->offset, SEEK_SET) < 0) {
393 if (read(walk->fd, &utmp, sizeof(utmp)) != sizeof(utmp)) {
398 * If the entry in the file is NEWER than the reboot
399 * packet, don't re-write it, and don't delete it.
401 if (utmp.time > walk->now) {
406 utmp.time = walk->now;
408 if (lseek(walk->fd, -(off_t)sizeof(utmp), SEEK_CUR) < 0) {
409 radlog(L_ERR, "rlm_radutmp: offset_walk: failed in lseek: %s",
414 write(walk->fd, &utmp, sizeof(utmp));
416 strlcpy(myUser.login, utmp.login, sizeof(myUser.login));
417 user = rbtree_finddata(walk->inst->user_tree, &myUser);
418 rad_assert(user != NULL);
419 rad_assert(user->simul_count > 0);
421 if (user->simul_count == 0) {
422 rbtree_deletebydata(walk->inst->user_tree, user);
425 if (rbtree_deletebydata(walk->cache->nas_ports, nas_port) == 0) {
426 radlog(L_ERR, "rlm_radutmp: Failed to delete entry from cache");
431 * Insert the entry into the free list.
433 nas_port->next = walk->cache->free_offsets;
434 walk->cache->free_offsets = nas_port;
441 * Zap all users on a NAS from the radutmp file.
443 static int radutmp_zap(rlm_radutmp_t *inst,
444 radutmp_cache_t *cache,
445 uint32_t nas_address,
449 rbtree_t *offset_tree;
452 rad_assert(now != 0);
455 * If there's nothing in the file, do nothing,
456 * but truncate the file, just to be safe.
458 if (rbtree_num_elements(cache->nas_ports) == 0) {
459 truncate(cache->filename, (off_t) 0);
460 DEBUG2(" rlm_radutmp: No entries in file. Quenching zap.");
465 * Create the offset tree, as we want to delete utmp
466 * entries starting from the start of the file, and we
467 * can't delete nodes from an rbtree while we're walking
470 offset_tree = rbtree_create(offset_cmp, NULL, 0);
472 radlog(L_ERR, "rlm_radutmp: Out of memory");
476 pthread_mutex_lock(&cache->mutex);
479 * Walk through the cache, finding entries for this NAS,
480 * and add those entries to the offset tree.
482 memset(&walk, 0, sizeof(walk));
484 walk.offset_tree = offset_tree;
485 walk.nas_address = nas_address;
486 rcode = rbtree_walk(cache->nas_ports, PreOrder, nas_port_walk, &walk);
488 pthread_mutex_unlock(&cache->mutex);
489 rbtree_free(offset_tree);
490 radlog(L_ERR, "rlm_radutmp: Failed walking the cache.");
495 * If both trees have the same number of elements, then
496 * don't do anything special, as UDP packets may be
497 * received out of order, by several seconds. The
498 * "offset_walk" routine MAY NOT delete the entries, if
499 * it sees that the entries in the file are newer than
504 * If there's nothing to do, don't do anything.
506 if (rbtree_num_elements(offset_tree) == 0) {
507 DEBUG2(" rlm_radutmp: NAS IP %08x has no users recorded in file %s.",
508 htonl(nas_address), cache->filename);
509 pthread_mutex_unlock(&cache->mutex);
510 rbtree_free(offset_tree);
515 * Open the file, to re-write only a few of the entries.
517 walk.fd = open(cache->filename, O_RDWR);
519 pthread_mutex_unlock(&cache->mutex);
520 rbtree_free(offset_tree);
521 radlog(L_ERR, "rlm_radutmp: Error accessing file %s: %s",
522 cache->filename, strerror(errno));
527 * Lock the utmp file, prefer lockf() over flock().
529 * FIXME: maybe we want to lock per-record?
531 rad_lockfd(walk.fd, LOCK_LEN);
534 * Walk through the offset tree, from start to finish,
535 * deleting entries from the NAS tree, adding them to
536 * the "free offset" cache, and lseek'ing to that offset
537 * in the file, and clearing out the data.
541 rcode = rbtree_walk(offset_tree, InOrder, offset_walk, &walk);
542 rbtree_free(offset_tree);
544 radlog(L_ERR, "rlm_radutmp: Failed walking the offsets.");
548 close(walk.fd); /* and implicitly release the locks */
551 * Just to clean up the file. If it's empty,
554 if (rbtree_num_elements(cache->nas_ports) == 0) {
555 NAS_PORT *this, *next; /* too many copies of code */
557 for (this = inst->cache.free_offsets;
564 truncate(cache->filename, 0);
565 rad_assert(rbtree_num_elements(inst->user_tree) == 0);
568 pthread_mutex_unlock(&cache->mutex);
575 * Read a file, to cache all of its entries.
577 static int cache_file(rlm_radutmp_t *inst, radutmp_cache_t *cache)
585 rad_assert(cache->max_offset == 0);
586 rad_assert(cache->free_offsets == NULL);
589 * Doesn't exist, we're fine.
591 if (stat(cache->filename, &buf) < 0) {
592 if (errno == ENOENT) {
593 cache->cached_file = 1;
596 radlog(L_ERR, "rlm_radutmp: Cannot stat %s: %s",
597 cache->filename, strerror(errno));
602 * Nothing's there, we're OK.
604 if (buf.st_size == 0) {
605 cache->cached_file = 1;
610 * Don't let others much around with our data.
612 pthread_mutex_lock(&cache->mutex);
615 * Read the file and cache it's entries.
617 fd = open(cache->filename, O_RDONLY, cache->permission);
619 pthread_mutex_unlock(&cache->mutex);
620 radlog(L_ERR, "rlm_radutmp: Error opening %s: %s",
621 cache->filename, strerror(errno));
626 * Insert free entries into the tail, so that entries
627 * get used from the start.
629 tail = &(cache->free_offsets);
632 * Don't lock the file, as we're only reading it.
635 read_size = read(fd, &utmp, sizeof(utmp));
640 if (read_size == sizeof(utmp)) {
641 radutmp_simul_t *user, myUser;
642 NAS_PORT *nas_port = rad_malloc(sizeof(*nas_port));
644 memset(nas_port, 0, sizeof(nas_port));
645 nas_port->offset = cache->max_offset;
646 cache->max_offset += sizeof(utmp);
649 * Idle. Add it to the list of free
652 if (utmp.type == P_IDLE) {
654 tail = &(nas_port->next);
659 * It's a login record,
661 nas_port->nas_address = utmp.nas_address;
662 nas_port->nas_port = utmp.nas_port;
664 if (!rbtree_insert(cache->nas_ports, nas_port)) {
669 * Adds a trailing \0, so myUser.login has
670 * an extra char allocated..
672 strlcpy(myUser.login, utmp.login, sizeof(myUser.login));
673 user = rbtree_finddata(inst->user_tree, &myUser);
678 * Allocate new entry, and add it
681 user = rad_malloc(sizeof(user));
682 strlcpy(user->login, utmp.login,
683 sizeof(user->login));
684 user->simul_count = 1;
686 if (!rbtree_insert(inst->user_tree, user)) {
694 * We've read a partial record. WTF?
696 if (read_size != 0) {
697 pthread_mutex_unlock(&cache->mutex);
699 radlog(L_ERR, "rlm_radutmp: Badly formed file %s",
705 * Read nothing, stop.
707 } while (read_size != 0);
709 pthread_mutex_unlock(&cache->mutex);
710 close(fd); /* and release the lock. */
711 cache->cached_file = 1;
718 * Store logins in the RADIUS utmp file.
720 static int radutmp_accounting(void *instance, REQUEST *request)
722 rlm_radutmp_t *inst = instance;
723 struct radutmp utmp, u;
726 uint32_t nas_address = 0;
727 uint32_t framed_address = 0;
733 char ip_name[32]; /* 255.255.255.255 */
735 NAS_PORT *nas_port, myPort;
736 radutmp_cache_t *cache;
741 * Which type is this.
743 if ((vp = pairfind(request->packet->vps, PW_ACCT_STATUS_TYPE)) == NULL) {
744 radlog(L_ERR, "rlm_radutmp: No Accounting-Status-Type record.");
745 return RLM_MODULE_NOOP;
750 * Look for weird reboot packets.
752 * ComOS (up to and including 3.5.1b20) does not send
753 * standard PW_STATUS_ACCOUNTING_* messages.
755 * Check for: o no Acct-Session-Time, or time of 0
756 * o Acct-Session-Id of "00000000".
758 * We could also check for NAS-Port, that attribute
759 * should NOT be present (but we don't right now).
761 if ((status != PW_STATUS_ACCOUNTING_ON) &&
762 (status != PW_STATUS_ACCOUNTING_OFF)) do {
766 if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_TIME))
767 == NULL || vp->lvalue == 0)
769 if ((vp = pairfind(request->packet->vps, PW_ACCT_SESSION_ID))
770 != NULL && vp->length == 8 &&
771 memcmp(vp->vp_strvalue, "00000000", 8) == 0)
773 if (check1 == 0 || check2 == 0) {
774 #if 0 /* Cisco sometimes sends START records without username. */
775 radlog(L_ERR, "rlm_radutmp: no username in record");
776 return RLM_MODULE_FAIL;
781 radlog(L_INFO, "rlm_radutmp: converting reboot records.");
782 if (status == PW_STATUS_STOP)
783 status = PW_STATUS_ACCOUNTING_OFF;
784 if (status == PW_STATUS_START)
785 status = PW_STATUS_ACCOUNTING_ON;
788 memset(&utmp, 0, sizeof(utmp));
792 * First, find the interesting attributes.
794 for (vp = request->packet->vps; vp; vp = vp->next) {
795 switch (vp->attribute) {
796 case PW_LOGIN_IP_HOST:
797 case PW_FRAMED_IP_ADDRESS:
798 framed_address = vp->lvalue;
799 utmp.framed_address = vp->lvalue;
801 case PW_FRAMED_PROTOCOL:
802 protocol = vp->lvalue;
804 case PW_NAS_IP_ADDRESS:
805 nas_address = vp->lvalue;
806 utmp.nas_address = vp->lvalue;
809 utmp.nas_port = vp->lvalue;
812 case PW_ACCT_DELAY_TIME:
813 utmp.delay = vp->lvalue;
815 case PW_ACCT_SESSION_ID:
817 * If it's too big, only use the
820 if (vp->length > sizeof(utmp.session_id)) {
821 int length = vp->length - sizeof(utmp.session_id);
824 * Ascend is br0ken - it
825 * adds a \0 to the end
829 if (vp->vp_strvalue[vp->length - 1] == 0) {
833 memcpy(utmp.session_id,
834 vp->vp_strvalue + length,
835 sizeof(utmp.session_id));
837 memset(utmp.session_id, 0,
838 sizeof(utmp.session_id));
839 memcpy(utmp.session_id,
844 case PW_NAS_PORT_TYPE:
846 utmp.porttype = porttypes[vp->lvalue];
848 case PW_CALLING_STATION_ID:
849 if(inst->callerid_ok)
850 strlcpy(utmp.caller_id,
851 (char *)vp->vp_strvalue,
852 sizeof(utmp.caller_id));
858 * If we didn't find out the NAS address, use the
859 * originator's IP address.
861 if (nas_address == 0) {
862 nas_address = request->packet->src_ipaddr;
863 utmp.nas_address = nas_address;
864 nas = client_name(nas_address); /* MUST be a valid client */
866 } else { /* might be a client, might not be. */
870 * Hack like 'client_name()', but with sane
873 cl = client_find(nas_address);
875 if (cl->shortname && cl->shortname[0]) {
882 * The NAS isn't a client, it's behind
883 * a proxy server. In that case, just
884 * get the IP address.
886 nas = ip_ntoa(ip_name, nas_address);
891 * Set the protocol field.
893 if (protocol == PW_PPP)
895 else if (protocol == PW_SLIP)
900 utmp.time = request->timestamp - utmp.delay;
903 * Get the utmp filename, via xlat.
905 radius_xlat(filename, sizeof(filename), inst->filename, request, NULL);
908 * Future: look up filename in filename tree, to get
909 * radutmp_cache_t pointer
911 cache = &inst->cache;
914 * For now, double-check the filename, to be sure it isn't
917 if (!cache->filename) {
918 cache->filename = strdup(filename);
919 rad_assert(cache->filename != NULL);
921 } else if (strcmp(cache->filename, filename) != 0) {
922 radlog(L_ERR, "rlm_radutmp: We do not support dynamically named files.");
923 return RLM_MODULE_FAIL;
927 * If the lookup failed, create a new one, and add it
928 * to the filename tree, and cache the file, as below.
932 * For aging, in the future.
934 cache->last_used = request->timestamp;
937 * If we haven't already read the file, then read the
938 * entire file, in order to cache its entries.
940 if (!cache->cached_file) {
941 cache_file(inst, cache);
945 * See if this was a reboot.
947 * Hmm... we may not want to zap all of the users when
948 * the NAS comes up, because of issues with receiving
949 * UDP packets out of order.
951 if (status == PW_STATUS_ACCOUNTING_ON && nas_address) {
952 radlog(L_INFO, "rlm_radutmp: NAS %s restarted (Accounting-On packet seen)",
954 if (!radutmp_zap(inst, cache, nas_address, utmp.time)) {
957 return RLM_MODULE_OK;
960 if (status == PW_STATUS_ACCOUNTING_OFF && nas_address) {
961 radlog(L_INFO, "rlm_radutmp: NAS %s rebooted (Accounting-Off packet seen)",
963 if (!radutmp_zap(inst, cache, nas_address, utmp.time)) {
966 return RLM_MODULE_OK;
970 * If we don't know this type of entry, then pretend we
973 if (status != PW_STATUS_START &&
974 status != PW_STATUS_STOP &&
975 status != PW_STATUS_ALIVE) {
976 radlog(L_ERR, "rlm_radutmp: NAS %s port %u unknown packet type %d, ignoring it.",
977 nas, utmp.nas_port, status);
978 return RLM_MODULE_NOOP;
982 * Perhaps we don't want to store this record into
983 * radutmp. We skip records:
985 * - without a NAS-Port (telnet / tcp access)
986 * - with the username "!root" (console admin login)
989 DEBUG2(" rlm_radutmp: No NAS-Port in the packet. Cannot do anything.");
990 DEBUG2(" rlm_radumtp: WARNING: checkrad will probably not work!");
991 return RLM_MODULE_NOOP;
995 * Translate the User-Name attribute, or whatever else
996 * they told us to use.
999 radius_xlat(buffer, sizeof(buffer), inst->username, request, NULL);
1002 * Don't log certain things...
1004 if (strcmp(buffer, "!root") == 0) {
1005 DEBUG2(" rlm_radutmp: Not recording administrative user");
1007 return RLM_MODULE_NOOP;
1009 strlcpy(utmp.login, buffer, RUT_NAMESIZE);
1012 * First, try to open the file. If it doesn't exist,
1013 * nuke the existing caches, and try to create it.
1015 * FIXME: Create any intermediate directories, as
1016 * appropriate. See rlm_detail.
1018 fd = open(cache->filename, O_RDWR, inst->permission);
1020 if (errno == ENOENT) {
1021 DEBUG2(" rlm_radutmp: File %s doesn't exist, creating it.", cache->filename);
1022 if (!cache_reset(inst, cache)) return RLM_MODULE_FAIL;
1025 * Try to create the file.
1027 fd = open(cache->filename, O_RDWR | O_CREAT,
1030 } else { /* exists, but may be empty */
1034 * If the file is empty, reset the cache.
1036 if ((stat(cache->filename, &buf) == 0) &&
1037 (buf.st_size == 0) &&
1038 (!cache_reset(inst, cache))) {
1039 return RLM_MODULE_FAIL;
1041 DEBUG2(" rlm_radutmp: File %s was truncated. Resetting cache.",
1046 * Error from creation, or error other than ENOENT: die.
1049 radlog(L_ERR, "rlm_radutmp: Error accessing file %s: %s",
1050 cache->filename, strerror(errno));
1051 return RLM_MODULE_FAIL;
1055 * OK. Now that we've prepared everything we want to do,
1056 * let's see if we've cached the entry.
1058 myPort.nas_address = utmp.nas_address;
1059 myPort.nas_port = utmp.nas_port;
1061 pthread_mutex_lock(&cache->mutex);
1062 node = rbtree_find(cache->nas_ports, &myPort);
1063 pthread_mutex_unlock(&cache->mutex);
1066 nas_port = rbtree_node2data(cache->nas_ports, node);
1070 * stat the file, and get excited if it's been
1073 * i.e wipe out the cache, and re-read the file.
1077 * Now find the new entry.
1079 pthread_mutex_lock(&cache->mutex);
1080 node = rbtree_find(cache->nas_ports, &myPort);
1081 pthread_mutex_unlock(&cache->mutex);
1086 radutmp_simul_t *user;
1089 * Not found in the cache, and we're trying to
1090 * delete an existing record: ignore it.
1092 if (status == PW_STATUS_STOP) {
1093 DEBUG2(" rlm_radumtp: Logout entry for NAS %s port %u with no Login: ignoring it.",
1094 nas, utmp.nas_port);
1095 return RLM_MODULE_NOOP;
1098 pthread_mutex_lock(&cache->mutex);
1101 * It's a START or ALIVE. Try to find a free
1102 * offset where we can store the new entry, or
1103 * create one, if one doesn't already exist.
1105 if (!cache->free_offsets) {
1106 cache->free_offsets = rad_malloc(sizeof(NAS_PORT));
1107 memset(cache->free_offsets, 0,
1108 sizeof(*(cache->free_offsets)));
1109 cache->free_offsets->offset = cache->max_offset;
1110 cache->max_offset += sizeof(u);
1114 * Grab the offset, and put it into the various
1117 nas_port = cache->free_offsets;
1118 cache->free_offsets = nas_port->next;
1120 nas_port->nas_address = nas_address;
1121 nas_port->nas_port = utmp.nas_port;
1123 if (!rbtree_insert(cache->nas_ports, nas_port)) {
1128 * Allocate new entry, and add it
1131 user = rad_malloc(sizeof(user));
1132 strlcpy(user->login, utmp.login,
1133 sizeof(user->login));
1134 user->simul_count = 1;
1136 if (!rbtree_insert(inst->user_tree, user)) {
1140 pthread_mutex_unlock(&cache->mutex);
1145 * Entry was found, or newly created in the cache.
1146 * Seek to the place in the file.
1148 lseek(fd, nas_port->offset, SEEK_SET);
1151 * Lock the utmp file, prefer lockf() over flock().
1153 rad_lockfd(fd, LOCK_LEN);
1156 * If it WAS found in the cache, double-check it against
1157 * what is in the file.
1161 * If we didn't read anything, then this entry
1164 * Similarly, if the entry in the file doesn't
1165 * match what we recall, then nuke the cache
1168 read_size = read(fd, &u, sizeof(u));
1169 if ((read_size < 0) ||
1170 ((read_size > 0) && (read_size != sizeof(u)))) {
1172 * Bad read, or bad record.
1174 radlog(L_ERR, "rlm_radutmp: Badly formed file %s",
1177 return RLM_MODULE_FAIL;
1180 rad_assert(read_size != 0);
1183 * We've read a record, go poke at it.
1185 if (read_size > 0) {
1187 * If these aren't true, then
1189 * a) we have cached a "logout" entry,
1190 * which we don't do.
1192 * b) we have cached the wrong NAS address
1194 * c) we have cached the wrong NAS port.
1196 rad_assert(u.type == P_LOGIN);
1197 rad_assert(u.nas_address == utmp.nas_address);
1198 rad_assert(u.nas_port == utmp.nas_port);
1201 * An update for the same session.
1203 if (strncmp(utmp.session_id, u.session_id,
1204 sizeof(u.session_id)) == 0) {
1207 * It's a duplicate start, so we
1208 * don't bother writing it.
1210 if (status == PW_STATUS_START) {
1211 DEBUG2(" rlm_radutmp: Login entry for NAS %s port %u duplicate, ignoring it.",
1214 return RLM_MODULE_OK;
1218 * ALIVE for this session, keep the
1219 * original login time.
1221 } else if (status == PW_STATUS_ALIVE) {
1225 * Stop: delete it from our cache.
1227 } else if (status == PW_STATUS_STOP) {
1228 radutmp_simul_t *user, myUser;
1230 pthread_mutex_lock(&cache->mutex);
1231 rbtree_deletebydata(cache->nas_ports,
1234 strlcpy(myUser.login,
1235 u.login, sizeof(myUser.login));
1236 user = rbtree_finddata(inst->user_tree,
1238 rad_assert(user != NULL);
1239 rad_assert(user->simul_count > 0);
1241 user->simul_count--;
1242 if (user->simul_count == 0) {
1243 rbtree_deletebydata(inst->user_tree, user);
1246 pthread_mutex_unlock(&cache->mutex);
1250 * We don't know how to
1256 } else { /* session ID doesn't match */
1258 * STOP for the right NAS & port,
1259 * but the Acct-Session-Id is
1260 * different. This means that
1261 * we missed the original "stop",
1262 * and a new "start".
1264 if (status == PW_STATUS_STOP) {
1265 radlog(L_ERR, "rlm_radutmp: Logout entry for NAS %s port %u has old Acct-Session-ID, ignoring it.",
1268 return RLM_MODULE_OK;
1270 } /* checked session ID's */
1271 } /* else we haven't read anything from the file. */
1272 } /* else the entry wasn't cached, but could have been inserted */
1275 * Hmm... we may have received a start or alive packet
1276 * AFTER a stop or nas-down, in that case, we want to
1277 * discard the new packet. However, the original code
1278 * could over-write an idle record with a new login
1279 * record for another NAS && port, so we won't worry
1280 * about this case too much.
1284 * Seek to where the entry is, and write it blindly.
1286 lseek(fd, nas_port->offset, SEEK_SET); /* FIXME: err */
1288 if (status != PW_STATUS_STOP) {
1289 utmp.type = P_LOGIN;
1290 rad_assert(nas_port != NULL); /* it WAS cached */
1292 /* FIXME: maybe assert that the entry was deleted... */
1293 memcpy(&utmp, &u, sizeof(utmp));
1297 write(fd, &utmp, sizeof(utmp)); /* FIXME: err */
1299 close(fd); /* and implicitly release the locks */
1301 return RLM_MODULE_OK;
1305 * See if a user is already logged in. Sets request->simul_count
1306 * to the current session count for this user and sets
1307 * request->simul_mpp to 2 if it looks like a multilink attempt
1308 * based on the requested IP address, otherwise leaves
1309 * request->simul_mpp alone.
1311 * Check twice. If on the first pass the user exceeds his
1312 * max. number of logins, do a second pass and validate all
1313 * logins by querying the terminal server (using eg. SNMP).
1315 static int radutmp_checksimul(void *instance, REQUEST *request)
1321 char *call_num = NULL;
1323 rlm_radutmp_t *inst = instance;
1325 char filename[1024];
1326 radutmp_cache_t *cache;
1327 radutmp_simul_t *user, myUser;
1330 * Get the filename, via xlat.
1332 radius_xlat(filename, sizeof(filename), inst->filename, request, NULL);
1335 * Future: look up filename in filename tree, to get
1336 * radutmp_cache_t pointer
1338 cache = &inst->cache;
1341 * For now, double-check the filename, to be sure it isn't
1344 if (!cache->filename) {
1345 cache->filename = strdup(filename);
1346 rad_assert(cache->filename != NULL);
1348 } else if (strcmp(cache->filename, filename) != 0) {
1349 radlog(L_ERR, "rlm_radutmp: We do not support dynamically named files.");
1350 return RLM_MODULE_FAIL;
1354 radius_xlat(login, sizeof(login), inst->username, request, NULL);
1356 return RLM_MODULE_NOOP;
1360 * WTF? This is probably wrong... we probably want to
1361 * be able to check users across multiple session accounting
1364 request->simul_count = 0;
1366 strlcpy(myUser.login, login, sizeof(myUser.login));
1367 pthread_mutex_lock(&inst->cache.mutex);
1368 user = rbtree_finddata(inst->user_tree, &myUser);
1369 if (user) request->simul_count = user->simul_count;
1370 user = NULL; /* someone else may delete it */
1371 pthread_mutex_unlock(&inst->cache.mutex);
1374 * The number of users logged in is OK,
1375 * OR, we've been told to not check the NAS.
1377 if ((request->simul_count < request->simul_max) ||
1379 return RLM_MODULE_OK;
1383 * The user is logged in at least N times, and
1384 * we're told to check the NAS. In that case,
1385 * we've got to read the file, and check each
1388 if ((fd = open(cache->filename, O_RDWR)) < 0) {
1390 * If the file doesn't exist, then no users
1393 if (errno == ENOENT) {
1394 request->simul_count = 0;
1395 return RLM_MODULE_OK;
1399 * Error accessing the file.
1401 radlog(L_ERR, "rlm_radumtp: Error accessing file %s: %s",
1402 cache->filename, strerror(errno));
1403 return RLM_MODULE_FAIL;
1407 * Setup some stuff, like for MPP detection.
1409 if ((vp = pairfind(request->packet->vps, PW_FRAMED_IP_ADDRESS)) != NULL)
1411 if ((vp = pairfind(request->packet->vps, PW_CALLING_STATION_ID)) != NULL)
1412 call_num = vp->vp_strvalue;
1415 * lock the file while reading/writing.
1417 rad_lockfd(fd, LOCK_LEN);
1420 * FIXME: If we get a 'Start' for a user/nas/port which is
1421 * listed, but for which we did NOT get a 'Stop', then
1422 * it's not a duplicate session. This happens with
1423 * static IP's like DSL.
1425 request->simul_count = 0;
1426 while (read(fd, &u, sizeof(u)) == sizeof(u)) {
1427 if (((strncmp(login, u.login, RUT_NAMESIZE) == 0) ||
1428 (!inst->case_sensitive &&
1429 (strncasecmp(login, u.login, RUT_NAMESIZE) == 0))) &&
1430 (u.type == P_LOGIN)) {
1431 char session_id[sizeof(u.session_id) + 1];
1432 char utmp_login[sizeof(u.login) + 1];
1434 strlcpy(session_id, u.session_id, sizeof(session_id));
1437 * The login name MAY fill the whole field,
1438 * and thus won't be zero-filled.
1440 * Note that we take the user name from
1441 * the utmp file, as that's the canonical
1442 * form. The 'login' variable may contain
1443 * a string which is an upper/lowercase
1444 * version of u.login. When we call the
1445 * routine to check the terminal server,
1446 * the NAS may be case sensitive.
1448 * e.g. We ask if "bob" is using a port,
1449 * and the NAS says "no", because "BOB"
1450 * is using the port.
1452 strlcpy(utmp_login, u.login, sizeof(u.login));
1455 * rad_check_ts may take seconds
1456 * to return, and we don't want
1457 * to block everyone else while
1458 * that's happening. */
1459 rad_unlockfd(fd, LOCK_LEN);
1460 rcode = rad_check_ts(u.nas_address, u.nas_port,
1461 utmp_login, session_id);
1462 rad_lockfd(fd, LOCK_LEN);
1466 * Stale record - zap it.
1468 * Hmm... this ends up calling
1469 * the accounting section
1472 session_zap(request, u.nas_address,
1473 u.nas_port, login, session_id,
1474 u.framed_address, u.proto,0);
1476 else if (rcode == 1) {
1478 * User is still logged in.
1480 ++request->simul_count;
1483 * Does it look like a MPP attempt?
1485 if (strchr("SCPA", u.proto) &&
1486 ipno && u.framed_address == ipno)
1487 request->simul_mpp = 2;
1488 else if (strchr("SCPA", u.proto) && call_num &&
1489 !strncmp(u.caller_id,call_num,16))
1490 request->simul_mpp = 2;
1494 * Failed to check the terminal
1495 * server for duplicate logins:
1499 radlog(L_ERR, "rlm_radutmp: Failed to check the terminal server for user '%s'.", utmp_login);
1500 return RLM_MODULE_FAIL;
1504 close(fd); /* and implicitly release the locks */
1506 return RLM_MODULE_OK;
1509 /* globally exported name */
1510 module_t rlm_radutmp = {
1512 0, /* type: reserved */
1513 NULL, /* initialization */
1514 radutmp_instantiate, /* instantiation */
1516 NULL, /* authentication */
1517 NULL, /* authorization */
1518 NULL, /* preaccounting */
1519 radutmp_accounting, /* accounting */
1520 radutmp_checksimul, /* checksimul */
1521 NULL, /* pre-proxy */
1522 NULL, /* post-proxy */
1523 NULL /* post-auth */
1525 radutmp_detach, /* detach */