Include files used to build the server are now <freeradius-devel/*.h>
[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        <freeradius-devel/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        <freeradius-devel/radiusd.h>
36 #include        <freeradius-devel/modules.h>
37 #include        <freeradius-devel/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                         free((char *) inst->last_made_directory);
179                         inst->last_made_directory = strdup(buffer);
180                 }
181
182                 /*
183                  *      stat the directory, and don't do anything if
184                  *      it exists.  If it doesn't exist, create it.
185                  *
186                  *      This also catches the case where some idiot
187                  *      deleted a directory that the server was using.
188                  */
189                 if (rad_mkdir(inst->last_made_directory, inst->dirperm) < 0) {
190                         radlog(L_ERR, "rlm_detail: Failed to create directory %s: %s", inst->last_made_directory, strerror(errno));
191                         return RLM_MODULE_FAIL;
192                 }
193
194                 *p = '/';
195         } /* else there was no directory delimiter. */
196
197         locked = 0;
198         lock_count = 0;
199         do {
200                 /*
201                  *      Open & create the file, with the given
202                  *      permissions.
203                  */
204                 if ((outfd = open(buffer, O_WRONLY | O_APPEND | O_CREAT,
205                                   inst->detailperm)) < 0) {
206                         radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
207                                buffer, strerror(errno));
208                         return RLM_MODULE_FAIL;
209                 }
210
211                 /*
212                  *      If we fail to aquire the filelock in 80 tries
213                  *      (approximately two seconds) we bail out.
214                  */
215                 if (inst->locking) {
216                         lseek(outfd, 0L, SEEK_SET);
217                         if (rad_lockfd_nonblock(outfd, 0) < 0) {
218                                 close(outfd);
219                                 tv.tv_sec = 0;
220                                 tv.tv_usec = 25000;
221                                 select(0, NULL, NULL, NULL, &tv);
222                                 lock_count++;
223                                 continue;
224                         }
225
226                         /*
227                          *      The file might have been deleted by
228                          *      radrelay while we tried to acquire
229                          *      the lock (race condition)
230                          */
231                         if (fstat(outfd, &st) != 0) {
232                                 radlog(L_ERR, "rlm_detail: Couldn't stat file %s: %s",
233                                        buffer, strerror(errno));
234                                 close(outfd);
235                                 return RLM_MODULE_FAIL;
236                         }
237                         if (st.st_nlink == 0) {
238                                 DEBUG("rlm_detail: File %s removed by another program, retrying",
239                                       buffer);
240                                 close(outfd);
241                                 lock_count = 0;
242                                 continue;
243                         }
244
245                         DEBUG("rlm_detail: Acquired filelock, tried %d time(s)",
246                               lock_count + 1);
247                         locked = 1;
248                 }
249         } while (inst->locking && !locked && lock_count < 80);
250
251         if (inst->locking && !locked) {
252                 close(outfd);
253                 radlog(L_ERR, "rlm_detail: Failed to aquire filelock for %s, giving up",
254                        buffer);
255                 return RLM_MODULE_FAIL;
256         }
257
258         /*
259          *      Convert the FD to FP.  The FD is no longer valid
260          *      after this operation.
261          */
262         if ((outfp = fdopen(outfd, "a")) == NULL) {
263                 radlog(L_ERR, "rlm_detail: Couldn't open file %s: %s",
264                        buffer, strerror(errno));
265                 if (inst->locking) {
266                         lseek(outfd, 0L, SEEK_SET);
267                         rad_unlockfd(outfd, 0);
268                         DEBUG("rlm_detail: Released filelock");
269                 }
270                 close(outfd);   /* automatically releases the lock */
271
272                 return RLM_MODULE_FAIL;
273         }
274
275         /*
276          *      Write the information to the file.
277          */
278         if (!compat) {
279                 /*
280                  *      Print out names, if they're OK.
281                  *      Numbers, if not.
282                  */
283                 if ((packet->code > 0) &&
284                     (packet->code <= PW_ACCESS_CHALLENGE)) {
285                         fprintf(outfp, "Packet-Type = %s\n",
286                                 packet_codes[packet->code]);
287                 } else {
288                         fprintf(outfp, "Packet-Type = %d\n", packet->code);
289                 }
290         }
291
292         /*
293          *      Post a timestamp
294          */
295         fseek(outfp, 0L, SEEK_END);
296         radius_xlat(timestamp, sizeof(timestamp), inst->header, request, NULL);
297         fprintf(outfp, "%s\n", timestamp);
298
299         /* Write each attribute/value to the log file */
300         while (pair) {
301                 /*
302                  *      Don't print passwords in old format...
303                  */
304                 if (compat && (pair->attribute == PW_PASSWORD)) {
305                         pair = pair->next;
306                         continue;
307                 }
308
309                 /*
310                  *      Print all of the attributes.
311                  */
312                 fputs("\t", outfp);
313                 vp_print(outfp, pair);
314                 fputs("\n", outfp);
315                 pair = pair->next;
316         }
317
318         /*
319          *      Add non-protocol attibutes.
320          */
321         if (compat) {
322                 if ((pair = pairfind(request->config_items,
323                                      PW_PROXY_TO_REALM)) != NULL) {
324                         proxy_realm = realm_find(pair->vp_strvalue, TRUE);
325                         if (proxy_realm) {
326                                 memset((char *) proxy_buffer, 0, 16);
327
328                                 rad_assert(proxy_realm->acct_ipaddr.af == AF_INET);
329
330                                 inet_ntop(proxy_realm->acct_ipaddr.af,
331                                           &proxy_realm->acct_ipaddr.ipaddr,
332                                           proxy_buffer, sizeof(proxy_buffer));
333                                 fprintf(outfp, "\tFreeradius-Proxied-To = %s\n",
334                                         proxy_buffer);
335                                 DEBUG("rlm_detail: Freeradius-Proxied-To set to %s",
336                                       proxy_buffer);
337                         }
338                 }
339                 fprintf(outfp, "\tTimestamp = %ld\n",
340                         (unsigned long) request->timestamp);
341
342                 if (request->packet->verified == 2)
343                         fputs("\tRequest-Authenticator = Verified\n", outfp);
344                 else if (request->packet->verified == 1)
345                         fputs("\tRequest-Authenticator = None\n", outfp);
346         }
347
348         fputs("\n", outfp);
349
350         if (inst->locking) {
351                 fflush(outfp);
352                 lseek(outfd, 0L, SEEK_SET);
353                 rad_unlockfd(outfd, 0);
354                 DEBUG("rlm_detail: Released filelock");
355         }
356
357         fclose(outfp);
358
359         /*
360          *      And everything is fine.
361          */
362         return RLM_MODULE_OK;
363 }
364
365 /*
366  *      Accounting - write the detail files.
367  */
368 static int detail_accounting(void *instance, REQUEST *request)
369 {
370
371         return do_detail(instance,request,request->packet, TRUE);
372 }
373
374 /*
375  *      Incoming Access Request - write the detail files.
376  */
377 static int detail_authorize(void *instance, REQUEST *request)
378 {
379         return do_detail(instance,request,request->packet, FALSE);
380 }
381
382 /*
383  *      Outgoing Access-Request Reply - write the detail files.
384  */
385 static int detail_postauth(void *instance, REQUEST *request)
386 {
387         return do_detail(instance,request,request->reply, FALSE);
388 }
389
390
391 /*
392  *      Outgoing Access-Request to home server - write the detail files.
393  */
394 static int detail_pre_proxy(void *instance, REQUEST *request)
395 {
396         if (request->proxy &&
397             request->proxy->vps) {
398                 return do_detail(instance,request,request->proxy, FALSE);
399         }
400
401         return RLM_MODULE_NOOP;
402 }
403
404
405 /*
406  *      Outgoing Access-Request Reply - write the detail files.
407  */
408 static int detail_post_proxy(void *instance, REQUEST *request)
409 {
410         if (request->proxy_reply &&
411             request->proxy_reply->vps) {
412                 return do_detail(instance,request,request->proxy_reply, FALSE);
413         }
414
415         return RLM_MODULE_NOOP;
416 }
417
418
419 /*
420  *      Clean up.
421  */
422 static int detail_detach(void *instance)
423 {
424         struct detail_instance *inst = instance;
425         free((char *) inst->detailfile);
426
427         if (inst->last_made_directory)
428                 free((char*) inst->last_made_directory);
429         free(inst);
430         return 0;
431 }
432
433
434 /* globally exported name */
435 module_t rlm_detail = {
436         RLM_MODULE_INIT,
437         "detail",
438         RLM_TYPE_THREAD_UNSAFE,        /* type: reserved */
439         detail_instantiate,             /* instantiation */
440         detail_detach,                  /* detach */
441         {
442                 NULL,                   /* authentication */
443                 detail_authorize,       /* authorization */
444                 NULL,                   /* preaccounting */
445                 detail_accounting,      /* accounting */
446                 NULL,                   /* checksimul */
447                 detail_pre_proxy,       /* pre-proxy */
448                 detail_post_proxy,      /* post-proxy */
449                 detail_postauth         /* post-auth */
450         },
451 };
452