2 Unix SMB/CIFS implementation.
4 POSIX NTVFS backend - filename resolution
6 Copyright (C) Andrew Tridgell 2004
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.
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.
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/>.
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
30 #include "vfs_posix.h"
31 #include "system/dir.h"
32 #include "param/param.h"
35 compare two filename components. This is where the name mangling hook will go
37 static int component_compare(struct pvfs_state *pvfs, const char *comp, const char *name)
41 ret = strcasecmp_m(comp, name);
44 char *shortname = pvfs_short_name_component(pvfs, name);
46 ret = strcasecmp_m(comp, shortname);
47 talloc_free(shortname);
55 search for a filename in a case insensitive fashion
57 TODO: add a cache for previously resolved case-insensitive names
58 TODO: add mangled name support
60 static NTSTATUS pvfs_case_search(struct pvfs_state *pvfs, struct pvfs_filename *name)
62 /* break into a series of components */
65 char *p, *partial_name;
68 /* break up the full name info pathname components */
70 p = name->full_name + strlen(pvfs->base_directory) + 1;
78 components = talloc_array(name, char *, num_components);
79 p = name->full_name + strlen(pvfs->base_directory);
82 components[0] = name->full_name;
84 for (i=1;i<num_components;i++) {
88 if (pvfs_is_reserved_name(pvfs, components[i])) {
89 return NT_STATUS_ACCESS_DENIED;
93 partial_name = talloc_strdup(name, components[0]);
95 return NT_STATUS_NO_MEMORY;
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++) {
104 char *long_component;
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;
112 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
114 return NT_STATUS_NO_MEMORY;
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;
122 talloc_free(partial_name);
123 partial_name = test_name;
124 if (i == num_components - 1) {
130 /* the filesystem might be case insensitive, in which
131 case a search is pointless unless the name is
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;
138 partial_name = test_name;
142 dir = opendir(partial_name);
144 return pvfs_map_errno(pvfs, errno);
147 while ((de = readdir(dir))) {
148 if (component_compare(pvfs, components[i], de->d_name) == 0) {
154 if (i < num_components-1) {
156 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
159 components[i] = talloc_strdup(name, de->d_name);
161 test_name = talloc_asprintf(name, "%s/%s", partial_name, components[i]);
162 talloc_free(partial_name);
163 partial_name = test_name;
169 if (stat(partial_name, &name->st) == 0) {
174 talloc_free(name->full_name);
175 name->full_name = partial_name;
178 return pvfs_fill_dos_info(pvfs, name, -1);
185 parse a alternate data stream name
187 static NTSTATUS parse_stream_name(struct pvfs_filename *name, const char *s)
190 name->stream_name = talloc_strdup(name, s+1);
191 if (name->stream_name == NULL) {
192 return NT_STATUS_NO_MEMORY;
194 p = strchr_m(name->stream_name, ':');
196 name->stream_id = pvfs_name_hash(name->stream_name,
197 strlen(name->stream_name));
200 if (strcasecmp_m(p, ":$DATA") != 0) {
201 return NT_STATUS_OBJECT_NAME_INVALID;
204 if (strcmp(name->stream_name, "") == 0) {
205 name->stream_name = NULL;
208 name->stream_id = pvfs_name_hash(name->stream_name,
209 strlen(name->stream_name));
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
222 errors are returned if the filename is illegal given the flags
224 static NTSTATUS pvfs_unix_path(struct pvfs_state *pvfs, const char *cifs_name,
225 uint_t flags, struct pvfs_filename *name)
227 char *ret, *p, *p_start;
230 name->original_name = talloc_strdup(name, cifs_name);
231 name->stream_name = NULL;
233 name->has_wildcard = false;
235 while (*cifs_name == '\\') {
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;
247 ret = talloc_asprintf(name, "%s/%s", pvfs->base_directory, cifs_name);
249 return NT_STATUS_NO_MEMORY;
252 p = ret + strlen(pvfs->base_directory) + 1;
254 /* now do an in-place conversion of '\' to '/', checking
255 for legal characters */
260 codepoint_t c = next_codepoint(lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx), p, &c_size);
263 if (name->has_wildcard) {
264 /* wildcards are only allowed in the last part
266 return NT_STATUS_ILLEGAL_CHARACTER;
268 if (p > p_start && (p[1] == '\\' || p[1] == '\0')) {
269 /* see if it is definately a "\\" or
270 * a trailing "\". If it is then fail here,
271 * and let the next layer up try again after
272 * pvfs_reduce_name() if it wants to. This is
273 * much more efficient on average than always
274 * scanning for these separately
276 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
282 if (!(flags & PVFS_RESOLVE_STREAMS)) {
283 return NT_STATUS_ILLEGAL_CHARACTER;
285 if (name->has_wildcard) {
286 return NT_STATUS_ILLEGAL_CHARACTER;
288 status = parse_stream_name(name, p);
289 if (!NT_STATUS_IS_OK(status)) {
299 if (!(flags & PVFS_RESOLVE_WILDCARD)) {
300 return NT_STATUS_OBJECT_NAME_INVALID;
302 name->has_wildcard = true;
306 return NT_STATUS_ILLEGAL_CHARACTER;
308 /* see if it is definately a .. or
309 . component. If it is then fail here, and
310 let the next layer up try again after
311 pvfs_reduce_name() if it wants to. This is
312 much more efficient on average than always
313 scanning for these separately */
315 (p[2] == 0 || p[2] == '\\') &&
316 (p == p_start || p[-1] == '/')) {
317 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
319 if ((p[1] == 0 || p[1] == '\\') &&
320 (p == p_start || p[-1] == '/')) {
321 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
329 name->full_name = ret;
336 reduce a name that contains .. components or repeated \ separators
337 return NULL if it can't be reduced
339 static NTSTATUS pvfs_reduce_name(TALLOC_CTX *mem_ctx,
340 struct smb_iconv_convenience *iconv_convenience,
341 const char **fname, uint_t flags)
345 int i, num_components, err_count;
349 s = talloc_strdup(mem_ctx, *fname);
350 if (s == NULL) return NT_STATUS_NO_MEMORY;
352 for (num_components=1, p=s; *p; p += c_size) {
353 c = next_codepoint(iconv_convenience, p, &c_size);
354 if (c == '\\') num_components++;
357 components = talloc_array(s, char *, num_components+1);
358 if (components == NULL) {
360 return NT_STATUS_NO_MEMORY;
364 for (i=0, p=s; *p; p += c_size) {
365 c = next_codepoint(iconv_convenience, p, &c_size);
368 components[++i] = p+1;
371 components[i+1] = NULL;
376 '.' components are not allowed, but the rules for what error
377 code to give don't seem to make sense. This is a close
380 for (err_count=i=0;components[i];i++) {
381 if (strcmp(components[i], "") == 0) {
384 if (ISDOT(components[i]) || err_count) {
389 if (flags & PVFS_RESOLVE_WILDCARD) err_count--;
392 return NT_STATUS_OBJECT_NAME_INVALID;
394 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
398 /* remove any null components */
399 for (i=0;components[i];i++) {
400 if (strcmp(components[i], "") == 0) {
401 memmove(&components[i], &components[i+1],
402 sizeof(char *)*(num_components-i));
406 if (ISDOTDOT(components[i])) {
407 if (i < 1) return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
408 memmove(&components[i-1], &components[i+1],
409 sizeof(char *)*(num_components-i));
415 if (components[0] == NULL) {
417 *fname = talloc_strdup(mem_ctx, "\\");
421 for (len=i=0;components[i];i++) {
422 len += strlen(components[i]) + 1;
425 /* rebuild the name */
426 ret = talloc_size(mem_ctx, len+1);
429 return NT_STATUS_NO_MEMORY;
432 for (len=0,i=0;components[i];i++) {
433 size_t len1 = strlen(components[i]);
435 memcpy(ret+len+1, components[i], len1);
449 resolve a name from relative client format to a struct pvfs_filename
450 the memory for the filename is made as a talloc child of 'name'
453 PVFS_RESOLVE_NO_WILDCARD = wildcards are considered illegal characters
454 PVFS_RESOLVE_STREAMS = stream names are allowed
456 TODO: ../ collapsing, and outside share checking
458 NTSTATUS pvfs_resolve_name(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
459 const char *cifs_name,
460 uint_t flags, struct pvfs_filename **name)
464 *name = talloc(mem_ctx, struct pvfs_filename);
466 return NT_STATUS_NO_MEMORY;
469 (*name)->exists = false;
470 (*name)->stream_exists = false;
472 if (!(pvfs->fs_attribs & FS_ATTR_NAMED_STREAMS)) {
473 flags &= ~PVFS_RESOLVE_STREAMS;
476 /* do the basic conversion to a unix formatted path,
477 also checking for allowable characters */
478 status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
480 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_SYNTAX_BAD)) {
481 /* it might contain .. components which need to be reduced */
482 status = pvfs_reduce_name(*name, lp_iconv_convenience(pvfs->ntvfs->ctx->lp_ctx), &cifs_name, flags);
483 if (!NT_STATUS_IS_OK(status)) {
486 status = pvfs_unix_path(pvfs, cifs_name, flags, *name);
489 if (!NT_STATUS_IS_OK(status)) {
493 /* if it has a wildcard then no point doing a stat() of the
494 full name. Instead We need check if the directory exists
496 if ((*name)->has_wildcard) {
498 char *dir_name, *saved_name;
499 p = strrchr((*name)->full_name, '/');
501 /* root directory wildcard is OK */
504 dir_name = talloc_strndup(*name, (*name)->full_name, (p-(*name)->full_name));
505 if (stat(dir_name, &(*name)->st) == 0) {
506 talloc_free(dir_name);
509 /* we need to search for a matching name */
510 saved_name = (*name)->full_name;
511 (*name)->full_name = dir_name;
512 status = pvfs_case_search(pvfs, *name);
513 if (!NT_STATUS_IS_OK(status)) {
514 /* the directory doesn't exist */
515 (*name)->full_name = saved_name;
518 /* it does exist, but might need a case change */
519 if (dir_name != (*name)->full_name) {
520 (*name)->full_name = talloc_asprintf(*name, "%s%s",
521 (*name)->full_name, p);
522 NT_STATUS_HAVE_NO_MEMORY((*name)->full_name);
524 (*name)->full_name = saved_name;
525 talloc_free(dir_name);
530 /* if we can stat() the full name now then we are done */
531 if (stat((*name)->full_name, &(*name)->st) == 0) {
532 (*name)->exists = true;
533 return pvfs_fill_dos_info(pvfs, *name, -1);
536 /* search for a matching filename */
537 status = pvfs_case_search(pvfs, *name);
544 do a partial resolve, returning a pvfs_filename structure given a
545 base path and a relative component. It is an error if the file does
546 not exist. No case-insensitive matching is done.
548 this is used in places like directory searching where we need a pvfs_filename
549 to pass to a function, but already know the unix base directory and component
551 NTSTATUS pvfs_resolve_partial(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
552 const char *unix_dir, const char *fname,
553 struct pvfs_filename **name)
557 *name = talloc(mem_ctx, struct pvfs_filename);
559 return NT_STATUS_NO_MEMORY;
562 (*name)->full_name = talloc_asprintf(*name, "%s/%s", unix_dir, fname);
563 if ((*name)->full_name == NULL) {
564 return NT_STATUS_NO_MEMORY;
567 if (stat((*name)->full_name, &(*name)->st) == -1) {
568 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
571 (*name)->exists = true;
572 (*name)->stream_exists = true;
573 (*name)->has_wildcard = false;
574 (*name)->original_name = talloc_strdup(*name, fname);
575 (*name)->stream_name = NULL;
576 (*name)->stream_id = 0;
578 status = pvfs_fill_dos_info(pvfs, *name, -1);
585 fill in the pvfs_filename info for an open file, given the current
586 info for a (possibly) non-open file. This is used by places that need
587 to update the pvfs_filename stat information, and by pvfs_open()
589 NTSTATUS pvfs_resolve_name_fd(struct pvfs_state *pvfs, int fd,
590 struct pvfs_filename *name)
592 dev_t device = (dev_t)0;
596 device = name->st.st_dev;
597 inode = name->st.st_ino;
601 if (stat(name->full_name, &name->st) == -1) {
602 return NT_STATUS_INVALID_HANDLE;
605 if (fstat(fd, &name->st) == -1) {
606 return NT_STATUS_INVALID_HANDLE;
611 (device != name->st.st_dev || inode != name->st.st_ino)) {
612 /* the file we are looking at has changed! this could
613 be someone trying to exploit a race
614 condition. Certainly we don't want to continue
615 operating on this file */
616 DEBUG(0,("pvfs: WARNING: file '%s' changed during resolve - failing\n",
618 return NT_STATUS_UNEXPECTED_IO_ERROR;
623 return pvfs_fill_dos_info(pvfs, name, fd);
627 fill in the pvfs_filename info for an open file, given the current
628 info for a (possibly) non-open file. This is used by places that need
629 to update the pvfs_filename stat information, and the path
630 after a possible rename on a different handle.
632 NTSTATUS pvfs_resolve_name_handle(struct pvfs_state *pvfs,
633 struct pvfs_file_handle *h)
637 if (h->have_opendb_entry) {
638 struct odb_lock *lck;
639 const char *name = NULL;
641 lck = odb_lock(h, h->pvfs->odb_context, &h->odb_locking_key);
643 DEBUG(0,("%s: failed to lock file '%s' in opendb\n",
644 __FUNCTION__, h->name->full_name));
645 /* we were supposed to do a blocking lock, so something
647 return NT_STATUS_INTERNAL_DB_CORRUPTION;
650 status = odb_get_path(lck, &name);
651 if (NT_STATUS_IS_OK(status)) {
653 * This relies an the fact that
654 * renames of open files are only
655 * allowed by setpathinfo() and setfileinfo()
656 * and there're only renames within the same
657 * directory supported
659 if (strcmp(h->name->full_name, name) != 0) {
660 const char *orig_dir;
661 const char *new_file;
662 const char *new_orig;
665 delim = strrchr(name, '/');
668 return NT_STATUS_INTERNAL_ERROR;
671 new_file = delim + 1;
672 delim = strrchr(h->name->original_name, '\\');
675 orig_dir = h->name->original_name;
676 new_orig = talloc_asprintf(h->name, "%s\\%s",
680 return NT_STATUS_NO_MEMORY;
683 new_orig = talloc_strdup(h->name, new_file);
686 return NT_STATUS_NO_MEMORY;
690 talloc_free(h->name->original_name);
691 talloc_free(h->name->full_name);
692 h->name->full_name = talloc_steal(h->name, name);
693 h->name->original_name = new_orig;
700 status = pvfs_resolve_name_fd(pvfs, h->fd, h->name);
701 NT_STATUS_NOT_OK_RETURN(status);
708 resolve the parent of a given name
710 NTSTATUS pvfs_resolve_parent(struct pvfs_state *pvfs, TALLOC_CTX *mem_ctx,
711 const struct pvfs_filename *child,
712 struct pvfs_filename **name)
717 *name = talloc(mem_ctx, struct pvfs_filename);
719 return NT_STATUS_NO_MEMORY;
722 (*name)->full_name = talloc_strdup(*name, child->full_name);
723 if ((*name)->full_name == NULL) {
724 return NT_STATUS_NO_MEMORY;
727 p = strrchr_m((*name)->full_name, '/');
729 return NT_STATUS_OBJECT_PATH_SYNTAX_BAD;
732 /* this handles the root directory */
733 if (p == (*name)->full_name) {
739 if (stat((*name)->full_name, &(*name)->st) == -1) {
740 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
743 (*name)->exists = true;
744 (*name)->stream_exists = true;
745 (*name)->has_wildcard = false;
746 /* we can't get the correct 'original_name', but for the purposes
747 of this call this is close enough */
748 (*name)->original_name = talloc_reference(*name, child->original_name);
749 (*name)->stream_name = NULL;
750 (*name)->stream_id = 0;
752 status = pvfs_fill_dos_info(pvfs, *name, -1);