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