krb5 commit: Move utility functions to hostrealm.c

Greg Hudson ghudson at MIT.EDU
Thu Aug 15 12:51:26 EDT 2013


https://github.com/krb5/krb5/commit/4f7f1fce6edca17db625d76c1f81ea098f29c313
commit 4f7f1fce6edca17db625d76c1f81ea098f29c313
Author: Greg Hudson <ghudson at mit.edu>
Date:   Mon Aug 5 16:05:27 2013 -0400

    Move utility functions to hostrealm.c
    
    Move the remaining internal functions from hst_realm.c to hostrealm.c,
    and get rid of hst_realm.c.
    
    ticket: 7687

 src/lib/krb5/os/Makefile.in |    3 -
 src/lib/krb5/os/deps        |   15 +----
 src/lib/krb5/os/hostrealm.c |  132 +++++++++++++++++++++++++++++++++
 src/lib/krb5/os/hst_realm.c |  168 -------------------------------------------
 4 files changed, 134 insertions(+), 184 deletions(-)

diff --git a/src/lib/krb5/os/Makefile.in b/src/lib/krb5/os/Makefile.in
index 769f234..5add9f9 100644
--- a/src/lib/krb5/os/Makefile.in
+++ b/src/lib/krb5/os/Makefile.in
@@ -25,7 +25,6 @@ STLIBOBJS= \
 	hostrealm_dns.o \
 	hostrealm_domain.o \
 	hostrealm_profile.o \
-	hst_realm.o	\
 	init_os_ctx.o	\
 	krbfileio.o	\
 	ktdefname.o	\
@@ -72,7 +71,6 @@ OBJS= \
 	$(OUTPRE)hostrealm_dns.$(OBJEXT) \
 	$(OUTPRE)hostrealm_domain.$(OBJEXT) \
 	$(OUTPRE)hostrealm_profile.$(OBJEXT) \
-	$(OUTPRE)hst_realm.$(OBJEXT)	\
 	$(OUTPRE)init_os_ctx.$(OBJEXT)	\
 	$(OUTPRE)krbfileio.$(OBJEXT)	\
 	$(OUTPRE)ktdefname.$(OBJEXT)	\
@@ -119,7 +117,6 @@ SRCS= \
 	$(srcdir)/hostrealm_dns.c \
 	$(srcdir)/hostrealm_domain.c \
 	$(srcdir)/hostrealm_profile.c \
-	$(srcdir)/hst_realm.c	\
 	$(srcdir)/init_os_ctx.c	\
 	$(srcdir)/krbfileio.c	\
 	$(srcdir)/ktdefname.c	\
diff --git a/src/lib/krb5/os/deps b/src/lib/krb5/os/deps
index 0b53b97..3dd6d46 100644
--- a/src/lib/krb5/os/deps
+++ b/src/lib/krb5/os/deps
@@ -142,7 +142,8 @@ hostaddr.so hostaddr.po $(OUTPRE)hostaddr.$(OBJEXT): \
 hostrealm.so hostrealm.po $(OUTPRE)hostrealm.$(OBJEXT): \
   $(BUILDTOP)/include/autoconf.h $(BUILDTOP)/include/krb5/krb5.h \
   $(BUILDTOP)/include/osconf.h $(BUILDTOP)/include/profile.h \
-  $(COM_ERR_DEPS) $(top_srcdir)/include/k5-buf.h $(top_srcdir)/include/k5-err.h \
+  $(COM_ERR_DEPS) $(top_srcdir)/include/fake-addrinfo.h \
+  $(top_srcdir)/include/k5-buf.h $(top_srcdir)/include/k5-err.h \
   $(top_srcdir)/include/k5-gmt_mktime.h $(top_srcdir)/include/k5-int-pkinit.h \
   $(top_srcdir)/include/k5-int.h $(top_srcdir)/include/k5-platform.h \
   $(top_srcdir)/include/k5-plugin.h $(top_srcdir)/include/k5-thread.h \
