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$";
26 #include "libradius.h"
29 #include <sys/select.h>
40 static const char *packet_codes[] = {
46 "Accounting-Response",
56 struct detail_instance {
60 /* detail file permissions */
63 /* directory permissions */
66 /* last made directory */
67 char *last_made_directory;
69 /* timestamp & stuff */
72 /* if we want file locking */
76 static const CONF_PARSER module_config[] = {
77 { "detailfile", PW_TYPE_STRING_PTR,
78 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
79 { "header", PW_TYPE_STRING_PTR,
80 offsetof(struct detail_instance,header), NULL, "%t" },
81 { "detailperm", PW_TYPE_INTEGER,
82 offsetof(struct detail_instance,detailperm), NULL, "0600" },
83 { "dirperm", PW_TYPE_INTEGER,
84 offsetof(struct detail_instance,dirperm), NULL, "0755" },
85 { "locking", PW_TYPE_BOOLEAN,
86 offsetof(struct detail_instance,locking), NULL, "no" },
87 { NULL, -1, 0, NULL, NULL }
91 * (Re-)read radiusd.conf into memory.
93 static int detail_instantiate(CONF_SECTION *conf, void **instance)
95 struct detail_instance *inst;
97 inst = rad_malloc(sizeof(*inst));
101 memset(inst, 0, sizeof(*inst));
103 if (cf_section_parse(conf, inst, module_config) < 0) {
108 inst->last_made_directory = NULL;
115 * Do detail, compatible with old accounting
117 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
130 char proxy_buffer[16];
131 VALUE_PAIR *pair = packet->vps;
133 struct detail_instance *inst = instance;
136 * Nothing to log: don't do anything.
139 return RLM_MODULE_NOOP;
143 * Create a directory for this nas.
145 * Generate the path for the detail file. Use the
146 * same format, but truncate at the last /. Then
147 * feed it through radius_xlat() to expand the
150 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
151 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
154 * Grab the last directory delimiter.
156 p = strrchr(buffer,'/');
159 * There WAS a directory delimiter there, and
160 * the file doesn't exist, so
161 * we prolly must create it the dir(s)
163 if ((p) && (stat(buffer, &st) < 0)) {
166 * NO previously cached directory name, so we've
167 * got to create a new one.
169 * OR the new directory name is different than the old,
170 * so we've got to create a new one.
172 * OR the cached directory has somehow gotten removed,
173 * so we've got to create a new one.
175 if ((inst->last_made_directory == NULL) ||
176 (strcmp(inst->last_made_directory, buffer) != 0)) {
179 * Free any previously cached name.
181 if (inst->last_made_directory != NULL) {
182 free((char *) inst->last_made_directory);
183 inst->last_made_directory = NULL;
187 * Go create possibly multiple directories.
189 if (rad_mkdir(buffer, inst->dirperm) < 0) {
190 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", buffer, strerror(errno));
191 return RLM_MODULE_FAIL;
193 inst->last_made_directory = strdup(buffer);
197 } /* else there was no directory delimiter. */
200 * Open & create the file, with the given permissions.
202 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
203 inst->detailperm)) < 0) {
204 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
205 buffer, strerror(errno));
206 return RLM_MODULE_FAIL;
210 * If we're not using locking, we'll just pass straight though
212 * If we fail to aquire the filelock in 80 tries (approximately
213 * two seconds) we bail out.
219 lseek(outfd, 0L, SEEK_SET);
220 if (rad_lockfd_nonblock(outfd, 0) < 0) {
223 select(0, NULL, NULL, NULL, &tv);
226 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
231 } while (!locked && inst->locking && lock_count < 80);
233 if (!locked && inst->locking && lock_count >= 80) {
235 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
237 return RLM_MODULE_FAIL;
241 * Convert the FD to FP. The FD is no longer valid
242 * after this operation.
244 if ((outfp = fdopen(outfd, "a")) == NULL) {
245 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
246 buffer, strerror(errno));
248 lseek(outfd, 0L, SEEK_SET);
249 rad_unlockfd(outfd, 0);
250 DEBUG("rlm_detail: Released filelock");
252 close(outfd); /* automatically releases the lock */
254 return RLM_MODULE_FAIL;
258 * Write the information to the file.
262 * Print out names, if they're OK.
265 if ((packet->code > 0) &&
266 (packet->code <= PW_ACCESS_CHALLENGE)) {
267 fprintf(outfp, "Packet-Type = %s\n",
268 packet_codes[packet->code]);
270 fprintf(outfp, "Packet-Type = %d\n", packet->code);
277 fseek(outfp, 0L, SEEK_END);
278 radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
279 fprintf(outfp, "%s\n", timestamp);
281 /* Write each attribute/value to the log file */
284 * Don't print passwords in old format...
286 if (compat && (pair->attribute == PW_PASSWORD)) {
292 * Print all of the attributes.
295 vp_print(outfp, pair);
301 * Add non-protocol attibutes.
304 if ((pair = pairfind(request->config_items,
305 PW_PROXY_TO_REALM)) != NULL) {
306 proxy_realm = realm_find(pair->strvalue, TRUE);
308 memset((char *) proxy_buffer, 0, 16);
309 ip_ntoa(proxy_buffer, proxy_realm->acct_ipaddr);
310 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
312 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
316 fprintf(outfp, "\tTimestamp = %ld\n",
317 (unsigned long) request->timestamp);
319 if (request->packet->verified == 2)
320 fputs("\tRequest-Authenticator = Verified\n", outfp);
321 else if (request->packet->verified == 1)
322 fputs("\tRequest-Authenticator = None\n", outfp);
329 lseek(outfd, 0L, SEEK_SET);
330 rad_unlockfd(outfd, 0);
331 DEBUG("rlm_detail: Released filelock");
337 * And everything is fine.
339 return RLM_MODULE_OK;
343 * Accounting - write the detail files.
345 static int detail_accounting(void *instance, REQUEST *request)
348 return do_detail(instance,request,request->packet, TRUE);
352 * Incoming Access Request - write the detail files.
354 static int detail_authorize(void *instance, REQUEST *request)
356 return do_detail(instance,request,request->packet, FALSE);
360 * Outgoing Access-Request Reply - write the detail files.
362 static int detail_postauth(void *instance, REQUEST *request)
364 return do_detail(instance,request,request->reply, FALSE);
369 * Outgoing Access-Request to home server - write the detail files.
371 static int detail_pre_proxy(void *instance, REQUEST *request)
373 if (request->proxy &&
374 request->proxy->vps) {
375 return do_detail(instance,request,request->proxy, FALSE);
378 return RLM_MODULE_NOOP;
383 * Outgoing Access-Request Reply - write the detail files.
385 static int detail_post_proxy(void *instance, REQUEST *request)
387 if (request->proxy_reply &&
388 request->proxy_reply->vps) {
389 return do_detail(instance,request,request->proxy_reply, FALSE);
392 return RLM_MODULE_NOOP;
399 static int detail_detach(void *instance)
401 struct detail_instance *inst = instance;
402 free((char *) inst->detailfile);
404 if (inst->last_made_directory)
405 free((char*) inst->last_made_directory);
411 /* globally exported name */
412 module_t rlm_detail = {
414 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
415 NULL, /* initialization */
416 detail_instantiate, /* instantiation */
418 NULL, /* authentication */
419 detail_authorize, /* authorization */
420 NULL, /* preaccounting */
421 detail_accounting, /* accounting */
422 NULL, /* checksimul */
423 detail_pre_proxy, /* pre-proxy */
424 detail_post_proxy, /* post-proxy */
425 detail_postauth /* post-auth */
427 detail_detach, /* detach */