} else {
int rtt;
struct timeval now;
+
+ RDEBUG("detail (%s): Done %s packet.", data->name, fr_packet_codes[request->packet->code]);
+
/*
* We call gettimeofday a lot. But it should be OK,
* because there's nothing else to do.
* this file will be read && processed before the
* file globbing is done.
*/
+ data->fp = NULL;
data->work_fd = open(data->filename_work, O_RDWR);
+
+ /*
+ * Couldn't open it for a reason OTHER than "it doesn't
+ * exist". Complain and tell the admin.
+ */
+ if ((data->work_fd < 0) && (errno != ENOENT)) {
+ ERROR("Failed opening detail file %s: %s",
+ data->filename_work, fr_syserror(errno));
+ return 0;
+ }
+
+ /*
+ * The file doesn't exist. Poll for it again.
+ */
if (data->work_fd < 0) {
#ifndef HAVE_GLOB_H
return 0;
* And try to open the filename.
*/
data->work_fd = open(data->filename_work, O_RDWR);
- if (data->work_fd < 0) return 0;
+ if (data->work_fd < 0) {
+ ERROR("Failed opening detail file %s: %s",
+ data->filename_work, fr_syserror(errno));
+ return 0;
+ }
#endif
} /* else detail.work existed, and we opened it */
vp_cursor_t cursor;
DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
-
for (vp = fr_cursor_init(&cursor, &packet->vps);
vp;
vp = fr_cursor_next(&cursor)) {
rcode = read(data->master_pipe[0], &packet, sizeof(packet));
if (rcode <= 0) return rcode;
+ rad_assert(packet != NULL);
+
if (DEBUG_ENABLED2) {
VALUE_PAIR *vp;
vp_cursor_t cursor;
debug_pair(vp);
}
}
- rad_assert(packet != NULL);
switch (packet->code) {
case PW_CODE_ACCOUNTING_REQUEST:
static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
{
+ int y;
char key[256], op[8], value[1024];
vp_cursor_t cursor;
VALUE_PAIR *vp;
* try again.
*/
close(data->work_fd);
+ data->fp = NULL;
data->work_fd = -1;
data->state = STATE_UNOPENED;
return NULL;
}
- data->fp = fdopen(data->work_fd, "r");
+ /*
+ * Only open for writing if we're
+ * marking requests as completed.
+ */
+ data->fp = fdopen(data->work_fd, data->track ? "r+" : "r");
if (!data->fp) {
ERROR("detail (%s): FATAL: Failed to re-open detail file: %s",
data->name, fr_syserror(errno));
/*
* Else go read something.
*/
- break;
+ if (!fgets(buffer, sizeof(buffer), data->fp)) {
+ DEBUG("detail (%s): Failed reading header from file - %s",
+ data->name, data->filename_work);
+ goto cleanup;
+ }
+
+ /*
+ * Badly formatted file: delete it.
+ */
+ if (!strchr(buffer, '\n')) {
+ DEBUG("detail (%s): Invalid line without trailing LF - %s", data->name, buffer);
+ goto cleanup;
+ }
+
+ if (!sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
+ DEBUG("detail (%s): Failed reading detail file header in line - %s", data->name, buffer);
+ goto cleanup;
+ }
+
+ data->state = STATE_READING;
+ /* FALL-THROUGH */
+
/*
* Read more value-pair's, unless we're
* we have.
*/
case STATE_READING:
- if (data->fp && !feof(data->fp)) break;
- data->state = STATE_QUEUED;
+ rad_assert(data->fp != NULL);
- /* FALL-THROUGH */
+ fr_cursor_init(&cursor, &data->vps);
+
+ /*
+ * Read a header, OR a value-pair.
+ */
+ while (fgets(buffer, sizeof(buffer), data->fp)) {
+ data->last_offset = data->offset;
+ data->offset = ftell(data->fp); /* for statistics */
+
+ /*
+ * Badly formatted file: delete it.
+ */
+ if (!strchr(buffer, '\n')) {
+ WARN("detail (%s): Skipping line without trailing LF - %s", data->name, buffer);
+ fr_pair_list_free(&data->vps);
+ goto cleanup;
+ }
+
+ /*
+ * We're reading VP's, and got a blank line.
+ * That indicates the end of an entry. Queue the
+ * packet.
+ */
+ if (buffer[0] == '\n') {
+ data->state = STATE_QUEUED;
+ data->tries = 0;
+ data->packets++;
+ goto alloc_packet;
+ }
+
+ /*
+ * We have a full "attribute = value" line.
+ * If it doesn't look reasonable, skip it.
+ *
+ * FIXME: print an error for badly formatted attributes?
+ */
+ if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
+ DEBUG("detail (%s): Skipping badly formatted line - %s", data->name, buffer);
+ continue;
+ }
+
+ /*
+ * Should be =, :=, +=, ...
+ */
+ if (!strchr(op, '=')) {
+ DEBUG("detail (%s): Skipping line without operator - %s", data->name, buffer);
+ continue;
+ }
+
+ /*
+ * Skip non-protocol attributes.
+ */
+ if (!strcasecmp(key, "Request-Authenticator")) continue;
+
+ /*
+ * Set the original client IP address, based on
+ * what's in the detail file.
+ *
+ * Hmm... we don't set the server IP address.
+ * or port. Oh well.
+ */
+ if (!strcasecmp(key, "Client-IP-Address")) {
+ data->client_ip.af = AF_INET;
+ if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
+ DEBUG("detail (%s): Failed parsing Client-IP-Address", data->name);
+ fr_pair_list_free(&data->vps);
+ goto cleanup;
+ }
+ continue;
+ }
+
+ /*
+ * The original time at which we received the
+ * packet. We need this to properly calculate
+ * Acct-Delay-Time.
+ */
+ if (!strcasecmp(key, "Timestamp")) {
+ data->timestamp = atoi(value);
+ data->timestamp_offset = data->last_offset;
+
+ vp = fr_pair_afrom_num(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
+ if (vp) {
+ vp->vp_date = (uint32_t) data->timestamp;
+ vp->type = VT_DATA;
+ fr_cursor_insert(&cursor, vp);
+ }
+ continue;
+ }
+
+ if (!strcasecmp(key, "Donestamp")) {
+ data->timestamp = atoi(value);
+ data->done_entry = true;
+ continue;
+ }
+
+ DEBUG3("detail (%s): Trying to read VP from line - %s", data->name, buffer);
+
+ /*
+ * Read one VP.
+ *
+ * FIXME: do we want to check for non-protocol
+ * attributes like radsqlrelay does?
+ */
+ vp = NULL;
+ if ((fr_pair_list_afrom_str(data, buffer, &vp) > 0) &&
+ (vp != NULL)) {
+ fr_cursor_merge(&cursor, vp);
+ } else {
+ DEBUG("detail (%s): Failed reading VP from line - %s", data->name, buffer);
+ goto cleanup;
+ }
+ }
+
+ /*
+ * The writer doesn't check that the
+ * record was completely written. If the
+ * disk is full, this can result in a
+ * truncated record which has no trailing
+ * blank line. When that happens, it's a
+ * bad record, and we ignore it.
+ */
+ if (feof(data->fp)) {
+ DEBUG("detail (%s): Truncated record: treating it as EOF for detail file %s",
+ data->name, data->filename_work);
+ fr_pair_list_free(&data->vps);
+ goto cleanup;
+ }
+
+ /*
+ * Some kind of non-eof error.
+ *
+ * FIXME: Leave the file in-place, and warn the
+ * administrator?
+ */
+ DEBUG("detail (%s): Unknown error, deleting detail file %s",
+ data->name, data->filename_work);
+ goto cleanup;
case STATE_QUEUED:
goto alloc_packet;
if (data->track) {
rad_assert(data->fp != NULL);
- if ((fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) ||
- (fwrite("\tDone", 1, 5, data->fp) < 5)) {
- WARN("detail (%s): Failed marking detail request as done: %s",
+ if (fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) {
+ DEBUG("detail (%s): Failed seeking to timestamp offset: %s",
+ data->name, fr_syserror(errno));
+ } else if (fwrite("\tDone", 1, 5, data->fp) < 5) {
+ DEBUG("detail (%s): Failed marking request as done: %s",
+ data->name, fr_syserror(errno));
+ } else if (fflush(data->fp) != 0) {
+ DEBUG("detail (%s): Failed flushing marked detail file to disk: %s",
data->name, fr_syserror(errno));
}
- fflush(data->fp);
+
if (fseek(data->fp, data->offset, SEEK_SET) < 0) {
- WARN("detail (%s): Failed seeking to next detail request: %s",
+ DEBUG("detail (%s): Failed seeking to next detail request: %s",
data->name, fr_syserror(errno));
}
}
- pairfree(&data->vps);
+ fr_pair_list_free(&data->vps);
data->state = STATE_HEADER;
goto do_header;
}
- fr_cursor_init(&cursor, &data->vps);
-
- /*
- * Read a header, OR a value-pair.
- */
- while (fgets(buffer, sizeof(buffer), data->fp)) {
- data->last_offset = data->offset;
- data->offset = ftell(data->fp); /* for statistics */
-
- /*
- * Badly formatted file: delete it.
- *
- * FIXME: Maybe flag an error?
- */
- if (!strchr(buffer, '\n')) {
- pairfree(&data->vps);
- goto cleanup;
- }
-
- /*
- * We're reading VP's, and got a blank line.
- * Queue the packet.
- */
- if ((data->state == STATE_READING) &&
- (buffer[0] == '\n')) {
- data->state = STATE_QUEUED;
- break;
- }
-
- /*
- * Look for date/time header, and read VP's if
- * found. If not, keep reading lines until we
- * find one.
- */
- if (data->state == STATE_HEADER) {
- int y;
-
- if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
- data->state = STATE_READING;
- }
- continue;
- }
-
- /*
- * We have a full "attribute = value" line.
- * If it doesn't look reasonable, skip it.
- *
- * FIXME: print an error for badly formatted attributes?
- */
- if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
- WARN("detail (%s): Skipping badly formatted line %s", data->name, buffer);
- continue;
- }
-
- /*
- * Should be =, :=, +=, ...
- */
- if (!strchr(op, '=')) continue;
-
- /*
- * Skip non-protocol attributes.
- */
- if (!strcasecmp(key, "Request-Authenticator")) continue;
-
- /*
- * Set the original client IP address, based on
- * what's in the detail file.
- *
- * Hmm... we don't set the server IP address.
- * or port. Oh well.
- */
- if (!strcasecmp(key, "Client-IP-Address")) {
- data->client_ip.af = AF_INET;
- if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
- ERROR("detail (%s): Failed parsing Client-IP-Address", data->name);
-
- pairfree(&data->vps);
- goto cleanup;
- }
- continue;
- }
-
- /*
- * The original time at which we received the
- * packet. We need this to properly calculate
- * Acct-Delay-Time.
- */
- if (!strcasecmp(key, "Timestamp")) {
- data->timestamp = atoi(value);
- data->timestamp_offset = data->last_offset;
-
- vp = paircreate(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
- if (vp) {
- vp->vp_date = (uint32_t) data->timestamp;
- vp->type = VT_DATA;
- fr_cursor_insert(&cursor, vp);
- }
- continue;
- }
-
- if (!strcasecmp(key, "Donestamp")) {
- data->timestamp = atoi(value);
- data->done_entry = true;
- continue;
- }
-
- /*
- * Read one VP.
- *
- * FIXME: do we want to check for non-protocol
- * attributes like radsqlrelay does?
- */
- vp = NULL;
- if ((userparse(data, buffer, &vp) > 0) &&
- (vp != NULL)) {
- fr_cursor_merge(&cursor, vp);
- }
- }
-
- /*
- * Some kind of error.
- *
- * FIXME: Leave the file in-place, and warn the
- * administrator?
- */
- if (ferror(data->fp)) goto cleanup;
-
- data->tries = 0;
- data->packets++;
-
/*
* Process the packet.
*/
alloc_packet:
if (data->done_entry) {
DEBUG2("detail (%s): Skipping record for timestamp %lu", data->name, data->timestamp);
- pairfree(&data->vps);
+ fr_pair_list_free(&data->vps);
data->state = STATE_HEADER;
goto do_header;
}
data->tries++;
/*
- * The writer doesn't check that the record was
- * completely written. If the disk is full, this can
- * result in a truncated record. When that happens,
- * treat it as EOF.
- */
- if (data->state != STATE_QUEUED) {
- ERROR("detail (%s): Truncated record: treating it as EOF for detail file %s",
- data->name, data->filename_work);
- pairfree(&data->vps);
- goto cleanup;
- }
-
- /*
* We're done reading the file, but we didn't read
* anything. Clean up, and don't return anything.
*/
if (!data->vps) {
+ WARN("detail (%s): Read empty packet from file %s",
+ data->name, data->filename_work);
data->state = STATE_HEADER;
- if (!data->fp || feof(data->fp)) goto cleanup;
return NULL;
}
* Otherwise, it lets us re-send the original packet
* contents, unmolested.
*/
- packet->vps = paircopy(packet, data->vps);
+ packet->vps = fr_pair_list_copy(packet, data->vps);
packet->code = PW_CODE_ACCOUNTING_REQUEST;
- vp = pairfind(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
if (vp) packet->code = vp->vp_integer;
gettimeofday(&packet->timestamp, NULL);
packet->src_ipaddr = data->client_ip;
}
- vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
if (vp) {
packet->src_ipaddr.af = AF_INET;
packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
packet->src_ipaddr.prefix = 32;
} else {
- vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
if (vp) {
packet->src_ipaddr.af = AF_INET6;
memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
}
}
- vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
if (vp) {
packet->dst_ipaddr.af = AF_INET;
packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
packet->dst_ipaddr.prefix = 32;
} else {
- vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
if (vp) {
packet->dst_ipaddr.af = AF_INET6;
memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
* "Timestamp" field is when we wrote the packet to the
* detail file, which could have been much later.
*/
- vp = pairfind(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
if (vp) {
data->timestamp = vp->vp_integer;
}
* Look for Acct-Delay-Time, and update
* based on Acct-Delay-Time += (time(NULL) - timestamp)
*/
- vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
if (!vp) {
- vp = paircreate(packet, PW_ACCT_DELAY_TIME, 0);
+ vp = fr_pair_afrom_num(packet, PW_ACCT_DELAY_TIME, 0);
rad_assert(vp != NULL);
- pairadd(&packet->vps, vp);
+ fr_pair_add(&packet->vps, vp);
}
if (data->timestamp != 0) {
vp->vp_integer += time(NULL) - data->timestamp;
/*
* Set the transmission count.
*/
- vp = pairfind(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
+ vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
if (!vp) {
- vp = paircreate(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
+ vp = fr_pair_afrom_num(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
rad_assert(vp != NULL);
- pairadd(&packet->vps, vp);
+ fr_pair_add(&packet->vps, vp);
}
vp->vp_integer = data->tries;
/*
* Overloaded to return "should we fix delay times"
*/
-int detail_decode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
+int detail_decode(rad_listen_t *this, REQUEST *request)
{
#ifdef WITH_DETAIL_THREAD
+ listen_detail_t *data = this->data;
+
+ RDEBUG("Received %s from detail file %s",
+ fr_packet_codes[request->packet->code], data->filename_work);
+
+ rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
+
return 0;
#else
listen_detail_t *data = this->data;
+ RDEBUG("Received %s from detail file %s",
+ fr_packet_codes[request->packet->code], data->filename_work);
+
+ rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
+
return data->signal;
#endif
}
{ "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
{ "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
{ "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
- { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), NULL },
- { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), NULL },
- { "max_outstanding", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), NULL },
-
- { NULL, -1, 0, NULL, NULL } /* end the list */
+ { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), "no" },
+ { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), "no" },
+ CONF_PARSER_TERMINATOR
};
/*
return -1;
}
- if ((data->load_factor < 1) || (data->load_factor > 100)) {
- cf_log_err_cs(cs, "Load factor must be between 1 and 100");
- return -1;
- }
+ FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, >=, 1);
+ FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, <=, 100);
- if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
- cf_log_err_cs(cs, "poll_interval must be between 1 and 20");
- return -1;
- }
-
- if (check_config) return 0;
-
- if (data->max_outstanding == 0) data->max_outstanding = 1;
+ FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, >=, 1);
+ FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, <=, 60);
FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, >=, 4);
FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, <=, 3600);
/*
+ * Only checking the config. Don't start threads or anything else.
+ */
+ if (check_config) return 0;
+
+ /*
* If the filename is a glob, use "detail.work" as the
* work file name.
*/
} else {
buffer[0] = '\0';
}
+
+ /*
+ * Globbing cannot be done across directories.
+ */
+ if ((strchr(buffer, '*') != NULL) ||
+ (strchr(buffer, '[') != NULL)) {
+ cf_log_err_cs(cs, "Wildcard directories are not supported");
+ return -1;
+ }
+
strlcat(buffer, "detail.work",
sizeof(buffer) - strlen(buffer));