@@ -187,18 +188,6 @@ hostrealm_profile.so hostrealm_profile.po $(OUTPRE)hostrealm_profile.$(OBJEXT):
   $(top_srcdir)/include/krb5/locate_plugin.h $(top_srcdir)/include/krb5/plugin.h \
   $(top_srcdir)/include/port-sockets.h $(top_srcdir)/include/socket-utils.h \
   hostrealm_profile.c os-proto.h
-hst_realm.so hst_realm.po $(OUTPRE)hst_realm.$(OBJEXT): \
-  $(BUILDTOP)/include/autoconf.h $(BUILDTOP)/include/krb5/krb5.h \
-  $(BUILDTOP)/include/osconf.h $(BUILDTOP)/include/profile.h \
-  $(COM_ERR_DEPS) $(top_srcdir)/include/fake-addrinfo.h \
-  $(top_srcdir)/include/k5-buf.h $(top_srcdir)/include/k5-err.h \
-  $(top_srcdir)/include/k5-gmt_mktime.h $(top_srcdir)/include/k5-int-pkinit.h \
-  $(top_srcdir)/include/k5-int.h $(top_srcdir)/include/k5-platform.h \
-  $(top_srcdir)/include/k5-plugin.h $(top_srcdir)/include/k5-thread.h \
-  $(top_srcdir)/include/k5-trace.h $(top_srcdir)/include/krb5.h \
-  $(top_srcdir)/include/krb5/authdata_plugin.h $(top_srcdir)/include/krb5/locate_plugin.h \
-  $(top_srcdir)/include/krb5/plugin.h $(top_srcdir)/include/port-sockets.h \
-  $(top_srcdir)/include/socket-utils.h hst_realm.c os-proto.h
 init_os_ctx.so init_os_ctx.po $(OUTPRE)init_os_ctx.$(OBJEXT): \
   $(BUILDTOP)/include/autoconf.h $(BUILDTOP)/include/krb5/krb5.h \
   $(BUILDTOP)/include/osconf.h $(BUILDTOP)/include/profile.h \
diff --git a/src/lib/krb5/os/hostrealm.c b/src/lib/krb5/os/hostrealm.c
index 87f8ffb..6adcb0a 100644
--- a/src/lib/krb5/os/hostrealm.c
+++ b/src/lib/krb5/os/hostrealm.c
@@ -32,9 +32,16 @@
 
 #include "k5-int.h"
 #include "os-proto.h"
+#include "fake-addrinfo.h"
 #include <krb5/hostrealm_plugin.h>
 #include <ctype.h>
 
+#if defined(_WIN32) && !defined(__CYGWIN32__)
+#ifndef EAFNOSUPPORT
+#define EAFNOSUPPORT WSAEAFNOSUPPORT
+#endif
+#endif
+
 struct hostrealm_module_handle {
     struct krb5_hostrealm_vtable_st vt;
     krb5_hostrealm_moddata data;
@@ -199,6 +206,131 @@ copy_list(char **in, char ***out)
     return 0;
 }
 
