compare_addrinfo_sockaddr: Add code to compare AF_INET6 addresses.
[rsync/rsync.git] / clientname.c
index aedf05e..f83eb8d 100644 (file)
@@ -99,7 +99,10 @@ char *client_name(int fd)
 
 
 /**
- * Get the sockaddr for the client.  
+ * Get the sockaddr for the client.
+ *
+ * If it comes in as an ipv4 address mapped into IPv6 format then we
+ * convert it back to a regular IPv4.
  **/
 void client_sockaddr(int fd,
                     struct sockaddr_storage *ss,
@@ -172,9 +175,49 @@ int lookup_name(int fd, const struct sockaddr_storage *ss,
 
 
 
-/* Do a forward lookup on name_buf and make sure it corresponds to ss
- * -- otherwise we may be being spoofed.  If we suspect we are, then
- * we don't abort the connection but just emit a warning. */
+/**
+ * Compare an addrinfo from the resolver to a sockinfo.
+ *
+ * Like strcmp, returns 0 for identical.
+ **/
+int compare_addrinfo_sockaddr(const struct addrinfo *ai,
+                             const struct sockaddr_storage *ss)
+{
+       int ss_family = get_sockaddr_family(ss);
+       const char fn[] = "compare_addrinfo_sockaddr";
+       size_t valid_len;
+                     
+       if (ai->ai_family != ss_family) {
+               rprintf(FERROR,
+                       "%s: response family %d != %d\n",
+                       fn, ai->ai_family, ss_family);
+               return 1;
+       }
+
+       switch (ss_family) {
+       case AF_INET:
+               valid_len = sizeof (struct sockaddr_in);
+               break;
+#ifdef INET6
+       case AF_INET6:
+               valid_len = sizeof (struct sockaddr_in6);
+               break;
+#endif
+       default:
+               /* Don't know what to do! */
+               return 1;
+       }
+
+       return memcmp(ss, ai->ai_addr, valid_len);
+}
+
+
+/**
+ * Do a forward lookup on @p name_buf and make sure it corresponds to
+ * @p ss -- otherwise we may be being spoofed.  If we suspect we are,
+ * then we don't abort the connection but just emit a warning, and
+ * change @p name_buf to be "UNKNOWN".
+ **/
 int check_name(int fd,
               const struct sockaddr_storage *ss,
               socklen_t ss_len,
@@ -192,38 +235,18 @@ int check_name(int fd,
        error = getaddrinfo(name_buf, port_buf, &hints, &res0);
        if (error) {
                rprintf(FERROR,
-                       RSYNC_NAME ": forward name lookup for %s:%s failed: %s\n",
-                       name_buf, port_buf,
-                       gai_strerror(error));
+                       RSYNC_NAME ": forward name lookup for %s failed: %s\n",
+                       name_buf, gai_strerror(error));
                strcpy(name_buf, default_name);
                return error;
        }
 
 
-       /* We expect that one of the results will be the same as ss. */
+       /* Given all these results, we expect that one of them will be
+        * the same as ss.  The comparison is a bit complicated. */
        for (res = res0; res; res = res->ai_next) {
-               if (res->ai_family != ss_family) {
-                       rprintf(FERROR,
-                               "check_name: response family %d != %d\n",
-                               res->ai_family, ss_family);
-                       continue;
-               }
-               if (res->ai_addrlen != ss_len) {
-                       rprintf(FERROR,
-                               "check_name: addrlen %d != %d\n",
-                               res->ai_addrlen, ss_len);
-                       continue;
-               }
-               if (memcmp(res->ai_addr, ss, res->ai_addrlen) == 0) {
-                       rprintf(FERROR,
-                               "check_name: %d bytes of address identical\n",
-                               res->ai_addrlen);
-                       break;
-               } else{
-                       rprintf(FERROR,
-                               "check_name: %d bytes of address NOT identical\n",
-                               res->ai_addrlen);
-               }
+               if (!compare_addrinfo_sockaddr(res, ss))
+                       break;  /* OK, identical */
        }
 
        if (!res0) {
@@ -234,8 +257,7 @@ int check_name(int fd,
                        "spoofed address?\n",
                        name_buf);
                strcpy(name_buf, default_name);
-       }
-       if (res == NULL) {
+       } else if (res == NULL) {
                /* We hit the end of the list without finding an
                 * address that was the same as ss. */
                rprintf(FERROR, RSYNC_NAME