fdc9e6d80483ff101c30cb78464dd7cf4f770817
[freeradius.git] / src / main / detail.c
1 /*
2  * detail.c     Process the detail file
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19  *
20  * Copyright 2007  The FreeRADIUS server project
21  * Copyright 2007  Alan DeKok <aland@deployingradius.com>
22  */
23
24 RCSID("$Id$")
25
26 #include <freeradius-devel/radiusd.h>
27 #include <freeradius-devel/modules.h>
28 #include <freeradius-devel/detail.h>
29 #include <freeradius-devel/process.h>
30 #include <freeradius-devel/rad_assert.h>
31
32 #ifdef HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35
36 #ifdef HAVE_GLOB_H
37 #include <glob.h>
38 #endif
39
40 #include <fcntl.h>
41
42 #ifdef WITH_DETAIL
43
44 #define USEC (1000000)
45
46 static FR_NAME_NUMBER state_names[] = {
47         { "unopened", STATE_UNOPENED },
48         { "unlocked", STATE_UNLOCKED },
49         { "header", STATE_HEADER },
50         { "reading", STATE_READING },
51         { "queued", STATE_QUEUED },
52         { "running", STATE_RUNNING },
53         { "no-reply", STATE_NO_REPLY },
54         { "replied", STATE_REPLIED },
55
56         { NULL, 0 }
57 };
58
59
60 /*
61  *      If we're limiting outstanding packets, then mark the response
62  *      as being sent.
63  */
64 int detail_send(rad_listen_t *listener, REQUEST *request)
65 {
66 #ifdef WITH_DETAIL_THREAD
67         char c = 0;
68 #endif
69         listen_detail_t *data = listener->data;
70
71         rad_assert(request->listener == listener);
72         rad_assert(listener->send == detail_send);
73
74         /*
75          *      This request timed out.  Remember that, and tell the
76          *      caller it's OK to read more "detail" file stuff.
77          */
78         if (request->reply->code == 0) {
79                 data->delay_time = data->retry_interval * USEC;
80                 data->signal = 1;
81                 data->state = STATE_NO_REPLY;
82
83                 RDEBUG("detail (%s): No response to request.  Will retry in %d seconds",
84                        data->name, data->retry_interval);
85         } else {
86                 int rtt;
87                 struct timeval now;
88
89                 RDEBUG("detail (%s): Done %s packet.", data->name, fr_packet_codes[request->packet->code]);
90
91                 /*
92                  *      We call gettimeofday a lot.  But it should be OK,
93                  *      because there's nothing else to do.
94                  */
95                 gettimeofday(&now, NULL);
96
97                 /*
98                  *      If we haven't sent a packet in the last second, reset
99                  *      the RTT.
100                  */
101                 now.tv_sec -= 1;
102                 if (timercmp(&data->last_packet, &now, <)) {
103                         data->has_rtt = false;
104                 }
105                 now.tv_sec += 1;
106
107                 /*
108                  *      Only one detail packet may be outstanding at a time,
109                  *      so it's safe to update some entries in the detail
110                  *      structure.
111                  *
112                  *      We keep smoothed round trip time (SRTT), but not round
113                  *      trip timeout (RTO).  We use SRTT to calculate a rough
114                  *      load factor.
115                  */
116                 rtt = now.tv_sec - request->packet->timestamp.tv_sec;
117                 rtt *= USEC;
118                 rtt += now.tv_usec;
119                 rtt -= request->packet->timestamp.tv_usec;
120
121                 /*
122                  *      If we're proxying, the RTT is our processing time,
123                  *      plus the network delay there and back, plus the time
124                  *      on the other end to process the packet.  Ideally, we
125                  *      should remove the network delays from the RTT, but we
126                  *      don't know what they are.
127                  *
128                  *      So, to be safe, we over-estimate the total cost of
129                  *      processing the packet.
130                  */
131                 if (!data->has_rtt) {
132                         data->has_rtt = true;
133                         data->srtt = rtt;
134                         data->rttvar = rtt / 2;
135
136                 } else {
137                         data->rttvar -= data->rttvar >> 2;
138                         data->rttvar += (data->srtt - rtt);
139                         data->srtt -= data->srtt >> 3;
140                         data->srtt += rtt >> 3;
141                 }
142
143                 /*
144                  *      Calculate the time we wait before sending the next
145                  *      packet.
146                  *
147                  *      rtt / (rtt + delay) = load_factor / 100
148                  */
149                 data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
150
151                 /*
152                  *      Cap delay at no less than 4 packets/s.  If the
153                  *      end system can't handle this, then it's very
154                  *      broken.
155                  */
156                 if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
157
158                 RDEBUG3("detail (%s): Received response for request %d.  Will read the next packet in %d seconds",
159                         data->name, request->number, data->delay_time / USEC);
160
161                 data->last_packet = now;
162                 data->signal = 1;
163                 data->state = STATE_REPLIED;
164                 data->counter++;
165         }
166
167 #ifdef WITH_DETAIL_THREAD
168         if (write(data->child_pipe[1], &c, 1) < 0) {
169                 RERROR("detail (%s): Failed writing ack to reader thread: %s", data->name, fr_syserror(errno));
170         }
171 #else
172         radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
173 #endif
174
175         return 0;
176 }
177
178
179 /*
180  *      Open the detail file, if we can.
181  *
182  *      FIXME: create it, if it's not already there, so that the main
183  *      server select() will wake us up if there's anything to read.
184  */
185 static int detail_open(rad_listen_t *this)
186 {
187         struct stat st;
188         listen_detail_t *data = this->data;
189
190         rad_assert(data->state == STATE_UNOPENED);
191         data->delay_time = USEC;
192
193         /*
194          *      Open detail.work first, so we don't lose
195          *      accounting packets.  It's probably better to
196          *      duplicate them than to lose them.
197          *
198          *      Note that we're not writing to the file, but
199          *      we've got to open it for writing in order to
200          *      establish the lock, to prevent rlm_detail from
201          *      writing to it.
202          *
203          *      This also means that if we're doing globbing,
204          *      this file will be read && processed before the
205          *      file globbing is done.
206          */
207         data->fp = NULL;
208         data->work_fd = open(data->filename_work, O_RDWR);
209
210         /*
211          *      Couldn't open it for a reason OTHER than "it doesn't
212          *      exist".  Complain and tell the admin.
213          */
214         if ((data->work_fd < 0) && (errno != ENOENT)) {
215                 ERROR("Failed opening detail file %s: %s",
216                       data->filename_work, fr_syserror(errno));
217                 return 0;
218         }
219
220         /*
221          *      The file doesn't exist.  Poll for it again.
222          */
223         if (data->work_fd < 0) {
224 #ifndef HAVE_GLOB_H
225                 return 0;
226 #else
227                 unsigned int    i;
228                 int             found;
229                 time_t          chtime;
230                 char const      *filename;
231                 glob_t          files;
232
233                 DEBUG2("detail (%s): Polling for detail file", data->name);
234
235                 memset(&files, 0, sizeof(files));
236                 if (glob(data->filename, 0, NULL, &files) != 0) {
237                 noop:
238                         globfree(&files);
239                         return 0;
240                 }
241
242                 /*
243                  *      Loop over the glob'd files, looking for the
244                  *      oldest one.
245                  */
246                 chtime = 0;
247                 found = -1;
248                 for (i = 0; i < files.gl_pathc; i++) {
249                         if (stat(files.gl_pathv[i], &st) < 0) continue;
250
251                         if ((i == 0) || (st.st_ctime < chtime)) {
252                                 chtime = st.st_ctime;
253                                 found = i;
254                         }
255                 }
256
257                 if (found < 0) goto noop;
258
259                 /*
260                  *      Rename detail to detail.work
261                  */
262                 filename = files.gl_pathv[found];
263
264                 DEBUG("detail (%s): Renaming %s -> %s", data->name, filename, data->filename_work);
265                 if (rename(filename, data->filename_work) < 0) {
266                         ERROR("detail (%s): Failed renaming %s to %s: %s",
267                               data->name, filename, data->filename_work, fr_syserror(errno));
268                         goto noop;
269                 }
270
271                 globfree(&files);       /* Shouldn't be using anything in files now */
272
273                 /*
274                  *      And try to open the filename.
275                  */
276                 data->work_fd = open(data->filename_work, O_RDWR);
277                 if (data->work_fd < 0) {
278                         ERROR("Failed opening detail file %s: %s",
279                                         data->filename_work, fr_syserror(errno));
280                         return 0;
281                 }
282 #endif
283         } /* else detail.work existed, and we opened it */
284
285         rad_assert(data->vps == NULL);
286         rad_assert(data->fp == NULL);
287
288         data->state = STATE_UNLOCKED;
289
290         data->client_ip.af = AF_UNSPEC;
291         data->timestamp = 0;
292         data->offset = data->last_offset = data->timestamp_offset = 0;
293         data->packets = 0;
294         data->tries = 0;
295         data->done_entry = false;
296
297         return 1;
298 }
299
300
301 /*
302  *      FIXME: add a configuration "exit when done" so that the detail
303  *      file reader can be used as a one-off tool to update stuff.
304  *
305  *      The time sequence for reading from the detail file is:
306  *
307  *      t_0             signalled that the server is idle, and we
308  *                      can read from the detail file.
309  *
310  *      t_rtt           the packet has been processed successfully,
311  *                      wait for t_delay to enforce load factor.
312  *
313  *      t_rtt + t_delay wait for signal that the server is idle.
314  *
315  */
316 #ifndef WITH_DETAIL_THREAD
317 static RADIUS_PACKET *detail_poll(rad_listen_t *listener);
318
319 int detail_recv(rad_listen_t *listener)
320 {
321         RADIUS_PACKET *packet;
322         listen_detail_t *data = listener->data;
323         RAD_REQUEST_FUNP fun = NULL;
324
325         /*
326          *      We may be in the main thread.  It needs to update the
327          *      timers before we try to read from the file again.
328          */
329         if (data->signal) return 0;
330
331         packet = detail_poll(listener);
332         if (!packet) return -1;
333
334         if (DEBUG_ENABLED2) {
335                 VALUE_PAIR *vp;
336                 vp_cursor_t cursor;
337
338                 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
339                 for (vp = fr_cursor_init(&cursor, &packet->vps);
340                      vp;
341                      vp = fr_cursor_next(&cursor)) {
342                         debug_pair(vp);
343                 }
344         }
345
346         switch (packet->code) {
347         case PW_CODE_ACCOUNTING_REQUEST:
348                 fun = rad_accounting;
349                 break;
350
351         case PW_CODE_COA_REQUEST:
352         case PW_CODE_DISCONNECT_REQUEST:
353                 fun = rad_coa_recv;
354                 break;
355
356         default:
357                 rad_free(&packet);
358                 data->state = STATE_REPLIED;
359                 return 0;
360         }
361
362         /*
363          *      Don't bother doing limit checks, etc.
364          */
365         if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
366                 rad_free(&packet);
367                 data->state = STATE_NO_REPLY;   /* try again later */
368                 return 0;
369         }
370
371         return 1;
372 }
373 #else
374 int detail_recv(rad_listen_t *listener)
375 {
376         char c = 0;
377         ssize_t rcode;
378         RADIUS_PACKET *packet;
379         listen_detail_t *data = listener->data;
380         RAD_REQUEST_FUNP fun = NULL;
381
382         /*
383          *      Block until there's a packet ready.
384          */
385         rcode = read(data->master_pipe[0], &packet, sizeof(packet));
386         if (rcode <= 0) return rcode;
387
388         if (DEBUG_ENABLED2) {
389                 VALUE_PAIR *vp;
390                 vp_cursor_t cursor;
391
392                 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
393                 for (vp = fr_cursor_init(&cursor, &packet->vps);
394                      vp;
395                      vp = fr_cursor_next(&cursor)) {
396                         debug_pair(vp);
397                 }
398         }
399         rad_assert(packet != NULL);
400
401         switch (packet->code) {
402         case PW_CODE_ACCOUNTING_REQUEST:
403                 fun = rad_accounting;
404                 break;
405
406         case PW_CODE_COA_REQUEST:
407         case PW_CODE_DISCONNECT_REQUEST:
408                 fun = rad_coa_recv;
409                 break;
410
411         default:
412                 data->state = STATE_REPLIED;
413                 goto signal_thread;
414         }
415
416         if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
417                 data->state = STATE_NO_REPLY;   /* try again later */
418
419         signal_thread:
420                 rad_free(&packet);
421                 if (write(data->child_pipe[1], &c, 1) < 0) {
422                         ERROR("detail (%s): Failed writing ack to reader thread: %s", data->name,
423                               fr_syserror(errno));
424                 }
425         }
426
427         /*
428          *      Wait for the child thread to write an answer to the pipe
429          */
430         return 0;
431 }
432 #endif
433
434 static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
435 {
436         char            key[256], op[8], value[1024];
437         vp_cursor_t     cursor;
438         VALUE_PAIR      *vp;
439         RADIUS_PACKET   *packet;
440         char            buffer[2048];
441         listen_detail_t *data = listener->data;
442
443         switch (data->state) {
444         case STATE_UNOPENED:
445 open_file:
446                 rad_assert(data->work_fd < 0);
447
448                 if (!detail_open(listener)) return NULL;
449
450                 rad_assert(data->state == STATE_UNLOCKED);
451                 rad_assert(data->work_fd >= 0);
452
453                 /* FALL-THROUGH */
454
455         /*
456          *      Try to lock fd.  If we can't, return.
457          *      If we can, continue.  This means that
458          *      the server doesn't block while waiting
459          *      for the lock to open...
460          */
461         case STATE_UNLOCKED:
462                 /*
463                  *      Note that we do NOT block waiting for
464                  *      the lock.  We've re-named the file
465                  *      above, so we've already guaranteed
466                  *      that any *new* detail writer will not
467                  *      be opening this file.  The only
468                  *      purpose of the lock is to catch a race
469                  *      condition where the execution
470                  *      "ping-pongs" between radiusd &
471                  *      radrelay.
472                  */
473                 if (rad_lockfd_nonblock(data->work_fd, 0) < 0) {
474                         /*
475                          *      Close the FD.  The main loop
476                          *      will wake up in a second and
477                          *      try again.
478                          */
479                         close(data->work_fd);
480                         data->fp = NULL;
481                         data->work_fd = -1;
482                         data->state = STATE_UNOPENED;
483                         return NULL;
484                 }
485
486                 /*
487                  *      Only open for writing if we're
488                  *      marking requests as completed.
489                  */
490                 data->fp = fdopen(data->work_fd, data->track ? "r+" : "r");
491                 if (!data->fp) {
492                         ERROR("detail (%s): FATAL: Failed to re-open detail file: %s",
493                               data->name, fr_syserror(errno));
494                         fr_exit(1);
495                 }
496
497                 /*
498                  *      Look for the header
499                  */
500                 data->state = STATE_HEADER;
501                 data->delay_time = USEC;
502                 data->vps = NULL;
503
504                 /* FALL-THROUGH */
505
506         case STATE_HEADER:
507         do_header:
508                 data->done_entry = false;
509                 data->timestamp_offset = 0;
510
511                 data->tries = 0;
512                 if (!data->fp) {
513                         data->state = STATE_UNOPENED;
514                         goto open_file;
515                 }
516
517                 {
518                         struct stat buf;
519
520                         if (fstat(data->work_fd, &buf) < 0) {
521                                 ERROR("detail (%s): Failed to stat detail file: %s",
522                                       data->name, fr_syserror(errno));
523
524                                 goto cleanup;
525                         }
526                         if (((off_t) ftell(data->fp)) == buf.st_size) {
527                                 goto cleanup;
528                         }
529                 }
530
531                 /*
532                  *      End of file.  Delete it, and re-set
533                  *      everything.
534                  */
535                 if (feof(data->fp)) {
536                 cleanup:
537                         DEBUG("detail (%s): Unlinking %s", data->name, data->filename_work);
538                         unlink(data->filename_work);
539                         if (data->fp) fclose(data->fp);
540                         data->fp = NULL;
541                         data->work_fd = -1;
542                         data->state = STATE_UNOPENED;
543                         rad_assert(data->vps == NULL);
544
545                         if (data->one_shot) {
546                                 INFO("detail (%s): Finished reading \"one shot\" detail file - Exiting", data->name);
547                                 radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
548                         }
549
550                         return NULL;
551                 }
552
553                 /*
554                  *      Else go read something.
555                  */
556                 break;
557
558         /*
559          *      Read more value-pair's, unless we're
560          *      at EOF.  In that case, queue whatever
561          *      we have.
562          */
563         case STATE_READING:
564                 if (data->fp && !feof(data->fp)) break;
565                 data->state = STATE_QUEUED;
566
567                 /* FALL-THROUGH */
568
569         case STATE_QUEUED:
570                 goto alloc_packet;
571
572         /*
573          *      Periodically check what's going on.
574          *      If the request is taking too long,
575          *      retry it.
576          */
577         case STATE_RUNNING:
578                 if (time(NULL) < (data->running + (int)data->retry_interval)) {
579                         return NULL;
580                 }
581
582                 DEBUG("detail (%s): No response to detail request.  Retrying", data->name);
583                 /* FALL-THROUGH */
584
585         /*
586          *      If there's no reply, keep
587          *      retransmitting the current packet
588          *      forever.
589          */
590         case STATE_NO_REPLY:
591                 data->state = STATE_QUEUED;
592                 goto alloc_packet;
593
594         /*
595          *      We have a reply.  Clean up the old
596          *      request, and go read another one.
597          */
598         case STATE_REPLIED:
599                 if (data->track) {
600                         rad_assert(data->fp != NULL);
601
602                         if (fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) {
603                                 WARN("detail (%s): Failed seeking to timestamp offset: %s",
604                                      data->name, fr_syserror(errno));
605                         } else if (fwrite("\tDone", 1, 5, data->fp) < 5) {
606                                 WARN("detail (%s): Failed marking request as done: %s",
607                                      data->name, fr_syserror(errno));
608                         } else if (fflush(data->fp) != 0) {
609                                 WARN("detail (%s): Failed flushing marked detail file to disk: %s",
610                                      data->name, fr_syserror(errno));
611                         }
612
613                         if (fseek(data->fp, data->offset, SEEK_SET) < 0) {
614                                 WARN("detail (%s): Failed seeking to next detail request: %s",
615                                      data->name, fr_syserror(errno));
616                         }
617                 }
618
619                 fr_pair_list_free(&data->vps);
620                 data->state = STATE_HEADER;
621                 goto do_header;
622         }
623
624         fr_cursor_init(&cursor, &data->vps);
625
626         /*
627          *      Read a header, OR a value-pair.
628          */
629         while (fgets(buffer, sizeof(buffer), data->fp)) {
630                 data->last_offset = data->offset;
631                 data->offset = ftell(data->fp); /* for statistics */
632
633                 /*
634                  *      Badly formatted file: delete it.
635                  *
636                  *      FIXME: Maybe flag an error?
637                  */
638                 if (!strchr(buffer, '\n')) {
639                         fr_pair_list_free(&data->vps);
640                         goto cleanup;
641                 }
642
643                 /*
644                  *      We're reading VP's, and got a blank line.
645                  *      Queue the packet.
646                  */
647                 if ((data->state == STATE_READING) &&
648                     (buffer[0] == '\n')) {
649                         data->state = STATE_QUEUED;
650                         break;
651                 }
652
653                 /*
654                  *      Look for date/time header, and read VP's if
655                  *      found.  If not, keep reading lines until we
656                  *      find one.
657                  */
658                 if (data->state == STATE_HEADER) {
659                         int y;
660
661                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
662                                 data->state = STATE_READING;
663                         }
664                         continue;
665                 }
666
667                 /*
668                  *      We have a full "attribute = value" line.
669                  *      If it doesn't look reasonable, skip it.
670                  *
671                  *      FIXME: print an error for badly formatted attributes?
672                  */
673                 if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
674                         WARN("detail (%s): Skipping badly formatted line - %s", data->name, buffer);
675                         continue;
676                 }
677
678                 /*
679                  *      Should be =, :=, +=, ...
680                  */
681                 if (!strchr(op, '=')) {
682                         WARN("detail (%s): Skipping line without operator - %s", data->name, buffer);
683                         continue;
684                 }
685
686                 /*
687                  *      Skip non-protocol attributes.
688                  */
689                 if (!strcasecmp(key, "Request-Authenticator")) continue;
690
691                 /*
692                  *      Set the original client IP address, based on
693                  *      what's in the detail file.
694                  *
695                  *      Hmm... we don't set the server IP address.
696                  *      or port.  Oh well.
697                  */
698                 if (!strcasecmp(key, "Client-IP-Address")) {
699                         data->client_ip.af = AF_INET;
700                         if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
701                                 ERROR("detail (%s): Failed parsing Client-IP-Address", data->name);
702
703                                 fr_pair_list_free(&data->vps);
704                                 goto cleanup;
705                         }
706                         continue;
707                 }
708
709                 /*
710                  *      The original time at which we received the
711                  *      packet.  We need this to properly calculate
712                  *      Acct-Delay-Time.
713                  */
714                 if (!strcasecmp(key, "Timestamp")) {
715                         data->timestamp = atoi(value);
716                         data->timestamp_offset = data->last_offset;
717
718                         vp = fr_pair_afrom_num(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
719                         if (vp) {
720                                 vp->vp_date = (uint32_t) data->timestamp;
721                                 vp->type = VT_DATA;
722                                 fr_cursor_insert(&cursor, vp);
723                         }
724                         continue;
725                 }
726
727                 if (!strcasecmp(key, "Donestamp")) {
728                         data->timestamp = atoi(value);
729                         data->done_entry = true;
730                         continue;
731                 }
732
733                 DEBUG3("detail (%s): Trying to read VP from line - %s", data->name, buffer);
734
735                 /*
736                  *      Read one VP.
737                  *
738                  *      FIXME: do we want to check for non-protocol
739                  *      attributes like radsqlrelay does?
740                  */
741                 vp = NULL;
742                 if ((fr_pair_list_afrom_str(data, buffer, &vp) > 0) &&
743                     (vp != NULL)) {
744                         fr_cursor_merge(&cursor, vp);
745                 } else {
746                         WARN("detail (%s): Failed reading VP from line - %s", data->name, buffer);
747                 }
748         }
749
750         /*
751          *      Some kind of error.
752          *
753          *      FIXME: Leave the file in-place, and warn the
754          *      administrator?
755          */
756         if (ferror(data->fp)) goto cleanup;
757
758         data->tries = 0;
759         data->packets++;
760
761         /*
762          *      Process the packet.
763          */
764  alloc_packet:
765         if (data->done_entry) {
766                 DEBUG2("detail (%s): Skipping record for timestamp %lu", data->name, data->timestamp);
767                 fr_pair_list_free(&data->vps);
768                 data->state = STATE_HEADER;
769                 goto do_header;
770         }
771
772         data->tries++;
773
774         /*
775          *      The writer doesn't check that the record was
776          *      completely written.  If the disk is full, this can
777          *      result in a truncated record.  When that happens,
778          *      treat it as EOF.
779          */
780         if (data->state != STATE_QUEUED) {
781                 ERROR("detail (%s): Truncated record: treating it as EOF for detail file %s",
782                       data->name, data->filename_work);
783                 fr_pair_list_free(&data->vps);
784                 goto cleanup;
785         }
786
787         /*
788          *      We're done reading the file, but we didn't read
789          *      anything.  Clean up, and don't return anything.
790          */
791         if (!data->vps) {
792                 WARN("detail (%s): Read empty packet from file %s",
793                      data->name, data->filename_work);
794                 data->state = STATE_HEADER;
795                 if (!data->fp || feof(data->fp)) goto cleanup;
796                 return NULL;
797         }
798
799         /*
800          *      Allocate the packet.  If we fail, it's a serious
801          *      problem.
802          */
803         packet = rad_alloc(NULL, true);
804         if (!packet) {
805                 ERROR("detail (%s): FATAL: Failed allocating memory for detail", data->name);
806                 fr_exit(1);
807         }
808
809         memset(packet, 0, sizeof(*packet));
810         packet->sockfd = -1;
811         packet->src_ipaddr.af = AF_INET;
812         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
813
814         /*
815          *      If everything's OK, this is a waste of memory.
816          *      Otherwise, it lets us re-send the original packet
817          *      contents, unmolested.
818          */
819         packet->vps = fr_pair_list_copy(packet, data->vps);
820
821         packet->code = PW_CODE_ACCOUNTING_REQUEST;
822         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
823         if (vp) packet->code = vp->vp_integer;
824
825         gettimeofday(&packet->timestamp, NULL);
826
827         /*
828          *      Remember where it came from, so that we don't
829          *      proxy it to the place it came from...
830          */
831         if (data->client_ip.af != AF_UNSPEC) {
832                 packet->src_ipaddr = data->client_ip;
833         }
834
835         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
836         if (vp) {
837                 packet->src_ipaddr.af = AF_INET;
838                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
839                 packet->src_ipaddr.prefix = 32;
840         } else {
841                 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
842                 if (vp) {
843                         packet->src_ipaddr.af = AF_INET6;
844                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
845                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
846                         packet->src_ipaddr.prefix = 128;
847                 }
848         }
849
850         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
851         if (vp) {
852                 packet->dst_ipaddr.af = AF_INET;
853                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
854                 packet->dst_ipaddr.prefix = 32;
855         } else {
856                 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
857                 if (vp) {
858                         packet->dst_ipaddr.af = AF_INET6;
859                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
860                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
861                         packet->dst_ipaddr.prefix = 128;
862                 }
863         }
864
865         /*
866          *      Generate packet ID, ports, IP via a counter.
867          */
868         packet->id = data->counter & 0xff;
869         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
870         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
871
872         packet->dst_ipaddr.af = AF_INET;
873         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
874
875         /*
876          *      Create / update accounting attributes.
877          */
878         if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
879                 /*
880                  *      Prefer the Event-Timestamp in the packet, if it
881                  *      exists.  That is when the event occurred, whereas the
882                  *      "Timestamp" field is when we wrote the packet to the
883                  *      detail file, which could have been much later.
884                  */
885                 vp = fr_pair_find_by_num(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
886                 if (vp) {
887                         data->timestamp = vp->vp_integer;
888                 }
889
890                 /*
891                  *      Look for Acct-Delay-Time, and update
892                  *      based on Acct-Delay-Time += (time(NULL) - timestamp)
893                  */
894                 vp = fr_pair_find_by_num(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
895                 if (!vp) {
896                         vp = fr_pair_afrom_num(packet, PW_ACCT_DELAY_TIME, 0);
897                         rad_assert(vp != NULL);
898                         fr_pair_add(&packet->vps, vp);
899                 }
900                 if (data->timestamp != 0) {
901                         vp->vp_integer += time(NULL) - data->timestamp;
902                 }
903         }
904
905         /*
906          *      Set the transmission count.
907          */
908         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
909         if (!vp) {
910                 vp = fr_pair_afrom_num(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
911                 rad_assert(vp != NULL);
912                 fr_pair_add(&packet->vps, vp);
913         }
914         vp->vp_integer = data->tries;
915
916         data->state = STATE_RUNNING;
917         data->running = packet->timestamp.tv_sec;
918
919         return packet;
920 }
921
922 /*
923  *      Free detail-specific stuff.
924  */
925 void detail_free(rad_listen_t *this)
926 {
927         listen_detail_t *data = this->data;
928
929 #ifdef WITH_DETAIL_THREAD
930         if (!check_config) {
931                 ssize_t ret;
932                 void *arg = NULL;
933
934                 /*
935                  *      Mark the child pipes as unusable
936                  */
937                 close(data->child_pipe[0]);
938                 close(data->child_pipe[1]);
939                 data->child_pipe[0] = -1;
940
941                 /*
942                  *      Tell it to stop (interrupting its sleep)
943                  */
944                 pthread_kill(data->pthread_id, SIGTERM);
945
946                 /*
947                  *      Wait for it to acknowledge that it's stopped.
948                  */
949                 ret = read(data->master_pipe[0], &arg, sizeof(arg));
950                 if (ret < 0) {
951                         ERROR("detail (%s): Reader thread exited without informing the master: %s",
952                               data->name, fr_syserror(errno));
953                 } else if (ret != sizeof(arg)) {
954                         ERROR("detail (%s): Invalid thread pointer received from reader thread during exit",
955                               data->name);
956                         ERROR("detail (%s): Expected %zu bytes, got %zi bytes", data->name, sizeof(arg), ret);
957                 }
958
959                 close(data->master_pipe[0]);
960                 close(data->master_pipe[1]);
961
962                 if (arg) pthread_join(data->pthread_id, &arg);
963         }
964 #endif
965
966         if (data->fp != NULL) {
967                 fclose(data->fp);
968                 data->fp = NULL;
969         }
970 }
971
972
973 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
974 {
975         if (!this->server) {
976                 return snprintf(buffer, bufsize, "%s",
977                                 ((listen_detail_t *)(this->data))->filename);
978         }
979
980         return snprintf(buffer, bufsize, "detail file %s as server %s",
981                         ((listen_detail_t *)(this->data))->filename,
982                         this->server);
983 }
984
985
986 /*
987  *      Delay while waiting for a file to be ready
988  */
989 static int detail_delay(listen_detail_t *data)
990 {
991         int delay = (data->poll_interval - 1) * USEC;
992
993         /*
994          *      Add +/- 0.25s of jitter
995          */
996         delay += (USEC * 3) / 4;
997         delay += fr_rand() % (USEC / 2);
998
999         DEBUG2("detail (%s): Detail listener state %s waiting %d.%06d sec",
1000                data->name,
1001                fr_int2str(state_names, data->state, "?"),
1002                (delay / USEC), delay % USEC);
1003
1004         return delay;
1005 }
1006
1007 /*
1008  *      Overloaded to return delay times.
1009  */
1010 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1011 {
1012 #ifdef WITH_DETAIL_THREAD
1013         return 0;
1014 #else
1015         listen_detail_t *data = this->data;
1016
1017         /*
1018          *      We haven't sent a packet... delay things a bit.
1019          */
1020         if (!data->signal) return detail_delay(data);
1021
1022         data->signal = 0;
1023
1024         DEBUG2("detail (%s): Detail listener state %s signalled %d waiting %d.%06d sec",
1025                data->name,
1026                fr_int2str(state_names, data->state, "?"),
1027                data->signal,
1028                data->delay_time / USEC,
1029                data->delay_time % USEC);
1030
1031         return data->delay_time;
1032 #endif
1033 }
1034
1035 /*
1036  *      Overloaded to return "should we fix delay times"
1037  */
1038 int detail_decode(rad_listen_t *this, REQUEST *request)
1039 {
1040 #ifdef WITH_DETAIL_THREAD
1041         listen_detail_t *data = this->data;
1042
1043         RDEBUG("Received %s from detail file %s",
1044                fr_packet_codes[request->packet->code], data->filename_work);
1045
1046         rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1047
1048         return 0;
1049 #else
1050         listen_detail_t *data = this->data;
1051
1052         RDEBUG("Received %s from detail file %s",
1053                fr_packet_codes[request->packet->code], data->filename_work);
1054
1055         rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1056
1057         return data->signal;
1058 #endif
1059 }
1060
1061
1062 #ifdef WITH_DETAIL_THREAD
1063 static void *detail_handler_thread(void *arg)
1064 {
1065         char c;
1066         rad_listen_t *this = arg;
1067         listen_detail_t *data = this->data;
1068
1069         while (true) {
1070                 RADIUS_PACKET *packet;
1071
1072                 while ((packet = detail_poll(this)) == NULL) {
1073                         usleep(detail_delay(data));
1074
1075                         /*
1076                          *      If we're supposed to exit then tell
1077                          *      the master thread we've exited.
1078                          */
1079                         if (data->child_pipe[0] < 0) {
1080                                 packet = NULL;
1081                                 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1082                                         ERROR("detail (%s): Failed writing exit status to master: %s",
1083                                               data->name, fr_syserror(errno));
1084                                 }
1085                                 return NULL;
1086                         }
1087                 }
1088
1089                 /*
1090                  *      Keep retrying forever.
1091                  *
1092                  *      FIXME: cap the retries.
1093                  */
1094                 do {
1095                         if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1096                                 ERROR("detail (%s): Failed passing detail packet pointer to master: %s",
1097                                       data->name, fr_syserror(errno));
1098                         }
1099
1100                         if (read(data->child_pipe[0], &c, 1) < 0) {
1101                                 ERROR("detail (%s): Failed getting detail packet ack from master: %s",
1102                                       data->name, fr_syserror(errno));
1103                                 break;
1104                         }
1105
1106                         if (data->delay_time > 0) usleep(data->delay_time);
1107
1108                         packet = detail_poll(this);
1109                         if (!packet) break;
1110                 } while (data->state != STATE_REPLIED);
1111         }
1112
1113         return NULL;
1114 }
1115 #endif
1116
1117
1118 static const CONF_PARSER detail_config[] = {
1119         { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
1120         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
1121         { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
1122         { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
1123         { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
1124         { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), "no" },
1125         { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), "no" },
1126         CONF_PARSER_TERMINATOR
1127 };
1128
1129 /*
1130  *      Parse a detail section.
1131  */
1132 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
1133 {
1134         int             rcode;
1135         listen_detail_t *data;
1136         RADCLIENT       *client;
1137         char            buffer[2048];
1138
1139         data = this->data;
1140
1141         rcode = cf_section_parse(cs, data, detail_config);
1142         if (rcode < 0) {
1143                 cf_log_err_cs(cs, "Failed parsing listen section");
1144                 return -1;
1145         }
1146
1147         data->name = cf_section_name2(cs);
1148         if (!data->name) data->name = data->filename;
1149
1150         /*
1151          *      We don't do duplicate detection for "detail" sockets.
1152          */
1153         this->nodup = true;
1154         this->synchronous = false;
1155
1156         if (!data->filename) {
1157                 cf_log_err_cs(cs, "No detail file specified in listen section");
1158                 return -1;
1159         }
1160
1161         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, >=, 1);
1162         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, <=, 100);
1163
1164         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, >=, 1);
1165         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, <=, 60);
1166
1167         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, >=, 4);
1168         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, <=, 3600);
1169
1170         /*
1171          *      Only checking the config.  Don't start threads or anything else.
1172          */
1173         if (check_config) return 0;
1174
1175         /*
1176          *      If the filename is a glob, use "detail.work" as the
1177          *      work file name.
1178          */
1179         if ((strchr(data->filename, '*') != NULL) ||
1180             (strchr(data->filename, '[') != NULL)) {
1181                 char *p;
1182
1183 #ifndef HAVE_GLOB_H
1184                 WARN("detail (%s): File \"%s\" appears to use file globbing, but it is not supported on this system",
1185                      data->name, data->filename);
1186 #endif
1187                 strlcpy(buffer, data->filename, sizeof(buffer));
1188                 p = strrchr(buffer, FR_DIR_SEP);
1189                 if (p) {
1190                         p[1] = '\0';
1191                 } else {
1192                         buffer[0] = '\0';
1193                 }
1194
1195                 /*
1196                  *      Globbing cannot be done across directories.
1197                  */
1198                 if ((strchr(buffer, '*') != NULL) ||
1199                     (strchr(buffer, '[') != NULL)) {
1200                         cf_log_err_cs(cs, "Wildcard directories are not supported");
1201                         return -1;
1202                 }
1203
1204                 strlcat(buffer, "detail.work",
1205                         sizeof(buffer) - strlen(buffer));
1206
1207         } else {
1208                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1209         }
1210
1211         data->filename_work = talloc_strdup(data, buffer);
1212
1213         data->work_fd = -1;
1214         data->vps = NULL;
1215         data->fp = NULL;
1216         data->state = STATE_UNOPENED;
1217         data->delay_time = data->poll_interval * USEC;
1218         data->signal = 1;
1219
1220         /*
1221          *      Initialize the fake client.
1222          */
1223         client = &data->detail_client;
1224         memset(client, 0, sizeof(*client));
1225         client->ipaddr.af = AF_INET;
1226         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1227         client->ipaddr.prefix = 0;
1228         client->longname = client->shortname = data->filename;
1229         client->secret = client->shortname;
1230         client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1231
1232 #ifdef WITH_DETAIL_THREAD
1233         /*
1234          *      Create the communication pipes.
1235          */
1236         if (pipe(data->master_pipe) < 0) {
1237                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1238                 fr_exit(1);
1239         }
1240
1241         if (pipe(data->child_pipe) < 0) {
1242                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1243                 fr_exit(1);
1244         }
1245
1246         pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1247
1248         this->fd = data->master_pipe[0];
1249 #endif
1250
1251         return 0;
1252 }
1253 #endif