1 /***************************************************************************
2 * _ _ ____ _
3 * Project ___| | | | _ \| |
4 * / __| | | | |_) | |
5 * | (__| |_| | _ <| |___
6 * \___|\___/|_| \_\_____|
7 *
8 * Copyright (C) 1998 - 2014, 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 http://curl.haxx.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 #include "tool_setup.h"
23
24 #if defined(MSDOS) || defined(WIN32)
25
26 #if defined(HAVE_LIBGEN_H) && defined(HAVE_BASENAME)
27 # include <libgen.h>
28 #endif
29
30 #ifdef WIN32
31 # include "tool_cfgable.h"
32 # include "tool_libinfo.h"
33 #endif
34
35 #include "tool_bname.h"
36 #include "tool_doswin.h"
37
38 #include "memdebug.h" /* keep this as LAST include */
39
40 /*
41 * Macros ALWAYS_TRUE and ALWAYS_FALSE are used to avoid compiler warnings.
42 */
43
44 #define ALWAYS_TRUE (1)
45 #define ALWAYS_FALSE (0)
46
47 #if defined(_MSC_VER) && !defined(__POCC__)
48 # undef ALWAYS_TRUE
49 # undef ALWAYS_FALSE
50 # if (_MSC_VER < 1500)
51 # define ALWAYS_TRUE (0, 1)
52 # define ALWAYS_FALSE (1, 0)
53 # else
54 # define ALWAYS_TRUE \
55 __pragma(warning(push)) \
56 __pragma(warning(disable:4127)) \
57 (1) \
58 __pragma(warning(pop))
59 # define ALWAYS_FALSE \
60 __pragma(warning(push)) \
61 __pragma(warning(disable:4127)) \
62 (0) \
63 __pragma(warning(pop))
64 # endif
65 #endif
66
67 #ifdef WIN32
68 # undef PATH_MAX
69 # define PATH_MAX MAX_PATH
70 #endif
71
72 #ifndef S_ISCHR
73 # ifdef S_IFCHR
74 # define S_ISCHR(m) (((m) & S_IFMT) == S_IFCHR)
75 # else
76 # define S_ISCHR(m) (0) /* cannot tell if file is a device */
77 # endif
78 #endif
79
80 #ifdef WIN32
81 # define _use_lfn(f) ALWAYS_TRUE /* long file names always available */
82 #elif !defined(__DJGPP__) || (__DJGPP__ < 2) /* DJGPP 2.0 has _use_lfn() */
83 # define _use_lfn(f) ALWAYS_FALSE /* long file names never available */
84 #elif defined(__DJGPP__)
85 # include <fcntl.h> /* _use_lfn(f) prototype */
86 #endif
87
88 static const char *msdosify (const char *file_name);
89 static char *rename_if_dos_device_name (char *file_name);
90
91 /*
92 * sanitize_dos_name: returns a newly allocated string holding a
93 * valid file name which will be a transformation of given argument
94 * in case this wasn't already a valid file name.
95 *
96 * This function takes ownership of given argument, free'ing it before
97 * returning. Caller is responsible of free'ing returned string. Upon
98 * out of memory condition function returns NULL.
99 */
100
sanitize_dos_name(char * file_name)101 char *sanitize_dos_name(char *file_name)
102 {
103 char new_name[PATH_MAX];
104
105 if(!file_name)
106 return NULL;
107
108 if(strlen(file_name) >= PATH_MAX)
109 file_name[PATH_MAX-1] = '\0'; /* truncate it */
110
111 strcpy(new_name, msdosify(file_name));
112
113 Curl_safefree(file_name);
114
115 return strdup(rename_if_dos_device_name(new_name));
116 }
117
118 /* The following functions are taken with modification from the DJGPP
119 * port of tar 1.12. They use algorithms originally from DJTAR. */
120
msdosify(const char * file_name)121 static const char *msdosify (const char *file_name)
122 {
123 static char dos_name[PATH_MAX];
124 static const char illegal_chars_dos[] = ".+, ;=[]" /* illegal in DOS */
125 "|<>\\\":?*"; /* illegal in DOS & W95 */
126 static const char *illegal_chars_w95 = &illegal_chars_dos[8];
127 int idx, dot_idx;
128 const char *s = file_name;
129 char *d = dos_name;
130 const char *const dlimit = dos_name + sizeof(dos_name) - 1;
131 const char *illegal_aliens = illegal_chars_dos;
132 size_t len = sizeof(illegal_chars_dos) - 1;
133
134 /* Support for Windows 9X VFAT systems, when available. */
135 if(_use_lfn(file_name)) {
136 illegal_aliens = illegal_chars_w95;
137 len -= (illegal_chars_w95 - illegal_chars_dos);
138 }
139
140 /* Get past the drive letter, if any. */
141 if(s[0] >= 'A' && s[0] <= 'z' && s[1] == ':') {
142 *d++ = *s++;
143 *d++ = *s++;
144 }
145
146 for(idx = 0, dot_idx = -1; *s && d < dlimit; s++, d++) {
147 if(memchr(illegal_aliens, *s, len)) {
148 /* Dots are special: DOS doesn't allow them as the leading character,
149 and a file name cannot have more than a single dot. We leave the
150 first non-leading dot alone, unless it comes too close to the
151 beginning of the name: we want sh.lex.c to become sh_lex.c, not
152 sh.lex-c. */
153 if(*s == '.') {
154 if(idx == 0 && (s[1] == '/' || (s[1] == '.' && s[2] == '/'))) {
155 /* Copy "./" and "../" verbatim. */
156 *d++ = *s++;
157 if(*s == '.')
158 *d++ = *s++;
159 *d = *s;
160 }
161 else if(idx == 0)
162 *d = '_';
163 else if(dot_idx >= 0) {
164 if(dot_idx < 5) { /* 5 is a heuristic ad-hoc'ery */
165 d[dot_idx - idx] = '_'; /* replace previous dot */
166 *d = '.';
167 }
168 else
169 *d = '-';
170 }
171 else
172 *d = '.';
173
174 if(*s == '.')
175 dot_idx = idx;
176 }
177 else if(*s == '+' && s[1] == '+') {
178 if(idx - 2 == dot_idx) { /* .c++, .h++ etc. */
179 *d++ = 'x';
180 *d = 'x';
181 }
182 else {
183 /* libg++ etc. */
184 memcpy (d, "plus", 4);
185 d += 3;
186 }
187 s++;
188 idx++;
189 }
190 else
191 *d = '_';
192 }
193 else
194 *d = *s;
195 if(*s == '/') {
196 idx = 0;
197 dot_idx = -1;
198 }
199 else
200 idx++;
201 }
202
203 *d = '\0';
204 return dos_name;
205 }
206
rename_if_dos_device_name(char * file_name)207 static char *rename_if_dos_device_name (char *file_name)
208 {
209 /* We could have a file whose name is a device on MS-DOS. Trying to
210 * retrieve such a file would fail at best and wedge us at worst. We need
211 * to rename such files. */
212 char *base;
213 struct_stat st_buf;
214 char fname[PATH_MAX];
215
216 strncpy(fname, file_name, PATH_MAX-1);
217 fname[PATH_MAX-1] = '\0';
218 base = basename(fname);
219 if(((stat(base, &st_buf)) == 0) && (S_ISCHR(st_buf.st_mode))) {
220 size_t blen = strlen(base);
221
222 if(strlen(fname) >= PATH_MAX-1) {
223 /* Make room for the '_' */
224 blen--;
225 base[blen] = '\0';
226 }
227 /* Prepend a '_'. */
228 memmove(base + 1, base, blen + 1);
229 base[0] = '_';
230 strcpy(file_name, fname);
231 }
232 return file_name;
233 }
234
235 #if defined(MSDOS) && (defined(__DJGPP__) || defined(__GO32__))
236
237 /*
238 * Disable program default argument globbing. We do it on our own.
239 */
__crt0_glob_function(char * arg)240 char **__crt0_glob_function(char *arg)
241 {
242 (void)arg;
243 return (char**)0;
244 }
245
246 #endif /* MSDOS && (__DJGPP__ || __GO32__) */
247
248 #ifdef WIN32
249
250 /*
251 * Function to find CACert bundle on a Win32 platform using SearchPath.
252 * (SearchPath is already declared via inclusions done in setup header file)
253 * (Use the ASCII version instead of the unicode one!)
254 * The order of the directories it searches is:
255 * 1. application's directory
256 * 2. current working directory
257 * 3. Windows System directory (e.g. C:\windows\system32)
258 * 4. Windows Directory (e.g. C:\windows)
259 * 5. all directories along %PATH%
260 *
261 * For WinXP and later search order actually depends on registry value:
262 * HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\SafeProcessSearchMode
263 */
264
FindWin32CACert(struct OperationConfig * config,const char * bundle_file)265 CURLcode FindWin32CACert(struct OperationConfig *config,
266 const char *bundle_file)
267 {
268 CURLcode result = CURLE_OK;
269
270 /* search and set cert file only if libcurl supports SSL */
271 if(curlinfo->features & CURL_VERSION_SSL) {
272
273 DWORD res_len;
274 DWORD buf_tchar_size = PATH_MAX + 1;
275 DWORD buf_bytes_size = sizeof(TCHAR) * buf_tchar_size;
276 char *ptr = NULL;
277
278 char *buf = malloc(buf_bytes_size);
279 if(!buf)
280 return CURLE_OUT_OF_MEMORY;
281 buf[0] = '\0';
282
283 res_len = SearchPathA(NULL, bundle_file, NULL, buf_tchar_size, buf, &ptr);
284 if(res_len > 0) {
285 Curl_safefree(config->cacert);
286 config->cacert = strdup(buf);
287 if(!config->cacert)
288 result = CURLE_OUT_OF_MEMORY;
289 }
290
291 Curl_safefree(buf);
292 }
293
294 return result;
295 }
296
297 #endif /* WIN32 */
298
299 #endif /* MSDOS || WIN32 */
300