X-Git-Url: https://git.decadent.org.uk/gitweb/?p=nfs-utils.git;a=blobdiff_plain;f=support%2Fexport%2Fhostname.c;h=3957d80069c5510b9faa6e60a4c876d00573ace5;hp=299fe9937b4d03606dcbc14a749cae3bd7e057a7;hb=a980156c122e975cc185a6c41ef705f166a5765f;hpb=282284fac3c3fe20187f892e3ce15ac319c8acb7 diff --git a/support/export/hostname.c b/support/export/hostname.c index 299fe99..3957d80 100644 --- a/support/export/hostname.c +++ b/support/export/hostname.c @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef TEST #define xmalloc malloc #else @@ -216,6 +217,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)