1 /* Licensed to the Apache Software Foundation (ASF) under one or more
2 * contributor license agreements. See the NOTICE file distributed with
3 * this work for additional information regarding copyright ownership.
4 * The ASF licenses this file to You under the Apache License, Version 2.0
5 * (the "License"); you may not use this file except in compliance with
6 * the License. You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "apr.h"
18 #include "apr_arch_file_io.h"
19 #include "apr_strings.h"
20
21 /* Win32 Exceptions:
22 *
23 * Note that trailing spaces and trailing periods are never recorded
24 * in the file system, except by a very obscure bug where any file
25 * that is created with a trailing space or period, followed by the
26 * ':' stream designator on an NTFS volume can never be accessed again.
27 * In other words, don't ever accept them when designating a stream!
28 *
29 * An interesting side effect is that two or three periods are both
30 * treated as the parent directory, although the fourth and on are
31 * not [strongly suggest all trailing periods are trimmed off, or
32 * down to two if there are no other characters.]
33 *
34 * Leading spaces and periods are accepted, however.
35 * The * ? < > codes all have wildcard side effects
36 * The " / \ : are exclusively component separator tokens
37 * The system doesn't accept | for any (known) purpose
38 * Oddly, \x7f _is_ acceptable ;)
39 */
40
41 /* apr_c_is_fnchar[] maps Win32's file name and shell escape symbols
42 *
43 * element & 1 == valid file name character [excluding delimiters]
44 * element & 2 == character should be shell (caret) escaped from cmd.exe
45 *
46 * this must be in-sync with Apache httpd's gen_test_char.c for cgi escaping.
47 */
48
49 const char apr_c_is_fnchar[256] =
50 {/* Reject all ctrl codes... Escape \n and \r (ascii 10 and 13) */
51 0,0,0,0,0,0,0,0,0,0,2,0,0,2,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
52 /* ! " # $ % & ' ( ) * + , - . / 0 1 2 3 4 5 6 7 8 9 : ; < = > ? */
53 1,1,2,1,3,3,3,3,3,3,2,1,1,1,1,0, 1,1,1,1,1,1,1,1,1,1,0,3,2,1,2,2,
54 /* @ A B C D E F G H I J K L M N O P Q R S T U V W X Y Z [ \ ] ^ _ */
55 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
56 /* ` a b c d e f g h i j k l m n o p q r s t u v w x y z { | } ~ */
57 3,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,3,2,3,3,1,
58 /* High bit codes are accepted (subject to utf-8->Unicode xlation) */
59 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
60 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
61 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
62 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1
63 };
64
65
filepath_root_test(char * path,apr_pool_t * p)66 apr_status_t filepath_root_test(char *path, apr_pool_t *p)
67 {
68 apr_status_t rv;
69 #if APR_HAS_UNICODE_FS
70 if (apr_os_level >= APR_WIN_NT)
71 {
72 apr_wchar_t wpath[APR_PATH_MAX];
73 if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
74 / sizeof(apr_wchar_t), path)))
75 return rv;
76 rv = GetDriveTypeW(wpath);
77 }
78 else
79 #endif
80 rv = GetDriveType(path);
81
82 if (rv == DRIVE_UNKNOWN || rv == DRIVE_NO_ROOT_DIR)
83 return APR_EBADPATH;
84 return APR_SUCCESS;
85 }
86
87
filepath_drive_get(char ** rootpath,char drive,apr_int32_t flags,apr_pool_t * p)88 apr_status_t filepath_drive_get(char **rootpath, char drive,
89 apr_int32_t flags, apr_pool_t *p)
90 {
91 char path[APR_PATH_MAX];
92 #if APR_HAS_UNICODE_FS
93 IF_WIN_OS_IS_UNICODE
94 {
95 apr_wchar_t *ignored;
96 apr_wchar_t wdrive[8];
97 apr_wchar_t wpath[APR_PATH_MAX];
98 apr_status_t rv;
99 /* ???: This needs review, apparently "\\?\d:." returns "\\?\d:"
100 * as if that is useful for anything.
101 */
102 wcscpy(wdrive, L"D:.");
103 wdrive[0] = (apr_wchar_t)(unsigned char)drive;
104 if (!GetFullPathNameW(wdrive, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
105 return apr_get_os_error();
106 if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
107 return rv;
108 }
109 #endif
110 #if APR_HAS_ANSI_FS
111 ELSE_WIN_OS_IS_ANSI
112 {
113 char *ignored;
114 char drivestr[4];
115 drivestr[0] = drive;
116 drivestr[1] = ':';
117 drivestr[2] = '.';;
118 drivestr[3] = '\0';
119 if (!GetFullPathName(drivestr, sizeof(path), path, &ignored))
120 return apr_get_os_error();
121 }
122 #endif
123 if (!(flags & APR_FILEPATH_NATIVE)) {
124 for (*rootpath = path; **rootpath; ++*rootpath) {
125 if (**rootpath == '\\')
126 **rootpath = '/';
127 }
128 }
129 *rootpath = apr_pstrdup(p, path);
130 return APR_SUCCESS;
131 }
132
133
filepath_root_case(char ** rootpath,char * root,apr_pool_t * p)134 apr_status_t filepath_root_case(char **rootpath, char *root, apr_pool_t *p)
135 {
136 #if APR_HAS_UNICODE_FS
137 IF_WIN_OS_IS_UNICODE
138 {
139 apr_wchar_t *ignored;
140 apr_wchar_t wpath[APR_PATH_MAX];
141 apr_status_t rv;
142 apr_wchar_t wroot[APR_PATH_MAX];
143 /* ???: This needs review, apparently "\\?\d:." returns "\\?\d:"
144 * as if that is useful for anything.
145 */
146 if ((rv = utf8_to_unicode_path(wroot, sizeof(wroot)
147 / sizeof(apr_wchar_t), root)))
148 return rv;
149 if (!GetFullPathNameW(wroot, sizeof(wpath) / sizeof(apr_wchar_t), wpath, &ignored))
150 return apr_get_os_error();
151
152 /* Borrow wroot as a char buffer (twice as big as necessary)
153 */
154 if ((rv = unicode_to_utf8_path((char*)wroot, sizeof(wroot), wpath)))
155 return rv;
156 *rootpath = apr_pstrdup(p, (char*)wroot);
157 }
158 #endif
159 #if APR_HAS_ANSI_FS
160 ELSE_WIN_OS_IS_ANSI
161 {
162 char path[APR_PATH_MAX];
163 char *ignored;
164 if (!GetFullPathName(root, sizeof(path), path, &ignored))
165 return apr_get_os_error();
166 *rootpath = apr_pstrdup(p, path);
167 }
168 #endif
169 return APR_SUCCESS;
170 }
171
172
apr_filepath_get(char ** rootpath,apr_int32_t flags,apr_pool_t * p)173 APR_DECLARE(apr_status_t) apr_filepath_get(char **rootpath, apr_int32_t flags,
174 apr_pool_t *p)
175 {
176 char path[APR_PATH_MAX];
177 #if APR_HAS_UNICODE_FS
178 IF_WIN_OS_IS_UNICODE
179 {
180 apr_wchar_t wpath[APR_PATH_MAX];
181 apr_status_t rv;
182 if (!GetCurrentDirectoryW(sizeof(wpath) / sizeof(apr_wchar_t), wpath))
183 return apr_get_os_error();
184 if ((rv = unicode_to_utf8_path(path, sizeof(path), wpath)))
185 return rv;
186 }
187 #endif
188 #if APR_HAS_ANSI_FS
189 ELSE_WIN_OS_IS_ANSI
190 {
191 if (!GetCurrentDirectory(sizeof(path), path))
192 return apr_get_os_error();
193 }
194 #endif
195 if (!(flags & APR_FILEPATH_NATIVE)) {
196 for (*rootpath = path; **rootpath; ++*rootpath) {
197 if (**rootpath == '\\')
198 **rootpath = '/';
199 }
200 }
201 *rootpath = apr_pstrdup(p, path);
202 return APR_SUCCESS;
203 }
204
205
apr_filepath_set(const char * rootpath,apr_pool_t * p)206 APR_DECLARE(apr_status_t) apr_filepath_set(const char *rootpath,
207 apr_pool_t *p)
208 {
209 #if APR_HAS_UNICODE_FS
210 IF_WIN_OS_IS_UNICODE
211 {
212 apr_wchar_t wpath[APR_PATH_MAX];
213 apr_status_t rv;
214 if ((rv = utf8_to_unicode_path(wpath, sizeof(wpath)
215 / sizeof(apr_wchar_t), rootpath)))
216 return rv;
217 if (!SetCurrentDirectoryW(wpath))
218 return apr_get_os_error();
219 }
220 #endif
221 #if APR_HAS_ANSI_FS
222 ELSE_WIN_OS_IS_ANSI
223 {
224 if (!SetCurrentDirectory(rootpath))
225 return apr_get_os_error();
226 }
227 #endif
228 return APR_SUCCESS;
229 }
230