1/***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9 *
10 * This software is licensed as described in the file COPYING, which
11 * you should have received as part of this distribution. The terms
12 * are also available at https://curl.se/docs/copyright.html.
13 *
14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15 * copies of the Software, and permit persons to whom the Software is
16 * furnished to do so, under the terms of the COPYING file.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ***************************************************************************/
22
23#include "curl_setup.h"
24
25#include "curl_gethostname.h"
26
27/*
28 * Curl_gethostname() is a wrapper around gethostname() which allows
29 * overriding the host name that the function would normally return.
30 * This capability is used by the test suite to verify exact matching
31 * of NTLM authentication, which exercises libcurl's MD4 and DES code
32 * as well as by the SMTP module when a hostname is not provided.
33 *
34 * For libcurl debug enabled builds host name overriding takes place
35 * when environment variable CURL_GETHOSTNAME is set, using the value
36 * held by the variable to override returned host name.
37 *
38 * Note: The function always returns the un-qualified hostname rather
39 * than being provider dependent.
40 *
41 * For libcurl shared library release builds the test suite preloads
42 * another shared library named libhostname using the LD_PRELOAD
43 * mechanism which intercepts, and might override, the gethostname()
44 * function call. In this case a given platform must support the
45 * LD_PRELOAD mechanism and additionally have environment variable
46 * CURL_GETHOSTNAME set in order to override the returned host name.
47 *
48 * For libcurl static library release builds no overriding takes place.
49 */
50
51int Curl_gethostname(char * const name, GETHOSTNAME_TYPE_ARG2 namelen)
52{
53#ifndef HAVE_GETHOSTNAME
54
55 /* Allow compilation and return failure when unavailable */
56 (void) name;
57 (void) namelen;
58 return -1;
59
60#else
61 int err;
62 char *dot;
63
64#ifdef DEBUGBUILD
65
66 /* Override host name when environment variable CURL_GETHOSTNAME is set */
67 const char *force_hostname = getenv("CURL_GETHOSTNAME");
68 if(force_hostname) {
69 strncpy(name, force_hostname, namelen);
70 err = 0;
71 }
72 else {
73 name[0] = '\0';
74 err = gethostname(name, namelen);
75 }
76
77#else /* DEBUGBUILD */
78
79 /* The call to system's gethostname() might get intercepted by the
80 libhostname library when libcurl is built as a non-debug shared
81 library when running the test suite. */
82 name[0] = '\0';
83 err = gethostname(name, namelen);
84
85#endif
86
87 name[namelen - 1] = '\0';
88
89 if(err)
90 return err;
91
92 /* Truncate domain, leave only machine name */
93 dot = strchr(name, '.');
94 if(dot)
95 *dot = '\0';
96
97 return 0;
98#endif
99
100}
101