move assertion to correct place. Found by PVS-Studio
[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         rad_assert(packet != NULL);
389
390         if (DEBUG_ENABLED2) {
391                 VALUE_PAIR *vp;
392                 vp_cursor_t cursor;
393
394                 DEBUG2("detail (%s): Read packet from %s", data->name, data->filename_work);
395                 for (vp = fr_cursor_init(&cursor, &packet->vps);
396                      vp;
397                      vp = fr_cursor_next(&cursor)) {
398                         debug_pair(vp);
399                 }
400         }
401
402         switch (packet->code) {
403         case PW_CODE_ACCOUNTING_REQUEST:
404                 fun = rad_accounting;
405                 break;
406
407         case PW_CODE_COA_REQUEST:
408         case PW_CODE_DISCONNECT_REQUEST:
409                 fun = rad_coa_recv;
410                 break;
411
412         default:
413                 data->state = STATE_REPLIED;
414                 goto signal_thread;
415         }
416
417         if (!request_receive(NULL, listener, packet, &data->detail_client, fun)) {
418                 data->state = STATE_NO_REPLY;   /* try again later */
419
420         signal_thread:
421                 rad_free(&packet);
422                 if (write(data->child_pipe[1], &c, 1) < 0) {
423                         ERROR("detail (%s): Failed writing ack to reader thread: %s", data->name,
424                               fr_syserror(errno));
425                 }
426         }
427
428         /*
429          *      Wait for the child thread to write an answer to the pipe
430          */
431         return 0;
432 }
433 #endif
434
435 static RADIUS_PACKET *detail_poll(rad_listen_t *listener)
436 {
437         int             y;
438         char            key[256], op[8], value[1024];
439         vp_cursor_t     cursor;
440         VALUE_PAIR      *vp;
441         RADIUS_PACKET   *packet;
442         char            buffer[2048];
443         listen_detail_t *data = listener->data;
444
445         switch (data->state) {
446         case STATE_UNOPENED:
447 open_file:
448                 rad_assert(data->work_fd < 0);
449
450                 if (!detail_open(listener)) return NULL;
451
452                 rad_assert(data->state == STATE_UNLOCKED);
453                 rad_assert(data->work_fd >= 0);
454
455                 /* FALL-THROUGH */
456
457         /*
458          *      Try to lock fd.  If we can't, return.
459          *      If we can, continue.  This means that
460          *      the server doesn't block while waiting
461          *      for the lock to open...
462          */
463         case STATE_UNLOCKED:
464                 /*
465                  *      Note that we do NOT block waiting for
466                  *      the lock.  We've re-named the file
467                  *      above, so we've already guaranteed
468                  *      that any *new* detail writer will not
469                  *      be opening this file.  The only
470                  *      purpose of the lock is to catch a race
471                  *      condition where the execution
472                  *      "ping-pongs" between radiusd &
473                  *      radrelay.
474                  */
475                 if (rad_lockfd_nonblock(data->work_fd, 0) < 0) {
476                         /*
477                          *      Close the FD.  The main loop
478                          *      will wake up in a second and
479                          *      try again.
480                          */
481                         close(data->work_fd);
482                         data->fp = NULL;
483                         data->work_fd = -1;
484                         data->state = STATE_UNOPENED;
485                         return NULL;
486                 }
487
488                 /*
489                  *      Only open for writing if we're
490                  *      marking requests as completed.
491                  */
492                 data->fp = fdopen(data->work_fd, data->track ? "r+" : "r");
493                 if (!data->fp) {
494                         ERROR("detail (%s): FATAL: Failed to re-open detail file: %s",
495                               data->name, fr_syserror(errno));
496                         fr_exit(1);
497                 }
498
499                 /*
500                  *      Look for the header
501                  */
502                 data->state = STATE_HEADER;
503                 data->delay_time = USEC;
504                 data->vps = NULL;
505
506                 /* FALL-THROUGH */
507
508         case STATE_HEADER:
509         do_header:
510                 data->done_entry = false;
511                 data->timestamp_offset = 0;
512
513                 data->tries = 0;
514                 if (!data->fp) {
515                         data->state = STATE_UNOPENED;
516                         goto open_file;
517                 }
518
519                 {
520                         struct stat buf;
521
522                         if (fstat(data->work_fd, &buf) < 0) {
523                                 ERROR("detail (%s): Failed to stat detail file: %s",
524                                       data->name, fr_syserror(errno));
525
526                                 goto cleanup;
527                         }
528                         if (((off_t) ftell(data->fp)) == buf.st_size) {
529                                 goto cleanup;
530                         }
531                 }
532
533                 /*
534                  *      End of file.  Delete it, and re-set
535                  *      everything.
536                  */
537                 if (feof(data->fp)) {
538                 cleanup:
539                         DEBUG("detail (%s): Unlinking %s", data->name, data->filename_work);
540                         unlink(data->filename_work);
541                         if (data->fp) fclose(data->fp);
542                         data->fp = NULL;
543                         data->work_fd = -1;
544                         data->state = STATE_UNOPENED;
545                         rad_assert(data->vps == NULL);
546
547                         if (data->one_shot) {
548                                 INFO("detail (%s): Finished reading \"one shot\" detail file - Exiting", data->name);
549                                 radius_signal_self(RADIUS_SIGNAL_SELF_EXIT);
550                         }
551
552                         return NULL;
553                 }
554
555                 /*
556                  *      Else go read something.
557                  */
558                 if (!fgets(buffer, sizeof(buffer), data->fp)) {
559                         DEBUG("detail (%s): Failed reading header from file - %s",
560                               data->name, data->filename_work);
561                         goto cleanup;
562                 }
563
564                 /*
565                  *      Badly formatted file: delete it.
566                  */
567                 if (!strchr(buffer, '\n')) {
568                         DEBUG("detail (%s): Invalid line without trailing LF - %s", data->name, buffer);
569                         goto cleanup;
570                 }
571
572                 if (!sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
573                         DEBUG("detail (%s): Failed reading detail file header in line - %s", data->name, buffer);
574                         goto cleanup;
575                 }
576
577                 data->state = STATE_READING;
578                 /* FALL-THROUGH */
579
580
581         /*
582          *      Read more value-pair's, unless we're
583          *      at EOF.  In that case, queue whatever
584          *      we have.
585          */
586         case STATE_READING:
587                 rad_assert(data->fp != NULL);
588
589                 fr_cursor_init(&cursor, &data->vps);
590
591                 /*
592                  *      Read a header, OR a value-pair.
593                  */
594                 while (fgets(buffer, sizeof(buffer), data->fp)) {
595                         data->last_offset = data->offset;
596                         data->offset = ftell(data->fp); /* for statistics */
597
598                         /*
599                          *      Badly formatted file: delete it.
600                          */
601                         if (!strchr(buffer, '\n')) {
602                                 WARN("detail (%s): Skipping line without trailing LF - %s", data->name, buffer);
603                                 fr_pair_list_free(&data->vps);
604                                 goto cleanup;
605                         }
606
607                         /*
608                          *      We're reading VP's, and got a blank line.
609                          *      That indicates the end of an entry.  Queue the
610                          *      packet.
611                          */
612                         if (buffer[0] == '\n') {
613                                 data->state = STATE_QUEUED;
614                                 data->tries = 0;
615                                 data->packets++;
616                                 goto alloc_packet;
617                         }
618
619                         /*
620                          *      We have a full "attribute = value" line.
621                          *      If it doesn't look reasonable, skip it.
622                          *
623                          *      FIXME: print an error for badly formatted attributes?
624                          */
625                         if (sscanf(buffer, "%255s %7s %1023s", key, op, value) != 3) {
626                                 DEBUG("detail (%s): Skipping badly formatted line - %s", data->name, buffer);
627                                 continue;
628                         }
629
630                         /*
631                          *      Should be =, :=, +=, ...
632                          */
633                         if (!strchr(op, '=')) {
634                                 DEBUG("detail (%s): Skipping line without operator - %s", data->name, buffer);
635                                 continue;
636                         }
637
638                         /*
639                          *      Skip non-protocol attributes.
640                          */
641                         if (!strcasecmp(key, "Request-Authenticator")) continue;
642
643                         /*
644                          *      Set the original client IP address, based on
645                          *      what's in the detail file.
646                          *
647                          *      Hmm... we don't set the server IP address.
648                          *      or port.  Oh well.
649                          */
650                         if (!strcasecmp(key, "Client-IP-Address")) {
651                                 data->client_ip.af = AF_INET;
652                                 if (ip_hton(&data->client_ip, AF_INET, value, false) < 0) {
653                                         DEBUG("detail (%s): Failed parsing Client-IP-Address", data->name);
654                                         fr_pair_list_free(&data->vps);
655                                         goto cleanup;
656                                 }
657                                 continue;
658                         }
659
660                         /*
661                          *      The original time at which we received the
662                          *      packet.  We need this to properly calculate
663                          *      Acct-Delay-Time.
664                          */
665                         if (!strcasecmp(key, "Timestamp")) {
666                                 data->timestamp = atoi(value);
667                                 data->timestamp_offset = data->last_offset;
668
669                                 vp = fr_pair_afrom_num(data, PW_PACKET_ORIGINAL_TIMESTAMP, 0);
670                                 if (vp) {
671                                         vp->vp_date = (uint32_t) data->timestamp;
672                                         vp->type = VT_DATA;
673                                         fr_cursor_insert(&cursor, vp);
674                                 }
675                                 continue;
676                         }
677
678                         if (!strcasecmp(key, "Donestamp")) {
679                                 data->timestamp = atoi(value);
680                                 data->done_entry = true;
681                                 continue;
682                         }
683
684                         DEBUG3("detail (%s): Trying to read VP from line - %s", data->name, buffer);
685
686                         /*
687                          *      Read one VP.
688                          *
689                          *      FIXME: do we want to check for non-protocol
690                          *      attributes like radsqlrelay does?
691                          */
692                         vp = NULL;
693                         if ((fr_pair_list_afrom_str(data, buffer, &vp) > 0) &&
694                             (vp != NULL)) {
695                                 fr_cursor_merge(&cursor, vp);
696                         } else {
697                                 DEBUG("detail (%s): Failed reading VP from line - %s", data->name, buffer);
698                                 goto cleanup;
699                         }
700                 }
701
702                 /*
703                  *      The writer doesn't check that the
704                  *      record was completely written.  If the
705                  *      disk is full, this can result in a
706                  *      truncated record which has no trailing
707                  *      blank line.  When that happens, it's a
708                  *      bad record, and we ignore it.
709                  */
710                 if (feof(data->fp)) {
711                         DEBUG("detail (%s): Truncated record: treating it as EOF for detail file %s",
712                               data->name, data->filename_work);
713                         fr_pair_list_free(&data->vps);
714                         goto cleanup;
715                 }
716
717                 /*
718                  *      Some kind of non-eof error.
719                  *
720                  *      FIXME: Leave the file in-place, and warn the
721                  *      administrator?
722                  */
723                 DEBUG("detail (%s): Unknown error, deleting detail file %s",
724                       data->name, data->filename_work);
725                 goto cleanup;
726
727         case STATE_QUEUED:
728                 goto alloc_packet;
729
730         /*
731          *      Periodically check what's going on.
732          *      If the request is taking too long,
733          *      retry it.
734          */
735         case STATE_RUNNING:
736                 if (time(NULL) < (data->running + (int)data->retry_interval)) {
737                         return NULL;
738                 }
739
740                 DEBUG("detail (%s): No response to detail request.  Retrying", data->name);
741                 /* FALL-THROUGH */
742
743         /*
744          *      If there's no reply, keep
745          *      retransmitting the current packet
746          *      forever.
747          */
748         case STATE_NO_REPLY:
749                 data->state = STATE_QUEUED;
750                 goto alloc_packet;
751
752         /*
753          *      We have a reply.  Clean up the old
754          *      request, and go read another one.
755          */
756         case STATE_REPLIED:
757                 if (data->track) {
758                         rad_assert(data->fp != NULL);
759
760                         if (fseek(data->fp, data->timestamp_offset, SEEK_SET) < 0) {
761                                 DEBUG("detail (%s): Failed seeking to timestamp offset: %s",
762                                      data->name, fr_syserror(errno));
763                         } else if (fwrite("\tDone", 1, 5, data->fp) < 5) {
764                                 DEBUG("detail (%s): Failed marking request as done: %s",
765                                      data->name, fr_syserror(errno));
766                         } else if (fflush(data->fp) != 0) {
767                                 DEBUG("detail (%s): Failed flushing marked detail file to disk: %s",
768                                      data->name, fr_syserror(errno));
769                         }
770
771                         if (fseek(data->fp, data->offset, SEEK_SET) < 0) {
772                                 DEBUG("detail (%s): Failed seeking to next detail request: %s",
773                                      data->name, fr_syserror(errno));
774                         }
775                 }
776
777                 fr_pair_list_free(&data->vps);
778                 data->state = STATE_HEADER;
779                 goto do_header;
780         }
781
782         /*
783          *      Process the packet.
784          */
785  alloc_packet:
786         if (data->done_entry) {
787                 DEBUG2("detail (%s): Skipping record for timestamp %lu", data->name, data->timestamp);
788                 fr_pair_list_free(&data->vps);
789                 data->state = STATE_HEADER;
790                 goto do_header;
791         }
792
793         data->tries++;
794
795         /*
796          *      We're done reading the file, but we didn't read
797          *      anything.  Clean up, and don't return anything.
798          */
799         if (!data->vps) {
800                 WARN("detail (%s): Read empty packet from file %s",
801                      data->name, data->filename_work);
802                 data->state = STATE_HEADER;
803                 return NULL;
804         }
805
806         /*
807          *      Allocate the packet.  If we fail, it's a serious
808          *      problem.
809          */
810         packet = rad_alloc(NULL, true);
811         if (!packet) {
812                 ERROR("detail (%s): FATAL: Failed allocating memory for detail", data->name);
813                 fr_exit(1);
814         }
815
816         memset(packet, 0, sizeof(*packet));
817         packet->sockfd = -1;
818         packet->src_ipaddr.af = AF_INET;
819         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
820
821         /*
822          *      If everything's OK, this is a waste of memory.
823          *      Otherwise, it lets us re-send the original packet
824          *      contents, unmolested.
825          */
826         packet->vps = fr_pair_list_copy(packet, data->vps);
827
828         packet->code = PW_CODE_ACCOUNTING_REQUEST;
829         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TYPE, 0, TAG_ANY);
830         if (vp) packet->code = vp->vp_integer;
831
832         gettimeofday(&packet->timestamp, NULL);
833
834         /*
835          *      Remember where it came from, so that we don't
836          *      proxy it to the place it came from...
837          */
838         if (data->client_ip.af != AF_UNSPEC) {
839                 packet->src_ipaddr = data->client_ip;
840         }
841
842         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IP_ADDRESS, 0, TAG_ANY);
843         if (vp) {
844                 packet->src_ipaddr.af = AF_INET;
845                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
846                 packet->src_ipaddr.prefix = 32;
847         } else {
848                 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS, 0, TAG_ANY);
849                 if (vp) {
850                         packet->src_ipaddr.af = AF_INET6;
851                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
852                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
853                         packet->src_ipaddr.prefix = 128;
854                 }
855         }
856
857         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IP_ADDRESS, 0, TAG_ANY);
858         if (vp) {
859                 packet->dst_ipaddr.af = AF_INET;
860                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
861                 packet->dst_ipaddr.prefix = 32;
862         } else {
863                 vp = fr_pair_find_by_num(packet->vps, PW_PACKET_DST_IPV6_ADDRESS, 0, TAG_ANY);
864                 if (vp) {
865                         packet->dst_ipaddr.af = AF_INET6;
866                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
867                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
868                         packet->dst_ipaddr.prefix = 128;
869                 }
870         }
871
872         /*
873          *      Generate packet ID, ports, IP via a counter.
874          */
875         packet->id = data->counter & 0xff;
876         packet->src_port = 1024 + ((data->counter >> 8) & 0xff);
877         packet->dst_port = 1024 + ((data->counter >> 16) & 0xff);
878
879         packet->dst_ipaddr.af = AF_INET;
880         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | ((data->counter >> 24) & 0xff));
881
882         /*
883          *      Create / update accounting attributes.
884          */
885         if (packet->code == PW_CODE_ACCOUNTING_REQUEST) {
886                 /*
887                  *      Prefer the Event-Timestamp in the packet, if it
888                  *      exists.  That is when the event occurred, whereas the
889                  *      "Timestamp" field is when we wrote the packet to the
890                  *      detail file, which could have been much later.
891                  */
892                 vp = fr_pair_find_by_num(packet->vps, PW_EVENT_TIMESTAMP, 0, TAG_ANY);
893                 if (vp) {
894                         data->timestamp = vp->vp_integer;
895                 }
896
897                 /*
898                  *      Look for Acct-Delay-Time, and update
899                  *      based on Acct-Delay-Time += (time(NULL) - timestamp)
900                  */
901                 vp = fr_pair_find_by_num(packet->vps, PW_ACCT_DELAY_TIME, 0, TAG_ANY);
902                 if (!vp) {
903                         vp = fr_pair_afrom_num(packet, PW_ACCT_DELAY_TIME, 0);
904                         rad_assert(vp != NULL);
905                         fr_pair_add(&packet->vps, vp);
906                 }
907                 if (data->timestamp != 0) {
908                         vp->vp_integer += time(NULL) - data->timestamp;
909                 }
910         }
911
912         /*
913          *      Set the transmission count.
914          */
915         vp = fr_pair_find_by_num(packet->vps, PW_PACKET_TRANSMIT_COUNTER, 0, TAG_ANY);
916         if (!vp) {
917                 vp = fr_pair_afrom_num(packet, PW_PACKET_TRANSMIT_COUNTER, 0);
918                 rad_assert(vp != NULL);
919                 fr_pair_add(&packet->vps, vp);
920         }
921         vp->vp_integer = data->tries;
922
923         data->state = STATE_RUNNING;
924         data->running = packet->timestamp.tv_sec;
925
926         return packet;
927 }
928
929 /*
930  *      Free detail-specific stuff.
931  */
932 void detail_free(rad_listen_t *this)
933 {
934         listen_detail_t *data = this->data;
935
936 #ifdef WITH_DETAIL_THREAD
937         if (!check_config) {
938                 ssize_t ret;
939                 void *arg = NULL;
940
941                 /*
942                  *      Mark the child pipes as unusable
943                  */
944                 close(data->child_pipe[0]);
945                 close(data->child_pipe[1]);
946                 data->child_pipe[0] = -1;
947
948                 /*
949                  *      Tell it to stop (interrupting its sleep)
950                  */
951                 pthread_kill(data->pthread_id, SIGTERM);
952
953                 /*
954                  *      Wait for it to acknowledge that it's stopped.
955                  */
956                 ret = read(data->master_pipe[0], &arg, sizeof(arg));
957                 if (ret < 0) {
958                         ERROR("detail (%s): Reader thread exited without informing the master: %s",
959                               data->name, fr_syserror(errno));
960                 } else if (ret != sizeof(arg)) {
961                         ERROR("detail (%s): Invalid thread pointer received from reader thread during exit",
962                               data->name);
963                         ERROR("detail (%s): Expected %zu bytes, got %zi bytes", data->name, sizeof(arg), ret);
964                 }
965
966                 close(data->master_pipe[0]);
967                 close(data->master_pipe[1]);
968
969                 if (arg) pthread_join(data->pthread_id, &arg);
970         }
971 #endif
972
973         if (data->fp != NULL) {
974                 fclose(data->fp);
975                 data->fp = NULL;
976         }
977 }
978
979
980 int detail_print(rad_listen_t const *this, char *buffer, size_t bufsize)
981 {
982         if (!this->server) {
983                 return snprintf(buffer, bufsize, "%s",
984                                 ((listen_detail_t *)(this->data))->filename);
985         }
986
987         return snprintf(buffer, bufsize, "detail file %s as server %s",
988                         ((listen_detail_t *)(this->data))->filename,
989                         this->server);
990 }
991
992
993 /*
994  *      Delay while waiting for a file to be ready
995  */
996 static int detail_delay(listen_detail_t *data)
997 {
998         int delay = (data->poll_interval - 1) * USEC;
999
1000         /*
1001          *      Add +/- 0.25s of jitter
1002          */
1003         delay += (USEC * 3) / 4;
1004         delay += fr_rand() % (USEC / 2);
1005
1006         DEBUG2("detail (%s): Detail listener state %s waiting %d.%06d sec",
1007                data->name,
1008                fr_int2str(state_names, data->state, "?"),
1009                (delay / USEC), delay % USEC);
1010
1011         return delay;
1012 }
1013
1014 /*
1015  *      Overloaded to return delay times.
1016  */
1017 int detail_encode(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1018 {
1019 #ifdef WITH_DETAIL_THREAD
1020         return 0;
1021 #else
1022         listen_detail_t *data = this->data;
1023
1024         /*
1025          *      We haven't sent a packet... delay things a bit.
1026          */
1027         if (!data->signal) return detail_delay(data);
1028
1029         data->signal = 0;
1030
1031         DEBUG2("detail (%s): Detail listener state %s signalled %d waiting %d.%06d sec",
1032                data->name,
1033                fr_int2str(state_names, data->state, "?"),
1034                data->signal,
1035                data->delay_time / USEC,
1036                data->delay_time % USEC);
1037
1038         return data->delay_time;
1039 #endif
1040 }
1041
1042 /*
1043  *      Overloaded to return "should we fix delay times"
1044  */
1045 int detail_decode(rad_listen_t *this, REQUEST *request)
1046 {
1047 #ifdef WITH_DETAIL_THREAD
1048         listen_detail_t *data = this->data;
1049
1050         RDEBUG("Received %s from detail file %s",
1051                fr_packet_codes[request->packet->code], data->filename_work);
1052
1053         rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1054
1055         return 0;
1056 #else
1057         listen_detail_t *data = this->data;
1058
1059         RDEBUG("Received %s from detail file %s",
1060                fr_packet_codes[request->packet->code], data->filename_work);
1061
1062         rdebug_pair_list(L_DBG_LVL_1, request, request->packet->vps, "\t");
1063
1064         return data->signal;
1065 #endif
1066 }
1067
1068
1069 #ifdef WITH_DETAIL_THREAD
1070 static void *detail_handler_thread(void *arg)
1071 {
1072         char c;
1073         rad_listen_t *this = arg;
1074         listen_detail_t *data = this->data;
1075
1076         while (true) {
1077                 RADIUS_PACKET *packet;
1078
1079                 while ((packet = detail_poll(this)) == NULL) {
1080                         usleep(detail_delay(data));
1081
1082                         /*
1083                          *      If we're supposed to exit then tell
1084                          *      the master thread we've exited.
1085                          */
1086                         if (data->child_pipe[0] < 0) {
1087                                 packet = NULL;
1088                                 if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1089                                         ERROR("detail (%s): Failed writing exit status to master: %s",
1090                                               data->name, fr_syserror(errno));
1091                                 }
1092                                 return NULL;
1093                         }
1094                 }
1095
1096                 /*
1097                  *      Keep retrying forever.
1098                  *
1099                  *      FIXME: cap the retries.
1100                  */
1101                 do {
1102                         if (write(data->master_pipe[1], &packet, sizeof(packet)) < 0) {
1103                                 ERROR("detail (%s): Failed passing detail packet pointer to master: %s",
1104                                       data->name, fr_syserror(errno));
1105                         }
1106
1107                         if (read(data->child_pipe[0], &c, 1) < 0) {
1108                                 ERROR("detail (%s): Failed getting detail packet ack from master: %s",
1109                                       data->name, fr_syserror(errno));
1110                                 break;
1111                         }
1112
1113                         if (data->delay_time > 0) usleep(data->delay_time);
1114
1115                         packet = detail_poll(this);
1116                         if (!packet) break;
1117                 } while (data->state != STATE_REPLIED);
1118         }
1119
1120         return NULL;
1121 }
1122 #endif
1123
1124
1125 static const CONF_PARSER detail_config[] = {
1126         { "detail", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_DEPRECATED, listen_detail_t, filename), NULL },
1127         { "filename", FR_CONF_OFFSET(PW_TYPE_FILE_OUTPUT | PW_TYPE_REQUIRED, listen_detail_t, filename), NULL },
1128         { "load_factor", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, load_factor), STRINGIFY(10) },
1129         { "poll_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, poll_interval), STRINGIFY(1) },
1130         { "retry_interval", FR_CONF_OFFSET(PW_TYPE_INTEGER, listen_detail_t, retry_interval), STRINGIFY(30) },
1131         { "one_shot", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, one_shot), "no" },
1132         { "track", FR_CONF_OFFSET(PW_TYPE_BOOLEAN, listen_detail_t, track), "no" },
1133         CONF_PARSER_TERMINATOR
1134 };
1135
1136 /*
1137  *      Parse a detail section.
1138  */
1139 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
1140 {
1141         int             rcode;
1142         listen_detail_t *data;
1143         RADCLIENT       *client;
1144         char            buffer[2048];
1145
1146         data = this->data;
1147
1148         rcode = cf_section_parse(cs, data, detail_config);
1149         if (rcode < 0) {
1150                 cf_log_err_cs(cs, "Failed parsing listen section");
1151                 return -1;
1152         }
1153
1154         data->name = cf_section_name2(cs);
1155         if (!data->name) data->name = data->filename;
1156
1157         /*
1158          *      We don't do duplicate detection for "detail" sockets.
1159          */
1160         this->nodup = true;
1161         this->synchronous = false;
1162
1163         if (!data->filename) {
1164                 cf_log_err_cs(cs, "No detail file specified in listen section");
1165                 return -1;
1166         }
1167
1168         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, >=, 1);
1169         FR_INTEGER_BOUND_CHECK("load_factor", data->load_factor, <=, 100);
1170
1171         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, >=, 1);
1172         FR_INTEGER_BOUND_CHECK("poll_interval", data->poll_interval, <=, 60);
1173
1174         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, >=, 4);
1175         FR_INTEGER_BOUND_CHECK("retry_interval", data->retry_interval, <=, 3600);
1176
1177         /*
1178          *      Only checking the config.  Don't start threads or anything else.
1179          */
1180         if (check_config) return 0;
1181
1182         /*
1183          *      If the filename is a glob, use "detail.work" as the
1184          *      work file name.
1185          */
1186         if ((strchr(data->filename, '*') != NULL) ||
1187             (strchr(data->filename, '[') != NULL)) {
1188                 char *p;
1189
1190 #ifndef HAVE_GLOB_H
1191                 WARN("detail (%s): File \"%s\" appears to use file globbing, but it is not supported on this system",
1192                      data->name, data->filename);
1193 #endif
1194                 strlcpy(buffer, data->filename, sizeof(buffer));
1195                 p = strrchr(buffer, FR_DIR_SEP);
1196                 if (p) {
1197                         p[1] = '\0';
1198                 } else {
1199                         buffer[0] = '\0';
1200                 }
1201
1202                 /*
1203                  *      Globbing cannot be done across directories.
1204                  */
1205                 if ((strchr(buffer, '*') != NULL) ||
1206                     (strchr(buffer, '[') != NULL)) {
1207                         cf_log_err_cs(cs, "Wildcard directories are not supported");
1208                         return -1;
1209                 }
1210
1211                 strlcat(buffer, "detail.work",
1212                         sizeof(buffer) - strlen(buffer));
1213
1214         } else {
1215                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
1216         }
1217
1218         data->filename_work = talloc_strdup(data, buffer);
1219
1220         data->work_fd = -1;
1221         data->vps = NULL;
1222         data->fp = NULL;
1223         data->state = STATE_UNOPENED;
1224         data->delay_time = data->poll_interval * USEC;
1225         data->signal = 1;
1226
1227         /*
1228          *      Initialize the fake client.
1229          */
1230         client = &data->detail_client;
1231         memset(client, 0, sizeof(*client));
1232         client->ipaddr.af = AF_INET;
1233         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
1234         client->ipaddr.prefix = 0;
1235         client->longname = client->shortname = data->filename;
1236         client->secret = client->shortname;
1237         client->nas_type = talloc_strdup(data, "none"); /* Part of 'data' not dynamically allocated */
1238
1239 #ifdef WITH_DETAIL_THREAD
1240         /*
1241          *      Create the communication pipes.
1242          */
1243         if (pipe(data->master_pipe) < 0) {
1244                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1245                 fr_exit(1);
1246         }
1247
1248         if (pipe(data->child_pipe) < 0) {
1249                 ERROR("detail (%s): Error opening internal pipe: %s", data->name, fr_syserror(errno));
1250                 fr_exit(1);
1251         }
1252
1253         pthread_create(&data->pthread_id, NULL, detail_handler_thread, this);
1254
1255         this->fd = data->master_pipe[0];
1256 #endif
1257
1258         return 0;
1259 }
1260 #endif