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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 * Copyright 2000 The FreeRADIUS server project
23 static const char rcsid[] = "$Id$";
28 #include <sys/select.h>
37 #include "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 */
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 { NULL, -1, 0, NULL, NULL }
92 * (Re-)read radiusd.conf into memory.
94 static int detail_instantiate(CONF_SECTION *conf, void **instance)
96 struct detail_instance *inst;
98 inst = rad_malloc(sizeof(*inst));
102 memset(inst, 0, sizeof(*inst));
104 if (cf_section_parse(conf, inst, module_config) < 0) {
109 inst->last_made_directory = NULL;
116 * Do detail, compatible with old accounting
118 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
131 char proxy_buffer[16];
132 VALUE_PAIR *pair = packet->vps;
134 struct detail_instance *inst = instance;
137 * Nothing to log: don't do anything.
140 return RLM_MODULE_NOOP;
144 * Create a directory for this nas.
146 * Generate the path for the detail file. Use the
147 * same format, but truncate at the last /. Then
148 * feed it through radius_xlat() to expand the
151 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
152 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
155 * Grab the last directory delimiter.
157 p = strrchr(buffer,'/');
160 * There WAS a directory delimiter there, and
161 * the file doesn't exist, so
162 * we prolly must create it the dir(s)
164 if ((p) && (stat(buffer, &st) < 0)) {
167 * NO previously cached directory name, so we've
168 * got to create a new one.
170 * OR the new directory name is different than the old,
171 * so we've got to create a new one.
173 * OR the cached directory has somehow gotten removed,
174 * so we've got to create a new one.
176 if ((inst->last_made_directory == NULL) ||
177 (strcmp(inst->last_made_directory, buffer) != 0)) {
180 * Free any previously cached name.
182 if (inst->last_made_directory != NULL) {
183 free((char *) inst->last_made_directory);
184 inst->last_made_directory = NULL;
188 * Go create possibly multiple directories.
190 if (rad_mkdir(buffer, inst->dirperm) < 0) {
191 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", buffer, strerror(errno));
192 return RLM_MODULE_FAIL;
194 inst->last_made_directory = strdup(buffer);
198 } /* else there was no directory delimiter. */
201 * Open & create the file, with the given permissions.
203 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
204 inst->detailperm)) < 0) {
205 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
206 buffer, strerror(errno));
207 return RLM_MODULE_FAIL;
211 * If we're not using locking, we'll just pass straight though
213 * If we fail to aquire the filelock in 80 tries (approximately
214 * two seconds) we bail out.
220 lseek(outfd, 0L, SEEK_SET);
221 if (rad_lockfd_nonblock(outfd, 0) < 0) {
224 select(0, NULL, NULL, NULL, &tv);
227 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
232 } while (!locked && inst->locking && lock_count < 80);
234 if (!locked && inst->locking && lock_count >= 80) {
236 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
238 return RLM_MODULE_FAIL;
242 * Convert the FD to FP. The FD is no longer valid
243 * after this operation.
245 if ((outfp = fdopen(outfd, "a")) == NULL) {
246 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
247 buffer, strerror(errno));
249 lseek(outfd, 0L, SEEK_SET);
250 rad_unlockfd(outfd, 0);
251 DEBUG("rlm_detail: Released filelock");
253 close(outfd); /* automatically releases the lock */
255 return RLM_MODULE_FAIL;
259 * Write the information to the file.
263 * Print out names, if they're OK.
266 if ((packet->code > 0) &&
267 (packet->code <= PW_ACCESS_CHALLENGE)) {
268 fprintf(outfp, "Packet-Type = %s\n",
269 packet_codes[packet->code]);
271 fprintf(outfp, "Packet-Type = %d\n", packet->code);
278 fseek(outfp, 0L, SEEK_END);
279 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
280 fprintf(outfp, "%s\n", timestamp);
282 /* Write each attribute/value to the log file */
285 * Don't print passwords in old format...
287 if (compat && (pair->attribute == PW_PASSWORD)) {
293 * Print all of the attributes.
296 vp_print(outfp, pair);
302 * Add non-protocol attibutes.
305 if ((pair = pairfind(request->config_items,
306 PW_PROXY_TO_REALM)) != NULL) {
307 proxy_realm = realm_find(pair->strvalue, TRUE);
309 memset((char *) proxy_buffer, 0, 16);
311 rad_assert(proxy_realm->acct_ipaddr.af == AF_INET);
313 inet_ntop(proxy_realm->acct_ipaddr.af,
314 &proxy_realm->acct_ipaddr.ipaddr,
315 proxy_buffer, sizeof(proxy_buffer));
316 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
318 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
322 fprintf(outfp, "\tTimestamp = %ld\n",
323 (unsigned long) request->timestamp);
325 if (request->packet->verified == 2)
326 fputs("\tRequest-Authenticator = Verified\n", outfp);
327 else if (request->packet->verified == 1)
328 fputs("\tRequest-Authenticator = None\n", outfp);
335 lseek(outfd, 0L, SEEK_SET);
336 rad_unlockfd(outfd, 0);
337 DEBUG("rlm_detail: Released filelock");
343 * And everything is fine.
345 return RLM_MODULE_OK;
349 * Accounting - write the detail files.
351 static int detail_accounting(void *instance, REQUEST *request)
354 return do_detail(instance,request,request->packet, TRUE);
358 * Incoming Access Request - write the detail files.
360 static int detail_authorize(void *instance, REQUEST *request)
362 return do_detail(instance,request,request->packet, FALSE);
366 * Outgoing Access-Request Reply - write the detail files.
368 static int detail_postauth(void *instance, REQUEST *request)
370 return do_detail(instance,request,request->reply, FALSE);
375 * Outgoing Access-Request to home server - write the detail files.
377 static int detail_pre_proxy(void *instance, REQUEST *request)
379 if (request->proxy &&
380 request->proxy->vps) {
381 return do_detail(instance,request,request->proxy, FALSE);
384 return RLM_MODULE_NOOP;
389 * Outgoing Access-Request Reply - write the detail files.
391 static int detail_post_proxy(void *instance, REQUEST *request)
393 if (request->proxy_reply &&
394 request->proxy_reply->vps) {
395 return do_detail(instance,request,request->proxy_reply, FALSE);
398 return RLM_MODULE_NOOP;
405 static int detail_detach(void *instance)
407 struct detail_instance *inst = instance;
408 free((char *) inst->detailfile);
410 if (inst->last_made_directory)
411 free((char*) inst->last_made_directory);
417 /* globally exported name */
418 module_t rlm_detail = {
420 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
421 NULL, /* initialization */
422 detail_instantiate, /* instantiation */
424 NULL, /* authentication */
425 detail_authorize, /* authorization */
426 NULL, /* preaccounting */
427 detail_accounting, /* accounting */
428 NULL, /* checksimul */
429 detail_pre_proxy, /* pre-proxy */
430 detail_post_proxy, /* post-proxy */
431 detail_postauth /* post-auth */
433 detail_detach, /* detach */