smbd: pass symlink target path to safe_symlink_target_path()
[Samba.git] / source3 / smbd / filename.c
bloba3392ef986fe4459d20740b7d7d50852e1c68b16
1 /*
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.
27 #include "includes.h"
28 #include "system/filesys.h"
29 #include "fake_file.h"
30 #include "smbd/smbd.h"
31 #include "smbd/globals.h"
32 #include "libcli/smb/reparse.h"
33 #include "source3/smbd/dir.h"
35 uint32_t ucf_flags_from_smb_request(struct smb_request *req)
37 uint32_t ucf_flags = 0;
39 if (req == NULL) {
40 return 0;
43 if (req->posix_pathnames) {
44 ucf_flags |= UCF_POSIX_PATHNAMES;
46 if (!req->sconn->using_smb2) {
47 ucf_flags |= UCF_LCOMP_LNK_OK;
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;
57 return ucf_flags;
60 uint32_t filename_create_ucf_flags(struct smb_request *req, uint32_t create_disposition)
62 uint32_t ucf_flags = 0;
64 ucf_flags |= ucf_flags_from_smb_request(req);
66 switch (create_disposition) {
67 case FILE_OPEN:
68 case FILE_OVERWRITE:
69 break;
70 case FILE_SUPERSEDE:
71 case FILE_CREATE:
72 case FILE_OPEN_IF:
73 case FILE_OVERWRITE_IF:
74 ucf_flags |= UCF_PREP_CREATEFILE;
75 break;
78 return ucf_flags;
81 /****************************************************************************
82 Mangle the 2nd name and check if it is then equal to the first name.
83 ****************************************************************************/
85 static bool mangled_equal(const char *name1,
86 const char *name2,
87 const struct share_params *p)
89 char mname[13];
91 if (!name_to_8_3(name2, mname, False, p)) {
92 return False;
94 return strequal(name1, mname);
98 * Strip a valid @GMT-token from any incoming filename path,
99 * adding any NTTIME encoded in the pathname into the
100 * twrp field of the passed in smb_fname.
102 * Valid @GMT-tokens look like @GMT-YYYY-MM-DD-HH-MM-SS
103 * at the *start* of a pathname component.
105 * If twrp is passed in then smb_fname->twrp is set to that
106 * value, and the @GMT-token part of the filename is removed
107 * and does not change the stored smb_fname->twrp.
111 NTSTATUS canonicalize_snapshot_path(struct smb_filename *smb_fname,
112 uint32_t ucf_flags,
113 NTTIME twrp)
115 bool found;
117 if (twrp != 0) {
118 smb_fname->twrp = twrp;
121 if (!(ucf_flags & UCF_GMT_PATHNAME)) {
122 return NT_STATUS_OK;
125 found = extract_snapshot_token(smb_fname->base_name, &twrp);
126 if (!found) {
127 return NT_STATUS_OK;
130 if (smb_fname->twrp == 0) {
131 smb_fname->twrp = twrp;
134 return NT_STATUS_OK;
137 static bool strnorm(char *s, int case_default)
139 if (case_default == CASE_UPPER)
140 return strupper_m(s);
141 else
142 return strlower_m(s);
146 * Utility function to normalize case on an incoming client filename
147 * if required on this connection struct.
148 * Performs an in-place case conversion guaranteed to stay the same size.
151 static NTSTATUS normalize_filename_case(connection_struct *conn,
152 char *filename,
153 uint32_t ucf_flags)
155 bool ok;
157 if (ucf_flags & UCF_POSIX_PATHNAMES) {
159 * POSIX never normalizes filename case.
161 return NT_STATUS_OK;
163 if (!conn->case_sensitive) {
164 return NT_STATUS_OK;
166 if (conn->case_preserve) {
167 return NT_STATUS_OK;
169 if (conn->short_case_preserve) {
170 return NT_STATUS_OK;
172 ok = strnorm(filename, lp_default_case(SNUM(conn)));
173 if (!ok) {
174 return NT_STATUS_INVALID_PARAMETER;
176 return NT_STATUS_OK;
179 /****************************************************************************
180 Check if two filenames are equal.
181 This needs to be careful about whether we are case sensitive.
182 ****************************************************************************/
184 static bool fname_equal(const char *name1, const char *name2,
185 bool case_sensitive)
187 /* Normal filename handling */
188 if (case_sensitive) {
189 return(strcmp(name1,name2) == 0);
192 return(strequal(name1,name2));
195 static bool sname_equal(const char *name1, const char *name2,
196 bool case_sensitive)
198 bool match;
199 const char *s1 = NULL;
200 const char *s2 = NULL;
201 size_t n1;
202 size_t n2;
203 const char *e1 = NULL;
204 const char *e2 = NULL;
205 char *c1 = NULL;
206 char *c2 = NULL;
208 match = fname_equal(name1, name2, case_sensitive);
209 if (match) {
210 return true;
213 if (name1[0] != ':') {
214 return false;
216 if (name2[0] != ':') {
217 return false;
219 s1 = &name1[1];
220 e1 = strchr(s1, ':');
221 if (e1 == NULL) {
222 n1 = strlen(s1);
223 } else {
224 n1 = PTR_DIFF(e1, s1);
226 s2 = &name2[1];
227 e2 = strchr(s2, ':');
228 if (e2 == NULL) {
229 n2 = strlen(s2);
230 } else {
231 n2 = PTR_DIFF(e2, s2);
234 /* Normal filename handling */
235 if (case_sensitive) {
236 return (strncmp(s1, s2, n1) == 0);
240 * We can't use strnequal() here
241 * as it takes the number of codepoints
242 * and not the number of bytes.
244 * So we make a copy before calling
245 * strequal().
247 * Note that we TALLOC_FREE() in reverse order
248 * in order to avoid memory fragmentation.
251 c1 = talloc_strndup(talloc_tos(), s1, n1);
252 c2 = talloc_strndup(talloc_tos(), s2, n2);
253 if (c1 == NULL || c2 == NULL) {
254 TALLOC_FREE(c2);
255 TALLOC_FREE(c1);
256 return (strncmp(s1, s2, n1) == 0);
259 match = strequal(c1, c2);
260 TALLOC_FREE(c2);
261 TALLOC_FREE(c1);
262 return match;
265 /****************************************************************************
266 Scan a directory to find a filename, matching without case sensitivity.
267 If the name looks like a mangled name then try via the mangling functions
268 ****************************************************************************/
270 NTSTATUS get_real_filename_full_scan_at(struct files_struct *dirfsp,
271 const char *name,
272 bool mangled,
273 TALLOC_CTX *mem_ctx,
274 char **found_name)
276 struct connection_struct *conn = dirfsp->conn;
277 struct smb_Dir *cur_dir = NULL;
278 const char *dname = NULL;
279 char *talloced = NULL;
280 char *unmangled_name = NULL;
281 NTSTATUS status;
283 /* If we have a case-sensitive filesystem, it doesn't do us any
284 * good to search for a name. If a case variation of the name was
285 * there, then the original stat(2) would have found it.
287 if (!mangled && !(conn->fs_capabilities & FILE_CASE_SENSITIVE_SEARCH)) {
288 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
292 * The incoming name can be mangled, and if we de-mangle it
293 * here it will not compare correctly against the filename (name2)
294 * read from the directory and then mangled by the name_to_8_3()
295 * call. We need to mangle both names or neither.
296 * (JRA).
298 * Fix for bug found by Dina Fine. If in case sensitive mode then
299 * the mangle cache is no good (3 letter extension could be wrong
300 * case - so don't demangle in this case - leave as mangled and
301 * allow the mangling of the directory entry read (which is done
302 * case insensitively) to match instead. This will lead to more
303 * false positive matches but we fail completely without it. JRA.
306 if (mangled && !conn->case_sensitive) {
307 mangled = !mangle_lookup_name_from_8_3(talloc_tos(), name,
308 &unmangled_name,
309 conn->params);
310 if (!mangled) {
311 /* Name is now unmangled. */
312 name = unmangled_name;
316 /* open the directory */
317 status = OpenDir_from_pathref(talloc_tos(), dirfsp, NULL, 0, &cur_dir);
318 if (!NT_STATUS_IS_OK(status)) {
319 DBG_NOTICE("scan dir didn't open dir [%s]: %s\n",
320 fsp_str_dbg(dirfsp),
321 nt_errstr(status));
322 TALLOC_FREE(unmangled_name);
323 return status;
326 /* now scan for matching names */
327 while ((dname = ReadDirName(cur_dir, &talloced))) {
329 /* Is it dot or dot dot. */
330 if (ISDOT(dname) || ISDOTDOT(dname)) {
331 TALLOC_FREE(talloced);
332 continue;
336 * At this point dname is the unmangled name.
337 * name is either mangled or not, depending on the state
338 * of the "mangled" variable. JRA.
342 * Check mangled name against mangled name, or unmangled name
343 * against unmangled name.
346 if ((mangled && mangled_equal(name,dname,conn->params)) ||
347 fname_equal(name, dname, conn->case_sensitive)) {
348 /* we've found the file, change it's name and return */
349 *found_name = talloc_strdup(mem_ctx, dname);
350 TALLOC_FREE(unmangled_name);
351 TALLOC_FREE(cur_dir);
352 if (!*found_name) {
353 TALLOC_FREE(talloced);
354 return NT_STATUS_NO_MEMORY;
356 TALLOC_FREE(talloced);
357 return NT_STATUS_OK;
359 TALLOC_FREE(talloced);
362 TALLOC_FREE(unmangled_name);
363 TALLOC_FREE(cur_dir);
364 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
367 /****************************************************************************
368 Wrapper around the vfs get_real_filename and the full directory scan
369 fallback.
370 ****************************************************************************/
372 NTSTATUS get_real_filename_at(struct files_struct *dirfsp,
373 const char *name,
374 TALLOC_CTX *mem_ctx,
375 char **found_name)
377 struct connection_struct *conn = dirfsp->conn;
378 NTSTATUS status;
379 bool mangled;
381 mangled = mangle_is_mangled(name, conn->params);
383 if (mangled) {
384 status = get_real_filename_full_scan_at(
385 dirfsp, name, mangled, mem_ctx, found_name);
386 return status;
389 /* Try the vfs first to take advantage of case-insensitive stat. */
390 status = SMB_VFS_GET_REAL_FILENAME_AT(
391 dirfsp->conn, dirfsp, name, mem_ctx, found_name);
394 * If the case-insensitive stat was successful, or returned an error
395 * other than EOPNOTSUPP then there is no need to fall back on the
396 * full directory scan.
398 if (NT_STATUS_IS_OK(status) ||
399 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
400 return status;
403 status = get_real_filename_full_scan_at(
404 dirfsp, name, mangled, mem_ctx, found_name);
405 return status;
409 * Lightweight function to just get last component
410 * for rename / enumerate directory calls.
413 char *get_original_lcomp(TALLOC_CTX *ctx,
414 connection_struct *conn,
415 const char *filename_in,
416 uint32_t ucf_flags)
418 char *last_slash = NULL;
419 char *orig_lcomp;
420 NTSTATUS status;
422 last_slash = strrchr(filename_in, '/');
423 if (last_slash != NULL) {
424 orig_lcomp = talloc_strdup(ctx, last_slash+1);
425 } else {
426 orig_lcomp = talloc_strdup(ctx, filename_in);
428 if (orig_lcomp == NULL) {
429 return NULL;
431 status = normalize_filename_case(conn, orig_lcomp, ucf_flags);
432 if (!NT_STATUS_IS_OK(status)) {
433 TALLOC_FREE(orig_lcomp);
434 return NULL;
436 return orig_lcomp;
440 * Get the correct capitalized stream name hanging off
441 * base_fsp. Equivalent of get_real_filename(), but for streams.
443 static NTSTATUS get_real_stream_name(
444 TALLOC_CTX *mem_ctx,
445 struct files_struct *base_fsp,
446 const char *stream_name,
447 char **_found)
449 unsigned int i, num_streams = 0;
450 struct stream_struct *streams = NULL;
451 NTSTATUS status;
453 status = vfs_fstreaminfo(
454 base_fsp, talloc_tos(), &num_streams, &streams);
455 if (!NT_STATUS_IS_OK(status)) {
456 return status;
459 for (i=0; i<num_streams; i++) {
460 bool equal = sname_equal(stream_name, streams[i].name, false);
462 DBG_DEBUG("comparing [%s] and [%s]: %sequal\n",
463 stream_name,
464 streams[i].name,
465 equal ? "" : "not ");
467 if (equal) {
468 *_found = talloc_move(mem_ctx, &streams[i].name);
469 TALLOC_FREE(streams);
470 return NT_STATUS_OK;
474 TALLOC_FREE(streams);
475 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
478 static bool filename_split_lcomp(
479 TALLOC_CTX *mem_ctx,
480 const char *name_in,
481 bool posix,
482 char **_dirname,
483 const char **_fname_rel,
484 const char **_streamname)
486 const char *lcomp = NULL;
487 const char *fname_rel = NULL;
488 const char *streamname = NULL;
489 char *dirname = NULL;
491 if (name_in[0] == '\0') {
492 fname_rel = ".";
493 dirname = talloc_strdup(mem_ctx, "");
494 if (dirname == NULL) {
495 return false;
497 goto done;
500 lcomp = strrchr_m(name_in, '/');
501 if (lcomp != NULL) {
502 fname_rel = lcomp+1;
503 dirname = talloc_strndup(mem_ctx, name_in, lcomp - name_in);
504 if (dirname == NULL) {
505 return false;
507 goto find_stream;
511 * No slash, dir is empty
513 dirname = talloc_strdup(mem_ctx, "");
514 if (dirname == NULL) {
515 return false;
518 if (!posix && (name_in[0] == ':')) {
520 * Special case for stream on root directory
522 fname_rel = ".";
523 streamname = name_in;
524 goto done;
527 fname_rel = name_in;
529 find_stream:
530 if (!posix) {
531 streamname = strchr_m(fname_rel, ':');
533 if (streamname != NULL) {
534 fname_rel = talloc_strndup(
535 mem_ctx,
536 fname_rel,
537 streamname - fname_rel);
538 if (fname_rel == NULL) {
539 TALLOC_FREE(dirname);
540 return false;
545 done:
546 *_dirname = dirname;
547 *_fname_rel = fname_rel;
548 *_streamname = streamname;
549 return true;
553 * Create the correct capitalization of a file name to be created.
555 static NTSTATUS filename_convert_normalize_new(
556 TALLOC_CTX *mem_ctx,
557 struct connection_struct *conn,
558 char *name_in,
559 char **_normalized)
561 char *name = name_in;
563 *_normalized = NULL;
565 if (!conn->case_preserve ||
566 (mangle_is_8_3(name, false,
567 conn->params) &&
568 !conn->short_case_preserve)) {
570 char *normalized = talloc_strdup(mem_ctx, name);
571 if (normalized == NULL) {
572 return NT_STATUS_NO_MEMORY;
575 strnorm(normalized, lp_default_case(SNUM(conn)));
576 name = normalized;
579 if (mangle_is_mangled(name, conn->params)) {
580 bool found;
581 char *unmangled = NULL;
583 found = mangle_lookup_name_from_8_3(
584 mem_ctx, name, &unmangled, conn->params);
585 if (found) {
586 name = unmangled;
590 if (name != name_in) {
591 *_normalized = name;
594 return NT_STATUS_OK;
597 static const char *previous_slash(const char *name_in, const char *slash)
599 const char *prev = NULL;
601 SMB_ASSERT((name_in <= slash) && (slash[0] == '/'));
603 prev = strchr_m(name_in, '/');
605 if (prev == slash) {
606 /* No previous slash */
607 return NULL;
610 while (true) {
611 const char *next = strchr_m(prev + 1, '/');
613 if (next == slash) {
614 return prev;
616 prev = next;
619 return NULL; /* unreachable */
622 static char *symlink_target_path(
623 TALLOC_CTX *mem_ctx,
624 const char *name_in,
625 const char *substitute,
626 size_t unparsed)
628 size_t name_in_len = strlen(name_in);
629 const char *p_unparsed = NULL;
630 const char *parent = NULL;
631 char *ret;
633 SMB_ASSERT(unparsed <= name_in_len);
635 p_unparsed = name_in + (name_in_len - unparsed);
637 if (substitute[0] == '/') {
638 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
639 return ret;
642 if (unparsed == 0) {
643 parent = strrchr_m(name_in, '/');
644 } else {
645 parent = previous_slash(name_in, p_unparsed);
648 if (parent == NULL) {
649 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
650 } else {
651 ret = talloc_asprintf(mem_ctx,
652 "%.*s/%s%s",
653 (int)(parent - name_in),
654 name_in,
655 substitute,
656 p_unparsed);
659 return ret;
662 NTSTATUS safe_symlink_target_path(TALLOC_CTX *mem_ctx,
663 const char *connectpath,
664 const char *target,
665 size_t unparsed,
666 char **_relative)
668 char *abs_target = NULL;
669 char *abs_target_canon = NULL;
670 const char *relative = NULL;
671 bool in_share;
672 NTSTATUS status = NT_STATUS_NO_MEMORY;
674 DBG_DEBUG("connectpath [%s] target [%s] unparsed [%zu]\n",
675 connectpath, target, unparsed);
677 if (target[0] == '/') {
678 abs_target = talloc_strdup(mem_ctx, target);
679 } else {
680 abs_target = talloc_asprintf(mem_ctx,
681 "%s/%s",
682 connectpath,
683 target);
685 if (abs_target == NULL) {
686 goto fail;
689 abs_target_canon = canonicalize_absolute_path(abs_target, abs_target);
690 if (abs_target_canon == NULL) {
691 goto fail;
694 DBG_DEBUG("abs_target_canon=%s\n", abs_target_canon);
696 in_share = subdir_of(
697 connectpath, strlen(connectpath), abs_target_canon, &relative);
698 if (!in_share) {
699 DBG_DEBUG("wide link to %s\n", abs_target_canon);
700 status = (unparsed != 0) ? NT_STATUS_OBJECT_PATH_NOT_FOUND
701 : NT_STATUS_OBJECT_NAME_NOT_FOUND;
702 goto fail;
705 *_relative = talloc_strdup(mem_ctx, relative);
706 if (*_relative == NULL) {
707 goto fail;
710 status = NT_STATUS_OK;
711 fail:
712 TALLOC_FREE(abs_target);
713 return status;
717 * Split up name_in as sent by the client into a directory pathref fsp
718 * and a relative smb_filename.
720 static NTSTATUS filename_convert_dirfsp_nosymlink(
721 TALLOC_CTX *mem_ctx,
722 connection_struct *conn,
723 const char *name_in,
724 uint32_t ucf_flags,
725 NTTIME twrp,
726 struct files_struct **_dirfsp,
727 struct smb_filename **_smb_fname,
728 struct open_symlink_err **_symlink_err)
730 struct smb_filename *smb_dirname = NULL;
731 struct smb_filename *smb_fname_rel = NULL;
732 struct smb_filename *smb_fname = NULL;
733 struct open_symlink_err *symlink_err = NULL;
734 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
735 char *dirname = NULL;
736 const char *fname_rel = NULL;
737 const char *streamname = NULL;
738 char *saved_streamname = NULL;
739 struct files_struct *base_fsp = NULL;
740 bool ok;
741 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
743 SMB_ASSERT(!(ucf_flags & UCF_DFS_PATHNAME));
745 if (is_fake_file_path(name_in)) {
746 smb_fname = synthetic_smb_fname_split(mem_ctx, name_in, posix);
747 if (smb_fname == NULL) {
748 return NT_STATUS_NO_MEMORY;
750 smb_fname->st = (SMB_STRUCT_STAT){
751 .st_ex_nlink = 1,
752 .st_ex_mode = S_IFREG | 0644,
754 smb_fname->st.st_ex_btime =
755 (struct timespec){0, SAMBA_UTIME_OMIT};
756 smb_fname->st.st_ex_atime =
757 (struct timespec){0, SAMBA_UTIME_OMIT};
758 smb_fname->st.st_ex_mtime =
759 (struct timespec){0, SAMBA_UTIME_OMIT};
760 smb_fname->st.st_ex_ctime =
761 (struct timespec){0, SAMBA_UTIME_OMIT};
763 *_dirfsp = conn->cwd_fsp;
764 *_smb_fname = smb_fname;
765 return NT_STATUS_OK;
769 * Catch an invalid path of "." before we
770 * call filename_split_lcomp(). We need to
771 * do this as filename_split_lcomp() will
772 * use "." for the missing relative component
773 * when an empty name_in path is sent by
774 * the client.
776 if (ISDOT(name_in)) {
777 status = NT_STATUS_OBJECT_NAME_INVALID;
778 goto fail;
781 ok = filename_split_lcomp(
782 talloc_tos(),
783 name_in,
784 posix,
785 &dirname,
786 &fname_rel,
787 &streamname);
788 if (!ok) {
789 status = NT_STATUS_NO_MEMORY;
790 goto fail;
793 if ((streamname != NULL) &&
794 ((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
795 status = NT_STATUS_OBJECT_NAME_INVALID;
796 goto fail;
799 if (!posix) {
800 bool name_has_wild = ms_has_wild(dirname);
801 name_has_wild |= ms_has_wild(fname_rel);
802 if (name_has_wild) {
803 status = NT_STATUS_OBJECT_NAME_INVALID;
804 goto fail;
808 if (dirname[0] == '\0') {
809 status = synthetic_pathref(
810 mem_ctx,
811 conn->cwd_fsp,
812 ".",
813 NULL,
814 NULL,
816 posix ? SMB_FILENAME_POSIX_PATH : 0,
817 &smb_dirname);
818 } else {
819 status = normalize_filename_case(conn, dirname, ucf_flags);
820 if (!NT_STATUS_IS_OK(status)) {
821 DBG_ERR("normalize_filename_case %s failed: %s\n",
822 dirname,
823 nt_errstr(status));
824 goto fail;
827 status = openat_pathref_fsp_nosymlink(mem_ctx,
828 conn,
829 conn->cwd_fsp,
830 dirname,
831 twrp,
832 posix,
833 &smb_dirname,
834 &symlink_err);
836 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
837 size_t name_in_len, dirname_len;
839 name_in_len = strlen(name_in);
840 dirname_len = strlen(dirname);
842 SMB_ASSERT(name_in_len >= dirname_len);
844 symlink_err->unparsed += (name_in_len - dirname_len);
845 *_symlink_err = symlink_err;
847 goto fail;
851 if (!NT_STATUS_IS_OK(status)) {
852 DBG_DEBUG("opening directory %s failed: %s\n",
853 dirname,
854 nt_errstr(status));
855 TALLOC_FREE(dirname);
857 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
859 * Except ACCESS_DENIED, everything else leads
860 * to PATH_NOT_FOUND.
862 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
865 goto fail;
868 if (!VALID_STAT_OF_DIR(smb_dirname->st)) {
869 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
870 goto fail;
872 smb_dirname->fsp->fsp_flags.is_directory = true;
875 * Only look at bad last component values
876 * once we know we have a valid directory. That
877 * way we won't confuse error messages from
878 * opening the directory path with error
879 * messages from a bad last component.
882 /* Relative filename can't be empty */
883 if (fname_rel[0] == '\0') {
884 status = NT_STATUS_OBJECT_NAME_INVALID;
885 goto fail;
888 /* Relative filename can't be ".." */
889 if (ISDOTDOT(fname_rel)) {
890 status = NT_STATUS_OBJECT_NAME_INVALID;
891 goto fail;
893 /* Relative name can only be dot if directory is empty. */
894 if (ISDOT(fname_rel) && dirname[0] != '\0') {
895 status = NT_STATUS_OBJECT_NAME_INVALID;
896 goto fail;
899 TALLOC_FREE(dirname);
901 smb_fname_rel = synthetic_smb_fname(
902 mem_ctx,
903 fname_rel,
904 streamname,
905 NULL,
906 twrp,
907 posix ? SMB_FILENAME_POSIX_PATH : 0);
908 if (smb_fname_rel == NULL) {
909 status = NT_STATUS_NO_MEMORY;
910 goto fail;
913 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
914 is_named_stream(smb_fname_rel)) {
916 * Find the base_fsp first without the stream.
918 saved_streamname = smb_fname_rel->stream_name;
919 smb_fname_rel->stream_name = NULL;
922 status = normalize_filename_case(
923 conn, smb_fname_rel->base_name, ucf_flags);
924 if (!NT_STATUS_IS_OK(status)) {
925 DBG_ERR("normalize_filename_case %s failed: %s\n",
926 smb_fname_rel->base_name,
927 nt_errstr(status));
928 goto fail;
931 status = openat_pathref_fsp_lcomp(smb_dirname->fsp,
932 smb_fname_rel,
933 ucf_flags);
935 if (NT_STATUS_IS_OK(status) && S_ISLNK(smb_fname_rel->st.st_ex_mode)) {
938 * Upper layers might need the link target. Here we
939 * still have the relname around, get the symlink err.
941 status = create_open_symlink_err(mem_ctx,
942 smb_dirname->fsp,
943 smb_fname_rel,
944 &symlink_err);
945 if (!NT_STATUS_IS_OK(status)) {
946 DBG_DEBUG("Could not read symlink for %s: %s\n",
947 smb_fname_str_dbg(
948 smb_fname_rel->fsp->fsp_name),
949 nt_errstr(status));
950 goto fail;
954 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
955 !VALID_STAT(smb_fname_rel->st)) {
957 char *normalized = NULL;
960 * Creating a new file
963 status = filename_convert_normalize_new(
964 smb_fname_rel,
965 conn,
966 smb_fname_rel->base_name,
967 &normalized);
968 if (!NT_STATUS_IS_OK(status)) {
969 DBG_DEBUG("filename_convert_normalize_new failed: "
970 "%s\n",
971 nt_errstr(status));
972 goto fail;
974 if (normalized != NULL) {
975 smb_fname_rel->base_name = normalized;
978 smb_fname_rel->stream_name = saved_streamname;
980 smb_fname = full_path_from_dirfsp_atname(
981 mem_ctx, smb_dirname->fsp, smb_fname_rel);
982 if (smb_fname == NULL) {
983 status = NT_STATUS_NO_MEMORY;
984 goto fail;
986 goto done;
989 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_OPEN_RESTRICTION)) {
990 /* A vetoed file, pretend it's not there */
991 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
993 if (!NT_STATUS_IS_OK(status)) {
994 goto fail;
997 if (saved_streamname == NULL) {
998 /* smb_fname must be allocated off mem_ctx. */
999 smb_fname = cp_smb_filename(mem_ctx,
1000 smb_fname_rel->fsp->fsp_name);
1001 if (smb_fname == NULL) {
1002 goto fail;
1004 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1005 if (!NT_STATUS_IS_OK(status)) {
1006 goto fail;
1008 goto done;
1011 base_fsp = smb_fname_rel->fsp;
1012 smb_fname_fsp_unlink(smb_fname_rel);
1013 SET_STAT_INVALID(smb_fname_rel->st);
1015 smb_fname_rel->stream_name = saved_streamname;
1017 status = open_stream_pathref_fsp(&base_fsp, smb_fname_rel);
1019 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1020 !conn->case_sensitive) {
1021 char *found = NULL;
1023 status = get_real_stream_name(
1024 smb_fname_rel,
1025 base_fsp,
1026 smb_fname_rel->stream_name,
1027 &found);
1029 if (NT_STATUS_IS_OK(status)) {
1030 smb_fname_rel->stream_name = found;
1031 found = NULL;
1032 status = open_stream_pathref_fsp(
1033 &base_fsp, smb_fname_rel);
1037 if (NT_STATUS_IS_OK(status)) {
1038 /* smb_fname must be allocated off mem_ctx. */
1039 smb_fname = cp_smb_filename(mem_ctx,
1040 smb_fname_rel->fsp->fsp_name);
1041 if (smb_fname == NULL) {
1042 goto fail;
1044 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1045 if (!NT_STATUS_IS_OK(status)) {
1046 goto fail;
1048 goto done;
1051 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1053 * Creating a new stream
1055 * We should save the already-open base fsp for
1056 * create_file_unixpath() somehow.
1058 smb_fname = full_path_from_dirfsp_atname(
1059 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1060 if (smb_fname == NULL) {
1061 status = NT_STATUS_NO_MEMORY;
1062 goto fail;
1065 * When open_stream_pathref_fsp() returns
1066 * NT_STATUS_OBJECT_NAME_NOT_FOUND, smb_fname_rel->fsp
1067 * has been set to NULL, so we must free base_fsp separately
1068 * to prevent fd-leaks when opening a stream that doesn't
1069 * exist.
1071 fd_close(base_fsp);
1072 file_free(NULL, base_fsp);
1073 base_fsp = NULL;
1074 goto done;
1077 if (!NT_STATUS_IS_OK(status)) {
1078 goto fail;
1081 done:
1082 *_dirfsp = smb_dirname->fsp;
1083 *_smb_fname = smb_fname;
1084 *_symlink_err = symlink_err;
1086 smb_fname_fsp_unlink(smb_fname_rel);
1087 TALLOC_FREE(smb_fname_rel);
1088 return NT_STATUS_OK;
1090 fail:
1092 * If open_stream_pathref_fsp() returns an error, smb_fname_rel->fsp
1093 * has been set to NULL, so we must free base_fsp separately
1094 * to prevent fd-leaks when opening a stream that doesn't
1095 * exist.
1097 if (base_fsp != NULL) {
1098 fd_close(base_fsp);
1099 file_free(NULL, base_fsp);
1100 base_fsp = NULL;
1102 TALLOC_FREE(dirname);
1103 TALLOC_FREE(smb_dirname);
1104 TALLOC_FREE(smb_fname_rel);
1105 return status;
1108 NTSTATUS filename_convert_dirfsp(
1109 TALLOC_CTX *mem_ctx,
1110 connection_struct *conn,
1111 const char *name_in,
1112 uint32_t ucf_flags,
1113 NTTIME twrp,
1114 struct files_struct **_dirfsp,
1115 struct smb_filename **_smb_fname)
1117 struct open_symlink_err *symlink_err = NULL;
1118 NTSTATUS status;
1119 char *target = NULL;
1120 char *safe_target = NULL;
1121 size_t symlink_redirects = 0;
1123 next:
1124 if (symlink_redirects > 40) {
1125 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1128 status = filename_convert_dirfsp_nosymlink(mem_ctx,
1129 conn,
1130 name_in,
1131 ucf_flags,
1132 twrp,
1133 _dirfsp,
1134 _smb_fname,
1135 &symlink_err);
1137 if (NT_STATUS_IS_OK(status) && S_ISLNK((*_smb_fname)->st.st_ex_mode)) {
1139 * lcomp is a symlink
1141 if (ucf_flags & UCF_LCOMP_LNK_OK) {
1142 TALLOC_FREE(symlink_err);
1143 return NT_STATUS_OK;
1145 close_file_free(NULL, _dirfsp, ERROR_CLOSE);
1146 status = NT_STATUS_STOPPED_ON_SYMLINK;
1149 if (!NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1150 return status;
1154 * If we're on an MSDFS share, see if this is
1155 * an MSDFS link.
1157 if (lp_host_msdfs() && lp_msdfs_root(SNUM(conn)) &&
1158 strnequal(symlink_err->reparse->substitute_name, "msdfs:", 6))
1160 TALLOC_FREE(*_smb_fname);
1161 TALLOC_FREE(symlink_err);
1162 return NT_STATUS_PATH_NOT_COVERED;
1165 if (!lp_follow_symlinks(SNUM(conn))) {
1166 status = (symlink_err->unparsed == 0)
1167 ? NT_STATUS_OBJECT_NAME_NOT_FOUND
1168 : NT_STATUS_OBJECT_PATH_NOT_FOUND;
1169 TALLOC_FREE(symlink_err);
1170 return status;
1174 * Right now, SMB2 and SMB1 always traverse symlinks
1175 * within the share. SMB1+POSIX traverses non-terminal
1176 * symlinks within the share.
1178 * When we add SMB2+POSIX we need to return
1179 * a NT_STATUS_STOPPED_ON_SYMLINK error here, using the
1180 * symlink target data read below if SMB2+POSIX has
1181 * UCF_POSIX_PATHNAMES set to cause the client to
1182 * resolve all symlinks locally.
1185 target = symlink_target_path(mem_ctx,
1186 name_in,
1187 symlink_err->reparse->substitute_name,
1188 symlink_err->unparsed);
1189 if (target == NULL) {
1190 return NT_STATUS_NO_MEMORY;
1193 status = safe_symlink_target_path(mem_ctx,
1194 conn->connectpath,
1195 target,
1196 symlink_err->unparsed,
1197 &safe_target);
1198 TALLOC_FREE(symlink_err);
1199 if (!NT_STATUS_IS_OK(status)) {
1200 return status;
1202 name_in = safe_target;
1204 symlink_redirects += 1;
1206 goto next;
1209 char *full_path_from_dirfsp_at_basename(TALLOC_CTX *mem_ctx,
1210 const struct files_struct *dirfsp,
1211 const char *at_base_name)
1213 char *path = NULL;
1215 if (dirfsp == dirfsp->conn->cwd_fsp ||
1216 ISDOT(dirfsp->fsp_name->base_name) || at_base_name[0] == '/') {
1217 path = talloc_strdup(mem_ctx, at_base_name);
1218 } else {
1219 path = talloc_asprintf(mem_ctx,
1220 "%s/%s",
1221 dirfsp->fsp_name->base_name,
1222 at_base_name);
1225 return path;
1229 * Build the full path from a dirfsp and dirfsp relative name
1231 struct smb_filename *
1232 full_path_from_dirfsp_atname(TALLOC_CTX *mem_ctx,
1233 const struct files_struct *dirfsp,
1234 const struct smb_filename *atname)
1236 struct smb_filename *fname = NULL;
1237 char *path = NULL;
1239 path = full_path_from_dirfsp_at_basename(mem_ctx,
1240 dirfsp,
1241 atname->base_name);
1242 if (path == NULL) {
1243 return NULL;
1246 fname = synthetic_smb_fname(mem_ctx,
1247 path,
1248 atname->stream_name,
1249 &atname->st,
1250 atname->twrp,
1251 atname->flags);
1252 TALLOC_FREE(path);
1253 if (fname == NULL) {
1254 return NULL;
1257 return fname;