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>
45 * We'll use this below.
47 typedef int (*rad_listen_parse_t)(CONF_SECTION *, rad_listen_t *);
48 typedef void (*rad_listen_free_t)(rad_listen_t *);
50 typedef struct rad_listen_master_t {
51 rad_listen_parse_t parse;
52 rad_listen_free_t free;
53 rad_listen_recv_t recv;
54 rad_listen_send_t send;
55 rad_listen_print_t print;
56 rad_listen_encode_t encode;
57 rad_listen_decode_t decode;
58 } rad_listen_master_t;
60 typedef struct listen_socket_t {
66 RADCLIENT_LIST *clients;
70 * Find a per-socket client.
72 RADCLIENT *client_listener_find(const rad_listen_t *listener,
73 const lrad_ipaddr_t *ipaddr)
75 const RADCLIENT_LIST *clients;
77 rad_assert(listener != NULL);
78 rad_assert(ipaddr != NULL);
80 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
81 (listener->type == RAD_LISTEN_ACCT) ||
82 (listener->type == RAD_LISTEN_VQP));
84 clients = ((listen_socket_t *)listener->data)->clients;
87 * This HAS to have been initialized previously.
89 rad_assert(clients != NULL);
91 return client_find(clients, ipaddr);
94 static int listen_bind(rad_listen_t *this);
98 * Process and reply to a server-status request.
99 * Like rad_authenticate and rad_accounting this should
100 * live in it's own file but it's so small we don't bother.
102 static int rad_status_server(REQUEST *request)
104 int rcode = RLM_MODULE_OK;
107 switch (request->listener->type) {
108 case RAD_LISTEN_AUTH:
109 dval = dict_valbyname(PW_AUTZ_TYPE, "Status-Server");
111 rcode = module_authorize(dval->value, request);
113 rcode = RLM_MODULE_OK;
118 case RLM_MODULE_UPDATED:
119 request->reply->code = PW_AUTHENTICATION_ACK;
122 case RLM_MODULE_FAIL:
123 case RLM_MODULE_HANDLED:
124 request->reply->code = 0; /* don't reply */
128 case RLM_MODULE_REJECT:
129 request->reply->code = PW_AUTHENTICATION_REJECT;
134 case RAD_LISTEN_ACCT:
135 dval = dict_valbyname(PW_ACCT_TYPE, "Status-Server");
137 rcode = module_accounting(dval->value, request);
139 rcode = RLM_MODULE_OK;
144 case RLM_MODULE_UPDATED:
145 request->reply->code = PW_ACCOUNTING_RESPONSE;
149 request->reply->code = 0; /* don't reply */
162 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
164 listen_socket_t *sock = this->data;
168 if ((sock->ipaddr.af == AF_INET) &&
169 (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_ANY))) {
172 ip_ntoh(&sock->ipaddr, ip_buf, sizeof(ip_buf));
175 switch (this->type) {
176 case RAD_LISTEN_AUTH:
177 name = "authentication";
180 case RAD_LISTEN_ACCT:
184 case RAD_LISTEN_PROXY:
200 return snprintf(buffer, bufsize, "%s address %s port %d",
201 name, ip_buf, sock->port);
204 return snprintf(buffer, bufsize, "%s address %s port %d as server %s",
205 name, ip_buf, sock->port, this->server);
210 * Parse an authentication or accounting socket.
212 static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
216 lrad_ipaddr_t ipaddr;
217 listen_socket_t *sock = this->data;
218 const char *section_name = NULL;
219 CONF_SECTION *client_cs, *parentcs;
224 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
225 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
226 &ipaddr.ipaddr.ip4addr, NULL);
227 if (rcode < 0) return -1;
229 if (rcode == 0) { /* successfully parsed IPv4 */
232 } else { /* maybe IPv6? */
233 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
234 &ipaddr.ipaddr.ip6addr, NULL);
235 if (rcode < 0) return -1;
238 cf_log_err(cf_sectiontoitem(cs),
239 "No address specified in listen section");
242 ipaddr.af = AF_INET6;
245 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
247 if (rcode < 0) return -1;
249 sock->ipaddr = ipaddr;
250 sock->port = listen_port;
253 * And bind it to the port.
255 if (listen_bind(this) < 0) {
257 cf_log_err(cf_sectiontoitem(cs),
258 "Error binding to port for %s port %d",
259 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
265 * If we can bind to interfaces, do so,
268 if (cf_pair_find(cs, "interface")) {
269 #ifndef SO_BINDTODEVICE
270 cf_log_err(cf_sectiontoitem(cs),
271 "System does not support binding to interfaces. Delete this line from the configuration file.");
275 CONF_PAIR *cp = cf_pair_find(cs, "interface");
278 rad_assert(cp != NULL);
279 value = cf_pair_value(cp);
281 cf_log_err(cf_sectiontoitem(cs),
282 "No interface name given");
286 strcpy(ifreq.ifr_name, value);
288 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
289 (char *)&ifreq, sizeof(ifreq)) < 0) {
290 cf_log_err(cf_sectiontoitem(cs),
291 "Failed binding to interface %s: %s",
292 value, strerror(errno));
294 } /* else it worked. */
299 * The more specific configurations are preferred to more
303 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
304 §ion_name, NULL);
305 if (rcode < 0) return -1; /* bad string */
308 * Explicit list given: use it.
310 client_cs = cf_section_find(section_name);
313 cf_log_err(cf_sectiontoitem(cs),
314 "Failed to find clients %s {...}",
318 } /* else there was no "clients = " entry. */
320 parentcs = cf_top_section(cs);
322 CONF_SECTION *server_cs;
324 server_cs = cf_section_sub_find_name2(parentcs,
328 * Found a "server foo" section. If there are clients
332 (cf_section_sub_find(server_cs, "client") != NULL)) {
333 client_cs = server_cs;
338 * Still nothing. Look for global clients.
340 if (!client_cs) client_cs = parentcs;
342 sock->clients = clients_parse_section(client_cs);
343 if (!sock->clients) {
344 cf_log_err(cf_sectiontoitem(cs),
345 "Failed to find any clients for this listen section");
353 * Send an authentication response packet
355 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
357 rad_assert(request->listener == listener);
358 rad_assert(listener->send == auth_socket_send);
360 return rad_send(request->reply, request->packet,
361 request->client->secret);
366 * Send an accounting response packet (or not)
368 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
370 rad_assert(request->listener == listener);
371 rad_assert(listener->send == acct_socket_send);
374 * Accounting reject's are silently dropped.
376 * We do it here to avoid polluting the rest of the
377 * code with this knowledge
379 if (request->reply->code == 0) return 0;
381 return rad_send(request->reply, request->packet,
382 request->client->secret);
387 * Send a packet to a home server.
389 * FIXME: have different code for proxy auth & acct!
391 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
393 listen_socket_t *sock = listener->data;
395 rad_assert(request->proxy_listener == listener);
396 rad_assert(listener->send == proxy_socket_send);
398 request->proxy->src_ipaddr = sock->ipaddr;
399 request->proxy->src_port = sock->port;
401 return rad_send(request->proxy, request->packet,
402 request->home_server->secret);
407 * Check if an incoming request is "ok"
409 * It takes packets, not requests. It sees if the packet looks
410 * OK. If so, it does a number of sanity checks on it.
412 static int auth_socket_recv(rad_listen_t *listener,
413 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
417 RADIUS_PACKET *packet;
418 RAD_REQUEST_FUNP fun = NULL;
421 lrad_ipaddr_t src_ipaddr;
423 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
424 if (rcode < 0) return 0;
426 RAD_SNMP_TYPE_INC(listener, total_requests);
428 if (rcode < 20) { /* AUTH_HDR_LEN */
429 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
433 if ((client = client_listener_find(listener,
434 &src_ipaddr)) == NULL) {
435 rad_recv_discard(listener->fd);
436 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
438 if (debug_flag > 0) {
441 listener->print(listener, name, sizeof(name));
444 * This is debugging rather than logging, so that
445 * DoS attacks don't affect us.
447 DEBUG("Ignoring request to %s from unknown client %s port %d",
449 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
450 buffer, sizeof(buffer)), src_port);
457 * Some sanity checks, based on the packet code.
460 case PW_AUTHENTICATION_REQUEST:
461 RAD_SNMP_CLIENT_INC(listener, client, requests);
462 fun = rad_authenticate;
465 case PW_STATUS_SERVER:
466 if (!mainconfig.status_server) {
467 rad_recv_discard(listener->fd);
468 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
469 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
470 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
473 fun = rad_status_server;
477 rad_recv_discard(listener->fd);
478 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
479 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
481 DEBUG("Invalid packet code %d sent to authentication port from client %s port %d : IGNORED",
482 code, client->shortname, src_port);
485 } /* switch over packet types */
488 * Now that we've sanity checked everything, receive the
491 packet = rad_recv(listener->fd);
493 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
494 radlog(L_ERR, "%s", librad_errstr);
498 if (!received_request(listener, packet, prequest, client)) {
499 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
500 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
511 * Receive packets from an accounting socket
513 static int acct_socket_recv(rad_listen_t *listener,
514 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
518 RADIUS_PACKET *packet;
519 RAD_REQUEST_FUNP fun = NULL;
522 lrad_ipaddr_t src_ipaddr;
524 rcode = rad_recv_header(listener->fd, &src_ipaddr, &src_port, &code);
525 if (rcode < 0) return 0;
527 RAD_SNMP_TYPE_INC(listener, total_requests);
529 if (rcode < 20) { /* AUTH_HDR_LEN */
530 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
534 if ((client = client_listener_find(listener,
535 &src_ipaddr)) == NULL) {
536 rad_recv_discard(listener->fd);
537 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
540 * This is debugging rather than logging, so that
541 * DoS attacks don't affect us.
543 if (debug_flag > 0) {
546 listener->print(listener, name, sizeof(name));
548 DEBUG("Ignoring request to %s from unknown client %s port %d",
550 inet_ntop(src_ipaddr.af, &src_ipaddr.ipaddr,
551 buffer, sizeof(buffer)), src_port);
558 * Some sanity checks, based on the packet code.
561 case PW_ACCOUNTING_REQUEST:
562 RAD_SNMP_CLIENT_INC(listener, client, requests);
563 fun = rad_accounting;
566 case PW_STATUS_SERVER:
567 if (!mainconfig.status_server) {
568 rad_recv_discard(listener->fd);
569 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
570 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
572 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
575 fun = rad_status_server;
579 rad_recv_discard(listener->fd);
580 RAD_SNMP_TYPE_INC(listener, total_unknown_types);
581 RAD_SNMP_CLIENT_INC(listener, client, unknown_types);
583 DEBUG("Invalid packet code %d sent to a accounting port from client %s port %d : IGNORED",
584 code, client->shortname, src_port);
586 } /* switch over packet types */
589 * Now that we've sanity checked everything, receive the
592 packet = rad_recv(listener->fd);
594 RAD_SNMP_TYPE_INC(listener, total_malformed_requests);
595 radlog(L_ERR, "%s", librad_errstr);
600 * There can be no duplicate accounting packets.
602 if (!received_request(listener, packet, prequest, client)) {
603 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
604 RAD_SNMP_CLIENT_INC(listener, client, packets_dropped);
615 * Recieve packets from a proxy socket.
617 static int proxy_socket_recv(rad_listen_t *listener,
618 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
621 RADIUS_PACKET *packet;
622 RAD_REQUEST_FUNP fun = NULL;
625 packet = rad_recv(listener->fd);
627 radlog(L_ERR, "%s", librad_errstr);
632 * FIXME: Client MIB updates?
634 switch(packet->code) {
635 case PW_AUTHENTICATION_ACK:
636 case PW_ACCESS_CHALLENGE:
637 case PW_AUTHENTICATION_REJECT:
638 fun = rad_authenticate;
641 case PW_ACCOUNTING_RESPONSE:
642 fun = rad_accounting;
647 * FIXME: Update MIB for packet types?
649 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
650 "from home server %s port %d - ID %d : IGNORED",
652 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
653 packet->src_port, packet->id);
658 request = received_proxy_response(packet);
663 rad_assert(fun != NULL);
671 static int client_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
673 if (!request->reply->code) return 0;
675 rad_encode(request->reply, request->packet,
676 request->client->secret);
677 rad_sign(request->reply, request->packet,
678 request->client->secret);
684 static int client_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
686 if (rad_verify(request->packet, NULL,
687 request->client->secret) < 0) {
691 return rad_decode(request->packet, NULL,
692 request->client->secret);
695 static int proxy_socket_encode(UNUSED rad_listen_t *listener, REQUEST *request)
697 rad_encode(request->proxy, NULL, request->home_server->secret);
698 rad_sign(request->proxy, NULL, request->home_server->secret);
704 static int proxy_socket_decode(UNUSED rad_listen_t *listener, REQUEST *request)
706 if (rad_verify(request->proxy_reply, request->proxy,
707 request->home_server->secret) < 0) {
711 return rad_decode(request->proxy_reply, request->proxy,
712 request->home_server->secret);
717 static int radius_snmp_recv(rad_listen_t *listener,
718 UNUSED RAD_REQUEST_FUNP *pfun,
719 UNUSED REQUEST **prequest)
721 if ((rad_snmp.smux_fd >= 0) &&
722 (rad_snmp.smux_event == SMUX_READ)) {
727 * If we've got to re-connect, then do so now,
728 * before calling select again.
730 if (rad_snmp.smux_event == SMUX_CONNECT) {
735 * Reset this every time, as the smux connect may have
736 * opened a new socket.
738 listener->fd = rad_snmp.smux_fd;
744 static int radius_snmp_print(UNUSED rad_listen_t *this, char *buffer, size_t bufsize)
746 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.11344.1.1.1");
751 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
752 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
756 proxy_socket_recv, proxy_socket_send,
757 socket_print, proxy_socket_encode, proxy_socket_decode },
760 { common_socket_parse, NULL,
761 auth_socket_recv, auth_socket_send,
762 socket_print, client_socket_encode, client_socket_decode },
765 { common_socket_parse, NULL,
766 acct_socket_recv, acct_socket_send,
767 socket_print, client_socket_encode, client_socket_decode},
770 { detail_parse, detail_free,
771 detail_recv, detail_send,
772 detail_print, detail_encode, detail_decode },
775 /* vlan query protocol */
776 { common_socket_parse, NULL,
777 vqp_socket_recv, vqp_socket_send,
778 socket_print, vqp_socket_encode, vqp_socket_decode },
780 { NULL, NULL, NULL, NULL, NULL, NULL, NULL},
783 { NULL, NULL, NULL, NULL, NULL, NULL, NULL} /* RAD_LISTEN_SNMP */
789 * Binds a listener to a socket.
791 static int listen_bind(rad_listen_t *this)
794 listen_socket_t *sock = this->data;
797 * If the port is zero, then it means the appropriate
798 * thing from /etc/services.
800 if (sock->port == 0) {
803 switch (this->type) {
804 case RAD_LISTEN_AUTH:
805 svp = getservbyname ("radius", "udp");
807 sock->port = ntohs(svp->s_port);
809 sock->port = PW_AUTH_UDP_PORT;
813 case RAD_LISTEN_ACCT:
814 svp = getservbyname ("radacct", "udp");
816 sock->port = ntohs(svp->s_port);
818 sock->port = PW_ACCT_UDP_PORT;
829 radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
835 * Find it in the old list, AFTER updating the port. If
836 * it's there, use that, rather than creating a new
837 * socket. This allows HUP's to re-use the old sockets,
838 * which means that packets waiting in the socket queue
841 for (last = &mainconfig.listen;
843 last = &((*last)->next)) {
844 listen_socket_t *other;
846 if (this->type != (*last)->type) continue;
848 if ((this->type == RAD_LISTEN_DETAIL) ||
849 (this->type == RAD_LISTEN_SNMP)) continue;
851 other = (listen_socket_t *)((*last)->data);
853 if ((sock->port == other->port) &&
854 (sock->ipaddr.af == other->ipaddr.af) &&
855 (lrad_ipaddr_cmp(&sock->ipaddr, &other->ipaddr) == 0)) {
856 this->fd = (*last)->fd;
862 this->fd = lrad_socket(&sock->ipaddr, sock->port);
864 radlog(L_ERR, "ERROR: Failed to open socket: %s",
870 struct sockaddr_storage src;
871 socklen_t sizeof_src = sizeof(src);
873 memset(&src, 0, sizeof_src);
874 if (getsockname(this->fd, (struct sockaddr *) &src,
879 if (src.ss_family == AF_INET) {
880 struct sockaddr_in *s4;
882 s4 = (struct sockaddr_in *)&src;
883 sock->ipaddr.ipaddr.ip4addr = s4->sin_addr;
885 #ifdef HAVE_STRUCT_SOCKADDR_IN6
886 } else if (src.ss_family == AF_INET6) {
887 struct sockaddr_in6 *s6;
889 s6 = (struct sockaddr_in6 *)&src;
890 sock->ipaddr.ipaddr.ip6addr = s6->sin6_addr;
899 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
900 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
905 if( fcntl(this->fd, F_SETFL, flags) < 0) {
906 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
917 * Allocate & initialize a new listener.
919 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
923 this = rad_malloc(sizeof(*this));
924 memset(this, 0, sizeof(*this));
927 this->recv = master_listen[this->type].recv;
928 this->send = master_listen[this->type].send;
929 this->print = master_listen[this->type].print;
930 this->encode = master_listen[this->type].encode;
931 this->decode = master_listen[this->type].decode;
934 case RAD_LISTEN_AUTH:
935 case RAD_LISTEN_ACCT:
936 case RAD_LISTEN_PROXY:
938 this->data = rad_malloc(sizeof(listen_socket_t));
939 memset(this->data, 0, sizeof(listen_socket_t));
942 case RAD_LISTEN_DETAIL:
947 rad_assert("Unsupported option!" == NULL);
956 * Externally visible function for creating a new proxy LISTENER.
958 * For now, don't take ipaddr or port.
960 * Not thread-safe, but all calls to it are protected by the
961 * proxy mutex in request_list.c
963 rad_listen_t *proxy_new_listener()
965 int last_proxy_port, port;
966 rad_listen_t *this, *tmp, **last;
967 listen_socket_t *sock, *old;
969 this = listen_alloc(RAD_LISTEN_PROXY);
972 * Find an existing proxy socket to copy.
974 * FIXME: Make it per-realm, or per-home server!
978 last = &mainconfig.listen;
979 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
980 if (tmp->type == RAD_LISTEN_PROXY) {
982 if (sock->port > last_proxy_port) {
983 last_proxy_port = sock->port + 1;
985 if (!old) old = sock;
991 if (!old) return NULL; /* This is a serious error. */
994 * FIXME: find a new IP address to listen on?
996 * This could likely be done in the "home server"
997 * configuration, to have per-home-server source IP's.
1000 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1003 * Keep going until we find an unused port.
1005 for (port = last_proxy_port; port < 64000; port++) {
1007 if (listen_bind(this) == 0) {
1009 * Add the new listener to the list of
1021 static const LRAD_NAME_NUMBER listen_compare[] = {
1022 { "auth", RAD_LISTEN_AUTH },
1023 { "acct", RAD_LISTEN_ACCT },
1024 { "detail", RAD_LISTEN_DETAIL },
1026 { "vmps", RAD_LISTEN_VQP },
1032 static rad_listen_t *listen_parse(CONF_SECTION *cs, const char *server)
1042 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1044 if (rcode < 0) return NULL;
1047 cf_log_err(cf_sectiontoitem(cs),
1048 "No type specified in listen section");
1052 type = lrad_str2int(listen_compare, listen_type,
1054 if (type == RAD_LISTEN_NONE) {
1055 cf_log_err(cf_sectiontoitem(cs),
1056 "Invalid type \"%s\" in listen section.",
1064 * Allow listen sections in the default config to
1065 * refer to a server.
1068 rcode = cf_item_parse(cs, "virtual_server", PW_TYPE_STRING_PTR,
1070 if (rcode == 1) { /* compatiblity with 2.0-pre */
1071 rcode = cf_item_parse(cs, "server", PW_TYPE_STRING_PTR,
1074 if (rcode < 0) return NULL;
1078 * Set up cross-type data.
1080 this = listen_alloc(type);
1081 this->server = server;
1085 * Call per-type parser.
1087 if (master_listen[type].parse(cs, this) < 0) {
1098 * Generate a list of listeners. Takes an input list of
1099 * listeners, too, so we don't close sockets with waiting packets.
1101 int listen_init(CONF_SECTION *config, rad_listen_t **head)
1105 rad_listen_t **last;
1107 lrad_ipaddr_t server_ipaddr;
1111 * We shouldn't be called with a pre-existing list.
1113 rad_assert(head && (*head == NULL));
1116 server_ipaddr.af = AF_UNSPEC;
1119 * If the port is specified on the command-line,
1120 * it over-rides the configuration file.
1122 * FIXME: If argv[0] == "vmpsd", then don't listen on auth/acct!
1124 if (mainconfig.port >= 0) auth_port = mainconfig.port;
1127 * If the IP address was configured on the command-line,
1128 * use that as the "bind_address"
1130 if (mainconfig.myip.af != AF_UNSPEC) {
1131 memcpy(&server_ipaddr, &mainconfig.myip,
1132 sizeof(server_ipaddr));
1137 * Else look for bind_address and/or listen sections.
1139 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1140 rcode = cf_item_parse(config, "bind_address",
1142 &server_ipaddr.ipaddr.ip4addr, NULL);
1143 if (rcode < 0) return -1; /* error parsing it */
1145 if (rcode == 0) { /* successfully parsed IPv4 */
1146 listen_socket_t *sock;
1147 server_ipaddr.af = AF_INET;
1149 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'.");
1153 if (strcmp(progname, "vmpsd") == 0) {
1154 this = listen_alloc(RAD_LISTEN_VQP);
1155 if (!auth_port) auth_port = 1589;
1158 this = listen_alloc(RAD_LISTEN_AUTH);
1162 sock->ipaddr = server_ipaddr;
1163 sock->port = auth_port;
1165 if (listen_bind(this) < 0) {
1168 radlog(L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1171 auth_port = sock->port; /* may have been updated in listen_bind */
1173 last = &(this->next);
1179 if (strcmp(progname, "vmpsd") == 0) goto do_proxy;
1183 * Open Accounting Socket.
1185 * If we haven't already gotten acct_port from
1186 * /etc/services, then make it auth_port + 1.
1188 this = listen_alloc(RAD_LISTEN_ACCT);
1192 * Create the accounting socket.
1194 * The accounting port is always the
1195 * authentication port + 1
1197 sock->ipaddr = server_ipaddr;
1198 sock->port = auth_port + 1;
1200 if (listen_bind(this) < 0) {
1203 radlog(L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1208 last = &(this->next);
1210 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1211 radlog(L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1217 * They specified an IP on the command-line, ignore
1218 * all listen sections.
1220 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1223 * Walk through the "listen" sections, if they exist.
1225 for (cs = cf_subsection_find_next(config, NULL, "listen");
1227 cs = cf_subsection_find_next(config, cs, "listen")) {
1228 this = listen_parse(cs, NULL);
1235 last = &(this->next);
1239 * Check virtual servers for "listen" sections, too.
1241 * FIXME: Move to virtual server init?
1243 for (cs = cf_subsection_find_next(config, NULL, "server");
1245 cs = cf_subsection_find_next(config, cs, "server")) {
1246 CONF_SECTION *subcs;
1247 const char *name2 = cf_section_name2(cs);
1249 for (subcs = cf_subsection_find_next(cs, NULL, "listen");
1251 subcs = cf_subsection_find_next(cs, subcs, "listen")) {
1252 this = listen_parse(subcs, name2);
1259 last = &(this->next);
1260 } /* loop over "listen" directives in virtual servers */
1261 } /* loop over virtual servers */
1264 * If we're proxying requests, open the proxy FD.
1265 * Otherwise, don't do anything.
1268 if (mainconfig.proxy_requests == TRUE) {
1270 listen_socket_t *sock = NULL;
1273 * No sockets to receive packets, therefore
1274 * proxying is pointless.
1276 if (!*head) return -1;
1279 * If we previously had proxy sockets, copy them
1280 * to the new config.
1282 if (mainconfig.listen != NULL) {
1283 rad_listen_t *old, *next, **tail;
1285 tail = &mainconfig.listen;
1286 for (old = mainconfig.listen;
1291 if (old->type != RAD_LISTEN_PROXY) {
1292 tail = &((*tail)->next);
1299 last = &(old->next);
1306 * Find the first authentication port,
1309 for (this = *head; this != NULL; this = this->next) {
1310 if (this->type == RAD_LISTEN_AUTH) {
1312 if (server_ipaddr.af == AF_UNSPEC) {
1313 server_ipaddr = sock->ipaddr;
1315 port = sock->port + 2; /* skip acct port */
1318 if (this->type == RAD_LISTEN_VQP) {
1320 if (server_ipaddr.af == AF_UNSPEC) {
1321 server_ipaddr = sock->ipaddr;
1323 port = sock->port + 1;
1328 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1331 * Address is still unspecified, use IPv4.
1333 if (server_ipaddr.af == AF_UNSPEC) {
1334 server_ipaddr.af = AF_INET;
1335 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1338 this = listen_alloc(RAD_LISTEN_PROXY);
1342 * Create the first proxy socket.
1344 sock->ipaddr = server_ipaddr;
1347 * Try to find a proxy port (value doesn't matter)
1349 for (sock->port = port;
1352 if (listen_bind(this) == 0) {
1354 last = &(this->next); /* just in case */
1359 if (sock->port >= 64000) {
1362 radlog(L_ERR, "Failed to open socket for proxying");
1369 if (radius_snmp_init(config)) {
1371 * Forget about the old one.
1373 for (this = mainconfig.listen;
1375 this = this->next) {
1376 if (this->type != RAD_LISTEN_SNMP) continue;
1380 this = rad_malloc(sizeof(*this));
1381 memset(this, 0, sizeof(*this));
1383 this->type = RAD_LISTEN_SNMP;
1384 this->fd = rad_snmp.smux_fd;
1386 this->recv = radius_snmp_recv;
1387 this->print = radius_snmp_print;
1390 last = &(this->next);
1398 * Free a linked list of listeners;
1400 void listen_free(rad_listen_t **head)
1404 if (!head || !*head) return;
1408 rad_listen_t *next = this->next;
1411 * Other code may have eaten the FD.
1413 if (this->fd >= 0) close(this->fd);
1415 if (master_listen[this->type].free) {
1416 master_listen[this->type].free(this);