e9cc98e7dbe94b68af5cc08e07f81c8345a81c5b
[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 #include <freeradius-devel/ident.h>
25 RCSID("$Id$")
26
27 #include <freeradius-devel/radiusd.h>
28 #include <freeradius-devel/modules.h>
29 #include <freeradius-devel/detail.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 typedef enum detail_state_t {
47   STATE_UNOPENED = 0,
48   STATE_UNLOCKED,
49   STATE_HEADER,
50   STATE_READING,
51   STATE_QUEUED,
52   STATE_RUNNING,
53   STATE_NO_REPLY,
54   STATE_REPLIED
55 } detail_state_t;
56
57 static FR_NAME_NUMBER state_names[] = {
58         { "unopened", STATE_UNOPENED },
59         { "unlocked", STATE_UNLOCKED },
60         { "header", STATE_HEADER },
61         { "reading", STATE_READING },
62         { "queued", STATE_QUEUED },
63         { "running", STATE_RUNNING },
64         { "no-reply", STATE_NO_REPLY },
65         { "replied", STATE_REPLIED },
66
67         { NULL, 0 }
68 };
69
70 typedef struct listen_detail_t {
71         fr_event_t      *ev;    /* has to be first entry (ugh) */
72         int             delay_time;
73         char            *filename;
74         char            *filename_work;
75         VALUE_PAIR      *vps;
76         FILE            *fp;
77         detail_state_t  state;
78         time_t          timestamp;
79         fr_ipaddr_t     client_ip;
80         int             load_factor; /* 1..100 */
81         int             signal;
82         int             poll_interval;
83         int             retry_interval;
84
85         int             has_rtt;
86         int             srtt;
87         int             rttvar;
88         struct timeval  last_packet;
89         RADCLIENT       detail_client;
90 } listen_detail_t;
91
92
93 /*
94  *      If we're limiting outstanding packets, then mark the response
95  *      as being sent.
96  */
97 int detail_send(rad_listen_t *listener, REQUEST *request)
98 {
99         int rtt;
100         struct timeval now;
101         listen_detail_t *data = listener->data;
102
103         rad_assert(request->listener == listener);
104         rad_assert(listener->send == detail_send);
105
106         /*
107          *      This request timed out.  Remember that, and tell the
108          *      caller it's OK to read more "detail" file stuff.
109          */
110         if (request->reply->code == 0) {
111                 data->delay_time = data->retry_interval * USEC;
112                 data->signal = 1;
113                 data->state = STATE_NO_REPLY;
114
115                 RDEBUG("Detail - No response configured for request %d.  Will retry in %d seconds",
116                        request->number, data->retry_interval);
117
118                 radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
119                 return 0;
120         }
121
122         /*
123          *      We call gettimeofday a lot.  But it should be OK,
124          *      because there's nothing else to do.
125          */
126         gettimeofday(&now, NULL);
127
128         /*
129          *      If we haven't sent a packet in the last second, reset
130          *      the RTT.
131          */
132         now.tv_sec -= 1;
133         if (timercmp(&data->last_packet, &now, <)) {
134                 data->has_rtt = FALSE;
135         }
136         now.tv_sec += 1;
137
138         /*
139          *      Only one detail packet may be outstanding at a time,
140          *      so it's safe to update some entries in the detail
141          *      structure.
142          *
143          *      We keep smoothed round trip time (SRTT), but not round
144          *      trip timeout (RTO).  We use SRTT to calculate a rough
145          *      load factor.
146          */
147         rtt = now.tv_sec - request->received.tv_sec;
148         rtt *= USEC;
149         rtt += now.tv_usec;
150         rtt -= request->received.tv_usec;
151
152         /*
153          *      If we're proxying, the RTT is our processing time,
154          *      plus the network delay there and back, plus the time
155          *      on the other end to process the packet.  Ideally, we
156          *      should remove the network delays from the RTT, but we
157          *      don't know what they are.
158          *
159          *      So, to be safe, we over-estimate the total cost of
160          *      processing the packet.
161          */
162         if (!data->has_rtt) {
163                 data->has_rtt = TRUE;
164                 data->srtt = rtt;
165                 data->rttvar = rtt / 2;
166
167         } else {
168                 data->rttvar -= data->rttvar >> 2;
169                 data->rttvar += (data->srtt - rtt);
170                 data->srtt -= data->srtt >> 3;
171                 data->srtt += rtt >> 3;
172         }
173
174         /*
175          *      Calculate the time we wait before sending the next
176          *      packet.
177          *
178          *      rtt / (rtt + delay) = load_factor / 100
179          */
180         data->delay_time = (data->srtt * (100 - data->load_factor)) / (data->load_factor);
181
182         /*
183          *      Cap delay at 4 packets/s.  If the end system can't
184          *      handle this, then it's very broken.
185          */
186         if (data->delay_time > (USEC / 4)) data->delay_time= USEC / 4;
187         
188         RDEBUG3("Received response for request %d.  Will read the next packet in %d seconds",
189                 request->number, data->delay_time / USEC);
190         
191         data->last_packet = now;
192         data->signal = 1;
193         data->state = STATE_REPLIED;
194         radius_signal_self(RADIUS_SIGNAL_SELF_DETAIL);
195
196         return 0;
197 }
198
199
200 /*
201  *      Open the detail file, if we can.
202  *
203  *      FIXME: create it, if it's not already there, so that the main
204  *      server select() will wake us up if there's anything to read.
205  */
206 static int detail_open(rad_listen_t *this)
207 {
208         struct stat st;
209         listen_detail_t *data = this->data;
210         char *filename = data->filename;
211
212         rad_assert(data->state == STATE_UNOPENED);
213         data->delay_time = USEC;
214
215         /*
216          *      Open detail.work first, so we don't lose
217          *      accounting packets.  It's probably better to
218          *      duplicate them than to lose them.
219          *
220          *      Note that we're not writing to the file, but
221          *      we've got to open it for writing in order to
222          *      establish the lock, to prevent rlm_detail from
223          *      writing to it.
224          *
225          *      This also means that if we're doing globbing,
226          *      this file will be read && processed before the
227          *      file globbing is done.
228          */
229         this->fd = open(data->filename_work, O_RDWR);
230         if (this->fd < 0) {
231                 DEBUG2("Polling for detail file %s", filename);
232
233                 /*
234                  *      Try reading the detail file.  If it
235                  *      doesn't exist, we can't do anything.
236                  *
237                  *      Doing the stat will tell us if the file
238                  *      exists, even if we don't have permissions
239                  *      to read it.
240                  */
241                 if (stat(filename, &st) < 0) {
242 #ifdef HAVE_GLOB_H
243                         unsigned int i;
244                         int found;
245                         time_t chtime;
246                         glob_t files;
247
248                         memset(&files, 0, sizeof(files));
249                         if (glob(filename, 0, NULL, &files) != 0) {
250                                 return 0;
251                         }
252
253                         chtime = 0;
254                         found = -1;
255                         for (i = 0; i < files.gl_pathc; i++) {
256                                 if (stat(files.gl_pathv[i], &st) < 0) continue;
257
258                                 if ((i == 0) ||
259                                     (st.st_ctime < chtime)) {
260                                         chtime = st.st_ctime;
261                                         found = i;
262                                 }
263                         }
264
265                         if (found < 0) {
266                                 globfree(&files);
267                                 return 0;
268                         }
269
270                         filename = strdup(files.gl_pathv[found]);
271                         globfree(&files);
272 #else
273                         return 0;
274 #endif
275                 }
276
277                 /*
278                  *      Open it BEFORE we rename it, just to
279                  *      be safe...
280                  */
281                 this->fd = open(filename, O_RDWR);
282                 if (this->fd < 0) {
283                         radlog(L_ERR, "Detail - Failed to open %s: %s",
284                                filename, strerror(errno));
285                         if (filename != data->filename) free(filename);
286                         return 0;
287                 }
288
289                 /*
290                  *      Rename detail to detail.work
291                  */
292                 DEBUG("Detail - Renaming %s -> %s", filename, data->filename_work);
293                 if (rename(filename, data->filename_work) < 0) {
294                         if (filename != data->filename) free(filename);
295                         close(this->fd);
296                         this->fd = -1;
297                         return 0;
298                 }
299                 if (filename != data->filename) free(filename);
300         } /* else detail.work existed, and we opened it */
301
302         rad_assert(data->vps == NULL);
303         rad_assert(data->fp == NULL);
304
305         data->state = STATE_UNLOCKED;
306
307         data->client_ip.af = AF_UNSPEC;
308         data->timestamp = 0;
309
310         return 1;
311 }
312
313
314 /*
315  *      FIXME: add a configuration "exit when done" so that the detail
316  *      file reader can be used as a one-off tool to update stuff.
317  *
318  *      The time sequence for reading from the detail file is:
319  *
320  *      t_0             signalled that the server is idle, and we
321  *                      can read from the detail file.
322  *
323  *      t_rtt           the packet has been processed successfully,
324  *                      wait for t_delay to enforce load factor.
325  *                      
326  *      t_rtt + t_delay wait for signal that the server is idle.
327  *      
328  */
329 int detail_recv(rad_listen_t *listener,
330                 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
331 {
332         char            key[256], op[8], value[1024];
333         VALUE_PAIR      *vp, **tail;
334         RADIUS_PACKET   *packet;
335         char            buffer[2048];
336         listen_detail_t *data = listener->data;
337
338         /*
339          *      We may be in the main thread.  It needs to update the
340          *      timers before we try to read from the file again.
341          */
342         if (data->signal) return 0;
343
344         switch (data->state) {
345                 case STATE_UNOPENED:
346         open_file:
347                         rad_assert(listener->fd < 0);
348                         
349                         if (!detail_open(listener)) return 0;
350
351                         rad_assert(data->state == STATE_UNLOCKED);
352                         rad_assert(listener->fd >= 0);
353
354                         /* FALL-THROUGH */
355
356                         /*
357                          *      Try to lock fd.  If we can't, return.
358                          *      If we can, continue.  This means that
359                          *      the server doesn't block while waiting
360                          *      for the lock to open...
361                          */
362                 case STATE_UNLOCKED:
363                         /*
364                          *      Note that we do NOT block waiting for
365                          *      the lock.  We've re-named the file
366                          *      above, so we've already guaranteed
367                          *      that any *new* detail writer will not
368                          *      be opening this file.  The only
369                          *      purpose of the lock is to catch a race
370                          *      condition where the execution
371                          *      "ping-pongs" between radiusd &
372                          *      radrelay.
373                          */
374                         if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
375                                 /*
376                                  *      Close the FD.  The main loop
377                                  *      will wake up in a second and
378                                  *      try again.
379                                  */
380                                 close(listener->fd);
381                                 listener->fd = -1;
382                                 data->state = STATE_UNOPENED;
383                                 return 0;
384                         }
385
386                         data->fp = fdopen(listener->fd, "r");
387                         if (!data->fp) {
388                                 radlog(L_ERR, "FATAL: Failed to re-open detail file %s: %s",
389                                        data->filename, strerror(errno));
390                                 exit(1);
391                         }
392
393                         /*
394                          *      Look for the header
395                          */
396                         data->state = STATE_HEADER;
397                         data->delay_time = USEC;
398                         data->vps = NULL;
399
400                         /* FALL-THROUGH */
401
402                 case STATE_HEADER:
403                 do_header:
404                         if (!data->fp) {
405                                 data->state = STATE_UNOPENED;
406                                 goto open_file;
407                         }
408
409                         {
410                                 struct stat buf;
411                                 
412                                 fstat(listener->fd, &buf);
413                                 if (((off_t) ftell(data->fp)) == buf.st_size) {
414                                         goto cleanup;
415                                 }
416                         }
417
418                         /*
419                          *      End of file.  Delete it, and re-set
420                          *      everything.
421                          */
422                         if (feof(data->fp)) {
423                         cleanup:
424                                 DEBUG("Detail - unlinking %s",
425                                       data->filename_work);
426                                 unlink(data->filename_work);
427                                 if (data->fp) fclose(data->fp);
428                                 data->fp = NULL;
429                                 listener->fd = -1;
430                                 data->state = STATE_UNOPENED;
431                                 rad_assert(data->vps == NULL);
432                                 return 0;
433                         }
434
435                         /*
436                          *      Else go read something.
437                          */
438                         break;
439
440                         /*
441                          *      Read more value-pair's, unless we're
442                          *      at EOF.  In that case, queue whatever
443                          *      we have.
444                          */
445                 case STATE_READING:
446                         if (data->fp && !feof(data->fp)) break;
447                         data->state = STATE_QUEUED;
448
449                         /* FALL-THROUGH */
450
451                 case STATE_QUEUED:
452                         goto alloc_packet;
453
454                         /*
455                          *      We still have an outstanding packet.
456                          *      Don't read any more.
457                          */
458                 case STATE_RUNNING:
459                         return 0;
460
461                         /*
462                          *      If there's no reply, keep
463                          *      retransmitting the current packet
464                          *      forever.
465                          */
466                 case STATE_NO_REPLY:
467                         data->state = STATE_QUEUED;
468                         goto alloc_packet;
469                                 
470                         /*
471                          *      We have a reply.  Clean up the old
472                          *      request, and go read another one.
473                          */
474                 case STATE_REPLIED:
475                         pairfree(&data->vps);
476                         data->state = STATE_HEADER;
477                         goto do_header;
478         }
479         
480         tail = &data->vps;
481         while (*tail) tail = &(*tail)->next;
482
483         /*
484          *      Read a header, OR a value-pair.
485          */
486         while (fgets(buffer, sizeof(buffer), data->fp)) {
487                 /*
488                  *      Badly formatted file: delete it.
489                  *
490                  *      FIXME: Maybe flag an error?
491                  */
492                 if (!strchr(buffer, '\n')) {
493                         pairfree(&data->vps);
494                         goto cleanup;
495                 }
496
497                 /*
498                  *      We're reading VP's, and got a blank line.
499                  *      Queue the packet.
500                  */
501                 if ((data->state == STATE_READING) &&
502                     (buffer[0] == '\n')) {
503                         data->state = STATE_QUEUED;
504                         break;
505                 }
506
507                 /*
508                  *      Look for date/time header, and read VP's if
509                  *      found.  If not, keep reading lines until we
510                  *      find one.
511                  */
512                 if (data->state == STATE_HEADER) {
513                         int y;
514
515                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
516                                 data->state = STATE_READING;
517                         }
518                         continue;
519                 }
520
521                 /*
522                  *      We have a full "attribute = value" line.
523                  *      If it doesn't look reasonable, skip it.
524                  *
525                  *      FIXME: print an error for badly formatted attributes?
526                  */
527                 if (sscanf(buffer, "%255s %8s %1023s", key, op, value) != 3) {
528                         DEBUG2("WARNING: Skipping badly formatted line %s",
529                                buffer);
530                         continue;
531                 }
532
533                 /*
534                  *      Should be =, :=, +=, ...
535                  */
536                 if (!strchr(op, '=')) continue;
537
538                 /*
539                  *      Skip non-protocol attributes.
540                  */
541                 if (!strcasecmp(key, "Request-Authenticator")) continue;
542
543                 /*
544                  *      Set the original client IP address, based on
545                  *      what's in the detail file.
546                  *
547                  *      Hmm... we don't set the server IP address.
548                  *      or port.  Oh well.
549                  */
550                 if (!strcasecmp(key, "Client-IP-Address")) {
551                         data->client_ip.af = AF_INET;
552                         ip_hton(value, AF_INET, &data->client_ip);
553                         continue;
554                 }
555
556                 /*
557                  *      The original time at which we received the
558                  *      packet.  We need this to properly calculate
559                  *      Acct-Delay-Time.
560                  */
561                 if (!strcasecmp(key, "Timestamp")) {
562                         data->timestamp = atoi(value);
563
564                         vp = paircreate(PW_PACKET_ORIGINAL_TIMESTAMP,
565                                         PW_TYPE_DATE);
566                         if (vp) {
567                                 vp->vp_date = (uint32_t) data->timestamp;
568                                 *tail = vp;
569                                 tail = &(vp->next);
570                         }
571                         continue;
572                 }
573
574                 /*
575                  *      Read one VP.
576                  *
577                  *      FIXME: do we want to check for non-protocol
578                  *      attributes like radsqlrelay does?
579                  */
580                 vp = NULL;
581                 if ((userparse(buffer, &vp) > 0) &&
582                     (vp != NULL)) {
583                         *tail = vp;
584                         tail = &(vp->next);
585                 }
586         }
587
588         /*
589          *      Some kind of error.
590          *
591          *      FIXME: Leave the file in-place, and warn the
592          *      administrator?
593          */
594         if (ferror(data->fp)) goto cleanup;
595
596         /*
597          *      Process the packet.
598          */
599  alloc_packet:
600         rad_assert(data->state == STATE_QUEUED);
601
602         /*
603          *      We're done reading the file, but we didn't read
604          *      anything.  Clean up, and don't return anything.
605          */
606         if (!data->vps) {
607                 data->state = STATE_HEADER;
608                 if (feof(data->fp)) goto cleanup; 
609                 return 0;
610         }
611
612         /*
613          *      Allocate the packet.  If we fail, it's a serious
614          *      problem.
615          */
616         packet = rad_alloc(1);
617         if (!packet) {
618                 radlog(L_ERR, "FATAL: Failed allocating memory for detail");
619                 exit(1);
620         }
621
622         memset(packet, 0, sizeof(*packet));
623         packet->sockfd = -1;
624         packet->src_ipaddr.af = AF_INET;
625         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
626         packet->code = PW_ACCOUNTING_REQUEST;
627         packet->timestamp = time(NULL);
628
629         /*
630          *      Remember where it came from, so that we don't
631          *      proxy it to the place it came from...
632          */
633         if (data->client_ip.af != AF_UNSPEC) {
634                 packet->src_ipaddr = data->client_ip;
635         }
636
637         vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
638         if (vp) {
639                 packet->src_ipaddr.af = AF_INET;
640                 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
641         } else {
642                 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
643                 if (vp) {
644                         packet->src_ipaddr.af = AF_INET6;
645                         memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
646                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
647                 }
648         }
649
650         vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
651         if (vp) {
652                 packet->dst_ipaddr.af = AF_INET;
653                 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->vp_ipaddr;
654         } else {
655                 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
656                 if (vp) {
657                         packet->dst_ipaddr.af = AF_INET6;
658                         memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
659                                &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
660                 }
661         }
662
663         /*
664          *      We've got to give SOME value for Id & ports, so that
665          *      the packets can be added to the request queue.
666          *      However, we don't want to keep track of used/unused
667          *      id's and ports, as that's a lot of work.  This hack
668          *      ensures that (if we have real random numbers), that
669          *      there will be a collision on every 2^(16+15+15+24 - 1)
670          *      packets, on average.  That means we can read 2^37
671          *      packets before having a collision, which means it's
672          *      effectively impossible.
673          */
674         packet->id = fr_rand() & 0xffff;
675         packet->src_port = 1024 + (fr_rand() & 0x7fff);
676         packet->dst_port = 1024 + (fr_rand() & 0x7fff);
677
678         packet->dst_ipaddr.af = AF_INET;
679         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (fr_rand() & 0xffffff));
680
681         /*
682          *      If everything's OK, this is a waste of memory.
683          *      Otherwise, it lets us re-send the original packet
684          *      contents, unmolested.
685          */
686         packet->vps = paircopy(data->vps);
687
688         /*
689          *      Look for Acct-Delay-Time, and update
690          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
691          */
692         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
693         if (!vp) {
694                 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
695                 rad_assert(vp != NULL);
696                 pairadd(&packet->vps, vp);
697         }
698         if (data->timestamp != 0) {
699                 vp->vp_integer += time(NULL) - data->timestamp;
700         }
701
702         *pfun = rad_accounting;
703
704         if (debug_flag) {
705                 fr_printf_log("detail_recv: Read packet from %s\n", data->filename_work);
706                 for (vp = packet->vps; vp; vp = vp->next) {
707                         debug_pair(vp);
708                 }
709         }
710
711         /*
712          *      FIXME: many of these checks may not be necessary when
713          *      reading from the detail file.
714          *
715          *      Try again later...
716          */
717         if (!received_request(listener, packet, prequest,
718                               &data->detail_client)) {
719                 rad_free(&packet);
720                 data->state = STATE_NO_REPLY;   /* try again later */
721                 return 0;
722         }
723
724         data->state = STATE_RUNNING;
725
726         return 1;
727 }
728
729
730 /*
731  *      Free detail-specific stuff.
732  */
733 void detail_free(rad_listen_t *this)
734 {
735         listen_detail_t *data = this->data;
736
737         free(data->filename);
738         pairfree(&data->vps);
739
740         if (data->fp != NULL) fclose(data->fp);
741 }
742
743
744 int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
745 {
746         if (!this->server) {
747                 return snprintf(buffer, bufsize, "%s",
748                                 ((listen_detail_t *)(this->data))->filename);
749         }
750
751         return snprintf(buffer, bufsize, "detail file %s as server %s",
752                         ((listen_detail_t *)(this->data))->filename,
753                         this->server);
754 }
755
756 /*
757  *      Overloaded to return delay times.
758  */
759 int detail_encode(rad_listen_t *this, UNUSED REQUEST *request)
760 {
761         listen_detail_t *data = this->data;
762
763         /*
764          *      We haven't sent a packet... delay things a bit.
765          */
766         if (!data->signal) {
767                 int delay = (data->poll_interval - 1) * USEC;
768
769                 /*
770                  *      Add +/- 0.25s of jitter
771                  */
772                 delay += (USEC * 3) / 4;
773                 delay += fr_rand() % (USEC / 2);
774
775                 DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
776                        data->filename,
777                        fr_int2str(state_names, data->state, "?"), data->signal,
778                        (delay / USEC), delay % USEC);
779
780                 return delay;
781         }
782
783         data->signal = 0;
784         
785         DEBUG2("Detail listener %s state %s signalled %d waiting %d.%06d sec",
786                data->filename, fr_int2str(state_names, data->state, "?"),
787                data->signal,
788                data->delay_time / USEC,
789                data->delay_time % USEC);
790
791         return data->delay_time;
792 }
793
794
795 /*
796  *      Overloaded to return "should we fix delay times"
797  */
798 int detail_decode(rad_listen_t *this, UNUSED REQUEST *request)
799 {
800         listen_detail_t *data = this->data;
801
802         return data->signal;
803 }
804
805
806 static const CONF_PARSER detail_config[] = {
807         { "filename",   PW_TYPE_STRING_PTR,
808           offsetof(listen_detail_t, filename), NULL,  NULL },
809         { "load_factor",   PW_TYPE_INTEGER,
810           offsetof(listen_detail_t, load_factor), NULL, Stringify(10)},
811         { "poll_interval",   PW_TYPE_INTEGER,
812           offsetof(listen_detail_t, poll_interval), NULL, Stringify(1)},
813         { "retry_interval",   PW_TYPE_INTEGER,
814           offsetof(listen_detail_t, retry_interval), NULL, Stringify(30)},
815
816         { NULL, -1, 0, NULL, NULL }             /* end the list */
817 };
818
819
820 /*
821  *      Parse a detail section.
822  */
823 int detail_parse(CONF_SECTION *cs, rad_listen_t *this)
824 {
825         int             rcode;
826         listen_detail_t *data;
827         RADCLIENT       *client;
828         char buffer[2048];
829
830         if (!this->data) {
831                 this->data = rad_malloc(sizeof(*data));
832                 memset(this->data, 0, sizeof(*data));
833         }
834
835         data = this->data;
836
837         rcode = cf_section_parse(cs, data, detail_config);
838         if (rcode < 0) {
839                 cf_log_err(cf_sectiontoitem(cs), "Failed parsing listen section");
840                 return -1;
841         }
842
843         if (!data->filename) {
844                 cf_log_err(cf_sectiontoitem(cs), "No detail file specified in listen section");
845                 return -1;
846         }
847
848         if ((data->load_factor < 1) || (data->load_factor > 100)) {
849                 cf_log_err(cf_sectiontoitem(cs), "Load factor must be between 1 and 100");
850                 return -1;
851         }
852
853         if ((data->poll_interval < 1) || (data->poll_interval > 20)) {
854                 cf_log_err(cf_sectiontoitem(cs), "poll_interval must be between 1 and 20");
855                 return -1;
856         }
857
858         /*
859          *      If the filename is a glob, use "detail.work" as the
860          *      work file name.
861          */
862         if ((strchr(data->filename, '*') != NULL) ||
863             (strchr(data->filename, '[') != NULL)) {
864                 char *p;
865
866 #ifndef HAVE_GLOB_H
867                 radlog(L_INFO, "WARNING: Detail file \"%s\" appears to use file globbing, but it is not supported on this system.", data->filename);
868 #endif
869                 strlcpy(buffer, data->filename, sizeof(buffer));
870                 p = strrchr(buffer, FR_DIR_SEP);
871                 if (p) {
872                         p[1] = '\0';
873                 } else {
874                         buffer[0] = '\0';
875                 }
876                 strlcat(buffer, "detail.work",
877                         sizeof(buffer) - strlen(buffer));
878                         
879         } else {
880                 snprintf(buffer, sizeof(buffer), "%s.work", data->filename);
881         }
882
883         free(data->filename_work);
884         data->filename_work = strdup(buffer); /* FIXME: leaked */
885
886         data->vps = NULL;
887         data->fp = NULL;
888         data->state = STATE_UNOPENED;
889         data->delay_time = data->poll_interval * USEC;
890         data->signal = 1;
891
892         /*
893          *      Initialize the fake client.
894          */
895         client = &data->detail_client;
896         memset(client, 0, sizeof(*client));
897         client->ipaddr.af = AF_INET;
898         client->ipaddr.ipaddr.ip4addr.s_addr = INADDR_NONE;
899         client->prefix = 0;
900         client->longname = client->shortname = data->filename;
901         client->secret = client->shortname;
902         client->nastype = strdup("none");
903
904         return 0;
905 }
906 #endif