Skip to content

Commit 42be24a

Browse files
captain-caveman2kbagder
authored andcommitted
Curl_gethostname: return un-qualified machine name
Fixed Curl_gethostname() so that it always returns the un-qualified machine name rather than being dependent on the socket provider. Note: The return of getenv("CURL_GETHOSTNAME") is also parsed in case the developer / test harness provided a fully qualified domain name as it's value as well.
1 parent 260b0f4 commit 42be24a

File tree

1 file changed

+37
-6
lines changed

1 file changed

+37
-6
lines changed

lib/curl_gethostname.c

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,16 +28,23 @@
2828

2929
#include "curl_gethostname.h"
3030

31+
/* Hostname buffer size */
32+
#define HOSTNAME_MAX 1024
33+
3134
/*
3235
* Curl_gethostname() is a wrapper around gethostname() which allows
3336
* overriding the host name that the function would normally return.
3437
* This capability is used by the test suite to verify exact matching
35-
* of NTLM authentication, which exercises libcurl's MD4 and DES code.
38+
* of NTLM authentication, which exercises libcurl's MD4 and DES code
39+
* as well as by the SMTP module when a hostname is not provided.
3640
*
3741
* For libcurl debug enabled builds host name overriding takes place
3842
* when environment variable CURL_GETHOSTNAME is set, using the value
3943
* held by the variable to override returned host name.
4044
*
45+
* Note: The function always returns the un-qualified hostname rather
46+
* than being provider dependent.
47+
*
4148
* For libcurl shared library release builds the test suite preloads
4249
* another shared library named libhostname using the LD_PRELOAD
4350
* mechanism which intercepts, and might override, the gethostname()
@@ -58,24 +65,48 @@ int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) {
5865
return -1;
5966

6067
#else
68+
int err = 0;
69+
char* dot = NULL;
70+
char hostname[HOSTNAME_MAX + 1];
6171

6272
#ifdef DEBUGBUILD
6373

6474
/* Override host name when environment variable CURL_GETHOSTNAME is set */
6575
const char *force_hostname = getenv("CURL_GETHOSTNAME");
6676
if(force_hostname) {
67-
strncpy(name, force_hostname, namelen);
68-
name[namelen-1] = '\0';
69-
return 0;
77+
strncpy(hostname, force_hostname, sizeof(hostname));
78+
hostname[sizeof(hostname) - 1] = '\0';
7079
}
80+
else
81+
err = gethostname(hostname, sizeof(hostname));
7182

72-
#endif /* DEBUGBUILD */
83+
#else /* DEBUGBUILD */
7384

7485
/* The call to system's gethostname() might get intercepted by the
7586
libhostname library when libcurl is built as a non-debug shared
7687
library when running the test suite. */
77-
return gethostname(name, namelen);
88+
err = gethostname(hostname, sizeof(hostname));
89+
90+
#endif
91+
92+
if(err != 0)
93+
return err;
94+
95+
/* Is the hostname fully qualified? */
96+
dot = strchr(hostname, '.');
97+
if(dot) {
98+
/* Copy only the machine name to the specified buffer */
99+
size_t size = dot - hostname;
100+
strncpy(name, hostname, namelen > size ? size : namelen);
101+
name[(namelen > size ? size : namelen) - 1] = '\0';
102+
}
103+
else {
104+
/* Copy the hostname to the specified buffer */
105+
strncpy(name, hostname, namelen);
106+
name[namelen - 1] = '\0';
107+
}
78108

109+
return 0;
79110
#endif
80111

81112
}

0 commit comments

Comments
 (0)