2 * rlm_detail.c accounting: Write the "detail" files.
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,2006 The FreeRADIUS server project
23 #include <freeradius-devel/ident.h>
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/rad_assert.h>
29 #include <freeradius-devel/detail.h>
41 struct detail_instance {
45 /* detail file permissions */
48 /* directory permissions */
51 /* timestamp & stuff */
54 /* if we want file locking */
57 /* log src/dst information */
63 static const CONF_PARSER module_config[] = {
64 { "detailfile", PW_TYPE_STRING_PTR,
65 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
66 { "header", PW_TYPE_STRING_PTR,
67 offsetof(struct detail_instance,header), NULL, "%t" },
68 { "detailperm", PW_TYPE_INTEGER,
69 offsetof(struct detail_instance,detailperm), NULL, "0600" },
70 { "dirperm", PW_TYPE_INTEGER,
71 offsetof(struct detail_instance,dirperm), NULL, "0755" },
72 { "locking", PW_TYPE_BOOLEAN,
73 offsetof(struct detail_instance,locking), NULL, "no" },
74 { "log_packet_header", PW_TYPE_BOOLEAN,
75 offsetof(struct detail_instance,log_srcdst), NULL, "no" },
76 { NULL, -1, 0, NULL, NULL }
83 static int detail_detach(void *instance)
85 struct detail_instance *inst = instance;
86 if (inst->ht) fr_hash_table_free(inst->ht);
93 static uint32_t detail_hash(const void *data)
95 const DICT_ATTR *da = data;
96 return fr_hash(&(da->attr), sizeof(da->attr));
99 static int detail_cmp(const void *a, const void *b)
101 return ((const DICT_ATTR *)a)->attr - ((const DICT_ATTR *)b)->attr;
106 * (Re-)read radiusd.conf into memory.
108 static int detail_instantiate(CONF_SECTION *conf, void **instance)
110 struct detail_instance *inst;
113 inst = rad_malloc(sizeof(*inst));
117 memset(inst, 0, sizeof(*inst));
119 if (cf_section_parse(conf, inst, module_config) < 0) {
125 * Suppress certain attributes.
127 cs = cf_section_sub_find(conf, "suppress");
131 inst->ht = fr_hash_table_create(detail_hash, detail_cmp,
134 for (ci = cf_item_find_next(cs, NULL);
136 ci = cf_item_find_next(cs, ci)) {
140 if (!cf_item_is_pair(ci)) continue;
142 attr = cf_pair_attr(cf_itemtopair(ci));
143 if (!attr) continue; /* pair-anoia */
145 da = dict_attrbyname(attr);
147 radlog(L_INFO, "rlm_detail: WARNING: No such attribute %s: Cannot suppress printing it.", attr);
152 * For better distribution we should really
153 * hash the attribute number or name. But
154 * since the suppression list will usually
155 * be small, it doesn't matter.
157 if (!fr_hash_table_insert(inst->ht, da)) {
158 radlog(L_ERR, "rlm_detail: Failed trying to remember %s", attr);
171 * Do detail, compatible with old accounting
173 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
188 struct detail_instance *inst = instance;
190 rad_assert(request != NULL);
193 * Nothing to log: don't do anything.
196 return RLM_MODULE_NOOP;
200 * Create a directory for this nas.
202 * Generate the path for the detail file. Use the
203 * same format, but truncate at the last /. Then
204 * feed it through radius_xlat() to expand the
207 if (radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL) == 0) {
208 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to expand detail file %s",
210 return RLM_MODULE_FAIL;
212 RDEBUG2("%s expands to %s", inst->detailfile, buffer);
214 #ifdef HAVE_FNMATCH_H
217 * If we read it from a detail file, and we're about to
218 * write it back to the SAME detail file directory, then
219 * suppress the write. This check prevents an infinite
222 if ((request->listener->type == RAD_LISTEN_DETAIL) &&
223 (fnmatch(((listen_detail_t *)request->listener->data)->filename,
224 buffer, FNM_FILE_NAME | FNM_PERIOD ) == 0)) {
225 RDEBUG2("WARNING: Suppressing infinite loop.");
226 return RLM_MODULE_NOOP;
232 * Grab the last directory delimiter.
234 p = strrchr(buffer,'/');
237 * There WAS a directory delimiter there, and the file
238 * doesn't exist, so we must create it the directories..
244 * Always try to create the directory. If it
245 * exists, rad_mkdir() will check via stat(), and
246 * return immediately.
248 * This catches the case where some idiot deleted
249 * a directory that the server was using.
251 if (rad_mkdir(buffer, inst->dirperm) < 0) {
252 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to create directory %s: %s", buffer, strerror(errno));
253 return RLM_MODULE_FAIL;
257 } /* else there was no directory delimiter. */
263 * Open & create the file, with the given
266 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
267 inst->detailperm)) < 0) {
268 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
269 buffer, strerror(errno));
270 return RLM_MODULE_FAIL;
274 * If we fail to aquire the filelock in 80 tries
275 * (approximately two seconds) we bail out.
278 lseek(outfd, 0L, SEEK_SET);
279 if (rad_lockfd_nonblock(outfd, 0) < 0) {
283 select(0, NULL, NULL, NULL, &tv);
289 * The file might have been deleted by
290 * radrelay while we tried to acquire
291 * the lock (race condition)
293 if (fstat(outfd, &st) != 0) {
294 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't stat file %s: %s",
295 buffer, strerror(errno));
297 return RLM_MODULE_FAIL;
299 if (st.st_nlink == 0) {
300 RDEBUG2("File %s removed by another program, retrying",
307 RDEBUG2("Acquired filelock, tried %d time(s)",
311 } while (inst->locking && !locked && lock_count < 80);
313 if (inst->locking && !locked) {
315 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to acquire filelock for %s, giving up",
317 return RLM_MODULE_FAIL;
323 fsize = lseek(outfd, 0L, SEEK_END);
325 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to seek to the end of detail file %s",
328 return RLM_MODULE_FAIL;
331 if (radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL) == 0) {
332 radlog_request(L_ERR, 0, request, "rlm_detail: Unable to expand detail header format %s",
335 return RLM_MODULE_FAIL;
339 * Open the FP for buffering.
341 if ((fp = fdopen(outfd, "a")) == NULL) {
342 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
343 buffer, strerror(errno));
345 return RLM_MODULE_FAIL;
348 fprintf(fp, "%s\n", timestamp);
351 * Write the information to the file.
355 * Print out names, if they're OK.
358 if ((packet->code > 0) &&
359 (packet->code < FR_MAX_PACKET_CODE)) {
360 fprintf(fp, "\tPacket-Type = %s\n",
361 fr_packet_codes[packet->code]);
363 fprintf(fp, "\tPacket-Type = %d\n", packet->code);
367 if (inst->log_srcdst) {
368 VALUE_PAIR src_vp, dst_vp;
370 memset(&src_vp, 0, sizeof(src_vp));
371 memset(&dst_vp, 0, sizeof(dst_vp));
372 src_vp.operator = dst_vp.operator = T_OP_EQ;
374 switch (packet->src_ipaddr.af) {
376 src_vp.name = "Packet-Src-IP-Address";
377 src_vp.type = PW_TYPE_IPADDR;
378 src_vp.attribute = PW_PACKET_SRC_IP_ADDRESS;
379 src_vp.vp_ipaddr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
380 dst_vp.name = "Packet-Dst-IP-Address";
381 dst_vp.type = PW_TYPE_IPADDR;
382 dst_vp.attribute = PW_PACKET_DST_IP_ADDRESS;
383 dst_vp.vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
386 src_vp.name = "Packet-Src-IPv6-Address";
387 src_vp.type = PW_TYPE_IPV6ADDR;
388 src_vp.attribute = PW_PACKET_SRC_IPV6_ADDRESS;
389 memcpy(src_vp.vp_strvalue,
390 &packet->src_ipaddr.ipaddr.ip6addr,
391 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
392 dst_vp.name = "Packet-Dst-IPv6-Address";
393 dst_vp.type = PW_TYPE_IPV6ADDR;
394 dst_vp.attribute = PW_PACKET_DST_IPV6_ADDRESS;
395 memcpy(dst_vp.vp_strvalue,
396 &packet->dst_ipaddr.ipaddr.ip6addr,
397 sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
403 vp_print(fp, &src_vp);
404 vp_print(fp, &dst_vp);
406 src_vp.name = "Packet-Src-IP-Port";
407 src_vp.attribute = PW_PACKET_SRC_PORT;
408 src_vp.type = PW_TYPE_INTEGER;
409 src_vp.vp_integer = packet->src_port;
410 dst_vp.name = "Packet-Dst-IP-Port";
411 dst_vp.attribute = PW_PACKET_DST_PORT;
412 dst_vp.type = PW_TYPE_INTEGER;
413 dst_vp.vp_integer = packet->dst_port;
415 vp_print(fp, &src_vp);
416 vp_print(fp, &dst_vp);
419 /* Write each attribute/value to the log file */
420 for (pair = packet->vps; pair != NULL; pair = pair->next) {
422 da.attr = pair->attribute;
425 fr_hash_table_finddata(inst->ht, &da)) continue;
428 * Don't print passwords in old format...
430 if (compat && (pair->attribute == PW_USER_PASSWORD)) continue;
433 * Print all of the attributes.
439 * Add non-protocol attibutes.
443 if (request->proxy) {
444 char proxy_buffer[128];
446 inet_ntop(request->proxy->dst_ipaddr.af,
447 &request->proxy->dst_ipaddr.ipaddr,
448 proxy_buffer, sizeof(proxy_buffer));
449 fprintf(fp, "\tFreeradius-Proxied-To = %s\n",
451 RDEBUG("Freeradius-Proxied-To = %s",
456 fprintf(fp, "\tTimestamp = %ld\n",
457 (unsigned long) request->timestamp);
463 * If we can't flush it to disk, truncate the file and
466 if (fflush(fp) != 0) {
467 ftruncate(outfd, fsize); /* ignore errors! */
469 return RLM_MODULE_FAIL;
475 * And everything is fine.
477 return RLM_MODULE_OK;
481 * Accounting - write the detail files.
483 static int detail_accounting(void *instance, REQUEST *request)
486 if (request->listener->type == RAD_LISTEN_DETAIL &&
487 strcmp(((struct detail_instance *)instance)->detailfile,
488 ((listen_detail_t *)request->listener->data)->filename) == 0) {
489 RDEBUG("Suppressing writes to detail file as the request was just read from a detail file.");
490 return RLM_MODULE_NOOP;
494 return do_detail(instance,request,request->packet, TRUE);
498 * Incoming Access Request - write the detail files.
500 static int detail_authorize(void *instance, REQUEST *request)
502 return do_detail(instance,request,request->packet, FALSE);
506 * Outgoing Access-Request Reply - write the detail files.
508 static int detail_postauth(void *instance, REQUEST *request)
510 return do_detail(instance,request,request->reply, FALSE);
515 * Incoming CoA - write the detail files.
517 static int detail_recv_coa(void *instance, REQUEST *request)
519 return do_detail(instance,request,request->packet, FALSE);
523 * Outgoing CoA - write the detail files.
525 static int detail_send_coa(void *instance, REQUEST *request)
527 return do_detail(instance,request,request->reply, FALSE);
532 * Outgoing Access-Request to home server - write the detail files.
535 static int detail_pre_proxy(void *instance, REQUEST *request)
537 if (request->proxy &&
538 request->proxy->vps) {
539 return do_detail(instance,request,request->proxy, FALSE);
542 return RLM_MODULE_NOOP;
547 * Outgoing Access-Request Reply - write the detail files.
549 static int detail_post_proxy(void *instance, REQUEST *request)
551 if (request->proxy_reply &&
552 request->proxy_reply->vps) {
553 return do_detail(instance,request,request->proxy_reply, FALSE);
557 * No reply: we must be doing Post-Proxy-Type = Fail.
559 * Note that we just call the normal accounting function,
560 * to minimize the amount of code, and to highlight that
561 * it's doing normal accounting.
563 if (!request->proxy_reply) {
566 rcode = detail_accounting(instance, request);
567 if (rcode == RLM_MODULE_OK) {
568 request->reply->code = PW_ACCOUNTING_RESPONSE;
573 return RLM_MODULE_NOOP;
578 /* globally exported name */
579 module_t rlm_detail = {
582 RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,
583 detail_instantiate, /* instantiation */
584 detail_detach, /* detach */
586 NULL, /* authentication */
587 detail_authorize, /* authorization */
588 NULL, /* preaccounting */
589 detail_accounting, /* accounting */
590 NULL, /* checksimul */
592 detail_pre_proxy, /* pre-proxy */
593 detail_post_proxy, /* post-proxy */
597 detail_postauth /* post-auth */