r4423: give lp_parm_int() and lp_parm_ulong() default values
[kamenim/samba.git] / source4 / ntvfs / posix / pvfs_shortname.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - 8.3 name routines
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 #include "includes.h"
24 #include "vfs_posix.h"
25 #include "system/iconv.h"
26
27 /*
28   this mangling scheme uses the following format
29
30   Annnn~n.AAA
31
32   where nnnnn is a base 36 hash, and A represents characters from the original string
33
34   The hash is taken of the leading part of the long filename, in uppercase
35
36   for simplicity, we only allow ascii characters in 8.3 names
37 */
38
39 /*
40   ===============================================================================
41   NOTE NOTE NOTE!!!
42
43   This file deliberately uses non-multibyte string functions in many places. This
44   is *not* a mistake. This code is multi-byte safe, but it gets this property
45   through some very subtle knowledge of the way multi-byte strings are encoded 
46   and the fact that this mangling algorithm only supports ascii characters in
47   8.3 names.
48
49   please don't convert this file to use the *_m() functions!!
50   ===============================================================================
51 */
52
53
54 #if 1
55 #define M_DEBUG(level, x) DEBUG(level, x)
56 #else
57 #define M_DEBUG(level, x)
58 #endif
59
60 /* these flags are used to mark characters in as having particular
61    properties */
62 #define FLAG_BASECHAR 1
63 #define FLAG_ASCII 2
64 #define FLAG_ILLEGAL 4
65 #define FLAG_WILDCARD 8
66
67 /* the "possible" flags are used as a fast way to find possible DOS
68    reserved filenames */
69 #define FLAG_POSSIBLE1 16
70 #define FLAG_POSSIBLE2 32
71 #define FLAG_POSSIBLE3 64
72 #define FLAG_POSSIBLE4 128
73
74 /* by default have a max of 512 entries in the cache. */
75 #ifndef MANGLE_CACHE_SIZE
76 #define MANGLE_CACHE_SIZE 512
77 #endif
78
79 #define DEFAULT_MANGLE_PREFIX 4
80
81 #define MANGLE_BASECHARS "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
82
83 #define FLAG_CHECK(c, flag) (ctx->char_flags[(uint8_t)(c)] & (flag))
84
85 static const char *reserved_names[] = 
86 { "AUX", "CON", "COM1", "COM2", "COM3", "COM4",
87   "LPT1", "LPT2", "LPT3", "NUL", "PRN", NULL };
88
89
90 /* 
91    hash a string of the specified length. The string does not need to be
92    null terminated 
93
94    this hash needs to be fast with a low collision rate (what hash doesn't?)
95 */
96 static uint32_t mangle_hash(struct pvfs_mangle_context *ctx,
97                             const char *key, size_t length)
98 {
99         return pvfs_name_hash(key, length) % ctx->mangle_modulus;
100 }
101
102 /*
103   insert an entry into the prefix cache. The string might not be null
104   terminated */
105 static void cache_insert(struct pvfs_mangle_context *ctx,
106                          const char *prefix, int length, uint32_t hash)
107 {
108         int i = hash % MANGLE_CACHE_SIZE;
109
110         if (ctx->prefix_cache[i]) {
111                 talloc_free(ctx->prefix_cache[i]);
112         }
113
114         ctx->prefix_cache[i] = talloc_strndup(ctx->prefix_cache, prefix, length);
115         ctx->prefix_cache_hashes[i] = hash;
116 }
117
118 /*
119   lookup an entry in the prefix cache. Return NULL if not found.
120 */
121 static const char *cache_lookup(struct pvfs_mangle_context *ctx, uint32_t hash)
122 {
123         int i = hash % MANGLE_CACHE_SIZE;
124
125
126         if (!ctx->prefix_cache[i] || hash != ctx->prefix_cache_hashes[i]) {
127                 return NULL;
128         }
129
130         /* yep, it matched */
131         return ctx->prefix_cache[i];
132 }
133
134
135 /* 
136    determine if a string is possibly in a mangled format, ignoring
137    case 
138
139    In this algorithm, mangled names use only pure ascii characters (no
140    multi-byte) so we can avoid doing a UCS2 conversion 
141  */
142 static BOOL is_mangled_component(struct pvfs_mangle_context *ctx,
143                                  const char *name, size_t len)
144 {
145         unsigned int i;
146
147         M_DEBUG(10,("is_mangled_component %s (len %u) ?\n", name, (unsigned int)len));
148
149         /* check the length */
150         if (len > 12 || len < 8)
151                 return False;
152
153         /* the best distinguishing characteristic is the ~ */
154         if (name[6] != '~')
155                 return False;
156
157         /* check extension */
158         if (len > 8) {
159                 if (name[8] != '.')
160                         return False;
161                 for (i=9; name[i] && i < len; i++) {
162                         if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
163                                 return False;
164                         }
165                 }
166         }
167         
168         /* check lead characters */
169         for (i=0;i<ctx->mangle_prefix;i++) {
170                 if (! FLAG_CHECK(name[i], FLAG_ASCII)) {
171                         return False;
172                 }
173         }
174         
175         /* check rest of hash */
176         if (! FLAG_CHECK(name[7], FLAG_BASECHAR)) {
177                 return False;
178         }
179         for (i=ctx->mangle_prefix;i<6;i++) {
180                 if (! FLAG_CHECK(name[i], FLAG_BASECHAR)) {
181                         return False;
182                 }
183         }
184
185         M_DEBUG(10,("is_mangled_component %s (len %u) -> yes\n", name, (unsigned int)len));
186
187         return True;
188 }
189
190
191
192 /* 
193    determine if a string is possibly in a mangled format, ignoring
194    case 
195
196    In this algorithm, mangled names use only pure ascii characters (no
197    multi-byte) so we can avoid doing a UCS2 conversion 
198
199    NOTE! This interface must be able to handle a path with unix
200    directory separators. It should return true if any component is
201    mangled
202  */
203 static BOOL is_mangled(struct pvfs_mangle_context *ctx, const char *name)
204 {
205         const char *p;
206         const char *s;
207
208         M_DEBUG(10,("is_mangled %s ?\n", name));
209
210         for (s=name; (p=strchr(s, '/')); s=p+1) {
211                 if (is_mangled_component(ctx, s, PTR_DIFF(p, s))) {
212                         return True;
213                 }
214         }
215         
216         /* and the last part ... */
217         return is_mangled_component(ctx, s, strlen(s));
218 }
219
220
221 /* 
222    see if a filename is an allowable 8.3 name.
223
224    we are only going to allow ascii characters in 8.3 names, as this
225    simplifies things greatly (it means that we know the string won't
226    get larger when converted from UNIX to DOS formats)
227 */
228 static BOOL is_8_3(struct pvfs_mangle_context *ctx,
229                    const char *name, BOOL check_case, BOOL allow_wildcards)
230 {
231         int len, i;
232         char *dot_p;
233
234         /* as a special case, the names '.' and '..' are allowable 8.3 names */
235         if (name[0] == '.') {
236                 if (!name[1] || (name[1] == '.' && !name[2])) {
237                         return True;
238                 }
239         }
240
241         /* the simplest test is on the overall length of the
242          filename. Note that we deliberately use the ascii string
243          length (not the multi-byte one) as it is faster, and gives us
244          the result we need in this case. Using strlen_m would not
245          only be slower, it would be incorrect */
246         len = strlen(name);
247         if (len > 12)
248                 return False;
249
250         /* find the '.'. Note that once again we use the non-multibyte
251            function */
252         dot_p = strchr(name, '.');
253
254         if (!dot_p) {
255                 /* if the name doesn't contain a '.' then its length
256                    must be less than 8 */
257                 if (len > 8) {
258                         return False;
259                 }
260         } else {
261                 int prefix_len, suffix_len;
262
263                 /* if it does contain a dot then the prefix must be <=
264                    8 and the suffix <= 3 in length */
265                 prefix_len = PTR_DIFF(dot_p, name);
266                 suffix_len = len - (prefix_len+1);
267
268                 if (prefix_len > 8 || suffix_len > 3 || suffix_len == 0) {
269                         return False;
270                 }
271
272                 /* a 8.3 name cannot contain more than 1 '.' */
273                 if (strchr(dot_p+1, '.')) {
274                         return False;
275                 }
276         }
277
278         /* the length are all OK. Now check to see if the characters themselves are OK */
279         for (i=0; name[i]; i++) {
280                 /* note that we may allow wildcard petterns! */
281                 if (!FLAG_CHECK(name[i], FLAG_ASCII|(allow_wildcards ? FLAG_WILDCARD : 0)) && 
282                     name[i] != '.') {
283                         return False;
284                 }
285         }
286
287         /* it is a good 8.3 name */
288         return True;
289 }
290
291
292 /*
293   try to find a 8.3 name in the cache, and if found then
294   return the original long name. 
295 */
296 static const char *check_cache(struct pvfs_mangle_context *ctx, 
297                                const char *name)
298 {
299         uint32_t hash, multiplier;
300         unsigned int i;
301         const char *prefix;
302         char extension[4];
303
304         /* make sure that this is a mangled name from this cache */
305         if (!is_mangled(ctx, name)) {
306                 M_DEBUG(10,("check_cache: %s -> not mangled\n", name));
307                 return NULL;
308         }
309
310         /* we need to extract the hash from the 8.3 name */
311         hash = ctx->base_reverse[(unsigned char)name[7]];
312         for (multiplier=36, i=5;i>=ctx->mangle_prefix;i--) {
313                 uint32_t v = ctx->base_reverse[(unsigned char)name[i]];
314                 hash += multiplier * v;
315                 multiplier *= 36;
316         }
317
318         /* now look in the prefix cache for that hash */
319         prefix = cache_lookup(ctx, hash);
320         if (!prefix) {
321                 M_DEBUG(10,("check_cache: %s -> %08X -> not found\n", name, hash));
322                 return NULL;
323         }
324
325         /* we found it - construct the full name */
326         if (name[8] == '.') {
327                 strncpy(extension, name+9, 3);
328                 extension[3] = 0;
329         } else {
330                 extension[0] = 0;
331         }
332
333         if (extension[0]) {
334                 return talloc_asprintf(ctx, "%s.%s", prefix, extension);
335         }
336
337         return talloc_strdup(ctx, prefix);
338 }
339
340
341 /*
342   look for a DOS reserved name
343 */
344 static BOOL is_reserved_name(struct pvfs_mangle_context *ctx, const char *name)
345 {
346         if (FLAG_CHECK(name[0], FLAG_POSSIBLE1) &&
347             FLAG_CHECK(name[1], FLAG_POSSIBLE2) &&
348             FLAG_CHECK(name[2], FLAG_POSSIBLE3) &&
349             FLAG_CHECK(name[3], FLAG_POSSIBLE4)) {
350                 /* a likely match, scan the lot */
351                 int i;
352                 for (i=0; reserved_names[i]; i++) {
353                         if (strcasecmp(name, reserved_names[i]) == 0) {
354                                 return True;
355                         }
356                 }
357         }
358
359         return False;
360 }
361
362
363 /*
364  See if a filename is a legal long filename.
365  A filename ending in a '.' is not legal unless it's "." or "..". JRA.
366 */
367 static BOOL is_legal_name(struct pvfs_mangle_context *ctx, const char *name)
368 {
369         while (*name) {
370                 size_t c_size;
371                 codepoint_t c = next_codepoint(name, &c_size);
372                 if (c == INVALID_CODEPOINT) {
373                         return False;
374                 }
375                 /* all high chars are OK */
376                 if (c >= 128) {
377                         name += c_size;
378                         continue;
379                 }
380                 if (FLAG_CHECK(c, FLAG_ILLEGAL)) {
381                         return False;
382                 }
383                 name += c_size;
384         }
385
386         return True;
387 }
388
389 /*
390   the main forward mapping function, which converts a long filename to 
391   a 8.3 name
392
393   if need83 is not set then we only do the mangling if the name is illegal
394   as a long name
395
396   if cache83 is not set then we don't cache the result
397
398   return NULL if we don't need to do any conversion
399 */
400 static char *name_map(struct pvfs_mangle_context *ctx,
401                       const char *name, BOOL need83, BOOL cache83)
402 {
403         char *dot_p;
404         char lead_chars[7];
405         char extension[4];
406         unsigned int extension_length, i;
407         unsigned int prefix_len;
408         uint32_t hash, v;
409         char *new_name;
410         const char *basechars = MANGLE_BASECHARS;
411
412         /* reserved names are handled specially */
413         if (!is_reserved_name(ctx, name)) {
414                 /* if the name is already a valid 8.3 name then we don't need to 
415                    do anything */
416                 if (is_8_3(ctx, name, False, False)) {
417                         return NULL;
418                 }
419
420                 /* if the caller doesn't strictly need 8.3 then just check for illegal 
421                    filenames */
422                 if (!need83 && is_legal_name(ctx, name)) {
423                         return NULL;
424                 }
425         }
426
427         /* find the '.' if any */
428         dot_p = strrchr(name, '.');
429
430         if (dot_p) {
431                 /* if the extension contains any illegal characters or
432                    is too long or zero length then we treat it as part
433                    of the prefix */
434                 for (i=0; i<4 && dot_p[i+1]; i++) {
435                         if (! FLAG_CHECK(dot_p[i+1], FLAG_ASCII)) {
436                                 dot_p = NULL;
437                                 break;
438                         }
439                 }
440                 if (i == 0 || i == 4) dot_p = NULL;
441         }
442
443         /* the leading characters in the mangled name is taken from
444            the first characters of the name, if they are ascii otherwise
445            '_' is used
446         */
447         for (i=0;i<ctx->mangle_prefix && name[i];i++) {
448                 lead_chars[i] = name[i];
449                 if (! FLAG_CHECK(lead_chars[i], FLAG_ASCII)) {
450                         lead_chars[i] = '_';
451                 }
452                 lead_chars[i] = toupper(lead_chars[i]);
453         }
454         for (;i<ctx->mangle_prefix;i++) {
455                 lead_chars[i] = '_';
456         }
457
458         /* the prefix is anything up to the first dot */
459         if (dot_p) {
460                 prefix_len = PTR_DIFF(dot_p, name);
461         } else {
462                 prefix_len = strlen(name);
463         }
464
465         /* the extension of the mangled name is taken from the first 3
466            ascii chars after the dot */
467         extension_length = 0;
468         if (dot_p) {
469                 for (i=1; extension_length < 3 && dot_p[i]; i++) {
470                         char c = dot_p[i];
471                         if (FLAG_CHECK(c, FLAG_ASCII)) {
472                                 extension[extension_length++] = toupper(c);
473                         }
474                 }
475         }
476            
477         /* find the hash for this prefix */
478         v = hash = mangle_hash(ctx, name, prefix_len);
479
480         new_name = talloc_array_p(ctx, char, 13);
481         if (new_name == NULL) {
482                 return NULL;
483         }
484
485         /* now form the mangled name. */
486         for (i=0;i<ctx->mangle_prefix;i++) {
487                 new_name[i] = lead_chars[i];
488         }
489         new_name[7] = basechars[v % 36];
490         new_name[6] = '~';      
491         for (i=5; i>=ctx->mangle_prefix; i--) {
492                 v = v / 36;
493                 new_name[i] = basechars[v % 36];
494         }
495
496         /* add the extension */
497         if (extension_length) {
498                 new_name[8] = '.';
499                 memcpy(&new_name[9], extension, extension_length);
500                 new_name[9+extension_length] = 0;
501         } else {
502                 new_name[8] = 0;
503         }
504
505         if (cache83) {
506                 /* put it in the cache */
507                 cache_insert(ctx, name, prefix_len, hash);
508         }
509
510         M_DEBUG(10,("name_map: %s -> %08X -> %s (cache=%d)\n", 
511                    name, hash, new_name, cache83));
512
513         return new_name;
514 }
515
516
517 /* initialise the flags table 
518
519   we allow only a very restricted set of characters as 'ascii' in this
520   mangling backend. This isn't a significant problem as modern clients
521   use the 'long' filenames anyway, and those don't have these
522   restrictions. 
523 */
524 static void init_tables(struct pvfs_mangle_context *ctx)
525 {
526         const char *basechars = MANGLE_BASECHARS;
527         int i;
528         /* the list of reserved dos names - all of these are illegal */
529
530         ZERO_STRUCT(ctx->char_flags);
531
532         for (i=1;i<128;i++) {
533                 if ((i >= '0' && i <= '9') || 
534                     (i >= 'a' && i <= 'z') || 
535                     (i >= 'A' && i <= 'Z')) {
536                         ctx->char_flags[i] |=  (FLAG_ASCII | FLAG_BASECHAR);
537                 }
538                 if (strchr("_-$~", i)) {
539                         ctx->char_flags[i] |= FLAG_ASCII;
540                 }
541
542                 if (strchr("*\\/?<>|\":", i)) {
543                         ctx->char_flags[i] |= FLAG_ILLEGAL;
544                 }
545
546                 if (strchr("*?\"<>", i)) {
547                         ctx->char_flags[i] |= FLAG_WILDCARD;
548                 }
549         }
550
551         ZERO_STRUCT(ctx->base_reverse);
552         for (i=0;i<36;i++) {
553                 ctx->base_reverse[(uint8_t)basechars[i]] = i;
554         }       
555
556         /* fill in the reserved names flags. These are used as a very
557            fast filter for finding possible DOS reserved filenames */
558         for (i=0; reserved_names[i]; i++) {
559                 unsigned char c1, c2, c3, c4;
560
561                 c1 = (unsigned char)reserved_names[i][0];
562                 c2 = (unsigned char)reserved_names[i][1];
563                 c3 = (unsigned char)reserved_names[i][2];
564                 c4 = (unsigned char)reserved_names[i][3];
565
566                 ctx->char_flags[c1] |= FLAG_POSSIBLE1;
567                 ctx->char_flags[c2] |= FLAG_POSSIBLE2;
568                 ctx->char_flags[c3] |= FLAG_POSSIBLE3;
569                 ctx->char_flags[c4] |= FLAG_POSSIBLE4;
570                 ctx->char_flags[tolower(c1)] |= FLAG_POSSIBLE1;
571                 ctx->char_flags[tolower(c2)] |= FLAG_POSSIBLE2;
572                 ctx->char_flags[tolower(c3)] |= FLAG_POSSIBLE3;
573                 ctx->char_flags[tolower(c4)] |= FLAG_POSSIBLE4;
574
575                 ctx->char_flags[(unsigned char)'.'] |= FLAG_POSSIBLE4;
576         }
577
578         ctx->mangle_modulus = 1;
579         for (i=0;i<(7-ctx->mangle_prefix);i++) {
580                 ctx->mangle_modulus *= 36;
581         }
582 }
583
584 /* 
585    initialise the mangling code 
586  */
587 NTSTATUS pvfs_mangle_init(struct pvfs_state *pvfs)
588 {
589         struct pvfs_mangle_context *ctx;
590
591         ctx = talloc_p(pvfs, struct pvfs_mangle_context);
592         if (ctx == NULL) {
593                 return NT_STATUS_NO_MEMORY;
594         }
595         ctx->prefix_cache = talloc_array_p(ctx, char *, MANGLE_CACHE_SIZE);
596         if (ctx->prefix_cache == NULL) {
597                 return NT_STATUS_NO_MEMORY;
598         }
599         ctx->prefix_cache_hashes = talloc_array_p(ctx, uint32_t, MANGLE_CACHE_SIZE);
600         if (ctx->prefix_cache_hashes == NULL) {
601                 return NT_STATUS_NO_MEMORY;
602         }
603
604         memset(ctx->prefix_cache, 0, sizeof(char *)*MANGLE_CACHE_SIZE);
605         memset(ctx->prefix_cache_hashes, 0, sizeof(uint32_t)*MANGLE_CACHE_SIZE);
606
607         ctx->mangle_prefix = lp_parm_int(-1, "mangle", "prefix", -1);
608         if (ctx->mangle_prefix < 0 || ctx->mangle_prefix > 6) {
609                 ctx->mangle_prefix = DEFAULT_MANGLE_PREFIX;
610         }
611
612         init_tables(ctx);
613
614         pvfs->mangle_ctx = ctx;
615
616         return NT_STATUS_OK;
617 }
618
619
620 /*
621   return the short name for a component of a full name
622 */
623 char *pvfs_short_name_component(struct pvfs_state *pvfs, const char *name)
624 {
625         return name_map(pvfs->mangle_ctx, name, True, True);
626 }
627
628
629 /*
630   return the short name for a given entry in a directory
631 */
632 const char *pvfs_short_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
633                             struct pvfs_filename *name)
634 {
635         char *p = strrchr(name->full_name, '/');
636         char *ret = pvfs_short_name_component(pvfs, p+1);
637         if (ret == NULL) {
638                 return p+1;
639         }
640         talloc_steal(mem_ctx, ret);
641         return ret;
642 }
643
644 /*
645   lookup a mangled name, returning the original long name if present
646   in the cache
647 */
648 char *pvfs_mangled_lookup(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx, 
649                           const char *name)
650 {
651         const char *ret;
652         ret = check_cache(pvfs->mangle_ctx, name);
653         if (ret) {
654                 return talloc_steal(mem_ctx, ret);
655         }
656         return NULL;
657 }
658
659
660 /*
661   look for a DOS reserved name
662 */
663 BOOL pvfs_is_reserved_name(struct pvfs_state *pvfs, const char *name)
664 {
665         return is_reserved_name(pvfs->mangle_ctx, name);
666 }
667
668
669 /*
670   see if a component of a filename could be a mangled name from our
671   mangling code
672 */
673 BOOL pvfs_is_mangled_component(struct pvfs_state *pvfs, const char *name)
674 {
675         return is_mangled_component(pvfs->mangle_ctx, name, strlen(name));
676 }