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>
36 static const char *packet_codes[] = {
42 "Accounting-Response",
52 struct detail_instance {
56 /* detail file permissions */
59 /* directory permissions */
62 /* last made directory */
63 char *last_made_directory;
65 /* timestamp & stuff */
68 /* if we want file locking */
71 /* log src/dst information */
77 static const CONF_PARSER module_config[] = {
78 { "detailfile", PW_TYPE_STRING_PTR,
79 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
80 { "header", PW_TYPE_STRING_PTR,
81 offsetof(struct detail_instance,header), NULL, "%t" },
82 { "detailperm", PW_TYPE_INTEGER,
83 offsetof(struct detail_instance,detailperm), NULL, "0600" },
84 { "dirperm", PW_TYPE_INTEGER,
85 offsetof(struct detail_instance,dirperm), NULL, "0755" },
86 { "locking", PW_TYPE_BOOLEAN,
87 offsetof(struct detail_instance,locking), NULL, "no" },
88 { "log_packet_header", PW_TYPE_BOOLEAN,
89 offsetof(struct detail_instance,log_srcdst), NULL, "no" },
90 { NULL, -1, 0, NULL, NULL }
97 static int detail_detach(void *instance)
99 struct detail_instance *inst = instance;
100 free((char*) inst->last_made_directory);
101 if (inst->ht) fr_hash_table_free(inst->ht);
108 static uint32_t detail_hash(const void *data)
110 const DICT_ATTR *da = data;
111 return fr_hash(&(da->attr), sizeof(da->attr));
114 static int detail_cmp(const void *a, const void *b)
116 return ((const DICT_ATTR *)a)->attr - ((const DICT_ATTR *)b)->attr;
121 * (Re-)read radiusd.conf into memory.
123 static int detail_instantiate(CONF_SECTION *conf, void **instance)
125 struct detail_instance *inst;
128 inst = rad_malloc(sizeof(*inst));
132 memset(inst, 0, sizeof(*inst));
134 if (cf_section_parse(conf, inst, module_config) < 0) {
139 inst->last_made_directory = NULL;
142 * Suppress certain attributes.
144 cs = cf_section_sub_find(conf, "suppress");
148 inst->ht = fr_hash_table_create(detail_hash, detail_cmp,
151 for (ci = cf_item_find_next(cs, NULL);
153 ci = cf_item_find_next(cs, ci)) {
157 if (!cf_item_is_pair(ci)) continue;
159 attr = cf_pair_attr(cf_itemtopair(ci));
160 if (!attr) continue; /* pair-anoia */
162 da = dict_attrbyname(attr);
164 radlog(L_INFO, "rlm_detail: WARNING: No such attribute %s: Cannot suppress printing it.", attr);
169 * For better distribution we should really
170 * hash the attribute number or name. But
171 * since the suppression list will usually
172 * be small, it doesn't matter.
174 if (!fr_hash_table_insert(inst->ht, da)) {
175 radlog(L_ERR, "rlm_detail: Failed trying to remember %s", attr);
188 * Do detail, compatible with old accounting
190 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
204 struct detail_instance *inst = instance;
206 rad_assert(request != NULL);
209 * Nothing to log: don't do anything.
212 return RLM_MODULE_NOOP;
216 * Create a directory for this nas.
218 * Generate the path for the detail file. Use the
219 * same format, but truncate at the last /. Then
220 * feed it through radius_xlat() to expand the
223 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
224 RDEBUG2("%s expands to %s", inst->detailfile, buffer);
227 * Grab the last directory delimiter.
229 p = strrchr(buffer,'/');
232 * There WAS a directory delimiter there, and
233 * the file doesn't exist, so
234 * we prolly must create it the dir(s)
236 if ((p) && (stat(buffer, &st) < 0)) {
239 * NO previously cached directory name, so we've
240 * got to create a new one.
242 * OR the new directory name is different than the old,
243 * so we've got to create a new one.
245 * OR the cached directory has somehow gotten removed,
246 * so we've got to create a new one.
248 if ((inst->last_made_directory == NULL) ||
249 (strcmp(inst->last_made_directory, buffer) != 0)) {
250 free((char *) inst->last_made_directory);
251 inst->last_made_directory = strdup(buffer);
255 * stat the directory, and don't do anything if
256 * it exists. If it doesn't exist, create it.
258 * This also catches the case where some idiot
259 * deleted a directory that the server was using.
261 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
262 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
263 return RLM_MODULE_FAIL;
267 } /* else there was no directory delimiter. */
273 * Open & create the file, with the given
276 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
277 inst->detailperm)) < 0) {
278 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
279 buffer, strerror(errno));
280 return RLM_MODULE_FAIL;
284 * If we fail to aquire the filelock in 80 tries
285 * (approximately two seconds) we bail out.
288 lseek(outfd, 0L, SEEK_SET);
289 if (rad_lockfd_nonblock(outfd, 0) < 0) {
293 select(0, NULL, NULL, NULL, &tv);
299 * The file might have been deleted by
300 * radrelay while we tried to acquire
301 * the lock (race condition)
303 if (fstat(outfd, &st) != 0) {
304 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't stat file %s: %s",
305 buffer, strerror(errno));
307 return RLM_MODULE_FAIL;
309 if (st.st_nlink == 0) {
310 RDEBUG2("File %s removed by another program, retrying",
317 RDEBUG2("Acquired filelock, tried %d time(s)",
321 } while (inst->locking && !locked && lock_count < 80);
323 if (inst->locking && !locked) {
325 radlog_request(L_ERR, 0, request, "rlm_detail: Failed to acquire filelock for %s, giving up",
327 return RLM_MODULE_FAIL;
331 * Convert the FD to FP. The FD is no longer valid
332 * after this operation.
334 if ((outfp = fdopen(outfd, "a")) == NULL) {
335 radlog_request(L_ERR, 0, request, "rlm_detail: Couldn't open file %s: %s",
336 buffer, strerror(errno));
338 lseek(outfd, 0L, SEEK_SET);
339 rad_unlockfd(outfd, 0);
340 RDEBUG2("Released filelock");
342 close(outfd); /* automatically releases the lock */
344 return RLM_MODULE_FAIL;
350 fseek(outfp, 0L, SEEK_END);
351 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
352 fprintf(outfp, "%s\n", timestamp);
355 * Write the information to the file.
359 * Print out names, if they're OK.
362 if ((packet->code > 0) &&
363 (packet->code <= PW_ACCESS_CHALLENGE)) {
364 fprintf(outfp, "\tPacket-Type = %s\n",
365 packet_codes[packet->code]);
367 fprintf(outfp, "\tPacket-Type = %d\n", packet->code);
371 if (inst->log_srcdst) {
372 VALUE_PAIR src_vp, dst_vp;
374 memset(&src_vp, 0, sizeof(src_vp));
375 memset(&dst_vp, 0, sizeof(dst_vp));
376 src_vp.operator = dst_vp.operator = T_OP_EQ;
378 switch (packet->src_ipaddr.af) {
380 src_vp.type = PW_TYPE_IPADDR;
381 src_vp.attribute = PW_PACKET_SRC_IP_ADDRESS;
382 src_vp.vp_ipaddr = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
383 dst_vp.type = PW_TYPE_IPADDR;
384 dst_vp.attribute = PW_PACKET_DST_IP_ADDRESS;
385 dst_vp.vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
388 src_vp.type = PW_TYPE_IPV6ADDR;
389 src_vp.attribute = PW_PACKET_SRC_IPV6_ADDRESS;
390 memcpy(src_vp.vp_strvalue,
391 &packet->src_ipaddr.ipaddr.ip6addr,
392 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
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));
404 vp_print(outfp, &src_vp);
407 vp_print(outfp, &dst_vp);
410 src_vp.attribute = PW_PACKET_SRC_PORT;
411 src_vp.type = PW_TYPE_INTEGER;
412 src_vp.vp_integer = packet->src_port;
413 dst_vp.attribute = PW_PACKET_DST_PORT;
414 dst_vp.type = PW_TYPE_INTEGER;
415 dst_vp.vp_integer = packet->dst_port;
418 vp_print(outfp, &src_vp);
421 vp_print(outfp, &dst_vp);
425 /* Write each attribute/value to the log file */
426 for (pair = packet->vps; pair != NULL; pair = pair->next) {
428 da.attr = pair->attribute;
431 fr_hash_table_finddata(inst->ht, &da)) continue;
434 * Don't print passwords in old format...
436 if (compat && (pair->attribute == PW_USER_PASSWORD)) continue;
439 * Print all of the attributes.
442 vp_print(outfp, pair);
447 * Add non-protocol attibutes.
450 if (request->proxy) {
451 char proxy_buffer[128];
453 inet_ntop(request->proxy->dst_ipaddr.af,
454 &request->proxy->dst_ipaddr.ipaddr,
455 proxy_buffer, sizeof(proxy_buffer));
456 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
458 RDEBUG("Freeradius-Proxied-To = %s",
462 fprintf(outfp, "\tTimestamp = %ld\n",
463 (unsigned long) request->timestamp);
466 * We no longer permit Accounting-Request packets
467 * with an authenticator of zero.
469 fputs("\tRequest-Authenticator = Verified\n", outfp);
476 lseek(outfd, 0L, SEEK_SET);
477 rad_unlockfd(outfd, 0);
478 RDEBUG2("Released filelock");
484 * And everything is fine.
486 return RLM_MODULE_OK;
490 * Accounting - write the detail files.
492 static int detail_accounting(void *instance, REQUEST *request)
494 if (request->listener->type == RAD_LISTEN_DETAIL) {
495 RDEBUG("Suppressing writes to detail file as the request was just read from a detail file.");
496 return RLM_MODULE_NOOP;
499 return do_detail(instance,request,request->packet, TRUE);
503 * Incoming Access Request - write the detail files.
505 static int detail_authorize(void *instance, REQUEST *request)
507 return do_detail(instance,request,request->packet, FALSE);
511 * Outgoing Access-Request Reply - write the detail files.
513 static int detail_postauth(void *instance, REQUEST *request)
515 return do_detail(instance,request,request->reply, FALSE);
520 * Outgoing Access-Request to home server - write the detail files.
522 static int detail_pre_proxy(void *instance, REQUEST *request)
524 if (request->proxy &&
525 request->proxy->vps) {
526 return do_detail(instance,request,request->proxy, FALSE);
529 return RLM_MODULE_NOOP;
534 * Outgoing Access-Request Reply - write the detail files.
536 static int detail_post_proxy(void *instance, REQUEST *request)
538 if (request->proxy_reply &&
539 request->proxy_reply->vps) {
540 return do_detail(instance,request,request->proxy_reply, FALSE);
544 * No reply: we must be doing Post-Proxy-Type = Fail.
546 * Note that we just call the normal accounting function,
547 * to minimize the amount of code, and to highlight that
548 * it's doing normal accounting.
550 if (!request->proxy_reply) {
553 rcode = detail_accounting(instance, request);
554 if (rcode == RLM_MODULE_OK) {
555 request->reply->code = PW_ACCOUNTING_RESPONSE;
560 return RLM_MODULE_NOOP;
564 /* globally exported name */
565 module_t rlm_detail = {
568 RLM_TYPE_THREAD_UNSAFE | RLM_TYPE_CHECK_CONFIG_SAFE | RLM_TYPE_HUP_SAFE,
569 detail_instantiate, /* instantiation */
570 detail_detach, /* detach */
572 NULL, /* authentication */
573 detail_authorize, /* authorization */
574 NULL, /* preaccounting */
575 detail_accounting, /* accounting */
576 NULL, /* checksimul */
577 detail_pre_proxy, /* pre-proxy */
578 detail_post_proxy, /* post-proxy */
579 detail_postauth /* post-auth */