1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/sn2princ.c */
4 * Copyright 1991,2002 by the Massachusetts Institute of Technology.
7 * Export of this software from the United States of America may
8 * require a specific license from the United States Government.
9 * It is the responsibility of any person or organization contemplating
10 * export to obtain such a license before exporting.
12 * WITHIN THAT CONSTRAINT, permission to use, copy, modify, and
13 * distribute this software and its documentation for any purpose and
14 * without fee is hereby granted, provided that the above copyright
15 * notice appear in all copies and that both that copyright notice and
16 * this permission notice appear in supporting documentation, and that
17 * the name of M.I.T. not be used in advertising or publicity pertaining
18 * to distribution of the software without specific, written prior
19 * permission. Furthermore if you modify this software you must label
20 * your software as modified software and not distribute it in such a
21 * fashion that it might be confused with the original M.I.T. software.
22 * M.I.T. makes no representations about the suitability of
23 * this software for any purpose. It is provided "as is" without express
24 * or implied warranty.
27 /* Convert a hostname and service name to a principal in the "standard"
32 #include "fake-addrinfo.h"
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
38 #if !defined(DEFAULT_RDNS_LOOKUP)
39 #define DEFAULT_RDNS_LOOKUP 1
43 maybe_use_reverse_dns (krb5_context context, int defalt)
49 code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
50 KRB5_CONF_RDNS, 0, 0, &value);
57 use_rdns = _krb5_conf_boolean(value);
58 profile_release_string(value);
63 krb5_error_code KRB5_CALLCONV
64 krb5_sname_to_principal(krb5_context context, const char *hostname, const char *sname, krb5_int32 type, krb5_principal *ret_princ)
66 char **hrealms, *realm, *remote_host;
67 krb5_error_code retval;
69 char localname[MAXHOSTNAMELEN];
71 #ifdef DEBUG_REFERRALS
72 printf("krb5_sname_to_principal(host=%s, sname=%s, type=%d)\n",hostname,sname,type);
73 printf(" name types: 0=unknown, 3=srv_host\n");
76 if ((type == KRB5_NT_UNKNOWN) ||
77 (type == KRB5_NT_SRV_HST)) {
79 /* if hostname is NULL, use local hostname */
81 if (gethostname(localname, MAXHOSTNAMELEN))
86 /* if sname is NULL, use "host" */
90 /* copy the hostname into non-volatile storage */
92 if (type == KRB5_NT_SRV_HST) {
93 struct addrinfo *ai = NULL, hints;
95 char hnamebuf[NI_MAXHOST];
97 /* Note that the old code would accept numeric addresses,
98 and if the gethostbyaddr step could convert them to
99 real hostnames, you could actually get reasonable
100 results. If the mapping failed, you'd get dotted
101 triples as realm names. *sigh*
103 The latter has been fixed in hst_realm.c, but we should
104 keep supporting numeric addresses if they do have
105 hostnames associated. */
107 memset(&hints, 0, sizeof(hints));
108 hints.ai_flags = AI_CANONNAME;
109 err = getaddrinfo(hostname, 0, &hints, &ai);
111 #ifdef DEBUG_REFERRALS
112 printf("sname_to_princ: failed to canonicalize %s; "
113 "using as-is\n", hostname);
116 remote_host = strdup((ai && ai->ai_canonname) ? ai->ai_canonname : hostname);
123 if ((!err) && maybe_use_reverse_dns(context, DEFAULT_RDNS_LOOKUP)) {
125 * Do a reverse resolution to get the full name, just in
126 * case there's some funny business going on. If there
127 * isn't an in-addr record, give up.
129 /* XXX: This is *so* bogus. There are several cases where
130 this won't get us the canonical name of the host, but
131 this is what we've trained people to expect. We'll
132 probably fix it at some point, but let's try to
133 preserve the current behavior and only shake things up
134 once when it comes time to fix this lossage. */
135 err = getnameinfo(ai->ai_addr, ai->ai_addrlen,
136 hnamebuf, sizeof(hnamebuf), 0, 0, NI_NAMEREQD);
140 remote_host = strdup(hnamebuf);
146 } else /* type == KRB5_NT_UNKNOWN */ {
147 remote_host = strdup(hostname);
151 #ifdef DEBUG_REFERRALS
152 printf("sname_to_princ: hostname <%s> after rdns processing\n",remote_host);
155 if (type == KRB5_NT_SRV_HST)
156 for (cp = remote_host; *cp; cp++)
157 if (isupper((unsigned char) (*cp)))
158 *cp = tolower((unsigned char) (*cp));
161 * Windows NT5's broken resolver gratuitously tacks on a
162 * trailing period to the hostname (at least it does in
163 * Beta2). Find and remove it.
165 if (remote_host[0]) {
166 cp = remote_host + strlen(remote_host)-1;
172 if ((retval = krb5_get_host_realm(context, remote_host, &hrealms))) {
177 #ifdef DEBUG_REFERRALS
178 printf("sname_to_princ: realm <%s> after krb5_get_host_realm\n",hrealms[0]);
184 return KRB5_ERR_HOST_REALM_UNKNOWN;
188 retval = krb5_build_principal(context, ret_princ, strlen(realm),
189 realm, sname, remote_host,
192 krb5_princ_type(context, *ret_princ) = type;
194 #ifdef DEBUG_REFERRALS
195 printf("krb5_sname_to_principal returning\n");
196 printf("realm: <%s>, sname: <%s>, remote_host: <%s>\n",
197 realm,sname,remote_host);
198 krb5int_dbgref_dump_principal("krb5_sname_to_principal",*ret_princ);
203 krb5_free_host_realm(context, hrealms);
206 return KRB5_SNAME_UNSUPP_NAMETYPE;