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 /* if we want file locking */
73 static CONF_PARSER module_config[] = {
74 { "detailfile", PW_TYPE_STRING_PTR,
75 offsetof(struct detail_instance,detailfile), NULL, "%A/%{Client-IP-Address}/detail" },
76 { "detailperm", PW_TYPE_INTEGER,
77 offsetof(struct detail_instance,detailperm), NULL, "0600" },
78 { "dirperm", PW_TYPE_INTEGER,
79 offsetof(struct detail_instance,dirperm), NULL, "0755" },
80 { "locking", PW_TYPE_BOOLEAN,
81 offsetof(struct detail_instance,locking), NULL, "no" },
82 { NULL, -1, 0, NULL, NULL }
86 * (Re-)read radiusd.conf into memory.
88 static int detail_instantiate(CONF_SECTION *conf, void **instance)
90 struct detail_instance *inst;
92 inst = rad_malloc(sizeof(*inst));
96 memset(inst, 0, sizeof(*inst));
98 if (cf_section_parse(conf, inst, module_config) < 0) {
103 inst->last_made_directory = NULL;
110 * Do detail, compatible with old accounting
112 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
124 char proxy_buffer[16];
125 VALUE_PAIR *pair = packet->vps;
127 struct detail_instance *inst = instance;
130 * Nothing to log: don't do anything.
133 return RLM_MODULE_NOOP;
137 * Create a directory for this nas.
139 * Generate the path for the detail file. Use the
140 * same format, but truncate at the last /. Then
141 * feed it through radius_xlat() to expand the
144 radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
145 DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
148 * Grab the last directory delimiter.
150 p = strrchr(buffer,'/');
153 * There WAS a directory delimiter there, and
154 * the file doesn't exist, so
155 * we prolly must create it the dir(s)
157 if ((p) && (stat(buffer, &st) < 0)) {
160 * NO previously cached directory name, so we've
161 * got to create a new one.
163 * OR the new directory name is different than the old,
164 * so we've got to create a new one.
166 * OR the cached directory has somehow gotten removed,
167 * so we've got to create a new one.
169 if ((inst->last_made_directory == NULL) ||
170 (strcmp(inst->last_made_directory, buffer) != 0)) {
173 * Free any previously cached name.
175 if (inst->last_made_directory != NULL) {
176 free((char *) inst->last_made_directory);
177 inst->last_made_directory = NULL;
181 * Go create possibly multiple directories.
183 if (rad_mkdir(buffer, inst->dirperm) < 0) {
184 radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", buffer, strerror(errno));
185 return RLM_MODULE_FAIL;
187 inst->last_made_directory = strdup(buffer);
191 } /* else there was no directory delimiter. */
194 * Open & create the file, with the given permissions.
196 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
197 inst->detailperm)) < 0) {
198 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
199 buffer, strerror(errno));
200 return RLM_MODULE_FAIL;
204 * If we're not using locking, we'll just pass straight though
206 * If we fail to aquire the filelock in 80 tries (approximately
207 * two seconds) we bail out.
213 lseek(outfd, 0L, SEEK_SET);
214 if (rad_lockfd_nonblock(outfd, 0) < 0) {
218 select(0, NULL, NULL, NULL, &tv);
221 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
226 } while (!locked && inst->locking && lock_count < 80);
228 if (!locked && inst->locking && lock_count >= 80) {
229 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
231 return RLM_MODULE_FAIL;
235 * Convert the FD to FP. The FD is no longer valid
236 * after this operation.
238 if ((outfp = fdopen(outfd, "a")) == NULL) {
239 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
240 buffer, strerror(errno));
242 lseek(outfd, 0L, SEEK_SET);
243 rad_unlockfd(outfd, 0);
244 DEBUG("rlm_detail: Released filelock");
248 return RLM_MODULE_FAIL;
252 * Write the information to the file.
256 * Print out names, if they're OK.
259 if ((packet->code > 0) &&
260 (packet->code <= PW_ACCESS_CHALLENGE)) {
261 fprintf(outfp, "Packet-Type = %s\n",
262 packet_codes[packet->code]);
264 fprintf(outfp, "Packet-Type = %d\n", packet->code);
271 fseek(outfp, 0L, SEEK_END);
272 fputs(CTIME_R(&request->timestamp, buffer, DIRLEN), outfp);
274 /* Write each attribute/value to the log file */
277 * Don't print passwords in old format...
279 if (compat && (pair->attribute == PW_PASSWORD)) {
285 * Print all of the attributes.
288 vp_print(outfp, pair);
294 * Add non-protocol attibutes.
297 if ((pair = pairfind(request->config_items,
298 PW_PROXY_TO_REALM)) != NULL) {
299 proxy_realm = realm_find(pair->strvalue, TRUE);
301 memset((char *) proxy_buffer, 0, 16);
302 ip_ntoa(proxy_buffer, proxy_realm->acct_ipaddr);
303 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
305 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
309 fprintf(outfp, "\tTimestamp = %ld\n",
310 (unsigned long) request->timestamp);
312 if (request->packet->verified == 2)
313 fputs("\tRequest-Authenticator = Verified\n", outfp);
314 else if (request->packet->verified == 1)
315 fputs("\tRequest-Authenticator = None\n", outfp);
322 lseek(outfd, 0L, SEEK_SET);
323 rad_unlockfd(outfd, 0);
324 DEBUG("rlm_detail: Released filelock");
330 * And everything is fine.
332 return RLM_MODULE_OK;
336 * Accounting - write the detail files.
338 static int detail_accounting(void *instance, REQUEST *request)
341 return do_detail(instance,request,request->packet, TRUE);
345 * Incoming Access Request - write the detail files.
347 static int detail_authorize(void *instance, REQUEST *request)
349 return do_detail(instance,request,request->packet, FALSE);
353 * Outgoing Access-Request Reply - write the detail files.
355 static int detail_postauth(void *instance, REQUEST *request)
357 return do_detail(instance,request,request->reply, FALSE);
362 * Outgoing Access-Request to home server - write the detail files.
364 static int detail_pre_proxy(void *instance, REQUEST *request)
366 if (request->proxy &&
367 request->proxy->vps) {
368 return do_detail(instance,request,request->proxy, FALSE);
371 return RLM_MODULE_NOOP;
376 * Outgoing Access-Request Reply - write the detail files.
378 static int detail_post_proxy(void *instance, REQUEST *request)
380 if (request->proxy_reply &&
381 request->proxy_reply->vps) {
382 return do_detail(instance,request,request->proxy_reply, FALSE);
385 return RLM_MODULE_NOOP;
392 static int detail_detach(void *instance)
394 struct detail_instance *inst = instance;
395 free((char *) inst->detailfile);
397 if (inst->last_made_directory)
398 free((char*) inst->last_made_directory);
404 /* globally exported name */
405 module_t rlm_detail = {
407 RLM_TYPE_THREAD_UNSAFE, /* type: reserved */
408 NULL, /* initialization */
409 detail_instantiate, /* instantiation */
411 NULL, /* authentication */
412 detail_authorize, /* authorization */
413 NULL, /* preaccounting */
414 detail_accounting, /* accounting */
415 NULL, /* checksimul */
416 detail_pre_proxy, /* pre-proxy */
417 detail_post_proxy, /* post-proxy */
418 detail_postauth /* post-auth */
420 detail_detach, /* detach */