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