Made listener_print be CONST
[freeradius.git] / src / main / listen.c
index 0c4957d..7c603e8 100644 (file)
@@ -72,9 +72,7 @@ typedef struct listen_socket_t {
         */
        fr_ipaddr_t     ipaddr;
        int             port;
-#ifdef SO_BINDTODEVICE
        const char              *interface;
-#endif
        RADCLIENT_LIST  *clients;
 } listen_socket_t;
 
@@ -191,7 +189,8 @@ RADCLIENT *client_listener_find(const rad_listen_t *listener,
                 *      can be defined.
                 */
                rad_assert(client->dynamic == 0);
-       } else {
+
+       } else if (!client->dynamic && client->rate_limit) {
                /*
                 *      The IP is unknown, so we've found an enclosing
                 *      network.  Enable DoS protection.  We only
@@ -375,7 +374,7 @@ static int rad_status_server(REQUEST *request)
 }
 
 
-static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
+static int socket_print(const rad_listen_t *this, char *buffer, size_t bufsize)
 {
        size_t len;
        listen_socket_t *sock = this->data;
@@ -432,12 +431,10 @@ static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
 
        ADDSTRING(name);
 
-#ifdef SO_BINDTODEVICE
        if (sock->interface) {
                ADDSTRING(" interface ");
                ADDSTRING(sock->interface);
        }
-#endif
 
        ADDSTRING(" address ");
        
@@ -464,6 +461,8 @@ static int socket_print(rad_listen_t *this, char *buffer, size_t bufsize)
        return 1;
 }
 
+extern int check_config;       /* radiusd.c */
+
 
 /*
  *     Parse an authentication or accounting socket.
@@ -514,16 +513,24 @@ static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
        sock->ipaddr = ipaddr;
        sock->port = listen_port;
 
+       if (check_config) {
+               if (home_server_find(&sock->ipaddr, sock->port)) {
+                               char buffer[128];
+                               
+                               DEBUG("ERROR: We have been asked to listen on %s port %d, which is also listed as a home server.  This can create a proxy loop.",
+                                     ip_ntoh(&sock->ipaddr, buffer, sizeof(buffer)),
+                                     sock->port);
+                               return -1;
+               }
+
+               return 0;       /* don't do anything */
+       }
+
        /*
         *      If we can bind to interfaces, do so,
         *      else don't.
         */
        if (cf_pair_find(cs, "interface")) {
-#ifndef SO_BINDTODEVICE
-               cf_log_err(cf_sectiontoitem(cs),
-                          "System does not support binding to interfaces.  Delete this line from the configuration file.");
-               return -1;
-#else
                const char *value;
                CONF_PAIR *cp = cf_pair_find(cs, "interface");
 
@@ -535,7 +542,6 @@ static int common_socket_parse(CONF_SECTION *cs, rad_listen_t *this)
                        return -1;
                }
                sock->interface = value;
-#endif
        }
 
        /*
@@ -1360,7 +1366,7 @@ static const rad_listen_master_t master_listen[RAD_LISTEN_MAX] = {
 
 #ifdef WITH_COMMAND_SOCKET
        /* TCP command socket */
-       { command_socket_parse, NULL,
+       { command_socket_parse, command_socket_free,
          command_domain_accept, command_domain_send,
          command_socket_print, command_socket_encode, command_socket_decode },
 #endif
@@ -1428,12 +1434,17 @@ static int listen_bind(rad_listen_t *this)
 
 #ifdef WITH_COA
                case RAD_LISTEN_COA:
-                       sock->port = PW_COA_UDP_PORT;
+                       svp = getservbyname ("radius-dynauth", "udp");
+                       if (svp != NULL) {
+                               sock->port = ntohs(svp->s_port);
+                       } else {
+                               sock->port = PW_COA_UDP_PORT;
+                       }
                        break;
 #endif
 
                default:
-                       radlog(L_ERR, "ERROR: Non-fatal internal sanity check failed in bind.");
+                       DEBUG("WARNING: Internal sanity check failed in binding to socket.  Ignoring problem.");
                        return -1;
                }
        }
