]> git.ipfire.org Git - thirdparty/krb5.git/commitdiff
Split off hostname munging code into krb5_clean_hostname. Currently
authorAndrew Boardman <amb@mit.edu>
Tue, 15 Aug 2006 22:51:55 +0000 (22:51 +0000)
committerAndrew Boardman <amb@mit.edu>
Tue, 15 Aug 2006 22:51:55 +0000 (22:51 +0000)
broken work-in-progress.

git-svn-id: svn://anonsvn.mit.edu/krb5/users/amb/referrals@18446 dc483132-0cff-0310-8789-dd5450dbe970

src/lib/krb5/os/hst_realm.c

index 9a6049386a6e5499f9d7547304146f984433d899..ae3e15312e1b7d76a71f006de3936e8a62a83c0a 100644 (file)
@@ -196,54 +196,13 @@ krb5_error_code KRB5_CALLCONV
 krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp)
 {
     char **retrealms;
-    char *default_realm, *realm, *cp, *temp_realm;
+    char *realm, *cp, *temp_realm;
     krb5_error_code retval;
-    int l;
     char local_host[MAXDNAME+1];
 
     printf("get_host_realm(host:%s) called\n",host);
 
-    if (host) {
-       /* Filter out numeric addresses if the caller utterly failed to
-          convert them to names.  */
-       /* IPv4 - dotted quads only */
-       if (strspn(host, "01234567890.") == strlen(host)) {
-           /* All numbers and dots... if it's three dots, it's an
-              IP address, and we reject it.  But "12345" could be
-              a local hostname, couldn't it?  We'll just assume
-              that a name with three dots is not meant to be an
-              all-numeric hostname three all-numeric domains down
-              from the current domain.  */
-           int ndots = 0;
-           const char *p;
-           for (p = host; *p; p++)
-               if (*p == '.')
-                   ndots++;
-           if (ndots == 3)
-               return KRB5_ERR_NUMERIC_REALM;
-       }
-       if (strchr(host, ':'))
-           /* IPv6 numeric address form?  Bye bye.  */
-           return KRB5_ERR_NUMERIC_REALM;
-
-       /* Should probably error out if strlen(host) > MAXDNAME.  */
-       strncpy(local_host, host, 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;
-    }
-
-    for (cp = local_host; *cp; cp++) {
-       if (isupper((unsigned char) (*cp)))
-           *cp = tolower((unsigned char) *cp);
-    }
-    l = strlen(local_host);
-    /* strip off trailing dot */
-    if (l && local_host[l-1] == '.')
-           local_host[l-1] = 0;
+    krb5_clean_hostname(context, host, &local_host);
 
     /*
        Search for the best match for the host or domain.
@@ -259,7 +218,7 @@ krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp)
 
     cp = local_host;
     printf("  local_host: %s\n",local_host);
-    realm = default_realm = (char *)NULL;
+    realm = (char *)NULL;
     temp_realm = 0;
     while (cp) {
         printf("  trying to look up %s in the domain_realm map\n",cp);
@@ -273,10 +232,6 @@ krb5_get_host_realm(krb5_context context, const char *host, char ***realmsp)
        /* Setup for another test */
        if (*cp == '.') {
            cp++;
-           if (default_realm == (char *)NULL) {
-               /* If nothing else works, use the host's domain */
-               default_realm = cp;
-           }
        } else {
            cp = strchr(cp, '.');
        }
@@ -371,7 +326,29 @@ krb5_get_fallback_host_realm(krb5_context context, const char *host, char ***rea
     krb5_error_code retval;
     char local_host[MAXDNAME+1];
 
-    printf("get_fallback_host_realm(host:%s) called\n",host);
+    printf("get_fallback_host_realm(host >%s<) called\n",host);
+
+    krb5_clean_hostname(context, host, &local_host);
+
+    /* Scan hostname for DNS realm, and save as last-ditch realm
+       assumption. */
+    cp = local_host;
+    printf("  local_host: %s\n",local_host);
+    realm = default_realm = (char *)NULL;
+    temp_realm = 0;
+    while (cp && !default_realm) {
+       if (*cp == '.') {
+           cp++;
+           if (default_realm == (char *)NULL) {
+               /* If nothing else works, use the host's domain */
+               default_realm = cp;
+           }
+       } else {
+           cp = strchr(cp, '.');
+       }
+    }
+    printf("  done finding DNS-based default realm: >%s<\n",default_realm);
+
 
 #ifdef KRB5_DNS_LOOKUP
     if (realm == (char *)NULL) {
@@ -394,6 +371,8 @@ krb5_get_fallback_host_realm(krb5_context context, const char *host, char ***rea
         }
     }
 #endif /* KRB5_DNS_LOOKUP */
+
+      
     if (realm == (char *)NULL) {
         if (default_realm != (char *)NULL) {
             /* We are defaulting to the realm of the host */
@@ -426,3 +405,65 @@ krb5_get_fallback_host_realm(krb5_context context, const char *host, char ***rea
     *realmsp = retrealms;
     return 0;
 }
+
+/*
+ * Common code for krb5_get_host_realm and krb5_get_fallback_host_realm
+ * to do basic sanity checks on supplied hostname.
+ */
+krb5_error_code KRB5_CALLCONV
+krb5_clean_hostname(krb5_context context, const char *host, char **local_hostp)
+{
+    char **retrealms;
+    char *realm, *cp, *temp_realm, *local_host;
+    krb5_error_code retval;
+    int l;
+
+    local_host=*local_hostp;
+
+    printf("krb5_clean_hostname called: host<%s>, local_host<%s>\n",host,local_host);
+    if (host) {
+       /* Filter out numeric addresses if the caller utterly failed to
+          convert them to names.  */
+       /* IPv4 - dotted quads only */
+       if (strspn(host, "01234567890.") == strlen(host)) {
+           /* All numbers and dots... if it's three dots, it's an
+              IP address, and we reject it.  But "12345" could be
+              a local hostname, couldn't it?  We'll just assume
+              that a name with three dots is not meant to be an
+              all-numeric hostname three all-numeric domains down
+              from the current domain.  */
+           int ndots = 0;
+           const char *p;
+           for (p = host; *p; p++)
+               if (*p == '.')
+                   ndots++;
+           if (ndots == 3)
+               return KRB5_ERR_NUMERIC_REALM;
+       }
+       if (strchr(host, ':'))
+           /* IPv6 numeric address form?  Bye bye.  */
+           return KRB5_ERR_NUMERIC_REALM;
+
+       /* Should probably error out if strlen(host) > MAXDNAME.  */
+       strncpy(local_host, host, 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;
+    }
+
+    /* fold to lowercase */
+    for (cp = local_host; *cp; cp++) {
+       if (isupper((unsigned char) (*cp)))
+           *cp = tolower((unsigned char) *cp);
+    }
+    l = strlen(local_host);
+    /* strip off trailing dot */
+    if (l && local_host[l-1] == '.')
+           local_host[l-1] = 0;
+
+    printf("krb5_clean_hostname ending: host<%s>, local_host<%s>\n",host,local_host);
+    return 0;
+}