error_code KRB5_CONFIG_ETYPE_NOSUPP, "No supported encryption types (config file error?)"
error_code KRB5_OBSOLETE_FN, "Program called an obsolete, deleted function"
+# translated versions of getaddrinfo errors
+error_code KRB5_EAI_FAIL, "unknown getaddrinfo failure"
+error_code KRB5_EAI_NODATA, "no data available for host/domain name"
+error_code KRB5_EAI_NONAME, "host/domain name not found"
+error_code KRB5_EAI_SERVICE, "service name unknown"
+
end
*/
char localhost[MAX_DNS_NAMELEN+1];
char * p;
- struct hostent * h;
- localhost[0] = 0;
- gethostname(localhost, sizeof(localhost));
- localhost[sizeof(localhost) - 1] = 0;
+ krb5int_get_fq_local_hostname (localhost, sizeof(localhost));
if ( localhost[0] ) {
- /*
- * Try to make sure that we have a fully qualified
- * name if possible. We want to be able to handle
- * the case where gethostname returns a partial
- * name (i.e., it has a dot, but it is not a
- * FQDN).
- */
- h = gethostbyname(localhost);
- if (h) {
- strncpy(localhost, h->h_name, sizeof(localhost));
- localhost[sizeof(localhost) - 1] = '\0';
- }
-
p = localhost;
do {
retval = krb5_try_realm_txt_rr("_kerberos", p,
if (p)
p++;
} while (retval && p && p[0]);
-
+
if (retval)
retval = krb5_try_realm_txt_rr("_kerberos", "",
&context->default_realm);
#endif /* WSHELPER */
#endif /* KRB5_DNS_LOOKUP */
+#define FAI_PREFIX krb5int
+#include "fake-addrinfo.h"
+
/* for old Unixes and friends ... */
#ifndef MAXHOSTNAMELEN
#define MAXHOSTNAMELEN 64
}
#endif /* KRB5_DNS_LOOKUP */
+krb5_error_code krb5int_translate_gai_error (int);
+
+static krb5_error_code
+krb5int_get_fq_hostname (char *buf, size_t bufsize, const char *name)
+{
+ struct addrinfo *ai, hints;
+ int err;
+
+ memset (&hints, 0, sizeof (hints));
+ hints.ai_flags = AI_CANONNAME;
+ err = getaddrinfo (name, 0, &hints, &ai);
+ if (err)
+ return krb5int_translate_gai_error (err);
+ if (ai->ai_canonname == 0)
+ return KRB5_EAI_FAIL;
+ strncpy (buf, ai->ai_canonname, bufsize);
+ buf[bufsize-1] = 0;
+ freeaddrinfo (ai);
+ return 0;
+}
+
+/* Get the local host name, try to make it fully-qualified.
+ Always return a null-terminated string.
+ Might return an error if gethostname fails. */
+krb5_error_code
+krb5int_get_fq_local_hostname (char *buf, size_t bufsiz)
+{
+ buf[0] = 0;
+ if (gethostname (buf, bufsiz) == -1)
+ return SOCKET_ERRNO;
+ buf[bufsiz - 1] = 0;
+ return krb5int_get_fq_hostname (buf, bufsiz, buf);
+}
krb5_error_code KRB5_CALLCONV
krb5_get_host_realm(context, host, realmsp)
krb5_error_code retval;
int l;
char local_host[MAX_DNS_NAMELEN+1];
- struct hostent *h;
-
- if (host)
+ if (host) {
+ /* Should probably error out if strlen(host) > MAX_DNS_NAMELEN. */
strncpy(local_host, host, sizeof(local_host));
- else {
- if (gethostname(local_host, sizeof(local_host)) == -1)
- return SOCKET_ERRNO;
- /*
- * Try to make sure that we have a fully qualified name if
- * possible. We need to handle the case where the host has a
- * dot but is not FQDN, so we call gethostbyname.
- */
- h = gethostbyname(local_host);
- if (h) {
- strncpy(local_host, h->h_name, sizeof(local_host));
- }
+ local_host[sizeof(local_host) - 1] = '\0';
+ } else {
+ retval = krb5int_get_fq_local_hostname (local_host,
+ sizeof (local_host));
+ if (retval)
+ return retval;
}
- local_host[sizeof(local_host) - 1] = '\0';
for (cp = local_host; *cp; cp++) {
if (isupper((int) (*cp)))
*realmsp = retrealms;
return 0;
}
+
+
+krb5_error_code
+krb5int_translate_gai_error (int num)
+{
+ switch (num) {
+ case EAI_ADDRFAMILY:
+ return EAFNOSUPPORT;
+ case EAI_AGAIN:
+ return EAGAIN;
+ case EAI_BADFLAGS:
+ return EINVAL;
+ case EAI_FAIL:
+ return KRB5_EAI_FAIL;
+ case EAI_FAMILY:
+ return EAFNOSUPPORT;
+ case EAI_MEMORY:
+ return ENOMEM;
+ case EAI_NODATA:
+ return KRB5_EAI_NODATA;
+ case EAI_NONAME:
+ return KRB5_EAI_NONAME;
+ case EAI_SERVICE:
+ return KRB5_EAI_SERVICE;
+ case EAI_SOCKTYPE:
+ return EINVAL;
+ case EAI_SYSTEM:
+ return errno;
+ }
+ abort ();
+ return -1;
+}