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