]> git.decadent.org.uk Git - nfs-utils.git/blobdiff - support/export/hostname.c
libexport.a: Allow client_init() to fail instead of exit
[nfs-utils.git] / support / export / hostname.c
index a37d4de44429ea670d450811415cc00b212adb3f..8a23a89b7874466b8146946e5ec65a15b96d57b8 100644 (file)
@@ -5,7 +5,9 @@
  *
  */
 
-#include "config.h"
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 /*
 #define TEST
@@ -16,6 +18,7 @@
 #include <netinet/in.h>
 #include <arpa/inet.h>
 #include <stdlib.h>
+#include <xlog.h>
 #ifdef TEST
 #define xmalloc malloc
 #else
@@ -104,7 +107,7 @@ hostent_dup (struct hostent *hp)
   char **sp;
   struct hostent *cp;
 
-  for (sp = hp->h_aliases; *sp; sp++)
+  for (sp = hp->h_aliases; sp && *sp; sp++)
     {
       num_aliases++;
       len_aliases += align (strlen (*sp) + 1, ALIGNMENT)
@@ -117,7 +120,7 @@ hostent_dup (struct hostent *hp)
       len_addr_list += align (hp->h_length, ALIGNMENT)
                       + sizeof (char *);
     }
-  
+
   cp = (struct hostent *) xmalloc (len_ent + len_name + len_aliases
                                   + len_addr_list);
 
@@ -130,14 +133,14 @@ hostent_dup (struct hostent *hp)
   cp->h_aliases = (char **) &(((char *) cp) [pos]);
   pos += num_aliases * sizeof (char *);
   for (sp = hp->h_aliases, i = 0; i < num_aliases; i++, sp++)
-    if (*sp)
+    if (sp && *sp)
       {
        cp->h_aliases [i] = (char *) &(((char *) cp) [pos]);
        strcpy (cp->h_aliases [i], *sp);
        pos += align (strlen (*sp) + 1, ALIGNMENT);
       }
     else
-      cp->h_aliases [i] = *sp;
+      cp->h_aliases [i] = NULL;
 
   pos = len_ent + len_name + len_aliases;
   cp->h_addr_list = (char **) &(((char *) cp) [pos]);
@@ -216,6 +219,56 @@ matchhostname (const char *h1, const char *h2)
   return status;
 }
 
+
+/* Map IP to hostname, and then map back to addr to make sure it is a
+ * reliable hostname
+ */
+struct hostent *
+get_reliable_hostbyaddr(const char *addr, int len, int type)
+{
+       struct hostent *hp = NULL;
+
+       struct hostent *reverse;
+       struct hostent *forward;
+       char **sp;
+
+       reverse = gethostbyaddr (addr, len, type);
+       if (!reverse)
+               return NULL;
+
+       /* must make sure the hostent is authorative. */
+
+       reverse = hostent_dup (reverse);
+       forward = gethostbyname (reverse->h_name);
+
+       if (forward) {
+               /* now make sure the "addr" is in the list */
+               for (sp = forward->h_addr_list ; *sp ; sp++) {
+                       if (memcmp (*sp, addr, forward->h_length) == 0)
+                               break;
+               }
+
+               if (*sp) {
+                       /* it's valid */
+                       hp = hostent_dup (forward);
+               }
+               else {
+                       /* it was a FAKE */
+                       xlog (L_WARNING, "Fake hostname %s for %s - forward lookup doesn't match reverse",
+                             reverse->h_name, inet_ntoa(*(struct in_addr*)addr));
+               }
+       }
+       else {
+               /* never heard of it. misconfigured DNS? */
+               xlog (L_WARNING, "Fake hostname %s for %s - forward lookup doesn't exist",
+                     reverse->h_name, inet_ntoa(*(struct in_addr*)addr));
+       }
+
+       free (reverse);
+       return hp;
+}
+
+
 #ifdef TEST
 void
 print_host (struct hostent *hp)
@@ -224,7 +277,7 @@ print_host (struct hostent *hp)
 
   if (hp)
     {
-      printf ("official hostname: %s\n", hp->h_name); 
+      printf ("official hostname: %s\n", hp->h_name);
       printf ("aliases:\n");
       for (sp = hp->h_aliases; *sp; sp++)
        printf ("  %s\n", *sp);