2 * listen.c Handle socket stuff
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.
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.
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
20 * Copyright 2005,2006 The FreeRADIUS server project
21 * Copyright 2005 Alan DeKok <aland@ox.org>
24 #include <freeradius-devel/ident.h>
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>
33 #include <freeradius-devel/vmps.h>
34 #include <freeradius-devel/detail.h>
36 #ifdef HAVE_SYS_RESOURCE_H
37 #include <sys/resource.h>
50 * We'll use this below.
52 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
53 typedef void (*rad_listen_free_t)(rad_listen_t *);
55 typedef struct rad_listen_master_t {
56 rad_listen_parse_t parse;
57 rad_listen_free_t free;
58 rad_listen_recv_t recv;
59 rad_listen_send_t send;
60 rad_listen_print_t print;
61 rad_listen_encode_t encode;
62 rad_listen_decode_t decode;
63 } rad_listen_master_t;
65 typedef struct listen_socket_t {
71 RADCLIENT_LIST *clients;
75 * Find a per-socket client.
77 RADCLIENT *client_listener_find(const rad_listen_t *listener,
78 const fr_ipaddr_t *ipaddr)
80 const RADCLIENT_LIST *clients;
82 rad_assert(listener != NULL);
83 rad_assert(ipaddr != NULL);
85 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
86 (listener->type == RAD_LISTEN_ACCT) ||
87 (listener->type == RAD_LISTEN_VQP));
89 clients = ((listen_socket_t *)listener->data)->clients;
92 * This HAS to have been initialized previously.
94 rad_assert(clients != NULL);
96 return client_find(clients, ipaddr);
99 static int listen_bind(rad_listen_t *this);
103 * Process and reply to a server-status request.
104 * Like rad_authenticate and rad_accounting this should
105 * live in it's own file but it's so small we don't bother.
107 static int rad_status_server(REQUEST *request)
109 int rcode = RLM_MODULE_OK;
112 switch (request->listener->type) {
113 case RAD_LISTEN_AUTH:
114 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
116 rcode = module_authorize(dval->value, request);
118 rcode = RLM_MODULE_OK;
123 case RLM_MODULE_UPDATED:
124 request->reply->code = PW_AUTHENTICATION_ACK;
127 case RLM_MODULE_FAIL:
128 case RLM_MODULE_HANDLED:
129 request->reply->code = 0; /* don't reply */
133 case RLM_MODULE_REJECT:
134 request->reply->code = PW_AUTHENTICATION_REJECT;
139 case RAD_LISTEN_ACCT:
140 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
142 rcode = module_accounting(dval->value, request);
144 rcode = RLM_MODULE_OK;
149 case RLM_MODULE_UPDATED:
150 request->reply->code = PW_ACCOUNTING_RESPONSE;
154 request->reply->code = 0; /* don't reply */
167 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
169 listen_socket_t *sock = this->data;
173 if ((sock->ipaddr.af == AF_INET) &&
174 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
177 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
180 switch (this->type) {
181 case RAD_LISTEN_AUTH:
182 name = "authentication";
185 case RAD_LISTEN_ACCT:
189 case RAD_LISTEN_PROXY:
205 return snprintf(buffer, bufsize, "%s address %s port %d",
206 name, ip_buf, sock->port);
209 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
210 name, ip_buf, sock->port, this->server);
215 * Parse an authentication or accounting socket.
217 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
222 listen_socket_t *sock = this->data;
223 char *section_name = NULL;
224 CONF_SECTION *client_cs, *parentcs;
229 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
230 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
231 &ipaddr.ipaddr.ip4addr, NULL);
232 if (rcode < 0) return -1;
234 if (rcode == 0) { /* successfully parsed IPv4 */
237 } else { /* maybe IPv6? */
238 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
239 &ipaddr.ipaddr.ip6addr, NULL);
240 if (rcode < 0) return -1;
243 cf_log_err(cf_sectiontoitem(cs),
244 "No address specified in listen section");
247 ipaddr.af = AF_INET6;
250 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
252 if (rcode < 0) return -1;
254 if ((listen_port < 0) || (listen_port > 65500)) {
255 cf_log_err(cf_sectiontoitem(cs),
256 "Invalid value for \"port\"");
260 sock->ipaddr = ipaddr;
261 sock->port = listen_port;
264 * And bind it to the port.
266 if (listen_bind(this) < 0) {
268 cf_log_err(cf_sectiontoitem(cs),
269 "Error binding to port for %s port %d",
270 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
276 * If we can bind to interfaces, do so,
279 if (cf_pair_find(cs, "interface")) {
280 #ifndef SO_BINDTODEVICE
281 cf_log_err(cf_sectiontoitem(cs),
282 "System does not support binding to interfaces. Delete this line from the configuration file.");
286 CONF_PAIR *cp = cf_pair_find(cs, "interface");
289 rad_assert(cp != NULL);
290 value = cf_pair_value(cp);
292 cf_log_err(cf_sectiontoitem(cs),
293 "No interface name given");
297 strcpy(ifreq.ifr_name, value);
299 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
300 (char *)&ifreq, sizeof(ifreq)) < 0) {
301 cf_log_err(cf_sectiontoitem(cs),
302 "Failed binding to interface %s: %s",
303 value, strerror(errno));
305 } /* else it worked. */
310 * Proxy sockets don't have clients.
312 if (this->type == RAD_LISTEN_PROXY) return 0;
315 * The more specific configurations are preferred to more
319 parentcs = cf_top_section(cs);
320 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
321 §ion_name, NULL);
322 if (rcode < 0) return -1; /* bad string */
325 * Explicit list given: use it.
327 client_cs = cf_section_sub_find_name2(parentcs,
331 cf_log_err(cf_sectiontoitem(cs),
332 "Failed to find clients %s {...}",
338 } /* else there was no "clients = " entry. */
341 CONF_SECTION *server_cs;
343 server_cs = cf_section_sub_find_name2(parentcs,
347 * Found a "server foo" section. If there are clients
351 (cf_section_sub_find(server_cs, "client") != NULL)) {
352 client_cs = server_cs;
357 * Still nothing. Look for global clients.
359 if (!client_cs) client_cs = parentcs;
361 sock->clients = clients_parse_section(client_cs);
362 if (!sock->clients) {
363 cf_log_err(cf_sectiontoitem(cs),
364 "Failed to find any clients for this listen section");
372 * Send an authentication response packet
374 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
376 rad_assert(request->listener == listener);
377 rad_assert(listener->send == auth_socket_send);
379 return rad_send(request->reply, request->packet,
380 request->client->secret);
385 * Send an accounting response packet (or not)
387 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
389 rad_assert(request->listener == listener);
390 rad_assert(listener->send == acct_socket_send);
393 * Accounting reject's are silently dropped.
395 * We do it here to avoid polluting the rest of the
396 * code with this knowledge
398 if (request->reply->code == 0) return 0;
400 return rad_send(request->reply, request->packet,
401 request->client->secret);
406 * Send a packet to a home server.
408 * FIXME: have different code for proxy auth & acct!
410 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
412 listen_socket_t *sock = listener->data;
414 rad_assert(request->proxy_listener == listener);
415 rad_assert(listener->send == proxy_socket_send);
417 request->proxy->src_ipaddr = sock->ipaddr;
418 request->proxy->src_port = sock->port;
420 return rad_send(request->proxy, request->packet,
421 request->home_server->secret);
426 * Check if an incoming request is "ok"
428 * It takes packets, not requests. It sees if the packet looks
429 * OK. If so, it does a number of sanity checks on it.
431 static int auth_socket_recv(rad_listen_t *listener,
432 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
436 RADIUS_PACKET *packet;
437 RAD_REQUEST_FUNP fun = NULL;
440 fr_ipaddr_t src_ipaddr;
442 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
443 if (rcode < 0) return 0;
445 RAD_SNMP_TYPE_INC(listener, total_requests);
447 if (rcode < 20) { /* AUTH_HDR_LEN */
448 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
452 if ((client = client_listener_find(listener,
453 &src_ipaddr)) == NULL) {
454 rad_recv_discard(listener->fd);
455 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
457 if (debug_flag > 0) {
460 listener->print(listener, name, sizeof(name));
463 * This is debugging rather than logging, so that
464 * DoS attacks don't affect us.
466 DEBUG("Ignoring request to %s from unknown client %s port %d",
468 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
469 buffer, sizeof(buffer)), src_port);
476 * Some sanity checks, based on the packet code.
479 case PW_AUTHENTICATION_REQUEST:
480 RAD_SNMP_CLIENT_INC(listener, client, requests);
481 fun = rad_authenticate;
484 case PW_STATUS_SERVER:
485 if (!mainconfig.status_server) {
486 rad_recv_discard(listener->fd);
487 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
488 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
489 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
492 fun = rad_status_server;
496 rad_recv_discard(listener->fd);
497 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
498 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
500 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
501 code, client->shortname, src_port);
504 } /* switch over packet types */
507 * Now that we've sanity checked everything, receive the
510 packet = rad_recv(listener->fd, client->message_authenticator);
512 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
513 DEBUG("%s", librad_errstr);
517 if (!received_request(listener, packet, prequest, client)) {
518 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
519 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
530 * Receive packets from an accounting socket
532 static int acct_socket_recv(rad_listen_t *listener,
533 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
537 RADIUS_PACKET *packet;
538 RAD_REQUEST_FUNP fun = NULL;
541 fr_ipaddr_t src_ipaddr;
543 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
544 if (rcode < 0) return 0;
546 RAD_SNMP_TYPE_INC(listener, total_requests);
548 if (rcode < 20) { /* AUTH_HDR_LEN */
549 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
553 if ((client = client_listener_find(listener,
554 &src_ipaddr)) == NULL) {
555 rad_recv_discard(listener->fd);
556 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
559 * This is debugging rather than logging, so that
560 * DoS attacks don't affect us.
562 if (debug_flag > 0) {
565 listener->print(listener, name, sizeof(name));
567 DEBUG("Ignoring request to %s from unknown client %s port %d",
569 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
570 buffer, sizeof(buffer)), src_port);
577 * Some sanity checks, based on the packet code.
580 case PW_ACCOUNTING_REQUEST:
581 RAD_SNMP_CLIENT_INC(listener, client, requests);
582 fun = rad_accounting;
585 case PW_STATUS_SERVER:
586 if (!mainconfig.status_server) {
587 rad_recv_discard(listener->fd);
588 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
589 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
591 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
594 fun = rad_status_server;
598 rad_recv_discard(listener->fd);
599 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
600 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
602 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
603 code, client->shortname, src_port);
605 } /* switch over packet types */
608 * Now that we've sanity checked everything, receive the
611 packet = rad_recv(listener->fd, 0);
613 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
614 radlog(L_ERR, "%s", librad_errstr);
619 * There can be no duplicate accounting packets.
621 if (!received_request(listener, packet, prequest, client)) {
622 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
623 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
634 * Recieve packets from a proxy socket.
636 static int proxy_socket_recv(rad_listen_t *listener,
637 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
640 RADIUS_PACKET *packet;
641 RAD_REQUEST_FUNP fun = NULL;
644 packet = rad_recv(listener->fd, 0);
646 radlog(L_ERR, "%s", librad_errstr);
651 * FIXME: Client MIB updates?
653 switch(packet->code) {
654 case PW_AUTHENTICATION_ACK:
655 case PW_ACCESS_CHALLENGE:
656 case PW_AUTHENTICATION_REJECT:
657 fun = rad_authenticate;
660 case PW_ACCOUNTING_RESPONSE:
661 fun = rad_accounting;
666 * FIXME: Update MIB for packet types?
668 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
669 "from home server %s port %d - ID %d : IGNORED",
671 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
672 packet->src_port, packet->id);
677 request = received_proxy_response(packet);
682 rad_assert(fun != NULL);
690 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
692 if (!request->reply->code) return 0;
694 rad_encode(request->reply, request->packet,
695 request->client->secret);
696 rad_sign(request->reply, request->packet,
697 request->client->secret);
703 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
705 if (rad_verify(request->packet, NULL,
706 request->client->secret) < 0) {
710 return rad_decode(request->packet, NULL,
711 request->client->secret);
714 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
716 rad_encode(request->proxy, NULL, request->home_server->secret);
717 rad_sign(request->proxy, NULL, request->home_server->secret);
723 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
725 if (rad_verify(request->proxy_reply, request->proxy,
726 request->home_server->secret) < 0) {
730 return rad_decode(request->proxy_reply, request->proxy,
731 request->home_server->secret);
736 static int radius_snmp_recv(rad_listen_t *listener,
737 UNUSED RAD_REQUEST_FUNP *pfun,
738 UNUSED REQUEST **prequest)
740 if ((rad_snmp.smux_fd >= 0) &&
741 (rad_snmp.smux_event == SMUX_READ)) {
746 * If we've got to re-connect, then do so now,
747 * before calling select again.
749 if (rad_snmp.smux_event == SMUX_CONNECT) {
754 * Reset this every time, as the smux connect may have
755 * opened a new socket.
757 listener->fd = rad_snmp.smux_fd;
763 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
765 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
770 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
771 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
774 { common_socket_parse, NULL,
775 proxy_socket_recv, proxy_socket_send,
776 socket_print, proxy_socket_encode, proxy_socket_decode },
779 { common_socket_parse, NULL,
780 auth_socket_recv, auth_socket_send,
781 socket_print, client_socket_encode, client_socket_decode },
784 { common_socket_parse, NULL,
785 acct_socket_recv, acct_socket_send,
786 socket_print, client_socket_encode, client_socket_decode},
789 { detail_parse, detail_free,
790 detail_recv, detail_send,
791 detail_print, detail_encode, detail_decode },
794 /* vlan query protocol */
795 { common_socket_parse, NULL,
796 vqp_socket_recv, vqp_socket_send,
797 socket_print, vqp_socket_encode, vqp_socket_decode },
799 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
802 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
808 * Binds a listener to a socket.
810 static int listen_bind(rad_listen_t *this)
812 listen_socket_t *sock = this->data;
815 * If the port is zero, then it means the appropriate
816 * thing from /etc/services.
818 if (sock->port == 0) {
821 switch (this->type) {
822 case RAD_LISTEN_AUTH:
823 svp = getservbyname ("radius", "udp");
825 sock->port = ntohs(svp->s_port);
827 sock->port = PW_AUTH_UDP_PORT;
831 case RAD_LISTEN_ACCT:
832 svp = getservbyname ("radacct", "udp");
834 sock->port = ntohs(svp->s_port);
836 sock->port = PW_ACCT_UDP_PORT;
840 case RAD_LISTEN_PROXY:
851 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
856 this->fd = fr_socket(&sock->ipaddr, sock->port);
858 radlog(L_ERR, "ERROR: Failed to open socket: %s",
864 * FreeBSD jail issues. We bind to 0.0.0.0, but the
865 * kernel instead binds us to a 1.2.3.4. If this
866 * happens, notice, and remember our real IP.
869 struct sockaddr_storage src;
870 socklen_t sizeof_src = sizeof(src);
872 memset(&src, 0, sizeof_src);
873 if (getsockname(this->fd, (struct sockaddr *) &src,
875 radlog(L_ERR, "Failed getting socket name: %s",
880 if (src.ss_family == AF_INET) {
881 struct sockaddr_in *s4;
883 s4 = (struct sockaddr_in *)&src;
884 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
885 sock->port = ntohs(s4->sin_port);
887 #ifdef HAVE_STRUCT_SOCKADDR_IN6
888 } else if (src.ss_family == AF_INET6) {
889 struct sockaddr_in6 *s6;
891 s6 = (struct sockaddr_in6 *)&src;
892 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
893 sock->port = ntohs(s6->sin6_port);
896 radlog(L_ERR, "Socket has unsupported address family");
905 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
906 radlog(L_ERR, "Failure getting socket flags: %s)\n",
912 if( fcntl(this->fd, F_SETFL, flags) < 0) {
913 radlog(L_ERR, "Failure setting socket flags: %s)\n",
925 * Allocate & initialize a new listener.
927 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
931 this = rad_malloc(sizeof(*this));
932 memset(this, 0, sizeof(*this));
935 this->recv = master_listen[this->type].recv;
936 this->send = master_listen[this->type].send;
937 this->print = master_listen[this->type].print;
938 this->encode = master_listen[this->type].encode;
939 this->decode = master_listen[this->type].decode;
942 case RAD_LISTEN_AUTH:
943 case RAD_LISTEN_ACCT:
944 case RAD_LISTEN_PROXY:
946 this->data = rad_malloc(sizeof(listen_socket_t));
947 memset(this->data, 0, sizeof(listen_socket_t));
950 case RAD_LISTEN_DETAIL:
955 rad_assert("Unsupported option!" == NULL);
964 * Externally visible function for creating a new proxy LISTENER.
966 * For now, don't take ipaddr or port.
968 * Not thread-safe, but all calls to it are protected by the
969 * proxy mutex in request_list.c
971 rad_listen_t *proxy_new_listener()
973 int last_proxy_port, port;
974 rad_listen_t *this, *tmp, **last;
975 listen_socket_t *sock, *old;
977 this = listen_alloc(RAD_LISTEN_PROXY);
980 * Find an existing proxy socket to copy.
982 * FIXME: Make it per-realm, or per-home server!
986 last = &mainconfig.listen;
987 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
988 if (tmp->type == RAD_LISTEN_PROXY) {
990 if (sock->port > last_proxy_port) {
991 last_proxy_port = sock->port + 1;
993 if (!old) old = sock;
1001 return NULL; /* This is a serious error. */
1005 * FIXME: find a new IP address to listen on?
1007 * This could likely be done in the "home server"
1008 * configuration, to have per-home-server source IP's.
1011 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1014 * Keep going until we find an unused port.
1016 for (port = last_proxy_port; port < 64000; port++) {
1018 if (listen_bind(this) == 0) {
1020 * Add the new listener to the list of
1033 static const FR_NAME_NUMBER listen_compare[] = {
1034 { "auth", RAD_LISTEN_AUTH },
1035 { "acct", RAD_LISTEN_ACCT },
1036 { "detail", RAD_LISTEN_DETAIL },
1037 { "proxy", RAD_LISTEN_PROXY },
1039 { "vmps", RAD_LISTEN_VQP },
1045 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1053 cf_log_info(cs, "listen {");
1055 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1057 if (rcode < 0) return NULL;
1060 cf_log_err(cf_sectiontoitem(cs),
1061 "No type specified in listen section");
1065 type = fr_str2int(listen_compare, listen_type,
1067 if (type == RAD_LISTEN_NONE) {
1068 cf_log_err(cf_sectiontoitem(cs),
1069 "Invalid type \"%s\" in listen section.",
1077 * Allow listen sections in the default config to
1078 * refer to a server.
1081 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1083 if (rcode == 1) { /* compatiblity with 2.0-pre */
1084 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1087 if (rcode < 0) return NULL;
1091 * Set up cross-type data.
1093 this = listen_alloc(type);
1094 this->server = server;
1098 * Call per-type parser.
1100 if (master_listen[type].parse(cs, this) < 0) {
1105 cf_log_info(cs, "}");
1111 * Generate a list of listeners. Takes an input list of
1112 * listeners, too, so we don't close sockets with waiting packets.
1114 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1116 int override = FALSE;
1119 rad_listen_t **last;
1121 fr_ipaddr_t server_ipaddr;
1123 int defined_proxy = 0;
1126 * We shouldn't be called with a pre-existing list.
1128 rad_assert(head && (*head == NULL));
1131 server_ipaddr.af = AF_UNSPEC;
1134 * If the port is specified on the command-line,
1135 * it over-rides the configuration file.
1137 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1139 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1142 * If the IP address was configured on the command-line,
1143 * use that as the "bind_address"
1145 if (mainconfig.myip.af != AF_UNSPEC) {
1146 memcpy(&server_ipaddr, &mainconfig.myip,
1147 sizeof(server_ipaddr));
1153 * Else look for bind_address and/or listen sections.
1155 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1156 rcode = cf_item_parse(config, "bind_address",
1158 &server_ipaddr.ipaddr.ip4addr, NULL);
1159 if (rcode < 0) return -1; /* error parsing it */
1161 if (rcode == 0) { /* successfully parsed IPv4 */
1162 listen_socket_t *sock;
1163 server_ipaddr.af = AF_INET;
1165 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'.");
1169 if (strcmp(progname, "vmpsd") == 0) {
1170 this = listen_alloc(RAD_LISTEN_VQP);
1171 if (!auth_port) auth_port = 1589;
1174 this = listen_alloc(RAD_LISTEN_AUTH);
1178 sock->ipaddr = server_ipaddr;
1179 sock->port = auth_port;
1181 sock->clients = clients_parse_section(config);
1182 if (!sock->clients) {
1183 cf_log_err(cf_sectiontoitem(config),
1184 "Failed to find any clients for this listen section");
1188 if (listen_bind(this) < 0) {
1191 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1194 auth_port = sock->port; /* may have been updated in listen_bind */
1196 cs = cf_section_sub_find_name2(config, "server",
1198 if (cs) this->server = mainconfig.name;
1202 last = &(this->next);
1208 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1212 * Open Accounting Socket.
1214 * If we haven't already gotten acct_port from
1215 * /etc/services, then make it auth_port + 1.
1217 this = listen_alloc(RAD_LISTEN_ACCT);
1221 * Create the accounting socket.
1223 * The accounting port is always the
1224 * authentication port + 1
1226 sock->ipaddr = server_ipaddr;
1227 sock->port = auth_port + 1;
1229 sock->clients = clients_parse_section(config);
1230 if (!sock->clients) {
1231 cf_log_err(cf_sectiontoitem(config),
1232 "Failed to find any clients for this listen section");
1236 if (listen_bind(this) < 0) {
1239 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1244 cs = cf_section_sub_find_name2(config, "server",
1246 if (cs) this->server = mainconfig.name;
1250 last = &(this->next);
1252 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1253 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1259 * They specified an IP on the command-line, ignore
1260 * all listen sections except the one in '-n'.
1262 if (mainconfig.myip.af != AF_UNSPEC) {
1263 CONF_SECTION *subcs;
1264 const char *name2 = cf_section_name2(cs);
1266 cs = cf_section_sub_find_name2(config, "server",
1268 if (!cs) goto do_proxy;
1271 * Should really abstract this code...
1273 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1275 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1276 this = listen_parse(subcs, name2);
1282 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1285 last = &(this->next);
1286 } /* loop over "listen" directives in server <foo> */
1292 * Walk through the "listen" sections, if they exist.
1294 for (cs = cf_subsection_find_next(config, NULL, "listen");
1296 cs = cf_subsection_find_next(config, cs, "listen")) {
1297 this = listen_parse(cs, NULL);
1303 if (this->type == RAD_LISTEN_PROXY) defined_proxy = 1;
1306 last = &(this->next);
1310 * Check virtual servers for "listen" sections, too.
1312 * FIXME: Move to virtual server init?
1314 for (cs = cf_subsection_find_next(config, NULL, "server");
1316 cs = cf_subsection_find_next(config, cs, "server")) {
1317 CONF_SECTION *subcs;
1318 const char *name2 = cf_section_name2(cs);
1320 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1322 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1323 this = listen_parse(subcs, name2);
1329 if (this->type == RAD_LISTEN_PROXY) {
1330 radlog(L_ERR, "Error: listen type \"proxy\" Cannot appear in a virtual server section");
1336 last = &(this->next);
1337 } /* loop over "listen" directives in virtual servers */
1338 } /* loop over virtual servers */
1341 * If we're proxying requests, open the proxy FD.
1342 * Otherwise, don't do anything.
1345 if (mainconfig.proxy_requests == TRUE) {
1347 listen_socket_t *sock = NULL;
1350 * No sockets to receive packets, therefore
1351 * proxying is pointless.
1353 if (!*head) return -1;
1355 if (defined_proxy) goto do_snmp;
1358 * Find the first authentication port,
1361 for (this = *head; this != NULL; this = this->next) {
1362 if (this->type == RAD_LISTEN_AUTH) {
1364 if (server_ipaddr.af == AF_UNSPEC) {
1365 server_ipaddr = sock->ipaddr;
1367 port = sock->port + 2; /* skip acct port */
1370 if (this->type == RAD_LISTEN_VQP) {
1372 if (server_ipaddr.af == AF_UNSPEC) {
1373 server_ipaddr = sock->ipaddr;
1375 port = sock->port + 1;
1380 if (port < 0) port = 1024 + (fr_rand() & 0x1ff);
1383 * Address is still unspecified, use IPv4.
1385 if (server_ipaddr.af == AF_UNSPEC) {
1386 server_ipaddr.af = AF_INET;
1387 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1390 this = listen_alloc(RAD_LISTEN_PROXY);
1394 * Create the first proxy socket.
1396 sock->ipaddr = server_ipaddr;
1399 * Try to find a proxy port (value doesn't matter)
1401 for (sock->port = port;
1404 if (listen_bind(this) == 0) {
1406 last = &(this->next); /* just in case */
1411 if (sock->port >= 64000) {
1414 radlog(L_ERR, "Failed to open socket for proxying");
1421 if (radius_snmp_init(config)) {
1422 this = rad_malloc(sizeof(*this));
1423 memset(this, 0, sizeof(*this));
1425 this->type = RAD_LISTEN_SNMP;
1426 this->fd = rad_snmp.smux_fd;
1428 this->recv = radius_snmp_recv;
1429 this->print = radius_snmp_print;
1432 last = &(this->next);
1440 * Free a linked list of listeners;
1442 void listen_free(rad_listen_t **head)
1446 if (!head || !*head) return;
1450 rad_listen_t *next = this->next;
1453 * Other code may have eaten the FD.
1455 if (this->fd >= 0) close(this->fd);
1457 if (master_listen[this->type].free) {
1458 master_listen[this->type].free(this);