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 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
25 #include <freeradius-devel/autoconf.h>
28 #include <sys/select.h>
35 #include <freeradius-devel/radiusd.h>
36 #include <freeradius-devel/modules.h>
37 #include <freeradius-devel/rad_assert.h>
41 static const char *packet_codes[] = {
47 "Accounting-Response",
57 struct detail_instance {
61 /* detail file permissions */
64 /* directory permissions */
67 /* last made directory */
68 char *last_made_directory;
70 /* timestamp & stuff */
73 /* if we want file locking */
76 lrad_hash_table_t *ht;
79 static const CONF_PARSER module_config[] = {
80 { "detailfile", PW_TYPE_STRING_PTR,
81 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
82 { "header", PW_TYPE_STRING_PTR,
83 offsetof(struct detail_instance,header), NULL, "%t" },
84 { "detailperm", PW_TYPE_INTEGER,
85 offsetof(struct detail_instance,detailperm), NULL, "0600" },
86 { "dirperm", PW_TYPE_INTEGER,
87 offsetof(struct detail_instance,dirperm), NULL, "0755" },
88 { "locking", PW_TYPE_BOOLEAN,
89 offsetof(struct detail_instance,locking), 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->detailfile);
102 free((char*) inst->last_made_directory);
104 if (inst->ht) lrad_hash_table_free(inst->ht);
111 static uint32_t detail_hash(const void *data)
113 const DICT_ATTR *da = data;
114 return lrad_hash(&(da->attr), sizeof(da->attr));
117 static int detail_cmp(const void *a, const void *b)
119 return ((const DICT_ATTR *)a)->attr - ((const DICT_ATTR *)b)->attr;
124 * (Re-)read radiusd.conf into memory.
126 static int detail_instantiate(CONF_SECTION *conf, void **instance)
128 struct detail_instance *inst;
131 inst = rad_malloc(sizeof(*inst));
135 memset(inst, 0, sizeof(*inst));
137 if (cf_section_parse(conf, inst, module_config) < 0) {
142 inst->last_made_directory = NULL;
145 * Suppress certain attributes.
147 cs = cf_section_sub_find(conf, "suppress");
151 inst->ht = lrad_hash_table_create(detail_hash, detail_cmp,
154 for (ci = cf_item_find_next(cs, NULL);
156 ci = cf_item_find_next(cs, ci)) {
160 if (!cf_item_is_pair(ci)) continue;
162 attr = cf_pair_attr(cf_itemtopair(ci));
163 if (!attr) continue; /* pair-anoia */
165 da = dict_attrbyname(attr);
167 radlog(L_INFO, "rlm_detail: WARNING: No such attribute %s: Cannot suppress printing it.", attr);
172 * For better distribution we should really
173 * hash the attribute number or name. But
174 * since the suppression list will usually
175 * be small, it doesn't matter.
177 if (!lrad_hash_table_insert(inst->ht, da)) {
178 radlog(L_ERR, "rlm_detail: Failed trying to remember %s", attr);
191 * Do detail, compatible with old accounting
193 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
206 char proxy_buffer[16];
207 VALUE_PAIR *pair = packet->vps;
209 struct detail_instance *inst = instance;
212 * Nothing to log: don't do anything.
215 return RLM_MODULE_NOOP;
219 * Create a directory for this nas.
221 * Generate the path for the detail file. Use the
222 * same format, but truncate at the last /. Then
223 * feed it through radius_xlat() to expand the
226 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
227 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
230 * Grab the last directory delimiter.
232 p = strrchr(buffer,'/');
235 * There WAS a directory delimiter there, and
236 * the file doesn't exist, so
237 * we prolly must create it the dir(s)
239 if ((p) && (stat(buffer, &st) < 0)) {
242 * NO previously cached directory name, so we've
243 * got to create a new one.
245 * OR the new directory name is different than the old,
246 * so we've got to create a new one.
248 * OR the cached directory has somehow gotten removed,
249 * so we've got to create a new one.
251 if ((inst->last_made_directory == NULL) ||
252 (strcmp(inst->last_made_directory, buffer) != 0)) {
253 free((char *) inst->last_made_directory);
254 inst->last_made_directory = strdup(buffer);
258 * stat the directory, and don't do anything if
259 * it exists. If it doesn't exist, create it.
261 * This also catches the case where some idiot
262 * deleted a directory that the server was using.
264 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
265 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
266 return RLM_MODULE_FAIL;
270 } /* else there was no directory delimiter. */
276 * Open & create the file, with the given
279 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
280 inst->detailperm)) < 0) {
281 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
282 buffer, strerror(errno));
283 return RLM_MODULE_FAIL;
287 * If we fail to aquire the filelock in 80 tries
288 * (approximately two seconds) we bail out.
291 lseek(outfd, 0L, SEEK_SET);
292 if (rad_lockfd_nonblock(outfd, 0) < 0) {
296 select(0, NULL, NULL, NULL, &tv);
302 * The file might have been deleted by
303 * radrelay while we tried to acquire
304 * the lock (race condition)
306 if (fstat(outfd, &st) != 0) {
307 radlog(L_ERR, "rlm_detail: Couldn't stat file %s: %s",
308 buffer, strerror(errno));
310 return RLM_MODULE_FAIL;
312 if (st.st_nlink == 0) {
313 DEBUG("rlm_detail: File %s removed by another program, retrying",
320 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
324 } while (inst->locking && !locked && lock_count < 80);
326 if (inst->locking && !locked) {
328 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
330 return RLM_MODULE_FAIL;
334 * Convert the FD to FP. The FD is no longer valid
335 * after this operation.
337 if ((outfp = fdopen(outfd, "a")) == NULL) {
338 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
339 buffer, strerror(errno));
341 lseek(outfd, 0L, SEEK_SET);
342 rad_unlockfd(outfd, 0);
343 DEBUG("rlm_detail: Released filelock");
345 close(outfd); /* automatically releases the lock */
347 return RLM_MODULE_FAIL;
351 * Write the information to the file.
355 * Print out names, if they're OK.
358 if ((packet->code > 0) &&
359 (packet->code <= PW_ACCESS_CHALLENGE)) {
360 fprintf(outfp, "Packet-Type = %s\n",
361 packet_codes[packet->code]);
363 fprintf(outfp, "Packet-Type = %d\n", packet->code);
370 fseek(outfp, 0L, SEEK_END);
371 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
372 fprintf(outfp, "%s\n", timestamp);
374 /* Write each attribute/value to the log file */
375 for (; pair != NULL; pair = pair->next) {
377 da.attr = pair->attribute;
380 lrad_hash_table_finddata(inst->ht, &da)) continue;
383 * Don't print passwords in old format...
385 if (compat && (pair->attribute == PW_PASSWORD)) continue;
388 * Print all of the attributes.
391 vp_print(outfp, pair);
396 * Add non-protocol attibutes.
399 if ((pair = pairfind(request->config_items,
400 PW_PROXY_TO_REALM)) != NULL) {
401 proxy_realm = realm_find(pair->vp_strvalue, TRUE);
403 memset((char *) proxy_buffer, 0, 16);
405 rad_assert(proxy_realm->acct_ipaddr.af == AF_INET);
407 inet_ntop(proxy_realm->acct_ipaddr.af,
408 &proxy_realm->acct_ipaddr.ipaddr,
409 proxy_buffer, sizeof(proxy_buffer));
410 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
412 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
416 fprintf(outfp, "\tTimestamp = %ld\n",
417 (unsigned long) request->timestamp);
419 if (request->packet->verified == 2)
420 fputs("\tRequest-Authenticator = Verified\n", outfp);
421 else if (request->packet->verified == 1)
422 fputs("\tRequest-Authenticator = None\n", outfp);
429 lseek(outfd, 0L, SEEK_SET);
430 rad_unlockfd(outfd, 0);
431 DEBUG("rlm_detail: Released filelock");
437 * And everything is fine.
439 return RLM_MODULE_OK;
443 * Accounting - write the detail files.
445 static int detail_accounting(void *instance, REQUEST *request)
448 return do_detail(instance,request,request->packet, TRUE);
452 * Incoming Access Request - write the detail files.
454 static int detail_authorize(void *instance, REQUEST *request)
456 return do_detail(instance,request,request->packet, FALSE);
460 * Outgoing Access-Request Reply - write the detail files.
462 static int detail_postauth(void *instance, REQUEST *request)
464 return do_detail(instance,request,request->reply, FALSE);
469 * Outgoing Access-Request to home server - write the detail files.
471 static int detail_pre_proxy(void *instance, REQUEST *request)
473 if (request->proxy &&
474 request->proxy->vps) {
475 return do_detail(instance,request,request->proxy, FALSE);
478 return RLM_MODULE_NOOP;
483 * Outgoing Access-Request Reply - write the detail files.
485 static int detail_post_proxy(void *instance, REQUEST *request)
487 if (request->proxy_reply &&
488 request->proxy_reply->vps) {
489 return do_detail(instance,request,request->proxy_reply, FALSE);
492 return RLM_MODULE_NOOP;
496 /* globally exported name */
497 module_t rlm_detail = {
500 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
501 detail_instantiate, /* instantiation */
502 detail_detach, /* detach */
504 NULL, /* authentication */
505 detail_authorize, /* authorization */
506 NULL, /* preaccounting */
507 detail_accounting, /* accounting */
508 NULL, /* checksimul */
509 detail_pre_proxy, /* pre-proxy */
510 detail_post_proxy, /* post-proxy */
511 detail_postauth /* post-auth */