From 2388652d8bdaea29cd1e156846c00c4fba77cb26 Mon Sep 17 00:00:00 2001 From: Theodore Tso Date: Thu, 3 Jun 1993 00:19:07 +0000 Subject: [PATCH] Changed interface to include canonicalization and different requested name types. git-svn-id: svn://anonsvn.mit.edu/krb5/trunk@2559 dc483132-0cff-0310-8789-dd5450dbe970 --- src/lib/krb5/os/sn2princ.c | 84 ++++++++++++++++++++++++-------------- 1 file changed, 54 insertions(+), 30 deletions(-) diff --git a/src/lib/krb5/os/sn2princ.c b/src/lib/krb5/os/sn2princ.c index 675e85605..9f349c78f 100644 --- a/src/lib/krb5/os/sn2princ.c +++ b/src/lib/krb5/os/sn2princ.c @@ -36,55 +36,79 @@ static char rcsid_sn2princ_c[] = #include #include #include +#include krb5_error_code krb5_sname_to_principal(DECLARG(const char *,hostname), DECLARG(const char *,sname), - DECLARG(krb5_boolean,canonicalize), + DECLARG(krb5_int32,type), DECLARG(krb5_principal *,ret_princ)) OLDDECLARG(const char *,hostname) OLDDECLARG(const char *,sname) -OLDDECLARG(krb5_boolean,canonicalize) +OLDDECLARG(krb5_int32,type) OLDDECLARG(krb5_principal *,ret_princ) { struct hostent *hp; char **hrealms, *remote_host; krb5_error_code retval; register char *cp; + char localname[MAXHOSTNAMELEN]; - /* copy the hostname into non-volatile storage */ + if ((type == KRB5_NT_UNKNOWN) || + (type == KRB5_NT_SRV_HST)) { - if (canonicalize) { - if (!(hp = gethostbyname(hostname))) - return KRB5_ERR_BAD_HOSTNAME; - remote_host = strdup(hp->h_name); - } else { - remote_host = strdup(hostname); - } - if (!remote_host) - return ENOMEM; + /* convenience hack: if hostname is NULL, use gethostbyname() */ - for (cp = remote_host; *cp; cp++) - if (isupper(*cp)) - *cp = tolower(*cp); + if (! hostname) { + if (gethostname(localname, MAXHOSTNAMELEN)) + return errno; + hostname = localname; + } - if (retval = krb5_get_host_realm(remote_host, &hrealms)) { - free(remote_host); - return retval; - } - if (!hrealms[0]) { - free(remote_host); - xfree(hrealms); - return KRB5_ERR_HOST_REALM_UNKNOWN; - } + /* if sname is NULL, use "host" */ + + if (! sname) { + sname = "host"; + } - retval = krb5_build_principal(ret_princ, strlen(hrealms[0]), hrealms[0], - sname, remote_host, (char *)0); + /* copy the hostname into non-volatile storage */ - krb5_princ_type(*ret_princ) = KRB5_NT_SRV_HST; + if (type == KRB5_NT_SRV_HST) { + if (!(hp = gethostbyname(hostname))) + return KRB5_ERR_BAD_HOSTNAME; + remote_host = strdup(hp->h_name); + } else /* type == KRB5_NT_UNKNOWN */ { + remote_host = strdup(hostname); + } + if (!remote_host) + return ENOMEM; - free(remote_host); - krb5_free_host_realm(hrealms); - return retval; + if (type == KRB5_NT_SRV_HST) + for (cp = remote_host; *cp; cp++) + if (isupper(*cp)) + *cp = tolower(*cp); + + if (retval = krb5_get_host_realm(remote_host, &hrealms)) { + free(remote_host); + return retval; + } + if (!hrealms[0]) { + free(remote_host); + xfree(hrealms); + return KRB5_ERR_HOST_REALM_UNKNOWN; + } + + retval = krb5_build_principal(ret_princ, strlen(hrealms[0]), + hrealms[0], sname, remote_host, + (char *)0); + + krb5_princ_type(*ret_princ) = type; + + free(remote_host); + krb5_free_host_realm(hrealms); + return retval; + } else { + return KRB5_SNAME_UNSUPP_NAMETYPE; + } } -- 2.26.2