1 /* ====================================================================
2 * The Apache Software License, Version 1.1
4 * Copyright (c) 2000 The Apache Software Foundation. All rights
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. The end-user documentation included with the redistribution,
20 * if any, must include the following acknowledgment:
21 * "This product includes software developed by the
22 * Apache Software Foundation (http://www.apache.org/)."
23 * Alternately, this acknowledgment may appear in the software itself,
24 * if and wherever such third-party acknowledgments normally appear.
26 * 4. The names "Apache" and "Apache Software Foundation" must
27 * not be used to endorse or promote products derived from this
28 * software without prior written permission. For written
29 * permission, please contact apache@apache.org.
31 * 5. Products derived from this software may not be called "Apache",
32 * nor may "Apache" appear in their name, without prior written
33 * permission of the Apache Software Foundation.
35 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
36 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
37 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
38 * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
39 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
42 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
43 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
44 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
45 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47 * ====================================================================
49 * This software consists of voluntary contributions made by many
50 * individuals on behalf of the Apache Software Foundation. For more
51 * information on the Apache Software Foundation, please see
52 * <http://www.apache.org/>.
54 * Portions of this software are based upon public domain software
55 * originally written at the National Center for Supercomputing Applications,
56 * University of Illinois, Urbana-Champaign.
61 #include "apr_strings.h"
67 /* Returns TRUE if the input string is a string
68 * of one or more '.' characters.
70 static BOOL OnlyDots(char *pString)
77 for (c = pString;*c;c++)
84 /* Accepts as input a pathname, and tries to match it to an
85 * existing path and return the pathname in the case that
86 * is present on the existing path. This routine also
87 * converts alias names to long names.
89 API_EXPORT(char *) ap_os_systemcase_filename(apr_pool_t *pPool,
92 char buf[HUGE_STRING_LEN];
96 BOOL bFileExists = TRUE;
100 if (!szFile || strlen(szFile) == 0 || strlen(szFile) >= sizeof(buf))
101 return apr_pstrdup(pPool, "");
104 pInputName = apr_pstrdup(pPool, szFile);
106 /* First convert all slashes to \ so Win32 calls work OK */
107 for (p = pInputName; *p; p++) {
113 /* If there is drive information, copy it over. */
114 if (pInputName[1] == ':') {
115 buf[0] = tolower(*p++);
119 /* If all we have is a drive letter, then we are done */
120 if (strlen(pInputName) == 2)
127 if (*p == '\\') /* Possible UNC name */
130 /* Get past the machine name. FindFirstFile */
131 /* will not find a machine name only */
136 /* Get past the share name. FindFirstFile */
137 /* will not find a \\machine\share name only */
157 if (strchr(q, '*') || strchr(q, '?'))
160 /* If the path exists so far, call FindFirstFile
161 * again. However, if this portion of the path contains
162 * only '.' charaters, skip the call to FindFirstFile
163 * since it will convert '.' and '..' to actual names.
164 * Note: in the call to OnlyDots, we may have to skip
167 if (bFileExists && !OnlyDots((*q == '.' ? q : q+1))) {
168 hFind = FindFirstFile(pInputName, &wfd);
170 if (hFind == INVALID_HANDLE_VALUE) {
178 strcat(buf, wfd.cFileName);
182 if (!bFileExists || OnlyDots((*q == '.' ? q : q+1))) {
196 /* First convert all slashes to / so server code handles it ok */
197 for (p = buf; *p; p++) {
202 return apr_pstrdup(pPool, buf);
206 /* Perform canonicalization with the exception that the
207 * input case is preserved.
209 API_EXPORT(char *) ap_os_case_canonical_filename(apr_pool_t *pPool,
217 if (szFile == NULL || strlen(szFile) == 0)
218 return apr_pstrdup(pPool, "");
220 pNewStr = apr_pstrdup(pPool, szFile);
222 /* Change all '\' characters to '/' characters.
223 * While doing this, remove any trailing '.'.
224 * Also, blow away any directories with 3 or
227 for (p = pNewStr,s = pNewStr; *s; s++,p++) {
228 if (*s == '\\' || *s == '/') {
231 while (p > pNewStr && *(p-1) == '.')
234 if (p == pNewStr && q-p <= 2 && *p == '.')
236 else if (p > pNewStr && p < q && *(p-1) == '/') {
251 /* Blow away any final trailing '.' since on Win32
252 * foo.bat == foo.bat. == foo.bat... etc.
253 * Also blow away any trailing spaces since
254 * "filename" == "filename "
257 while (p > pNewStr && (*(p-1) == '.' || *(p-1) == ' '))
260 (p == pNewStr && q-p > 2))
264 /* One more security issue to deal with. Win32 allows
265 * you to create long filenames. However, alias filenames
266 * are always created so that the filename will
267 * conform to 8.3 rules. According to the Microsoft
268 * Developer's network CD (1/98)
269 * "Automatically generated aliases are composed of the
270 * first six characters of the filename plus ~n
271 * (where n is a number) and the first three characters
272 * after the last period."
273 * Here, we attempt to detect and decode these names.
275 p = strchr(pNewStr, '~');
277 char *pConvertedName, *pQstr, *pPstr;
278 char buf[HUGE_STRING_LEN];
279 /* We potentially have a short name. Call
280 * ap_os_systemcase_filename to examine the filesystem
281 * and possibly extract the long name.
283 pConvertedName = ap_os_systemcase_filename(pPool, pNewStr);
285 /* Since we want to preserve the incoming case as much
286 * as we can, compare for differences in the string and
287 * only substitute in the path names that changed.
289 if (stricmp(pNewStr, pConvertedName)) {
292 q = pQstr = pConvertedName;
303 if (stricmp(pQstr, pPstr))
304 strcat(buf, pQstr); /* Converted name */
306 strcat(buf, pPstr); /* Original name */
318 pNewStr = apr_pstrdup(pPool, buf);
326 /* Perform complete canonicalization.
328 API_EXPORT(char *) ap_os_canonical_filename(apr_pool_t *pPool, const char *szFile)
331 pNewName = ap_os_case_canonical_filename(pPool, szFile);
337 * ap_os_is_filename_valid is given a filename, and returns 0 if the filename
338 * is not valid for use on this system. On Windows, this means it fails any
339 * of the tests below. Otherwise returns 1.
341 * Test for filename validity on Win32. This is of tests come in part from
342 * the MSDN article at "Technical Articles, Windows Platform, Base Services,
343 * Guidelines, Making Room for Long Filenames" although the information
344 * in MSDN about filename testing is incomplete or conflicting. There is a
345 * similar set of tests in "Technical Articles, Windows Platform, Base Services,
346 * Guidelines, Moving Unix Applications to Windows NT".
350 * 1) total path length greater than MAX_PATH
352 * 2) anything using the octets 0-31 or characters " < > | :
353 * (these are reserved for Windows use in filenames. In addition
354 * each file system has its own additional characters that are
355 * invalid. See KB article Q100108 for more details).
357 * 3) anything ending in "." (no matter how many)
358 * (filename doc, doc. and doc... all refer to the same file)
360 * 4) any segment in which the basename (before first period) matches
361 * one of the DOS device names
362 * (the list comes from KB article Q100108 although some people
363 * reports that additional names such as "COM5" are also special
366 * If the path fails ANY of these tests, the result must be to deny access.
369 API_EXPORT(int) ap_os_is_filename_valid(const char *file)
371 const char *segstart;
372 unsigned int seglength;
374 static const char * const invalid_characters = "?\"<>*|:";
375 static const char * const invalid_filenames[] = {
376 "CON", "AUX", "COM1", "COM2", "COM3",
377 "COM4", "LPT1", "LPT2", "LPT3", "PRN", "NUL", NULL
381 if (strlen(file) >= MAX_PATH) {
382 /* Path too long for Windows. Note that this test is not valid
383 * if the path starts with //?/ or \\?\. */
389 /* Skip any leading non-path components. This can be either a
390 * drive letter such as C:, or a UNC path such as \\SERVER\SHARE\.
391 * We continue and check the rest of the path based on the rules above.
392 * This means we could eliminate valid filenames from servers which
393 * are not running NT (such as Samba).
396 if (pos[0] && pos[1] == ':') {
397 /* Skip leading drive letter */
401 if ((pos[0] == '\\' || pos[0] == '/') &&
402 (pos[1] == '\\' || pos[1] == '/')) {
403 /* Is a UNC, so skip the server name and share name */
405 while (*pos && *pos != '/' && *pos != '\\')
411 pos++; /* Move to start of share name */
412 while (*pos && *pos != '/' && *pos != '\\')
415 /* No path information */
423 unsigned int baselength;
425 while (*pos == '/' || *pos == '\\') {
431 segstart = pos; /* start of segment */
432 while (*pos && *pos != '/' && *pos != '\\') {
435 seglength = pos - segstart;
437 * Now we have a segment of the path, starting at position "segstart"
438 * and length "seglength"
442 for (idx = 0; idx < seglength; idx++) {
443 if ((segstart[idx] > 0 && segstart[idx] < 32) ||
444 strchr(invalid_characters, segstart[idx])) {
450 if (segstart[seglength-1] == '.') {
455 for (baselength = 0; baselength < seglength; baselength++) {
456 if (segstart[baselength] == '.') {
461 /* baselength is the number of characters in the base path of
462 * the segment (which could be the same as the whole segment length,
463 * if it does not include any dot characters). */
464 if (baselength == 3 || baselength == 4) {
465 for (idx = 0; invalid_filenames[idx]; idx++) {
466 if (strlen(invalid_filenames[idx]) == baselength &&
467 !strnicmp(invalid_filenames[idx], segstart, baselength)) {