00001 /*************************************************************************** 00002 * _ _ ____ _ 00003 * Project ___| | | | _ \| | 00004 * / __| | | | |_) | | 00005 * | (__| |_| | _ <| |___ 00006 * \___|\___/|_| \_\_____| 00007 * 00008 * Copyright (C) 1998 - 2016, Daniel Stenberg, <daniel@haxx.se>, et al. 00009 * 00010 * This software is licensed as described in the file COPYING, which 00011 * you should have received as part of this distribution. The terms 00012 * are also available at https://curl.haxx.se/docs/copyright.html. 00013 * 00014 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 00015 * copies of the Software, and permit persons to whom the Software is 00016 * furnished to do so, under the terms of the COPYING file. 00017 * 00018 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 00019 * KIND, either express or implied. 00020 * 00021 ***************************************************************************/ 00022 00023 #include "curl_setup.h" 00024 00025 #include "curl_gethostname.h" 00026 00027 /* 00028 * Curl_gethostname() is a wrapper around gethostname() which allows 00029 * overriding the host name that the function would normally return. 00030 * This capability is used by the test suite to verify exact matching 00031 * of NTLM authentication, which exercises libcurl's MD4 and DES code 00032 * as well as by the SMTP module when a hostname is not provided. 00033 * 00034 * For libcurl debug enabled builds host name overriding takes place 00035 * when environment variable CURL_GETHOSTNAME is set, using the value 00036 * held by the variable to override returned host name. 00037 * 00038 * Note: The function always returns the un-qualified hostname rather 00039 * than being provider dependent. 00040 * 00041 * For libcurl shared library release builds the test suite preloads 00042 * another shared library named libhostname using the LD_PRELOAD 00043 * mechanism which intercepts, and might override, the gethostname() 00044 * function call. In this case a given platform must support the 00045 * LD_PRELOAD mechanism and additionally have environment variable 00046 * CURL_GETHOSTNAME set in order to override the returned host name. 00047 * 00048 * For libcurl static library release builds no overriding takes place. 00049 */ 00050 00051 int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) 00052 { 00053 #ifndef HAVE_GETHOSTNAME 00054 00055 /* Allow compilation and return failure when unavailable */ 00056 (void) name; 00057 (void) namelen; 00058 return -1; 00059 00060 #else 00061 int err; 00062 char *dot; 00063 00064 #ifdef DEBUGBUILD 00065 00066 /* Override host name when environment variable CURL_GETHOSTNAME is set */ 00067 const char *force_hostname = getenv("CURL_GETHOSTNAME"); 00068 if(force_hostname) { 00069 strncpy(name, force_hostname, namelen); 00070 err = 0; 00071 } 00072 else { 00073 name[0] = '\0'; 00074 err = gethostname(name, namelen); 00075 } 00076 00077 #else /* DEBUGBUILD */ 00078 00079 /* The call to system's gethostname() might get intercepted by the 00080 libhostname library when libcurl is built as a non-debug shared 00081 library when running the test suite. */ 00082 name[0] = '\0'; 00083 err = gethostname(name, namelen); 00084 00085 #endif 00086 00087 name[namelen - 1] = '\0'; 00088 00089 if(err) 00090 return err; 00091 00092 /* Truncate domain, leave only machine name */ 00093 dot = strchr(name, '.'); 00094 if(dot) 00095 *dot = '\0'; 00096 00097 return 0; 00098 #endif 00099 00100 }