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 */
74 lrad_hash_table_t *ht;
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) lrad_hash_table_free(inst->ht);
108 static uint32_t detail_hash(const void *data)
110 const DICT_ATTR *da = data;
111 return lrad_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 = lrad_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 (!lrad_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;
207 * Nothing to log: don't do anything.
210 return RLM_MODULE_NOOP;
214 * Create a directory for this nas.
216 * Generate the path for the detail file. Use the
217 * same format, but truncate at the last /. Then
218 * feed it through radius_xlat() to expand the
221 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
222 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
225 * Grab the last directory delimiter.
227 p = strrchr(buffer,'/');
230 * There WAS a directory delimiter there, and
231 * the file doesn't exist, so
232 * we prolly must create it the dir(s)
234 if ((p) && (stat(buffer, &st) < 0)) {
237 * NO previously cached directory name, so we've
238 * got to create a new one.
240 * OR the new directory name is different than the old,
241 * so we've got to create a new one.
243 * OR the cached directory has somehow gotten removed,
244 * so we've got to create a new one.
246 if ((inst->last_made_directory == NULL) ||
247 (strcmp(inst->last_made_directory, buffer) != 0)) {
248 free((char *) inst->last_made_directory);
249 inst->last_made_directory = strdup(buffer);
253 * stat the directory, and don't do anything if
254 * it exists. If it doesn't exist, create it.
256 * This also catches the case where some idiot
257 * deleted a directory that the server was using.
259 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
260 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
261 return RLM_MODULE_FAIL;
265 } /* else there was no directory delimiter. */
271 * Open & create the file, with the given
274 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
275 inst->detailperm)) < 0) {
276 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
277 buffer, strerror(errno));
278 return RLM_MODULE_FAIL;
282 * If we fail to aquire the filelock in 80 tries
283 * (approximately two seconds) we bail out.
286 lseek(outfd, 0L, SEEK_SET);
287 if (rad_lockfd_nonblock(outfd, 0) < 0) {
291 select(0, NULL, NULL, NULL, &tv);
297 * The file might have been deleted by
298 * radrelay while we tried to acquire
299 * the lock (race condition)
301 if (fstat(outfd, &st) != 0) {
302 radlog(L_ERR, "rlm_detail: Couldn't stat file %s: %s",
303 buffer, strerror(errno));
305 return RLM_MODULE_FAIL;
307 if (st.st_nlink == 0) {
308 DEBUG("rlm_detail: File %s removed by another program, retrying",
315 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
319 } while (inst->locking && !locked && lock_count < 80);
321 if (inst->locking && !locked) {
323 radlog(L_ERR, "rlm_detail: Failed to acquire filelock for %s, giving up",
325 return RLM_MODULE_FAIL;
329 * Convert the FD to FP. The FD is no longer valid
330 * after this operation.
332 if ((outfp = fdopen(outfd, "a")) == NULL) {
333 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
334 buffer, strerror(errno));
336 lseek(outfd, 0L, SEEK_SET);
337 rad_unlockfd(outfd, 0);
338 DEBUG("rlm_detail: Released filelock");
340 close(outfd); /* automatically releases the lock */
342 return RLM_MODULE_FAIL;
348 fseek(outfp, 0L, SEEK_END);
349 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
350 fprintf(outfp, "%s\n", timestamp);
353 * Write the information to the file.
357 * Print out names, if they're OK.
360 if ((packet->code > 0) &&
361 (packet->code <= PW_ACCESS_CHALLENGE)) {
362 fprintf(outfp, "\tPacket-Type = %s\n",
363 packet_codes[packet->code]);
365 fprintf(outfp, "\tPacket-Type = %d\n", packet->code);
369 if (inst->log_srcdst) {
370 VALUE_PAIR src_vp, dst_vp;
372 src_vp.name[0] = dst_vp.name[0] = '\0'; /* for vp_prints() */
373 src_vp.operator = dst_vp.operator = T_OP_EQ;
375 switch (packet->src_ipaddr.af) {
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.type = PW_TYPE_IPADDR;
381 dst_vp.attribute = PW_PACKET_DST_IP_ADDRESS;
382 dst_vp.vp_ipaddr = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
385 src_vp.type = PW_TYPE_IPV6ADDR;
386 src_vp.attribute = PW_PACKET_SRC_IPV6_ADDRESS;
387 memcpy(src_vp.vp_strvalue,
388 &packet->src_ipaddr.ipaddr.ip6addr,
389 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
390 dst_vp.type = PW_TYPE_IPV6ADDR;
391 dst_vp.attribute = PW_PACKET_DST_IPV6_ADDRESS;
392 memcpy(dst_vp.vp_strvalue,
393 &packet->dst_ipaddr.ipaddr.ip6addr,
394 sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
401 vp_print(outfp, &src_vp);
404 vp_print(outfp, &dst_vp);
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.attribute = PW_PACKET_DST_PORT;
411 dst_vp.type = PW_TYPE_INTEGER;
412 dst_vp.vp_integer = packet->dst_port;
415 vp_print(outfp, &src_vp);
418 vp_print(outfp, &dst_vp);
422 /* Write each attribute/value to the log file */
423 for (pair = packet->vps; pair != NULL; pair = pair->next) {
425 da.attr = pair->attribute;
428 lrad_hash_table_finddata(inst->ht, &da)) continue;
431 * Don't print passwords in old format...
433 if (compat && (pair->attribute == PW_USER_PASSWORD)) continue;
436 * Print all of the attributes.
439 vp_print(outfp, pair);
444 * Add non-protocol attibutes.
447 if (request->proxy) {
448 char proxy_buffer[128];
450 inet_ntop(request->proxy->dst_ipaddr.af,
451 &request->proxy->dst_ipaddr.ipaddr,
452 proxy_buffer, sizeof(proxy_buffer));
453 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
455 DEBUG("rlm_detail: Freeradius-Proxied-To = %s",
459 fprintf(outfp, "\tTimestamp = %ld\n",
460 (unsigned long) request->timestamp);
463 * We no longer permit Accounting-Request packets
464 * with an authenticator of zero.
466 fputs("\tRequest-Authenticator = Verified\n", outfp);
473 lseek(outfd, 0L, SEEK_SET);
474 rad_unlockfd(outfd, 0);
475 DEBUG("rlm_detail: Released filelock");
481 * And everything is fine.
483 return RLM_MODULE_OK;
487 * Accounting - write the detail files.
489 static int detail_accounting(void *instance, REQUEST *request)
491 if (request->listener->type == RAD_LISTEN_DETAIL) {
492 DEBUG2(" rlm_detail: Suppressing writes to detail file as the request was just read from a detail file.");
493 return RLM_MODULE_NOOP;
496 return do_detail(instance,request,request->packet, TRUE);
500 * Incoming Access Request - write the detail files.
502 static int detail_authorize(void *instance, REQUEST *request)
504 return do_detail(instance,request,request->packet, FALSE);
508 * Outgoing Access-Request Reply - write the detail files.
510 static int detail_postauth(void *instance, REQUEST *request)
512 return do_detail(instance,request,request->reply, FALSE);
517 * Outgoing Access-Request to home server - write the detail files.
519 static int detail_pre_proxy(void *instance, REQUEST *request)
521 if (request->proxy &&
522 request->proxy->vps) {
523 return do_detail(instance,request,request->proxy, FALSE);
526 return RLM_MODULE_NOOP;
531 * Outgoing Access-Request Reply - write the detail files.
533 static int detail_post_proxy(void *instance, REQUEST *request)
535 if (request->proxy_reply &&
536 request->proxy_reply->vps) {
537 return do_detail(instance,request,request->proxy_reply, FALSE);
541 * No reply: we must be doing Post-Proxy-Type = Fail.
543 * Note that we just call the normal accounting function,
544 * to minimize the amount of code, and to highlight that
545 * it's doing normal accounting.
547 if (!request->proxy_reply) {
548 return detail_accounting(instance, request);
551 return RLM_MODULE_NOOP;
555 /* globally exported name */
556 module_t rlm_detail = {
559 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
560 detail_instantiate, /* instantiation */
561 detail_detach, /* detach */
563 NULL, /* authentication */
564 detail_authorize, /* authorization */
565 NULL, /* preaccounting */
566 detail_accounting, /* accounting */
567 NULL, /* checksimul */
568 detail_pre_proxy, /* pre-proxy */
569 detail_post_proxy, /* post-proxy */
570 detail_postauth /* post-auth */