+static krb5_error_code
+translate_gai_error(int num)
+{
+    switch (num) {
+#ifdef EAI_ADDRFAMILY
+    case EAI_ADDRFAMILY:
+        return EAFNOSUPPORT;
+#endif
+    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;
+#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
+    case EAI_NODATA:
+        return KRB5_EAI_NODATA;
+#endif
+    case EAI_NONAME:
+        return KRB5_EAI_NONAME;
+#if defined(EAI_OVERFLOW)
+    case EAI_OVERFLOW:
+        return EINVAL;          /* XXX */
+#endif
+    case EAI_SERVICE:
+        return KRB5_EAI_SERVICE;
+    case EAI_SOCKTYPE:
+        return EINVAL;
+#ifdef EAI_SYSTEM
+    case EAI_SYSTEM:
+        return errno;
+#endif
+    }
+    abort();
+    return -1;
+}
+
+/* Get the canonical form of the local host name, using forward
+ * canonicalization only. */
+krb5_error_code
+krb5int_get_fq_local_hostname(char *buf, size_t bufsize)
+{
+    struct addrinfo *ai, hints;
+    int err;
+
+    buf[0] = '\0';
+    if (gethostname(buf, bufsize) == -1)
+        return SOCKET_ERRNO;
+
+    memset(&hints, 0, sizeof(hints));
+    hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
+    err = getaddrinfo(buf, NULL, &hints, &ai);
+    if (err)
+        return translate_gai_error(err);
+    if (ai->ai_canonname == NULL) {
+        freeaddrinfo(ai);
+        return KRB5_EAI_FAIL;
+    }
+    if (strlcpy(buf, ai->ai_canonname, bufsize) >= bufsize)
+        return ENOMEM;
+    freeaddrinfo(ai);
+    return 0;
+}
+
+krb5_error_code
+k5_clean_hostname(krb5_context context, const char *host, char *cleanname,
+                  size_t lhsize)
+{
+    char *p;
+    krb5_error_code ret;
+    size_t l;
+
+    cleanname[0] = '\0';
+    if (host != NULL) {
+        if (strlcpy(cleanname, host, lhsize) >= lhsize)
+            return ENOMEM;
+    } else {
+        ret = krb5int_get_fq_local_hostname(cleanname, lhsize);
+        if (ret)
+            return ret;
+    }
+
+    /* Fold to lowercase. */
+    for (p = cleanname; *p; p++) {
+        if (isupper((unsigned char)*p))
+            *p = tolower((unsigned char)*p);
+    }
+
+    /* Strip off trailing dot. */
+    l = strlen(cleanname);
+    if (l > 0 && cleanname[l - 1] == '.')
+        cleanname[l - 1] = '\0';
+
+    return 0;
+}
+
+/* Return true if name appears to be an IPv4 or IPv6 address. */
+krb5_boolean
+k5_is_numeric_address(const char *name)
+{
+    int ndots = 0;
+    const char *p;
+
+    /* If name contains only numbers and three dots, consider it to be an IPv4
+     * address. */
+    if (strspn(name, "01234567890.") == strlen(name)) {
+        for (p = name; *p; p++) {
+            if (*p == '.')
+                ndots++;
+        }
+        if (ndots == 3)
+            return TRUE;
+    }
+
+    /* If name contains a colon, consider it to be an IPv6 address. */
+    if (strchr(name, ':') != NULL)
+        return TRUE;
+
+    return FALSE;
+}
+
 /* Construct a one-element realm list containing a copy of realm. */
 krb5_error_code
 k5_make_realmlist(const char *realm, char ***realms_out)
