Convert DEBUG_REFERRALS to TRACE_* framework
[krb5.git] / src / lib / krb5 / os / sn2princ.c
1 /* -*- mode: c; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /* lib/krb5/os/sn2princ.c */
3 /*
4  * Copyright 1991,2002 by the Massachusetts Institute of Technology.
5  * All Rights Reserved.
6  *
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.
11  *
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.
25  */
26
27 /* Convert a hostname and service name to a principal in the "standard"
28  * form. */
29
30 #include "k5-int.h"
31 #include "os-proto.h"
32 #include "fake-addrinfo.h"
33 #include <ctype.h>
34 #ifdef HAVE_SYS_PARAM_H
35 #include <sys/param.h>
36 #endif
37
38 #if !defined(DEFAULT_RDNS_LOOKUP)
39 #define DEFAULT_RDNS_LOOKUP 1
40 #endif
41
42 static int
43 maybe_use_reverse_dns (krb5_context context, int defalt)
44 {
45     krb5_error_code code;
46     char * value = NULL;
47     int use_rdns = 0;
48
49     code = profile_get_string(context->profile, KRB5_CONF_LIBDEFAULTS,
50                               KRB5_CONF_RDNS, 0, 0, &value);
51     if (code)
52         return defalt;
53
54     if (value == 0)
55         return defalt;
56
57     use_rdns = _krb5_conf_boolean(value);
58     profile_release_string(value);
59     return use_rdns;
60 }
61
62
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)
65 {
66     char **hrealms, *realm, *remote_host;
67     krb5_error_code retval;
68     register char *cp;
69     char localname[MAXHOSTNAMELEN];
70
71     TRACE_SNAME_TO_PRINCIPAL(context, hostname, sname, type);
72
73     if ((type == KRB5_NT_UNKNOWN) ||
74         (type == KRB5_NT_SRV_HST)) {
75
76         /* if hostname is NULL, use local hostname */
77         if (! hostname) {
78             if (gethostname(localname, MAXHOSTNAMELEN))
79                 return SOCKET_ERRNO;
80             hostname = localname;
81         }
82
83         /* if sname is NULL, use "host" */
84         if (! sname)
85             sname = "host";
86
87         /* copy the hostname into non-volatile storage */
88
89         if (type == KRB5_NT_SRV_HST) {
90             struct addrinfo *ai = NULL, hints;
91             int err;
92             char hnamebuf[NI_MAXHOST];
93
94             /* Note that the old code would accept numeric addresses,
95                and if the gethostbyaddr step could convert them to
96                real hostnames, you could actually get reasonable
97                results.  If the mapping failed, you'd get dotted
98                triples as realm names.  *sigh*
99
100                The latter has been fixed in hst_realm.c, but we should
101                keep supporting numeric addresses if they do have
102                hostnames associated.  */
103
104             memset(&hints, 0, sizeof(hints));
105             hints.ai_flags = AI_CANONNAME;
106             err = getaddrinfo(hostname, 0, &hints, &ai);
107             if (err) {
108                 TRACE_SNAME_TO_PRINCIPAL_NOCANON(context, hostname);
109             }
110             remote_host = strdup((ai && ai->ai_canonname) ? ai->ai_canonname : hostname);
111             if (!remote_host) {
112                 if(ai)
113                     freeaddrinfo(ai);
114                 return ENOMEM;
115             }
116             TRACE_SNAME_TO_PRINCIPAL_CANON(context, remote_host);
117             if ((!err) && maybe_use_reverse_dns(context, DEFAULT_RDNS_LOOKUP)) {
118                 /*
119                  * Do a reverse resolution to get the full name, just in
120                  * case there's some funny business going on.  If there
121                  * isn't an in-addr record, give up.
122                  */
123                 /* XXX: This is *so* bogus.  There are several cases where
124                    this won't get us the canonical name of the host, but
125                    this is what we've trained people to expect.  We'll
126                    probably fix it at some point, but let's try to
127                    preserve the current behavior and only shake things up
128                    once when it comes time to fix this lossage.  */
129                 err = getnameinfo(ai->ai_addr, ai->ai_addrlen,
130                                   hnamebuf, sizeof(hnamebuf), 0, 0, NI_NAMEREQD);
131                 freeaddrinfo(ai);
132                 if (err == 0) {
133                     free(remote_host);
134                     remote_host = strdup(hnamebuf);
135                     if (!remote_host)
136                         return ENOMEM;
137                 }
138             } else
139                 freeaddrinfo(ai);
140         } else /* type == KRB5_NT_UNKNOWN */ {
141             remote_host = strdup(hostname);
142         }
143         if (!remote_host)
144             return ENOMEM;
145         TRACE_SNAME_TO_PRINCIPAL_RDNS(context, remote_host);
146
147         if (type == KRB5_NT_SRV_HST)
148             for (cp = remote_host; *cp; cp++)
149                 if (isupper((unsigned char) (*cp)))
150                     *cp = tolower((unsigned char) (*cp));
151
152         /*
153          * Windows NT5's broken resolver gratuitously tacks on a
154          * trailing period to the hostname (at least it does in
155          * Beta2).  Find and remove it.
156          */
157         if (remote_host[0]) {
158             cp = remote_host + strlen(remote_host)-1;
159             if (*cp == '.')
160                 *cp = 0;
161         }
162
163
164         if ((retval = krb5_get_host_realm(context, remote_host, &hrealms))) {
165             free(remote_host);
166             return retval;
167         }
168
169         if (!hrealms[0]) {
170             free(remote_host);
171             free(hrealms);
172             return KRB5_ERR_HOST_REALM_UNKNOWN;
173         }
174         realm = hrealms[0];
175
176         retval = krb5_build_principal(context, ret_princ, strlen(realm),
177                                       realm, sname, remote_host,
178                                       (char *)0);
179         if (retval == 0)
180             krb5_princ_type(context, *ret_princ) = type;
181
182         TRACE_SNAME_TO_PRINCIPAL_RETURN(context, *ret_princ);
183
184         free(remote_host);
185
186         krb5_free_host_realm(context, hrealms);
187         return retval;
188     } else {
189         return KRB5_SNAME_UNSUPP_NAMETYPE;
190     }
191 }