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