diff --git a/src/lib/krb5/os/hst_realm.c b/src/lib/krb5/os/hst_realm.c
deleted file mode 100644
index 7cb7c5f..0000000
--- a/src/lib/krb5/os/hst_realm.c
+++ /dev/null
@@ -1,168 +0,0 @@
-/* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
-/* lib/krb5/os/hst_realm.c - Hostname to realm translation */
-/*
- * Copyright 1990,1991,2002,2008,2009,2013 by the Massachusetts Institute of
- * Technology.  All Rights Reserved.
- *
- * Export of this software from the United States of America may
- *   require a specific license from the United States Government.
- *   It is the responsibility of any person or organization contemplating
- *   export to obtain such a license before exporting.
- *
- * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
- * distribute this software and its documentation for any purpose and
- * without fee is hereby granted, provided that the above copyright
- * notice appear in all copies and that both that copyright notice and
- * this permission notice appear in supporting documentation, and that
- * the name of M.I.T. not be used in advertising or publicity pertaining
- * to distribution of the software without specific, written prior
- * permission.  Furthermore if you modify this software you must label
- * your software as modified software and not distribute it in such a
- * fashion that it might be confused with the original M.I.T. software.
- * M.I.T. makes no representations about the suitability of
- * this software for any purpose.  It is provided "as is" without express
- * or implied warranty.
- */
-/*
- * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
- * Use is subject to license terms.
- */
-
-#include "k5-int.h"
-#include "os-proto.h"
-#include <ctype.h>
-
-#include "fake-addrinfo.h"
-
-#if defined(_WIN32) && !defined(__CYGWIN32__)
-#ifndef EAFNOSUPPORT
-#define EAFNOSUPPORT WSAEAFNOSUPPORT
-#endif
-#endif
-
-static krb5_error_code
-krb5int_translate_gai_error(int num)
-{
-    switch (num) {
-#ifdef EAI_ADDRFAMILY
-    case EAI_ADDRFAMILY:
-        return EAFNOSUPPORT;
-#endif
-    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;
-#if defined(EAI_NODATA) && EAI_NODATA != EAI_NONAME
-    case EAI_NODATA:
-        return KRB5_EAI_NODATA;
-#endif
-    case EAI_NONAME:
-        return KRB5_EAI_NONAME;
-#if defined(EAI_OVERFLOW)
-    case EAI_OVERFLOW:
-        return EINVAL;          /* XXX */
-#endif
-    case EAI_SERVICE:
-        return KRB5_EAI_SERVICE;
-    case EAI_SOCKTYPE:
-        return EINVAL;
-#ifdef EAI_SYSTEM
-    case EAI_SYSTEM:
-        return errno;
-#endif
-    }
-    abort();
-    return -1;
-}
-
-/* Get the canonical form of the local host name, using forward
- * canonicalization only. */
-krb5_error_code
-krb5int_get_fq_local_hostname(char *buf, size_t bufsize)
-{
-    struct addrinfo *ai, hints;
-    int err;
-
-    buf[0] = '\0';
-    if (gethostname(buf, bufsize) == -1)
-        return SOCKET_ERRNO;
-
-    memset(&hints, 0, sizeof(hints));
-    hints.ai_flags = AI_CANONNAME | AI_ADDRCONFIG;
-    err = getaddrinfo(buf, NULL, &hints, &ai);
-    if (err)
-        return krb5int_translate_gai_error(err);
-    if (ai->ai_canonname == NULL) {
-        freeaddrinfo(ai);
-        return KRB5_EAI_FAIL;
-    }
-    if (strlcpy(buf, ai->ai_canonname, bufsize) >= bufsize)
-        return ENOMEM;
-    freeaddrinfo(ai);
-    return 0;
-}
-
-/* Return true if name appears to be an IPv4 or IPv6 address. */
-krb5_boolean
-k5_is_numeric_address(const char *name)
-{
-    int ndots = 0;
-    const char *p;
-
-    /* If name contains only numbers and three dots, consider it to be an IPv4
-     * address. */
-    if (strspn(name, "01234567890.") == strlen(name)) {
-        for (p = name; *p; p++) {
-            if (*p == '.')
-                ndots++;
-        }
-        if (ndots == 3)
-            return TRUE;
-    }
-
-    /* If name contains a colon, consider it to be an IPv6 address. */
-    if (strchr(name, ':') != NULL)
-        return TRUE;
-
-    return FALSE;
-}
-
-/* Common code for krb5_get_host_realm and krb5_get_fallback_host_realm
- * to do basic sanity checks on supplied hostname. */
-krb5_error_code
-k5_clean_hostname(krb5_context context, const char *host, char *cleanname,
-                  size_t lhsize)
-{
-    char *p;
-    krb5_error_code ret;
-    size_t l;
-
-    cleanname[0] = '\0';
-    if (host) {
-        if (strlcpy(cleanname, host, lhsize) >= lhsize)
-            return ENOMEM;
-    } else {
-        ret = krb5int_get_fq_local_hostname(cleanname, lhsize);
-        if (ret)
-            return ret;
-    }
-
-    /* Fold to lowercase. */
-    for (p = cleanname; *p; p++) {
-        if (isupper((unsigned char)*p))
-            *p = tolower((unsigned char)*p);
-    }
-
-    /* Strip off trailing dot. */
-    l = strlen(cleanname);
-    if (l > 0 && cleanname[l - 1] == '.')
-        cleanname[l - 1] = '\0';
-
-    return 0;
-}


More information about the cvs-krb5 mailing list