r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[vlendec/samba-autobuild/.git] / source3 / smbd / mangle_map.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Name mapping code 
4    Copyright (C) Jeremy Allison 1998
5    Copyright (C) Andrew Tridgell 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 #include "includes.h"
22
23
24 /* ************************************************************************** **
25  * Used only in do_fwd_mangled_map(), below.
26  * ************************************************************************** **
27  */
28 static char *map_filename( char *s,         /* This is null terminated */
29                            const char *pattern,   /* This isn't. */
30                            int len )        /* This is the length of pattern. */
31   {
32   static pstring matching_bit;  /* The bit of the string which matches */
33                                 /* a * in pattern if indeed there is a * */
34   char *sp;                     /* Pointer into s. */
35   char *pp;                     /* Pointer into p. */
36   char *match_start;            /* Where the matching bit starts. */
37   pstring pat;
38
39   StrnCpy( pat, pattern, len ); /* Get pattern into a proper string! */
40   pstrcpy( matching_bit, "" );  /* Match but no star gets this. */
41   pp = pat;                     /* Initialize the pointers. */
42   sp = s;
43
44   if( strequal(s, ".") || strequal(s, ".."))
45     {
46     return NULL;                /* Do not map '.' and '..' */
47     }
48
49   if( (len == 1) && (*pattern == '*') )
50     {
51     return NULL;                /* Impossible, too ambiguous for */
52     }                           /* words! */
53
54   while( (*sp)                  /* Not the end of the string. */
55       && (*pp)                  /* Not the end of the pattern. */
56       && (*sp == *pp)           /* The two match. */
57       && (*pp != '*') )         /* No wildcard. */
58     {
59     sp++;                       /* Keep looking. */
60     pp++;
61     }
62
63   if( !*sp && !*pp )            /* End of pattern. */
64     return( matching_bit );     /* Simple match.  Return empty string. */
65
66   if( *pp == '*' )
67     {
68     pp++;                       /* Always interrested in the chacter */
69                                 /* after the '*' */
70     if( !*pp )                  /* It is at the end of the pattern. */
71       {
72       StrnCpy( matching_bit, s, sp-s );
73       return( matching_bit );
74       }
75     else
76       {
77       /* The next character in pattern must match a character further */
78       /* along s than sp so look for that character. */
79       match_start = sp;
80       while( (*sp)              /* Not the end of s. */
81           && (*sp != *pp) )     /* Not the same  */
82         sp++;                   /* Keep looking. */
83       if( !*sp )                /* Got to the end without a match. */
84         {
85         return( NULL );
86         }                       /* Still hope for a match. */
87       else
88         {
89         /* Now sp should point to a matching character. */
90         StrnCpy(matching_bit, match_start, sp-match_start);
91         /* Back to needing a stright match again. */
92         while( (*sp)            /* Not the end of the string. */
93             && (*pp)            /* Not the end of the pattern. */
94             && (*sp == *pp) )   /* The two match. */
95           {
96           sp++;                 /* Keep looking. */
97           pp++;
98           }
99         if( !*sp && !*pp )      /* Both at end so it matched */
100           return( matching_bit );
101         else
102           return( NULL );
103         }
104       }
105     }
106   return( NULL );               /* No match. */
107   } /* map_filename */
108
109
110 /* ************************************************************************** **
111  * MangledMap is a series of name pairs in () separated by spaces.
112  * If s matches the first of the pair then the name given is the
113  * second of the pair.  A * means any number of any character and if
114  * present in the second of the pair as well as the first the
115  * matching part of the first string takes the place of the * in the
116  * second.
117  *
118  * I wanted this so that we could have RCS files which can be used
119  * by UNIX and DOS programs.  My mapping string is (RCS rcs) which
120  * converts the UNIX RCS file subdirectory to lowercase thus
121  * preventing mangling.
122  *
123  * See 'mangled map' in smb.conf(5).
124  *
125  * ************************************************************************** **
126  */
127 static void mangled_map(char *s, const char *MangledMap)
128 {
129         const char *start=MangledMap;       /* Use this to search for mappings. */
130         const char *end;                    /* Used to find the end of strings. */
131         char *match_string;
132         pstring new_string;           /* Make up the result here. */
133         char *np;                     /* Points into new_string. */
134
135         DEBUG( 5, ("Mangled Mapping '%s' map '%s'\n", s, MangledMap) );
136         while( *start ) {
137                 while( (*start) && (*start != '(') )
138                         start++;
139                 if( !*start )
140                         continue;                 /* Always check for the end. */
141                 start++;                    /* Skip the ( */
142                 end = start;                /* Search for the ' ' or a ')' */
143                 DEBUG( 5, ("Start of first in pair '%s'\n", start) );
144                 while( (*end) && !((*end == ' ') || (*end == ')')) )
145                         end++;
146                 if( !*end ) {
147                         start = end;
148                         continue;                 /* Always check for the end. */
149                 }
150                 DEBUG( 5, ("End of first in pair '%s'\n", end) );
151                 if( (match_string = map_filename( s, start, end-start )) ) {
152                         int size_left = sizeof(new_string) - 1;
153                         DEBUG( 5, ("Found a match\n") );
154                         /* Found a match. */
155                         start = end + 1; /* Point to start of what it is to become. */
156                         DEBUG( 5, ("Start of second in pair '%s'\n", start) );
157                         end = start;
158                         np = new_string;
159                         while( (*end && size_left > 0)    /* Not the end of string. */
160                                && (*end != ')')      /* Not the end of the pattern. */
161                                && (*end != '*') ) {   /* Not a wildcard. */
162                                 *np++ = *end++;
163                                 size_left--;
164                         }
165
166                         if( !*end ) {
167                                 start = end;
168                                 continue;               /* Always check for the end. */
169                         }
170                         if( *end == '*' ) {
171                                 if (size_left > 0 )
172                                         safe_strcpy( np, match_string, size_left );
173                                 np += strlen( match_string );
174                                 size_left -= strlen( match_string );
175                                 end++;                  /* Skip the '*' */
176                                 while ((*end && size_left >  0)   /* Not the end of string. */
177                                        && (*end != ')') /* Not the end of the pattern. */
178                                        && (*end != '*')) { /* Not a wildcard. */
179                                         *np++ = *end++;
180                                         size_left--;
181                                 }
182                         }
183                         if (!*end) {
184                                 start = end;
185                                 continue;               /* Always check for the end. */
186                         }
187                         if (size_left > 0)
188                                 *np++ = '\0';             /* NULL terminate it. */
189                         DEBUG(5,("End of second in pair '%s'\n", end));
190                         new_string[sizeof(new_string)-1] = '\0';
191                         pstrcpy( s, new_string );  /* Substitute with the new name. */
192                         DEBUG( 5, ("s is now '%s'\n", s) );
193                 }
194                 start = end;  /* Skip a bit which cannot be wanted anymore. */
195                 start++;
196         }
197 }
198
199 /*
200   front end routine to the mangled map code 
201   personally I think that the whole idea of "mangled map" is completely bogus
202 */
203 void mangle_map_filename(fstring fname, const struct share_params *p)
204 {
205         char *map;
206
207         map = lp_mangled_map(p);
208         if (!map || !*map) return;
209
210         mangled_map(fname, map);
211 }