1 | /*
|
---|
2 | Unix SMB/CIFS implementation.
|
---|
3 | new hash based name mangling implementation
|
---|
4 | Copyright (C) Andrew Tridgell 2002
|
---|
5 | Copyright (C) Simo Sorce 2002
|
---|
6 |
|
---|
7 | This program is free software; you can redistribute it and/or modify
|
---|
8 | it under the terms of the GNU General Public License as published by
|
---|
9 | the Free Software Foundation; either version 3 of the License, or
|
---|
10 | (at your option) any later version.
|
---|
11 |
|
---|
12 | This program is distributed in the hope that it will be useful,
|
---|
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
15 | GNU General Public License for more details.
|
---|
16 |
|
---|
17 | You should have received a copy of the GNU General Public License
|
---|
18 | along with this program. If not, see <http://www.gnu.org/licenses/>.
|
---|
19 | */
|
---|
20 |
|
---|
21 | /*
|
---|
22 | this mangling scheme uses the following format
|
---|
23 |
|
---|
24 | Annnn~n.AAA
|
---|
25 |
|
---|
26 | where nnnnn is a base 36 hash, and A represents characters from the original string
|
---|
27 |
|
---|
28 | The hash is taken of the leading part of the long filename, in uppercase
|
---|
29 |
|
---|
30 | for simplicity, we only allow ascii characters in 8.3 names
|
---|
31 | */
|
---|
32 |
|
---|
33 | /* hash alghorithm changed to FNV1 by idra@samba.org (Simo Sorce).
|
---|
34 | * see http://www.isthe.com/chongo/tech/comp/fnv/index.html for a
|
---|
35 | * discussion on Fowler / Noll / Vo (FNV) Hash by one of it's authors
|
---|
36 | */
|
---|
37 |
|
---|
38 | /*
|
---|
39 | ===============================================================================
|
---|
40 | NOTE NOTE NOTE!!!
|
---|
41 |
|
---|
42 | This file deliberately uses non-multibyte string functions in many places. This
|
---|
43 | is *not* a mistake. This code is multi-byte safe, but it gets this property
|
---|
44 | through some very subtle knowledge of the way multi-byte strings are encoded
|
---|
45 | and the fact that this mangling algorithm only supports ascii characters in
|
---|
46 | 8.3 names.
|
---|
47 |
|
---|
48 | please don't convert this file to use the *_m() functions!!
|
---|
49 | ===============================================================================
|
---|
50 | */
|
---|
51 |
|
---|
52 | /*
|
---|
53 | * ============================================================================
|
---|
54 | * Whenever you change anything in the FLAG_ or other fields,
|
---|
55 | * re-initialize the tables char_flags and base_reverse by running the
|
---|
56 | * init_tables() routine once and dump its results. To do this, a
|
---|
57 | * single smbd run with
|
---|
58 | *
|
---|
59 | * #define DYNAMIC_MANGLE_TABLES 1
|
---|
60 | *
|
---|
61 | * and debug level 10 should be sufficient.
|
---|
62 | * ============================================================================
|
---|
63 | */
|
---|
64 |
|
---|
65 |
|
---|
66 | #include "includes.h"
|
---|
67 | #include "smbd/smbd.h"
|
---|
68 | #include "smbd/globals.h"
|
---|
69 | #include "memcache.h"
|
---|
70 | #include "mangle.h"
|
---|
71 |
|
---|
72 | #if 1
|
---|
73 | #define M_DEBUG(level, x) DEBUG(level, x)
|
---|
74 | #else
|
---|
75 | #define M_DEBUG(level, x)
|
---|
76 | #endif
|
---|
77 |
|
---|
78 | /* these flags are used to mark characters in as having particular
|
---|
79 | properties */
|
---|
80 | #define FLAG_BASECHAR 1
|
---|
81 | #define FLAG_ASCII 2
|
---|
82 | #define FLAG_ILLEGAL 4
|
---|
83 | #define FLAG_WILDCARD 8
|
---|
84 |
|
---|
85 | /* the "possible" flags are used as a fast way to find possible DOS
|
---|
86 | reserved filenames */
|
---|
87 | #define FLAG_POSSIBLE1 16
|
---|
88 | #define FLAG_POSSIBLE2 32
|
---|
89 | #define FLAG_POSSIBLE3 64
|
---|
90 | #define FLAG_POSSIBLE4 128
|
---|
91 |
|
---|
92 | /* by default have a max of 4096 entries in the cache. */
|
---|
93 | #ifndef MANGLE_CACHE_SIZE
|
---|
94 | #define MANGLE_CACHE_SIZE 4096
|
---|
95 | #endif
|
---|
96 |
|
---|
97 | #define FNV1_PRIME 0x01000193
|
---|
98 | /*the following number is a fnv1 of the string: idra@samba.org 2002 */
|
---|
99 | #define FNV1_INIT 0xa6b93095
|
---|
100 |
|
---|
101 | #define FLAG_CHECK(c, flag) (char_flags[(unsigned char)(c)] & (flag))
|
---|
102 |
|
---|
103 | /* these are the characters we use in the 8.3 hash. Must be 36 chars long */
|
---|
104 | static const char basechars[36] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
---|
105 | #define base_forward(v) basechars[v]
|
---|
106 |
|
---|
107 | /* the list of reserved dos names - all of these are illegal */
|
---|
108 | static const char * const reserved_names[] =
|
---|
109 | { "AUX", "LOCK$", "CON", "COM1", "COM2", "COM3", "COM4",
|
---|
110 | "LPT1", "LPT2", "LPT3", "NUL", "PRN", NULL };
|
---|
111 |
|
---|
112 | #define DYNAMIC_MANGLE_TABLES 0
|
---|
113 |
|
---|
114 | #if DYNAMIC_MANGLE_TABLES
|
---|
115 |
|
---|
116 | /* these tables are used to provide fast tests for characters */
|
---|
117 | static unsigned char char_flags[256];
|
---|
118 | static unsigned char base_reverse[256];
|
---|
119 |
|
---|
120 | /* initialise the flags table
|
---|
121 |
|
---|
122 | we allow only a very restricted set of characters as 'ascii' in this
|
---|
123 | mangling backend. This isn't a significant problem as modern clients
|
---|
124 | use the 'long' filenames anyway, and those don't have these
|
---|
125 | restrictions.
|
---|
126 | */
|
---|
127 | static void init_tables(void)
|
---|
128 | {
|
---|
129 | int i;
|
---|
130 |
|
---|
131 | memset(char_flags, 0, sizeof(char_flags));
|
---|
132 |
|
---|
133 | for (i=1;i<128;i++) {
|
---|
134 | if (i <= 0x1f) {
|
---|
135 | /* Control characters. */
|
---|
136 | char_flags[i] |= FLAG_ILLEGAL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | if ((i >= '0' && i <= '9') ||
|
---|
140 | (i >= 'a' && i <= 'z') ||
|
---|
141 | (i >= 'A' && i <= 'Z')) {
|
---|
142 | char_flags[i] |= (FLAG_ASCII | FLAG_BASECHAR);
|
---|
143 | }
|
---|
144 | if (strchr("_-$~", i)) {
|
---|
145 | char_flags[i] |= FLAG_ASCII;
|
---|
146 | }
|
---|
147 |
|
---|
148 | if (strchr("*\\/?<>|\":", i)) {
|
---|
149 | char_flags[i] |= FLAG_ILLEGAL;
|
---|
150 | }
|
---|
151 |
|
---|
152 | if (strchr("*?\"<>", i)) {
|
---|
153 | char_flags[i] |= FLAG_WILDCARD;
|
---|
154 | }
|
---|
155 | }
|
---|
156 |
|
---|
157 | memset(base_reverse, 0, sizeof(base_reverse));
|
---|
158 | for (i=0;i<36;i++) {
|
---|
159 | base_reverse[(unsigned char)base_forward(i)] = i;
|
---|
160 | }
|
---|
161 |
|
---|
162 | /* fill in the reserved names flags. These are used as a very
|
---|
163 | fast filter for finding possible DOS reserved filenames */
|
---|
164 | for (i=0; reserved_names[i]; i++) {
|
---|
165 | unsigned char c1, c2, c3, c4;
|
---|
166 |
|
---|
167 | c1 = (unsigned char)reserved_names[i][0];
|
---|
168 | c2 = (unsigned char)reserved_names[i][1];
|
---|
169 | c3 = (unsigned char)reserved_names[i][2];
|
---|
170 | c4 = (unsigned char)reserved_names[i][3];
|
---|
171 |
|
---|
172 | char_flags[c1] |= FLAG_POSSIBLE1;
|
---|
173 | char_flags[c2] |= FLAG_POSSIBLE2;
|
---|
174 | char_flags[c3] |= FLAG_POSSIBLE3;
|
---|
175 | char_flags[c4] |= FLAG_POSSIBLE4;
|
---|
176 | char_flags[tolower_m(c1)] |= FLAG_POSSIBLE1;
|
---|
177 | char_flags[tolower_m(c2)] |= FLAG_POSSIBLE2;
|
---|
178 | char_flags[tolower_m(c3)] |= FLAG_POSSIBLE3;
|
---|
179 | char_flags[tolower_m(c4)] |= FLAG_POSSIBLE4;
|
---|
180 |
|
---|
181 | char_flags[(unsigned char)'.'] |= FLAG_POSSIBLE4;
|
---|
182 | }
|
---|
183 |
|
---|
184 | #if 0
|
---|
185 | DEBUG(10, ("char_flags\n"));
|
---|
186 | dump_data(10, char_flags, sizeof(char_flags));
|
---|
187 |
|
---|
188 | DEBUG(10, ("base_reverse\n"));
|
---|
189 | dump_data(10, base_reverse, sizeof(base_reverse));
|
---|
190 | #endif
|
---|
191 | }
|
---|
192 |
|
---|
193 | #else
|
---|
194 |
|
---|
195 | /*
|
---|
196 | * These tables were initialized by a single run of the above
|
---|
197 | * init_tables() routine, dumping the tables and a simple emacs macro.
|
---|
198 | *
|
---|
199 | * Technically we could leave out the 0's at the end of the array
|
---|
200 | * initializers, but I'll leave it in: less surprise.
|
---|
201 | */
|
---|
202 |
|
---|
203 | static uint8_t char_flags[256] = {
|
---|
204 | 0x80, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
---|
205 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
---|
206 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
---|
207 | 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04, 0x04,
|
---|
208 | 0x00, 0x00, 0x0C, 0x00, 0x02, 0x00, 0x00, 0x00,
|
---|
209 | 0x00, 0x00, 0x0C, 0x00, 0x00, 0x02, 0x80, 0x04,
|
---|
210 | 0x03, 0x83, 0x83, 0x83, 0x83, 0x03, 0x03, 0x03,
|
---|
211 | 0x03, 0x03, 0x04, 0x00, 0x0C, 0x00, 0x0C, 0x0C,
|
---|
212 | 0x00, 0x13, 0x03, 0x53, 0x03, 0x03, 0x03, 0x03,
|
---|
213 | 0x03, 0x03, 0x03, 0x83, 0x53, 0x43, 0x53, 0x23,
|
---|
214 | 0x33, 0x03, 0x23, 0x03, 0x43, 0x23, 0x03, 0x03,
|
---|
215 | 0x43, 0x03, 0x03, 0x00, 0x04, 0x00, 0x00, 0x02,
|
---|
216 | 0x00, 0x13, 0x03, 0x53, 0x03, 0x03, 0x03, 0x03,
|
---|
217 | 0x03, 0x03, 0x03, 0x83, 0x53, 0x43, 0x53, 0x23,
|
---|
218 | 0x33, 0x03, 0x23, 0x03, 0x43, 0x23, 0x03, 0x03,
|
---|
219 | 0x43, 0x03, 0x03, 0x00, 0x04, 0x00, 0x02, 0x00,
|
---|
220 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
221 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
222 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
223 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
224 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
225 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
226 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
227 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
228 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
229 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
230 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
231 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
232 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
233 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
234 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
235 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
---|
236 | };
|
---|
237 |
|
---|
238 | static uint8_t base_reverse[256] = {
|
---|
239 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
240 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
241 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
242 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
243 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
244 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
245 | 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
|
---|
246 | 0x08, 0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
247 | 0x00, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
|
---|
248 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18,
|
---|
249 | 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
|
---|
250 | 0x21, 0x22, 0x23, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
251 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
252 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
253 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
254 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
255 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
256 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
257 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
258 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
259 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
260 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
261 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
262 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
263 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
264 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
265 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
266 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
267 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
268 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
269 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
---|
270 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
|
---|
271 | };
|
---|
272 |
|
---|
273 | #endif
|
---|
274 |
|
---|
275 | /*
|
---|
276 | hash a string of the specified length. The string does not need to be
|
---|
277 | null terminated
|
---|
278 |
|
---|
279 | this hash needs to be fast with a low collision rate (what hash doesn't?)
|
---|
280 | */
|
---|
281 | static unsigned int mangle_hash(const char *key, unsigned int length)
|
---|
282 | {
|
---|
283 | unsigned int value;
|
---|
284 | unsigned int i;
|
---|
285 | fstring str;
|
---|
286 |
|
---|
287 | /* we have to uppercase here to ensure that the mangled name
|
---|
288 | doesn't depend on the case of the long name. Note that this
|
---|
289 | is the only place where we need to use a multi-byte string
|
---|
290 | function */
|
---|
291 | length = MIN(length,sizeof(fstring)-1);
|
---|
292 | strncpy(str, key, length);
|
---|
293 | str[length] = 0;
|
---|
294 | strupper_m(str);
|
---|
295 |
|
---|
296 | /* the length of a multi-byte string can change after a strupper_m */
|
---|
297 | length = strlen(str);
|
---|
298 |
|
---|
299 | /* Set the initial value from the key size. */
|
---|
300 | for (value = FNV1_INIT, i=0; i < length; i++) {
|
---|
301 | value *= (unsigned int)FNV1_PRIME;
|
---|
302 | value ^= (unsigned int)(str[i]);
|
---|
303 | }
|
---|
304 |
|
---|
305 | /* note that we force it to a 31 bit hash, to keep within the limits
|
---|
306 | of the 36^6 mangle space */
|
---|
307 | return value & ~0x80000000;
|
---|
308 | }
|
---|
309 |
|
---|
310 | /*
|
---|
311 | insert an entry into the prefix cache. The string might not be null
|
---|
312 | terminated */
|
---|
313 | static void cache_insert(const char *prefix, int length, unsigned int hash)
|
---|
314 | {
|
---|
315 | char *str = SMB_STRNDUP(prefix, length);
|
---|
316 |
|
---|
317 | if (str == NULL) {
|
---|
318 | return;
|
---|
319 | }
|
---|
320 |
|
---|
321 | memcache_add(smbd_memcache(), MANGLE_HASH2_CACHE,
|
---|
322 | data_blob_const(&hash, sizeof(hash)),
|
---|
323 | data_blob_const(str, length+1));
|
---|
324 | SAFE_FREE(str);
|
---|
325 | }
|
---|
326 |
|
---|
327 | /*
|
---|
328 | lookup an entry in the prefix cache. Return NULL if not found.
|
---|
329 | */
|
---|
330 | static char *cache_lookup(TALLOC_CTX *mem_ctx, unsigned int hash)
|
---|
331 | {
|
---|
332 | DATA_BLOB value;
|
---|
333 |
|
---|
334 | if (!memcache_lookup(smbd_memcache(), MANGLE_HASH2_CACHE,
|
---|
335 | data_blob_const(&hash, sizeof(hash)), &value)) {
|
---|
336 | return NULL;
|
---|
337 | }
|
---|
338 |
|
---|
339 | SMB_ASSERT((value.length > 0)
|
---|
340 | && (value.data[value.length-1] == '\0'));
|
---|
341 |
|
---|
342 | return talloc_strdup(mem_ctx, (char *)value.data);
|
---|
343 | }
|
---|
344 |
|
---|
345 |
|
---|
346 | /*
|
---|
347 | determine if a string is possibly in a mangled format, ignoring
|
---|
348 | case
|
---|
349 |
|
---|
350 | In this algorithm, mangled names use only pure ascii characters (no
|
---|
351 | multi-byte) so we can avoid doing a UCS2 conversion
|
---|
352 | */
|
---|
353 | static bool is_mangled_component(const char *name, size_t len)
|
---|
354 | {
|
---|
355 | unsigned int i;
|
---|
356 |
|
---|
357 | M_DEBUG(10,("is_mangled_component %s (len %lu) ?\n", name, (unsigned long)len));
|
---|
358 |
|
---|
359 | /* check the length */
|
---|
360 | if (len > 12 || len < 8)
|
---|
361 | return False;
|
---|
362 |
|
---|
363 | /* the best distinguishing characteristic is the ~ */
|
---|
364 | if (name[6] != '~')
|
---|
365 | return False;
|
---|
366 |
|
---|
367 | /* check extension */
|
---|
368 | if (len > 8) {
|
---|
369 | if (name[8] != '.')
|
---|
370 | return False;
|
---|
371 | for (i=9; name[i] && i < len; i++) {
|
---|
372 | if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
|
---|
373 | return False;
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 |
|
---|
378 | /* check lead characters */
|
---|
379 | for (i=0;i<mangle_prefix;i++) {
|
---|
380 | if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
|
---|
381 | return False;
|
---|
382 | }
|
---|
383 | }
|
---|
384 |
|
---|
385 | /* check rest of hash */
|
---|
386 | if (! FLAG_CHECK(name[7], FLAG_BASECHAR)) {
|
---|
387 | return False;
|
---|
388 | }
|
---|
389 | for (i=mangle_prefix;i<6;i++) {
|
---|
390 | if (! FLAG_CHECK(name[i], FLAG_BASECHAR)) {
|
---|
391 | return False;
|
---|
392 | }
|
---|
393 | }
|
---|
394 |
|
---|
395 | M_DEBUG(10,("is_mangled_component %s (len %lu) -> yes\n", name, (unsigned long)len));
|
---|
396 |
|
---|
397 | return True;
|
---|
398 | }
|
---|
399 |
|
---|
400 |
|
---|
401 |
|
---|
402 | /*
|
---|
403 | determine if a string is possibly in a mangled format, ignoring
|
---|
404 | case
|
---|
405 |
|
---|
406 | In this algorithm, mangled names use only pure ascii characters (no
|
---|
407 | multi-byte) so we can avoid doing a UCS2 conversion
|
---|
408 |
|
---|
409 | NOTE! This interface must be able to handle a path with unix
|
---|
410 | directory separators. It should return true if any component is
|
---|
411 | mangled
|
---|
412 | */
|
---|
413 | static bool is_mangled(const char *name, const struct share_params *parm)
|
---|
414 | {
|
---|
415 | const char *p;
|
---|
416 | const char *s;
|
---|
417 |
|
---|
418 | M_DEBUG(10,("is_mangled %s ?\n", name));
|
---|
419 |
|
---|
420 | for (s=name; (p=strchr(s, '/')); s=p+1) {
|
---|
421 | if (is_mangled_component(s, PTR_DIFF(p, s))) {
|
---|
422 | return True;
|
---|
423 | }
|
---|
424 | }
|
---|
425 |
|
---|
426 | /* and the last part ... */
|
---|
427 | return is_mangled_component(s,strlen(s));
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | /*
|
---|
432 | see if a filename is an allowable 8.3 name to return to the client.
|
---|
433 | Note this is not testing if this is a valid Samba mangled name, so
|
---|
434 | the rules are different for is_mangled.
|
---|
435 |
|
---|
436 | we are only going to allow ascii characters in 8.3 names, as this
|
---|
437 | simplifies things greatly (it means that we know the string won't
|
---|
438 | get larger when converted from UNIX to DOS formats)
|
---|
439 | */
|
---|
440 |
|
---|
441 | static char force_shortname_chars[] = " +,[];=";
|
---|
442 |
|
---|
443 | static bool is_8_3(const char *name, bool check_case, bool allow_wildcards, const struct share_params *p)
|
---|
444 | {
|
---|
445 | int len, i;
|
---|
446 | char *dot_p;
|
---|
447 |
|
---|
448 | /* as a special case, the names '.' and '..' are allowable 8.3 names */
|
---|
449 | if (name[0] == '.') {
|
---|
450 | if (!name[1] || (name[1] == '.' && !name[2])) {
|
---|
451 | return True;
|
---|
452 | }
|
---|
453 | }
|
---|
454 |
|
---|
455 | /* the simplest test is on the overall length of the
|
---|
456 | filename. Note that we deliberately use the ascii string
|
---|
457 | length (not the multi-byte one) as it is faster, and gives us
|
---|
458 | the result we need in this case. Using strlen_m would not
|
---|
459 | only be slower, it would be incorrect */
|
---|
460 | len = strlen(name);
|
---|
461 | if (len > 12)
|
---|
462 | return False;
|
---|
463 |
|
---|
464 | /* find the '.'. Note that once again we use the non-multibyte
|
---|
465 | function */
|
---|
466 | dot_p = strchr(name, '.');
|
---|
467 |
|
---|
468 | if (!dot_p) {
|
---|
469 | /* if the name doesn't contain a '.' then its length
|
---|
470 | must be less than 8 */
|
---|
471 | if (len > 8) {
|
---|
472 | return False;
|
---|
473 | }
|
---|
474 | } else {
|
---|
475 | int prefix_len, suffix_len;
|
---|
476 |
|
---|
477 | /* if it does contain a dot then the prefix must be <=
|
---|
478 | 8 and the suffix <= 3 in length */
|
---|
479 | prefix_len = PTR_DIFF(dot_p, name);
|
---|
480 | suffix_len = len - (prefix_len+1);
|
---|
481 |
|
---|
482 | if (prefix_len > 8 || suffix_len > 3 || suffix_len == 0) {
|
---|
483 | return False;
|
---|
484 | }
|
---|
485 |
|
---|
486 | /* a 8.3 name cannot contain more than 1 '.' */
|
---|
487 | if (strchr(dot_p+1, '.')) {
|
---|
488 | return False;
|
---|
489 | }
|
---|
490 | }
|
---|
491 |
|
---|
492 | /* the length are all OK. Now check to see if the characters themselves are OK */
|
---|
493 | for (i=0; name[i]; i++) {
|
---|
494 | if (FLAG_CHECK(name[i], FLAG_ILLEGAL)) {
|
---|
495 | return false;
|
---|
496 | }
|
---|
497 | /* note that we may allow wildcard petterns! */
|
---|
498 | if (!allow_wildcards && FLAG_CHECK(name[i], FLAG_WILDCARD)) {
|
---|
499 | return false;
|
---|
500 | }
|
---|
501 | if (((unsigned char)name[i]) > 0x7e) {
|
---|
502 | return false;
|
---|
503 | }
|
---|
504 | if (strchr(force_shortname_chars, name[i])) {
|
---|
505 | return false;
|
---|
506 | }
|
---|
507 | }
|
---|
508 |
|
---|
509 | /* it is a good 8.3 name */
|
---|
510 | return True;
|
---|
511 | }
|
---|
512 |
|
---|
513 |
|
---|
514 | /*
|
---|
515 | reset the mangling cache on a smb.conf reload. This only really makes sense for
|
---|
516 | mangling backends that have parameters in smb.conf, and as this backend doesn't
|
---|
517 | this is a NULL operation
|
---|
518 | */
|
---|
519 | static void mangle_reset(void)
|
---|
520 | {
|
---|
521 | /* noop */
|
---|
522 | }
|
---|
523 |
|
---|
524 |
|
---|
525 | /*
|
---|
526 | try to find a 8.3 name in the cache, and if found then
|
---|
527 | replace the string with the original long name.
|
---|
528 | */
|
---|
529 | static bool lookup_name_from_8_3(TALLOC_CTX *ctx,
|
---|
530 | const char *name,
|
---|
531 | char **pp_out, /* talloced on the given context. */
|
---|
532 | const struct share_params *p)
|
---|
533 | {
|
---|
534 | unsigned int hash, multiplier;
|
---|
535 | unsigned int i;
|
---|
536 | char *prefix;
|
---|
537 | char extension[4];
|
---|
538 |
|
---|
539 | *pp_out = NULL;
|
---|
540 |
|
---|
541 | /* make sure that this is a mangled name from this cache */
|
---|
542 | if (!is_mangled(name, p)) {
|
---|
543 | M_DEBUG(10,("lookup_name_from_8_3: %s -> not mangled\n", name));
|
---|
544 | return False;
|
---|
545 | }
|
---|
546 |
|
---|
547 | /* we need to extract the hash from the 8.3 name */
|
---|
548 | hash = base_reverse[(unsigned char)name[7]];
|
---|
549 | for (multiplier=36, i=5;i>=mangle_prefix;i--) {
|
---|
550 | unsigned int v = base_reverse[(unsigned char)name[i]];
|
---|
551 | hash += multiplier * v;
|
---|
552 | multiplier *= 36;
|
---|
553 | }
|
---|
554 |
|
---|
555 | /* now look in the prefix cache for that hash */
|
---|
556 | prefix = cache_lookup(ctx, hash);
|
---|
557 | if (!prefix) {
|
---|
558 | M_DEBUG(10,("lookup_name_from_8_3: %s -> %08X -> not found\n",
|
---|
559 | name, hash));
|
---|
560 | return False;
|
---|
561 | }
|
---|
562 |
|
---|
563 | /* we found it - construct the full name */
|
---|
564 | if (name[8] == '.') {
|
---|
565 | strncpy(extension, name+9, 3);
|
---|
566 | extension[3] = 0;
|
---|
567 | } else {
|
---|
568 | extension[0] = 0;
|
---|
569 | }
|
---|
570 |
|
---|
571 | if (extension[0]) {
|
---|
572 | M_DEBUG(10,("lookup_name_from_8_3: %s -> %s.%s\n",
|
---|
573 | name, prefix, extension));
|
---|
574 | *pp_out = talloc_asprintf(ctx, "%s.%s", prefix, extension);
|
---|
575 | } else {
|
---|
576 | M_DEBUG(10,("lookup_name_from_8_3: %s -> %s\n", name, prefix));
|
---|
577 | *pp_out = talloc_strdup(ctx, prefix);
|
---|
578 | }
|
---|
579 |
|
---|
580 | TALLOC_FREE(prefix);
|
---|
581 |
|
---|
582 | if (!*pp_out) {
|
---|
583 | M_DEBUG(0,("talloc_fail"));
|
---|
584 | return False;
|
---|
585 | }
|
---|
586 |
|
---|
587 | return True;
|
---|
588 | }
|
---|
589 |
|
---|
590 | /*
|
---|
591 | look for a DOS reserved name
|
---|
592 | */
|
---|
593 | static bool is_reserved_name(const char *name)
|
---|
594 | {
|
---|
595 | if (FLAG_CHECK(name[0], FLAG_POSSIBLE1) &&
|
---|
596 | FLAG_CHECK(name[1], FLAG_POSSIBLE2) &&
|
---|
597 | FLAG_CHECK(name[2], FLAG_POSSIBLE3) &&
|
---|
598 | FLAG_CHECK(name[3], FLAG_POSSIBLE4)) {
|
---|
599 | /* a likely match, scan the lot */
|
---|
600 | int i;
|
---|
601 | for (i=0; reserved_names[i]; i++) {
|
---|
602 | int len = strlen(reserved_names[i]);
|
---|
603 | /* note that we match on COM1 as well as COM1.foo */
|
---|
604 | if (strnequal(name, reserved_names[i], len) &&
|
---|
605 | (name[len] == '.' || name[len] == 0)) {
|
---|
606 | return True;
|
---|
607 | }
|
---|
608 | }
|
---|
609 | }
|
---|
610 |
|
---|
611 | return False;
|
---|
612 | }
|
---|
613 |
|
---|
614 | /*
|
---|
615 | See if a filename is a legal long filename.
|
---|
616 | A filename ending in a '.' is not legal unless it's "." or "..". JRA.
|
---|
617 | A filename ending in ' ' is not legal either. See bug id #2769.
|
---|
618 | */
|
---|
619 |
|
---|
620 | static bool is_legal_name(const char *name)
|
---|
621 | {
|
---|
622 | const char *dot_pos = NULL;
|
---|
623 | bool alldots = True;
|
---|
624 | size_t numdots = 0;
|
---|
625 |
|
---|
626 | while (*name) {
|
---|
627 | if (((unsigned int)name[0]) > 128 && (name[1] != 0)) {
|
---|
628 | /* Possible start of mb character. */
|
---|
629 | size_t size = 0;
|
---|
630 | (void)next_codepoint(name, &size);
|
---|
631 | /*
|
---|
632 | * Note that if CH_UNIX is utf8 a string may be 3
|
---|
633 | * bytes, but this is ok as mb utf8 characters don't
|
---|
634 | * contain embedded ascii bytes. We are really checking
|
---|
635 | * for mb UNIX asian characters like Japanese (SJIS) here.
|
---|
636 | * JRA.
|
---|
637 | */
|
---|
638 | if (size > 1) {
|
---|
639 | /* Was a mb string. */
|
---|
640 | name += size;
|
---|
641 | continue;
|
---|
642 | }
|
---|
643 | }
|
---|
644 |
|
---|
645 | if (FLAG_CHECK(name[0], FLAG_ILLEGAL)) {
|
---|
646 | return False;
|
---|
647 | }
|
---|
648 | if (name[0] == '.') {
|
---|
649 | dot_pos = name;
|
---|
650 | numdots++;
|
---|
651 | } else {
|
---|
652 | alldots = False;
|
---|
653 | }
|
---|
654 | if ((name[0] == ' ') && (name[1] == '\0')) {
|
---|
655 | /* Can't end in ' ' */
|
---|
656 | return False;
|
---|
657 | }
|
---|
658 | name++;
|
---|
659 | }
|
---|
660 |
|
---|
661 | if (dot_pos) {
|
---|
662 | if (alldots && (numdots == 1 || numdots == 2))
|
---|
663 | return True; /* . or .. is a valid name */
|
---|
664 |
|
---|
665 | /* A valid long name cannot end in '.' */
|
---|
666 | if (dot_pos[1] == '\0')
|
---|
667 | return False;
|
---|
668 | }
|
---|
669 | return True;
|
---|
670 | }
|
---|
671 |
|
---|
672 | static bool must_mangle(const char *name,
|
---|
673 | const struct share_params *p)
|
---|
674 | {
|
---|
675 | if (is_reserved_name(name)) {
|
---|
676 | return True;
|
---|
677 | }
|
---|
678 | return !is_legal_name(name);
|
---|
679 | }
|
---|
680 |
|
---|
681 | /*
|
---|
682 | the main forward mapping function, which converts a long filename to
|
---|
683 | a 8.3 name
|
---|
684 |
|
---|
685 | if cache83 is not set then we don't cache the result
|
---|
686 |
|
---|
687 | */
|
---|
688 | static bool hash2_name_to_8_3(const char *name,
|
---|
689 | char new_name[13],
|
---|
690 | bool cache83,
|
---|
691 | int default_case,
|
---|
692 | const struct share_params *p)
|
---|
693 | {
|
---|
694 | char *dot_p;
|
---|
695 | char lead_chars[7];
|
---|
696 | char extension[4];
|
---|
697 | unsigned int extension_length, i;
|
---|
698 | unsigned int prefix_len;
|
---|
699 | unsigned int hash, v;
|
---|
700 |
|
---|
701 | /* reserved names are handled specially */
|
---|
702 | if (!is_reserved_name(name)) {
|
---|
703 | /* if the name is already a valid 8.3 name then we don't need to
|
---|
704 | * change anything */
|
---|
705 | if (is_legal_name(name) && is_8_3(name, False, False, p)) {
|
---|
706 | safe_strcpy(new_name, name, 12);
|
---|
707 | return True;
|
---|
708 | }
|
---|
709 | }
|
---|
710 |
|
---|
711 | /* find the '.' if any */
|
---|
712 | dot_p = strrchr(name, '.');
|
---|
713 |
|
---|
714 | if (dot_p) {
|
---|
715 | /* if the extension contains any illegal characters or
|
---|
716 | is too long or zero length then we treat it as part
|
---|
717 | of the prefix */
|
---|
718 | for (i=0; i<4 && dot_p[i+1]; i++) {
|
---|
719 | if (! FLAG_CHECK(dot_p[i+1], FLAG_ASCII)) {
|
---|
720 | dot_p = NULL;
|
---|
721 | break;
|
---|
722 | }
|
---|
723 | }
|
---|
724 | if (i == 0 || i == 4) {
|
---|
725 | dot_p = NULL;
|
---|
726 | }
|
---|
727 | }
|
---|
728 |
|
---|
729 | /* the leading characters in the mangled name is taken from
|
---|
730 | the first characters of the name, if they are ascii otherwise
|
---|
731 | '_' is used
|
---|
732 | */
|
---|
733 | for (i=0;i<mangle_prefix && name[i];i++) {
|
---|
734 | lead_chars[i] = name[i];
|
---|
735 | if (! FLAG_CHECK(lead_chars[i], FLAG_ASCII)) {
|
---|
736 | lead_chars[i] = '_';
|
---|
737 | }
|
---|
738 | lead_chars[i] = toupper_m(lead_chars[i]);
|
---|
739 | }
|
---|
740 | for (;i<mangle_prefix;i++) {
|
---|
741 | lead_chars[i] = '_';
|
---|
742 | }
|
---|
743 |
|
---|
744 | /* the prefix is anything up to the first dot */
|
---|
745 | if (dot_p) {
|
---|
746 | prefix_len = PTR_DIFF(dot_p, name);
|
---|
747 | } else {
|
---|
748 | prefix_len = strlen(name);
|
---|
749 | }
|
---|
750 |
|
---|
751 | /* the extension of the mangled name is taken from the first 3
|
---|
752 | ascii chars after the dot */
|
---|
753 | extension_length = 0;
|
---|
754 | if (dot_p) {
|
---|
755 | for (i=1; extension_length < 3 && dot_p[i]; i++) {
|
---|
756 | char c = dot_p[i];
|
---|
757 | if (FLAG_CHECK(c, FLAG_ASCII)) {
|
---|
758 | extension[extension_length++] =
|
---|
759 | toupper_m(c);
|
---|
760 | }
|
---|
761 | }
|
---|
762 | }
|
---|
763 |
|
---|
764 | /* find the hash for this prefix */
|
---|
765 | v = hash = mangle_hash(name, prefix_len);
|
---|
766 |
|
---|
767 | /* now form the mangled name. */
|
---|
768 | for (i=0;i<mangle_prefix;i++) {
|
---|
769 | new_name[i] = lead_chars[i];
|
---|
770 | }
|
---|
771 | new_name[7] = base_forward(v % 36);
|
---|
772 | new_name[6] = '~';
|
---|
773 | for (i=5; i>=mangle_prefix; i--) {
|
---|
774 | v = v / 36;
|
---|
775 | new_name[i] = base_forward(v % 36);
|
---|
776 | }
|
---|
777 |
|
---|
778 | /* add the extension */
|
---|
779 | if (extension_length) {
|
---|
780 | new_name[8] = '.';
|
---|
781 | memcpy(&new_name[9], extension, extension_length);
|
---|
782 | new_name[9+extension_length] = 0;
|
---|
783 | } else {
|
---|
784 | new_name[8] = 0;
|
---|
785 | }
|
---|
786 |
|
---|
787 | if (cache83) {
|
---|
788 | /* put it in the cache */
|
---|
789 | cache_insert(name, prefix_len, hash);
|
---|
790 | }
|
---|
791 |
|
---|
792 | M_DEBUG(10,("hash2_name_to_8_3: %s -> %08X -> %s (cache=%d)\n",
|
---|
793 | name, hash, new_name, cache83));
|
---|
794 |
|
---|
795 | return True;
|
---|
796 | }
|
---|
797 |
|
---|
798 | /*
|
---|
799 | the following provides the abstraction layer to make it easier
|
---|
800 | to drop in an alternative mangling implementation */
|
---|
801 | static const struct mangle_fns mangle_hash2_fns = {
|
---|
802 | mangle_reset,
|
---|
803 | is_mangled,
|
---|
804 | must_mangle,
|
---|
805 | is_8_3,
|
---|
806 | lookup_name_from_8_3,
|
---|
807 | hash2_name_to_8_3
|
---|
808 | };
|
---|
809 |
|
---|
810 | /* return the methods for this mangling implementation */
|
---|
811 | const struct mangle_fns *mangle_hash2_init(void)
|
---|
812 | {
|
---|
813 | /* the mangle prefix can only be in the mange 1 to 6 */
|
---|
814 | mangle_prefix = lp_mangle_prefix();
|
---|
815 | if (mangle_prefix > 6) {
|
---|
816 | mangle_prefix = 6;
|
---|
817 | }
|
---|
818 | if (mangle_prefix < 1) {
|
---|
819 | mangle_prefix = 1;
|
---|
820 | }
|
---|
821 |
|
---|
822 | #if DYNAMIC_MANGLE_TABLES
|
---|
823 | init_tables();
|
---|
824 | #endif
|
---|
825 | mangle_reset();
|
---|
826 |
|
---|
827 | return &mangle_hash2_fns;
|
---|
828 | }
|
---|
829 |
|
---|
830 | static void posix_mangle_reset(void)
|
---|
831 | {;}
|
---|
832 |
|
---|
833 | static bool posix_is_mangled(const char *s, const struct share_params *p)
|
---|
834 | {
|
---|
835 | return False;
|
---|
836 | }
|
---|
837 |
|
---|
838 | static bool posix_must_mangle(const char *s, const struct share_params *p)
|
---|
839 | {
|
---|
840 | return False;
|
---|
841 | }
|
---|
842 |
|
---|
843 | static bool posix_is_8_3(const char *fname,
|
---|
844 | bool check_case,
|
---|
845 | bool allow_wildcards,
|
---|
846 | const struct share_params *p)
|
---|
847 | {
|
---|
848 | return False;
|
---|
849 | }
|
---|
850 |
|
---|
851 | static bool posix_lookup_name_from_8_3(TALLOC_CTX *ctx,
|
---|
852 | const char *in,
|
---|
853 | char **out, /* talloced on the given context. */
|
---|
854 | const struct share_params *p)
|
---|
855 | {
|
---|
856 | return False;
|
---|
857 | }
|
---|
858 |
|
---|
859 | static bool posix_name_to_8_3(const char *in,
|
---|
860 | char out[13],
|
---|
861 | bool cache83,
|
---|
862 | int default_case,
|
---|
863 | const struct share_params *p)
|
---|
864 | {
|
---|
865 | memset(out, '\0', 13);
|
---|
866 | return True;
|
---|
867 | }
|
---|
868 |
|
---|
869 | /* POSIX paths backend - no mangle. */
|
---|
870 | static const struct mangle_fns posix_mangle_fns = {
|
---|
871 | posix_mangle_reset,
|
---|
872 | posix_is_mangled,
|
---|
873 | posix_must_mangle,
|
---|
874 | posix_is_8_3,
|
---|
875 | posix_lookup_name_from_8_3,
|
---|
876 | posix_name_to_8_3
|
---|
877 | };
|
---|
878 |
|
---|
879 | const struct mangle_fns *posix_mangle_init(void)
|
---|
880 | {
|
---|
881 | return &posix_mangle_fns;
|
---|
882 | }
|
---|