2 Unix SMB/CIFS implementation.
4 Copyright (C) Jeremy Allison 1998
5 Copyright (C) Andrew Tridgell 2002
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.
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.
25 /* ************************************************************************** **
26 * Used only in do_fwd_mangled_map(), below.
27 * ************************************************************************** **
29 static char *map_filename( char *s, /* This is null terminated */
30 const char *pattern, /* This isn't. */
31 int len ) /* This is the length of pattern. */
33 static pstring matching_bit; /* The bit of the string which matches */
34 /* a * in pattern if indeed there is a * */
35 char *sp; /* Pointer into s. */
36 char *pp; /* Pointer into p. */
37 char *match_start; /* Where the matching bit starts. */
40 StrnCpy( pat, pattern, len ); /* Get pattern into a proper string! */
41 pstrcpy( matching_bit, "" ); /* Match but no star gets this. */
42 pp = pat; /* Initialize the pointers. */
45 if( strequal(s, ".") || strequal(s, ".."))
47 return NULL; /* Do not map '.' and '..' */
50 if( (len == 1) && (*pattern == '*') )
52 return NULL; /* Impossible, too ambiguous for */
55 while( (*sp) /* Not the end of the string. */
56 && (*pp) /* Not the end of the pattern. */
57 && (*sp == *pp) /* The two match. */
58 && (*pp != '*') ) /* No wildcard. */
60 sp++; /* Keep looking. */
64 if( !*sp && !*pp ) /* End of pattern. */
65 return( matching_bit ); /* Simple match. Return empty string. */
69 pp++; /* Always interrested in the chacter */
71 if( !*pp ) /* It is at the end of the pattern. */
73 StrnCpy( matching_bit, s, sp-s );
74 return( matching_bit );
78 /* The next character in pattern must match a character further */
79 /* along s than sp so look for that character. */
81 while( (*sp) /* Not the end of s. */
82 && (*sp != *pp) ) /* Not the same */
83 sp++; /* Keep looking. */
84 if( !*sp ) /* Got to the end without a match. */
87 } /* Still hope for a match. */
90 /* Now sp should point to a matching character. */
91 StrnCpy(matching_bit, match_start, sp-match_start);
92 /* Back to needing a stright match again. */
93 while( (*sp) /* Not the end of the string. */
94 && (*pp) /* Not the end of the pattern. */
95 && (*sp == *pp) ) /* The two match. */
97 sp++; /* Keep looking. */
100 if( !*sp && !*pp ) /* Both at end so it matched */
101 return( matching_bit );
107 return( NULL ); /* No match. */
111 /* ************************************************************************** **
112 * MangledMap is a series of name pairs in () separated by spaces.
113 * If s matches the first of the pair then the name given is the
114 * second of the pair. A * means any number of any character and if
115 * present in the second of the pair as well as the first the
116 * matching part of the first string takes the place of the * in the
119 * I wanted this so that we could have RCS files which can be used
120 * by UNIX and DOS programs. My mapping string is (RCS rcs) which
121 * converts the UNIX RCS file subdirectory to lowercase thus
122 * preventing mangling.
124 * See 'mangled map' in smb.conf(5).
126 * ************************************************************************** **
128 static void mangled_map(char *s, const char *MangledMap)
130 const char *start=MangledMap; /* Use this to search for mappings. */
131 const char *end; /* Used to find the end of strings. */
133 pstring new_string; /* Make up the result here. */
134 char *np; /* Points into new_string. */
136 DEBUG( 5, ("Mangled Mapping '%s' map '%s'\n", s, MangledMap) );
138 while( (*start) && (*start != '(') )
141 continue; /* Always check for the end. */
142 start++; /* Skip the ( */
143 end = start; /* Search for the ' ' or a ')' */
144 DEBUG( 5, ("Start of first in pair '%s'\n", start) );
145 while( (*end) && !((*end == ' ') || (*end == ')')) )
149 continue; /* Always check for the end. */
151 DEBUG( 5, ("End of first in pair '%s'\n", end) );
152 if( (match_string = map_filename( s, start, end-start )) ) {
153 int size_left = sizeof(new_string) - 1;
154 DEBUG( 5, ("Found a match\n") );
156 start = end + 1; /* Point to start of what it is to become. */
157 DEBUG( 5, ("Start of second in pair '%s'\n", start) );
160 while( (*end && size_left > 0) /* Not the end of string. */
161 && (*end != ')') /* Not the end of the pattern. */
162 && (*end != '*') ) { /* Not a wildcard. */
169 continue; /* Always check for the end. */
173 safe_strcpy( np, match_string, size_left );
174 np += strlen( match_string );
175 size_left -= strlen( match_string );
176 end++; /* Skip the '*' */
177 while ((*end && size_left > 0) /* Not the end of string. */
178 && (*end != ')') /* Not the end of the pattern. */
179 && (*end != '*')) { /* Not a wildcard. */
186 continue; /* Always check for the end. */
189 *np++ = '\0'; /* NULL terminate it. */
190 DEBUG(5,("End of second in pair '%s'\n", end));
191 new_string[sizeof(new_string)-1] = '\0';
192 pstrcpy( s, new_string ); /* Substitute with the new name. */
193 DEBUG( 5, ("s is now '%s'\n", s) );
195 start = end; /* Skip a bit which cannot be wanted anymore. */
201 front end routine to the mangled map code
202 personally I think that the whole idea of "mangled map" is completely bogus
204 void mangle_map_filename(fstring fname, const struct share_params *p)
208 map = lp_mangled_map(p);
209 if (!map || !*map) return;
211 mangled_map(fname, map);