2 Unix SMB/CIFS implementation.
3 filename handling routines
4 Copyright (C) Andrew Tridgell 1992-1998
5 Copyright (C) Jeremy Allison 1999-2007
6 Copyright (C) Ying Chen 2000
7 Copyright (C) Volker Lendecke 2007
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 3 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
24 * New hash table stat cache code added by Ying Chen.
28 #include "system/filesys.h"
29 #include "fake_file.h"
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
33 static int get_real_filename(connection_struct *conn,
34 struct smb_filename *path,
39 static NTSTATUS check_name(connection_struct *conn,
40 const struct smb_filename *smb_fname);
42 uint32_t ucf_flags_from_smb_request(struct smb_request *req)
44 uint32_t ucf_flags = 0;
47 if (req->posix_pathnames) {
48 ucf_flags |= UCF_POSIX_PATHNAMES;
50 if (req->flags2 & FLAGS2_DFS_PATHNAMES) {
51 ucf_flags |= UCF_DFS_PATHNAME;
53 if (req->flags2 & FLAGS2_REPARSE_PATH) {
54 ucf_flags |= UCF_GMT_PATHNAME;
61 uint32_t filename_create_ucf_flags(struct smb_request *req, uint32_t create_disposition)
63 uint32_t ucf_flags = 0;
65 ucf_flags |= ucf_flags_from_smb_request(req);
67 switch (create_disposition) {
74 case FILE_OVERWRITE_IF:
75 ucf_flags |= UCF_PREP_CREATEFILE;
82 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
83 connection_struct *conn,
84 struct smb_filename *smb_fname);
86 /****************************************************************************
87 Mangle the 2nd name and check if it is then equal to the first name.
88 ****************************************************************************/
90 static bool mangled_equal(const char *name1,
92 const struct share_params *p)
96 if (!name_to_8_3(name2, mname, False, p)) {
99 return strequal(name1, mname);
102 static NTSTATUS check_for_dot_component(const struct smb_filename *smb_fname)
104 /* Ensure we catch all names with in "/."
105 this is disallowed under Windows and
106 in POSIX they've already been removed. */
107 const char *p = strstr(smb_fname->base_name, "/."); /*mb safe*/
110 /* Error code within a pathname. */
111 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
112 } else if (p[2] == '\0') {
113 /* Error code at the end of a pathname. */
114 return NT_STATUS_OBJECT_NAME_INVALID;
120 /****************************************************************************
121 Optimization for common case where the missing part
122 is in the last component and the client already
123 sent the correct case.
124 Returns NT_STATUS_OK to mean continue the tree walk
125 (possibly with modified start pointer).
126 Any other NT_STATUS_XXX error means terminate the path
128 ****************************************************************************/
130 static NTSTATUS check_parent_exists(TALLOC_CTX *ctx,
131 connection_struct *conn,
132 bool posix_pathnames,
133 const struct smb_filename *smb_fname,
136 int *p_parent_stat_errno)
138 char *parent_name = NULL;
139 struct smb_filename *parent_fname = NULL;
140 const char *last_component = NULL;
144 if (!parent_dirname(ctx, smb_fname->base_name,
147 return NT_STATUS_NO_MEMORY;
150 if (!posix_pathnames) {
151 if (ms_has_wild(parent_name)) {
152 goto no_optimization_out;
157 * If there was no parent component in
158 * smb_fname->base_name then don't do this
161 if (smb_fname->base_name == last_component) {
162 goto no_optimization_out;
165 parent_fname = synthetic_smb_fname(ctx,
171 if (parent_fname == NULL) {
172 return NT_STATUS_NO_MEMORY;
175 ret = vfs_stat(conn, parent_fname);
177 /* If the parent stat failed, just continue
178 with the normal tree walk. */
182 * Optimization. Preserving the
183 * errno from the STAT/LSTAT here
184 * will allow us to save a duplicate
185 * STAT/LSTAT system call of the parent
186 * pathname in a hot code path in the caller.
188 if (p_parent_stat_errno != NULL) {
189 *p_parent_stat_errno = errno;
191 goto no_optimization_out;
194 status = check_for_dot_component(parent_fname);
195 if (!NT_STATUS_IS_OK(status)) {
199 /* Parent exists - set "start" to be the
200 * last component to shorten the tree walk. */
203 * Safe to use discard_const_p
204 * here as last_component points
205 * into our smb_fname->base_name.
207 *pp_start = discard_const_p(char, last_component);
209 /* Update dirpath. */
210 TALLOC_FREE(*pp_dirpath);
211 *pp_dirpath = talloc_strdup(ctx, parent_fname->base_name);
213 return NT_STATUS_NO_MEMORY;
216 DEBUG(5,("check_parent_exists: name "
217 "= %s, dirpath = %s, "
219 smb_fname->base_name,
228 * We must still return an *pp_dirpath
229 * initialized to ".", and a *pp_start
230 * pointing at smb_fname->base_name.
233 TALLOC_FREE(parent_name);
234 TALLOC_FREE(parent_fname);
236 *pp_dirpath = talloc_strdup(ctx, ".");
237 if (*pp_dirpath == NULL) {
238 return NT_STATUS_NO_MEMORY;
241 * Safe to use discard_const_p
242 * here as by convention smb_fname->base_name
243 * is allocated off ctx.
245 *pp_start = discard_const_p(char, smb_fname->base_name);
250 * Re-order a known good @GMT-token path.
253 static NTSTATUS rearrange_snapshot_path(struct smb_filename *smb_fname,
258 size_t gmt_len = endp - startp;
259 char gmt_store[gmt_len + 1];
261 const char *last_component = NULL;
265 DBG_DEBUG("|%s| -> ", smb_fname->base_name);
267 /* Save off the @GMT-token. */
268 memcpy(gmt_store, startp, gmt_len);
269 gmt_store[gmt_len] = '\0';
272 /* Remove any trailing '/' */
278 * @GMT-token was at end of path.
279 * Remove any preceding '/'
281 if (startp > smb_fname->base_name && startp[-1] == '/') {
286 /* Remove @GMT-token from the path. */
287 endlen = strlen(endp);
288 memmove(startp, endp, endlen + 1);
290 /* Split the remaining path into components. */
291 ret = parent_dirname(smb_fname,
292 smb_fname->base_name,
296 /* Must terminate debug with \n */
297 DBG_DEBUG("NT_STATUS_NO_MEMORY\n");
298 return NT_STATUS_NO_MEMORY;
302 if (last_component[0] == '\0') {
303 newstr = talloc_strdup(smb_fname,
306 newstr = talloc_asprintf(smb_fname,
312 newstr = talloc_asprintf(smb_fname,
320 TALLOC_FREE(smb_fname->base_name);
321 smb_fname->base_name = newstr;
323 DBG_DEBUG("|%s|\n", newstr);
329 * Strip a valid @GMT-token from any incoming filename path,
330 * adding any NTTIME encoded in the pathname into the
331 * twrp field of the passed in smb_fname.
333 * Valid @GMT-tokens look like @GMT-YYYY-MM-DD-HH-MM-SS
334 * at the *start* of a pathname component.
336 * If twrp is passed in then smb_fname->twrp is set to that
337 * value, and the @GMT-token part of the filename is removed
338 * and does not change the stored smb_fname->twrp.
342 NTSTATUS canonicalize_snapshot_path(struct smb_filename *smb_fname,
355 smb_fname->twrp = twrp;
358 if (!(ucf_flags & UCF_GMT_PATHNAME)) {
362 startp = strchr_m(smb_fname->base_name, '@');
363 if (startp == NULL) {
368 startp = strstr_m(startp, "@GMT-");
369 if (startp == NULL) {
374 if ((startp > smb_fname->base_name) && (startp[-1] != '/')) {
375 /* the GMT-token does not start a path-component */
379 endp = strptime(startp, GMT_FORMAT, &tm);
381 /* Not a valid timestring. */
385 if (endp[0] != '\0' && endp[0] != '/') {
387 * It is not a complete path component, i.e. the path
388 * component continues after the gmt-token.
393 status = rearrange_snapshot_path(smb_fname, startp, endp);
394 if (!NT_STATUS_IS_OK(status)) {
398 startp = smb_fname->base_name + GMT_NAME_LEN;
399 if (startp[0] == '/') {
403 tmp = talloc_strdup(smb_fname, startp);
405 return NT_STATUS_NO_MEMORY;
408 TALLOC_FREE(smb_fname->base_name);
409 smb_fname->base_name = tmp;
411 if (smb_fname->twrp == 0) {
414 unix_to_nt_time(&nt, t);
415 smb_fname->twrp = nt;
421 static bool strnorm(char *s, int case_default)
423 if (case_default == CASE_UPPER)
424 return strupper_m(s);
426 return strlower_m(s);
430 * Utility function to normalize case on an incoming client filename
431 * if required on this connection struct.
432 * Performs an in-place case conversion guaranteed to stay the same size.
435 static NTSTATUS normalize_filename_case(connection_struct *conn,
441 if (ucf_flags & UCF_POSIX_PATHNAMES) {
443 * POSIX never normalizes filename case.
447 if (!conn->case_sensitive) {
450 if (conn->case_preserve) {
453 if (conn->short_case_preserve) {
456 ok = strnorm(filename, lp_default_case(SNUM(conn)));
458 return NT_STATUS_INVALID_PARAMETER;
463 /****************************************************************************
464 This routine is called to convert names from the dos namespace to unix
465 namespace. It needs to handle any case conversions, mangling, format changes,
468 We assume that we have already done a chdir() to the right "root" directory
471 Conversion to basic unix format is already done in check_path_syntax().
473 Names must be relative to the root of the service - any leading /. and
474 trailing /'s should have been trimmed by check_path_syntax().
476 The function will return an NTSTATUS error if some part of the name except for
477 the last part cannot be resolved, else NT_STATUS_OK.
479 Note NT_STATUS_OK doesn't mean the name exists or is valid, just that we
480 didn't get any fatal errors that should immediately terminate the calling SMB
481 processing whilst resolving.
483 If the orig_path was a stream, smb_filename->base_name will point to the base
484 filename, and smb_filename->stream_name will point to the stream name. If
485 orig_path was not a stream, then smb_filename->stream_name will be NULL.
487 On exit from unix_convert, the smb_filename->st stat struct will be populated
488 if the file exists and was found, if not this stat struct will be filled with
489 zeros (and this can be detected by checking for nlinks = 0, which can never be
491 ****************************************************************************/
495 struct connection_struct *conn;
496 struct smb_filename *smb_fname;
497 const char *orig_path;
503 bool component_was_mangled;
504 bool posix_pathnames;
508 bool short_case_preserve;
511 static NTSTATUS unix_convert_step_search_fail(struct uc_state *state)
517 * An intermediate part of the name
520 DBG_DEBUG("Intermediate [%s] missing\n",
525 * We need to return the fact that the
526 * intermediate name resolution failed.
527 * This is used to return an error of
528 * ERRbadpath rather than ERRbadfile.
529 * Some Windows applications depend on
530 * the difference between these two
535 * ENOENT, ENOTDIR and ELOOP all map
536 * to NT_STATUS_OBJECT_PATH_NOT_FOUND
537 * in the filename walk.
540 if (errno == ENOENT ||
544 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
546 return map_nt_error_from_unix(errno);
550 * ENOENT/EACCESS are the only valid errors
554 if (errno == EACCES) {
555 if ((state->ucf_flags & UCF_PREP_CREATEFILE) == 0) {
557 * Could be a symlink pointing to
558 * a directory outside the share
559 * to which we don't have access.
560 * If so, we need to know that here
561 * so we can return the correct error code.
562 * check_name() is never called if we
563 * error out of filename_convert().
567 struct smb_filename dname = (struct smb_filename) {
568 .base_name = state->dirpath,
569 .twrp = state->smb_fname->twrp,
572 /* handle null paths */
573 if ((dname.base_name == NULL) ||
574 (dname.base_name[0] == '\0')) {
575 return NT_STATUS_ACCESS_DENIED;
577 ret = SMB_VFS_LSTAT(state->conn, &dname);
579 return NT_STATUS_ACCESS_DENIED;
581 if (!S_ISLNK(dname.st.st_ex_mode)) {
582 return NT_STATUS_ACCESS_DENIED;
584 status = check_name(state->conn, &dname);
585 if (!NT_STATUS_IS_OK(status)) {
586 /* We know this is an intermediate path. */
587 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
589 return NT_STATUS_ACCESS_DENIED;
592 * This is the dropbox
593 * behaviour. A dropbox is a
594 * directory with only -wx
596 * get_real_filename fails
597 * with EACCESS, it needs to
598 * list the directory. We
599 * nevertheless want to allow
600 * users creating a file.
606 if ((errno != 0) && (errno != ENOENT)) {
608 * ENOTDIR and ELOOP both map to
609 * NT_STATUS_OBJECT_PATH_NOT_FOUND
610 * in the filename walk.
612 if (errno == ENOTDIR || errno == ELOOP) {
613 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
615 return map_nt_error_from_unix(errno);
619 * POSIX pathnames must never call into mangling.
621 if (state->posix_pathnames) {
626 * Just the last part of the name doesn't exist.
627 * We need to strupper() or strlower() it as
628 * this conversion may be used for file creation
629 * purposes. Fix inspired by
630 * Thomas Neumann <t.neumann@iku-ag.de>.
632 if (!state->case_preserve ||
633 (mangle_is_8_3(state->name, false,
634 state->conn->params) &&
635 !state->short_case_preserve)) {
636 if (!strnorm(state->name,
637 lp_default_case(SNUM(state->conn)))) {
638 DBG_DEBUG("strnorm %s failed\n",
640 return NT_STATUS_INVALID_PARAMETER;
645 * check on the mangled stack to see if we can
646 * recover the base of the filename.
649 if (mangle_is_mangled(state->name, state->conn->params)
650 && mangle_lookup_name_from_8_3(state->mem_ctx,
653 state->conn->params)) {
656 state->name - state->smb_fname->base_name;
658 if (!ISDOT(state->dirpath)) {
659 tmp = talloc_asprintf(
660 state->smb_fname, "%s/%s",
661 state->dirpath, unmangled);
662 TALLOC_FREE(unmangled);
668 DBG_ERR("talloc failed\n");
669 return NT_STATUS_NO_MEMORY;
671 TALLOC_FREE(state->smb_fname->base_name);
672 state->smb_fname->base_name = tmp;
674 state->smb_fname->base_name + name_ofs;
675 state->end = state->name + strlen(state->name);
680 DBG_DEBUG("New file [%s]\n", state->name);
685 static NTSTATUS unix_convert_step_stat(struct uc_state *state)
687 struct smb_filename dname;
689 char *found_name = NULL;
693 * Check if the name exists up to this point.
696 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(state->smb_fname));
698 ret = vfs_stat(state->conn, state->smb_fname);
701 * It exists. it must either be a directory or this must
702 * be the last part of the path for it to be OK.
704 if (state->end && !S_ISDIR(state->smb_fname->st.st_ex_mode)) {
706 * An intermediate part of the name isn't
709 DBG_DEBUG("Not a dir [%s]\n", state->name);
712 * We need to return the fact that the
713 * intermediate name resolution failed. This
714 * is used to return an error of ERRbadpath
715 * rather than ERRbadfile. Some Windows
716 * applications depend on the difference between
719 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
724 /* Stat failed - ensure we don't use it. */
725 SET_STAT_INVALID(state->smb_fname->st);
727 if (state->posix_pathnames) {
729 * For posix_pathnames, we're done.
730 * Don't blunder into the
731 * get_real_filename() codepath as they may
732 * be doing case insensitive lookups. So when
733 * creating a new POSIX directory Foo they might
736 * BUG: https://bugzilla.samba.org/show_bug.cgi?id=13803
738 if (state->end != NULL) {
739 const char *morepath = NULL;
741 * If this is intermediate we must
742 * restore the full path.
746 * If there are any more components
747 * after the failed LSTAT we cannot
750 morepath = strchr(state->end + 1, '/');
751 if (morepath != NULL) {
752 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
755 if (errno == ENOENT) {
756 /* New file or directory. */
760 if ((errno == EACCES) &&
761 (state->ucf_flags & UCF_PREP_CREATEFILE)) {
762 /* POSIX Dropbox case. */
767 return map_nt_error_from_unix(errno);
771 * Reset errno so we can detect
772 * directory open errors.
777 * Try to find this part of the path in the directory.
780 dname = (struct smb_filename) {
781 .base_name = state->dirpath,
782 .twrp = state->smb_fname->twrp,
785 /* handle null paths */
786 if ((dname.base_name == NULL) || (dname.base_name[0] == '\0')) {
787 dname.base_name = dot;
790 ret = get_real_filename(state->conn,
796 return unix_convert_step_search_fail(state);
800 * Restore the rest of the string. If the string was
801 * mangled the size may have changed.
806 state->name - state->smb_fname->base_name;
808 if (!ISDOT(state->dirpath)) {
809 tmp = talloc_asprintf(state->smb_fname,
810 "%s/%s/%s", state->dirpath,
811 found_name, state->end+1);
814 tmp = talloc_asprintf(state->smb_fname,
819 DBG_ERR("talloc_asprintf failed\n");
820 return NT_STATUS_NO_MEMORY;
822 TALLOC_FREE(state->smb_fname->base_name);
823 state->smb_fname->base_name = tmp;
824 state->name = state->smb_fname->base_name + name_ofs;
825 state->end = state->name + strlen(found_name);
830 state->name - state->smb_fname->base_name;
832 if (!ISDOT(state->dirpath)) {
833 tmp = talloc_asprintf(state->smb_fname,
834 "%s/%s", state->dirpath,
837 tmp = talloc_strdup(state->smb_fname,
841 DBG_ERR("talloc failed\n");
842 return NT_STATUS_NO_MEMORY;
844 TALLOC_FREE(state->smb_fname->base_name);
845 state->smb_fname->base_name = tmp;
846 state->name = state->smb_fname->base_name + name_ofs;
849 * We just scanned for, and found the end of
850 * the path. We must return a valid stat struct
854 ret = vfs_stat(state->conn, state->smb_fname);
856 SET_STAT_INVALID(state->smb_fname->st);
860 TALLOC_FREE(found_name);
864 static NTSTATUS unix_convert_step(struct uc_state *state)
869 * Pinpoint the end of this section of the filename.
871 /* mb safe. '/' can't be in any encoded char. */
872 state->end = strchr(state->name, '/');
875 * Chop the name at this point.
877 if (state->end != NULL) {
881 DBG_DEBUG("dirpath [%s] name [%s]\n", state->dirpath, state->name);
883 /* The name cannot have a component of "." */
885 if (ISDOT(state->name)) {
886 if (state->end == NULL) {
887 /* Error code at the end of a pathname. */
888 return NT_STATUS_OBJECT_NAME_INVALID;
890 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
893 status = unix_convert_step_stat(state);
894 if (!NT_STATUS_IS_OK(status)) {
902 * Add to the dirpath that we have resolved so far.
905 if (!ISDOT(state->dirpath)) {
906 char *tmp = talloc_asprintf(state->mem_ctx,
907 "%s/%s", state->dirpath, state->name);
909 DBG_ERR("talloc_asprintf failed\n");
910 return NT_STATUS_NO_MEMORY;
912 TALLOC_FREE(state->dirpath);
913 state->dirpath = tmp;
916 TALLOC_FREE(state->dirpath);
917 if (!(state->dirpath = talloc_strdup(state->mem_ctx,state->name))) {
918 DBG_ERR("talloc_strdup failed\n");
919 return NT_STATUS_NO_MEMORY;
924 * Cache the dirpath thus far. Don't cache a name with mangled
925 * components as this can change the size.
927 if(!state->component_was_mangled) {
928 stat_cache_add(state->orig_path,
930 state->smb_fname->twrp,
931 state->case_sensitive);
935 * Restore the / that we wiped out earlier.
937 if (state->end != NULL) {
944 NTSTATUS unix_convert(TALLOC_CTX *mem_ctx,
945 connection_struct *conn,
946 const char *orig_path,
948 struct smb_filename **smb_fname_out,
951 struct uc_state uc_state;
952 struct uc_state *state = &uc_state;
955 int parent_stat_errno = 0;
957 *state = (struct uc_state) {
960 .orig_path = orig_path,
961 .ucf_flags = ucf_flags,
962 .posix_pathnames = (ucf_flags & UCF_POSIX_PATHNAMES),
963 .case_sensitive = conn->case_sensitive,
964 .case_preserve = conn->case_preserve,
965 .short_case_preserve = conn->short_case_preserve,
968 *smb_fname_out = NULL;
970 if (state->posix_pathnames) {
971 /* POSIX means ignore case settings on share. */
972 state->case_sensitive = true;
973 state->case_preserve = true;
974 state->short_case_preserve = true;
977 state->smb_fname = talloc_zero(state->mem_ctx, struct smb_filename);
978 if (state->smb_fname == NULL) {
979 return NT_STATUS_NO_MEMORY;
982 if (state->conn->printer) {
983 /* we don't ever use the filenames on a printer share as a
984 filename - so don't convert them */
985 state->smb_fname->base_name = talloc_strdup(
986 state->smb_fname, state->orig_path);
987 if (state->smb_fname->base_name == NULL) {
988 status = NT_STATUS_NO_MEMORY;
994 state->smb_fname->flags = state->posix_pathnames ? SMB_FILENAME_POSIX_PATH : 0;
996 DBG_DEBUG("Called on file [%s]\n", state->orig_path);
998 if (state->orig_path[0] == '/') {
999 DBG_ERR("Path [%s] starts with '/'\n", state->orig_path);
1000 return NT_STATUS_OBJECT_NAME_INVALID;
1003 /* Start with the full orig_path as given by the caller. */
1004 state->smb_fname->base_name = talloc_strdup(
1005 state->smb_fname, state->orig_path);
1006 if (state->smb_fname->base_name == NULL) {
1007 DBG_ERR("talloc_strdup failed\n");
1008 status = NT_STATUS_NO_MEMORY;
1012 /* Canonicalize any @GMT- paths. */
1013 status = canonicalize_snapshot_path(state->smb_fname, ucf_flags, twrp);
1014 if (!NT_STATUS_IS_OK(status)) {
1019 * If we trimmed down to a single '\0' character
1020 * then we should use the "." directory to avoid
1021 * searching the cache, but not if we are in a
1023 * As we know this is valid we can return true here.
1026 if (state->smb_fname->base_name[0] == '\0') {
1027 state->smb_fname->base_name = talloc_strdup(state->smb_fname, ".");
1028 if (state->smb_fname->base_name == NULL) {
1029 status = NT_STATUS_NO_MEMORY;
1032 if (SMB_VFS_STAT(state->conn, state->smb_fname) != 0) {
1033 status = map_nt_error_from_unix(errno);
1036 DBG_DEBUG("conversion finished [] -> [%s]\n",
1037 state->smb_fname->base_name);
1041 if (state->orig_path[0] == '.' && (state->orig_path[1] == '/' ||
1042 state->orig_path[1] == '\0')) {
1043 /* Start of pathname can't be "." only. */
1044 if (state->orig_path[1] == '\0' || state->orig_path[2] == '\0') {
1045 status = NT_STATUS_OBJECT_NAME_INVALID;
1047 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1053 * Large directory fix normalization. If we're case sensitive, and
1054 * the case preserving parameters are set to "no", normalize the case of
1055 * the incoming filename from the client WHETHER IT EXISTS OR NOT !
1056 * This is in conflict with the current (3.0.20) man page, but is
1057 * what people expect from the "large directory howto". I'll update
1058 * the man page. Thanks to jht@samba.org for finding this. JRA.
1061 status = normalize_filename_case(state->conn,
1062 state->smb_fname->base_name,
1064 if (!NT_STATUS_IS_OK(status)) {
1065 DBG_ERR("normalize_filename_case %s failed\n",
1066 state->smb_fname->base_name);
1071 * Strip off the stream, and add it back when we're done with the
1074 if (!state->posix_pathnames) {
1075 state->stream = strchr_m(state->smb_fname->base_name, ':');
1077 if (state->stream != NULL) {
1078 char *tmp = talloc_strdup(state->smb_fname, state->stream);
1080 status = NT_STATUS_NO_MEMORY;
1084 * Since this is actually pointing into
1085 * smb_fname->base_name this truncates base_name.
1087 *state->stream = '\0';
1088 state->stream = tmp;
1090 if (state->smb_fname->base_name[0] == '\0') {
1092 * orig_name was just a stream name.
1093 * This is a stream on the root of
1094 * the share. Replace base_name with
1097 state->smb_fname->base_name =
1098 talloc_strdup(state->smb_fname, ".");
1099 if (state->smb_fname->base_name == NULL) {
1100 status = NT_STATUS_NO_MEMORY;
1103 if (SMB_VFS_STAT(state->conn, state->smb_fname) != 0) {
1104 status = map_nt_error_from_unix(errno);
1107 /* dirpath must exist. */
1108 state->dirpath = talloc_strdup(state->mem_ctx,".");
1109 if (state->dirpath == NULL) {
1110 status = NT_STATUS_NO_MEMORY;
1113 DBG_INFO("conversion finished [%s] -> [%s]\n",
1115 state->smb_fname->base_name);
1121 state->name = state->smb_fname->base_name;
1124 * If we're providing case insensitive semantics or
1125 * the underlying filesystem is case insensitive,
1126 * then a case-normalized hit in the stat-cache is
1127 * authoritative. JRA.
1129 * Note: We're only checking base_name. The stream_name will be
1130 * added and verified in build_stream_path().
1133 if (!state->case_sensitive ||
1134 !(state->conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH))
1138 found = stat_cache_lookup(state->conn,
1139 &state->smb_fname->base_name,
1142 state->smb_fname->twrp,
1143 &state->smb_fname->st);
1150 * Make sure "dirpath" is an allocated string, we use this for
1151 * building the directories with talloc_asprintf and free it.
1154 if (state->dirpath == NULL) {
1155 state->dirpath = talloc_strdup(state->mem_ctx,".");
1156 if (state->dirpath == NULL) {
1157 DBG_ERR("talloc_strdup failed\n");
1158 status = NT_STATUS_NO_MEMORY;
1164 * If we have a wildcard we must walk the path to
1165 * find where the error is, even if case sensitive
1169 if (!state->posix_pathnames) {
1170 /* POSIX pathnames have no wildcards. */
1171 bool name_has_wildcard = ms_has_wild(state->smb_fname->base_name);
1172 if (name_has_wildcard) {
1173 /* Wildcard not valid anywhere. */
1174 status = NT_STATUS_OBJECT_NAME_INVALID;
1179 DBG_DEBUG("Begin: name [%s] dirpath [%s] name [%s]\n",
1180 state->smb_fname->base_name, state->dirpath, state->name);
1183 * stat the name - if it exists then we can add the stream back (if
1184 * there was one) and be done!
1187 ret = vfs_stat(state->conn, state->smb_fname);
1189 status = check_for_dot_component(state->smb_fname);
1190 if (!NT_STATUS_IS_OK(status)) {
1193 /* Add the path (not including the stream) to the cache. */
1194 stat_cache_add(state->orig_path,
1195 state->smb_fname->base_name,
1196 state->smb_fname->twrp,
1197 state->case_sensitive);
1198 DBG_DEBUG("Conversion of base_name finished "
1200 state->orig_path, state->smb_fname->base_name);
1204 /* Stat failed - ensure we don't use it. */
1205 SET_STAT_INVALID(state->smb_fname->st);
1208 * Note: we must continue processing a path if we get EACCES
1209 * from stat. With NFS4 permissions the file might be lacking
1210 * READ_ATTR, but if the parent has LIST permissions we can
1211 * resolve the path in the path traversal loop down below.
1214 if (errno == ENOENT) {
1215 /* Optimization when creating a new file - only
1216 the last component doesn't exist.
1217 NOTE : check_parent_exists() doesn't preserve errno.
1219 int saved_errno = errno;
1220 status = check_parent_exists(state->mem_ctx,
1222 state->posix_pathnames,
1226 &parent_stat_errno);
1227 errno = saved_errno;
1228 if (!NT_STATUS_IS_OK(status)) {
1234 * A special case - if we don't have any wildcards or mangling chars and are case
1235 * sensitive or the underlying filesystem is case insensitive then searching
1238 * NB. As POSIX sets state->case_sensitive as
1239 * true we will never call into mangle_is_mangled() here.
1242 if ((state->case_sensitive || !(state->conn->fs_capabilities &
1243 FILE_CASE_SENSITIVE_SEARCH)) &&
1244 !mangle_is_mangled(state->smb_fname->base_name, state->conn->params)) {
1246 status = check_for_dot_component(state->smb_fname);
1247 if (!NT_STATUS_IS_OK(status)) {
1252 * The stat failed. Could be ok as it could be
1256 if (errno == ENOTDIR || errno == ELOOP) {
1257 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1259 } else if (errno == ENOENT) {
1261 * Was it a missing last component ?
1262 * or a missing intermediate component ?
1266 * For this code path we can guarantee that
1267 * we have gone through check_parent_exists()
1268 * and it returned NT_STATUS_OK.
1270 * Either there was no parent component (".")
1271 * parent_stat_errno == 0 and we have a missing
1272 * last component here.
1274 * OR check_parent_exists() called STAT/LSTAT
1275 * and if it failed parent_stat_errno has been
1276 * set telling us if the parent existed or not.
1278 * Either way we can avoid another STAT/LSTAT
1279 * system call on the parent here.
1281 if (parent_stat_errno == ENOTDIR ||
1282 parent_stat_errno == ENOENT ||
1283 parent_stat_errno == ELOOP) {
1284 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1289 * Missing last component is ok - new file.
1290 * Also deal with permission denied elsewhere.
1291 * Just drop out to done.
1298 * is_mangled() was changed to look at an entire pathname, not
1299 * just a component. JRA.
1302 if (state->posix_pathnames) {
1304 * POSIX names are never mangled and we must not
1305 * call into mangling functions.
1307 state->component_was_mangled = false;
1308 } else if (mangle_is_mangled(state->name, state->conn->params)) {
1309 state->component_was_mangled = true;
1313 * Now we need to recursively match the name against the real
1314 * directory structure.
1318 * Match each part of the path name separately, trying the names
1319 * as is first, then trying to scan the directory for matching names.
1322 for (; state->name ; state->name = (state->end ? state->end + 1:(char *)NULL)) {
1323 status = unix_convert_step(state);
1324 if (!NT_STATUS_IS_OK(status)) {
1325 if (NT_STATUS_EQUAL(status, NT_STATUS_NO_MEMORY)) {
1336 * Cache the full path. Don't cache a name with mangled or wildcard
1337 * components as this can change the size.
1340 if(!state->component_was_mangled) {
1341 stat_cache_add(state->orig_path,
1342 state->smb_fname->base_name,
1343 state->smb_fname->twrp,
1344 state->case_sensitive);
1348 * The name has been resolved.
1352 /* Add back the stream if one was stripped off originally. */
1353 if (state->stream != NULL) {
1354 state->smb_fname->stream_name = state->stream;
1356 /* Check path now that the base_name has been converted. */
1357 status = build_stream_path(state->mem_ctx, state->conn, state->smb_fname);
1358 if (!NT_STATUS_IS_OK(status)) {
1363 DBG_DEBUG("Conversion finished [%s] -> [%s]\n",
1364 state->orig_path, smb_fname_str_dbg(state->smb_fname));
1366 TALLOC_FREE(state->dirpath);
1367 *smb_fname_out = state->smb_fname;
1368 return NT_STATUS_OK;
1370 DBG_DEBUG("Conversion failed: dirpath [%s] name [%s]\n",
1371 state->dirpath, state->name);
1372 if ((state->dirpath != NULL) && !ISDOT(state->dirpath)) {
1373 state->smb_fname->base_name = talloc_asprintf(
1379 state->smb_fname->base_name = talloc_strdup(
1380 state->smb_fname, state->name);
1382 if (state->smb_fname->base_name == NULL) {
1383 DBG_ERR("talloc_asprintf failed\n");
1384 status = NT_STATUS_NO_MEMORY;
1388 *smb_fname_out = state->smb_fname;
1389 TALLOC_FREE(state->dirpath);
1392 TALLOC_FREE(state->smb_fname);
1396 /****************************************************************************
1397 Ensure a path is not vetoed.
1398 ****************************************************************************/
1400 static NTSTATUS check_veto_path(connection_struct *conn,
1401 const struct smb_filename *smb_fname)
1403 const char *name = smb_fname->base_name;
1405 if (IS_VETO_PATH(conn, name)) {
1406 /* Is it not dot or dot dot. */
1407 if (!(ISDOT(name) || ISDOTDOT(name))) {
1408 DEBUG(5,("check_veto_path: file path name %s vetoed\n",
1410 return map_nt_error_from_unix(ENOENT);
1413 return NT_STATUS_OK;
1416 /****************************************************************************
1417 Check a filename - possibly calling check_reduced_name.
1418 This is called by every routine before it allows an operation on a filename.
1419 It does any final confirmation necessary to ensure that the filename is
1420 a valid one for the user to access.
1421 ****************************************************************************/
1423 static NTSTATUS check_name(connection_struct *conn,
1424 const struct smb_filename *smb_fname)
1426 NTSTATUS status = check_veto_path(conn, smb_fname);
1428 if (!NT_STATUS_IS_OK(status)) {
1432 if (!lp_widelinks(SNUM(conn)) || !lp_follow_symlinks(SNUM(conn))) {
1433 status = check_reduced_name(conn, NULL, smb_fname);
1434 if (!NT_STATUS_IS_OK(status)) {
1435 DEBUG(5,("check_name: name %s failed with %s\n",
1436 smb_fname->base_name,
1437 nt_errstr(status)));
1442 return NT_STATUS_OK;
1445 /****************************************************************************
1446 Check if two filenames are equal.
1447 This needs to be careful about whether we are case sensitive.
1448 ****************************************************************************/
1450 static bool fname_equal(const char *name1, const char *name2,
1451 bool case_sensitive)
1453 /* Normal filename handling */
1454 if (case_sensitive) {
1455 return(strcmp(name1,name2) == 0);
1458 return(strequal(name1,name2));
1461 static bool sname_equal(const char *name1, const char *name2,
1462 bool case_sensitive)
1465 const char *s1 = NULL;
1466 const char *s2 = NULL;
1469 const char *e1 = NULL;
1470 const char *e2 = NULL;
1474 match = fname_equal(name1, name2, case_sensitive);
1479 if (name1[0] != ':') {
1482 if (name2[0] != ':') {
1486 e1 = strchr(s1, ':');
1490 n1 = PTR_DIFF(e1, s1);
1493 e2 = strchr(s2, ':');
1497 n2 = PTR_DIFF(e2, s2);
1500 /* Normal filename handling */
1501 if (case_sensitive) {
1502 return (strncmp(s1, s2, n1) == 0);
1506 * We can't use strnequal() here
1507 * as it takes the number of codepoints
1508 * and not the number of bytes.
1510 * So we make a copy before calling
1513 * Note that we TALLOC_FREE() in reverse order
1514 * in order to avoid memory fragmentation.
1517 c1 = talloc_strndup(talloc_tos(), s1, n1);
1518 c2 = talloc_strndup(talloc_tos(), s2, n2);
1519 if (c1 == NULL || c2 == NULL) {
1522 return (strncmp(s1, s2, n1) == 0);
1525 match = strequal(c1, c2);
1531 /****************************************************************************
1532 Scan a directory to find a filename, matching without case sensitivity.
1533 If the name looks like a mangled name then try via the mangling functions
1534 ****************************************************************************/
1536 int get_real_filename_full_scan(connection_struct *conn,
1540 TALLOC_CTX *mem_ctx,
1543 struct smb_Dir *cur_dir;
1544 const char *dname = NULL;
1545 char *talloced = NULL;
1546 char *unmangled_name = NULL;
1548 struct smb_filename *smb_fname = NULL;
1550 /* handle null paths */
1551 if ((path == NULL) || (*path == 0)) {
1555 /* If we have a case-sensitive filesystem, it doesn't do us any
1556 * good to search for a name. If a case variation of the name was
1557 * there, then the original stat(2) would have found it.
1559 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
1565 * The incoming name can be mangled, and if we de-mangle it
1566 * here it will not compare correctly against the filename (name2)
1567 * read from the directory and then mangled by the name_to_8_3()
1568 * call. We need to mangle both names or neither.
1571 * Fix for bug found by Dina Fine. If in case sensitive mode then
1572 * the mangle cache is no good (3 letter extension could be wrong
1573 * case - so don't demangle in this case - leave as mangled and
1574 * allow the mangling of the directory entry read (which is done
1575 * case insensitively) to match instead. This will lead to more
1576 * false positive matches but we fail completely without it. JRA.
1579 if (mangled && !conn->case_sensitive) {
1580 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
1584 /* Name is now unmangled. */
1585 name = unmangled_name;
1589 smb_fname = synthetic_smb_fname(talloc_tos(),
1595 if (smb_fname == NULL) {
1596 TALLOC_FREE(unmangled_name);
1600 /* open the directory */
1601 if (!(cur_dir = OpenDir(talloc_tos(), conn, smb_fname, NULL, 0))) {
1602 DEBUG(3,("scan dir didn't open dir [%s]\n",path));
1603 TALLOC_FREE(unmangled_name);
1604 TALLOC_FREE(smb_fname);
1608 TALLOC_FREE(smb_fname);
1610 /* now scan for matching names */
1612 while ((dname = ReadDirName(cur_dir, &curpos, NULL, &talloced))) {
1614 /* Is it dot or dot dot. */
1615 if (ISDOT(dname) || ISDOTDOT(dname)) {
1616 TALLOC_FREE(talloced);
1621 * At this point dname is the unmangled name.
1622 * name is either mangled or not, depending on the state
1623 * of the "mangled" variable. JRA.
1627 * Check mangled name against mangled name, or unmangled name
1628 * against unmangled name.
1631 if ((mangled && mangled_equal(name,dname,conn->params)) ||
1632 fname_equal(name, dname, conn->case_sensitive)) {
1633 /* we've found the file, change it's name and return */
1634 *found_name = talloc_strdup(mem_ctx, dname);
1635 TALLOC_FREE(unmangled_name);
1636 TALLOC_FREE(cur_dir);
1639 TALLOC_FREE(talloced);
1642 TALLOC_FREE(talloced);
1645 TALLOC_FREE(talloced);
1648 TALLOC_FREE(unmangled_name);
1649 TALLOC_FREE(cur_dir);
1654 /****************************************************************************
1655 Wrapper around the vfs get_real_filename and the full directory scan
1657 ****************************************************************************/
1659 static int get_real_filename(connection_struct *conn,
1660 struct smb_filename *path,
1662 TALLOC_CTX *mem_ctx,
1668 mangled = mangle_is_mangled(name, conn->params);
1671 return get_real_filename_full_scan(conn,
1679 /* Try the vfs first to take advantage of case-insensitive stat. */
1680 ret = SMB_VFS_GET_REAL_FILENAME(conn,
1687 * If the case-insensitive stat was successful, or returned an error
1688 * other than EOPNOTSUPP then there is no need to fall back on the
1689 * full directory scan.
1691 if (ret == 0 || (ret == -1 && errno != EOPNOTSUPP)) {
1695 return get_real_filename_full_scan(conn,
1703 static NTSTATUS build_stream_path(TALLOC_CTX *mem_ctx,
1704 connection_struct *conn,
1705 struct smb_filename *smb_fname)
1708 unsigned int i, num_streams = 0;
1709 struct stream_struct *streams = NULL;
1710 struct smb_filename *pathref = NULL;
1712 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1713 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname)));
1714 return NT_STATUS_OK;
1717 if (errno != ENOENT) {
1718 DEBUG(10, ("vfs_stat failed: %s\n", strerror(errno)));
1719 status = map_nt_error_from_unix(errno);
1723 if (smb_fname->fsp == NULL) {
1724 status = synthetic_pathref(mem_ctx,
1726 smb_fname->base_name,
1732 if (!NT_STATUS_IS_OK(status)) {
1733 if (NT_STATUS_EQUAL(status,
1734 NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1735 TALLOC_FREE(pathref);
1736 SET_STAT_INVALID(smb_fname->st);
1737 return NT_STATUS_OK;
1739 DBG_DEBUG("synthetic_pathref failed: %s\n",
1744 pathref = smb_fname;
1747 /* Fall back to a case-insensitive scan of all streams on the file. */
1748 status = vfs_fstreaminfo(pathref->fsp, mem_ctx,
1749 &num_streams, &streams);
1750 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1751 SET_STAT_INVALID(smb_fname->st);
1752 TALLOC_FREE(pathref);
1753 return NT_STATUS_OK;
1756 if (!NT_STATUS_IS_OK(status)) {
1757 DEBUG(10, ("vfs_fstreaminfo failed: %s\n", nt_errstr(status)));
1761 for (i=0; i<num_streams; i++) {
1762 bool equal = sname_equal(
1763 smb_fname->stream_name,
1765 conn->case_sensitive);
1767 DBG_DEBUG("comparing [%s] and [%s]: %sequal\n",
1768 smb_fname->stream_name,
1770 equal ? "" : "not ");
1777 /* Couldn't find the stream. */
1778 if (i == num_streams) {
1779 SET_STAT_INVALID(smb_fname->st);
1780 TALLOC_FREE(pathref);
1781 TALLOC_FREE(streams);
1782 return NT_STATUS_OK;
1785 DEBUG(10, ("case insensitive stream. requested: %s, actual: %s\n",
1786 smb_fname->stream_name, streams[i].name));
1789 TALLOC_FREE(smb_fname->stream_name);
1790 smb_fname->stream_name = talloc_strdup(smb_fname, streams[i].name);
1791 if (smb_fname->stream_name == NULL) {
1792 status = NT_STATUS_NO_MEMORY;
1796 SET_STAT_INVALID(smb_fname->st);
1798 if (SMB_VFS_STAT(conn, smb_fname) == 0) {
1799 DEBUG(10, ("'%s' exists\n", smb_fname_str_dbg(smb_fname)));
1801 status = NT_STATUS_OK;
1803 TALLOC_FREE(pathref);
1804 TALLOC_FREE(streams);
1809 * Lightweight function to just get last component
1810 * for rename / enumerate directory calls.
1813 char *get_original_lcomp(TALLOC_CTX *ctx,
1814 connection_struct *conn,
1815 const char *filename_in,
1818 struct smb_filename *smb_fname = NULL;
1819 char *last_slash = NULL;
1825 if (ucf_flags & UCF_DFS_PATHNAME) {
1826 status = dfs_redirect(ctx,
1830 !conn->sconn->using_smb2,
1833 if (!NT_STATUS_IS_OK(status)) {
1834 DBG_DEBUG("dfs_redirect "
1835 "failed for name %s with %s\n",
1840 filename_in = fname;
1841 ucf_flags &= ~UCF_DFS_PATHNAME;
1845 * NB. We don't need to care about
1846 * is_fake_file_path(filename_in) here as these
1847 * code paths don't ever return original_lcomp
1851 if (ucf_flags & UCF_GMT_PATHNAME) {
1853 * Ensure we don't return a @GMT
1854 * value as the last component.
1856 smb_fname = synthetic_smb_fname(ctx,
1862 if (smb_fname == NULL) {
1866 status = canonicalize_snapshot_path(smb_fname,
1869 if (!NT_STATUS_IS_OK(status)) {
1871 TALLOC_FREE(smb_fname);
1874 filename_in = smb_fname->base_name;
1876 last_slash = strrchr(filename_in, '/');
1877 if (last_slash != NULL) {
1878 orig_lcomp = talloc_strdup(ctx, last_slash+1);
1880 orig_lcomp = talloc_strdup(ctx, filename_in);
1882 /* We're done with any temp names here. */
1883 TALLOC_FREE(smb_fname);
1885 if (orig_lcomp == NULL) {
1888 status = normalize_filename_case(conn, orig_lcomp, ucf_flags);
1889 if (!NT_STATUS_IS_OK(status)) {
1890 TALLOC_FREE(orig_lcomp);
1897 * Go through all the steps to validate a filename.
1899 * @param ctx talloc_ctx to allocate memory with.
1900 * @param conn connection struct for vfs calls.
1901 * @param smbreq SMB request if we're using privileges.
1902 * @param name_in The unconverted name.
1903 * @param ucf_flags flags to pass through to unix_convert().
1904 * @param twrp Optional VSS time
1905 * @param p_cont_wcard If not NULL, will be set to true if the dfs path
1906 * resolution detects a wildcard.
1907 * @param _smb_fname The final converted name will be allocated if the
1908 * return is NT_STATUS_OK.
1910 * @return NT_STATUS_OK if all operations completed successfully, appropriate
1913 NTSTATUS filename_convert(TALLOC_CTX *ctx,
1914 connection_struct *conn,
1915 const char *name_in,
1918 struct smb_filename **_smb_fname)
1920 struct smb_filename *smb_fname = NULL;
1925 if (ucf_flags & UCF_DFS_PATHNAME) {
1927 NTTIME dfs_twrp = 0;
1928 status = dfs_redirect(ctx, conn,
1931 !conn->sconn->using_smb2,
1934 if (!NT_STATUS_IS_OK(status)) {
1935 DBG_DEBUG("dfs_redirect "
1936 "failed for name %s with %s\n",
1942 ucf_flags &= ~UCF_DFS_PATHNAME;
1943 if (twrp == 0 && dfs_twrp != 0) {
1948 if (is_fake_file_path(name_in)) {
1949 smb_fname = synthetic_smb_fname_split(ctx,
1951 (ucf_flags & UCF_POSIX_PATHNAMES));
1952 if (smb_fname == NULL) {
1953 return NT_STATUS_NO_MEMORY;
1955 smb_fname->st = (SMB_STRUCT_STAT) { .st_ex_nlink = 1 };
1956 smb_fname->st.st_ex_btime = (struct timespec){0, SAMBA_UTIME_OMIT};
1957 smb_fname->st.st_ex_atime = (struct timespec){0, SAMBA_UTIME_OMIT};
1958 smb_fname->st.st_ex_mtime = (struct timespec){0, SAMBA_UTIME_OMIT};
1959 smb_fname->st.st_ex_ctime = (struct timespec){0, SAMBA_UTIME_OMIT};
1961 *_smb_fname = smb_fname;
1962 return NT_STATUS_OK;
1965 status = unix_convert(ctx, conn, name_in, twrp, &smb_fname, ucf_flags);
1966 if (!NT_STATUS_IS_OK(status)) {
1967 DBG_DEBUG("unix_convert failed "
1968 "for name %s with %s\n",
1974 if ((ucf_flags & UCF_POSIX_PATHNAMES) &&
1975 VALID_STAT(smb_fname->st) &&
1976 S_ISLNK(smb_fname->st.st_ex_mode))
1978 status = check_veto_path(conn, smb_fname);
1979 if (!NT_STATUS_IS_OK(status)) {
1980 TALLOC_FREE(smb_fname);
1984 status = check_name(conn, smb_fname);
1986 if (!NT_STATUS_IS_OK(status)) {
1987 DBG_NOTICE("check_name failed "
1988 "for name %s with %s\n",
1989 smb_fname_str_dbg(smb_fname),
1991 TALLOC_FREE(smb_fname);
1995 if (!VALID_STAT(smb_fname->st)) {
1996 DBG_DEBUG("[%s] does not exist, skipping pathref fsp\n",
1997 smb_fname_str_dbg(smb_fname));
1998 *_smb_fname = smb_fname;
1999 return NT_STATUS_OK;
2002 status = openat_pathref_fsp(conn->cwd_fsp, smb_fname);
2003 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
2005 * We deal with symlinks here as we do in
2006 * SMB_VFS_CREATE_FILE(): return success for POSIX clients with
2007 * the notable difference that there will be no fsp in
2010 * For Windows (non POSIX) clients fail with
2011 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
2013 if (smb_fname->flags & SMB_FILENAME_POSIX_PATH &&
2014 S_ISLNK(smb_fname->st.st_ex_mode))
2016 status = NT_STATUS_OK;
2019 if (!NT_STATUS_IS_OK(status)) {
2020 DBG_DEBUG("openat_pathref_fsp [%s] failed: %s\n",
2021 smb_fname_str_dbg(smb_fname),
2026 *_smb_fname = smb_fname;
2031 * Strip a @GMT component from an SMB1-DFS path. Could be anywhere
2035 static char *strip_gmt_from_raw_dfs(TALLOC_CTX *ctx,
2036 const char *name_in,
2037 bool posix_pathnames,
2041 struct smb_filename *smb_fname = NULL;
2042 char *name_out = NULL;
2044 smb_fname = synthetic_smb_fname(ctx,
2050 if (smb_fname == NULL) {
2053 if (!posix_pathnames) {
2055 * Raw DFS names are still '\\' separated.
2056 * canonicalize_snapshot_path() only works
2057 * on '/' separated paths. Convert.
2059 string_replace(smb_fname->base_name, '\\', '/');
2061 status = canonicalize_snapshot_path(smb_fname,
2064 if (!NT_STATUS_IS_OK(status)) {
2065 TALLOC_FREE(smb_fname);
2068 if (!posix_pathnames) {
2069 /* Replace as raw DFS names. */
2070 string_replace(smb_fname->base_name, '/', '\\');
2072 name_out = talloc_strdup(ctx, smb_fname->base_name);
2073 *_twrp = smb_fname->twrp;
2074 TALLOC_FREE(smb_fname);
2079 * Deal with the SMB1 semantics of sending a pathname with a
2080 * wildcard as the terminal component for a SMB1search or
2084 NTSTATUS filename_convert_smb1_search_path(TALLOC_CTX *ctx,
2085 connection_struct *conn,
2086 const char *name_in,
2088 struct smb_filename **_smb_fname_out,
2094 struct smb_filename *smb_fname = NULL;
2095 bool posix_pathnames = (ucf_flags & UCF_POSIX_PATHNAMES);
2097 TALLOC_CTX *frame = talloc_stackframe();
2099 *_smb_fname_out = NULL;
2102 DBG_DEBUG("name_in: %s\n", name_in);
2104 if (ucf_flags & UCF_DFS_PATHNAME) {
2106 * We've been given a raw DFS pathname.
2107 * In Windows mode this is separated by '\\'
2110 * We need to remove the last component
2111 * which must be a wildcard before passing
2112 * to dfs_redirect(). But the last component
2113 * may also be a @GMT- token so we have to
2114 * remove that first.
2116 char path_sep = posix_pathnames ? '/' : '\\';
2118 char *name_in_copy = NULL;
2119 char *last_component = NULL;
2121 /* Work on a copy of name_in. */
2122 if (ucf_flags & UCF_GMT_PATHNAME) {
2123 name_in_copy = strip_gmt_from_raw_dfs(frame,
2127 ucf_flags &= ~UCF_GMT_PATHNAME;
2129 name_in_copy = talloc_strdup(frame, name_in);
2131 if (name_in_copy == NULL) {
2133 return NT_STATUS_NO_MEMORY;
2137 * Now we know that the last component is the
2138 * wildcard. Copy it and truncate to remove it.
2140 p = strrchr_m(name_in_copy, path_sep);
2142 last_component = talloc_strdup(frame, name_in_copy);
2143 name_in_copy[0] = '\0';
2145 last_component = talloc_strdup(frame, p+1);
2148 if (last_component == NULL) {
2150 return NT_STATUS_NO_MEMORY;
2153 DBG_DEBUG("name_in_copy: %s\n", name_in);
2156 * Now we can call dfs_redirect()
2157 * on the name without wildcard.
2159 status = dfs_redirect(frame,
2163 !conn->sconn->using_smb2,
2166 if (!NT_STATUS_IS_OK(status)) {
2167 DBG_DEBUG("dfs_redirect "
2168 "failed for name %s with %s\n",
2174 /* Add the last component back. */
2175 if (fname[0] == '\0') {
2176 name_in = talloc_strdup(frame, last_component);
2178 name_in = talloc_asprintf(frame,
2184 if (name_in == NULL) {
2186 return NT_STATUS_NO_MEMORY;
2188 ucf_flags &= ~UCF_DFS_PATHNAME;
2190 DBG_DEBUG("After DFS redirect name_in: %s\n", name_in);
2193 smb_fname = synthetic_smb_fname(frame,
2199 SMB_FILENAME_POSIX_PATH : 0);
2200 if (smb_fname == NULL) {
2202 return NT_STATUS_NO_MEMORY;
2205 /* Canonicalize any @GMT- paths. */
2206 status = canonicalize_snapshot_path(smb_fname, ucf_flags, twrp);
2207 if (!NT_STATUS_IS_OK(status)) {
2212 /* Get the original lcomp. */
2213 mask = get_original_lcomp(frame,
2219 return NT_STATUS_NO_MEMORY;
2222 if (mask[0] == '\0') {
2223 /* Windows and OS/2 systems treat search on the root as * */
2225 mask = talloc_strdup(frame, "*");
2228 return NT_STATUS_NO_MEMORY;
2232 DBG_DEBUG("mask = %s\n", mask);
2235 * Remove the terminal component so
2236 * filename_convert never sees the mask.
2238 p = strrchr_m(smb_fname->base_name,'/');
2240 /* filename_convert handles a '\0' base_name. */
2241 smb_fname->base_name[0] = '\0';
2246 DBG_DEBUG("For filename_convert: smb_fname = %s\n",
2247 smb_fname_str_dbg(smb_fname));
2249 /* Convert the parent directory path. */
2250 status = filename_convert(frame,
2252 smb_fname->base_name,
2257 if (NT_STATUS_IS_OK(status)) {
2258 *_smb_fname_out = talloc_move(ctx, &smb_fname);
2259 *_mask_out = talloc_move(ctx, &mask);
2261 DBG_DEBUG("filename_convert error for %s: %s\n",
2262 smb_fname_str_dbg(smb_fname),
2271 * Build the full path from a dirfsp and dirfsp relative name
2273 struct smb_filename *full_path_from_dirfsp_atname(
2274 TALLOC_CTX *mem_ctx,
2275 const struct files_struct *dirfsp,
2276 const struct smb_filename *atname)
2278 struct smb_filename *fname = NULL;
2281 if (dirfsp == dirfsp->conn->cwd_fsp ||
2282 ISDOT(dirfsp->fsp_name->base_name) ||
2283 atname->base_name[0] == '/')
2285 path = talloc_strdup(mem_ctx, atname->base_name);
2287 path = talloc_asprintf(mem_ctx, "%s/%s",
2288 dirfsp->fsp_name->base_name,
2295 fname = synthetic_smb_fname(mem_ctx,
2297 atname->stream_name,
2302 if (fname == NULL) {