Path: blob/main/crypto/heimdal/lib/krb5/expand_hostname.c
34878 views
/*1* Copyright (c) 1999 - 2001 Kungliga Tekniska Högskolan2* (Royal Institute of Technology, Stockholm, Sweden).3* All rights reserved.4*5* Redistribution and use in source and binary forms, with or without6* modification, are permitted provided that the following conditions7* are met:8*9* 1. Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* 2. Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in the14* documentation and/or other materials provided with the distribution.15*16* 3. Neither the name of the Institute nor the names of its contributors17* may be used to endorse or promote products derived from this software18* without specific prior written permission.19*20* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND21* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE22* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE23* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE24* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL25* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS26* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)27* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT28* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY29* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF30* SUCH DAMAGE.31*/3233#include "krb5_locl.h"3435static krb5_error_code36copy_hostname(krb5_context context,37const char *orig_hostname,38char **new_hostname)39{40*new_hostname = strdup (orig_hostname);41if (*new_hostname == NULL) {42krb5_set_error_message(context, ENOMEM,43N_("malloc: out of memory", ""));44return ENOMEM;45}46strlwr (*new_hostname);47return 0;48}4950/**51* krb5_expand_hostname() tries to make orig_hostname into a more52* canonical one in the newly allocated space returned in53* new_hostname.5455* @param context a Keberos context56* @param orig_hostname hostname to canonicalise.57* @param new_hostname output hostname, caller must free hostname with58* krb5_xfree().59*60* @return Return an error code or 0, see krb5_get_error_message().61*62* @ingroup krb5_support63*/6465KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL66krb5_expand_hostname (krb5_context context,67const char *orig_hostname,68char **new_hostname)69{70struct addrinfo *ai, *a, hints;71int error;7273if ((context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) == 0)74return copy_hostname (context, orig_hostname, new_hostname);7576memset (&hints, 0, sizeof(hints));77hints.ai_flags = AI_CANONNAME;7879error = getaddrinfo (orig_hostname, NULL, &hints, &ai);80if (error)81return copy_hostname (context, orig_hostname, new_hostname);82for (a = ai; a != NULL; a = a->ai_next) {83if (a->ai_canonname != NULL) {84*new_hostname = strdup (a->ai_canonname);85freeaddrinfo (ai);86if (*new_hostname == NULL) {87krb5_set_error_message(context, ENOMEM,88N_("malloc: out of memory", ""));89return ENOMEM;90} else {91return 0;92}93}94}95freeaddrinfo (ai);96return copy_hostname (context, orig_hostname, new_hostname);97}9899/*100* handle the case of the hostname being unresolvable and thus identical101*/102103static krb5_error_code104vanilla_hostname (krb5_context context,105const char *orig_hostname,106char **new_hostname,107char ***realms)108{109krb5_error_code ret;110111ret = copy_hostname (context, orig_hostname, new_hostname);112if (ret)113return ret;114strlwr (*new_hostname);115116ret = krb5_get_host_realm (context, *new_hostname, realms);117if (ret) {118free (*new_hostname);119return ret;120}121return 0;122}123124/**125* krb5_expand_hostname_realms() expands orig_hostname to a name we126* believe to be a hostname in newly allocated space in new_hostname127* and return the realms new_hostname is believed to belong to in128* realms.129*130* @param context a Keberos context131* @param orig_hostname hostname to canonicalise.132* @param new_hostname output hostname, caller must free hostname with133* krb5_xfree().134* @param realms output possible realms, is an array that is terminated135* with NULL. Caller must free with krb5_free_host_realm().136*137* @return Return an error code or 0, see krb5_get_error_message().138*139* @ingroup krb5_support140*/141142KRB5_LIB_FUNCTION krb5_error_code KRB5_LIB_CALL143krb5_expand_hostname_realms (krb5_context context,144const char *orig_hostname,145char **new_hostname,146char ***realms)147{148struct addrinfo *ai, *a, hints;149int error;150krb5_error_code ret = 0;151152if ((context->flags & KRB5_CTX_F_DNS_CANONICALIZE_HOSTNAME) == 0)153return vanilla_hostname (context, orig_hostname, new_hostname,154realms);155156memset (&hints, 0, sizeof(hints));157hints.ai_flags = AI_CANONNAME;158159error = getaddrinfo (orig_hostname, NULL, &hints, &ai);160if (error)161return vanilla_hostname (context, orig_hostname, new_hostname,162realms);163164for (a = ai; a != NULL; a = a->ai_next) {165if (a->ai_canonname != NULL) {166ret = copy_hostname (context, a->ai_canonname, new_hostname);167if (ret) {168freeaddrinfo (ai);169return ret;170}171strlwr (*new_hostname);172ret = krb5_get_host_realm (context, *new_hostname, realms);173if (ret == 0) {174freeaddrinfo (ai);175return 0;176}177free (*new_hostname);178}179}180freeaddrinfo(ai);181return vanilla_hostname (context, orig_hostname, new_hostname, realms);182}183184185