1 /* $NetBSD: expand_hostname.c,v 1.1.1.1 2011/04/13 18:15:33 elric Exp $ */
4 * Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan
5 * (Royal Institute of Technology, Stockholm, Sweden).
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the Institute nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 #include "krb5_locl.h"
38 static krb5_error_code
39 copy_hostname(krb5_context context
,
40 const char *orig_hostname
,
43 *new_hostname
= strdup (orig_hostname
);
44 if (*new_hostname
== NULL
) {
45 krb5_set_error_message(context
, ENOMEM
,
46 N_("malloc: out of memory", ""));
49 strlwr (*new_hostname
);
54 * krb5_expand_hostname() tries to make orig_hostname into a more
55 * canonical one in the newly allocated space returned in
58 * @param context a Keberos context
59 * @param orig_hostname hostname to canonicalise.
60 * @param new_hostname output hostname, caller must free hostname with
63 * @return Return an error code or 0, see krb5_get_error_message().
65 * @ingroup krb5_support
68 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
69 krb5_expand_hostname (krb5_context context
,
70 const char *orig_hostname
,
73 struct addrinfo
*ai
, *a
, hints
;
76 if ((context
->flags
& KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
) == 0)
77 return copy_hostname (context
, orig_hostname
, new_hostname
);
79 memset (&hints
, 0, sizeof(hints
));
80 hints
.ai_flags
= AI_CANONNAME
;
82 error
= getaddrinfo (orig_hostname
, NULL
, &hints
, &ai
);
84 return copy_hostname (context
, orig_hostname
, new_hostname
);
85 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
86 if (a
->ai_canonname
!= NULL
) {
87 *new_hostname
= strdup (a
->ai_canonname
);
89 if (*new_hostname
== NULL
) {
90 krb5_set_error_message(context
, ENOMEM
,
91 N_("malloc: out of memory", ""));
99 return copy_hostname (context
, orig_hostname
, new_hostname
);
103 * handle the case of the hostname being unresolvable and thus identical
106 static krb5_error_code
107 vanilla_hostname (krb5_context context
,
108 const char *orig_hostname
,
114 ret
= copy_hostname (context
, orig_hostname
, new_hostname
);
117 strlwr (*new_hostname
);
119 ret
= krb5_get_host_realm (context
, *new_hostname
, realms
);
121 free (*new_hostname
);
128 * krb5_expand_hostname_realms() expands orig_hostname to a name we
129 * believe to be a hostname in newly allocated space in new_hostname
130 * and return the realms new_hostname is believed to belong to in
133 * @param context a Keberos context
134 * @param orig_hostname hostname to canonicalise.
135 * @param new_hostname output hostname, caller must free hostname with
137 * @param realms output possible realms, is an array that is terminated
138 * with NULL. Caller must free with krb5_free_host_realm().
140 * @return Return an error code or 0, see krb5_get_error_message().
142 * @ingroup krb5_support
145 KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL
146 krb5_expand_hostname_realms (krb5_context context
,
147 const char *orig_hostname
,
151 struct addrinfo
*ai
, *a
, hints
;
153 krb5_error_code ret
= 0;
155 if ((context
->flags
& KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME
) == 0)
156 return vanilla_hostname (context
, orig_hostname
, new_hostname
,
159 memset (&hints
, 0, sizeof(hints
));
160 hints
.ai_flags
= AI_CANONNAME
;
162 error
= getaddrinfo (orig_hostname
, NULL
, &hints
, &ai
);
164 return vanilla_hostname (context
, orig_hostname
, new_hostname
,
167 for (a
= ai
; a
!= NULL
; a
= a
->ai_next
) {
168 if (a
->ai_canonname
!= NULL
) {
169 ret
= copy_hostname (context
, a
->ai_canonname
, new_hostname
);
174 strlwr (*new_hostname
);
175 ret
= krb5_get_host_realm (context
, *new_hostname
, realms
);
180 free (*new_hostname
);
184 return vanilla_hostname (context
, orig_hostname
, new_hostname
, realms
);