Changed errno to SOCKET_ERRNO/SOCKET_SET_ERRNO for Mac OT SocketsLib
[krb5.git] / src / lib / krb5 / os / sn2princ.c
index 3a486462c8722785a4e6ea032c8f17b314e34529..766aa658e31127a894d343e8f7d24b5e00e20fd0 100644 (file)
@@ -1,6 +1,5 @@
 /*
- * $Source$
- * $Author$
+ * lib/krb5/os/sn2princ.c
  *
  * Copyright 1991 by the Massachusetts Institute of Technology.
  * All Rights Reserved.
  * form.
  */
 
-#if !defined(lint) && !defined(SABER)
-static char rcsid_sn2princ_c[] =
-"$Id$";
-#endif /* !lint & !SABER */
-
-#include <krb5/krb5.h>
-#include <krb5/ext-proto.h>
-#include <krb5/los-proto.h>
-#include <netdb.h>
+#define NEED_SOCKETS
+#include "k5-int.h"
 #include <ctype.h>
+#ifdef HAVE_SYS_PARAM_H
 #include <sys/param.h>
+#endif
 
-krb5_error_code
-krb5_sname_to_principal(DECLARG(const char *,hostname),
-                       DECLARG(const char *,sname),
-                       DECLARG(krb5_int32,type),
-                       DECLARG(krb5_principal *,ret_princ))
-OLDDECLARG(const char *,hostname)
-OLDDECLARG(const char *,sname)
-OLDDECLARG(krb5_int32,type)
-OLDDECLARG(krb5_principal *,ret_princ)
+KRB5_DLLIMP krb5_error_code KRB5_CALLCONV
+krb5_sname_to_principal(context, hostname, sname, type, ret_princ)
+    krb5_context context;
+    const char FAR * hostname;
+    const char FAR * sname;
+    krb5_int32 type;
+    krb5_principal FAR * ret_princ;
 {
     struct hostent *hp;
     char **hrealms, *realm, *remote_host;
     krb5_error_code retval;
     register char *cp;
     char localname[MAXHOSTNAMELEN];
-    int  use_local_realm = 0;
+
 
     if ((type == KRB5_NT_UNKNOWN) ||
        (type == KRB5_NT_SRV_HST)) {
 
-       if (!hostname) {
-           use_local_realm = 1;
-           /* convenience hack:  if hostname is NULL, use gethostname() */
+       /* if hostname is NULL, use local hostname */
+       if (! hostname) {
            if (gethostname(localname, MAXHOSTNAMELEN))
-               return errno;
+               return SOCKET_ERRNO;
            hostname = localname;
        }
 
        /* if sname is NULL, use "host" */
-
-       if (! sname) {
+       if (! sname)
            sname = "host";
-       }
 
        /* copy the hostname into non-volatile storage */
 
@@ -100,7 +89,7 @@ OLDDECLARG(krb5_principal *,ret_princ)
                    return ENOMEM;
            }
        } else /* type == KRB5_NT_UNKNOWN */ {
-           remote_host = strdup(hostname);
+           remote_host = strdup((char *) hostname);
        }
        if (!remote_host)
            return ENOMEM;
@@ -110,33 +99,38 @@ OLDDECLARG(krb5_principal *,ret_princ)
                if (isupper(*cp))
                    *cp = tolower(*cp);
 
-       if (use_local_realm) {
-           if (retval = krb5_get_default_realm(&realm)) {
-               free(remote_host);
-               return retval;
-           }
-       } else {
-           if (retval = krb5_get_host_realm(remote_host, &hrealms)) {
-               free(remote_host);
-               return retval;
-           }
-           if (!hrealms[0]) {
-               free(remote_host);
-               krb5_xfree(hrealms);
-               return KRB5_ERR_HOST_REALM_UNKNOWN;
-           }
-           realm = hrealms[0];
+       /*
+        * Windows NT5's broken resolver gratuitously tacks on a
+        * trailing period to the hostname (at least it does in
+        * Beta2).  Find and remove it.
+        */
+       if (remote_host[0]) {
+               cp = remote_host + strlen(remote_host)-1;
+               if (*cp == '.')
+                       *cp = 0;
        }
+       
 
-       retval = krb5_build_principal(ret_princ, strlen(realm),
+       if (retval = krb5_get_host_realm(context, remote_host, &hrealms)) {
+           free(remote_host);
+           return retval;
+       }
+       if (!hrealms[0]) {
+           free(remote_host);
+           krb5_xfree(hrealms);
+           return KRB5_ERR_HOST_REALM_UNKNOWN;
+       }
+       realm = hrealms[0];
+
+       retval = krb5_build_principal(context, ret_princ, strlen(realm),
                                      realm, sname, remote_host,
                                      (char *)0);
 
-       krb5_princ_type(*ret_princ) = type;
+       krb5_princ_type(context, *ret_princ) = type;
 
        free(remote_host);
-       if (!use_local_realm)
-           krb5_free_host_realm(hrealms);
+
+       krb5_free_host_realm(context, hrealms);
        return retval;
     } else {
        return KRB5_SNAME_UNSUPP_NAMETYPE;