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(UNUSED 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(UNUSED 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(UNUSED 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(UNUSED 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(UNUSED rad_listen_t *this, UNUSED REQUEST *request)
1112 * We never encode responses "sent to" the detail file.
1117 static int detail_decode(UNUSED rad_listen_t *this, UNUSED 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);
1177 static int radius_snmp_recv(rad_listen_t *listener,
1178 UNUSED RAD_REQUEST_FUNP *pfun,
1179 UNUSED REQUEST **prequest)
1181 if (!mainconfig.do_snmp) return 0;
1183 if ((rad_snmp.smux_fd >= 0) &&
1184 (rad_snmp.smux_event == SMUX_READ)) {
1189 * If we've got to re-connect, then do so now,
1190 * before calling select again.
1192 if (rad_snmp.smux_event == SMUX_CONNECT) {
1197 * Reset this every time, as the smux connect may have
1198 * opened a new socket.
1200 listener->fd = rad_snmp.smux_fd;
1206 static int radius_snmp_print(rad_listen_t *this, char *buffer, size_t bufsize)
1208 return snprintf(buffer, bufsize, "SMUX with OID .1.3.6.1.4.1.3317.1.3.1");
1213 static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
1214 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_NONE */
1216 /* authentication */
1217 { common_socket_parse, NULL,
1218 auth_socket_recv, auth_socket_send,
1219 socket_print, client_socket_encode, client_socket_decode },
1222 { common_socket_parse, NULL,
1223 acct_socket_recv, acct_socket_send,
1224 socket_print, client_socket_encode, client_socket_decode},
1228 proxy_socket_recv, proxy_socket_send,
1229 socket_print, proxy_socket_encode, proxy_socket_decode },
1232 { detail_parse, detail_free,
1233 detail_recv, detail_send,
1234 detail_print, detail_encode, detail_decode },
1236 { NULL, NULL, NULL, NULL, NULL, NULL, NULL}, /* RAD_LISTEN_SNMP */
1242 * Binds a listener to a socket.
1244 static int listen_bind(rad_listen_t *this)
1246 rad_listen_t **last;
1247 listen_socket_t *sock = this->data;
1250 * If the port is zero, then it means the appropriate
1251 * thing from /etc/services.
1253 if (sock->port == 0) {
1254 struct servent *svp;
1256 switch (this->type) {
1257 case RAD_LISTEN_AUTH:
1258 svp = getservbyname ("radius", "udp");
1260 sock->port = ntohs(svp->s_port);
1262 sock->port = PW_AUTH_UDP_PORT;
1266 case RAD_LISTEN_ACCT:
1267 svp = getservbyname ("radacct", "udp");
1269 sock->port = ntohs(svp->s_port);
1271 sock->port = PW_ACCT_UDP_PORT;
1276 radlog(L_ERR|L_CONS, "ERROR: Non-fatal internal sanity check failed in bind.");
1282 * Find it in the old list, AFTER updating the port. If
1283 * it's there, use that, rather than creating a new
1284 * socket. This allows HUP's to re-use the old sockets,
1285 * which means that packets waiting in the socket queue
1288 for (last = &mainconfig.listen;
1290 last = &((*last)->next)) {
1291 if ((this->type == (*last)->type) &&
1292 (sock->port == ((listen_socket_t *)((*last)->data))->port) &&
1293 (sock->ipaddr.af == ((listen_socket_t *)((*last)->data))->ipaddr.af)) {
1296 if (sock->ipaddr.af == AF_INET) {
1297 equal = (sock->ipaddr.ipaddr.ip4addr.s_addr == ((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip4addr.s_addr);
1298 } else if (sock->ipaddr.af == AF_INET6) {
1299 equal = IN6_ARE_ADDR_EQUAL(&(sock->ipaddr.ipaddr.ip6addr), &(((listen_socket_t *)((*last)->data))->ipaddr.ipaddr.ip6addr));
1305 this->fd = (*last)->fd;
1312 this->fd = lrad_socket(&sock->ipaddr, sock->port);
1314 radlog(L_ERR|L_CONS, "ERROR: Failed to open socket: %s",
1321 if ((flags = fcntl(this->fd, F_GETFL, NULL)) < 0) {
1322 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1326 flags |= O_NONBLOCK;
1327 if( fcntl(this->fd, F_SETFL, flags) < 0) {
1328 radlog(L_ERR, "Failure in fcntl: %s)\n", strerror(errno));
1339 * Allocate & initialize a new listener.
1341 static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
1345 this = rad_malloc(sizeof(*this));
1346 memset(this, 0, sizeof(*this));
1349 this->recv = master_listen[this->type].recv;
1350 this->send = master_listen[this->type].send;
1351 this->print = master_listen[this->type].print;
1352 this->encode = master_listen[this->type].encode;
1353 this->decode = master_listen[this->type].decode;
1356 case RAD_LISTEN_AUTH:
1357 case RAD_LISTEN_ACCT:
1358 case RAD_LISTEN_PROXY:
1359 this->data = rad_malloc(sizeof(listen_socket_t));
1360 memset(this->data, 0, sizeof(listen_socket_t));
1363 case RAD_LISTEN_DETAIL:
1364 this->data = rad_malloc(sizeof(listen_detail_t));
1365 memset(this->data, 0, sizeof(listen_detail_t));
1376 * Externally visible function for creating a new proxy LISTENER.
1378 * For now, don't take ipaddr or port.
1380 * Not thread-safe, but all calls to it are protected by the
1381 * proxy mutex in request_list.c
1383 rad_listen_t *proxy_new_listener()
1385 int last_proxy_port, port;
1386 rad_listen_t *this, *tmp, **last;
1387 listen_socket_t *sock, *old;
1389 this = listen_alloc(RAD_LISTEN_PROXY);
1392 * Find an existing proxy socket to copy.
1394 * FIXME: Make it per-realm, or per-home server!
1396 last_proxy_port = 0;
1398 last = &mainconfig.listen;
1399 for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
1400 if (tmp->type == RAD_LISTEN_PROXY) {
1402 if (sock->port > last_proxy_port) {
1403 last_proxy_port = sock->port + 1;
1405 if (!old) old = sock;
1408 last = &(tmp->next);
1411 if (!old) return NULL; /* This is a serious error. */
1414 * FIXME: find a new IP address to listen on?
1417 memcpy(&sock->ipaddr, &old->ipaddr, sizeof(sock->ipaddr));
1420 * Keep going until we find an unused port.
1422 for (port = last_proxy_port; port < 64000; port++) {
1424 if (listen_bind(this) == 0) {
1426 * Add the new listener to the list of
1438 static const LRAD_NAME_NUMBER listen_compare[] = {
1439 { "auth", RAD_LISTEN_AUTH },
1440 { "acct", RAD_LISTEN_ACCT },
1441 { "detail", RAD_LISTEN_DETAIL },
1447 * Generate a list of listeners. Takes an input list of
1448 * listeners, too, so we don't close sockets with waiting packets.
1450 int listen_init(const char *filename, rad_listen_t **head)
1454 rad_listen_t **last;
1456 lrad_ipaddr_t server_ipaddr;
1460 * We shouldn't be called with a pre-existing list.
1462 rad_assert(head && (*head == NULL));
1464 if (start_time != 0) start_time = time(NULL);
1467 server_ipaddr.af = AF_UNSPEC;
1470 * If the port is specified on the command-line,
1471 * it over-rides the configuration file.
1473 if (mainconfig.port >= 0) {
1474 auth_port = mainconfig.port;
1476 rcode = cf_item_parse(mainconfig.config, "port",
1477 PW_TYPE_INTEGER, &auth_port,
1478 Stringify(PW_AUTH_UDP_PORT));
1479 if (rcode < 0) return -1; /* error parsing it */
1482 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'.");
1486 * If the IP address was configured on the command-line,
1487 * use that as the "bind_address"
1489 if (mainconfig.myip.af != AF_UNSPEC) {
1490 memcpy(&server_ipaddr, &mainconfig.myip,
1491 sizeof(server_ipaddr));
1496 * Else look for bind_address and/or listen sections.
1498 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_NONE);
1499 rcode = cf_item_parse(mainconfig.config, "bind_address",
1501 &server_ipaddr.ipaddr.ip4addr, NULL);
1502 if (rcode < 0) return -1; /* error parsing it */
1504 if (rcode == 0) { /* successfully parsed IPv4 */
1505 listen_socket_t *sock;
1506 server_ipaddr.af = AF_INET;
1508 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'.");
1511 this = listen_alloc(RAD_LISTEN_AUTH);
1514 sock->ipaddr = server_ipaddr;
1515 sock->port = auth_port;
1517 if (listen_bind(this) < 0) {
1520 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the authentication port %d", sock->port);
1523 auth_port = sock->port; /* may have been updated in listen_bind */
1525 last = &(this->next);
1528 * Open Accounting Socket.
1530 * If we haven't already gotten acct_port from
1531 * /etc/services, then make it auth_port + 1.
1533 this = listen_alloc(RAD_LISTEN_ACCT);
1537 * Create the accounting socket.
1539 * The accounting port is always the
1540 * authentication port + 1
1542 sock->ipaddr = server_ipaddr;
1543 sock->port = auth_port + 1;
1545 if (listen_bind(this) < 0) {
1548 radlog(L_CONS|L_ERR, "There appears to be another RADIUS server running on the accounting port %d", sock->port);
1553 last = &(this->next);
1555 } else if (mainconfig.port > 0) { /* no bind address, but a port */
1556 radlog(L_CONS|L_ERR, "The command-line says \"-p %d\", but there is no associated IP address to use",
1562 * They specified an IP on the command-line, ignore
1563 * all listen sections.
1565 if (mainconfig.myip.af != AF_UNSPEC) goto do_proxy;
1568 * Walk through the "listen" sections, if they exist.
1570 for (cs = cf_subsection_find_next(mainconfig.config, NULL, "listen");
1572 cs = cf_subsection_find_next(mainconfig.config, cs, "listen")) {
1574 char *listen_type, *identity;
1575 int lineno = cf_section_lineno(cs);
1577 listen_type = identity = NULL;
1579 rcode = cf_item_parse(cs, "type", PW_TYPE_STRING_PTR,
1581 if (rcode < 0) return -1;
1585 radlog(L_ERR, "%s[%d]: No type specified in listen section",
1591 * See if there's an identity.
1593 rcode = cf_item_parse(cs, "identity", PW_TYPE_STRING_PTR,
1601 type = lrad_str2int(listen_compare, listen_type,
1604 if (type == RAD_LISTEN_NONE) {
1606 radlog(L_CONS|L_ERR, "%s[%d]: Invalid type in listen section.",
1612 * Set up cross-type data.
1614 this = listen_alloc(type);
1615 this->identity = identity;
1619 * Call per-type parser.
1621 if (master_listen[type].parse(filename, lineno,
1629 last = &(this->next);
1633 * If we're proxying requests, open the proxy FD.
1634 * Otherwise, don't do anything.
1637 if (mainconfig.proxy_requests == TRUE) {
1639 listen_socket_t *sock = NULL;
1642 * No sockets to receive packets, therefore
1643 * proxying is pointless.
1645 if (!*head) return -1;
1648 * If we previously had proxy sockets, copy them
1649 * to the new config.
1651 if (mainconfig.listen != NULL) {
1652 rad_listen_t *old, *next, **tail;
1654 tail = &mainconfig.listen;
1655 for (old = mainconfig.listen;
1660 if (old->type != RAD_LISTEN_PROXY) {
1661 tail = &((*tail)->next);
1668 last = &(old->next);
1675 * Find the first authentication port,
1678 for (this = *head; this != NULL; this = this->next) {
1679 if (this->type == RAD_LISTEN_AUTH) {
1681 if (server_ipaddr.af == AF_UNSPEC) {
1682 server_ipaddr = sock->ipaddr;
1684 port = sock->port + 2; /* skip acct port */
1689 if (port < 0) port = 1024 + (lrad_rand() & 0x1ff);
1692 * Address is still unspecified, use IPv4.
1694 if (server_ipaddr.af == AF_UNSPEC) {
1695 server_ipaddr.af = AF_INET;
1696 server_ipaddr.ipaddr.ip4addr.s_addr = htonl(INADDR_ANY);
1699 this = listen_alloc(RAD_LISTEN_PROXY);
1703 * Create the first proxy socket.
1705 sock->ipaddr = server_ipaddr;
1708 * Try to find a proxy port (value doesn't matter)
1710 for (sock->port = port;
1713 if (listen_bind(this) == 0) {
1715 last = &(this->next); /* just in case */
1720 if (sock->port >= 64000) {
1723 radlog(L_ERR|L_CONS, "Failed to open socket for proxying");
1729 if (mainconfig.do_snmp) {
1732 this = rad_malloc(sizeof(*this));
1733 memset(this, 0, sizeof(*this));
1735 this->type = RAD_LISTEN_SNMP;
1736 this->fd = rad_snmp.smux_fd;
1738 this->recv = radius_snmp_recv;
1739 this->print = radius_snmp_print;
1742 last = &(this->next);
1747 * Sanity check the configuration.
1750 for (this = *head; this != NULL; this = this->next) {
1751 if (((this->type == RAD_LISTEN_ACCT) &&
1752 (rcode == RAD_LISTEN_DETAIL)) ||
1753 ((this->type == RAD_LISTEN_DETAIL) &&
1754 (rcode == RAD_LISTEN_ACCT))) {
1755 rad_assert(0 == 1); /* FIXME: configuration error */
1758 if (rcode != 0) continue;
1760 if ((this->type == RAD_LISTEN_ACCT) ||
1761 (this->type == RAD_LISTEN_DETAIL)) {
1771 * Free a linked list of listeners;
1773 void listen_free(rad_listen_t **head)
1777 if (!head || !*head) return;
1781 rad_listen_t *next = this->next;
1783 free(this->identity);
1786 * Other code may have eaten the FD.
1788 if (this->fd >= 0) close(this->fd);
1790 if (master_listen[this->type].free) {
1791 master_listen[this->type].free(this);