--- get_host_realm.c.orig 2007-03-24 17:25:09.000000000 -0400 +++ get_host_realm.c 2007-03-24 17:25:48.000000000 -0400 @@ -210,6 +210,78 @@ return KRB5_ERR_HOST_REALM_UNKNOWN; } +static int +dns_get_default_fqdn(unsigned char *dst, size_t dn) +{ + unsigned char buf[HOST_NAME_MAX]; + unsigned char *name = buf, *dot, *dlim = dst + dn; + + if (dst == NULL || dn < 1) { + errno = EINVAL; + return -1; + } + dlim--; /* ensure room for zero term */ + + if (gethostname(buf, sizeof(buf)) == -1) { + return -1; + } + + dot = strchr(name, '.'); + if (dot == NULL || dot[1] == '\0') { + /* If name doesn't have components (trailing dot doesn't + * count), then try harder + */ + struct hostent *he; + + he = gethostbyname(buf); + if (he == NULL) { + return -1; + } + name = he->h_name; + + dot = strchr(name, '.'); + if (dot == NULL || dot[1] == '\0') { + /* Still no components, try harder still + */ + int ai; + + name = NULL; + for (ai = 0; he->h_aliases[ai]; ai++) { + name = he->h_aliases[ai]; + + dot = strchr(name, '.'); + if (dot && dot[1] != '\0') { + break; /* Found a name with components */ + } + name = NULL; + } + } + } + + if (name == NULL) { + errno = ENOENT; + return -1; + } + + /* If we make it here, we have a name with components. Copy + * everything except a trailing dot into dst. + */ + dot = NULL; + while (*name) { + if (dst == dlim) { + errno = ENOBUFS; + return -1; + } + if (*name == '.') + dot = dst; + *dst++ = *name++; + } + if (name[-1] == '.') + dst = dot; + *dst = '\0'; + + return 0; +} /* * Return the realm(s) of `host' as a NULL-terminated list in * `realms'. Free `realms' with krb5_free_host_realm(). @@ -226,7 +298,7 @@ int use_dns; if (host == NULL) { - if (gethostname (hostname, sizeof(hostname))) { + if (dns_get_default_fqdn(hostname, sizeof(hostname))) { *realms = NULL; return errno; }