@@ -1447,11 +1458,11 @@ static int listen_bind(rad_listen_t *this)
                return -1;
        }
                
-#ifdef SO_BINDTODEVICE
        /*
         *      Bind to a device BEFORE touching IP addresses.
         */
        if (sock->interface) {
+#ifdef SO_BINDTODEVICE
                struct ifreq ifreq;
                strcpy(ifreq.ifr_name, sock->interface);
 
@@ -1465,8 +1476,43 @@ static int listen_bind(rad_listen_t *this)
                               sock->interface, strerror(errno));
                        return -1;
                } /* else it worked. */
-       }
+#else
+#ifdef HAVE_STRUCT_SOCKADDR_IN6
+#ifdef HAVE_NET_IF_H
+               /*
+                *      Odds are that any system supporting "bind to
+                *      device" also supports IPv6, so this next bit
+                *      isn't necessary.  But it's here for
+                *      completeness.
+                *
+                *      If we're doing IPv6, and the scope hasn't yet
+                *      been defined, set the scope to the scope of
+                *      the interface.
+                */
+               if (sock->ipaddr.af == AF_INET6) {
+                       if (sock->ipaddr.scope == 0) {
+                               sock->ipaddr.scope = if_nametoindex(sock->interface);
+                               if (sock->ipaddr.scope == 0) {
+                                       close(this->fd);
+                                       radlog(L_ERR, "Failed finding interface %s: %s",
+                                              sock->interface, strerror(errno));
+                                       return -1;
+                               }
+                       } /* else scope was defined: we're OK. */
+               } else
 #endif
