2 Unix SMB/Netbios implementation.
5 Copyright (C) Andrew Tridgell 1992-1998
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 2 of the License, or
10 (at your option) any later version.
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.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 extern int DEBUGLEVEL;
25 extern int case_default;
26 extern BOOL case_mangle;
28 /****************************************************************************
29 * Provide a checksum on a string
31 * Input: s - the nul-terminated character string for which the checksum
33 * Output: The checksum value calculated for s.
35 ****************************************************************************/
36 int str_checksum(char *s)
45 res ^= (c << (i % 15)) ^ (c >> (15-(i%15)));
51 /****************************************************************************
52 return True if a name is a special msdos reserved name
53 ****************************************************************************/
54 static BOOL is_reserved_msdos(char *fname)
59 StrnCpy (upperFname, fname, 12);
61 /* lpt1.txt and con.txt etc are also illegal */
62 p=strchr(upperFname,'.');
65 strupper (upperFname);
66 if ((strcmp(upperFname,"CLOCK$") == 0) ||
67 (strcmp(upperFname,"CON") == 0) ||
68 (strcmp(upperFname,"AUX") == 0) ||
69 (strcmp(upperFname,"COM1") == 0) ||
70 (strcmp(upperFname,"COM2") == 0) ||
71 (strcmp(upperFname,"COM3") == 0) ||
72 (strcmp(upperFname,"COM4") == 0) ||
73 (strcmp(upperFname,"LPT1") == 0) ||
74 (strcmp(upperFname,"LPT2") == 0) ||
75 (strcmp(upperFname,"LPT3") == 0) ||
76 (strcmp(upperFname,"NUL") == 0) ||
77 (strcmp(upperFname,"PRN") == 0))
81 } /* is_reserved_msdos */
85 /****************************************************************************
86 return True if a name is in 8.3 dos format
87 ****************************************************************************/
88 BOOL is_8_3(char *fname, BOOL check_case)
92 char *slash_pos = strrchr(fname,'/');
99 DEBUG(5,("checking %s for 8.3\n",fname));
101 if( check_case && case_mangle )
103 switch (case_default)
106 if (strhasupper(fname)) return(False);
109 if (strhaslower(fname)) return(False);
114 /* can't be longer than 12 chars */
115 if( len == 0 || len > 12 )
118 /* can't be an MS-DOS Special file such as lpt1 or even lpt1.txt */
119 if( is_reserved_msdos(fname) )
122 /* can't contain invalid dos chars */
123 /* Windows use the ANSI charset.
124 But filenames are translated in the PC charset.
125 This Translation may be more or less relaxed depending
126 the Windows application. */
128 /* %%% A nice improvment to name mangling would be to translate
129 filename to ANSI charset on the smb server host */
131 dot_pos = strchr(fname,'.');
140 if((skip = skip_multibyte_char( *p )) != 0)
144 if (*p == '.' && !dot_pos)
145 dot_pos = (char *) p;
153 /* no dot and less than 9 means OK */
157 l = PTR_DIFF(dot_pos,fname);
159 /* base must be at least 1 char except special cases . and .. */
161 return(strcmp(fname,".") == 0 || strcmp(fname,"..") == 0);
163 /* base can't be greater than 8 */
167 if( lp_strip_dot() &&
169 !strchr(dot_pos+1,'.') )
175 /* extension must be between 1 and 3 */
176 if( (len - l < 2 ) || (len - l > 4) )
179 /* extension can't have a dot */
180 if( strchr(dot_pos+1,'.') )
183 /* must be in 8.3 format */
187 /* -------------------------------------------------------------------------- **
188 * This section creates and maintains a stack of name mangling results.
189 * The original comments read: "keep a stack of name mangling results - just
190 * so file moves and copies have a chance of working" (whatever that means).
192 * There are three functions to manage the stack:
193 * reset_mangled_stack() -
194 * push_mangled_name() -
195 * check_mangled_stack() -
198 fstring *mangled_stack = NULL;
199 int mangled_stack_size = 0;
200 int mangled_stack_len = 0;
202 /****************************************************************************
203 * create the mangled stack CRH
204 ****************************************************************************/
205 void reset_mangled_stack( int size )
210 mangled_stack_size = 0;
211 mangled_stack_len = 0;
216 mangled_stack = (fstring *)malloc( sizeof(fstring) * size );
218 mangled_stack_size = size;
221 mangled_stack = NULL;
222 } /* create_mangled_stack */
224 /****************************************************************************
225 * push a mangled name onto the stack CRH
226 ****************************************************************************/
227 static void push_mangled_name(char *s)
232 /* If the stack doesn't exist... Fail. */
236 /* If name <s> is already on the stack, move it to the top. */
237 for( i=0; i<mangled_stack_len; i++ )
239 if( strcmp( s, mangled_stack[i] ) == 0 )
241 array_promote( mangled_stack[0],sizeof(fstring), i );
246 /* If name <s> wasn't already there, add it to the top of the stack. */
247 memmove( mangled_stack[1], mangled_stack[0],
248 sizeof(fstring) * MIN(mangled_stack_len, mangled_stack_size-1) );
249 strcpy( mangled_stack[0], s );
250 mangled_stack_len = MIN( mangled_stack_size, mangled_stack_len+1 );
253 * Find the last dot '.' in the name,
254 * if there are any upper case characters past the last dot
255 * and there are no more than three characters past the last dot
256 * then terminate the name *at* the last dot.
258 p = strrchr( mangled_stack[0], '.' );
259 if( p && (!strhasupper(p+1)) && (strlen(p+1) < (size_t)4) )
262 } /* push_mangled_name */
264 /****************************************************************************
265 * check for a name on the mangled name stack CRH
266 ****************************************************************************/
267 BOOL check_mangled_stack(char *s)
272 char *p = strrchr( s, '.' );
273 BOOL check_extension = False;
277 /* If the stack doesn't exist, fail. */
281 /* If there is a file extension, then we need to play with it, too. */
284 check_extension = True;
285 StrnCpy( extension, p, 4 );
286 strlower( extension ); /* XXXXXXX */
289 for( i=0; i<mangled_stack_len; i++ )
291 strcpy(tmpname,mangled_stack[i]);
292 mangle_name_83(tmpname);
293 if( strequal(tmpname,s) )
295 strcpy(s,mangled_stack[i]);
298 if( check_extension && !strchr(mangled_stack[i],'.') )
300 pstrcpy(tmpname,mangled_stack[i]);
301 strcat(tmpname,extension);
302 mangle_name_83(tmpname);
303 if( strequal(tmpname,s) )
305 strcpy(s,mangled_stack[i]);
312 if( i < mangled_stack_len )
314 DEBUG(3,("Found %s on mangled stack as %s\n",s,mangled_stack[i]));
315 array_promote(mangled_stack[0],sizeof(fstring),i);
320 } /* check_mangled_stack */
323 /* End of the mangled stack section.
324 * -------------------------------------------------------------------------- **
328 static char *map_filename( char *s, /* This is null terminated */
329 char *pattern, /* This isn't. */
330 int len ) /* This is the length of pattern. */
332 static pstring matching_bit; /* The bit of the string which matches */
333 /* a * in pattern if indeed there is a * */
334 char *sp; /* Pointer into s. */
335 char *pp; /* Pointer into p. */
336 char *match_start; /* Where the matching bit starts. */
339 StrnCpy(pat, pattern, len); /* Get pattern into a proper string! */
340 pstrcpy(matching_bit,""); /* Match but no star gets this. */
341 pp = pat; /* Initialise the pointers. */
343 if( (len == 1) && (*pattern == '*') )
345 return NULL; /* Impossible, too ambiguous for */
348 while ((*sp) /* Not the end of the string. */
349 && (*pp) /* Not the end of the pattern. */
350 && (*sp == *pp) /* The two match. */
351 && (*pp != '*')) /* No wildcard. */
353 sp++; /* Keep looking. */
357 if( !*sp && !*pp ) /* End of pattern. */
358 return( matching_bit ); /* Simple match. Return empty string. */
362 pp++; /* Always interrested in the chacter */
364 if (!*pp) /* It is at the end of the pattern. */
366 StrnCpy(matching_bit, s, sp-s);
371 /* The next character in pattern must match a character further */
372 /* along s than sp so look for that character. */
374 while( (*sp) /* Not the end of s. */
375 && (*sp != *pp)) /* Not the same */
376 sp++; /* Keep looking. */
377 if (!*sp) /* Got to the end without a match. */
380 } /* Still hope for a match. */
383 /* Now sp should point to a matching character. */
384 StrnCpy(matching_bit, match_start, sp-match_start);
385 /* Back to needing a stright match again. */
386 while( (*sp) /* Not the end of the string. */
387 && (*pp) /* Not the end of the pattern. */
388 && (*sp == *pp) ) /* The two match. */
390 sp++; /* Keep looking. */
393 if (!*sp && !*pp) /* Both at end so it matched */
400 return NULL; /* No match. */
404 /* this is the magic char used for mangling */
405 char magic_char = '~';
408 /****************************************************************************
409 return True if the name could be a mangled name
410 ****************************************************************************/
411 BOOL is_mangled( char *s )
413 char *m = strchr(s,magic_char);
418 /* we use two base 36 chars before the extension */
419 if( m[1] == '.' || m[1] == 0 ||
420 m[2] == '.' || m[2] == 0 ||
421 (m[3] != '.' && m[3] != 0) )
422 return( is_mangled(m+1) );
430 /****************************************************************************
431 return a base 36 character. v must be from 0 to 35.
432 ****************************************************************************/
433 static char base36(unsigned int v)
435 static char basechars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
436 return basechars[v % 36];
440 static void do_fwd_mangled_map(char *s, char *MangledMap)
442 /* MangledMap is a series of name pairs in () separated by spaces.
443 * If s matches the first of the pair then the name given is the
444 * second of the pair. A * means any number of any character and if
445 * present in the second of the pair as well as the first the
446 * matching part of the first string takes the place of the * in the
449 * I wanted this so that we could have RCS files which can be used
450 * by UNIX and DOS programs. My mapping string is (RCS rcs) which
451 * converts the UNIX RCS file subdirectory to lowercase thus
452 * preventing mangling.
454 char *start=MangledMap; /* Use this to search for mappings. */
455 char *end; /* Used to find the end of strings. */
457 pstring new_string; /* Make up the result here. */
458 char *np; /* Points into new_string. */
460 DEBUG(5,("Mangled Mapping '%s' map '%s'\n", s, MangledMap));
463 while ((*start) && (*start != '('))
466 continue; /* Always check for the end. */
467 start++; /* Skip the ( */
468 end = start; /* Search for the ' ' or a ')' */
469 DEBUG(5,("Start of first in pair '%s'\n", start));
470 while ((*end) && !((*end == ' ') || (*end == ')')))
475 continue; /* Always check for the end. */
477 DEBUG(5,("End of first in pair '%s'\n", end));
478 if ((match_string = map_filename(s, start, end-start)))
480 DEBUG(5,("Found a match\n"));
482 start = end+1; /* Point to start of what it is to become. */
483 DEBUG(5,("Start of second in pair '%s'\n", start));
486 while ((*end) /* Not the end of string. */
487 && (*end != ')') /* Not the end of the pattern. */
488 && (*end != '*')) /* Not a wildcard. */
493 continue; /* Always check for the end. */
497 pstrcpy(np, match_string);
498 np += strlen(match_string);
499 end++; /* Skip the '*' */
500 while ((*end) /* Not the end of string. */
501 && (*end != ')') /* Not the end of the pattern. */
502 && (*end != '*')) /* Not a wildcard. */
508 continue; /* Always check for the end. */
510 *np++ = '\0'; /* NULL terminate it. */
511 DEBUG(5,("End of second in pair '%s'\n", end));
512 pstrcpy(s, new_string); /* Substitute with the new name. */
513 DEBUG(5,("s is now '%s'\n", s));
515 start = end; /* Skip a bit which cannot be wanted */
519 } /* do_fwd_mangled_map */
521 /****************************************************************************
522 do the actual mangling to 8.3 format
523 ****************************************************************************/
524 void mangle_name_83(char *s)
526 int csum = str_checksum(s);
538 if( p && (strlen(p+1) < (size_t)4) )
540 BOOL all_normal = (strisnormal(p+1)); /* XXXXXXXXX */
542 if (all_normal && p[1] != 0)
545 csum = str_checksum(s);
552 DEBUG(5,("Mangling name %s to ",s));
557 strcpy(extension,"___");
561 while (*p && extlen < 3)
563 skip = skip_multibyte_char(*p);
568 extension[extlen++] = p[0];
569 extension[extlen++] = p[1];
573 extension[extlen++] = base36 (((unsigned char) *p) % 36);
579 extension[extlen++] = p[0];
584 if (isdoschar (*p) && *p != '.')
585 extension[extlen++] = p[0];
589 extension[extlen] = 0;
595 while (*p && baselen < 5)
597 skip = skip_multibyte_char(*p);
602 base[baselen++] = p[0];
603 base[baselen++] = p[1];
607 base[baselen++] = base36 (((unsigned char) *p) % 36);
613 base[baselen++] = p[0];
618 if (isdoschar (*p) && *p != '.')
619 base[baselen++] = p[0];
625 csum = csum % (36*36);
627 sprintf(s,"%s%c%c%c",base,magic_char,base36(csum/36),base36(csum%36));
636 } /* mangle_name_83 */
640 /*******************************************************************
641 work out if a name is illegal, even for long names
642 ******************************************************************/
643 static BOOL illegal_name(char *name)
645 static unsigned char illegal[256];
646 static BOOL initialised=False;
652 char *ill = "*\\/?<>|\":";
655 bzero((char *)illegal,256);
656 for( s = (unsigned char *)ill; *s; s++ )
660 for (s = (unsigned char *)name; *s;)
662 skip = skip_multibyte_char( *s );
678 /****************************************************************************
679 convert a filename to DOS format. return True if successful.
680 ****************************************************************************/
681 BOOL name_map_mangle(char *OutName,BOOL need83,int snum)
683 #ifdef MANGLE_LONG_FILENAMES
684 if( !need83 && illegal_name(OutName) )
688 /* apply any name mappings */
690 char *map = lp_mangled_map(snum);
693 do_fwd_mangled_map(OutName,map);
696 /* check if it's already in 8.3 format */
697 if( need83 && !is_8_3(OutName, True) )
699 if( !lp_manglednames(snum) )
702 /* mangle it into 8.3 */
703 push_mangled_name(OutName);
704 mangle_name_83(OutName);
708 } /* name_map_mangle */