28
28
29
29
#include "curl_gethostname.h"
30
30
31
+ /* Hostname buffer size */
32
+ #define HOSTNAME_MAX 1024
33
+
31
34
/*
32
35
* Curl_gethostname() is a wrapper around gethostname() which allows
33
36
* overriding the host name that the function would normally return.
34
37
* 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.
36
40
*
37
41
* For libcurl debug enabled builds host name overriding takes place
38
42
* when environment variable CURL_GETHOSTNAME is set, using the value
39
43
* held by the variable to override returned host name.
40
44
*
45
+ * Note: The function always returns the un-qualified hostname rather
46
+ * than being provider dependent.
47
+ *
41
48
* For libcurl shared library release builds the test suite preloads
42
49
* another shared library named libhostname using the LD_PRELOAD
43
50
* mechanism which intercepts, and might override, the gethostname()
@@ -58,24 +65,48 @@ int Curl_gethostname(char *name, GETHOSTNAME_TYPE_ARG2 namelen) {
58
65
return -1 ;
59
66
60
67
#else
68
+ int err = 0 ;
69
+ char * dot = NULL ;
70
+ char hostname [HOSTNAME_MAX + 1 ];
61
71
62
72
#ifdef DEBUGBUILD
63
73
64
74
/* Override host name when environment variable CURL_GETHOSTNAME is set */
65
75
const char * force_hostname = getenv ("CURL_GETHOSTNAME" );
66
76
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' ;
70
79
}
80
+ else
81
+ err = gethostname (hostname , sizeof (hostname ));
71
82
72
- #endif /* DEBUGBUILD */
83
+ #else /* DEBUGBUILD */
73
84
74
85
/* The call to system's gethostname() might get intercepted by the
75
86
libhostname library when libcurl is built as a non-debug shared
76
87
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
+ }
78
108
109
+ return 0 ;
79
110
#endif
80
111
81
112
}
0 commit comments