+#endif
+                               /*
+                                *      IPv4: no link local addresses,
+                                *      and no bind to device.
+                                */
+               {
+                       close(this->fd);
+                       radlog(L_ERR, "Failed binding to interface %s: \"bind to device\" is unsupported", sock->interface);
+                       return -1;
+               }
+#endif
+       }
 
 #ifdef WITH_UDPFROMTO
        /*
@@ -1670,21 +1716,19 @@ static rad_listen_t *listen_alloc(RAD_LISTEN_TYPE type)
  */
 rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
 {
-       int last_proxy_port, port;
        rad_listen_t *this, *tmp, **last;
        listen_socket_t *sock, *old;
 
        /*
         *      Find an existing proxy socket to copy.
         */
-       last_proxy_port = 0;
        old = NULL;
        last = &mainconfig.listen;
        for (tmp = mainconfig.listen; tmp != NULL; tmp = tmp->next) {
                /*
                 *      Not proxy, ignore it.
                 */
-               if (tmp->type != RAD_LISTEN_PROXY) continue;
+               if (tmp->type != RAD_LISTEN_PROXY) goto next;
 
                sock = tmp->data;
 
@@ -1697,17 +1741,18 @@ rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
                if ((ipaddr->af != AF_UNSPEC) &&
                    (fr_ipaddr_cmp(&sock->ipaddr, ipaddr) != 0)) {
                        if (exists) return tmp;
-                       continue;
+                       goto next;
                }
                
-               if (sock->port > last_proxy_port) {
-                       last_proxy_port = sock->port + 1;
-               }
                if (!old) old = sock;
 
+       next:
                last = &(tmp->next);
        }
 
+       this = listen_alloc(RAD_LISTEN_PROXY);
+       sock = this->data;
+
        if (!old) {
                /*
                 *      The socket MUST already exist if we're binding
@@ -1716,33 +1761,23 @@ rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
                 *      If we're initializing the server, it's OK for the
                 *      socket to NOT exist.
                 */
-               if (!exists) return NULL;
-
-               this = listen_alloc(RAD_LISTEN_PROXY);
-
-               sock = this->data;
-               sock->ipaddr = *ipaddr;
+               if (!exists) {
+                       DEBUG("WARNING: No previous template for proxy socket.  Source IP address may be chosen by the OS");
+               }
 
+               if (ipaddr->af != AF_UNSPEC) {
+                       sock->ipaddr = *ipaddr;
+               } else {
+                       memset(&sock->ipaddr, 0, sizeof(sock->ipaddr));
+                       sock->ipaddr.af = AF_INET; /* Oh well */
+               }
        } else {
-               this = listen_alloc(RAD_LISTEN_PROXY);
-               
-               sock = this->data;
                sock->ipaddr = old->ipaddr;
        }
 
-       /*
-        *      Keep going until we find an unused port.
-        */
-       for (port = last_proxy_port; port < 64000; port++) {
-               int rcode;
-
-               sock->port = port;
+       sock->port = 0;
 
-               rcode = listen_bind(this);
-               if (rcode < 0) {
-                       continue;
-               }
-               
+       if (listen_bind(this) >= 0) {
                /*
                 *      Add the new listener to the list of
                 *      listeners.
@@ -1751,6 +1786,7 @@ rad_listen_t *proxy_new_listener(fr_ipaddr_t *ipaddr, int exists)
                return this;
        }
 
+       DEBUG("Failed binding to new proxy socket");
        listen_free(&this);
        return NULL;
 }
@@ -1907,7 +1943,7 @@ int listen_init(CONF_SECTION *config, rad_listen_t **head)
                listen_socket_t *sock;
                server_ipaddr.af = AF_INET;
 
-               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'.");
+               radlog(L_INFO, "WARNING: The directive 'bind_address' is deprecated, and will be removed in future versions of FreeRADIUS. Please edit the configuration files to use the directive 'listen'.");
 
        bind_it:
 #ifdef WITH_VMPS
@@ -2095,17 +2131,20 @@ int listen_init(CONF_SECTION *config, rad_listen_t **head)
         *      Otherwise, don't do anything.
         */
  do_proxy:
+       /*
+        *      No sockets to receive packets, this is an error.
+        *      proxying is pointless.
+        */
+       if (!*head) {
+               radlog(L_ERR, "The server is not configured to listen on any ports.  Cannot start.");
+               return -1;
+       }
+
 #ifdef WITH_PROXY
        if (mainconfig.proxy_requests == TRUE) {
                int             port = -1;
                listen_socket_t *sock = NULL;
 
-               /*
-                *      No sockets to receive packets, therefore
-                *      proxying is pointless.
-                */
-               if (!*head) return -1;
-
                if (defined_proxy) goto check_home_servers;
 
                /*
@@ -2115,6 +2154,19 @@ int listen_init(CONF_SECTION *config, rad_listen_t **head)
                for (this = *head; this != NULL; this = this->next) {
                        if (this->type == RAD_LISTEN_AUTH) {
                                sock = this->data;
+
+                               /*
+                                *      We shouldn't proxy on loopback.
+                                */
+                               if ((sock->ipaddr.af == AF_INET) &&
+                                   (sock->ipaddr.ipaddr.ip4addr.s_addr == htonl(INADDR_LOOPBACK))) continue;
+
+
+#ifdef HAVE_STRUCT_SOCKADDR_IN6
+                               if ((sock->ipaddr.af == AF_INET6) &&
+                                   (IN6_IS_ADDR_LINKLOCAL(&sock->ipaddr.ipaddr.ip6addr))) continue;
+#endif
+
                                if (server_ipaddr.af == AF_UNSPEC) {
                                        server_ipaddr = sock->ipaddr;
                                }
@@ -2176,7 +2228,7 @@ int listen_init(CONF_SECTION *config, rad_listen_t **head)
                 *      on their src_ipaddr.
                 */
        check_home_servers:
-               if (home_server_create_listeners(*head) != 0) return -1;
+               if (home_server_create_listeners() != 0) return -1;
        }
 #endif