Remove libradius.h from the top of the standard header list.
[freeradius.git] / src / modules / rlm_detail / rlm_detail.c
1 /*
2  * rlm_detail.c accounting:    Write the "detail" files.
3  *
4  * Version:     $Id$
5  *
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.
10  *
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.
15  *
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
19  *
20  * Copyright 2000  The FreeRADIUS server project
21  */
22
23 static const char rcsid[] = "$Id$";
24
25 #include        "autoconf.h"
26
27 #include        <sys/stat.h>
28 #include        <sys/select.h>
29
30 #include        <stdlib.h>
31 #include        <string.h>
32 #include        <ctype.h>
33 #include        <fcntl.h>
34
35 #include        "radiusd.h"
36 #include        "modules.h"
37 #include        "rad_assert.h"
38
39 #define         DIRLEN  8192
40
41 static const char *packet_codes[] = {
42   "",
43   "Access-Request",
44   "Access-Accept",
45   "Access-Reject",
46   "Accounting-Request",
47   "Accounting-Response",
48   "Accounting-Status",
49   "Password-Request",
50   "Password-Accept",
51   "Password-Reject",
52   "Accounting-Message",
53   "Access-Challenge"
54 };
55
56
57 struct detail_instance {
58         /* detail file */
59         char *detailfile;
60
61         /* detail file permissions */
62         int detailperm;
63
64         /* directory permissions */
65         int dirperm;
66
67         /* last made directory */
68         char *last_made_directory;
69
70         /* timestamp & stuff */
71         char *header;  
72
73         /* if we want file locking */
74         int locking;
75 };
76
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 }
89 };
90
91 /*
92  *      (Re-)read radiusd.conf into memory.
93  */
94 static int detail_instantiate(CONF_SECTION *conf, void **instance)
95 {
96         struct detail_instance *inst;
97
98         inst = rad_malloc(sizeof(*inst));
99         if (!inst) {
100                 return -1;
101         }
102         memset(inst, 0, sizeof(*inst));
103
104         if (cf_section_parse(conf, inst, module_config) < 0) {
105                 free(inst);
106                 return -1;
107         }
108
109         inst->last_made_directory = NULL;
110
111         *instance = inst;
112         return 0;
113 }
114
115 /*
116  *      Do detail, compatible with old accounting
117  */
118 static int do_detail(void *instance, REQUEST *request, RADIUS_PACKET *packet,
119                      int compat)
120 {
121         int             outfd;
122         FILE            *outfp;
123         char            timestamp[256];
124         char            buffer[DIRLEN];
125         char            *p;
126         struct stat     st;
127         int             locked;
128         int             lock_count;
129         struct timeval  tv;
130         REALM           *proxy_realm;
131         char            proxy_buffer[16];
132         VALUE_PAIR      *pair = packet->vps;
133
134         struct detail_instance *inst = instance;
135
136         /*
137          *      Nothing to log: don't do anything.
138          */
139         if (!packet) {
140                 return RLM_MODULE_NOOP;
141         }
142
143         /*
144          *      Create a directory for this nas.
145          *
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
149          *      variables.
150          */
151         radius_xlat(buffer, sizeof(buffer), inst->detailfile, request, NULL);
152         DEBUG2("rlm_detail: %s expands to %s", inst->detailfile, buffer);
153
154         /*
155          *      Grab the last directory delimiter.
156          */
157         p = strrchr(buffer,'/');
158
159         /*
160          *      There WAS a directory delimiter there, and
161          *      the file doesn't exist, so
162          *      we prolly must create it the dir(s)
163          */
164         if ((p) && (stat(buffer, &st) < 0)) {
165                 *p = '\0';
166                 /*
167                  *      NO previously cached directory name, so we've
168                  *      got to create a new one.
169                  *
170                  *      OR the new directory name is different than the old,
171                  *      so we've got to create a new one.
172                  *
173                  *      OR the cached directory has somehow gotten removed,
174                  *      so we've got to create a new one.
175                  */
176                 if ((inst->last_made_directory == NULL) ||
177                     (strcmp(inst->last_made_directory, buffer) != 0)) {
178
179                         /*
180                          *      Free any previously cached name.
181                          */
182                         if (inst->last_made_directory != NULL) {
183                                 free((char *) inst->last_made_directory);
184                                 inst->last_made_directory = NULL;
185                         }
186
187                         /*
188                          *      Go create possibly multiple directories.
189                          */
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;
193                         }
194                         inst->last_made_directory = strdup(buffer);
195                 }
196
197                 *p = '/';
198         } /* else there was no directory delimiter. */
199
200         /*
201          *      Open & create the file, with the given permissions.
202          */
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;
208         }
209
210         /*
211          *      If we're not using locking, we'll just pass straight though
212          *      the while loop.
213          *      If we fail to aquire the filelock in 80 tries (approximately
214          *      two seconds) we bail out.
215          */
216         locked = 0;
217         lock_count = 0;
218         do {
219                 if (inst->locking) {
220                         lseek(outfd, 0L, SEEK_SET);
221                         if (rad_lockfd_nonblock(outfd, 0) < 0) {
222                                 tv.tv_sec = 0;
223                                 tv.tv_usec = 25000;
224                                 select(0, NULL, NULL, NULL, &tv);
225                                 lock_count++;
226                         } else {
227                                 DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
228                                       lock_count + 1);
229                                 locked = 1;
230                         }
231                 }
232         } while (!locked && inst->locking && lock_count < 80);
233
234         if (!locked && inst->locking && lock_count >= 80) {
235                 close(outfd);
236                 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
237                        buffer);
238                 return RLM_MODULE_FAIL;
239         }
240
241         /*
242          *      Convert the FD to FP.  The FD is no longer valid
243          *      after this operation.
244          */
245         if ((outfp = fdopen(outfd, "a")) == NULL) {
246                 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
247                        buffer, strerror(errno));
248                 if (inst->locking) {
249                         lseek(outfd, 0L, SEEK_SET);
250                         rad_unlockfd(outfd, 0);
251                         DEBUG("rlm_detail: Released filelock");
252                 }
253                 close(outfd);   /* automatically releases the lock */
254
255                 return RLM_MODULE_FAIL;
256         }
257
258         /*
259          *      Write the information to the file.
260          */
261         if (!compat) {
262                 /*
263                  *      Print out names, if they're OK.
264                  *      Numbers, if not.
265                  */
266                 if ((packet->code > 0) &&
267                     (packet->code <= PW_ACCESS_CHALLENGE)) {
268                         fprintf(outfp, "Packet-Type = %s\n",
269                                 packet_codes[packet->code]);
270                 } else {
271                         fprintf(outfp, "Packet-Type = %d\n", packet->code);
272                 }
273         }
274
275         /*
276          *      Post a timestamp
277          */
278         fseek(outfp, 0L, SEEK_END);
279         radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
280         fprintf(outfp, "%s\n", timestamp);
281
282         /* Write each attribute/value to the log file */
283         while (pair) {
284                 /*
285                  *      Don't print passwords in old format...
286                  */
287                 if (compat && (pair->attribute == PW_PASSWORD)) {
288                         pair = pair->next;
289                         continue;
290                 }
291
292                 /*
293                  *      Print all of the attributes.
294                  */
295                 fputs("\t", outfp);
296                 vp_print(outfp, pair);
297                 fputs("\n", outfp);
298                 pair = pair->next;
299         }
300
301         /*
302          *      Add non-protocol attibutes.
303          */
304         if (compat) {
305                 if ((pair = pairfind(request->config_items,
306                                      PW_PROXY_TO_REALM)) != NULL) {
307                         proxy_realm = realm_find(pair->strvalue, TRUE);
308                         if (proxy_realm) {
309                                 memset((char *) proxy_buffer, 0, 16);
310
311                                 rad_assert(proxy_realm->acct_ipaddr.af == AF_INET);
312
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",
317                                         proxy_buffer);
318                                 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
319                                       proxy_buffer);
320                         }
321                 }
322                 fprintf(outfp, "\tTimestamp = %ld\n",
323                         (unsigned long) request->timestamp);
324
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);
329         }
330
331         fputs("\n", outfp);
332
333         if (inst->locking) {
334                 fflush(outfp);
335                 lseek(outfd, 0L, SEEK_SET);
336                 rad_unlockfd(outfd, 0);
337                 DEBUG("rlm_detail: Released filelock");
338         }
339
340         fclose(outfp);
341
342         /*
343          *      And everything is fine.
344          */
345         return RLM_MODULE_OK;
346 }
347
348 /*
349  *      Accounting - write the detail files.
350  */
351 static int detail_accounting(void *instance, REQUEST *request)
352 {
353
354         return do_detail(instance,request,request->packet, TRUE);
355 }
356
357 /*
358  *      Incoming Access Request - write the detail files.
359  */
360 static int detail_authorize(void *instance, REQUEST *request)
361 {
362         return do_detail(instance,request,request->packet, FALSE);
363 }
364
365 /*
366  *      Outgoing Access-Request Reply - write the detail files.
367  */
368 static int detail_postauth(void *instance, REQUEST *request)
369 {
370         return do_detail(instance,request,request->reply, FALSE);
371 }
372
373
374 /*
375  *      Outgoing Access-Request to home server - write the detail files.
376  */
377 static int detail_pre_proxy(void *instance, REQUEST *request)
378 {
379         if (request->proxy &&
380             request->proxy->vps) {
381                 return do_detail(instance,request,request->proxy, FALSE);
382         }
383
384         return RLM_MODULE_NOOP;
385 }
386
387
388 /*
389  *      Outgoing Access-Request Reply - write the detail files.
390  */
391 static int detail_post_proxy(void *instance, REQUEST *request)
392 {
393         if (request->proxy_reply &&
394             request->proxy_reply->vps) {
395                 return do_detail(instance,request,request->proxy_reply, FALSE);
396         }
397
398         return RLM_MODULE_NOOP;
399 }
400
401
402 /*
403  *      Clean up.
404  */
405 static int detail_detach(void *instance)
406 {
407         struct detail_instance *inst = instance;
408         free((char *) inst->detailfile);
409
410         if (inst->last_made_directory)
411                 free((char*) inst->last_made_directory);
412         free(inst);
413         return 0;
414 }
415
416
417 /* globally exported name */
418 module_t rlm_detail = {
419         "detail",
420         RLM_TYPE_THREAD_UNSAFE,        /* type: reserved */
421         NULL,                           /* initialization */
422         detail_instantiate,             /* instantiation */
423         {
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 */
432         },
433         detail_detach,                  /* detach */
434         NULL                            /* destroy */
435 };
436