2 * detail.c Process the detail file
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 2007 The FreeRADIUS server project
21 * Copyright 2007 Alan DeKok <aland@deployingradius.com>
24 #include <freeradius-devel/ident.h>
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/detail.h>
30 #include <freeradius-devel/rad_assert.h>
32 #ifdef HAVE_SYS_STAT_H
44 #define USEC (1000000)
46 static FR_NAME_NUMBER state_names[] = {
47 { "unopened", STATE_UNOPENED },
48 { "unlocked", STATE_UNLOCKED },
49 { "header", STATE_HEADER },
50 { "reading", STATE_READING },
51 { "queued", STATE_QUEUED },
52 { "running", STATE_RUNNING },
53 { "no-reply", STATE_NO_REPLY },
54 { "replied", STATE_REPLIED },
60 * If we're limiting outstanding packets, then mark the response
63 int detail_send(rad_listen_t *listener, REQUEST *request)
67 listen_detail_t *data = listener->data;
69 rad_assert(request->listener == listener);
70 rad_assert(listener->send == detail_send);
73 * This request timed out. Remember that, and tell the
74 * caller it's OK to read more "detail" file stuff.
76 if (request->reply->code == 0) {
77 data->delay_time = data->retry_interval * USEC;
79 data->state = STATE_NO_REPLY;
81 RDEBUG("Detail - No response configured for request %d. Will retry in %d seconds",
82 request->number, data->retry_interval);
84 radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
89 * We call gettimeofday a lot. But it should be OK,
90 * because there's nothing else to do.
92 gettimeofday(&now, NULL);
95 * If we haven't sent a packet in the last second, reset
99 if (timercmp(&data->last_packet, &now, <)) {
100 data->has_rtt = FALSE;
105 * Only one detail packet may be outstanding at a time,
106 * so it's safe to update some entries in the detail
109 * We keep smoothed round trip time (SRTT), but not round
110 * trip timeout (RTO). We use SRTT to calculate a rough
113 rtt = now.tv_sec - request->received.tv_sec;
116 rtt -= request->received.tv_usec;
119 * If we're proxying, the RTT is our processing time,
120 * plus the network delay there and back, plus the time
121 * on the other end to process the packet. Ideally, we
122 * should remove the network delays from the RTT, but we
123 * don't know what they are.
125 * So, to be safe, we over-estimate the total cost of
126 * processing the packet.
128 if (!data->has_rtt) {
129 data->has_rtt = TRUE;
131 data->rttvar = rtt / 2;
134 data->rttvar -= data->rttvar >> 2;
135 data->rttvar += (data->srtt - rtt);
136 data->srtt -= data->srtt >> 3;
137 data->srtt += rtt >> 3;
141 * Calculate the time we wait before sending the next
144 * rtt / (rtt + delay) = load_factor / 100
146 data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
149 * Cap delay at 4 packets/s. If the end system can't
150 * handle this, then it's very broken.
152 if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
154 RDEBUG3("Received response for request %d. Will read the next packet in %d seconds",
155 request->number, data->delay_time / USEC);
157 data->last_packet = now;
159 data->state = STATE_REPLIED;
160 radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
167 * Open the detail file, if we can.
169 * FIXME: create it, if it's not already there, so that the main
170 * server select() will wake us up if there's anything to read.
172 static int detail_open(rad_listen_t *this)
175 listen_detail_t *data = this->data;
176 char *filename = data->filename;
178 rad_assert(data->state == STATE_UNOPENED);
179 data->delay_time = USEC;
182 * Open detail.work first, so we don't lose
183 * accounting packets. It's probably better to
184 * duplicate them than to lose them.
186 * Note that we're not writing to the file, but
187 * we've got to open it for writing in order to
188 * establish the lock, to prevent rlm_detail from
191 * This also means that if we're doing globbing,
192 * this file will be read && processed before the
193 * file globbing is done.
195 this->fd = open(data->filename_work, O_RDWR);
197 DEBUG2("Polling for detail file %s", filename);
200 * Try reading the detail file. If it
201 * doesn't exist, we can't do anything.
203 * Doing the stat will tell us if the file
204 * exists, even if we don't have permissions
207 if (stat(filename, &st) < 0) {
214 memset(&files, 0, sizeof(files));
215 if (glob(filename, 0, NULL, &files) != 0) {
221 for (i = 0; i < files.gl_pathc; i++) {
222 if (stat(files.gl_pathv[i], &st) < 0) continue;
225 (st.st_ctime < chtime)) {
226 chtime = st.st_ctime;
236 filename = strdup(files.gl_pathv[found]);
244 * Open it BEFORE we rename it, just to
247 this->fd = open(filename, O_RDWR);
249 radlog(L_ERR, "Detail - Failed to open %s: %s",
250 filename, strerror(errno));
251 if (filename != data->filename) free(filename);
256 * Rename detail to detail.work
258 DEBUG("Detail - Renaming %s -> %s", filename, data->filename_work);
259 if (rename(filename, data->filename_work) < 0) {
260 if (filename != data->filename) free(filename);
265 if (filename != data->filename) free(filename);
266 } /* else detail.work existed, and we opened it */
268 rad_assert(data->vps == NULL);
269 rad_assert(data->fp == NULL);
271 data->state = STATE_UNLOCKED;
273 data->client_ip.af = AF_UNSPEC;
284 * FIXME: add a configuration "exit when done" so that the detail
285 * file reader can be used as a one-off tool to update stuff.
287 * The time sequence for reading from the detail file is:
289 * t_0 signalled that the server is idle, and we
290 * can read from the detail file.
292 * t_rtt the packet has been processed successfully,
293 * wait for t_delay to enforce load factor.
295 * t_rtt + t_delay wait for signal that the server is idle.
298 int detail_recv(rad_listen_t *listener,
299 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
301 char key[256], op[8], value[1024];
302 VALUE_PAIR *vp, **tail;
303 RADIUS_PACKET *packet;
305 listen_detail_t *data = listener->data;
308 * We may be in the main thread. It needs to update the
309 * timers before we try to read from the file again.
311 if (data->signal) return 0;
313 switch (data->state) {
316 rad_assert(listener->fd < 0);
318 if (!detail_open(listener)) return 0;
320 rad_assert(data->state == STATE_UNLOCKED);
321 rad_assert(listener->fd >= 0);
326 * Try to lock fd. If we can't, return.
327 * If we can, continue. This means that
328 * the server doesn't block while waiting
329 * for the lock to open...
333 * Note that we do NOT block waiting for
334 * the lock. We've re-named the file
335 * above, so we've already guaranteed
336 * that any *new* detail writer will not
337 * be opening this file. The only
338 * purpose of the lock is to catch a race
339 * condition where the execution
340 * "ping-pongs" between radiusd &
343 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
345 * Close the FD. The main loop
346 * will wake up in a second and
351 data->state = STATE_UNOPENED;
355 data->fp = fdopen(listener->fd, "r");
357 radlog(L_ERR, "FATAL: Failed to re-open detail file %s: %s",
358 data->filename, strerror(errno));
363 * Look for the header
365 data->state = STATE_HEADER;
366 data->delay_time = USEC;
374 data->state = STATE_UNOPENED;
381 fstat(listener->fd, &buf);
382 if (((off_t) ftell(data->fp)) == buf.st_size) {
388 * End of file. Delete it, and re-set
391 if (feof(data->fp)) {
393 DEBUG("Detail - unlinking %s",
394 data->filename_work);
395 unlink(data->filename_work);
396 if (data->fp) fclose(data->fp);
399 data->state = STATE_UNOPENED;
400 rad_assert(data->vps == NULL);
405 * Else go read something.
410 * Read more value-pair's, unless we're
411 * at EOF. In that case, queue whatever
415 if (data->fp && !feof(data->fp)) break;
416 data->state = STATE_QUEUED;
424 * Periodically check what's going on.
425 * If the request is taking too long,
429 if (time(NULL) < (data->running + data->retry_interval)) {
433 DEBUG("No response to detail request. Retrying");
434 data->state = STATE_NO_REPLY;
438 * If there's no reply, keep
439 * retransmitting the current packet
443 data->state = STATE_QUEUED;
447 * We have a reply. Clean up the old
448 * request, and go read another one.
451 pairfree(&data->vps);
452 data->state = STATE_HEADER;
457 while (*tail) tail = &(*tail)->next;
460 * Read a header, OR a value-pair.
462 while (fgets(buffer, sizeof(buffer), data->fp)) {
463 data->offset = ftell(data->fp); /* for statistics */
466 * Badly formatted file: delete it.
468 * FIXME: Maybe flag an error?
470 if (!strchr(buffer, '\n')) {
471 pairfree(&data->vps);
476 * We're reading VP's, and got a blank line.
479 if ((data->state == STATE_READING) &&
480 (buffer[0] == '\n')) {
481 data->state = STATE_QUEUED;
486 * Look for date/time header, and read VP's if
487 * found. If not, keep reading lines until we
490 if (data->state == STATE_HEADER) {
493 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
494 data->state = STATE_READING;
500 * We have a full "attribute = value" line.
501 * If it doesn't look reasonable, skip it.
503 * FIXME: print an error for badly formatted attributes?
505 if (sscanf(buffer, "%255s %8s %1023s", key, op, value) != 3) {
506 DEBUG2("WARNING: Skipping badly formatted line %s",
512 * Should be =, :=, +=, ...
514 if (!strchr(op, '=')) continue;
517 * Skip non-protocol attributes.
519 if (!strcasecmp(key, "Request-Authenticator")) continue;
522 * Set the original client IP address, based on
523 * what's in the detail file.
525 * Hmm... we don't set the server IP address.
528 if (!strcasecmp(key, "Client-IP-Address")) {
529 data->client_ip.af = AF_INET;
530 ip_hton(value, AF_INET, &data->client_ip);
535 * The original time at which we received the
536 * packet. We need this to properly calculate
539 if (!strcasecmp(key, "Timestamp")) {
540 data->timestamp = atoi(value);
542 vp = paircreate(PW_PACKET_ORIGINAL_TIMESTAMP, 0,
545 vp->vp_date = (uint32_t) data->timestamp;
555 * FIXME: do we want to check for non-protocol
556 * attributes like radsqlrelay does?
559 if ((userparse(buffer, &vp) > 0) &&
567 * Some kind of error.
569 * FIXME: Leave the file in-place, and warn the
572 if (ferror(data->fp)) goto cleanup;
578 * Process the packet.
584 * The writer doesn't check that the record was
585 * completely written. If the disk is full, this can
586 * result in a truncated record. When that happens,
589 if (data->state != STATE_QUEUED) {
590 radlog(L_ERR, "Truncated record: treating it as EOF for detail file %s", data->filename_work);
595 * We're done reading the file, but we didn't read
596 * anything. Clean up, and don't return anything.
599 data->state = STATE_HEADER;
600 if (feof(data->fp)) goto cleanup;
605 * Allocate the packet. If we fail, it's a serious
608 packet = rad_alloc(1);
610 radlog(L_ERR, "FATAL: Failed allocating memory for detail");
614 memset(packet, 0, sizeof(*packet));
616 packet->src_ipaddr.af = AF_INET;
617 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
618 packet->code = PW_ACCOUNTING_REQUEST;
619 packet->timestamp = time(NULL);
622 * Remember where it came from, so that we don't
623 * proxy it to the place it came from...
625 if (data->client_ip.af != AF_UNSPEC) {
626 packet->src_ipaddr = data->client_ip;
629 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0);
631 packet->src_ipaddr.af = AF_INET;
632 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
634 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0);
636 packet->src_ipaddr.af = AF_INET6;
637 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
638 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
642 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0);
644 packet->dst_ipaddr.af = AF_INET;
645 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
647 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0);
649 packet->dst_ipaddr.af = AF_INET6;
650 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
651 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
656 * We've got to give SOME value for Id & ports, so that
657 * the packets can be added to the request queue.
658 * However, we don't want to keep track of used/unused
659 * id's and ports, as that's a lot of work. This hack
660 * ensures that (if we have real random numbers), that
661 * there will be a collision on every 2^(16+15+15+24 - 1)
662 * packets, on average. That means we can read 2^37
663 * packets before having a collision, which means it's
664 * effectively impossible.
666 packet->id = fr_rand() & 0xffff;
667 packet->src_port = 1024 + (fr_rand() & 0x7fff);
668 packet->dst_port = 1024 + (fr_rand() & 0x7fff);
670 packet->dst_ipaddr.af = AF_INET;
671 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (fr_rand() & 0xffffff));
674 * If everything's OK, this is a waste of memory.
675 * Otherwise, it lets us re-send the original packet
676 * contents, unmolested.
678 packet->vps = paircopy(data->vps);
681 * Look for Acct-Delay-Time, and update
682 * based on Acct-Delay-Time += (time(NULL) - timestamp)
684 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0);
686 vp = paircreate(PW_ACCT_DELAY_TIME, 0, PW_TYPE_INTEGER);
687 rad_assert(vp != NULL);
688 pairadd(&packet->vps, vp);
690 if (data->timestamp != 0) {
691 vp->vp_integer += time(NULL) - data->timestamp;
694 *pfun = rad_accounting;
697 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
698 for (vp = packet->vps; vp; vp = vp->next) {
704 * FIXME: many of these checks may not be necessary when
705 * reading from the detail file.
709 if (!received_request(listener, packet, prequest,
710 &data->detail_client)) {
712 data->state = STATE_NO_REPLY; /* try again later */
716 data->state = STATE_RUNNING;
717 data->running = packet->timestamp;
724 * Free detail-specific stuff.
726 void detail_free(rad_listen_t *this)
728 listen_detail_t *data = this->data;
730 free(data->filename);
731 data->filename = NULL;
732 pairfree(&data->vps);
734 if (data->fp != NULL) {
741 int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
744 return snprintf(buffer, bufsize, "%s",
745 ((listen_detail_t *)(this->data))->filename);
748 return snprintf(buffer, bufsize, "detail file %s as server %s",
749 ((listen_detail_t *)(this->data))->filename,
754 * Overloaded to return delay times.
756 int detail_encode(rad_listen_t *this, UNUSED REQUEST *request)
758 listen_detail_t *data = this->data;
761 * We haven't sent a packet... delay things a bit.
764 int delay = (data->poll_interval - 1) * USEC;
767 * Add +/- 0.25s of jitter
769 delay += (USEC * 3) / 4;
770 delay += fr_rand() % (USEC / 2);
772 DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
774 fr_int2str(state_names, data->state, "?"), data->signal,
775 (delay / USEC), delay % USEC);
782 DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
783 data->filename, fr_int2str(state_names, data->state, "?"),
785 data->delay_time / USEC,
786 data->delay_time % USEC);
788 return data->delay_time;
793 * Overloaded to return "should we fix delay times"
795 int detail_decode(rad_listen_t *this, UNUSED REQUEST *request)
797 listen_detail_t *data = this->data;
803 static const CONF_PARSER detail_config[] = {
804 { "filename", PW_TYPE_STRING_PTR,
805 offsetof(listen_detail_t, filename), NULL, NULL },
806 { "load_factor", PW_TYPE_INTEGER,
807 offsetof(listen_detail_t, load_factor), NULL, Stringify(10)},
808 { "poll_interval", PW_TYPE_INTEGER,
809 offsetof(listen_detail_t, poll_interval), NULL, Stringify(1)},
810 { "retry_interval", PW_TYPE_INTEGER,
811 offsetof(listen_detail_t, retry_interval), NULL, Stringify(30)},
813 { NULL, -1, 0, NULL, NULL } /* end the list */
816 extern int check_config;
819 * Parse a detail section.
821 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
824 listen_detail_t *data;
828 if (check_config) return 0;
831 this->data = rad_malloc(sizeof(*data));
832 memset(this->data, 0, sizeof(*data));
837 rcode = cf_section_parse(cs, data, detail_config);
839 cf_log_err(cf_sectiontoitem(cs), "Failed parsing listen section");
843 if (!data->filename) {
844 cf_log_err(cf_sectiontoitem(cs), "No detail file specified in listen section");
848 if ((data->load_factor < 1) || (data->load_factor > 100)) {
849 cf_log_err(cf_sectiontoitem(cs), "Load factor must be between 1 and 100");
853 if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
854 cf_log_err(cf_sectiontoitem(cs), "poll_interval must be between 1 and 20");
859 * If the filename is a glob, use "detail.work" as the
862 if ((strchr(data->filename, '*') != NULL) ||
863 (strchr(data->filename, '[') != NULL)) {
867 radlog(L_INFO, "WARNING: Detail file \"%s\" appears to use file globbing, but it is not supported on this system.", data->filename);
869 strlcpy(buffer, data->filename, sizeof(buffer));
870 p = strrchr(buffer, FR_DIR_SEP);
876 strlcat(buffer, "detail.work",
877 sizeof(buffer) - strlen(buffer));
880 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
883 free(data->filename_work);
884 data->filename_work = strdup(buffer); /* FIXME: leaked */
888 data->state = STATE_UNOPENED;
889 data->delay_time = data->poll_interval * USEC;
893 * Initialize the fake client.
895 client = &data->detail_client;
896 memset(client, 0, sizeof(*client));
897 client->ipaddr.af = AF_INET;
898 client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
900 client->longname = client->shortname = data->filename;
901 client->secret = client->shortname;
902 client->nastype = strdup("none");