updated the 3.0 branch from the head branch - ready for alpha18
[gd/samba/.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 2 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, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 /* ************************************************************************** **
26  * Used only in do_fwd_mangled_map(), below.
27  * ************************************************************************** **
28  */
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. */
32   {
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. */
38   pstring pat;
39
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. */
43   sp = s;
44
45   if( strequal(s, ".") || strequal(s, ".."))
46     {
47     return NULL;                /* Do not map '.' and '..' */
48     }
49
50   if( (len == 1) && (*pattern == '*') )
51     {
52     return NULL;                /* Impossible, too ambiguous for */
53     }                           /* words! */
54
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. */
59     {
60     sp++;                       /* Keep looking. */
61     pp++;
62     }
63
64   if( !*sp && !*pp )            /* End of pattern. */
65     return( matching_bit );     /* Simple match.  Return empty string. */
66
67   if( *pp == '*' )
68     {
69     pp++;                       /* Always interrested in the chacter */
70                                 /* after the '*' */
71     if( !*pp )                  /* It is at the end of the pattern. */
72       {
73       StrnCpy( matching_bit, s, sp-s );
74       return( matching_bit );
75       }
76     else
77       {
78       /* The next character in pattern must match a character further */
79       /* along s than sp so look for that character. */
80       match_start = sp;
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. */
85         {
86         return( NULL );
87         }                       /* Still hope for a match. */
88       else
89         {
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. */
96           {
97           sp++;                 /* Keep looking. */
98           pp++;
99           }
100         if( !*sp && !*pp )      /* Both at end so it matched */
101           return( matching_bit );
102         else
103           return( NULL );
104         }
105       }
106     }
107   return( NULL );               /* No match. */
108   } /* map_filename */
109
110
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
117  * second.
118  *
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.
123  *
124  * See 'mangled map' in smb.conf(5).
125  *
126  * ************************************************************************** **
127  */
128 static void mangled_map(char *s, const char *MangledMap)
129 {
130         const char *start=MangledMap;       /* Use this to search for mappings. */
131         const char *end;                    /* Used to find the end of strings. */
132         char *match_string;
133         pstring new_string;           /* Make up the result here. */
134         char *np;                     /* Points into new_string. */
135
136         DEBUG( 5, ("Mangled Mapping '%s' map '%s'\n", s, MangledMap) );
137         while( *start ) {
138                 while( (*start) && (*start != '(') )
139                         start++;
140                 if( !*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 == ')')) )
146                         end++;
147                 if( !*end ) {
148                         start = end;
149                         continue;                 /* Always check for the end. */
150                 }
151                 DEBUG( 5, ("End of first in pair '%s'\n", end) );
152                 if( (match_string = map_filename( s, start, end-start )) ) {
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)             /* Not the end of string. */
160                                && (*end != ')')      /* Not the end of the pattern. */
161                                && (*end != '*') )    /* Not a wildcard. */
162                                 *np++ = *end++;
163
164                         if( !*end ) {
165                                 start = end;
166                                 continue;               /* Always check for the end. */
167                         }
168                         if( *end == '*' ) {
169                                 pstrcpy( np, match_string );
170                                 np += strlen( match_string );
171                                 end++;                  /* Skip the '*' */
172                                 while ((*end)           /* Not the end of string. */
173                                        && (*end != ')') /* Not the end of the pattern. */
174                                        && (*end != '*'))/* Not a wildcard. */
175                                         *np++ = *end++;
176                         }
177                         if (!*end) {
178                                 start = end;
179                                 continue;               /* Always check for the end. */
180                         }
181                         *np++ = '\0';             /* NULL terminate it. */
182                         DEBUG(5,("End of second in pair '%s'\n", end));
183                         pstrcpy( s, new_string );  /* Substitute with the new name. */
184                         DEBUG( 5, ("s is now '%s'\n", s) );
185                 }
186                 start = end;  /* Skip a bit which cannot be wanted anymore. */
187                 start++;
188         }
189 }
190
191 /*
192   front end routine to the mangled map code 
193   personally I think that the whole idea of "mangled map" is completely bogus
194 */
195 void mangle_map_filename(char *fname, int snum)
196 {
197         char *map;
198
199         map = lp_mangled_map(snum);
200         if (!map || !*map) return;
201
202         mangled_map(fname, map);
203 }