Update the GPL boilerplate with the new address of the FSF.
[freeradius.git] / src / main / listen.c
1 /*
2  * listen.c     Handle socket stuff
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 2005  The FreeRADIUS server project
21  * Copyright 2005  Alan DeKok <aland@ox.org>
22  */
23
24 #include <freeradius-devel/autoconf.h>
25
26 #include <stdlib.h>
27 #include <string.h>
28
29 #ifdef HAVE_NETINET_IN_H
30 #include <netinet/in.h>
31 #endif
32
33 #ifdef HAVE_ARPA_INET_H
34 #include <arpa/inet.h>
35 #endif
36
37 #include <sys/resource.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/socket.h>
41 #include <netdb.h>
42
43 #ifdef HAVE_NET_IF_H
44 #include <net/if.h>
45 #endif
46
47 #ifdef HAVE_SYS_STAT_H
48 #include <sys/stat.h>
49 #endif
50
51 #ifdef WITH_UDPFROMTO
52 #include <freeradius-devel/udpfromto.h>
53 #endif
54
55 #include <fcntl.h>
56
57 #include <freeradius-devel/radiusd.h>
58 #include <freeradius-devel/rad_assert.h>
59 #include <freeradius-devel/conffile.h>
60 #include <freeradius-devel/token.h>
61
62 #include <freeradius-devel/radius_snmp.h>
63 #include <freeradius-devel/request_list.h>
64
65 static time_t start_time = 0;
66
67 /*
68  *      FIXME: Delete this crap!
69  */
70 extern time_t time_now;
71
72 /*
73  *      We'll use this below.
74  */
75 typedef int (*rad_listen_parse_t)(const char *, int, const CONF_SECTION *, rad_listen_t *);
76 typedef void (*rad_listen_free_t)(rad_listen_t *);
77
78 typedef struct rad_listen_master_t {
79         rad_listen_parse_t      parse;
80         rad_listen_free_t       free;
81         rad_listen_recv_t       recv;
82         rad_listen_send_t       send;
83         rad_listen_update_t     update;
84         rad_listen_print_t      print;
85 } rad_listen_master_t;
86
87 typedef struct listen_socket_t {
88         /*
89          *      For normal sockets.
90          */
91         lrad_ipaddr_t   ipaddr;
92         int             port;
93         RADCLIENT_LIST  *clients;
94 } listen_socket_t;
95
96 typedef struct listen_detail_t {
97         const char      *detail;
98         VALUE_PAIR      *vps;
99         FILE            *fp;
100         int             state;
101         time_t          timestamp;
102         lrad_ipaddr_t   client_ip;
103         int             max_outstanding;
104         int             *outstanding;
105 } listen_detail_t;
106                                
107
108 /*
109  *      Find a per-socket client.
110  */
111 static RADCLIENT *client_listener_find(const rad_listen_t *listener,
112                                        const lrad_ipaddr_t *ipaddr)
113 {
114         const RADCLIENT_LIST *clients;
115
116         rad_assert(listener != NULL);
117         rad_assert(ipaddr != NULL);
118
119         rad_assert((listener->type == RAD_LISTEN_AUTH) ||
120                    (listener->type == RAD_LISTEN_ACCT));
121
122         clients = ((listen_socket_t *)listener->data)->clients;
123         if (!clients) clients = mainconfig.clients;
124
125         rad_assert(clients != NULL);
126         
127         return client_find(clients, ipaddr);
128 }
129
130 static int listen_bind(rad_listen_t *this);
131
132 /*
133  *      FIXME: have the detail reader use another config "exit when done",
134  *      so that it can be used as a one-off tool to update stuff.
135  */
136
137 /*
138  *      Process and reply to a server-status request.
139  *      Like rad_authenticate and rad_accounting this should
140  *      live in it's own file but it's so small we don't bother.
141  */
142 static int rad_status_server(REQUEST *request)
143 {
144         char            reply_msg[64];
145         time_t          t;
146         VALUE_PAIR      *vp;
147
148         /*
149          *      Reply with an ACK. We might want to add some more
150          *      interesting reply attributes, such as server uptime.
151          */
152         t = request->timestamp - start_time;
153         sprintf(reply_msg, "FreeRADIUS up %d day%s, %02d:%02d",
154                 (int)(t / 86400), (t / 86400) == 1 ? "" : "s",
155                 (int)((t / 3600) % 24), (int)(t / 60) % 60);
156         request->reply->code = PW_AUTHENTICATION_ACK;
157
158         vp = pairmake("Reply-Message", reply_msg, T_OP_SET);
159         pairadd(&request->reply->vps, vp); /* don't need to check if !vp */
160
161         return 0;
162 }
163
164 static int request_num_counter = 0;
165
166 /*
167  *      Check for dups, etc.  Common to Access-Request &&
168  *      Accounting-Request packets.
169  */
170 static int common_checks(rad_listen_t *listener,
171                          RADIUS_PACKET *packet, REQUEST **prequest,
172                          const RADCLIENT *client)
173 {
174         REQUEST *curreq;
175         char buffer[128];
176
177         rad_assert(listener->rl != NULL);
178
179         /*
180          *      If there is no existing request of id, code, etc.,
181          *      then we can return, and let it be processed.
182          */
183         if ((curreq = rl_find(listener->rl, packet)) == NULL) {
184                 /*
185                  *      Count the total number of requests, to see if
186                  *      there are too many.  If so, return with an
187                  *      error.
188                  */
189                 if (mainconfig.max_requests) {
190                         /*
191                          *      FIXME: This is now per-socket,
192                          *      when it should really be global
193                          *      to the server!
194                          */
195                         int request_count = rl_num_requests(listener->rl);
196
197                         /*
198                          *      This is a new request.  Let's see if
199                          *      it makes us go over our configured
200                          *      bounds.
201                          */
202                         if (request_count > mainconfig.max_requests) {
203                                 radlog(L_ERR, "Dropping request (%d is too many): "
204                                        "from client %s port %d - ID: %d", request_count,
205                                        client->shortname,
206                                        packet->src_port, packet->id);
207                                 radlog(L_INFO, "WARNING: Please check the %s file.\n"
208                                        "\tThe value for 'max_requests' is probably set too low.\n", mainconfig.radiusd_conf);
209                                 return 0;
210                         } /* else there were a small number of requests */
211                 } /* else there was no configured limit for requests */
212
213                 /*
214                  *      FIXME: Add checks for system load.  If the
215                  *      system is busy, start dropping requests...
216                  *
217                  *      We can probably keep some statistics
218                  *      ourselves...  if there are more requests
219                  *      coming in than we can handle, start dropping
220                  *      some.
221                  */
222
223         /*
224          *      The current request isn't finished, which
225          *      means that the NAS sent us a new packet, while
226          *      we are still processing the old request.
227          */
228         } else if (!curreq->finished) {
229                 /*
230                  *      If the authentication vectors are identical,
231                  *      then the NAS is re-transmitting it, trying to
232                  *      kick us into responding to the request.
233                  */
234                 if (memcmp(curreq->packet->vector, packet->vector,
235                            sizeof(packet->vector)) == 0) {
236                         RAD_SNMP_INC(rad_snmp.auth.total_dup_requests);
237
238                         /*
239                          *      It's not finished because the request
240                          *      was proxied, but there was no reply
241                          *      from the home server.
242                          *
243                          *      This code will never get hit for
244                          *      accounting packets, as they're always
245                          *      updated, and never re-transmitted.
246                          */
247                         if (curreq->proxy && !curreq->proxy_reply) {
248                                 DEBUG2("Sending duplicate proxied request to home server %s port %d - ID: %d",
249                                        inet_ntop(curreq->proxy->dst_ipaddr.af,
250                                                  &curreq->proxy->dst_ipaddr.ipaddr,
251                                                  buffer, sizeof(buffer)),                                              curreq->proxy->dst_port,
252                                        
253                                        curreq->proxy->id);
254                                 listener->send(curreq->proxy_listener, curreq);
255                                 return 0;
256                         } /* else the packet was not proxied */
257
258                         /*
259                          *      Someone's still working on it, so we
260                          *      ignore the duplicate request.
261                          */
262                         radlog(L_ERR, "Discarding duplicate request from "
263                                "client %s port %d - ID: %d due to unfinished request %d",
264                                client->shortname,
265                                packet->src_port, packet->id,
266                                curreq->number);
267                         return 0;
268                 } /* else the authentication vectors were different */
269
270                 /*
271                  *      The authentication vectors are different, so
272                  *      the NAS has given up on us, as we've taken too
273                  *      long to process the request.  This is a
274                  *      SERIOUS problem!
275                  */
276                 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
277
278                 radlog(L_ERR, "Dropping conflicting packet from "
279                        "client %s port %d - ID: %d due to unfinished request %d",
280                        client->shortname,
281                        packet->src_port, packet->id,
282                        curreq->number);
283                 return 0;
284                 
285                 /*
286                  *      The old request is finished.  We now check the
287                  *      authentication vectors.  If the client has sent us a
288                  *      request with identical code && ID, but different
289                  *      vector, then they MUST have gotten our response, so we
290                  *      can delete the original request, and process the new
291                  *      one.
292                  *
293                  *      If the vectors are the same, then it's a duplicate
294                  *      request, and we can send a duplicate reply.
295                  */
296         } else if (memcmp(curreq->packet->vector, packet->vector,
297                           sizeof(packet->vector)) == 0) {
298                 RAD_SNMP_INC(rad_snmp.auth.total_dup_requests);
299
300                 /*
301                  *      If the packet has been delayed, then silently
302                  *      send a response, and clear the delayed flag.
303                  *
304                  *      Note that this means if the NAS kicks us while
305                  *      we're delaying a reject, then the reject may
306                  *      be sent sooner than otherwise.
307                  *
308                  *      This COULD be construed as a bug.  Maybe what
309                  *      we want to do is to ignore the duplicate
310                  *      packet, and send the reject later.
311                  */
312                 if (curreq->options & RAD_REQUEST_OPTION_DELAYED_REJECT) {
313                         curreq->options &= ~RAD_REQUEST_OPTION_DELAYED_REJECT;
314                         rad_assert(curreq->listener == listener);
315                         listener->send(listener, curreq);
316                         return 0;
317                 }
318
319                 /*
320                  *      Maybe we've saved a reply packet.  If so,
321                  *      re-send it.  Otherwise, just complain.
322                  */
323                 if (curreq->reply->code != 0) {
324                         DEBUG2("Sending duplicate reply "
325                                "to client %s port %d - ID: %d",
326                                client->shortname,
327                                packet->src_port, packet->id);
328                         rad_assert(curreq->listener == listener);
329                         listener->send(listener, curreq);
330                         return 0;
331                 }
332
333                 /*
334                  *      Else we never sent a reply to the NAS,
335                  *      as we decided somehow we didn't like the request.
336                  *
337                  *      This shouldn't happen, in general...
338                  */
339                 DEBUG2("Discarding duplicate request from client %s port %d - ID: %d",
340                        client->shortname, packet->src_port, packet->id);
341                 return 0;
342         } /* else the vectors were different, so we discard the old request. */
343
344         /*
345          *      'packet' has the same source IP, source port, code,
346          *      and Id as 'curreq', but a different authentication
347          *      vector.  We can therefore delete 'curreq', as we were
348          *      only keeping it around to send out duplicate replies,
349          *      if the first reply got lost in the network.
350          */
351         if (curreq) {
352                 RADIUS_PACKET *reply = curreq->reply;
353
354                 rl_yank(listener->rl, curreq);
355                 rad_free(&curreq->packet);
356
357                 if (curreq->reply) {
358                         reply = curreq->reply;
359                         pairfree(&reply->vps);
360                         free(reply->data);
361                         memset(reply, 0, sizeof(*reply));
362                 }
363                 memset(curreq, 0, sizeof(*curreq));
364 #ifndef NDEBUG
365                 curreq->magic = REQUEST_MAGIC;
366 #endif
367                 curreq->timestamp = time(NULL);
368                 curreq->child_pid = NO_SUCH_CHILD_PID;
369                 curreq->options = RAD_REQUEST_OPTION_NONE;
370                 curreq->reply = reply;
371         } else {
372                 /*
373                  *      A unique per-request counter.
374                  */
375                 curreq = request_alloc(); /* never fails */
376
377                 if ((curreq->reply = rad_alloc(0)) == NULL) {
378                         radlog(L_ERR, "No memory");
379                         exit(1);
380                 }
381         }
382         curreq->listener = listener;
383         curreq->packet = packet;
384         curreq->packet->timestamp = curreq->timestamp;
385         curreq->number = request_num_counter++;
386         strNcpy(curreq->secret, client->secret, sizeof(curreq->secret));
387         
388         /*
389          *      Remember the request in the list.
390          */
391         if (!rl_add(listener->rl, curreq)) {
392                 radlog(L_ERR, "Failed to insert request %d in the list of live requests: discarding", curreq->number);
393                 request_free(&curreq);
394                 return 0;
395         }
396         
397         /*
398          *      ADD IN "server identifier" from "listen"
399          *      directive!
400          */
401         
402         /*
403          *      The request passes many of our sanity checks.
404          *      From here on in, if anything goes wrong, we
405          *      send a reject message, instead of dropping the
406          *      packet.
407          */
408
409         /*
410          *      Build the reply template from the request.
411          */
412
413         curreq->reply->sockfd = curreq->packet->sockfd;
414         curreq->reply->dst_ipaddr = curreq->packet->src_ipaddr;
415         curreq->reply->src_ipaddr = curreq->packet->dst_ipaddr;
416         curreq->reply->dst_port = curreq->packet->src_port;
417         curreq->reply->src_port = curreq->packet->dst_port;
418         curreq->reply->id = curreq->packet->id;
419         curreq->reply->code = 0; /* UNKNOWN code */
420         memcpy(curreq->reply->vector, curreq->packet->vector,
421                sizeof(curreq->reply->vector));
422         curreq->reply->vps = NULL;
423         curreq->reply->data = NULL;
424         curreq->reply->data_len = 0;
425
426         *prequest = curreq;
427         return 1;
428 }
429
430
431 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
432 {
433         size_t len;
434         listen_socket_t *sock = this->data;
435
436         if ((sock->ipaddr.af == AF_INET) &&
437             (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
438                 strcpy(buffer, "*");
439         } else {
440                 ip_ntoh(&sock->ipaddr, buffer, bufsize);
441         }
442
443         len = strlen(buffer);
444
445         return len + snprintf(buffer + len, bufsize - len, " port %d",
446                               sock->port);
447 }
448
449
450 /*
451  *      Parse an authentication or accounting socket.
452  */
453 static int common_socket_parse(const char *filename, int lineno,
454                              const CONF_SECTION *cs, rad_listen_t *this)
455 {
456         int             rcode;
457         int             listen_port;
458         lrad_ipaddr_t   ipaddr;
459         listen_socket_t *sock = this->data;
460         const char      *section_name = NULL;
461         CONF_SECTION    *client_cs;
462
463         /*
464          *      Try IPv4 first
465          */
466         ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
467         rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
468                               &ipaddr.ipaddr.ip4addr, NULL);
469         if (rcode < 0) return -1;
470         
471         if (rcode == 0) { /* successfully parsed IPv4 */
472                 ipaddr.af = AF_INET;
473                 
474         } else {        /* maybe IPv6? */
475                 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
476                                       &ipaddr.ipaddr.ip6addr, NULL);
477                 if (rcode < 0) return -1;
478                 
479                 if (rcode == 1) {
480                         radlog(L_ERR, "%s[%d]: No address specified in listen section",
481                                filename, lineno);
482                         return -1;
483                 }
484                 ipaddr.af = AF_INET6;
485         }
486         
487         rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
488                               &listen_port, "0");
489         if (rcode < 0) return -1;
490         
491         sock->ipaddr = ipaddr;
492         sock->port = listen_port;
493
494         /*
495          *      And bind it to the port.
496          */
497         if (listen_bind(this) < 0) {
498                 char buffer[128];
499                 radlog(L_CONS|L_ERR, "%s[%d]: Error binding to port for %s port %d",
500                        filename, cf_section_lineno(cs),
501                        ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
502                        sock->port);
503                 return -1;
504         }
505
506         /*
507          *      If we can bind to interfaces, do so,
508          *      else don't.
509          */
510         if (cf_pair_find(cs, "interface")) {
511 #ifndef SO_BINDTODEVICE
512                 radlog(L_CONS|L_ERR, "%s[%d]: System does not support binding to interfaces, delete this line from the configuration file.",
513                        filename, cf_section_lineno(cs));
514                 return -1;
515 #else
516                 const char *value;
517                 const CONF_PAIR *cp = cf_pair_find(cs, "interface");
518                 struct ifreq ifreq;
519
520                 rad_assert(cp != NULL);
521                 value = cf_pair_value(cp);
522                 rad_assert(value != NULL);
523                 
524                 strcpy(ifreq.ifr_name, value);
525         
526                 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
527                                (char *)&ifreq, sizeof(ifreq)) < 0) {
528                         radlog(L_CONS|L_ERR, "%s[%d]: Failed binding to interface %s: %s",
529                                filename, cf_section_lineno(cs),
530                                value, strerror(errno));
531                         return -1;
532                 } /* else it worked. */
533 #endif
534         }
535
536         /*
537          *      Look for the name of a section that holds a list
538          *      of clients.
539          */
540         rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
541                               &section_name, NULL);
542         if (rcode < 0) return -1; /* bad string */
543         if (rcode > 0) return 0; /* non-existent is OK. */
544
545         client_cs = cf_section_find(section_name);
546         if (!client_cs) {
547                 radlog(L_CONS|L_ERR, "%s[%d]: Failed to find client section %s",
548                        filename, cf_section_lineno(cs), section_name);
549                 return -1;
550         }
551
552         sock->clients = clients_parse_section(filename, client_cs);
553         if (!sock->clients) {
554                 return -1;
555         }
556
557         return 0;
558 }
559
560 /*
561  *      Send an authentication response packet
562  */
563 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
564 {
565         rad_assert(request->listener == listener);
566         rad_assert(listener->send == auth_socket_send);
567
568         /*
569          *      Ensure that the reply is sane
570          */
571         if (request->reply->code == 0) {
572                 DEBUG2("There was no response configured: rejecting request %d", request->number);
573                 request->reply->code = PW_AUTHENTICATION_REJECT;
574         }
575
576         /*
577          *      If we're delaying authentication rejects, then
578          *      mark the request as delayed, and do NOT send a
579          *      response right now.
580          *
581          *      However, if it's already marked as delayed, then
582          *      send it now.
583          */
584         if ((request->reply->code == PW_AUTHENTICATION_REJECT) &&
585             ((request->options & RAD_REQUEST_OPTION_DELAYED_REJECT) == 0) &&
586             (mainconfig.reject_delay > 0) &&
587             ((request->options & RAD_REQUEST_OPTION_FAKE_REQUEST) == 0)) {
588                 DEBUG2("Delaying request %d for %d seconds",
589                        request->number, mainconfig.reject_delay);
590                 request->options |= RAD_REQUEST_OPTION_DELAYED_REJECT;
591                 return 0;
592         }
593
594         return rad_send(request->reply, request->packet, request->secret);
595 }
596
597
598 /*
599  *      Send an accounting response packet (or not)
600  */
601 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
602 {
603         rad_assert(request->listener == listener);
604         rad_assert(listener->send == acct_socket_send);
605
606         /*
607          *      Accounting reject's are silently dropped.
608          *
609          *      We do it here to avoid polluting the rest of the
610          *      code with this knowledge
611          */
612         if (request->reply->code == 0) return 0;
613
614         return rad_send(request->reply, request->packet, request->secret);
615 }
616
617
618 /*
619  *      Send a packet to a home server.
620  *
621  *      FIXME: have different code for proxy auth & acct!
622  */
623 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
624 {
625         listen_socket_t *sock = listener->data;
626
627         rad_assert(request->proxy_listener == listener);
628         rad_assert(listener->send == proxy_socket_send);
629
630         request->proxy->src_ipaddr = sock->ipaddr;
631         request->proxy->src_port = sock->port;
632
633         return rad_send(request->proxy, request->packet, request->proxysecret);
634 }
635
636
637 /*
638  *      Check if an incoming request is "ok"
639  *
640  *      It takes packets, not requests.  It sees if the packet looks
641  *      OK.  If so, it does a number of sanity checks on it.
642   */
643 static int auth_socket_recv(rad_listen_t *listener,
644                             RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
645 {
646         RADIUS_PACKET   *packet;
647         RAD_REQUEST_FUNP fun = NULL;
648         char            buffer[128];
649         RADCLIENT       *client;
650
651         packet = rad_recv(listener->fd);
652         if (!packet) {
653                 radlog(L_ERR, "%s", librad_errstr);
654                 return 0;
655         }
656
657         RAD_SNMP_TYPE_INC(listener, total_requests); /* FIXME: auth specific */
658
659         if ((client = client_listener_find(listener,
660                                            &packet->src_ipaddr)) == NULL) {
661                 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
662                 
663                 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
664                        inet_ntop(packet->src_ipaddr.af,
665                                  &packet->src_ipaddr.ipaddr,
666                                  buffer, sizeof(buffer)),
667                        packet->src_port);
668                 rad_free(&packet);
669                 return 0;
670         }
671
672         /*
673          *      Some sanity checks, based on the packet code.
674          */
675         switch(packet->code) {
676         case PW_AUTHENTICATION_REQUEST:
677                 fun = rad_authenticate;
678                 break;
679                 
680         case PW_STATUS_SERVER:
681                 if (!mainconfig.status_server) {
682                         DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
683                         rad_free(&packet);
684                         return 0;
685                 }
686                 fun = rad_status_server;
687                 break;
688
689         default:
690                 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
691                 
692                 radlog(L_ERR, "Invalid packet code %d sent to authentication port from client %s port %d "
693                        "- ID %d : IGNORED",
694                        packet->code, client->shortname,
695                        packet->src_port, packet->id);
696                 rad_free(&packet);
697                 return 0;
698                 break;
699         } /* switch over packet types */
700         
701         if (!common_checks(listener, packet, prequest, client)) {
702                 rad_free(&packet);
703                 return 0;
704         }
705
706         *pfun = fun;
707         return 1;
708 }
709
710
711 /*
712  *      Receive packets from an accounting socket
713  */
714 static int acct_socket_recv(rad_listen_t *listener,
715         RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
716 {
717         RADIUS_PACKET   *packet;
718         RAD_REQUEST_FUNP fun = NULL;
719         char            buffer[128];
720         RADCLIENT       *client;
721         
722         packet = rad_recv(listener->fd);
723         if (!packet) {
724                 radlog(L_ERR, "%s", librad_errstr);
725                 return 0;
726         }
727         
728         RAD_SNMP_TYPE_INC(listener, total_requests); /* FIXME: acct-specific */
729
730         if ((client = client_listener_find(listener,
731                                            &packet->src_ipaddr)) == NULL) {
732                 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
733                 
734                 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
735                        inet_ntop(packet->src_ipaddr.af,
736                                  &packet->src_ipaddr.ipaddr,
737                                  buffer, sizeof(buffer)),
738                        packet->src_port);
739                 rad_free(&packet);
740                 return 0;
741         }
742
743         switch(packet->code) {
744         case PW_ACCOUNTING_REQUEST:
745                 fun = rad_accounting;
746                 break;
747                 
748         default:
749                 /*
750                  *      FIXME: Update MIB for packet types?
751                  */
752                 radlog(L_ERR, "Invalid packet code %d sent to a accounting port "
753                        "from client %s port %d - ID %d : IGNORED",
754                        packet->code, client->shortname,
755                        packet->src_port, packet->id);
756                 rad_free(&packet);
757                 return 0;
758         }
759
760         /*
761          *      FIXME: Accounting duplicates should be handled
762          *      differently than authentication duplicates.
763          */
764         if (!common_checks(listener, packet, prequest, client)) {
765                 rad_free(&packet);
766                 return 0;
767         }
768
769         *pfun = fun;
770         return 1;
771 }
772
773
774 /*
775  *      Recieve packets from a proxy socket.
776  */
777 static int proxy_socket_recv(rad_listen_t *listener,
778                               RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
779 {
780         REALM           *cl;
781         REQUEST         *oldreq;
782         RADIUS_PACKET   *packet;
783         RAD_REQUEST_FUNP fun = NULL;
784         char            buffer[128];
785         
786         packet = rad_recv(listener->fd);
787         if (!packet) {
788                 radlog(L_ERR, "%s", librad_errstr);
789                 return 0;
790         }
791
792         /*
793          *      Unsupported stuff
794          */
795         if (packet->src_ipaddr.af != AF_INET) {
796                 rad_assert("PROXY IPV6 NOT SUPPORTED" == NULL);
797         }
798         
799         /*
800          *      FIXME: Add support for home servers!
801          */
802         if ((cl = realm_findbyaddr(packet->src_ipaddr.ipaddr.ip4addr.s_addr,
803                                    packet->src_port)) == NULL) {
804                 radlog(L_ERR, "Ignoring request from unknown home server %s port %d",
805                        inet_ntop(packet->src_ipaddr.af,
806                                  &packet->src_ipaddr.ipaddr,
807                                  buffer, sizeof(buffer)),
808                                packet->src_port);
809                 rad_free(&packet);
810                 return 0;
811         }
812
813         /*
814          *      FIXME: Client MIB updates?
815          */
816         switch(packet->code) {
817         case PW_AUTHENTICATION_ACK:
818         case PW_ACCESS_CHALLENGE:
819         case PW_AUTHENTICATION_REJECT:
820                 fun = rad_authenticate;
821                 break;
822                 
823         case PW_ACCOUNTING_RESPONSE:
824                 fun = rad_accounting;
825                 break;
826                 
827         default:
828                 /*
829                  *      FIXME: Update MIB for packet types?
830                  */
831                 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
832                        "from home server %s port %d - ID %d : IGNORED",
833                        packet->code,
834                        ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
835                        packet->src_port, packet->id);
836                 rad_free(&packet);
837                 return 0;
838         }
839
840         /*
841          *      Find the original request in the request list
842          */
843         oldreq = rl_find_proxy(packet);
844
845         /*
846          *      If we haven't found the original request which was
847          *      sent, to get this reply.  Complain, and discard this
848          *      request, as there's no way for us to send it to a NAS.
849          */
850         if (!oldreq) {
851                 radlog(L_PROXY, "No outstanding request was found for proxy reply from home server %s port %d - ID %d",
852                        inet_ntop(packet->src_ipaddr.af,
853                                  &packet->src_ipaddr.ipaddr,
854                                  buffer, sizeof(buffer)),
855                        packet->src_port, packet->id);
856                 rad_free(&packet);
857                 return 0;
858         }
859
860         /*
861          *      The proxy reply has arrived too late, as the original
862          *      (old) request has timed out, been rejected, and marked
863          *      as finished.  The client has already received a
864          *      response, so there is nothing that can be done. Delete
865          *      the tardy reply from the home server, and return nothing.
866          */
867         if ((oldreq->reply->code != 0) ||
868             (oldreq->finished)) {
869                 radlog(L_ERR, "Reply from home server %s port %d  - ID: %d arrived too late for request %d. Try increasing 'retry_delay' or 'max_request_time'",
870                        inet_ntop(packet->src_ipaddr.af,
871                                  &packet->src_ipaddr.ipaddr,
872                                  buffer, sizeof(buffer)),
873                        packet->src_port, packet->id,
874                        oldreq->number);
875                 rad_free(&packet);
876                 return 0;
877         }
878
879         /*
880          *      If there is already a reply, maybe this one is a
881          *      duplicate?
882          */
883         if (oldreq->proxy_reply) {
884                 if (memcmp(oldreq->proxy_reply->vector,
885                            packet->vector,
886                            sizeof(oldreq->proxy_reply->vector)) == 0) {
887                         radlog(L_ERR, "Discarding duplicate reply from home server %s port %d  - ID: %d for request %d",
888                                inet_ntop(packet->src_ipaddr.af,
889                                          &packet->src_ipaddr.ipaddr,
890                                          buffer, sizeof(buffer)),
891                                packet->src_port, packet->id,
892                                oldreq->number);
893                 } else {
894                         /*
895                          *      ? The home server gave us a new proxy
896                          *      reply, which doesn't match the old
897                          *      one.  Delete it.
898                          */
899                         DEBUG2("Ignoring conflicting proxy reply");
900                 }
901
902                 /*
903                  *      We've already received a reply, so
904                  *      we discard this one, as we don't want
905                  *      to do duplicate work.
906                  */
907                 rad_free(&packet);
908                 return 0;
909         } /* else there wasn't a proxy reply yet, so we can process it */
910
911         /*
912          *       Refresh the old request, and update it with the proxy
913          *       reply.
914          *
915          *      ? Can we delete the proxy request here?  * Is there
916          *      any more need for it?
917          *
918          *      FIXME: we probably shouldn't be updating the time
919          *      stamp here.
920          */
921         oldreq->timestamp = time_now;
922         oldreq->proxy_reply = packet;
923
924         /*
925          *      FIXME: we should really verify the digest here,
926          *      before marking this packet as a valid response.
927          *
928          *      This is a security problem, I think...
929          */
930
931         /*
932          *      Now that we've verified the packet IS actually from
933          *      that home server, and not forged, we can go mark the
934          *      entries for this home server as active.
935          *
936          *      If we had done this check in the 'find realm by IP address'
937          *      function, then an attacker could force us to use a home
938          *      server which was inactive, by forging reply packets
939          *      which didn't match any request.  We would think that
940          *      the reply meant the home server was active, would
941          *      re-activate the realms, and THEN bounce the packet
942          *      as garbage.
943          */
944         for (cl = mainconfig.realms; cl != NULL; cl = cl->next) {
945                 if (oldreq->proxy_reply->src_ipaddr.af != cl->ipaddr.af) continue;
946                 if (cl->ipaddr.af != AF_INET) continue; /* FIXME */
947
948                 if (oldreq->proxy_reply->src_ipaddr.ipaddr.ip4addr.s_addr == cl->ipaddr.ipaddr.ip4addr.s_addr) {
949                         if (oldreq->proxy_reply->src_port == cl->auth_port) {
950                                 cl->active = TRUE;
951                                 cl->last_reply = oldreq->timestamp;
952                         } else if (oldreq->proxy_reply->src_port == cl->acct_port) {
953                                 cl->acct_active = TRUE;
954                                 cl->last_reply = oldreq->timestamp;
955                         }
956                 }
957         }
958
959         rad_assert(fun != NULL);
960         *pfun = fun;
961         *prequest = oldreq;
962
963         return 1;
964 }
965
966 #define STATE_UNOPENED  (0)
967 #define STATE_UNLOCKED  (1)
968 #define STATE_HEADER    (2)
969 #define STATE_READING   (3)
970 #define STATE_DONE      (4)
971 #define STATE_WAITING   (5)
972
973 /*
974  *      If we're limiting outstanding packets, then mark the response
975  *      as being sent.
976  */
977 static int detail_send(rad_listen_t *listener, REQUEST *request)
978 {
979         listen_detail_t *data = listener->data;
980
981         rad_assert(request->listener == listener);
982         rad_assert(listener->send == detail_send);
983
984         if (request->simul_max >= 0) {
985                 rad_assert(data->outstanding != NULL);
986                 rad_assert(request->simul_max < data->max_outstanding);
987
988                 data->outstanding[request->simul_max] = 0;
989         }
990
991         return 0;
992 }
993
994
995 /*
996  *      Open the detail file..
997  *
998  *      FIXME: create it, if it's not already there, so that the main
999  *      server select() will wake us up if there's anything to read.
1000  */
1001 static int detail_open(rad_listen_t *this)
1002 {
1003         struct stat st;
1004         char buffer[2048];
1005         listen_detail_t *data = this->data;
1006
1007         rad_assert(data->state == STATE_UNOPENED);
1008         snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
1009         
1010         /*
1011          *      FIXME: Have "one-shot" configuration, where it
1012          *      will read the detail file, and exit once it's
1013          *      done.
1014          *
1015          *      FIXME: Try harder to open the detail file.
1016          *      Maybe sleep for X usecs if it doesn't exist?
1017          */
1018
1019         /*
1020          *      Open detail.work first, so we don't lose
1021          *      accounting packets.  It's probably better to
1022          *      duplicate them than to lose them.
1023          *
1024          *      Note that we're not writing to the file, but
1025          *      we've got to open it for writing in order to
1026          *      establish the lock, to prevent rlm_detail from
1027          *      writing to it.
1028          */
1029         this->fd = open(buffer, O_RDWR);
1030         if (this->fd < 0) {
1031                 /*
1032                  *      Try reading the detail file.  If it
1033                  *      doesn't exist, we can't do anything.
1034                  *
1035                  *      Doing the stat will tell us if the file
1036                  *      exists, even if we don't have permissions
1037                  *      to read it.
1038                  */
1039                 if (stat(data->detail, &st) < 0) {
1040                         return 0;
1041                 }
1042                 
1043                 /*
1044                  *      Open it BEFORE we rename it, just to
1045                  *      be safe...
1046                  */
1047                 this->fd = open(data->detail, O_RDWR);
1048                 if (this->fd < 0) {
1049                         radlog(L_ERR, "Failed to open %s: %s",
1050                                data->detail, strerror(errno));
1051                         return 0;
1052                 }
1053                 
1054                 /*
1055                  *      Rename detail to detail.work
1056                  */
1057                 if (rename(data->detail, buffer) < 0) {
1058                         close(this->fd);
1059                         this->fd = -1;
1060                         return 0;
1061                 }
1062         } /* else detail.work existed, and we opened it */
1063         
1064         rad_assert(data->vps == NULL);
1065         
1066         rad_assert(data->fp == NULL);
1067         data->fp = fdopen(this->fd, "r");
1068         if (!data->fp) {
1069                 radlog(L_ERR, "Failed to re-open %s: %s",
1070                        data->detail, strerror(errno));
1071                 return 0;
1072         }
1073
1074         data->state = STATE_UNLOCKED;
1075
1076         data->client_ip.af = AF_UNSPEC;
1077         data->timestamp = 0;
1078         
1079         return 1;
1080 }
1081
1082 /*
1083  *      This is a bad hack, just so complaints have meaningful text.
1084  */
1085 static const RADCLIENT detail_client = {
1086         {               /* ipaddr */
1087                 AF_INET,
1088                 {{ INADDR_NONE }}
1089         },
1090         32,
1091         "<detail-file>",
1092         "secret",
1093         "UNKNOWN-CLIENT",
1094         "other",
1095         "",
1096         "",
1097         -1
1098 };
1099
1100 static int detail_recv(rad_listen_t *listener,
1101                        RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
1102 {
1103         int             free_slot = -1;
1104         char            key[256], value[1024];
1105         VALUE_PAIR      *vp, **tail;
1106         RADIUS_PACKET   *packet;
1107         char            buffer[2048];
1108         listen_detail_t *data = listener->data;
1109
1110         if (data->state == STATE_UNOPENED) {
1111                 rad_assert(listener->fd < 0);
1112                 if (!detail_open(listener)) return 0;
1113         }
1114         rad_assert(listener->fd >= 0);
1115
1116         /*
1117          *      Try to lock fd.  If we can't, return.  If we can,
1118          *      continue.  This means that the server doesn't block
1119          *      while waiting for the lock to open...
1120          */
1121         if (data->state == STATE_UNLOCKED) {
1122                 /*
1123                  *      Note that we do NOT block waiting for the
1124                  *      lock.  We've re-named the file above, so we've
1125                  *      already guaranteed that any *new* detail
1126                  *      writer will not be opening this file.  The
1127                  *      only purpose of the lock is to catch a race
1128                  *      condition where the execution "ping-pongs"
1129                  *      between radiusd & radrelay.
1130                  */
1131                 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
1132                         return 0;
1133                 }
1134                 /*
1135                  *      Look for the header
1136                  */
1137                 data->state = STATE_HEADER;
1138         }
1139
1140         /*
1141          *      If we keep track of the outstanding requests, do so
1142          *      here.  Note that to minimize potential work, we do
1143          *      so only once the file is opened & locked.
1144          */
1145         if (data->max_outstanding) {
1146                 int i;
1147
1148                 for (i = 0; i < data->max_outstanding; i++) {
1149                         if (!data->outstanding[i]) {
1150                                 free_slot = i;
1151                                 break;
1152                         }
1153                 }
1154
1155                 /*
1156                  *      All of the slots are full, don't read data.
1157                  */
1158                 if (free_slot < 0) return 0;
1159         }
1160
1161         /*
1162          *      Catch an out of memory condition which will most likely
1163          *      never be met.
1164          */
1165         if (data->state == STATE_DONE) goto alloc_packet;
1166
1167         /*
1168          *      If we're in another state, then it means that we read
1169          *      a partial packet, which is bad.
1170          */
1171         rad_assert(data->state == STATE_HEADER);
1172         rad_assert(data->vps == NULL);
1173
1174         /*
1175          *      We read the last packet, and returned it for
1176          *      processing.  We later come back here to shut
1177          *      everything down, and unlink the file.
1178          */
1179         if (feof(data->fp)) {
1180                 rad_assert(data->state == STATE_HEADER);
1181
1182                 /*
1183                  *      Don't unlink the file until we've received
1184                  *      all of the responses.
1185                  */
1186                 if (data->max_outstanding > 0) {
1187                         int i;
1188
1189                         for (i = 0; i < data->max_outstanding; i++) {
1190                                 /*
1191                                  *      FIXME: close the file?
1192                                  */
1193                                 if (data->outstanding[i]) {
1194                                         data->state = STATE_WAITING;
1195                                         return 0;
1196                                 }
1197                         }
1198                 }
1199
1200         cleanup:
1201                 rad_assert(data->vps == NULL);
1202
1203                 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
1204                 unlink(buffer);
1205                 fclose(data->fp); /* closes listener->fd */
1206                 data->fp = NULL;
1207                 listener->fd = -1;
1208                 data->state = STATE_UNOPENED;
1209
1210                 /*
1211                  *      Try to open "detail" again.  If we're on a
1212                  *      busy RADIUS server, odds are that it will
1213                  *      now exist.
1214                  */
1215                 detail_open(listener);
1216                 return 0;
1217         }
1218
1219         tail = &data->vps;
1220
1221         /*
1222          *      Fill the buffer...
1223          */
1224         while (fgets(buffer, sizeof(buffer), data->fp)) {
1225                 /*
1226                  *      No CR, die.
1227                  */
1228                 if (!strchr(buffer, '\n')) {
1229                         pairfree(&data->vps);
1230                         goto cleanup;
1231                 }
1232
1233                 /*
1234                  *      We've read a header, possibly packet contents,
1235                  *      and are now at the end of the packet.
1236                  */
1237                 if ((data->state == STATE_READING) &&
1238                     (buffer[0] == '\n')) {
1239                         data->state = STATE_DONE;
1240                         break;
1241                 }
1242
1243                 /*
1244                  *      Look for date/time header, and read VP's if
1245                  *      found.  If not, keep reading lines until we
1246                  *      find one.
1247                  */
1248                 if (data->state == STATE_HEADER) {
1249                         int y;
1250                         
1251                         if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
1252                                 data->state = STATE_READING;
1253                         }
1254                         continue;
1255                 }
1256
1257                 /*
1258                  *      We have a full "attribute = value" line.
1259                  *      If it doesn't look reasonable, skip it.
1260                  */
1261                 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
1262                         continue;
1263                 }
1264
1265                 /*
1266                  *      Skip non-protocol attributes.
1267                  */
1268                 if (!strcasecmp(key, "Request-Authenticator")) continue;
1269
1270                 /*
1271                  *      Set the original client IP address, based on
1272                  *      what's in the detail file.
1273                  *
1274                  *      Hmm... we don't set the server IP address.
1275                  *      or port.  Oh well.
1276                  */
1277                 if (!strcasecmp(key, "Client-IP-Address")) {
1278                         data->client_ip.af = AF_INET;
1279                         ip_hton(value, AF_INET, &data->client_ip);
1280                         continue;
1281                 }
1282
1283                 /*
1284                  *      The original time at which we received the
1285                  *      packet.  We need this to properly calculate
1286                  *      Acct-Delay-Time.
1287                  */
1288                 if (!strcasecmp(key, "Timestamp")) {
1289                         data->timestamp = atoi(value);
1290                         continue;
1291                 }
1292
1293                 /*
1294                  *      Read one VP.
1295                  *
1296                  *      FIXME: do we want to check for non-protocol
1297                  *      attributes like radsqlrelay does?
1298                  */
1299                 vp = NULL;
1300                 if ((userparse(buffer, &vp) > 0) &&
1301                     (vp != NULL)) {
1302                         *tail = vp;
1303                         tail = &(vp->next);
1304                 }               
1305         }
1306
1307         /*
1308          *      We got to EOF,  If we're in STATE_HEADER, it's OK.
1309          *      Otherwise it's a problem.  In any case, nuke the file
1310          *      and start over from scratch,
1311          */
1312         if (feof(data->fp)) {
1313                 goto cleanup;
1314         }
1315
1316         /*
1317          *      FIXME: Do load management.
1318          */
1319
1320         /*
1321          *      If we're not done, then there's a problem.  The checks
1322          *      above for EOF
1323          */
1324         rad_assert(data->state == STATE_DONE);
1325
1326         /*
1327          *      The packet we read was empty, re-set the state to look
1328          *      for a header, and don't return anything.
1329          */
1330         if (!data->vps) {
1331                 data->state = STATE_HEADER;
1332                 return 0;
1333         }
1334
1335         /*
1336          *      Allocate the packet.  If we fail, it's a serious
1337          *      problem.
1338          */
1339  alloc_packet:
1340         packet = rad_alloc(1);
1341         if (!packet) {
1342                 return 0;       /* maybe memory will magically free up... */
1343         }
1344
1345         memset(packet, 0, sizeof(*packet));
1346         packet->sockfd = -1;
1347         packet->src_ipaddr.af = AF_INET;
1348         packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1349         packet->code = PW_ACCOUNTING_REQUEST;
1350         packet->timestamp = time(NULL);
1351
1352         /*
1353          *      Look for Acct-Delay-Time, and update
1354          *      based on Acct-Delay-Time += (time(NULL) - timestamp)
1355          */
1356         vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
1357         if (!vp) {
1358                 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
1359                 rad_assert(vp != NULL);
1360         }
1361         if (data->timestamp != 0) {
1362                 vp->lvalue += time(NULL) - data->timestamp;
1363         }
1364
1365         /*
1366          *      Remember where it came from, so that we don't
1367          *      proxy it to the place it came from...
1368          */
1369         if (data->client_ip.af != AF_UNSPEC) {
1370                 packet->src_ipaddr = data->client_ip;
1371         }
1372
1373         /*
1374          *      We've got to give SOME value for Id & ports, so that
1375          *      the packets can be added to the request queue.
1376          *      However, we don't want to keep track of used/unused
1377          *      id's and ports, as that's a lot of work.  This hack
1378          *      ensures that (if we have real random numbers), that
1379          *      there will be a collision on every (2^(16+16+2+24))/2
1380          *      packets, on average.  That means we can read 2^32 (4G)
1381          *      packets before having a collision, which means it's
1382          *      effectively impossible.  Having 4G packets currently
1383          *      being process is ridiculous.
1384          */
1385         packet->id = lrad_rand() & 0xff;
1386         packet->src_port = lrad_rand() & 0xffff;
1387         packet->dst_port = lrad_rand() & 0xffff;
1388
1389         packet->dst_ipaddr.af = AF_INET;
1390         packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1391
1392         packet->vps = data->vps;
1393
1394         /*
1395          *      Re-set the state.
1396          */
1397         data->vps = NULL;
1398         data->state = STATE_HEADER;
1399
1400         /*
1401          *      FIXME: many of these checks may not be necessary...
1402          */
1403         if (!common_checks(listener, packet, prequest, &detail_client)) {
1404                 rad_free(&packet);
1405                 return 0;
1406         }
1407
1408         /*
1409          *      Keep track of free slots, as a hack, in an otherwise
1410          *      unused 'int'
1411          */
1412         (*prequest)->simul_max = free_slot;
1413         if (free_slot) data->outstanding[free_slot] = 1;
1414
1415         *pfun = rad_accounting;
1416
1417         if (debug_flag) {
1418                 printf("detail_recv: Read packet from %s\n", data->detail);
1419                 for (vp = packet->vps; vp; vp = vp->next) {
1420                         putchar('\t');
1421                         vp_print(stdout, vp);
1422                         putchar('\n');
1423                 }
1424         }
1425
1426         return 1;
1427 }
1428
1429
1430 /*
1431  *      Free detail-specific stuff.
1432  */
1433 static void detail_free(rad_listen_t *this)
1434 {
1435         listen_detail_t *data = this->data;
1436
1437         free(data->detail);
1438         pairfree(&data->vps);
1439         free(data->outstanding);
1440
1441         if (data->fp != NULL) fclose(data->fp);
1442 }
1443
1444
1445 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1446 {
1447         return snprintf(buffer, bufsize, "%s",
1448                         ((listen_detail_t *)(this->data))->detail);
1449 }
1450
1451
1452 static const CONF_PARSER detail_config[] = {
1453         { "detail",   PW_TYPE_STRING_PTR,
1454           offsetof(listen_detail_t, detail), NULL,  NULL },
1455         { "max_outstanding",  PW_TYPE_INTEGER,
1456            offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1457
1458         { NULL, -1, 0, NULL, NULL }             /* end the list */
1459 };
1460
1461
1462 /*
1463  *      Parse a detail section.
1464  */
1465 static int detail_parse(const char *filename, int lineno,
1466                         const CONF_SECTION *cs, rad_listen_t *this)
1467 {
1468         int             rcode;
1469         listen_detail_t *data;
1470
1471         data = this->data;
1472
1473         rcode = cf_section_parse(cs, data, detail_config);
1474         if (rcode < 0) {
1475                 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1476                        filename, lineno);
1477                 return -1;
1478         }
1479
1480         if (!data->detail) {
1481                 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1482                        filename, lineno);
1483                 return -1;
1484         }
1485         
1486         data->vps = NULL;
1487         data->fp = NULL;
1488         data->state = STATE_UNOPENED;
1489
1490         if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1491
1492         if (data->max_outstanding > 0) {
1493                 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1494         }
1495         
1496         detail_open(this);
1497
1498         return 0;
1499 }
1500
1501
1502 /*
1503  *      See radiusd.c & request_list.c
1504  */
1505 #define SLEEP_FOREVER (65536)
1506 /*
1507  *      A generic "update the request list once a second" function.
1508  */
1509 static int generic_update(rad_listen_t *this, time_t now)
1510 {
1511         if (!this->rl) return SLEEP_FOREVER;
1512
1513         return rl_clean_list(this->rl, now);
1514 }
1515
1516
1517
1518 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1519         { NULL, NULL, NULL, NULL, NULL, NULL},  /* RAD_LISTEN_NONE */
1520
1521         /* authentication */
1522         { common_socket_parse, NULL,
1523           auth_socket_recv, auth_socket_send,
1524           generic_update, socket_print },
1525
1526         /* accounting */
1527         { common_socket_parse, NULL,
1528           acct_socket_recv, acct_socket_send,
1529           generic_update, socket_print},
1530
1531         /* proxying */
1532         { NULL, NULL,
1533           proxy_socket_recv, proxy_socket_send,
1534           generic_update, socket_print }, /* FIXME: update func is wrong! */
1535
1536         /* detail */
1537         { detail_parse, detail_free,
1538           detail_recv, detail_send,
1539           generic_update, detail_print }
1540 };
1541
1542
1543 /*
1544  *      Binds a listener to a socket.
1545  */
1546 static int listen_bind(rad_listen_t *this)
1547 {
1548         struct sockaddr salocal;
1549         socklen_t       salen;
1550         rad_listen_t    **last;
1551         listen_socket_t *sock = this->data;
1552
1553         /*
1554          *      If the port is zero, then it means the appropriate
1555          *      thing from /etc/services.
1556          */
1557         if (sock->port == 0) {
1558                 struct servent  *svp;
1559
1560                 switch (this->type) {
1561                 case RAD_LISTEN_AUTH:
1562                         svp = getservbyname ("radius", "udp");
1563                         if (svp != NULL) {
1564                                 sock->port = ntohs(svp->s_port);
1565                         } else {
1566                                 sock->port = PW_AUTH_UDP_PORT;
1567                         }
1568                         break;
1569
1570                 case RAD_LISTEN_ACCT:
1571                         svp = getservbyname ("radacct", "udp");
1572                         if (svp != NULL) {
1573                                 sock->port = ntohs(svp->s_port);
1574                         } else {
1575                                 sock->port = PW_ACCT_UDP_PORT;
1576                         }
1577                         break;
1578
1579                 default:
1580                         radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1581                         return -1;
1582                 }
1583         }
1584
1585         /*
1586          *      Find it in the old list, AFTER updating the port.  If
1587          *      it's there, use that, rather than creating a new
1588          *      socket.  This allows HUP's to re-use the old sockets,
1589          *      which means that packets waiting in the socket queue
1590          *      don't get lost.
1591          */
1592         for (last = &mainconfig.listen;
1593              *last != NULL;
1594              last = &((*last)->next)) {
1595                 if ((this->type == (*last)->type) &&
1596                     (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1597                     (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1598                         int equal;
1599
1600                         if (sock->ipaddr.af == AF_INET) {
1601                                 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1602                         } else if (sock->ipaddr.af == AF_INET6) {
1603                                 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1604                         } else {
1605                                 equal = 0;
1606                         }
1607                         
1608                         if (equal) {
1609                                 this->rl = (*last)->rl;
1610                                 this->fd = (*last)->fd;
1611                                 (*last)->fd = -1;
1612                                 (*last)->rl = NULL;
1613                                 return 0;
1614                         }
1615                 }
1616         }
1617
1618         /*
1619          *      Create the socket.
1620          */
1621         this->fd = socket(sock->ipaddr.af, SOCK_DGRAM, 0);
1622         if (this->fd < 0) {
1623                 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1624                        strerror(errno));
1625                 return -1;
1626         }
1627         
1628
1629 #ifdef WITH_UDPFROMTO
1630         /*
1631          *      Initialize udpfromto for all sockets.
1632          */
1633         if (udpfromto_init(this->fd) != 0) {
1634                 radlog(L_ERR|L_CONS, "ERROR: udpfromto init failed.");
1635         }
1636 #endif
1637
1638         if (sock->ipaddr.af == AF_INET) {
1639                 struct sockaddr_in *sa;
1640
1641                 sa = (struct sockaddr_in *) &salocal;
1642                 memset(sa, 0, sizeof(salocal));
1643                 sa->sin_family = AF_INET;
1644                 sa->sin_addr = sock->ipaddr.ipaddr.ip4addr;
1645                 sa->sin_port = htons(sock->port);
1646                 salen = sizeof(*sa);
1647
1648 #ifdef HAVE_STRUCT_SOCKADDR_IN6
1649         } else if (sock->ipaddr.af == AF_INET6) {
1650                 struct sockaddr_in6 *sa;
1651
1652                 sa = (struct sockaddr_in6 *) &salocal;
1653                 memset(sa, 0, sizeof(salocal));
1654                 sa->sin6_family = AF_INET6;
1655                 sa->sin6_addr = sock->ipaddr.ipaddr.ip6addr;
1656                 sa->sin6_port = htons(sock->port);
1657                 salen = sizeof(*sa);
1658
1659                 /*
1660                  *      Listening on '::' does NOT get you IPv4 to
1661                  *      IPv6 mapping.  You've got to listen on an IPv4
1662                  *      address, too.  This makes the rest of the server
1663                  *      design a little simpler.
1664                  */
1665 #ifdef IPV6_V6ONLY
1666                 if (IN6_IS_ADDR_UNSPECIFIED(&sock->ipaddr.ipaddr.ip6addr)) {
1667                         int on = 1;
1668                         
1669                         setsockopt(this->fd, IPPROTO_IPV6, IPV6_V6ONLY,
1670                                    (char *)&on, sizeof(on));
1671                 }
1672 #endif /* IPV6_V6ONLY */
1673 #endif /* HAVE_STRUCT_SOCKADDR_IN6 */
1674         } else {
1675                 radlog(L_ERR|L_CONS, "ERROR: Unsupported protocol family %d",
1676                        sock->ipaddr.af);
1677                 close(this->fd);
1678                 this->fd = -1;
1679                 return -1;
1680         }
1681
1682         if (bind(this->fd, &salocal, salen) < 0) {
1683                 char buffer[128];
1684
1685                 radlog(L_ERR|L_CONS, "ERROR: Bind to %s port %d failed: %s",
1686                        inet_ntop(sock->ipaddr.af, &sock->ipaddr.ipaddr,
1687                                  buffer, sizeof(buffer)),
1688                        sock->port, strerror(errno));
1689                                  
1690                 close(this->fd);
1691                 this->fd = -1;
1692                 return -1;
1693         }
1694
1695         return 0;
1696 }
1697
1698
1699 /*
1700  *      Allocate & initialize a new listener.
1701  */
1702 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1703 {
1704         rad_listen_t *this;
1705
1706         this = rad_malloc(sizeof(*this));
1707         memset(this, 0, sizeof(*this));
1708
1709         this->type = type;
1710         this->recv = master_listen[this->type].recv;
1711         this->send = master_listen[this->type].send;
1712         this->update = master_listen[this->type].update;
1713         this->print = master_listen[this->type].print;
1714
1715         switch (type) {
1716         case RAD_LISTEN_AUTH:
1717         case RAD_LISTEN_ACCT:
1718         case RAD_LISTEN_PROXY:
1719                 this->data = rad_malloc(sizeof(listen_socket_t));
1720                 memset(this->data, 0, sizeof(listen_socket_t));
1721                 break;
1722
1723         case RAD_LISTEN_DETAIL:
1724                 this->data = rad_malloc(sizeof(listen_detail_t));
1725                 memset(this->data, 0, sizeof(listen_detail_t));
1726                 
1727         default:
1728                 break;
1729         }
1730
1731         return this;
1732 }
1733
1734
1735 /*
1736  *      Externally visible function for creating a new proxy LISTENER.
1737  *
1738  *      For now, don't take ipaddr or port.
1739  *
1740  *      Not thread-safe, but all calls to it are protected by the
1741  *      proxy mutex in request_list.c
1742  */
1743 rad_listen_t *proxy_new_listener()
1744 {
1745         int last_proxy_port, port;
1746         rad_listen_t *this, *tmp, **last;
1747         listen_socket_t *sock, *old;
1748
1749         this = listen_alloc(RAD_LISTEN_PROXY);
1750
1751         /*
1752          *      Find an existing proxy socket to copy.
1753          *
1754          *      FIXME: Make it per-realm, or per-home server!
1755          */
1756         last_proxy_port = 0;
1757         old = NULL;
1758         last = &mainconfig.listen;
1759         for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1760                 if (tmp->type == RAD_LISTEN_PROXY) {
1761                         sock = tmp->data;
1762                         if (sock->port > last_proxy_port) {
1763                                 last_proxy_port = sock->port + 1;
1764                         }
1765                         if (!old) old = sock;
1766                 }
1767
1768                 last = &(tmp->next);
1769         }
1770
1771         if (!old) return NULL;  /* This is a serious error. */
1772
1773         /*
1774          *      FIXME: find a new IP address to listen on?
1775          */
1776         sock = this->data;
1777         memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1778
1779         /*
1780          *      Keep going until we find an unused port.
1781          */
1782         for (port = last_proxy_port; port < 64000; port++) {
1783                 sock->port = port;
1784                 if (listen_bind(this) == 0) {
1785                         /*
1786                          *      Add the new listener to the list of
1787                          *      listeners.
1788                          */
1789                         *last = this;
1790                         return this;
1791                 }
1792         }
1793
1794         return NULL;
1795 }
1796
1797
1798 static const LRAD_NAME_NUMBER listen_compare[] = {
1799         { "auth",       RAD_LISTEN_AUTH },
1800         { "acct",       RAD_LISTEN_ACCT },
1801         { "detail",     RAD_LISTEN_DETAIL },
1802         { NULL, 0 },
1803 };
1804
1805
1806 /*
1807  *      Generate a list of listeners.  Takes an input list of
1808  *      listeners, too, so we don't close sockets with waiting packets.
1809  */
1810 int listen_init(const char *filename, rad_listen_t **head)
1811 {
1812         int             rcode;
1813         CONF_SECTION    *cs;
1814         rad_listen_t    **last;
1815         rad_listen_t    *this;
1816         lrad_ipaddr_t   server_ipaddr;
1817         int             auth_port = 0;
1818
1819         /*
1820          *      We shouldn't be called with a pre-existing list.
1821          */
1822         rad_assert(head && (*head == NULL));
1823         
1824         if (start_time != 0) start_time = time(NULL);
1825
1826         last = head;
1827         server_ipaddr.af = AF_UNSPEC;
1828
1829         /*
1830          *      If the port is specified on the command-line,
1831          *      it over-rides the configuration file.
1832          */
1833         if (mainconfig.port >= 0) {
1834                 auth_port = mainconfig.port;
1835         } else {
1836                 rcode = cf_item_parse(mainconfig.config, "port",
1837                                       PW_TYPE_INTEGER, &auth_port,
1838                                       Stringify(PW_AUTH_UDP_PORT));
1839                 if (rcode < 0) return -1; /* error parsing it */
1840
1841                 if (rcode == 0)
1842                         radlog(L_INFO, "WARNING: The directive 'port' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1843         }
1844
1845         /*
1846          *      If the IP address was configured on the command-line,
1847          *      use that as the "bind_address"
1848          */
1849         if (mainconfig.myip.af != AF_UNSPEC) {
1850                 memcpy(&server_ipaddr, &mainconfig.myip,
1851                        sizeof(server_ipaddr));
1852                 goto bind_it;
1853         }
1854
1855         /*
1856          *      Else look for bind_address and/or listen sections.
1857          */
1858         server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1859         rcode = cf_item_parse(mainconfig.config, "bind_address",
1860                               PW_TYPE_IPADDR,
1861                               &server_ipaddr.ipaddr.ip4addr, NULL);
1862         if (rcode < 0) return -1; /* error parsing it */
1863         
1864         if (rcode == 0) { /* successfully parsed IPv4 */
1865                 listen_socket_t *sock;
1866                 server_ipaddr.af = AF_INET;
1867
1868                 radlog(L_INFO, "WARNING: The directive 'bind_adress' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1869
1870         bind_it:
1871                 this = listen_alloc(RAD_LISTEN_AUTH);
1872                 sock = this->data;
1873
1874                 sock->ipaddr = server_ipaddr;
1875                 sock->port = auth_port;
1876                 
1877                 if (listen_bind(this) < 0) {
1878                         listen_free(&this);
1879                         listen_free(head);
1880                         radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1881                         return -1;
1882                 }
1883                 auth_port = sock->port; /* may have been updated in listen_bind */
1884                 *last = this;
1885                 last = &(this->next);
1886                 
1887                 /*
1888                  *      Open Accounting Socket.
1889                  *
1890                  *      If we haven't already gotten acct_port from
1891                  *      /etc/services, then make it auth_port + 1.
1892                  */
1893                 this = listen_alloc(RAD_LISTEN_ACCT);
1894                 sock = this->data;
1895                 
1896                 /*
1897                  *      Create the accounting socket.
1898                  *
1899                  *      The accounting port is always the
1900                  *      authentication port + 1
1901                  */
1902                 sock->ipaddr = server_ipaddr;
1903                 sock->port = auth_port + 1;
1904                 
1905                 if (listen_bind(this) < 0) {
1906                         listen_free(&this);
1907                         listen_free(head);
1908                         radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1909                         return -1;
1910                 }
1911
1912                 *last = this;
1913                 last = &(this->next);
1914
1915         } else if (mainconfig.port > 0) { /* no bind address, but a port */
1916                 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1917                        mainconfig.port);
1918                 return -1;
1919         }
1920
1921         /*
1922          *      They specified an IP on the command-line, ignore
1923          *      all listen sections.
1924          */
1925         if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1926
1927         /*
1928          *      Walk through the "listen" sections, if they exist.
1929          */
1930         for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1931              cs != NULL;
1932              cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1933                 int             type;
1934                 char            *listen_type, *identity;
1935                 int             lineno = cf_section_lineno(cs);
1936
1937                 listen_type = identity = NULL;
1938                 
1939                 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1940                                       &listen_type, "");
1941                 if (rcode < 0) return -1;
1942                 if (rcode == 1) {
1943                         listen_free(head);
1944                         free(listen_type);
1945                         radlog(L_ERR, "%s[%d]: No type specified in listen section",
1946                                filename, lineno);
1947                         return -1;
1948                 }
1949
1950                 /*
1951                  *      See if there's an identity.
1952                  */
1953                 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1954                                       &identity, NULL);
1955                 if (rcode < 0) {
1956                         listen_free(head);
1957                         free(identity);
1958                         return -1;
1959                 }
1960
1961                 type = lrad_str2int(listen_compare, listen_type,
1962                                     RAD_LISTEN_NONE);
1963                 free(listen_type);
1964                 if (type == RAD_LISTEN_NONE) {
1965                         listen_free(head);
1966                         radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1967                                filename, lineno);
1968                         return -1;
1969                 }
1970
1971                 /*
1972                  *      Set up cross-type data.
1973                  */
1974                 this = listen_alloc(type);
1975                 this->identity = identity;
1976                 this->fd = -1;
1977                 
1978                 /*
1979                  *      Call per-type parser.
1980                  */
1981                 if (master_listen[type].parse(filename, lineno,
1982                                               cs, this) < 0) {
1983                         listen_free(&this);
1984                         listen_free(head);
1985                         return -1;
1986                 }
1987
1988                 *last = this;
1989                 last = &(this->next);   
1990         }
1991
1992         /*
1993          *      If we're proxying requests, open the proxy FD.
1994          *      Otherwise, don't do anything.
1995          */
1996  do_proxy:
1997         if (mainconfig.proxy_requests == TRUE) {
1998                 int             port = -1;
1999                 listen_socket_t *sock = NULL;
2000
2001                 /*
2002                  *      No sockets to receive packets, therefore
2003                  *      proxying is pointless.
2004                  */
2005                 if (!*head) return -1;
2006
2007                 /*
2008                  *      If we previously had proxy sockets, copy them
2009                  *      to the new config.
2010                  */
2011                 if (mainconfig.listen != NULL) {
2012                         rad_listen_t *old, *next, **tail;
2013
2014                         tail = &mainconfig.listen;
2015                         for (old = mainconfig.listen;
2016                              old != NULL;
2017                              old = next) {
2018                                 next = old->next;
2019
2020                                 if (old->type != RAD_LISTEN_PROXY) {
2021                                         tail = &((*tail)->next);
2022                                         continue;
2023                                 }
2024
2025                                 *last = old;
2026                                 *tail = next;
2027                                 old->next = NULL;
2028                                 last = &(old->next);
2029                         }
2030
2031                         goto done;
2032                 }
2033
2034                 /*
2035                  *      Find the first authentication port,
2036                  *      and use it
2037                  */
2038                 for (this = *head; this != NULL; this = this->next) {
2039                         if (this->type == RAD_LISTEN_AUTH) {
2040                                 sock = this->data;
2041                                 if (server_ipaddr.af == AF_UNSPEC) {
2042                                         server_ipaddr = sock->ipaddr;
2043                                 }
2044                                 port = sock->port + 2; /* skip acct port */
2045                                 break;
2046                         }
2047                 }
2048                 rad_assert(port > 0); /* must have found at least one entry! */
2049
2050                 /*
2051                  *      Address is still unspecified, use IPv4.
2052                  */
2053                 if (server_ipaddr.af == AF_UNSPEC) {
2054                         server_ipaddr.af = AF_INET;
2055                         server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
2056                 }
2057
2058                 this = listen_alloc(RAD_LISTEN_PROXY);
2059                 sock = this->data;
2060
2061                 /*
2062                  *      Create the first proxy socket.
2063                  */
2064                 sock->ipaddr = server_ipaddr;
2065
2066                 /*
2067                  *      Try to find a proxy port (value doesn't matter)
2068                  */
2069                 for (sock->port = port;
2070                      sock->port < 64000;
2071                      sock->port++) {
2072                         if (listen_bind(this) == 0) {
2073                                 *last = this;
2074                                 last = &(this->next); /* just in case */
2075                                 break;
2076                         }
2077                 }
2078
2079                 if (sock->port >= 64000) {
2080                         listen_free(head);
2081                         listen_free(&this);
2082                         radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
2083                         return -1;
2084                 }
2085         }
2086
2087         /*
2088          *      Sanity check the configuration.
2089          */
2090         rcode = 0;
2091         for (this = *head; this != NULL; this = this->next) {
2092                 if ((this->type != RAD_LISTEN_PROXY) &&
2093                     !this->rl) {
2094                         /*
2095                          *      FIXME: Pass type to rl_init, so that
2096                          *      it knows how to deal with accounting
2097                          *      packets.  i.e. it caches them, but
2098                          *      doesn't bother trying to re-transmit.
2099                          */
2100                         this->rl = rl_init();
2101                         if (!this->rl) {
2102                                 rad_assert(0 == 1); /* FIXME: */
2103                         }
2104                 }
2105
2106                 if (((this->type == RAD_LISTEN_ACCT) &&
2107                      (rcode == RAD_LISTEN_DETAIL)) ||
2108                     ((this->type == RAD_LISTEN_DETAIL) &&
2109                      (rcode == RAD_LISTEN_ACCT))) {
2110                         rad_assert(0 == 1); /* FIXME: configuration error */
2111                 }
2112
2113                 if (rcode != 0) continue;
2114
2115                 if ((this->type == RAD_LISTEN_ACCT) ||
2116                     (this->type == RAD_LISTEN_DETAIL)) {
2117                         rcode = this->type;
2118                 }
2119         }
2120
2121  done:
2122         return 0;
2123 }
2124
2125 /*
2126  *      Free a linked list of listeners;
2127  */
2128 void listen_free(rad_listen_t **head)
2129 {
2130         rad_listen_t *this;
2131
2132         if (!head || !*head) return;
2133
2134         this = *head;
2135         while (this) {
2136                 rad_listen_t *next = this->next;
2137                 
2138                 free(this->identity);
2139
2140                 rl_deinit(this->rl);
2141
2142                 /*
2143                  *      Other code may have eaten the FD.
2144                  */
2145                 if (this->fd >= 0) close(this->fd);
2146
2147                 if (master_listen[this->type].free) {
2148                         master_listen[this->type].free(this);
2149                 }
2150                 free(this->data);
2151                 free(this);
2152                 
2153                 this = next;
2154         }
2155
2156         *head = NULL;
2157 }