be1662437e0c9acaae7645d3ff4cce5828ddfcaa
[jelmer/samba4-debian.git] / source / ntvfs / posix / pvfs_resolve.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    POSIX NTVFS backend - filename resolution
5
6    Copyright (C) Andrew Tridgell 2004
7
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 2 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program; if not, write to the Free Software
20    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23 /*
24   this is the core code for converting a filename from the format as
25   given by a client to a posix filename, including any case-matching
26   required, and checks for legal characters
27 */
28
29
30 #include "include/includes.h"
31 #include "vfs_posix.h"
32
33 /*
34   compare two filename components. This is where the name mangling hook will go
35 */
36 static int component_compare(struct pvfs_state *pvfs, const char *comp, const char *name)
37 {
38         int ret;
39
40         ret = StrCaseCmp(comp, name);
41
42         if (ret != 0) {
43                 char *shortname = pvfs_short_name_component(pvfs, name);
44                 if (shortname) {
45                         ret = StrCaseCmp(comp, shortname);
46                         talloc_free(shortname);
47                 }
48         }
49
50         return ret;
51 }
52
53 /*
54   search for a filename in a case insensitive fashion
55
56   TODO: add a cache for previously resolved case-insensitive names
57   TODO: add mangled name support
58 */
59 static NTSTATUS pvfs_case_search(struct pvfs_state *pvfs, struct pvfs_filename *name)
60 {
61         /* break into a series of components */
62         int num_components;
63         char **components;
64         char *p, *partial_name;
65         int i;
66
67         /* break up the full name info pathname components */
68         num_components=2;
69         p = name->full_name + strlen(pvfs->base_directory) + 1;
70
71         for (;*p;p++) {
72                 if (*p == '/') {
73                         num_components++;
74                 }
75         }
76
77         components = talloc_array_p(name, char *, num_components);
78         p = name->full_name + strlen(pvfs->base_directory);
79         *p++ = 0;
80
81         components[0] = name->full_name;
82
83         for (i=1;i<num_components;i++) {
84                 components[i] = p;
85                 p = strchr(p, '/');
86                 if (p) *p++ = 0;
87                 if (pvfs_is_reserved_name(pvfs, components[i])) {
88                         return NT_STATUS_ACCESS_DENIED;
89                 }
90         }
91
92         partial_name = talloc_strdup(name, components[0]);
93         if (!partial_name) {
94                 return NT_STATUS_NO_MEMORY;
95         }
96
97         /* for each component, check if it exists as-is, and if not then
98            do a directory scan */
99         for (i=1;i<num_components;i++) {
100                 char *test_name;
101                 DIR *dir;
102                 struct dirent *de;
103                 char *long_component;
104
105                 /* possibly remap from the short name cache */
106                 long_component = pvfs_mangled_lookup(pvfs, name, components[i]);
107                 if (long_component) {
108                         components[i] = long_component;
109                 }
110
111                 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
112                 if (!test_name) {
113                         return NT_STATUS_NO_MEMORY;
114                 }
115
116                 /* check if this component exists as-is */
117                 if (stat(test_name, &name->st) == 0) {
118                         if (i<num_components-1 && !S_ISDIR(name->st.st_mode)) {
119                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
120                         }
121                         talloc_free(partial_name);
122                         partial_name = test_name;
123                         if (i == num_components - 1) {
124                                 name->exists = True;
125                         }
126                         continue;
127                 }
128
129                 /* the filesystem might be case insensitive, in which
130                    case a search is pointless unless the name is
131                    mangled */
132                 if ((pvfs->flags & PVFS_FLAG_CI_FILESYSTEM) &&
133                     !pvfs_is_mangled_component(pvfs, components[i])) {
134                         if (i < num_components-1) {
135                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
136                         }
137                         partial_name = test_name;
138                         continue;
139                 }
140                 
141                 dir = opendir(partial_name);
142                 if (!dir) {
143                         return pvfs_map_errno(pvfs, errno);
144                 }
145
146                 while ((de = readdir(dir))) {
147                         if (component_compare(pvfs, components[i], de->d_name) == 0) {
148                                 break;
149                         }
150                 }
151
152                 if (!de) {
153                         if (i < num_components-1) {
154                                 closedir(dir);
155                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
156                         }
157                 } else {
158                         components[i] = talloc_strdup(name, de->d_name);
159                 }
160                 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
161                 talloc_free(partial_name);
162                 partial_name = test_name;
163
164                 closedir(dir);
165         }
166
167         if (!name->exists) {
168                 if (stat(partial_name, &name->st) == 0) {
169                         name->exists = True;
170                 }
171         }
172
173         talloc_free(name->full_name);
174         name->full_name = partial_name;
175
176         if (name->exists) {
177                 return pvfs_fill_dos_info(pvfs, name);
178         }
179
180         return NT_STATUS_OK;
181 }
182
183
184 /*
185   convert a CIFS pathname to a unix pathname. Note that this does NOT
186   take into account case insensitivity, and in fact does not access
187   the filesystem at all. It is merely a reformatting and charset
188   checking routine.
189
190   errors are returned if the filename is illegal given the flags
191 */
192 static NTSTATUS pvfs_unix_path(struct pvfs_state *pvfs, const char *cifs_name,
193                                uint_t flags, struct pvfs_filename *name)
194 {
195         char *ret, *p, *p_start;
196
197         name->original_name = talloc_strdup(name, cifs_name);
198         name->stream_name = NULL;
199         name->has_wildcard = False;
200
201         while (*cifs_name == '\\') {
202                 cifs_name++;
203         }
204
205         if (*cifs_name == 0) {
206                 name->full_name = talloc_asprintf(name, "%s/.", pvfs->base_directory);
207                 if (name->full_name == NULL) {
208                         return NT_STATUS_NO_MEMORY;
209                 }
210                 return NT_STATUS_OK;
211         }
212
213         ret = talloc_asprintf(name, "%s/%s", pvfs->base_directory, cifs_name);
214         if (ret == NULL) {
215                 return NT_STATUS_NO_MEMORY;
216         }
217
218         p = ret + strlen(pvfs->base_directory) + 1;
219
220         /* now do an in-place conversion of '\' to '/', checking
221            for legal characters */
222         p_start = p;
223
224         while (*p) {
225                 size_t c_size;
226                 codepoint_t c = next_codepoint(p, &c_size);
227                 switch (c) {
228                 case '\\':
229                         if (name->has_wildcard) {
230                                 /* wildcards are only allowed in the last part
231                                    of a name */
232                                 return NT_STATUS_ILLEGAL_CHARACTER;
233                         }
234                         if (p > p_start && p[1] == 0) {
235                                 *p = 0;
236                         } else {
237                                 *p = '/';
238                         }
239                         break;
240                 case ':':
241                         if (!(flags & PVFS_RESOLVE_STREAMS)) {
242                                 return NT_STATUS_ILLEGAL_CHARACTER;
243                         }
244                         name->stream_name = talloc_strdup(name, p+1);
245                         if (name->stream_name == NULL) {
246                                 return NT_STATUS_NO_MEMORY;
247                         }
248                         *p-- = 0;
249                         break;
250                 case '*':
251                 case '>':
252                 case '<':
253                 case '?':
254                 case '"':
255                         if (flags & PVFS_RESOLVE_NO_WILDCARD) {
256                                 return NT_STATUS_OBJECT_NAME_INVALID;
257                         }
258                         name->has_wildcard = True;
259                         break;
260                 case '/':
261                 case '|':
262                         return NT_STATUS_ILLEGAL_CHARACTER;
263                 case '.':
264                         /* see if it is definately a .. or
265                            . component. If it is then fail here, and
266                            let the next layer up try again after
267                            pvfs_reduce_name() if it wants to. This is
268                            much more efficient on average than always
269                            scanning for these separately */
270                         if (p[1] == '.' && 
271                             (p[2] == 0 || p[2] == '\\') &&
272                             (p == p_start || p[-1] == '/')) {
273                                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
274                         }
275                         if ((p[1] == 0 || p[1] == '\\') &&
276                             (p == p_start || p[-1] == '/')) {
277                                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
278                         }
279                         break;
280                 }
281
282                 p += c_size;
283         }
284
285         name->full_name = ret;
286
287         return NT_STATUS_OK;
288 }
289
290
291 /*
292   reduce a name that contains .. components or repeated \ separators
293   return NULL if it can't be reduced
294 */
295 static NTSTATUS pvfs_reduce_name(TALLOC_CTX *mem_ctx, const char **fname, uint_t flags)
296 {
297         codepoint_t c;
298         size_t c_size, len;
299         int i, num_components, err_count;
300         char **components;
301         char *p, *s, *ret;
302
303         s = talloc_strdup(mem_ctx, *fname);
304         if (s == NULL) return NT_STATUS_NO_MEMORY;
305
306         for (num_components=1, p=s; *p; p += c_size) {
307                 c = next_codepoint(p, &c_size);
308                 if (c == '\\') num_components++;
309         }
310
311         components = talloc_array_p(s, char *, num_components+1);
312         if (components == NULL) {
313                 talloc_free(s);
314                 return NT_STATUS_NO_MEMORY;
315         }
316
317         components[0] = s;
318         for (i=0, p=s; *p; p += c_size) {
319                 c = next_codepoint(p, &c_size);
320                 if (c == '\\') {
321                         *p = 0;
322                         components[++i] = p+1;
323                 }
324         }
325         components[i+1] = NULL;
326
327         /*
328           rather bizarre!
329
330           '.' components are not allowed, but the rules for what error
331           code to give don't seem to make sense. This is a close
332           approximation.
333         */
334         for (err_count=i=0;components[i];i++) {
335                 if (strcmp(components[i], "") == 0) {
336                         continue;
337                 }
338                 if (strcmp(components[i], ".") == 0 || err_count) {
339                         err_count++;
340                 }
341         }
342         if (err_count) {
343                 if (!(flags & PVFS_RESOLVE_NO_WILDCARD)) err_count--;
344
345                 if (err_count==1) {
346                         return NT_STATUS_OBJECT_NAME_INVALID;
347                 } else {
348                         return NT_STATUS_OBJECT_PATH_NOT_FOUND;
349                 }
350         }
351
352         /* remove any null components */
353         for (i=0;components[i];i++) {
354                 if (strcmp(components[i], "") == 0) {
355                         memmove(&components[i], &components[i+1], 
356                                 sizeof(char *)*(num_components-i));
357                         i--;
358                 }
359                 if (strcmp(components[i], "..") == 0) {
360                         if (i < 1) return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
361                         memmove(&components[i-1], &components[i+1], 
362                                 sizeof(char *)*(num_components-(i+1)));
363                         i -= 2;
364                 }
365         }
366
367         if (components[0] == NULL) {
368                 talloc_free(s);
369                 *fname = talloc_strdup(mem_ctx, "\\");
370                 return NT_STATUS_OK;
371         }
372
373         for (len=i=0;components[i];i++) {
374                 len += strlen(components[i]) + 1;
375         }
376
377         /* rebuild the name */
378         ret = talloc(mem_ctx, len+1);
379         if (ret == NULL) {
380                 talloc_free(s);
381                 return NT_STATUS_NO_MEMORY;
382         }
383
384         for (len=0,i=0;components[i];i++) {
385                 size_t len1 = strlen(components[i]);
386                 ret[len] = '\\';
387                 memcpy(ret+len+1, components[i], len1);
388                 len += len1 + 1;
389         }       
390         ret[len] = 0;
391
392         talloc_free(s);
393
394         *fname = ret;
395         
396         return NT_STATUS_OK;
397 }
398
399
400 /*
401   resolve a name from relative client format to a struct pvfs_filename
402   the memory for the filename is made as a talloc child of 'name'
403
404   flags include:
405      PVFS_RESOLVE_NO_WILDCARD = wildcards are considered illegal characters
406      PVFS_RESOLVE_STREAMS     = stream names are allowed
407
408      TODO: ../ collapsing, and outside share checking
409 */
410 NTSTATUS pvfs_resolve_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
411                            const char *cifs_name,
412                            uint_t flags, struct pvfs_filename **name)
413 {
414         NTSTATUS status;
415
416         *name = talloc_p(mem_ctx, struct pvfs_filename);
417         if (*name == NULL) {
418                 return NT_STATUS_NO_MEMORY;
419         }
420
421         (*name)->exists = False;
422
423         /* do the basic conversion to a unix formatted path,
424            also checking for allowable characters */
425         status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
426
427         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD)) {
428                 /* it might contain .. components which need to be reduced */
429                 status = pvfs_reduce_name(*name, &cifs_name, flags);
430                 if (!NT_STATUS_IS_OK(status)) {
431                         return status;
432                 }
433                 status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
434         }
435
436         if (!NT_STATUS_IS_OK(status)) {
437                 return status;
438         }
439
440         /* if it has a wildcard then no point doing a stat() */
441         if ((*name)->has_wildcard) {
442                 return NT_STATUS_OK;
443         }
444
445         /* if we can stat() the full name now then we are done */
446         if (stat((*name)->full_name, &(*name)->st) == 0) {
447                 (*name)->exists = True;
448                 return pvfs_fill_dos_info(pvfs, *name);
449         }
450
451         /* search for a matching filename */
452         status = pvfs_case_search(pvfs, *name);
453
454         return status;
455 }
456
457
458 /*
459   do a partial resolve, returning a pvfs_filename structure given a
460   base path and a relative component. It is an error if the file does
461   not exist. No case-insensitive matching is done.
462
463   this is used in places like directory searching where we need a pvfs_filename
464   to pass to a function, but already know the unix base directory and component
465 */
466 NTSTATUS pvfs_resolve_partial(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
467                               const char *unix_dir, const char *fname,
468                               struct pvfs_filename **name)
469 {
470         NTSTATUS status;
471
472         *name = talloc_p(mem_ctx, struct pvfs_filename);
473         if (*name == NULL) {
474                 return NT_STATUS_NO_MEMORY;
475         }
476
477         (*name)->full_name = talloc_asprintf(*name, "%s/%s", unix_dir, fname);
478         if ((*name)->full_name == NULL) {
479                 return NT_STATUS_NO_MEMORY;
480         }
481
482         if (stat((*name)->full_name, &(*name)->st) == -1) {
483                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
484         }
485
486         (*name)->exists = True;
487         (*name)->has_wildcard = False;
488         (*name)->original_name = talloc_strdup(*name, fname);
489         (*name)->stream_name = NULL;
490
491         status = pvfs_fill_dos_info(pvfs, *name);
492
493         return status;
494 }
495
496
497 /*
498   fill in the pvfs_filename info for an open file, given the current
499   info for a (possibly) non-open file. This is used by places that need
500   to update the pvfs_filename stat information, and by pvfs_open()
501 */
502 NTSTATUS pvfs_resolve_name_fd(struct pvfs_state *pvfs, int fd,
503                               struct pvfs_filename *name)
504 {
505         dev_t device;
506         ino_t inode;
507
508         if (name->exists) {
509                 device = name->st.st_dev;
510                 inode = name->st.st_ino;
511         }
512
513         if (name->dos.attrib & FILE_ATTRIBUTE_DIRECTORY) {
514                 if (stat(name->full_name, &name->st) == -1) {
515                         return NT_STATUS_INVALID_HANDLE;
516                 }
517         } else {
518                 if (fstat(fd, &name->st) == -1) {
519                         return NT_STATUS_INVALID_HANDLE;
520                 }
521         }
522
523         if (name->exists &&
524             (device != name->st.st_dev || inode != name->st.st_ino)) {
525                 /* the file we are looking at has changed! this could
526                  be someone trying to exploit a race
527                  condition. Certainly we don't want to continue
528                  operating on this file */
529                 DEBUG(0,("pvfs: WARNING: file '%s' changed during resole - failing\n",
530                          name->full_name));
531                 return NT_STATUS_UNEXPECTED_IO_ERROR;
532         }
533
534         name->exists = True;
535         
536         return pvfs_fill_dos_info(pvfs, name);
537 }