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