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/autoconf.h>
29 #include <sys/select.h>
36 #include <freeradius-devel/radiusd.h>
37 #include <freeradius-devel/modules.h>
38 #include <freeradius-devel/rad_assert.h>
42 static const char *packet_codes[] = {
48 "Accounting-Response",
58 struct detail_instance {
62 /* detail file permissions */
65 /* directory permissions */
68 /* last made directory */
69 char *last_made_directory;
71 /* timestamp & stuff */
74 /* if we want file locking */
77 /* log src/dst information */
80 lrad_hash_table_t *ht;
83 static const CONF_PARSER module_config[] = {
84 { "detailfile", PW_TYPE_STRING_PTR,
85 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
86 { "header", PW_TYPE_STRING_PTR,
87 offsetof(struct detail_instance,header), NULL, "%t" },
88 { "detailperm", PW_TYPE_INTEGER,
89 offsetof(struct detail_instance,detailperm), NULL, "0600" },
90 { "dirperm", PW_TYPE_INTEGER,
91 offsetof(struct detail_instance,dirperm), NULL, "0755" },
92 { "locking", PW_TYPE_BOOLEAN,
93 offsetof(struct detail_instance,locking), NULL, "no" },
94 { "log_packet_header", PW_TYPE_BOOLEAN,
95 offsetof(struct detail_instance,log_srcdst), NULL, "no" },
96 { NULL, -1, 0, NULL, NULL }
103 static int detail_detach(void *instance)
105 struct detail_instance *inst = instance;
106 free((char*) inst->last_made_directory);
107 if (inst->ht) lrad_hash_table_free(inst->ht);
114 static uint32_t detail_hash(const void *data)
116 const DICT_ATTR *da = data;
117 return lrad_hash(&(da->attr), sizeof(da->attr));
120 static int detail_cmp(const void *a, const void *b)
122 return ((const DICT_ATTR *)a)->attr - ((const DICT_ATTR *)b)->attr;
127 * (Re-)read radiusd.conf into memory.
129 static int detail_instantiate(CONF_SECTION *conf, void **instance)
131 struct detail_instance *inst;
134 inst = rad_malloc(sizeof(*inst));
138 memset(inst, 0, sizeof(*inst));
140 if (cf_section_parse(conf, inst, module_config) < 0) {
145 inst->last_made_directory = NULL;
148 * Suppress certain attributes.
150 cs = cf_section_sub_find(conf, "suppress");
154 inst->ht = lrad_hash_table_create(detail_hash, detail_cmp,
157 for (ci = cf_item_find_next(cs, NULL);
159 ci = cf_item_find_next(cs, ci)) {
163 if (!cf_item_is_pair(ci)) continue;
165 attr = cf_pair_attr(cf_itemtopair(ci));
166 if (!attr) continue; /* pair-anoia */
168 da = dict_attrbyname(attr);
170 radlog(L_INFO, "rlm_detail: WARNING: No such attribute %s: Cannot suppress printing it.", attr);
175 * For better distribution we should really
176 * hash the attribute number or name. But
177 * since the suppression list will usually
178 * be small, it doesn't matter.
180 if (!lrad_hash_table_insert(inst->ht, da)) {
181 radlog(L_ERR, "rlm_detail: Failed trying to remember %s", attr);
194 * Do detail, compatible with old accounting
196 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
209 char proxy_buffer[16];
210 VALUE_PAIR *pair = packet->vps;
212 struct detail_instance *inst = instance;
215 * Nothing to log: don't do anything.
218 return RLM_MODULE_NOOP;
222 * Create a directory for this nas.
224 * Generate the path for the detail file. Use the
225 * same format, but truncate at the last /. Then
226 * feed it through radius_xlat() to expand the
229 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
230 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
233 * Grab the last directory delimiter.
235 p = strrchr(buffer,'/');
238 * There WAS a directory delimiter there, and
239 * the file doesn't exist, so
240 * we prolly must create it the dir(s)
242 if ((p) && (stat(buffer, &st) < 0)) {
245 * NO previously cached directory name, so we've
246 * got to create a new one.
248 * OR the new directory name is different than the old,
249 * so we've got to create a new one.
251 * OR the cached directory has somehow gotten removed,
252 * so we've got to create a new one.
254 if ((inst->last_made_directory == NULL) ||
255 (strcmp(inst->last_made_directory, buffer) != 0)) {
256 free((char *) inst->last_made_directory);
257 inst->last_made_directory = strdup(buffer);
261 * stat the directory, and don't do anything if
262 * it exists. If it doesn't exist, create it.
264 * This also catches the case where some idiot
265 * deleted a directory that the server was using.
267 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
268 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
269 return RLM_MODULE_FAIL;
273 } /* else there was no directory delimiter. */
279 * Open & create the file, with the given
282 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
283 inst->detailperm)) < 0) {
284 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
285 buffer, strerror(errno));
286 return RLM_MODULE_FAIL;
290 * If we fail to aquire the filelock in 80 tries
291 * (approximately two seconds) we bail out.
294 lseek(outfd, 0L, SEEK_SET);
295 if (rad_lockfd_nonblock(outfd, 0) < 0) {
299 select(0, NULL, NULL, NULL, &tv);
305 * The file might have been deleted by
306 * radrelay while we tried to acquire
307 * the lock (race condition)
309 if (fstat(outfd, &st) != 0) {
310 radlog(L_ERR, "rlm_detail: Couldn't stat file %s: %s",
311 buffer, strerror(errno));
313 return RLM_MODULE_FAIL;
315 if (st.st_nlink == 0) {
316 DEBUG("rlm_detail: File %s removed by another program, retrying",
323 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
327 } while (inst->locking && !locked && lock_count < 80);
329 if (inst->locking && !locked) {
331 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
333 return RLM_MODULE_FAIL;
337 * Convert the FD to FP. The FD is no longer valid
338 * after this operation.
340 if ((outfp = fdopen(outfd, "a")) == NULL) {
341 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
342 buffer, strerror(errno));
344 lseek(outfd, 0L, SEEK_SET);
345 rad_unlockfd(outfd, 0);
346 DEBUG("rlm_detail: Released filelock");
348 close(outfd); /* automatically releases the lock */
350 return RLM_MODULE_FAIL;
356 fseek(outfp, 0L, SEEK_END);
357 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
358 fprintf(outfp, "%s\n", timestamp);
361 * Write the information to the file.
365 * Print out names, if they're OK.
368 if ((packet->code > 0) &&
369 (packet->code <= PW_ACCESS_CHALLENGE)) {
370 fprintf(outfp, "\tPacket-Type = %s\n",
371 packet_codes[packet->code]);
373 fprintf(outfp, "\tPacket-Type = %d\n", packet->code);
377 if (inst->log_srcdst) {
378 VALUE_PAIR src_vp, dst_vp;
380 src_vp.name[0] = dst_vp.name[0] = '\0'; /* for vp_prints() */
381 src_vp.operator = dst_vp.operator = T_OP_EQ;
383 switch (packet->src_ipaddr.af) {
385 src_vp.type = PW_TYPE_IPADDR;
386 src_vp.attribute = PW_PACKET_SRC_IP_ADDRESS;
387 src_vp.lvalue = packet->src_ipaddr.ipaddr.ip4addr.s_addr;
388 dst_vp.type = PW_TYPE_IPADDR;
389 dst_vp.attribute = PW_PACKET_DST_IP_ADDRESS;
390 dst_vp.lvalue = packet->dst_ipaddr.ipaddr.ip4addr.s_addr;
393 src_vp.type = PW_TYPE_IPV6ADDR;
394 src_vp.attribute = PW_PACKET_SRC_IPV6_ADDRESS;
395 memcpy(src_vp.vp_strvalue,
396 &packet->src_ipaddr.ipaddr.ip6addr,
397 sizeof(packet->src_ipaddr.ipaddr.ip6addr));
398 dst_vp.type = PW_TYPE_IPV6ADDR;
399 dst_vp.attribute = PW_PACKET_DST_IPV6_ADDRESS;
400 memcpy(dst_vp.vp_strvalue,
401 &packet->dst_ipaddr.ipaddr.ip6addr,
402 sizeof(packet->dst_ipaddr.ipaddr.ip6addr));
409 vp_print(outfp, &src_vp);
412 vp_print(outfp, &dst_vp);
415 src_vp.attribute = PW_PACKET_SRC_PORT;
416 src_vp.type = PW_TYPE_INTEGER;
417 src_vp.lvalue = packet->src_port;
418 dst_vp.attribute = PW_PACKET_DST_PORT;
419 dst_vp.type = PW_TYPE_INTEGER;
420 dst_vp.lvalue = packet->dst_port;
423 vp_print(outfp, &src_vp);
426 vp_print(outfp, &dst_vp);
430 /* Write each attribute/value to the log file */
431 for (; pair != NULL; pair = pair->next) {
433 da.attr = pair->attribute;
436 lrad_hash_table_finddata(inst->ht, &da)) continue;
439 * Don't print passwords in old format...
441 if (compat && (pair->attribute == PW_USER_PASSWORD)) continue;
444 * Print all of the attributes.
447 vp_print(outfp, pair);
452 * Add non-protocol attibutes.
455 if ((pair = pairfind(request->config_items,
456 PW_PROXY_TO_REALM)) != NULL) {
457 proxy_realm = realm_find(pair->vp_strvalue, TRUE);
459 memset((char *) proxy_buffer, 0, 16);
461 rad_assert(proxy_realm->acct_ipaddr.af == AF_INET);
463 inet_ntop(proxy_realm->acct_ipaddr.af,
464 &proxy_realm->acct_ipaddr.ipaddr,
465 proxy_buffer, sizeof(proxy_buffer));
466 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
468 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
472 fprintf(outfp, "\tTimestamp = %ld\n",
473 (unsigned long) request->timestamp);
475 if (request->packet->verified == 2)
476 fputs("\tRequest-Authenticator = Verified\n", outfp);
477 else if (request->packet->verified == 1)
478 fputs("\tRequest-Authenticator = None\n", outfp);
485 lseek(outfd, 0L, SEEK_SET);
486 rad_unlockfd(outfd, 0);
487 DEBUG("rlm_detail: Released filelock");
493 * And everything is fine.
495 return RLM_MODULE_OK;
499 * Accounting - write the detail files.
501 static int detail_accounting(void *instance, REQUEST *request)
504 return do_detail(instance,request,request->packet, TRUE);
508 * Incoming Access Request - write the detail files.
510 static int detail_authorize(void *instance, REQUEST *request)
512 return do_detail(instance,request,request->packet, FALSE);
516 * Outgoing Access-Request Reply - write the detail files.
518 static int detail_postauth(void *instance, REQUEST *request)
520 return do_detail(instance,request,request->reply, FALSE);
525 * Outgoing Access-Request to home server - write the detail files.
527 static int detail_pre_proxy(void *instance, REQUEST *request)
529 if (request->proxy &&
530 request->proxy->vps) {
531 return do_detail(instance,request,request->proxy, FALSE);
534 return RLM_MODULE_NOOP;
539 * Outgoing Access-Request Reply - write the detail files.
541 static int detail_post_proxy(void *instance, REQUEST *request)
543 if (request->proxy_reply &&
544 request->proxy_reply->vps) {
545 return do_detail(instance,request,request->proxy_reply, FALSE);
548 return RLM_MODULE_NOOP;
552 /* globally exported name */
553 module_t rlm_detail = {
556 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
557 detail_instantiate, /* instantiation */
558 detail_detach, /* detach */
560 NULL, /* authentication */
561 detail_authorize, /* authorization */
562 NULL, /* preaccounting */
563 detail_accounting, /* accounting */
564 NULL, /* checksimul */
565 detail_pre_proxy, /* pre-proxy */
566 detail_post_proxy, /* post-proxy */
567 detail_postauth /* post-auth */