xref: /freebsd/crypto/heimdal/lib/krb5/expand_hostname.c (revision 6a068746777241722b2b32c5d0bc443a2a64d80b)
1b528cefcSMark Murray /*
2*ae771770SStanislav Sedov  * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
3b528cefcSMark Murray  * (Royal Institute of Technology, Stockholm, Sweden).
4b528cefcSMark Murray  * All rights reserved.
5b528cefcSMark Murray  *
6b528cefcSMark Murray  * Redistribution and use in source and binary forms, with or without
7b528cefcSMark Murray  * modification, are permitted provided that the following conditions
8b528cefcSMark Murray  * are met:
9b528cefcSMark Murray  *
10b528cefcSMark Murray  * 1. Redistributions of source code must retain the above copyright
11b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer.
12b528cefcSMark Murray  *
13b528cefcSMark Murray  * 2. Redistributions in binary form must reproduce the above copyright
14b528cefcSMark Murray  *    notice, this list of conditions and the following disclaimer in the
15b528cefcSMark Murray  *    documentation and/or other materials provided with the distribution.
16b528cefcSMark Murray  *
17b528cefcSMark Murray  * 3. Neither the name of the Institute nor the names of its contributors
18b528cefcSMark Murray  *    may be used to endorse or promote products derived from this software
19b528cefcSMark Murray  *    without specific prior written permission.
20b528cefcSMark Murray  *
21b528cefcSMark Murray  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22b528cefcSMark Murray  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23b528cefcSMark Murray  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24b528cefcSMark Murray  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25b528cefcSMark Murray  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26b528cefcSMark Murray  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27b528cefcSMark Murray  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28b528cefcSMark Murray  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29b528cefcSMark Murray  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30b528cefcSMark Murray  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31b528cefcSMark Murray  * SUCH DAMAGE.
32b528cefcSMark Murray  */
33b528cefcSMark Murray 
34b528cefcSMark Murray #include "krb5_locl.h"
35b528cefcSMark Murray 
36b528cefcSMark Murray static krb5_error_code
copy_hostname(krb5_context context,const char * orig_hostname,char ** new_hostname)37b528cefcSMark Murray copy_hostname(krb5_context context,
38b528cefcSMark Murray 	      const char *orig_hostname,
39b528cefcSMark Murray 	      char **new_hostname)
40b528cefcSMark Murray {
41b528cefcSMark Murray     *new_hostname = strdup (orig_hostname);
42adb0ddaeSAssar Westerlund     if (*new_hostname == NULL) {
43*ae771770SStanislav Sedov 	krb5_set_error_message(context, ENOMEM,
44*ae771770SStanislav Sedov 			       N_("malloc: out of memory", ""));
45b528cefcSMark Murray 	return ENOMEM;
46adb0ddaeSAssar Westerlund     }
4713e3f4d6SMark Murray     strlwr (*new_hostname);
48b528cefcSMark Murray     return 0;
49b528cefcSMark Murray }
50b528cefcSMark Murray 
51*ae771770SStanislav Sedov /**
52*ae771770SStanislav Sedov  * krb5_expand_hostname() tries to make orig_hostname into a more
53*ae771770SStanislav Sedov  * canonical one in the newly allocated space returned in
54*ae771770SStanislav Sedov  * new_hostname.
55*ae771770SStanislav Sedov 
56*ae771770SStanislav Sedov  * @param context a Keberos context
57*ae771770SStanislav Sedov  * @param orig_hostname hostname to canonicalise.
58*ae771770SStanislav Sedov  * @param new_hostname output hostname, caller must free hostname with
59*ae771770SStanislav Sedov  *        krb5_xfree().
60*ae771770SStanislav Sedov  *
61*ae771770SStanislav Sedov  * @return Return an error code or 0, see krb5_get_error_message().
62*ae771770SStanislav Sedov  *
63*ae771770SStanislav Sedov  * @ingroup krb5_support
64b528cefcSMark Murray  */
65b528cefcSMark Murray 
66*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_expand_hostname(krb5_context context,const char * orig_hostname,char ** new_hostname)67b528cefcSMark Murray krb5_expand_hostname (krb5_context context,
68b528cefcSMark Murray 		      const char *orig_hostname,
69b528cefcSMark Murray 		      char **new_hostname)
70b528cefcSMark Murray {
71b528cefcSMark Murray     struct addrinfo *ai, *a, hints;
72b528cefcSMark Murray     int error;
73b528cefcSMark Murray 
74c19800e8SDoug Rabson     if ((context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) == 0)
75c19800e8SDoug Rabson 	return copy_hostname (context, orig_hostname, new_hostname);
76c19800e8SDoug Rabson 
77b528cefcSMark Murray     memset (&hints, 0, sizeof(hints));
78b528cefcSMark Murray     hints.ai_flags = AI_CANONNAME;
79b528cefcSMark Murray 
80b528cefcSMark Murray     error = getaddrinfo (orig_hostname, NULL, &hints, &ai);
81b528cefcSMark Murray     if (error)
82b528cefcSMark Murray 	return copy_hostname (context, orig_hostname, new_hostname);
83b528cefcSMark Murray     for (a = ai; a != NULL; a = a->ai_next) {
84b528cefcSMark Murray 	if (a->ai_canonname != NULL) {
85b528cefcSMark Murray 	    *new_hostname = strdup (a->ai_canonname);
86b528cefcSMark Murray 	    freeaddrinfo (ai);
87adb0ddaeSAssar Westerlund 	    if (*new_hostname == NULL) {
88*ae771770SStanislav Sedov 		krb5_set_error_message(context, ENOMEM,
89*ae771770SStanislav Sedov 				       N_("malloc: out of memory", ""));
90b528cefcSMark Murray 		return ENOMEM;
91adb0ddaeSAssar Westerlund 	    } else {
92b528cefcSMark Murray 		return 0;
93b528cefcSMark Murray 	    }
94b528cefcSMark Murray 	}
95adb0ddaeSAssar Westerlund     }
96b528cefcSMark Murray     freeaddrinfo (ai);
97b528cefcSMark Murray     return copy_hostname (context, orig_hostname, new_hostname);
98b528cefcSMark Murray }
9913e3f4d6SMark Murray 
10013e3f4d6SMark Murray /*
101d61f1c79SMark Murray  * handle the case of the hostname being unresolvable and thus identical
102d61f1c79SMark Murray  */
103d61f1c79SMark Murray 
104d61f1c79SMark Murray static krb5_error_code
vanilla_hostname(krb5_context context,const char * orig_hostname,char ** new_hostname,char *** realms)105d61f1c79SMark Murray vanilla_hostname (krb5_context context,
106d61f1c79SMark Murray 		  const char *orig_hostname,
107d61f1c79SMark Murray 		  char **new_hostname,
108d61f1c79SMark Murray 		  char ***realms)
109d61f1c79SMark Murray {
110d61f1c79SMark Murray     krb5_error_code ret;
111d61f1c79SMark Murray 
112d61f1c79SMark Murray     ret = copy_hostname (context, orig_hostname, new_hostname);
113d61f1c79SMark Murray     if (ret)
114d61f1c79SMark Murray 	return ret;
115d61f1c79SMark Murray     strlwr (*new_hostname);
116d61f1c79SMark Murray 
117d61f1c79SMark Murray     ret = krb5_get_host_realm (context, *new_hostname, realms);
118d61f1c79SMark Murray     if (ret) {
119d61f1c79SMark Murray 	free (*new_hostname);
120d61f1c79SMark Murray 	return ret;
121d61f1c79SMark Murray     }
122d61f1c79SMark Murray     return 0;
123d61f1c79SMark Murray }
124d61f1c79SMark Murray 
125*ae771770SStanislav Sedov /**
126*ae771770SStanislav Sedov  * krb5_expand_hostname_realms() expands orig_hostname to a name we
127*ae771770SStanislav Sedov  * believe to be a hostname in newly allocated space in new_hostname
128*ae771770SStanislav Sedov  * and return the realms new_hostname is believed to belong to in
129*ae771770SStanislav Sedov  * realms.
130*ae771770SStanislav Sedov  *
131*ae771770SStanislav Sedov  * @param context a Keberos context
132*ae771770SStanislav Sedov  * @param orig_hostname hostname to canonicalise.
133*ae771770SStanislav Sedov  * @param new_hostname output hostname, caller must free hostname with
134*ae771770SStanislav Sedov  *        krb5_xfree().
135*ae771770SStanislav Sedov  * @param realms output possible realms, is an array that is terminated
136*ae771770SStanislav Sedov  *        with NULL. Caller must free with krb5_free_host_realm().
137*ae771770SStanislav Sedov  *
138*ae771770SStanislav Sedov  * @return Return an error code or 0, see krb5_get_error_message().
139*ae771770SStanislav Sedov  *
140*ae771770SStanislav Sedov  * @ingroup krb5_support
14113e3f4d6SMark Murray  */
14213e3f4d6SMark Murray 
143*ae771770SStanislav Sedov KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
krb5_expand_hostname_realms(krb5_context context,const char * orig_hostname,char ** new_hostname,char *** realms)14413e3f4d6SMark Murray krb5_expand_hostname_realms (krb5_context context,
14513e3f4d6SMark Murray 			     const char *orig_hostname,
14613e3f4d6SMark Murray 			     char **new_hostname,
14713e3f4d6SMark Murray 			     char ***realms)
14813e3f4d6SMark Murray {
14913e3f4d6SMark Murray     struct addrinfo *ai, *a, hints;
15013e3f4d6SMark Murray     int error;
15113e3f4d6SMark Murray     krb5_error_code ret = 0;
15213e3f4d6SMark Murray 
153c19800e8SDoug Rabson     if ((context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) == 0)
154c19800e8SDoug Rabson 	return vanilla_hostname (context, orig_hostname, new_hostname,
155c19800e8SDoug Rabson 				 realms);
156c19800e8SDoug Rabson 
15713e3f4d6SMark Murray     memset (&hints, 0, sizeof(hints));
15813e3f4d6SMark Murray     hints.ai_flags = AI_CANONNAME;
15913e3f4d6SMark Murray 
16013e3f4d6SMark Murray     error = getaddrinfo (orig_hostname, NULL, &hints, &ai);
16113e3f4d6SMark Murray     if (error)
162d61f1c79SMark Murray 	return vanilla_hostname (context, orig_hostname, new_hostname,
163d61f1c79SMark Murray 				 realms);
164d61f1c79SMark Murray 
16513e3f4d6SMark Murray     for (a = ai; a != NULL; a = a->ai_next) {
16613e3f4d6SMark Murray 	if (a->ai_canonname != NULL) {
1675e9cd1aeSAssar Westerlund 	    ret = copy_hostname (context, a->ai_canonname, new_hostname);
168d61f1c79SMark Murray 	    if (ret) {
169d61f1c79SMark Murray 		freeaddrinfo (ai);
170d61f1c79SMark Murray 		return ret;
171d61f1c79SMark Murray 	    }
17213e3f4d6SMark Murray 	    strlwr (*new_hostname);
17313e3f4d6SMark Murray 	    ret = krb5_get_host_realm (context, *new_hostname, realms);
174d61f1c79SMark Murray 	    if (ret == 0) {
175d61f1c79SMark Murray 		freeaddrinfo (ai);
176d61f1c79SMark Murray 		return 0;
177d61f1c79SMark Murray 	    }
17813e3f4d6SMark Murray 	    free (*new_hostname);
17913e3f4d6SMark Murray 	}
18013e3f4d6SMark Murray     }
1814137ff4cSJacques Vidrine     freeaddrinfo(ai);
182d61f1c79SMark Murray     return vanilla_hostname (context, orig_hostname, new_hostname, realms);
18313e3f4d6SMark Murray }
184