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/rad_assert.h>
31 #include <sys/resource.h>
37 #ifdef HAVE_SYS_STAT_H
43 static time_t start_time = 0;
46 * FIXME: Delete this crap!
48 extern time_t time_now;
51 * We'll use this below.
53 typedef int (*rad_listen_parse_t)(const char *, int, const CONF_SECTION *, rad_listen_t *);
54 typedef void (*rad_listen_free_t)(rad_listen_t *);
56 typedef struct rad_listen_master_t {
57 rad_listen_parse_t parse;
58 rad_listen_free_t free;
59 rad_listen_recv_t recv;
60 rad_listen_send_t send;
61 rad_listen_print_t print;
62 rad_listen_encode_t encode;
63 rad_listen_decode_t decode;
64 } rad_listen_master_t;
66 typedef struct listen_socket_t {
72 RADCLIENT_LIST *clients;
75 typedef struct listen_detail_t {
81 lrad_ipaddr_t client_ip;
88 * Find a per-socket client.
90 static RADCLIENT *client_listener_find(const rad_listen_t *listener,
91 const lrad_ipaddr_t *ipaddr)
93 const RADCLIENT_LIST *clients;
95 rad_assert(listener != NULL);
96 rad_assert(ipaddr != NULL);
98 rad_assert((listener->type == RAD_LISTEN_AUTH) ||
99 (listener->type == RAD_LISTEN_ACCT));
101 clients = ((listen_socket_t *)listener->data)->clients;
102 if (!clients) clients = mainconfig.clients;
104 rad_assert(clients != NULL);
106 return client_find(clients, ipaddr);
109 static int listen_bind(rad_listen_t *this);
112 * FIXME: have the detail reader use another config "exit when done",
113 * so that it can be used as a one-off tool to update stuff.
117 * Process and reply to a server-status request.
118 * Like rad_authenticate and rad_accounting this should
119 * live in it's own file but it's so small we don't bother.
121 static int rad_status_server(REQUEST *request)
123 switch (request->listener->type) {
124 case RAD_LISTEN_AUTH:
125 request->reply->code = PW_AUTHENTICATION_ACK;
128 * Commented out for now.
137 * Reply with an ACK. We might want to add some more
138 * interesting reply attributes, such as server uptime.
140 t = request->timestamp - start_time;
141 sprintf(reply_msg, "FreeRADIUS STUFF %d day%s, %02d:%02d",
142 (int)(t / 86400), (t / 86400) == 1 ? "" : "s",
143 (int)((t / 3600) % 24), (int)(t / 60) % 60);
145 vp = pairmake("Reply-Message", reply_msg, T_OP_SET);
146 pairadd(&request->reply->vps, vp); /* don't need to check if !vp */
151 case RAD_LISTEN_ACCT:
152 request->reply->code = PW_ACCOUNTING_RESPONSE;
163 static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
166 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, buffer, bufsize);
175 len = strlen(buffer);
177 return len + snprintf(buffer + len, bufsize - len, " port %d",
183 * Parse an authentication or accounting socket.
185 static int common_socket_parse(const char *filename, int lineno,
186 const CONF_SECTION *cs, rad_listen_t *this)
190 lrad_ipaddr_t ipaddr;
191 listen_socket_t *sock = this->data;
192 const char *section_name = NULL;
193 CONF_SECTION *client_cs;
198 ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
199 rcode = cf_item_parse(cs, "ipaddr", PW_TYPE_IPADDR,
200 &ipaddr.ipaddr.ip4addr, NULL);
201 if (rcode < 0) return -1;
203 if (rcode == 0) { /* successfully parsed IPv4 */
206 } else { /* maybe IPv6? */
207 rcode = cf_item_parse(cs, "ipv6addr", PW_TYPE_IPV6ADDR,
208 &ipaddr.ipaddr.ip6addr, NULL);
209 if (rcode < 0) return -1;
212 radlog(L_ERR, "%s[%d]: No address specified in listen section",
216 ipaddr.af = AF_INET6;
219 rcode = cf_item_parse(cs, "port", PW_TYPE_INTEGER,
221 if (rcode < 0) return -1;
223 sock->ipaddr = ipaddr;
224 sock->port = listen_port;
227 * And bind it to the port.
229 if (listen_bind(this) < 0) {
231 radlog(L_CONS|L_ERR, "%s[%d]: Error binding to port for %s port %d",
232 filename, cf_section_lineno(cs),
233 ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
239 * If we can bind to interfaces, do so,
242 if (cf_pair_find(cs, "interface")) {
243 #ifndef SO_BINDTODEVICE
244 radlog(L_CONS|L_ERR, "%s[%d]: System does not support binding to interfaces, delete this line from the configuration file.",
245 filename, cf_section_lineno(cs));
249 const CONF_PAIR *cp = cf_pair_find(cs, "interface");
252 rad_assert(cp != NULL);
253 value = cf_pair_value(cp);
254 rad_assert(value != NULL);
256 strcpy(ifreq.ifr_name, value);
258 if (setsockopt(this->fd, SOL_SOCKET, SO_BINDTODEVICE,
259 (char *)&ifreq, sizeof(ifreq)) < 0) {
260 radlog(L_CONS|L_ERR, "%s[%d]: Failed binding to interface %s: %s",
261 filename, cf_section_lineno(cs),
262 value, strerror(errno));
264 } /* else it worked. */
269 * Look for the name of a section that holds a list
272 rcode = cf_item_parse(cs, "clients", PW_TYPE_STRING_PTR,
273 §ion_name, NULL);
274 if (rcode < 0) return -1; /* bad string */
275 if (rcode > 0) return 0; /* non-existent is OK. */
277 client_cs = cf_section_find(section_name);
280 radlog(L_CONS|L_ERR, "%s[%d]: Failed to find client section %s",
281 filename, cf_section_lineno(cs), section_name);
285 sock->clients = clients_parse_section(filename, client_cs);
286 if (!sock->clients) {
294 * Send an authentication response packet
296 static int auth_socket_send(rad_listen_t *listener, REQUEST *request)
298 rad_assert(request->listener == listener);
299 rad_assert(listener->send == auth_socket_send);
301 return rad_send(request->reply, request->packet, request->secret);
306 * Send an accounting response packet (or not)
308 static int acct_socket_send(rad_listen_t *listener, REQUEST *request)
310 rad_assert(request->listener == listener);
311 rad_assert(listener->send == acct_socket_send);
314 * Accounting reject's are silently dropped.
316 * We do it here to avoid polluting the rest of the
317 * code with this knowledge
319 if (request->reply->code == 0) return 0;
321 return rad_send(request->reply, request->packet, request->secret);
326 * Send a packet to a home server.
328 * FIXME: have different code for proxy auth & acct!
330 static int proxy_socket_send(rad_listen_t *listener, REQUEST *request)
332 listen_socket_t *sock = listener->data;
334 rad_assert(request->proxy_listener == listener);
335 rad_assert(listener->send == proxy_socket_send);
337 request->proxy->src_ipaddr = sock->ipaddr;
338 request->proxy->src_port = sock->port;
340 return rad_send(request->proxy, request->packet,
341 request->home_server->secret);
346 * Check if an incoming request is "ok"
348 * It takes packets, not requests. It sees if the packet looks
349 * OK. If so, it does a number of sanity checks on it.
351 static int auth_socket_recv(rad_listen_t *listener,
352 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
354 RADIUS_PACKET *packet;
355 RAD_REQUEST_FUNP fun = NULL;
359 packet = rad_recv(listener->fd);
361 radlog(L_ERR, "%s", librad_errstr);
365 RAD_SNMP_TYPE_INC(listener, total_requests); /* FIXME: auth specific */
367 if ((client = client_listener_find(listener,
368 &packet->src_ipaddr)) == NULL) {
369 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
371 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
372 inet_ntop(packet->src_ipaddr.af,
373 &packet->src_ipaddr.ipaddr,
374 buffer, sizeof(buffer)),
381 * Some sanity checks, based on the packet code.
383 switch(packet->code) {
384 case PW_AUTHENTICATION_REQUEST:
385 fun = rad_authenticate;
388 case PW_STATUS_SERVER:
389 if (!mainconfig.status_server) {
390 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
391 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
395 fun = rad_status_server;
399 RAD_SNMP_INC(rad_snmp.auth.total_unknown_types);
401 radlog(L_ERR, "Invalid packet code %d sent to authentication port from client %s port %d "
403 packet->code, client->shortname,
404 packet->src_port, packet->id);
408 } /* switch over packet types */
410 if (!received_request(listener, packet, prequest, client)) {
421 * Receive packets from an accounting socket
423 static int acct_socket_recv(rad_listen_t *listener,
424 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
426 RADIUS_PACKET *packet;
427 RAD_REQUEST_FUNP fun = NULL;
431 packet = rad_recv(listener->fd);
433 radlog(L_ERR, "%s", librad_errstr);
437 RAD_SNMP_TYPE_INC(listener, total_requests); /* FIXME: acct-specific */
439 if ((client = client_listener_find(listener,
440 &packet->src_ipaddr)) == NULL) {
441 RAD_SNMP_TYPE_INC(listener, total_invalid_requests);
443 radlog(L_ERR, "Ignoring request from unknown client %s port %d",
444 inet_ntop(packet->src_ipaddr.af,
445 &packet->src_ipaddr.ipaddr,
446 buffer, sizeof(buffer)),
452 switch(packet->code) {
453 case PW_ACCOUNTING_REQUEST:
454 fun = rad_accounting;
457 case PW_STATUS_SERVER:
458 if (!mainconfig.status_server) {
459 RAD_SNMP_TYPE_INC(listener, total_packets_dropped);
460 DEBUG("WARNING: Ignoring Status-Server request due to security configuration");
464 fun = rad_status_server;
469 * FIXME: Update MIB for packet types?
471 radlog(L_ERR, "Invalid packet code %d sent to a accounting port "
472 "from client %s port %d - ID %d : IGNORED",
473 packet->code, client->shortname,
474 packet->src_port, packet->id);
480 * FIXME: Accounting duplicates should be handled
481 * differently than authentication duplicates.
483 if (!received_request(listener, packet, prequest, client)) {
494 * Recieve packets from a proxy socket.
496 static int proxy_socket_recv(rad_listen_t *listener,
497 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
500 RADIUS_PACKET *packet;
501 RAD_REQUEST_FUNP fun = NULL;
504 packet = rad_recv(listener->fd);
506 radlog(L_ERR, "%s", librad_errstr);
511 * FIXME: Client MIB updates?
513 switch(packet->code) {
514 case PW_AUTHENTICATION_ACK:
515 case PW_ACCESS_CHALLENGE:
516 case PW_AUTHENTICATION_REJECT:
517 fun = rad_authenticate;
520 case PW_ACCOUNTING_RESPONSE:
521 fun = rad_accounting;
526 * FIXME: Update MIB for packet types?
528 radlog(L_ERR, "Invalid packet code %d sent to a proxy port "
529 "from home server %s port %d - ID %d : IGNORED",
531 ip_ntoh(&packet->src_ipaddr, buffer, sizeof(buffer)),
532 packet->src_port, packet->id);
537 request = received_proxy_response(packet);
542 rad_assert(fun != NULL);
550 static int client_socket_encode(rad_listen_t *listener, REQUEST *request)
552 if (!request->reply->code) return 0;
554 rad_encode(request->reply, request->packet, request->secret);
555 rad_sign(request->reply, request->packet, request->secret);
561 static int client_socket_decode(rad_listen_t *listener, REQUEST *request)
563 if (rad_verify(request->packet, NULL, request->secret) < 0) {
567 return rad_decode(request->packet, NULL, request->secret);
570 static int proxy_socket_encode(rad_listen_t *listener, REQUEST *request)
572 rad_encode(request->proxy, NULL, request->home_server->secret);
573 rad_sign(request->proxy, NULL, request->home_server->secret);
579 static int proxy_socket_decode(rad_listen_t *listener, REQUEST *request)
581 if (rad_verify(request->proxy_reply, request->proxy,
582 request->home_server->secret) < 0) {
586 return rad_decode(request->proxy_reply, request->proxy,
587 request->home_server->secret);
591 #define STATE_UNOPENED (0)
592 #define STATE_UNLOCKED (1)
593 #define STATE_HEADER (2)
594 #define STATE_READING (3)
595 #define STATE_DONE (4)
596 #define STATE_WAITING (5)
599 * If we're limiting outstanding packets, then mark the response
602 static int detail_send(rad_listen_t *listener, REQUEST *request)
604 listen_detail_t *data = listener->data;
606 rad_assert(request->listener == listener);
607 rad_assert(listener->send == detail_send);
609 if (request->simul_max >= 0) {
610 rad_assert(data->outstanding != NULL);
611 rad_assert(request->simul_max < data->max_outstanding);
613 data->outstanding[request->simul_max] = 0;
621 * Open the detail file..
623 * FIXME: create it, if it's not already there, so that the main
624 * server select() will wake us up if there's anything to read.
626 static int detail_open(rad_listen_t *this)
630 listen_detail_t *data = this->data;
632 rad_assert(data->state == STATE_UNOPENED);
633 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
636 * FIXME: Have "one-shot" configuration, where it
637 * will read the detail file, and exit once it's
640 * FIXME: Try harder to open the detail file.
641 * Maybe sleep for X usecs if it doesn't exist?
645 * Open detail.work first, so we don't lose
646 * accounting packets. It's probably better to
647 * duplicate them than to lose them.
649 * Note that we're not writing to the file, but
650 * we've got to open it for writing in order to
651 * establish the lock, to prevent rlm_detail from
654 this->fd = open(buffer, O_RDWR);
657 * Try reading the detail file. If it
658 * doesn't exist, we can't do anything.
660 * Doing the stat will tell us if the file
661 * exists, even if we don't have permissions
664 if (stat(data->detail, &st) < 0) {
669 * Open it BEFORE we rename it, just to
672 this->fd = open(data->detail, O_RDWR);
674 radlog(L_ERR, "Failed to open %s: %s",
675 data->detail, strerror(errno));
680 * Rename detail to detail.work
682 if (rename(data->detail, buffer) < 0) {
687 } /* else detail.work existed, and we opened it */
689 rad_assert(data->vps == NULL);
691 rad_assert(data->fp == NULL);
692 data->fp = fdopen(this->fd, "r");
694 radlog(L_ERR, "Failed to re-open %s: %s",
695 data->detail, strerror(errno));
699 data->state = STATE_UNLOCKED;
701 data->client_ip.af = AF_UNSPEC;
708 * This is a bad hack, just so complaints have meaningful text.
710 static const RADCLIENT detail_client = {
725 static int detail_recv(rad_listen_t *listener,
726 RAD_REQUEST_FUNP *pfun, REQUEST **prequest)
729 char key[256], value[1024];
730 VALUE_PAIR *vp, **tail;
731 RADIUS_PACKET *packet;
733 listen_detail_t *data = listener->data;
735 if (data->state == STATE_UNOPENED) {
736 rad_assert(listener->fd < 0);
737 if (!detail_open(listener)) return 0;
739 rad_assert(listener->fd >= 0);
742 * Try to lock fd. If we can't, return. If we can,
743 * continue. This means that the server doesn't block
744 * while waiting for the lock to open...
746 if (data->state == STATE_UNLOCKED) {
748 * Note that we do NOT block waiting for the
749 * lock. We've re-named the file above, so we've
750 * already guaranteed that any *new* detail
751 * writer will not be opening this file. The
752 * only purpose of the lock is to catch a race
753 * condition where the execution "ping-pongs"
754 * between radiusd & radrelay.
756 if (rad_lockfd_nonblock(listener->fd, 0) < 0) {
760 * Look for the header
762 data->state = STATE_HEADER;
766 * Catch an out of memory condition which will most likely
769 if (data->state == STATE_DONE) goto alloc_packet;
772 * If we're in another state, then it means that we read
773 * a partial packet, which is bad.
775 rad_assert(data->state == STATE_HEADER);
776 rad_assert(data->vps == NULL);
779 * We read the last packet, and returned it for
780 * processing. We later come back here to shut
781 * everything down, and unlink the file.
783 if (feof(data->fp)) {
784 rad_assert(data->state == STATE_HEADER);
787 * Don't unlink the file until we've received
788 * all of the responses.
790 if (data->max_outstanding > 0) {
793 for (i = 0; i < data->max_outstanding; i++) {
795 * FIXME: close the file?
797 if (data->outstanding[i]) {
798 data->state = STATE_WAITING;
805 rad_assert(data->vps == NULL);
807 snprintf(buffer, sizeof(buffer), "%s.work", data->detail);
809 fclose(data->fp); /* closes listener->fd */
812 data->state = STATE_UNOPENED;
815 * Try to open "detail" again. If we're on a
816 * busy RADIUS server, odds are that it will
819 detail_open(listener);
828 while (fgets(buffer, sizeof(buffer), data->fp)) {
832 if (!strchr(buffer, '\n')) {
833 pairfree(&data->vps);
838 * We've read a header, possibly packet contents,
839 * and are now at the end of the packet.
841 if ((data->state == STATE_READING) &&
842 (buffer[0] == '\n')) {
843 data->state = STATE_DONE;
848 * Look for date/time header, and read VP's if
849 * found. If not, keep reading lines until we
852 if (data->state == STATE_HEADER) {
855 if (sscanf(buffer, "%*s %*s %*d %*d:%*d:%*d %d", &y)) {
856 data->state = STATE_READING;
862 * We have a full "attribute = value" line.
863 * If it doesn't look reasonable, skip it.
865 if (sscanf(buffer, "%255s = %1023s", key, value) != 2) {
870 * Skip non-protocol attributes.
872 if (!strcasecmp(key, "Request-Authenticator")) continue;
875 * Set the original client IP address, based on
876 * what's in the detail file.
878 * Hmm... we don't set the server IP address.
881 if (!strcasecmp(key, "Client-IP-Address")) {
882 data->client_ip.af = AF_INET;
883 ip_hton(value, AF_INET, &data->client_ip);
888 * The original time at which we received the
889 * packet. We need this to properly calculate
892 if (!strcasecmp(key, "Timestamp")) {
893 data->timestamp = atoi(value);
900 * FIXME: do we want to check for non-protocol
901 * attributes like radsqlrelay does?
904 if ((userparse(buffer, &vp) > 0) &&
912 * We got to EOF, If we're in STATE_HEADER, it's OK.
913 * Otherwise it's a problem. In any case, nuke the file
914 * and start over from scratch,
916 if (feof(data->fp)) {
921 * FIXME: Do load management.
925 * If we're not done, then there's a problem. The checks
928 rad_assert(data->state == STATE_DONE);
931 * The packet we read was empty, re-set the state to look
932 * for a header, and don't return anything.
935 data->state = STATE_HEADER;
940 * Allocate the packet. If we fail, it's a serious
945 * If we keep track of the outstanding requests, do so
946 * here. Note that to minimize potential work, we do
947 * so only once the file is opened & locked.
949 if (data->max_outstanding) {
952 for (i = 0; i < data->max_outstanding; i++) {
953 if (!data->outstanding[i]) {
960 * All of the slots are full. Put the LF back,
961 * so select will say that data is ready, and set
962 * the state back to reading. Then, return so
963 * that we don't overload the server.
966 ungetc('\n', data->fp);
967 data->state = STATE_READING;
972 packet = rad_alloc(1);
974 return 0; /* maybe memory will magically free up... */
977 memset(packet, 0, sizeof(*packet));
979 packet->src_ipaddr.af = AF_INET;
980 packet->src_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
981 packet->code = PW_ACCOUNTING_REQUEST;
982 packet->timestamp = time(NULL);
985 * Look for Acct-Delay-Time, and update
986 * based on Acct-Delay-Time += (time(NULL) - timestamp)
988 vp = pairfind(packet->vps, PW_ACCT_DELAY_TIME);
990 vp = paircreate(PW_ACCT_DELAY_TIME, PW_TYPE_INTEGER);
991 rad_assert(vp != NULL);
993 if (data->timestamp != 0) {
994 vp->lvalue += time(NULL) - data->timestamp;
998 * Remember where it came from, so that we don't
999 * proxy it to the place it came from...
1001 if (data->client_ip.af != AF_UNSPEC) {
1002 packet->src_ipaddr = data->client_ip;
1005 vp = pairfind(packet->vps, PW_PACKET_SRC_IP_ADDRESS);
1007 packet->src_ipaddr.af = AF_INET;
1008 packet->src_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
1010 vp = pairfind(packet->vps, PW_PACKET_SRC_IPV6_ADDRESS);
1012 packet->src_ipaddr.af = AF_INET6;
1013 memcpy(&packet->src_ipaddr.ipaddr.ip6addr,
1014 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1018 vp = pairfind(packet->vps, PW_PACKET_DST_IP_ADDRESS);
1020 packet->dst_ipaddr.af = AF_INET;
1021 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = vp->lvalue;
1023 vp = pairfind(packet->vps, PW_PACKET_DST_IPV6_ADDRESS);
1025 packet->dst_ipaddr.af = AF_INET6;
1026 memcpy(&packet->dst_ipaddr.ipaddr.ip6addr,
1027 &vp->vp_ipv6addr, sizeof(vp->vp_ipv6addr));
1032 * We've got to give SOME value for Id & ports, so that
1033 * the packets can be added to the request queue.
1034 * However, we don't want to keep track of used/unused
1035 * id's and ports, as that's a lot of work. This hack
1036 * ensures that (if we have real random numbers), that
1037 * there will be a collision on every 2^(16+15+15+24 - 1)
1038 * packets, on average. That means we can read 2^37
1039 * packets before having a collision, which means it's
1040 * effectively impossible.
1042 packet->id = lrad_rand() & 0xffff;
1043 packet->src_port = 1024 + (lrad_rand() & 0x7fff);
1044 packet->dst_port = 1024 + (lrad_rand() & 0x7fff);
1046 packet->dst_ipaddr.af = AF_INET;
1047 packet->dst_ipaddr.ipaddr.ip4addr.s_addr = htonl((INADDR_LOOPBACK & ~0xffffff) | (lrad_rand() & 0xffffff));
1049 packet->vps = data->vps;
1055 data->state = STATE_HEADER;
1058 * Keep track of free slots, as a hack, in an otherwise
1061 (*prequest)->simul_max = free_slot;
1062 data->outstanding[free_slot] = 1;
1064 *pfun = rad_accounting;
1067 printf("detail_recv: Read packet from %s\n", data->detail);
1068 for (vp = packet->vps; vp; vp = vp->next) {
1070 vp_print(stdout, vp);
1076 * FIXME: many of these checks may not be necessary when
1077 * reading from the detail file.
1079 if (!received_request(listener, packet, prequest, &detail_client)) {
1089 * Free detail-specific stuff.
1091 static void detail_free(rad_listen_t *this)
1093 listen_detail_t *data = this->data;
1096 pairfree(&data->vps);
1097 free(data->outstanding);
1099 if (data->fp != NULL) fclose(data->fp);
1103 static int detail_print(rad_listen_t *this, char *buffer, size_t bufsize)
1105 return snprintf(buffer, bufsize, "%s",
1106 ((listen_detail_t *)(this->data))->detail);
1109 static int detail_encode(rad_listen_t *this, REQUEST *request)
1112 * We never encode responses "sent to" the detail file.
1117 static int detail_decode(rad_listen_t *this, REQUEST *request)
1120 * We never decode responses read from the detail file.
1126 static const CONF_PARSER detail_config[] = {
1127 { "detail", PW_TYPE_STRING_PTR,
1128 offsetof(listen_detail_t, detail), NULL, NULL },
1129 { "max_outstanding", PW_TYPE_INTEGER,
1130 offsetof(listen_detail_t, max_outstanding), NULL, "100" },
1132 { NULL, -1, 0, NULL, NULL } /* end the list */
1137 * Parse a detail section.
1139 static int detail_parse(const char *filename, int lineno,
1140 const CONF_SECTION *cs, rad_listen_t *this)
1143 listen_detail_t *data;
1147 rcode = cf_section_parse(cs, data, detail_config);
1149 radlog(L_ERR, "%s[%d]: Failed parsing listen section",
1154 if (!data->detail) {
1155 radlog(L_ERR, "%s[%d]: No detail file specified in listen section",
1162 data->state = STATE_UNOPENED;
1164 if (data->max_outstanding > 32768) data->max_outstanding = 32768;
1166 if (data->max_outstanding > 0) {
1167 data->outstanding = rad_malloc(sizeof(int) * data->max_outstanding);
1175 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1176 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1178 /* authentication */
1179 { common_socket_parse, NULL,
1180 auth_socket_recv, auth_socket_send,
1181 socket_print, client_socket_encode, client_socket_decode },
1184 { common_socket_parse, NULL,
1185 acct_socket_recv, acct_socket_send,
1186 socket_print, client_socket_encode, client_socket_decode},
1190 proxy_socket_recv, proxy_socket_send,
1191 socket_print, proxy_socket_encode, proxy_socket_decode },
1194 { detail_parse, detail_free,
1195 detail_recv, detail_send,
1196 detail_print, detail_encode, detail_decode }
1202 * Binds a listener to a socket.
1204 static int listen_bind(rad_listen_t *this)
1206 rad_listen_t **last;
1207 listen_socket_t *sock = this->data;
1210 * If the port is zero, then it means the appropriate
1211 * thing from /etc/services.
1213 if (sock->port == 0) {
1214 struct servent *svp;
1216 switch (this->type) {
1217 case RAD_LISTEN_AUTH:
1218 svp = getservbyname ("radius", "udp");
1220 sock->port = ntohs(svp->s_port);
1222 sock->port = PW_AUTH_UDP_PORT;
1226 case RAD_LISTEN_ACCT:
1227 svp = getservbyname ("radacct", "udp");
1229 sock->port = ntohs(svp->s_port);
1231 sock->port = PW_ACCT_UDP_PORT;
1236 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1242 * Find it in the old list, AFTER updating the port. If
1243 * it's there, use that, rather than creating a new
1244 * socket. This allows HUP's to re-use the old sockets,
1245 * which means that packets waiting in the socket queue
1248 for (last = &mainconfig.listen;
1250 last = &((*last)->next)) {
1251 if ((this->type == (*last)->type) &&
1252 (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1253 (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1256 if (sock->ipaddr.af == AF_INET) {
1257 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1258 } else if (sock->ipaddr.af == AF_INET6) {
1259 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1265 this->fd = (*last)->fd;
1272 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1274 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1281 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1282 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1286 flags |= O_NONBLOCK;
1287 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1288 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1299 * Allocate & initialize a new listener.
1301 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1305 this = rad_malloc(sizeof(*this));
1306 memset(this, 0, sizeof(*this));
1309 this->recv = master_listen[this->type].recv;
1310 this->send = master_listen[this->type].send;
1311 this->print = master_listen[this->type].print;
1312 this->encode = master_listen[this->type].encode;
1313 this->decode = master_listen[this->type].decode;
1316 case RAD_LISTEN_AUTH:
1317 case RAD_LISTEN_ACCT:
1318 case RAD_LISTEN_PROXY:
1319 this->data = rad_malloc(sizeof(listen_socket_t));
1320 memset(this->data, 0, sizeof(listen_socket_t));
1323 case RAD_LISTEN_DETAIL:
1324 this->data = rad_malloc(sizeof(listen_detail_t));
1325 memset(this->data, 0, sizeof(listen_detail_t));
1336 * Externally visible function for creating a new proxy LISTENER.
1338 * For now, don't take ipaddr or port.
1340 * Not thread-safe, but all calls to it are protected by the
1341 * proxy mutex in request_list.c
1343 rad_listen_t *proxy_new_listener()
1345 int last_proxy_port, port;
1346 rad_listen_t *this, *tmp, **last;
1347 listen_socket_t *sock, *old;
1349 this = listen_alloc(RAD_LISTEN_PROXY);
1352 * Find an existing proxy socket to copy.
1354 * FIXME: Make it per-realm, or per-home server!
1356 last_proxy_port = 0;
1358 last = &mainconfig.listen;
1359 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1360 if (tmp->type == RAD_LISTEN_PROXY) {
1362 if (sock->port > last_proxy_port) {
1363 last_proxy_port = sock->port + 1;
1365 if (!old) old = sock;
1368 last = &(tmp->next);
1371 if (!old) return NULL; /* This is a serious error. */
1374 * FIXME: find a new IP address to listen on?
1377 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1380 * Keep going until we find an unused port.
1382 for (port = last_proxy_port; port < 64000; port++) {
1384 if (listen_bind(this) == 0) {
1386 * Add the new listener to the list of
1398 static const LRAD_NAME_NUMBER listen_compare[] = {
1399 { "auth", RAD_LISTEN_AUTH },
1400 { "acct", RAD_LISTEN_ACCT },
1401 { "detail", RAD_LISTEN_DETAIL },
1407 * Generate a list of listeners. Takes an input list of
1408 * listeners, too, so we don't close sockets with waiting packets.
1410 int listen_init(const char *filename, rad_listen_t **head)
1414 rad_listen_t **last;
1416 lrad_ipaddr_t server_ipaddr;
1420 * We shouldn't be called with a pre-existing list.
1422 rad_assert(head && (*head == NULL));
1424 if (start_time != 0) start_time = time(NULL);
1427 server_ipaddr.af = AF_UNSPEC;
1430 * If the port is specified on the command-line,
1431 * it over-rides the configuration file.
1433 if (mainconfig.port >= 0) {
1434 auth_port = mainconfig.port;
1436 rcode = cf_item_parse(mainconfig.config, "port",
1437 PW_TYPE_INTEGER, &auth_port,
1438 Stringify(PW_AUTH_UDP_PORT));
1439 if (rcode < 0) return -1; /* error parsing it */
1442 radlog(L_INFO, "WARNING: The directive 'port' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
1446 * If the IP address was configured on the command-line,
1447 * use that as the "bind_address"
1449 if (mainconfig.myip.af != AF_UNSPEC) {
1450 memcpy(&server_ipaddr, &mainconfig.myip,
1451 sizeof(server_ipaddr));
1456 * Else look for bind_address and/or listen sections.
1458 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1459 rcode = cf_item_parse(mainconfig.config, "bind_address",
1461 &server_ipaddr.ipaddr.ip4addr, NULL);
1462 if (rcode < 0) return -1; /* error parsing it */
1464 if (rcode == 0) { /* successfully parsed IPv4 */
1465 listen_socket_t *sock;
1466 server_ipaddr.af = AF_INET;
1468 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'.");
1471 this = listen_alloc(RAD_LISTEN_AUTH);
1474 sock->ipaddr = server_ipaddr;
1475 sock->port = auth_port;
1477 if (listen_bind(this) < 0) {
1480 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1483 auth_port = sock->port; /* may have been updated in listen_bind */
1485 last = &(this->next);
1488 * Open Accounting Socket.
1490 * If we haven't already gotten acct_port from
1491 * /etc/services, then make it auth_port + 1.
1493 this = listen_alloc(RAD_LISTEN_ACCT);
1497 * Create the accounting socket.
1499 * The accounting port is always the
1500 * authentication port + 1
1502 sock->ipaddr = server_ipaddr;
1503 sock->port = auth_port + 1;
1505 if (listen_bind(this) < 0) {
1508 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1513 last = &(this->next);
1515 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1516 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1522 * They specified an IP on the command-line, ignore
1523 * all listen sections.
1525 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1528 * Walk through the "listen" sections, if they exist.
1530 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1532 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1534 char *listen_type, *identity;
1535 int lineno = cf_section_lineno(cs);
1537 listen_type = identity = NULL;
1539 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1541 if (rcode < 0) return -1;
1545 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1551 * See if there's an identity.
1553 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1561 type = lrad_str2int(listen_compare, listen_type,
1564 if (type == RAD_LISTEN_NONE) {
1566 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1572 * Set up cross-type data.
1574 this = listen_alloc(type);
1575 this->identity = identity;
1579 * Call per-type parser.
1581 if (master_listen[type].parse(filename, lineno,
1589 last = &(this->next);
1593 * If we're proxying requests, open the proxy FD.
1594 * Otherwise, don't do anything.
1597 if (mainconfig.proxy_requests == TRUE) {
1599 listen_socket_t *sock = NULL;
1602 * No sockets to receive packets, therefore
1603 * proxying is pointless.
1605 if (!*head) return -1;
1608 * If we previously had proxy sockets, copy them
1609 * to the new config.
1611 if (mainconfig.listen != NULL) {
1612 rad_listen_t *old, *next, **tail;
1614 tail = &mainconfig.listen;
1615 for (old = mainconfig.listen;
1620 if (old->type != RAD_LISTEN_PROXY) {
1621 tail = &((*tail)->next);
1628 last = &(old->next);
1635 * Find the first authentication port,
1638 for (this = *head; this != NULL; this = this->next) {
1639 if (this->type == RAD_LISTEN_AUTH) {
1641 if (server_ipaddr.af == AF_UNSPEC) {
1642 server_ipaddr = sock->ipaddr;
1644 port = sock->port + 2; /* skip acct port */
1649 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1652 * Address is still unspecified, use IPv4.
1654 if (server_ipaddr.af == AF_UNSPEC) {
1655 server_ipaddr.af = AF_INET;
1656 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1659 this = listen_alloc(RAD_LISTEN_PROXY);
1663 * Create the first proxy socket.
1665 sock->ipaddr = server_ipaddr;
1668 * Try to find a proxy port (value doesn't matter)
1670 for (sock->port = port;
1673 if (listen_bind(this) == 0) {
1675 last = &(this->next); /* just in case */
1680 if (sock->port >= 64000) {
1683 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1689 * Sanity check the configuration.
1692 for (this = *head; this != NULL; this = this->next) {
1693 if (((this->type == RAD_LISTEN_ACCT) &&
1694 (rcode == RAD_LISTEN_DETAIL)) ||
1695 ((this->type == RAD_LISTEN_DETAIL) &&
1696 (rcode == RAD_LISTEN_ACCT))) {
1697 rad_assert(0 == 1); /* FIXME: configuration error */
1700 if (rcode != 0) continue;
1702 if ((this->type == RAD_LISTEN_ACCT) ||
1703 (this->type == RAD_LISTEN_DETAIL)) {
1713 * Free a linked list of listeners;
1715 void listen_free(rad_listen_t **head)
1719 if (!head || !*head) return;
1723 rad_listen_t *next = this->next;
1725 free(this->identity);
1728 * Other code may have eaten the FD.
1730 if (this->fd >= 0) close(this->fd);
1732 if (master_listen[this->type].free) {
1733 master_listen[this->type].free(this);