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