Merge branch 'v4-0-test' of git://git.samba.org/samba into 4-0-local
[samba.git] / source4 / 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 3 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, see <http://www.gnu.org/licenses/>.
20 */
21
22 /*
23   this is the core code for converting a filename from the format as
24   given by a client to a posix filename, including any case-matching
25   required, and checks for legal characters
26 */
27
28
29 #include "includes.h"
30 #include "vfs_posix.h"
31 #include "system/dir.h"
32 #include "param/param.h"
33
34 /**
35   compare two filename components. This is where the name mangling hook will go
36 */
37 static int component_compare(struct pvfs_state *pvfs, const char *comp, const char *name)
38 {
39         int ret;
40
41         ret = strcasecmp_m(comp, name);
42
43         if (ret != 0) {
44                 char *shortname = pvfs_short_name_component(pvfs, name);
45                 if (shortname) {
46                         ret = strcasecmp_m(comp, shortname);
47                         talloc_free(shortname);
48                 }
49         }
50
51         return ret;
52 }
53
54 /*
55   search for a filename in a case insensitive fashion
56
57   TODO: add a cache for previously resolved case-insensitive names
58   TODO: add mangled name support
59 */
60 static NTSTATUS pvfs_case_search(struct pvfs_state *pvfs, struct pvfs_filename *name)
61 {
62         /* break into a series of components */
63         int num_components;
64         char **components;
65         char *p, *partial_name;
66         int i;
67
68         /* break up the full name info pathname components */
69         num_components=2;
70         p = name->full_name + strlen(pvfs->base_directory) + 1;
71
72         for (;*p;p++) {
73                 if (*p == '/') {
74                         num_components++;
75                 }
76         }
77
78         components = talloc_array(name, char *, num_components);
79         p = name->full_name + strlen(pvfs->base_directory);
80         *p++ = 0;
81
82         components[0] = name->full_name;
83
84         for (i=1;i<num_components;i++) {
85                 components[i] = p;
86                 p = strchr(p, '/');
87                 if (p) *p++ = 0;
88                 if (pvfs_is_reserved_name(pvfs, components[i])) {
89                         return NT_STATUS_ACCESS_DENIED;
90                 }
91         }
92
93         partial_name = talloc_strdup(name, components[0]);
94         if (!partial_name) {
95                 return NT_STATUS_NO_MEMORY;
96         }
97
98         /* for each component, check if it exists as-is, and if not then
99            do a directory scan */
100         for (i=1;i<num_components;i++) {
101                 char *test_name;
102                 DIR *dir;
103                 struct dirent *de;
104                 char *long_component;
105
106                 /* possibly remap from the short name cache */
107                 long_component = pvfs_mangled_lookup(pvfs, name, components[i]);
108                 if (long_component) {
109                         components[i] = long_component;
110                 }
111
112                 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
113                 if (!test_name) {
114                         return NT_STATUS_NO_MEMORY;
115                 }
116
117                 /* check if this component exists as-is */
118                 if (stat(test_name, &name->st) == 0) {
119                         if (i<num_components-1 && !S_ISDIR(name->st.st_mode)) {
120                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
121                         }
122                         talloc_free(partial_name);
123                         partial_name = test_name;
124                         if (i == num_components - 1) {
125                                 name->exists = true;
126                         }
127                         continue;
128                 }
129
130                 /* the filesystem might be case insensitive, in which
131                    case a search is pointless unless the name is
132                    mangled */
133                 if ((pvfs->flags & PVFS_FLAG_CI_FILESYSTEM) &&
134                     !pvfs_is_mangled_component(pvfs, components[i])) {
135                         if (i < num_components-1) {
136                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
137                         }
138                         partial_name = test_name;
139                         continue;
140                 }
141                 
142                 dir = opendir(partial_name);
143                 if (!dir) {
144                         return pvfs_map_errno(pvfs, errno);
145                 }
146
147                 while ((de = readdir(dir))) {
148                         if (component_compare(pvfs, components[i], de->d_name) == 0) {
149                                 break;
150                         }
151                 }
152
153                 if (!de) {
154                         if (i < num_components-1) {
155                                 closedir(dir);
156                                 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
157                         }
158                 } else {
159                         components[i] = talloc_strdup(name, de->d_name);
160                 }
161                 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
162                 talloc_free(partial_name);
163                 partial_name = test_name;
164
165                 closedir(dir);
166         }
167
168         if (!name->exists) {
169                 if (stat(partial_name, &name->st) == 0) {
170                         name->exists = true;
171                 }
172         }
173
174         talloc_free(name->full_name);
175         name->full_name = partial_name;
176
177         if (name->exists) {
178                 return pvfs_fill_dos_info(pvfs, name, -1);
179         }
180
181         return NT_STATUS_OK;
182 }
183
184 /*
185   parse a alternate data stream name
186 */
187 static NTSTATUS parse_stream_name(struct pvfs_filename *name, const char *s)
188 {
189         char *p;
190         name->stream_name = talloc_strdup(name, s+1);
191         if (name->stream_name == NULL) {
192                 return NT_STATUS_NO_MEMORY;
193         }
194         p = strchr_m(name->stream_name, ':');
195         if (p == NULL) {
196                 name->stream_id = pvfs_name_hash(name->stream_name, 
197                                                  strlen(name->stream_name));
198                 return NT_STATUS_OK;
199         }
200         if (strcasecmp_m(p, ":$DATA") != 0) {
201                 return NT_STATUS_OBJECT_NAME_INVALID;
202         }
203         *p = 0;
204         if (strcmp(name->stream_name, "") == 0) {
205                 name->stream_name = NULL;
206                 name->stream_id = 0;
207         } else {
208                 name->stream_id = pvfs_name_hash(name->stream_name, 
209                                                  strlen(name->stream_name));
210         }
211                                                  
212         return NT_STATUS_OK;    
213 }
214
215
216 /*
217   convert a CIFS pathname to a unix pathname. Note that this does NOT
218   take into account case insensitivity, and in fact does not access
219   the filesystem at all. It is merely a reformatting and charset
220   checking routine.
221
222   errors are returned if the filename is illegal given the flags
223 */
224 static NTSTATUS pvfs_unix_path(struct pvfs_state *pvfs, const char *cifs_name,
225                                uint_t flags, struct pvfs_filename *name)
226 {
227         char *ret, *p, *p_start;
228         NTSTATUS status;
229
230         name->original_name = talloc_strdup(name, cifs_name);
231         name->stream_name = NULL;
232         name->stream_id = 0;
233         name->has_wildcard = false;
234
235         while (*cifs_name == '\\') {
236                 cifs_name++;
237         }
238
239         if (*cifs_name == 0) {
240                 name->full_name = talloc_asprintf(name, "%s/.", pvfs->base_directory);
241                 if (name->full_name == NULL) {
242                         return NT_STATUS_NO_MEMORY;
243                 }
244                 return NT_STATUS_OK;
245         }
246
247         ret = talloc_asprintf(name, "%s/%s", pvfs->base_directory, cifs_name);
248         if (ret == NULL) {
249                 return NT_STATUS_NO_MEMORY;
250         }
251
252         p = ret + strlen(pvfs->base_directory) + 1;
253
254         /* now do an in-place conversion of '\' to '/', checking
255            for legal characters */
256         p_start = p;
257
258         while (*p) {
259                 size_t c_size;
260                 codepoint_t c = next_codepoint(lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx), p, &c_size);
261                 switch (c) {
262                 case '\\':
263                         if (name->has_wildcard) {
264                                 /* wildcards are only allowed in the last part
265                                    of a name */
266                                 return NT_STATUS_ILLEGAL_CHARACTER;
267                         }
268                         if (p > p_start && p[1] == 0) {
269                                 *p = 0;
270                         } else {
271                                 *p = '/';
272                         }
273                         break;
274                 case ':':
275                         if (!(flags & PVFS_RESOLVE_STREAMS)) {
276                                 return NT_STATUS_ILLEGAL_CHARACTER;
277                         }
278                         if (name->has_wildcard) {
279                                 return NT_STATUS_ILLEGAL_CHARACTER;
280                         }
281                         status = parse_stream_name(name, p);
282                         if (!NT_STATUS_IS_OK(status)) {
283                                 return status;
284                         }
285                         *p-- = 0;
286                         break;
287                 case '*':
288                 case '>':
289                 case '<':
290                 case '?':
291                 case '"':
292                         if (!(flags & PVFS_RESOLVE_WILDCARD)) {
293                                 return NT_STATUS_OBJECT_NAME_INVALID;
294                         }
295                         name->has_wildcard = true;
296                         break;
297                 case '/':
298                 case '|':
299                         return NT_STATUS_ILLEGAL_CHARACTER;
300                 case '.':
301                         /* see if it is definately a .. or
302                            . component. If it is then fail here, and
303                            let the next layer up try again after
304                            pvfs_reduce_name() if it wants to. This is
305                            much more efficient on average than always
306                            scanning for these separately */
307                         if (p[1] == '.' && 
308                             (p[2] == 0 || p[2] == '\\') &&
309                             (p == p_start || p[-1] == '/')) {
310                                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
311                         }
312                         if ((p[1] == 0 || p[1] == '\\') &&
313                             (p == p_start || p[-1] == '/')) {
314                                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
315                         }
316                         break;
317                 }
318
319                 p += c_size;
320         }
321
322         name->full_name = ret;
323
324         return NT_STATUS_OK;
325 }
326
327
328 /*
329   reduce a name that contains .. components or repeated \ separators
330   return NULL if it can't be reduced
331 */
332 static NTSTATUS pvfs_reduce_name(TALLOC_CTX *mem_ctx, 
333                                  struct smb_iconv_convenience *iconv_convenience, 
334                                  const char **fname, uint_t flags)
335 {
336         codepoint_t c;
337         size_t c_size, len;
338         int i, num_components, err_count;
339         char **components;
340         char *p, *s, *ret;
341
342         s = talloc_strdup(mem_ctx, *fname);
343         if (s == NULL) return NT_STATUS_NO_MEMORY;
344
345         for (num_components=1, p=s; *p; p += c_size) {
346                 c = next_codepoint(iconv_convenience, p, &c_size);
347                 if (c == '\\') num_components++;
348         }
349
350         components = talloc_array(s, char *, num_components+1);
351         if (components == NULL) {
352                 talloc_free(s);
353                 return NT_STATUS_NO_MEMORY;
354         }
355
356         components[0] = s;
357         for (i=0, p=s; *p; p += c_size) {
358                 c = next_codepoint(iconv_convenience, p, &c_size);
359                 if (c == '\\') {
360                         *p = 0;
361                         components[++i] = p+1;
362                 }
363         }
364         components[i+1] = NULL;
365
366         /*
367           rather bizarre!
368
369           '.' components are not allowed, but the rules for what error
370           code to give don't seem to make sense. This is a close
371           approximation.
372         */
373         for (err_count=i=0;components[i];i++) {
374                 if (strcmp(components[i], "") == 0) {
375                         continue;
376                 }
377                 if (ISDOT(components[i]) || err_count) {
378                         err_count++;
379                 }
380         }
381         if (err_count) {
382                 if (flags & PVFS_RESOLVE_WILDCARD) err_count--;
383
384                 if (err_count==1) {
385                         return NT_STATUS_OBJECT_NAME_INVALID;
386                 } else {
387                         return NT_STATUS_OBJECT_PATH_NOT_FOUND;
388                 }
389         }
390
391         /* remove any null components */
392         for (i=0;components[i];i++) {
393                 if (strcmp(components[i], "") == 0) {
394                         memmove(&components[i], &components[i+1], 
395                                 sizeof(char *)*(num_components-i));
396                         i--;
397                         continue;
398                 }
399                 if (ISDOTDOT(components[i])) {
400                         if (i < 1) return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
401                         memmove(&components[i-1], &components[i+1], 
402                                 sizeof(char *)*(num_components-(i+1)));
403                         i -= 2;
404                         continue;
405                 }
406         }
407
408         if (components[0] == NULL) {
409                 talloc_free(s);
410                 *fname = talloc_strdup(mem_ctx, "\\");
411                 return NT_STATUS_OK;
412         }
413
414         for (len=i=0;components[i];i++) {
415                 len += strlen(components[i]) + 1;
416         }
417
418         /* rebuild the name */
419         ret = talloc_size(mem_ctx, len+1);
420         if (ret == NULL) {
421                 talloc_free(s);
422                 return NT_STATUS_NO_MEMORY;
423         }
424
425         for (len=0,i=0;components[i];i++) {
426                 size_t len1 = strlen(components[i]);
427                 ret[len] = '\\';
428                 memcpy(ret+len+1, components[i], len1);
429                 len += len1 + 1;
430         }       
431         ret[len] = 0;
432
433         talloc_free(s);
434
435         *fname = ret;
436         
437         return NT_STATUS_OK;
438 }
439
440
441 /*
442   resolve a name from relative client format to a struct pvfs_filename
443   the memory for the filename is made as a talloc child of 'name'
444
445   flags include:
446      PVFS_RESOLVE_NO_WILDCARD = wildcards are considered illegal characters
447      PVFS_RESOLVE_STREAMS     = stream names are allowed
448
449      TODO: ../ collapsing, and outside share checking
450 */
451 NTSTATUS pvfs_resolve_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
452                            const char *cifs_name,
453                            uint_t flags, struct pvfs_filename **name)
454 {
455         NTSTATUS status;
456
457         *name = talloc(mem_ctx, struct pvfs_filename);
458         if (*name == NULL) {
459                 return NT_STATUS_NO_MEMORY;
460         }
461
462         (*name)->exists = false;
463         (*name)->stream_exists = false;
464
465         if (!(pvfs->fs_attribs & FS_ATTR_NAMED_STREAMS)) {
466                 flags &= ~PVFS_RESOLVE_STREAMS;
467         }
468
469         /* do the basic conversion to a unix formatted path,
470            also checking for allowable characters */
471         status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
472
473         if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD)) {
474                 /* it might contain .. components which need to be reduced */
475                 status = pvfs_reduce_name(*name, lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx), &cifs_name, flags);
476                 if (!NT_STATUS_IS_OK(status)) {
477                         return status;
478                 }
479                 status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
480         }
481
482         if (!NT_STATUS_IS_OK(status)) {
483                 return status;
484         }
485
486         /* if it has a wildcard then no point doing a stat() of the
487            full name. Instead We need check if the directory exists 
488          */
489         if ((*name)->has_wildcard) {
490                 const char *p;
491                 char *dir_name, *saved_name;
492                 p = strrchr((*name)->full_name, '/');
493                 if (p == NULL) {
494                         /* root directory wildcard is OK */
495                         return NT_STATUS_OK;
496                 }
497                 dir_name = talloc_strndup(*name, (*name)->full_name, (p-(*name)->full_name));
498                 if (stat(dir_name, &(*name)->st) == 0) {
499                         talloc_free(dir_name);
500                         return NT_STATUS_OK;
501                 }
502                 /* we need to search for a matching name */
503                 saved_name = (*name)->full_name;
504                 (*name)->full_name = dir_name;
505                 status = pvfs_case_search(pvfs, *name);
506                 if (!NT_STATUS_IS_OK(status)) {
507                         /* the directory doesn't exist */
508                         (*name)->full_name = saved_name;
509                         return status;
510                 }
511                 /* it does exist, but might need a case change */
512                 if (dir_name != (*name)->full_name) {
513                         (*name)->full_name = talloc_asprintf(*name, "%s%s",
514                                                              (*name)->full_name, p);
515                         NT_STATUS_HAVE_NO_MEMORY((*name)->full_name);
516                 } else {
517                         (*name)->full_name = saved_name;
518                         talloc_free(dir_name);
519                 }
520                 return NT_STATUS_OK;
521         }
522
523         /* if we can stat() the full name now then we are done */
524         if (stat((*name)->full_name, &(*name)->st) == 0) {
525                 (*name)->exists = true;
526                 return pvfs_fill_dos_info(pvfs, *name, -1);
527         }
528
529         /* search for a matching filename */
530         status = pvfs_case_search(pvfs, *name);
531
532         return status;
533 }
534
535
536 /*
537   do a partial resolve, returning a pvfs_filename structure given a
538   base path and a relative component. It is an error if the file does
539   not exist. No case-insensitive matching is done.
540
541   this is used in places like directory searching where we need a pvfs_filename
542   to pass to a function, but already know the unix base directory and component
543 */
544 NTSTATUS pvfs_resolve_partial(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
545                               const char *unix_dir, const char *fname,
546                               struct pvfs_filename **name)
547 {
548         NTSTATUS status;
549
550         *name = talloc(mem_ctx, struct pvfs_filename);
551         if (*name == NULL) {
552                 return NT_STATUS_NO_MEMORY;
553         }
554
555         (*name)->full_name = talloc_asprintf(*name, "%s/%s", unix_dir, fname);
556         if ((*name)->full_name == NULL) {
557                 return NT_STATUS_NO_MEMORY;
558         }
559
560         if (stat((*name)->full_name, &(*name)->st) == -1) {
561                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
562         }
563
564         (*name)->exists = true;
565         (*name)->stream_exists = true;
566         (*name)->has_wildcard = false;
567         (*name)->original_name = talloc_strdup(*name, fname);
568         (*name)->stream_name = NULL;
569         (*name)->stream_id = 0;
570
571         status = pvfs_fill_dos_info(pvfs, *name, -1);
572
573         return status;
574 }
575
576
577 /*
578   fill in the pvfs_filename info for an open file, given the current
579   info for a (possibly) non-open file. This is used by places that need
580   to update the pvfs_filename stat information, and by pvfs_open()
581 */
582 NTSTATUS pvfs_resolve_name_fd(struct pvfs_state *pvfs, int fd,
583                               struct pvfs_filename *name)
584 {
585         dev_t device = (dev_t)0;
586         ino_t inode = 0;
587
588         if (name->exists) {
589                 device = name->st.st_dev;
590                 inode = name->st.st_ino;
591         }
592
593         if (fd == -1) {
594                 if (stat(name->full_name, &name->st) == -1) {
595                         return NT_STATUS_INVALID_HANDLE;
596                 }
597         } else {
598                 if (fstat(fd, &name->st) == -1) {
599                         return NT_STATUS_INVALID_HANDLE;
600                 }
601         }
602
603         if (name->exists &&
604             (device != name->st.st_dev || inode != name->st.st_ino)) {
605                 /* the file we are looking at has changed! this could
606                  be someone trying to exploit a race
607                  condition. Certainly we don't want to continue
608                  operating on this file */
609                 DEBUG(0,("pvfs: WARNING: file '%s' changed during resolve - failing\n",
610                          name->full_name));
611                 return NT_STATUS_UNEXPECTED_IO_ERROR;
612         }
613
614         name->exists = true;
615         
616         return pvfs_fill_dos_info(pvfs, name, fd);
617 }
618
619
620 /*
621   resolve the parent of a given name
622 */
623 NTSTATUS pvfs_resolve_parent(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
624                              const struct pvfs_filename *child,
625                              struct pvfs_filename **name)
626 {
627         NTSTATUS status;
628         char *p;
629
630         *name = talloc(mem_ctx, struct pvfs_filename);
631         if (*name == NULL) {
632                 return NT_STATUS_NO_MEMORY;
633         }
634
635         (*name)->full_name = talloc_strdup(*name, child->full_name);
636         if ((*name)->full_name == NULL) {
637                 return NT_STATUS_NO_MEMORY;
638         }
639
640         p = strrchr_m((*name)->full_name, '/');
641         if (p == NULL) {
642                 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
643         }
644
645         /* this handles the root directory */
646         if (p == (*name)->full_name) {
647                 p[1] = 0;
648         } else {
649                 p[0] = 0;
650         }
651
652         if (stat((*name)->full_name, &(*name)->st) == -1) {
653                 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
654         }
655
656         (*name)->exists = true;
657         (*name)->stream_exists = true;
658         (*name)->has_wildcard = false;
659         /* we can't get the correct 'original_name', but for the purposes
660            of this call this is close enough */
661         (*name)->original_name = talloc_reference(*name, child->original_name);
662         (*name)->stream_name = NULL;
663         (*name)->stream_id = 0;
664
665         status = pvfs_fill_dos_info(pvfs, *name, -1);
666
667         return status;
668 }