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>
37 struct detail_instance {
41 /* detail file permissions */
44 /* directory permissions */
47 /* last made directory */
48 char *last_made_directory;
50 /* timestamp & stuff */
53 /* if we want file locking */
56 /* log src/dst information */
62 static const CONF_PARSER module_config[] = {
63 { "detailfile", PW_TYPE_STRING_PTR,
64 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
65 { "header", PW_TYPE_STRING_PTR,
66 offsetof(struct detail_instance,header), NULL, "%t" },
67 { "detailperm", PW_TYPE_INTEGER,
68 offsetof(struct detail_instance,detailperm), NULL, "0600" },
69 { "dirperm", PW_TYPE_INTEGER,
70 offsetof(struct detail_instance,dirperm), NULL, "0755" },
71 { "locking", PW_TYPE_BOOLEAN,
72 offsetof(struct detail_instance,locking), NULL, "no" },
73 { "log_packet_header", PW_TYPE_BOOLEAN,
74 offsetof(struct detail_instance,log_srcdst), NULL, "no" },
75 { NULL, -1, 0, NULL, NULL }
82 static int detail_detach(void *instance)
84 struct detail_instance *inst = instance;
85 free((char*) inst->last_made_directory);
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) {
124 inst->last_made_directory = NULL;
127 * Suppress certain attributes.
129 cs = cf_section_sub_find(conf, "suppress");
133 inst->ht = fr_hash_table_create(detail_hash, detail_cmp,
136 for (ci = cf_item_find_next(cs, NULL);
138 ci = cf_item_find_next(cs, ci)) {
142 if (!cf_item_is_pair(ci)) continue;
144 attr = cf_pair_attr(cf_itemtopair(ci));
145 if (!attr) continue; /* pair-anoia */
147 da = dict_attrbyname(attr);
149 radlog(L_INFO, "rlm_detail: WARNING: No such attribute %s: Cannot suppress printing it.", attr);
154 * For better distribution we should really
155 * hash the attribute number or name. But
156 * since the suppression list will usually
157 * be small, it doesn't matter.
159 if (!fr_hash_table_insert(inst->ht, da)) {
160 radlog(L_ERR, "rlm_detail: Failed trying to remember %s", attr);
173 * Do detail, compatible with old accounting
175 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
189 struct detail_instance *inst = instance;
191 rad_assert(request != NULL);
194 * Nothing to log: don't do anything.
197 return RLM_MODULE_NOOP;
201 * Create a directory for this nas.
203 * Generate the path for the detail file. Use the
204 * same format, but truncate at the last /. Then
205 * feed it through radius_xlat() to expand the
208 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
209 RDEBUG2("%s expands to %s", inst->detailfile, buffer);
212 * Grab the last directory delimiter.
214 p = strrchr(buffer,'/');
217 * There WAS a directory delimiter there, and
218 * the file doesn't exist, so
219 * we prolly must create it the dir(s)
221 if ((p) && (stat(buffer, &st) < 0)) {
224 * NO previously cached directory name, so we've
225 * got to create a new one.
227 * OR the new directory name is different than the old,
228 * so we've got to create a new one.
230 * OR the cached directory has somehow gotten removed,
231 * so we've got to create a new one.
233 if ((inst->last_made_directory == NULL) ||
234 (strcmp(inst->last_made_directory, buffer) != 0)) {
235 free((char *) inst->last_made_directory);
236 inst->last_made_directory = strdup(buffer);
240 * stat the directory, and don't do anything if
241 * it exists. If it doesn't exist, create it.
243 * This also catches the case where some idiot
244 * deleted a directory that the server was using.
246 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
247 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
248 return RLM_MODULE_FAIL;
252 } /* else there was no directory delimiter. */
258 * Open & create the file, with the given
261 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
262 inst->detailperm)) < 0) {
263 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
264 buffer, strerror(errno));
265 return RLM_MODULE_FAIL;
269 * If we fail to aquire the filelock in 80 tries
270 * (approximately two seconds) we bail out.
273 lseek(outfd, 0L, SEEK_SET);
274 if (rad_lockfd_nonblock(outfd, 0) < 0) {
278 select(0, NULL, NULL, NULL, &tv);
284 * The file might have been deleted by
285 * radrelay while we tried to acquire
286 * the lock (race condition)
288 if (fstat(outfd, &st) != 0) {
289 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't stat file %s: %s",
290 buffer, strerror(errno));
292 return RLM_MODULE_FAIL;
294 if (st.st_nlink == 0) {
295 RDEBUG2("File %s removed by another program, retrying",
302 RDEBUG2("Acquired filelock, tried %d time(s)",
306 } while (inst->locking && !locked && lock_count < 80);
308 if (inst->locking && !locked) {
310 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to acquire filelock for %s, giving up",
312 return RLM_MODULE_FAIL;
316 * Convert the FD to FP. The FD is no longer valid
317 * after this operation.
319 if ((outfp = fdopen(outfd, "a")) == NULL) {
320 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
321 buffer, strerror(errno));
323 lseek(outfd, 0L, SEEK_SET);
324 rad_unlockfd(outfd, 0);
325 RDEBUG2("Released filelock");
327 close(outfd); /* automatically releases the lock */
329 return RLM_MODULE_FAIL;
335 fseek(outfp, 0L, SEEK_END);
336 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
337 fprintf(outfp, "%s\n", timestamp);
340 * Write the information to the file.
344 * Print out names, if they're OK.
347 if ((packet->code > 0) &&
348 (packet->code < FR_MAX_PACKET_CODE)) {
349 fprintf(outfp, "\tPacket-Type = %s\n",
350 fr_packet_codes[packet->code]);
352 fprintf(outfp, "\tPacket-Type = %d\n", packet->code);
356 if (inst->log_srcdst) {
357 VALUE_PAIR src_vp, dst_vp;
359 memset(&src_vp, 0, sizeof(src_vp));
360 memset(&dst_vp, 0, sizeof(dst_vp));
361 src_vp.operator = dst_vp.operator = T_OP_EQ;
363 switch (packet->src_ipaddr.af) {
365 src_vp.type = PW_TYPE_IPADDR;
366 src_vp.attribute = PW_PACKET_SRC_IP_ADDRESS;
367 src_vp.vp_ipaddr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
368 dst_vp.type = PW_TYPE_IPADDR;
369 dst_vp.attribute = PW_PACKET_DST_IP_ADDRESS;
370 dst_vp.vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
373 src_vp.type = PW_TYPE_IPV6ADDR;
374 src_vp.attribute = PW_PACKET_SRC_IPV6_ADDRESS;
375 memcpy(src_vp.vp_strvalue,
376 &packet->src_ipaddr.ipaddr.ip6addr,
377 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
378 dst_vp.type = PW_TYPE_IPV6ADDR;
379 dst_vp.attribute = PW_PACKET_DST_IPV6_ADDRESS;
380 memcpy(dst_vp.vp_strvalue,
381 &packet->dst_ipaddr.ipaddr.ip6addr,
382 sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
389 vp_print(outfp, &src_vp);
392 vp_print(outfp, &dst_vp);
395 src_vp.attribute = PW_PACKET_SRC_PORT;
396 src_vp.type = PW_TYPE_INTEGER;
397 src_vp.vp_integer = packet->src_port;
398 dst_vp.attribute = PW_PACKET_DST_PORT;
399 dst_vp.type = PW_TYPE_INTEGER;
400 dst_vp.vp_integer = packet->dst_port;
403 vp_print(outfp, &src_vp);
406 vp_print(outfp, &dst_vp);
410 /* Write each attribute/value to the log file */
411 for (pair = packet->vps; pair != NULL; pair = pair->next) {
413 da.attr = pair->attribute;
416 fr_hash_table_finddata(inst->ht, &da)) continue;
419 * Don't print passwords in old format...
421 if (compat && (pair->attribute == PW_USER_PASSWORD)) continue;
424 * Print all of the attributes.
427 vp_print(outfp, pair);
432 * Add non-protocol attibutes.
436 if (request->proxy) {
437 char proxy_buffer[128];
439 inet_ntop(request->proxy->dst_ipaddr.af,
440 &request->proxy->dst_ipaddr.ipaddr,
441 proxy_buffer, sizeof(proxy_buffer));
442 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
444 RDEBUG("Freeradius-Proxied-To = %s",
449 fprintf(outfp, "\tTimestamp = %ld\n",
450 (unsigned long) request->timestamp);
453 * We no longer permit Accounting-Request packets
454 * with an authenticator of zero.
456 fputs("\tRequest-Authenticator = Verified\n", outfp);
463 lseek(outfd, 0L, SEEK_SET);
464 rad_unlockfd(outfd, 0);
465 RDEBUG2("Released filelock");
471 * And everything is fine.
473 return RLM_MODULE_OK;
477 * Accounting - write the detail files.
479 static int detail_accounting(void *instance, REQUEST *request)
481 if (request->listener->type == RAD_LISTEN_DETAIL &&
482 strcmp(((struct detail_instance *)instance)->detailfile,
483 ((listen_detail_t *)request->listener->data)->filename) == 0) {
484 RDEBUG("Suppressing writes to detail file as the request was just read from a detail file.");
485 return RLM_MODULE_NOOP;
488 return do_detail(instance,request,request->packet, TRUE);
492 * Incoming Access Request - write the detail files.
494 static int detail_authorize(void *instance, REQUEST *request)
496 return do_detail(instance,request,request->packet, FALSE);
500 * Outgoing Access-Request Reply - write the detail files.
502 static int detail_postauth(void *instance, REQUEST *request)
504 return do_detail(instance,request,request->reply, FALSE);
509 * Incoming CoA - write the detail files.
511 static int detail_recv_coa(void *instance, REQUEST *request)
513 return do_detail(instance,request,request->packet, FALSE);
517 * Outgoing CoA - write the detail files.
519 static int detail_send_coa(void *instance, REQUEST *request)
521 return do_detail(instance,request,request->reply, FALSE);
526 * Outgoing Access-Request to home server - write the detail files.
529 static int detail_pre_proxy(void *instance, REQUEST *request)
531 if (request->proxy &&
532 request->proxy->vps) {
533 return do_detail(instance,request,request->proxy, FALSE);
536 return RLM_MODULE_NOOP;
541 * Outgoing Access-Request Reply - write the detail files.
543 static int detail_post_proxy(void *instance, REQUEST *request)
545 if (request->proxy_reply &&
546 request->proxy_reply->vps) {
547 return do_detail(instance,request,request->proxy_reply, FALSE);
551 * No reply: we must be doing Post-Proxy-Type = Fail.
553 * Note that we just call the normal accounting function,
554 * to minimize the amount of code, and to highlight that
555 * it's doing normal accounting.
557 if (!request->proxy_reply) {
560 rcode = detail_accounting(instance, request);
561 if (rcode == RLM_MODULE_OK) {
562 request->reply->code = PW_ACCOUNTING_RESPONSE;
567 return RLM_MODULE_NOOP;
572 /* globally exported name */
573 module_t rlm_detail = {
576 RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,
577 detail_instantiate, /* instantiation */
578 detail_detach, /* detach */
580 NULL, /* authentication */
581 detail_authorize, /* authorization */
582 NULL, /* preaccounting */
583 detail_accounting, /* accounting */
584 NULL, /* checksimul */
586 detail_pre_proxy, /* pre-proxy */
587 detail_post_proxy, /* post-proxy */
591 detail_postauth /* post-auth */