113498266Sopenharmony_ci/*************************************************************************** 213498266Sopenharmony_ci * _ _ ____ _ 313498266Sopenharmony_ci * Project ___| | | | _ \| | 413498266Sopenharmony_ci * / __| | | | |_) | | 513498266Sopenharmony_ci * | (__| |_| | _ <| |___ 613498266Sopenharmony_ci * \___|\___/|_| \_\_____| 713498266Sopenharmony_ci * 813498266Sopenharmony_ci * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 913498266Sopenharmony_ci * 1013498266Sopenharmony_ci * This software is licensed as described in the file COPYING, which 1113498266Sopenharmony_ci * you should have received as part of this distribution. The terms 1213498266Sopenharmony_ci * are also available at https://curl.se/docs/copyright.html. 1313498266Sopenharmony_ci * 1413498266Sopenharmony_ci * You may opt to use, copy, modify, merge, publish, distribute and/or sell 1513498266Sopenharmony_ci * copies of the Software, and permit persons to whom the Software is 1613498266Sopenharmony_ci * furnished to do so, under the terms of the COPYING file. 1713498266Sopenharmony_ci * 1813498266Sopenharmony_ci * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 1913498266Sopenharmony_ci * KIND, either express or implied. 2013498266Sopenharmony_ci * 2113498266Sopenharmony_ci * SPDX-License-Identifier: curl 2213498266Sopenharmony_ci * 2313498266Sopenharmony_ci ***************************************************************************/ 2413498266Sopenharmony_ci 2513498266Sopenharmony_ci#include "curl_setup.h" 2613498266Sopenharmony_ci 2713498266Sopenharmony_ci#include "curl_gethostname.h" 2813498266Sopenharmony_ci 2913498266Sopenharmony_ci/* 3013498266Sopenharmony_ci * Curl_gethostname() is a wrapper around gethostname() which allows 3113498266Sopenharmony_ci * overriding the host name that the function would normally return. 3213498266Sopenharmony_ci * This capability is used by the test suite to verify exact matching 3313498266Sopenharmony_ci * of NTLM authentication, which exercises libcurl's MD4 and DES code 3413498266Sopenharmony_ci * as well as by the SMTP module when a hostname is not provided. 3513498266Sopenharmony_ci * 3613498266Sopenharmony_ci * For libcurl debug enabled builds host name overriding takes place 3713498266Sopenharmony_ci * when environment variable CURL_GETHOSTNAME is set, using the value 3813498266Sopenharmony_ci * held by the variable to override returned host name. 3913498266Sopenharmony_ci * 4013498266Sopenharmony_ci * Note: The function always returns the un-qualified hostname rather 4113498266Sopenharmony_ci * than being provider dependent. 4213498266Sopenharmony_ci * 4313498266Sopenharmony_ci * For libcurl shared library release builds the test suite preloads 4413498266Sopenharmony_ci * another shared library named libhostname using the LD_PRELOAD 4513498266Sopenharmony_ci * mechanism which intercepts, and might override, the gethostname() 4613498266Sopenharmony_ci * function call. In this case a given platform must support the 4713498266Sopenharmony_ci * LD_PRELOAD mechanism and additionally have environment variable 4813498266Sopenharmony_ci * CURL_GETHOSTNAME set in order to override the returned host name. 4913498266Sopenharmony_ci * 5013498266Sopenharmony_ci * For libcurl static library release builds no overriding takes place. 5113498266Sopenharmony_ci */ 5213498266Sopenharmony_ci 5313498266Sopenharmony_ciint Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen) 5413498266Sopenharmony_ci{ 5513498266Sopenharmony_ci#ifndef HAVE_GETHOSTNAME 5613498266Sopenharmony_ci 5713498266Sopenharmony_ci /* Allow compilation and return failure when unavailable */ 5813498266Sopenharmony_ci (void) name; 5913498266Sopenharmony_ci (void) namelen; 6013498266Sopenharmony_ci return -1; 6113498266Sopenharmony_ci 6213498266Sopenharmony_ci#else 6313498266Sopenharmony_ci int err; 6413498266Sopenharmony_ci char *dot; 6513498266Sopenharmony_ci 6613498266Sopenharmony_ci#ifdef DEBUGBUILD 6713498266Sopenharmony_ci 6813498266Sopenharmony_ci /* Override host name when environment variable CURL_GETHOSTNAME is set */ 6913498266Sopenharmony_ci const char *force_hostname = getenv("CURL_GETHOSTNAME"); 7013498266Sopenharmony_ci if(force_hostname) { 7113498266Sopenharmony_ci strncpy(name, force_hostname, namelen); 7213498266Sopenharmony_ci err = 0; 7313498266Sopenharmony_ci } 7413498266Sopenharmony_ci else { 7513498266Sopenharmony_ci name[0] = '\0'; 7613498266Sopenharmony_ci err = gethostname(name, namelen); 7713498266Sopenharmony_ci } 7813498266Sopenharmony_ci 7913498266Sopenharmony_ci#else /* DEBUGBUILD */ 8013498266Sopenharmony_ci 8113498266Sopenharmony_ci /* The call to system's gethostname() might get intercepted by the 8213498266Sopenharmony_ci libhostname library when libcurl is built as a non-debug shared 8313498266Sopenharmony_ci library when running the test suite. */ 8413498266Sopenharmony_ci name[0] = '\0'; 8513498266Sopenharmony_ci err = gethostname(name, namelen); 8613498266Sopenharmony_ci 8713498266Sopenharmony_ci#endif 8813498266Sopenharmony_ci 8913498266Sopenharmony_ci name[namelen - 1] = '\0'; 9013498266Sopenharmony_ci 9113498266Sopenharmony_ci if(err) 9213498266Sopenharmony_ci return err; 9313498266Sopenharmony_ci 9413498266Sopenharmony_ci /* Truncate domain, leave only machine name */ 9513498266Sopenharmony_ci dot = strchr(name, '.'); 9613498266Sopenharmony_ci if(dot) 9713498266Sopenharmony_ci *dot = '\0'; 9813498266Sopenharmony_ci 9913498266Sopenharmony_ci return 0; 10013498266Sopenharmony_ci#endif 10113498266Sopenharmony_ci 10213498266Sopenharmony_ci} 103