s3: smbd: Correctly process SMB3 POSIX paths in create.
[Samba.git] / source3 / smbd / filename.c
blobe5cb3c867cd92e68d1c3185a0a0ec0ca94f00c9f
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 "lib/util/memcache.h"
34 uint32_t ucf_flags_from_smb_request(struct smb_request *req)
36 uint32_t ucf_flags = 0;
38 if (req == NULL) {
39 return 0;
42 if (req->posix_pathnames) {
43 ucf_flags |= UCF_POSIX_PATHNAMES;
45 if (!req->sconn->using_smb2) {
46 ucf_flags |= UCF_LCOMP_LNK_OK;
49 if (req->flags2 & FLAGS2_DFS_PATHNAMES) {
50 ucf_flags |= UCF_DFS_PATHNAME;
52 if (req->flags2 & FLAGS2_REPARSE_PATH) {
53 ucf_flags |= UCF_GMT_PATHNAME;
56 return ucf_flags;
59 uint32_t filename_create_ucf_flags(struct smb_request *req, uint32_t create_disposition)
61 uint32_t ucf_flags = 0;
63 ucf_flags |= ucf_flags_from_smb_request(req);
65 switch (create_disposition) {
66 case FILE_OPEN:
67 case FILE_OVERWRITE:
68 break;
69 case FILE_SUPERSEDE:
70 case FILE_CREATE:
71 case FILE_OPEN_IF:
72 case FILE_OVERWRITE_IF:
73 ucf_flags |= UCF_PREP_CREATEFILE;
74 break;
77 return ucf_flags;
80 /****************************************************************************
81 Mangle the 2nd name and check if it is then equal to the first name.
82 ****************************************************************************/
84 static bool mangled_equal(const char *name1,
85 const char *name2,
86 const struct share_params *p)
88 char mname[13];
90 if (!name_to_8_3(name2, mname, False, p)) {
91 return False;
93 return strequal(name1, mname);
97 * Strip a valid @GMT-token from any incoming filename path,
98 * adding any NTTIME encoded in the pathname into the
99 * twrp field of the passed in smb_fname.
101 * Valid @GMT-tokens look like @GMT-YYYY-MM-DD-HH-MM-SS
102 * at the *start* of a pathname component.
104 * If twrp is passed in then smb_fname->twrp is set to that
105 * value, and the @GMT-token part of the filename is removed
106 * and does not change the stored smb_fname->twrp.
110 NTSTATUS canonicalize_snapshot_path(struct smb_filename *smb_fname,
111 uint32_t ucf_flags,
112 NTTIME twrp)
114 bool found;
116 if (twrp != 0) {
117 smb_fname->twrp = twrp;
120 if (!(ucf_flags & UCF_GMT_PATHNAME)) {
121 return NT_STATUS_OK;
124 found = extract_snapshot_token(smb_fname->base_name, &twrp);
125 if (!found) {
126 return NT_STATUS_OK;
129 if (smb_fname->twrp == 0) {
130 smb_fname->twrp = twrp;
133 return NT_STATUS_OK;
136 static bool strnorm(char *s, int case_default)
138 if (case_default == CASE_UPPER)
139 return strupper_m(s);
140 else
141 return strlower_m(s);
145 * Utility function to normalize case on an incoming client filename
146 * if required on this connection struct.
147 * Performs an in-place case conversion guaranteed to stay the same size.
150 static NTSTATUS normalize_filename_case(connection_struct *conn,
151 char *filename,
152 uint32_t ucf_flags)
154 bool ok;
156 if (ucf_flags & UCF_POSIX_PATHNAMES) {
158 * POSIX never normalizes filename case.
160 return NT_STATUS_OK;
162 if (!conn->case_sensitive) {
163 return NT_STATUS_OK;
165 if (conn->case_preserve) {
166 return NT_STATUS_OK;
168 if (conn->short_case_preserve) {
169 return NT_STATUS_OK;
171 ok = strnorm(filename, lp_default_case(SNUM(conn)));
172 if (!ok) {
173 return NT_STATUS_INVALID_PARAMETER;
175 return NT_STATUS_OK;
178 /****************************************************************************
179 Check if two filenames are equal.
180 This needs to be careful about whether we are case sensitive.
181 ****************************************************************************/
183 static bool fname_equal(const char *name1, const char *name2,
184 bool case_sensitive)
186 /* Normal filename handling */
187 if (case_sensitive) {
188 return(strcmp(name1,name2) == 0);
191 return(strequal(name1,name2));
194 static bool sname_equal(const char *name1, const char *name2,
195 bool case_sensitive)
197 bool match;
198 const char *s1 = NULL;
199 const char *s2 = NULL;
200 size_t n1;
201 size_t n2;
202 const char *e1 = NULL;
203 const char *e2 = NULL;
204 char *c1 = NULL;
205 char *c2 = NULL;
207 match = fname_equal(name1, name2, case_sensitive);
208 if (match) {
209 return true;
212 if (name1[0] != ':') {
213 return false;
215 if (name2[0] != ':') {
216 return false;
218 s1 = &name1[1];
219 e1 = strchr(s1, ':');
220 if (e1 == NULL) {
221 n1 = strlen(s1);
222 } else {
223 n1 = PTR_DIFF(e1, s1);
225 s2 = &name2[1];
226 e2 = strchr(s2, ':');
227 if (e2 == NULL) {
228 n2 = strlen(s2);
229 } else {
230 n2 = PTR_DIFF(e2, s2);
233 /* Normal filename handling */
234 if (case_sensitive) {
235 return (strncmp(s1, s2, n1) == 0);
239 * We can't use strnequal() here
240 * as it takes the number of codepoints
241 * and not the number of bytes.
243 * So we make a copy before calling
244 * strequal().
246 * Note that we TALLOC_FREE() in reverse order
247 * in order to avoid memory fragmentation.
250 c1 = talloc_strndup(talloc_tos(), s1, n1);
251 c2 = talloc_strndup(talloc_tos(), s2, n2);
252 if (c1 == NULL || c2 == NULL) {
253 TALLOC_FREE(c2);
254 TALLOC_FREE(c1);
255 return (strncmp(s1, s2, n1) == 0);
258 match = strequal(c1, c2);
259 TALLOC_FREE(c2);
260 TALLOC_FREE(c1);
261 return match;
264 /****************************************************************************
265 Scan a directory to find a filename, matching without case sensitivity.
266 If the name looks like a mangled name then try via the mangling functions
267 ****************************************************************************/
269 NTSTATUS get_real_filename_full_scan_at(struct files_struct *dirfsp,
270 const char *name,
271 bool mangled,
272 TALLOC_CTX *mem_ctx,
273 char **found_name)
275 struct connection_struct *conn = dirfsp->conn;
276 struct smb_Dir *cur_dir = NULL;
277 const char *dname = NULL;
278 char *talloced = NULL;
279 char *unmangled_name = NULL;
280 long curpos;
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 curpos = 0;
328 while ((dname = ReadDirName(cur_dir, &curpos, NULL, &talloced))) {
330 /* Is it dot or dot dot. */
331 if (ISDOT(dname) || ISDOTDOT(dname)) {
332 TALLOC_FREE(talloced);
333 continue;
337 * At this point dname is the unmangled name.
338 * name is either mangled or not, depending on the state
339 * of the "mangled" variable. JRA.
343 * Check mangled name against mangled name, or unmangled name
344 * against unmangled name.
347 if ((mangled && mangled_equal(name,dname,conn->params)) ||
348 fname_equal(name, dname, conn->case_sensitive)) {
349 /* we've found the file, change it's name and return */
350 *found_name = talloc_strdup(mem_ctx, dname);
351 TALLOC_FREE(unmangled_name);
352 TALLOC_FREE(cur_dir);
353 if (!*found_name) {
354 TALLOC_FREE(talloced);
355 return NT_STATUS_NO_MEMORY;
357 TALLOC_FREE(talloced);
358 return NT_STATUS_OK;
360 TALLOC_FREE(talloced);
363 TALLOC_FREE(unmangled_name);
364 TALLOC_FREE(cur_dir);
365 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
368 /****************************************************************************
369 Wrapper around the vfs get_real_filename and the full directory scan
370 fallback.
371 ****************************************************************************/
373 NTSTATUS get_real_filename_at(struct files_struct *dirfsp,
374 const char *name,
375 TALLOC_CTX *mem_ctx,
376 char **found_name)
378 struct connection_struct *conn = dirfsp->conn;
379 NTSTATUS status;
380 bool mangled;
382 mangled = mangle_is_mangled(name, conn->params);
384 if (mangled) {
385 status = get_real_filename_full_scan_at(
386 dirfsp, name, mangled, mem_ctx, found_name);
387 return status;
390 /* Try the vfs first to take advantage of case-insensitive stat. */
391 status = SMB_VFS_GET_REAL_FILENAME_AT(
392 dirfsp->conn, dirfsp, name, mem_ctx, found_name);
395 * If the case-insensitive stat was successful, or returned an error
396 * other than EOPNOTSUPP then there is no need to fall back on the
397 * full directory scan.
399 if (NT_STATUS_IS_OK(status) ||
400 !NT_STATUS_EQUAL(status, NT_STATUS_NOT_SUPPORTED)) {
401 return status;
404 status = get_real_filename_full_scan_at(
405 dirfsp, name, mangled, mem_ctx, found_name);
406 return status;
410 * Create the memcache-key for GETREALFILENAME_CACHE: This supplements
411 * the stat cache for the last component to be looked up. Cache
412 * contents is the correctly capitalized translation of the parameter
413 * "name" as it exists on disk. This is indexed by inode of the dirfsp
414 * and name, and contrary to stat_cahce_lookup() it does not
415 * vfs_stat() the last component. This will be taken care of by an
416 * attempt to do a openat_pathref_fsp().
418 static bool get_real_filename_cache_key(
419 TALLOC_CTX *mem_ctx,
420 struct files_struct *dirfsp,
421 const char *name,
422 DATA_BLOB *_key)
424 struct file_id fid = vfs_file_id_from_sbuf(
425 dirfsp->conn, &dirfsp->fsp_name->st);
426 char *upper = NULL;
427 uint8_t *key = NULL;
428 size_t namelen, keylen;
430 upper = talloc_strdup_upper(mem_ctx, name);
431 if (upper == NULL) {
432 return false;
434 namelen = talloc_get_size(upper);
436 keylen = namelen + sizeof(fid);
437 if (keylen < sizeof(fid)) {
438 TALLOC_FREE(upper);
439 return false;
442 key = talloc_size(mem_ctx, keylen);
443 if (key == NULL) {
444 TALLOC_FREE(upper);
445 return false;
448 memcpy(key, &fid, sizeof(fid));
449 memcpy(key + sizeof(fid), upper, namelen);
450 TALLOC_FREE(upper);
452 *_key = (DATA_BLOB) { .data = key, .length = keylen, };
453 return true;
457 * Lightweight function to just get last component
458 * for rename / enumerate directory calls.
461 char *get_original_lcomp(TALLOC_CTX *ctx,
462 connection_struct *conn,
463 const char *filename_in,
464 uint32_t ucf_flags)
466 char *last_slash = NULL;
467 char *orig_lcomp;
468 NTSTATUS status;
470 last_slash = strrchr(filename_in, '/');
471 if (last_slash != NULL) {
472 orig_lcomp = talloc_strdup(ctx, last_slash+1);
473 } else {
474 orig_lcomp = talloc_strdup(ctx, filename_in);
476 if (orig_lcomp == NULL) {
477 return NULL;
479 status = normalize_filename_case(conn, orig_lcomp, ucf_flags);
480 if (!NT_STATUS_IS_OK(status)) {
481 TALLOC_FREE(orig_lcomp);
482 return NULL;
484 return orig_lcomp;
488 * Deal with the SMB1 semantics of sending a pathname with a
489 * wildcard as the terminal component for a SMB1search or
490 * trans2 findfirst.
493 NTSTATUS filename_convert_smb1_search_path(TALLOC_CTX *ctx,
494 connection_struct *conn,
495 char *name_in,
496 uint32_t ucf_flags,
497 struct files_struct **_dirfsp,
498 struct smb_filename **_smb_fname_out,
499 char **_mask_out)
501 NTSTATUS status;
502 char *p = NULL;
503 char *mask = NULL;
504 struct smb_filename *smb_fname = NULL;
505 NTTIME twrp = 0;
507 *_smb_fname_out = NULL;
508 *_dirfsp = NULL;
509 *_mask_out = NULL;
511 DBG_DEBUG("name_in: %s\n", name_in);
513 if (ucf_flags & UCF_GMT_PATHNAME) {
514 extract_snapshot_token(name_in, &twrp);
515 ucf_flags &= ~UCF_GMT_PATHNAME;
518 /* Get the original lcomp. */
519 mask = get_original_lcomp(ctx,
520 conn,
521 name_in,
522 ucf_flags);
523 if (mask == NULL) {
524 return NT_STATUS_NO_MEMORY;
527 if (mask[0] == '\0') {
528 /* Windows and OS/2 systems treat search on the root as * */
529 TALLOC_FREE(mask);
530 mask = talloc_strdup(ctx, "*");
531 if (mask == NULL) {
532 return NT_STATUS_NO_MEMORY;
536 DBG_DEBUG("mask = %s\n", mask);
539 * Remove the terminal component so
540 * filename_convert_dirfsp never sees the mask.
542 p = strrchr_m(name_in,'/');
543 if (p == NULL) {
544 /* filename_convert_dirfsp handles a '\0' name. */
545 name_in[0] = '\0';
546 } else {
547 *p = '\0';
550 DBG_DEBUG("For filename_convert_dirfsp: name_in = %s\n",
551 name_in);
553 /* Convert the parent directory path. */
554 status = filename_convert_dirfsp(ctx,
555 conn,
556 name_in,
557 ucf_flags,
558 twrp,
559 _dirfsp,
560 &smb_fname);
562 if (!NT_STATUS_IS_OK(status)) {
563 DBG_DEBUG("filename_convert error for %s: %s\n",
564 name_in,
565 nt_errstr(status));
568 *_smb_fname_out = talloc_move(ctx, &smb_fname);
569 *_mask_out = talloc_move(ctx, &mask);
571 return status;
575 * Get the correct capitalized stream name hanging off
576 * base_fsp. Equivalent of get_real_filename(), but for streams.
578 static NTSTATUS get_real_stream_name(
579 TALLOC_CTX *mem_ctx,
580 struct files_struct *base_fsp,
581 const char *stream_name,
582 char **_found)
584 unsigned int i, num_streams = 0;
585 struct stream_struct *streams = NULL;
586 NTSTATUS status;
588 status = vfs_fstreaminfo(
589 base_fsp, talloc_tos(), &num_streams, &streams);
590 if (!NT_STATUS_IS_OK(status)) {
591 return status;
594 for (i=0; i<num_streams; i++) {
595 bool equal = sname_equal(stream_name, streams[i].name, false);
597 DBG_DEBUG("comparing [%s] and [%s]: %sequal\n",
598 stream_name,
599 streams[i].name,
600 equal ? "" : "not ");
602 if (equal) {
603 *_found = talloc_move(mem_ctx, &streams[i].name);
604 TALLOC_FREE(streams);
605 return NT_STATUS_OK;
609 TALLOC_FREE(streams);
610 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
613 static bool filename_split_lcomp(
614 TALLOC_CTX *mem_ctx,
615 const char *name_in,
616 bool posix,
617 char **_dirname,
618 const char **_fname_rel,
619 const char **_streamname)
621 const char *lcomp = NULL;
622 const char *fname_rel = NULL;
623 const char *streamname = NULL;
624 char *dirname = NULL;
626 if (name_in[0] == '\0') {
627 fname_rel = ".";
628 dirname = talloc_strdup(mem_ctx, "");
629 if (dirname == NULL) {
630 return false;
632 goto done;
635 lcomp = strrchr_m(name_in, '/');
636 if (lcomp != NULL) {
637 fname_rel = lcomp+1;
638 dirname = talloc_strndup(mem_ctx, name_in, lcomp - name_in);
639 if (dirname == NULL) {
640 return false;
642 goto find_stream;
646 * No slash, dir is emtpy
648 dirname = talloc_strdup(mem_ctx, "");
649 if (dirname == NULL) {
650 return false;
653 if (!posix && (name_in[0] == ':')) {
655 * Special case for stream on root directory
657 fname_rel = ".";
658 streamname = name_in;
659 goto done;
662 fname_rel = name_in;
664 find_stream:
665 if (!posix) {
666 streamname = strchr_m(fname_rel, ':');
668 if (streamname != NULL) {
669 fname_rel = talloc_strndup(
670 mem_ctx,
671 fname_rel,
672 streamname - fname_rel);
673 if (fname_rel == NULL) {
674 TALLOC_FREE(dirname);
675 return false;
680 done:
681 *_dirname = dirname;
682 *_fname_rel = fname_rel;
683 *_streamname = streamname;
684 return true;
688 * Create the correct capitalization of a file name to be created.
690 static NTSTATUS filename_convert_normalize_new(
691 TALLOC_CTX *mem_ctx,
692 struct connection_struct *conn,
693 char *name_in,
694 char **_normalized)
696 char *name = name_in;
698 *_normalized = NULL;
700 if (!conn->case_preserve ||
701 (mangle_is_8_3(name, false,
702 conn->params) &&
703 !conn->short_case_preserve)) {
705 char *normalized = talloc_strdup(mem_ctx, name);
706 if (normalized == NULL) {
707 return NT_STATUS_NO_MEMORY;
710 strnorm(normalized, lp_default_case(SNUM(conn)));
711 name = normalized;
714 if (mangle_is_mangled(name, conn->params)) {
715 bool found;
716 char *unmangled = NULL;
718 found = mangle_lookup_name_from_8_3(
719 mem_ctx, name, &unmangled, conn->params);
720 if (found) {
721 name = unmangled;
725 if (name != name_in) {
726 *_normalized = name;
729 return NT_STATUS_OK;
733 * Open smb_fname_rel->fsp as a pathref fsp with a case insensitive
734 * fallback using GETREALFILENAME_CACHE and get_real_filename_at() if
735 * the first attempt based on the filename sent by the client gives
736 * ENOENT.
738 static NTSTATUS openat_pathref_fsp_case_insensitive(
739 struct files_struct *dirfsp,
740 struct smb_filename *smb_fname_rel,
741 uint32_t ucf_flags)
743 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
744 DATA_BLOB cache_key = { .data = NULL, };
745 char *found_name = NULL;
746 NTSTATUS status;
747 bool ok;
749 SET_STAT_INVALID(smb_fname_rel->st);
751 /* Check veto files - only looks at last component. */
752 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
753 DBG_DEBUG("veto files rejecting last component %s\n",
754 smb_fname_str_dbg(smb_fname_rel));
755 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
758 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
760 if (NT_STATUS_IS_OK(status)) {
761 return NT_STATUS_OK;
764 if (VALID_STAT(smb_fname_rel->st)) {
766 * We got an error although the object existed. Might
767 * be a symlink we don't want.
769 return status;
772 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
774 * Only retry on ENOENT
776 return status;
779 if (posix || dirfsp->conn->case_sensitive) {
781 * Only return case insensitive if required
783 return status;
786 if (lp_stat_cache()) {
787 char *base_name = smb_fname_rel->base_name;
788 DATA_BLOB value = { .data = NULL };
790 ok = get_real_filename_cache_key(
791 talloc_tos(), dirfsp, base_name, &cache_key);
792 if (!ok) {
794 * probably ENOMEM, just bail
796 return status;
799 DO_PROFILE_INC(statcache_lookups);
801 ok = memcache_lookup(
802 NULL, GETREALFILENAME_CACHE, cache_key, &value);
803 if (!ok) {
804 DO_PROFILE_INC(statcache_misses);
805 goto lookup;
807 DO_PROFILE_INC(statcache_hits);
809 TALLOC_FREE(smb_fname_rel->base_name);
810 smb_fname_rel->base_name = talloc_memdup(
811 smb_fname_rel, value.data, value.length);
812 if (smb_fname_rel->base_name == NULL) {
813 TALLOC_FREE(cache_key.data);
814 return NT_STATUS_NO_MEMORY;
817 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
818 DBG_DEBUG("veto files rejecting last component %s\n",
819 smb_fname_str_dbg(smb_fname_rel));
820 TALLOC_FREE(cache_key.data);
821 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
824 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
825 if (NT_STATUS_IS_OK(status)) {
826 TALLOC_FREE(cache_key.data);
827 return NT_STATUS_OK;
830 memcache_delete(NULL, GETREALFILENAME_CACHE, cache_key);
833 lookup:
834 status = get_real_filename_at(
835 dirfsp, smb_fname_rel->base_name, smb_fname_rel, &found_name);
836 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
837 (ucf_flags & UCF_PREP_CREATEFILE)) {
839 * dropbox
841 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
844 if (NT_STATUS_IS_OK(status)) {
845 TALLOC_FREE(smb_fname_rel->base_name);
846 smb_fname_rel->base_name = found_name;
848 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
849 DBG_DEBUG("veto files rejecting last component %s\n",
850 smb_fname_str_dbg(smb_fname_rel));
851 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
854 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
857 if (NT_STATUS_IS_OK(status) && (cache_key.data != NULL)) {
858 DATA_BLOB value = {
859 .data = (uint8_t *)smb_fname_rel->base_name,
860 .length = strlen(smb_fname_rel->base_name) + 1,
863 memcache_add(NULL, GETREALFILENAME_CACHE, cache_key, value);
866 TALLOC_FREE(cache_key.data);
868 return status;
871 static const char *previous_slash(const char *name_in, const char *slash)
873 const char *prev = name_in;
875 while (true) {
876 const char *next = strchr_m(prev, '/');
878 SMB_ASSERT(next != NULL); /* we have at least one slash */
880 if (next == slash) {
881 break;
884 prev = next+1;
887 if (prev == name_in) {
888 /* no previous slash */
889 return NULL;
892 return prev;
895 static char *symlink_target_path(
896 TALLOC_CTX *mem_ctx,
897 const char *name_in,
898 const char *substitute,
899 size_t unparsed)
901 size_t name_in_len = strlen(name_in);
902 const char *p_unparsed = NULL;
903 const char *parent = NULL;
904 char *ret;
906 SMB_ASSERT(unparsed <= name_in_len);
908 p_unparsed = name_in + (name_in_len - unparsed);
910 if (substitute[0] == '/') {
911 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
912 return ret;
915 if (unparsed == 0) {
916 parent = strrchr_m(name_in, '/');
917 } else {
918 parent = previous_slash(name_in, p_unparsed);
921 if (parent == NULL) {
922 /* no previous slash */
923 parent = name_in;
926 ret = talloc_asprintf(
927 mem_ctx,
928 "%.*s%s%s",
929 (int)(parent - name_in),
930 name_in,
931 substitute,
932 p_unparsed);
933 return ret;
936 static NTSTATUS safe_symlink_target_path(
937 TALLOC_CTX *mem_ctx,
938 const char *connectpath,
939 const char *name_in,
940 const char *substitute,
941 size_t unparsed,
942 char **_name_out)
944 char *target = NULL;
945 char *abs_target = NULL;
946 char *abs_target_canon = NULL;
947 const char *relative = NULL;
948 char *name_out = NULL;
949 NTSTATUS status = NT_STATUS_NO_MEMORY;
950 bool in_share;
952 target = symlink_target_path(mem_ctx, name_in, substitute, unparsed);
953 if (target == NULL) {
954 goto fail;
957 DBG_DEBUG("name_in: %s, substitute: %s, unparsed: %zu, target=%s\n",
958 name_in,
959 substitute,
960 unparsed,
961 target);
963 if (target[0] == '/') {
964 abs_target = target;
965 } else {
966 abs_target = talloc_asprintf(
967 target, "%s/%s", connectpath, target);
968 if (abs_target == NULL) {
969 goto fail;
973 abs_target_canon = canonicalize_absolute_path(target, abs_target);
974 if (abs_target_canon == NULL) {
975 goto fail;
978 DBG_DEBUG("abs_target_canon=%s\n", abs_target_canon);
980 in_share = subdir_of(
981 connectpath, strlen(connectpath), abs_target_canon, &relative);
982 if (!in_share) {
983 DBG_DEBUG("wide link to %s\n", abs_target_canon);
984 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
985 goto fail;
988 name_out = talloc_strdup(mem_ctx, relative);
989 if (name_out == NULL) {
990 goto fail;
993 status = NT_STATUS_OK;
994 *_name_out = name_out;
995 fail:
996 TALLOC_FREE(target);
997 return status;
1001 * Split up name_in as sent by the client into a directory pathref fsp
1002 * and a relative smb_filename.
1004 static NTSTATUS filename_convert_dirfsp_nosymlink(
1005 TALLOC_CTX *mem_ctx,
1006 connection_struct *conn,
1007 const char *name_in,
1008 uint32_t ucf_flags,
1009 NTTIME twrp,
1010 struct files_struct **_dirfsp,
1011 struct smb_filename **_smb_fname,
1012 char **_substitute,
1013 size_t *_unparsed)
1015 struct smb_filename *smb_dirname = NULL;
1016 struct smb_filename *smb_fname_rel = NULL;
1017 struct smb_filename *smb_fname = NULL;
1018 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
1019 char *dirname = NULL;
1020 const char *fname_rel = NULL;
1021 const char *streamname = NULL;
1022 char *saved_streamname = NULL;
1023 struct files_struct *base_fsp = NULL;
1024 bool ok;
1025 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
1027 SMB_ASSERT(!(ucf_flags & UCF_DFS_PATHNAME));
1029 if (is_fake_file_path(name_in)) {
1030 smb_fname = synthetic_smb_fname_split(mem_ctx, name_in, posix);
1031 if (smb_fname == NULL) {
1032 return NT_STATUS_NO_MEMORY;
1034 smb_fname->st = (SMB_STRUCT_STAT) { .st_ex_nlink = 1 };
1035 smb_fname->st.st_ex_btime =
1036 (struct timespec){0, SAMBA_UTIME_OMIT};
1037 smb_fname->st.st_ex_atime =
1038 (struct timespec){0, SAMBA_UTIME_OMIT};
1039 smb_fname->st.st_ex_mtime =
1040 (struct timespec){0, SAMBA_UTIME_OMIT};
1041 smb_fname->st.st_ex_ctime =
1042 (struct timespec){0, SAMBA_UTIME_OMIT};
1044 *_dirfsp = conn->cwd_fsp;
1045 *_smb_fname = smb_fname;
1046 return NT_STATUS_OK;
1050 * Catch an invalid path of "." before we
1051 * call filename_split_lcomp(). We need to
1052 * do this as filename_split_lcomp() will
1053 * use "." for the missing relative component
1054 * when an empty name_in path is sent by
1055 * the client.
1057 if (ISDOT(name_in)) {
1058 status = NT_STATUS_OBJECT_NAME_INVALID;
1059 goto fail;
1062 ok = filename_split_lcomp(
1063 talloc_tos(),
1064 name_in,
1065 posix,
1066 &dirname,
1067 &fname_rel,
1068 &streamname);
1069 if (!ok) {
1070 status = NT_STATUS_NO_MEMORY;
1071 goto fail;
1074 if ((streamname != NULL) &&
1075 ((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
1076 status = NT_STATUS_OBJECT_NAME_INVALID;
1077 goto fail;
1080 if (!posix) {
1081 bool name_has_wild = ms_has_wild(dirname);
1082 name_has_wild |= ms_has_wild(fname_rel);
1083 if (name_has_wild) {
1084 status = NT_STATUS_OBJECT_NAME_INVALID;
1085 goto fail;
1089 if (dirname[0] == '\0') {
1090 status = synthetic_pathref(
1091 mem_ctx,
1092 conn->cwd_fsp,
1093 ".",
1094 NULL,
1095 NULL,
1097 posix ? SMB_FILENAME_POSIX_PATH : 0,
1098 &smb_dirname);
1099 } else {
1100 char *substitute = NULL;
1101 size_t unparsed = 0;
1103 status = normalize_filename_case(conn, dirname, ucf_flags);
1104 if (!NT_STATUS_IS_OK(status)) {
1105 DBG_ERR("normalize_filename_case %s failed: %s\n",
1106 dirname,
1107 nt_errstr(status));
1108 goto fail;
1111 status = openat_pathref_dirfsp_nosymlink(
1112 mem_ctx,
1113 conn,
1114 dirname,
1116 posix,
1117 &smb_dirname,
1118 &unparsed,
1119 &substitute);
1121 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1123 size_t name_in_len = strlen(name_in);
1124 size_t dirname_len = strlen(dirname);
1126 SMB_ASSERT(name_in_len >= dirname_len);
1128 *_substitute = substitute;
1129 *_unparsed = unparsed + (name_in_len - dirname_len);
1131 goto fail;
1135 if (!NT_STATUS_IS_OK(status)) {
1136 DBG_DEBUG("opening directory %s failed: %s\n",
1137 dirname,
1138 nt_errstr(status));
1139 TALLOC_FREE(dirname);
1141 if (NT_STATUS_EQUAL(status, NT_STATUS_PATH_NOT_COVERED)) {
1142 /* MS-DFS error must propagate back out. */
1143 goto fail;
1146 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1148 * Except ACCESS_DENIED, everything else leads
1149 * to PATH_NOT_FOUND.
1151 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1154 goto fail;
1157 if (!VALID_STAT_OF_DIR(smb_dirname->st)) {
1158 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1159 goto fail;
1163 * Only look at bad last component values
1164 * once we know we have a valid directory. That
1165 * way we won't confuse error messages from
1166 * opening the directory path with error
1167 * messages from a bad last component.
1170 /* Relative filename can't be empty */
1171 if (fname_rel[0] == '\0') {
1172 status = NT_STATUS_OBJECT_NAME_INVALID;
1173 goto fail;
1176 /* Relative filename can't be ".." */
1177 if (ISDOTDOT(fname_rel)) {
1178 status = NT_STATUS_OBJECT_NAME_INVALID;
1179 goto fail;
1181 /* Relative name can only be dot if directory is empty. */
1182 if (ISDOT(fname_rel) && dirname[0] != '\0') {
1183 status = NT_STATUS_OBJECT_NAME_INVALID;
1184 goto fail;
1187 TALLOC_FREE(dirname);
1189 smb_fname_rel = synthetic_smb_fname(
1190 mem_ctx,
1191 fname_rel,
1192 streamname,
1193 NULL,
1194 twrp,
1195 posix ? SMB_FILENAME_POSIX_PATH : 0);
1196 if (smb_fname_rel == NULL) {
1197 status = NT_STATUS_NO_MEMORY;
1198 goto fail;
1201 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
1202 is_named_stream(smb_fname_rel)) {
1204 * Find the base_fsp first without the stream.
1206 saved_streamname = smb_fname_rel->stream_name;
1207 smb_fname_rel->stream_name = NULL;
1210 status = normalize_filename_case(
1211 conn, smb_fname_rel->base_name, ucf_flags);
1212 if (!NT_STATUS_IS_OK(status)) {
1213 DBG_ERR("normalize_filename_case %s failed: %s\n",
1214 smb_fname_rel->base_name,
1215 nt_errstr(status));
1216 goto fail;
1219 status = openat_pathref_fsp_case_insensitive(
1220 smb_dirname->fsp, smb_fname_rel, ucf_flags);
1222 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1223 VALID_STAT(smb_fname_rel->st) &&
1224 S_ISLNK(smb_fname_rel->st.st_ex_mode)) {
1227 * If we're on an MSDFS share, see if this is
1228 * an MSDFS link.
1230 if (lp_host_msdfs() &&
1231 lp_msdfs_root(SNUM(conn)) &&
1232 is_msdfs_link(smb_dirname->fsp, smb_fname_rel))
1234 status = NT_STATUS_PATH_NOT_COVERED;
1235 goto fail;
1238 #if defined(WITH_SMB1SERVER)
1240 * In SMB1 posix mode, if this is a symlink,
1241 * allow access to the name with a NULL smb_fname->fsp.
1243 if (ucf_flags & UCF_LCOMP_LNK_OK) {
1244 SMB_ASSERT(smb_fname_rel->fsp == NULL);
1245 SMB_ASSERT(streamname == NULL);
1247 smb_fname = full_path_from_dirfsp_atname(
1248 mem_ctx,
1249 smb_dirname->fsp,
1250 smb_fname_rel);
1251 if (smb_fname == NULL) {
1252 status = NT_STATUS_NO_MEMORY;
1253 goto fail;
1255 goto done;
1257 #endif
1260 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1261 !VALID_STAT(smb_fname_rel->st)) {
1263 char *normalized = NULL;
1266 * Creating a new file
1269 status = filename_convert_normalize_new(
1270 smb_fname_rel,
1271 conn,
1272 smb_fname_rel->base_name,
1273 &normalized);
1274 if (!NT_STATUS_IS_OK(status)) {
1275 DBG_DEBUG("filename_convert_normalize_new failed: "
1276 "%s\n",
1277 nt_errstr(status));
1278 goto fail;
1280 if (normalized != NULL) {
1281 smb_fname_rel->base_name = normalized;
1284 smb_fname_rel->stream_name = saved_streamname;
1286 smb_fname = full_path_from_dirfsp_atname(
1287 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1288 if (smb_fname == NULL) {
1289 status = NT_STATUS_NO_MEMORY;
1290 goto fail;
1292 goto done;
1295 if (!NT_STATUS_IS_OK(status)) {
1296 goto fail;
1299 if (saved_streamname == NULL) {
1300 /* smb_fname must be allocated off mem_ctx. */
1301 smb_fname = cp_smb_filename(mem_ctx,
1302 smb_fname_rel->fsp->fsp_name);
1303 if (smb_fname == NULL) {
1304 goto fail;
1306 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1307 if (!NT_STATUS_IS_OK(status)) {
1308 goto fail;
1310 goto done;
1313 base_fsp = smb_fname_rel->fsp;
1314 smb_fname_fsp_unlink(smb_fname_rel);
1315 SET_STAT_INVALID(smb_fname_rel->st);
1317 smb_fname_rel->stream_name = saved_streamname;
1319 status = open_stream_pathref_fsp(&base_fsp, smb_fname_rel);
1321 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1322 !conn->case_sensitive) {
1323 char *found = NULL;
1325 status = get_real_stream_name(
1326 smb_fname_rel,
1327 base_fsp,
1328 smb_fname_rel->stream_name,
1329 &found);
1331 if (NT_STATUS_IS_OK(status)) {
1332 smb_fname_rel->stream_name = found;
1333 found = NULL;
1334 status = open_stream_pathref_fsp(
1335 &base_fsp, smb_fname_rel);
1339 if (NT_STATUS_IS_OK(status)) {
1340 /* smb_fname must be allocated off mem_ctx. */
1341 smb_fname = cp_smb_filename(mem_ctx,
1342 smb_fname_rel->fsp->fsp_name);
1343 if (smb_fname == NULL) {
1344 goto fail;
1346 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1347 if (!NT_STATUS_IS_OK(status)) {
1348 goto fail;
1350 goto done;
1353 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1355 * Creating a new stream
1357 * We should save the already-open base fsp for
1358 * create_file_unixpath() somehow.
1360 smb_fname = full_path_from_dirfsp_atname(
1361 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1362 if (smb_fname == NULL) {
1363 status = NT_STATUS_NO_MEMORY;
1364 goto fail;
1367 * When open_stream_pathref_fsp() returns
1368 * NT_STATUS_OBJECT_NAME_NOT_FOUND, smb_fname_rel->fsp
1369 * has been set to NULL, so we must free base_fsp separately
1370 * to prevent fd-leaks when opening a stream that doesn't
1371 * exist.
1373 fd_close(base_fsp);
1374 file_free(NULL, base_fsp);
1375 base_fsp = NULL;
1376 goto done;
1379 if (!NT_STATUS_IS_OK(status)) {
1380 goto fail;
1383 done:
1384 *_dirfsp = smb_dirname->fsp;
1385 *_smb_fname = smb_fname;
1387 smb_fname_fsp_unlink(smb_fname_rel);
1388 TALLOC_FREE(smb_fname_rel);
1389 return NT_STATUS_OK;
1391 fail:
1393 * If open_stream_pathref_fsp() returns an error, smb_fname_rel->fsp
1394 * has been set to NULL, so we must free base_fsp separately
1395 * to prevent fd-leaks when opening a stream that doesn't
1396 * exist.
1398 if (base_fsp != NULL) {
1399 fd_close(base_fsp);
1400 file_free(NULL, base_fsp);
1401 base_fsp = NULL;
1403 TALLOC_FREE(dirname);
1404 TALLOC_FREE(smb_dirname);
1405 TALLOC_FREE(smb_fname_rel);
1406 return status;
1409 NTSTATUS filename_convert_dirfsp(
1410 TALLOC_CTX *mem_ctx,
1411 connection_struct *conn,
1412 const char *name_in,
1413 uint32_t ucf_flags,
1414 NTTIME twrp,
1415 struct files_struct **_dirfsp,
1416 struct smb_filename **_smb_fname)
1418 char *substitute = NULL;
1419 size_t unparsed = 0;
1420 NTSTATUS status;
1421 char *target = NULL;
1422 size_t symlink_redirects = 0;
1424 next:
1425 if (symlink_redirects > 40) {
1426 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1429 status = filename_convert_dirfsp_nosymlink(
1430 mem_ctx,
1431 conn,
1432 name_in,
1433 ucf_flags,
1434 twrp,
1435 _dirfsp,
1436 _smb_fname,
1437 &substitute,
1438 &unparsed);
1440 if (!NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1441 return status;
1444 if (!lp_follow_symlinks(SNUM(conn))) {
1445 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1449 * Right now, SMB2 and SMB1 always traverse symlinks
1450 * within the share. SMB1+POSIX traverses non-terminal
1451 * symlinks within the share.
1453 * When we add SMB2+POSIX we need to return
1454 * a NT_STATUS_STOPPED_ON_SYMLINK error here, using the
1455 * symlink target data read below if SMB2+POSIX has
1456 * UCF_POSIX_PATHNAMES set to cause the client to
1457 * resolve all symlinks locally.
1460 status = safe_symlink_target_path(
1461 mem_ctx,
1462 conn->connectpath,
1463 name_in,
1464 substitute,
1465 unparsed,
1466 &target);
1467 if (!NT_STATUS_IS_OK(status)) {
1468 return status;
1470 name_in = target;
1472 symlink_redirects += 1;
1474 goto next;
1478 * Build the full path from a dirfsp and dirfsp relative name
1480 struct smb_filename *full_path_from_dirfsp_atname(
1481 TALLOC_CTX *mem_ctx,
1482 const struct files_struct *dirfsp,
1483 const struct smb_filename *atname)
1485 struct smb_filename *fname = NULL;
1486 char *path = NULL;
1488 if (dirfsp == dirfsp->conn->cwd_fsp ||
1489 ISDOT(dirfsp->fsp_name->base_name) ||
1490 atname->base_name[0] == '/')
1492 path = talloc_strdup(mem_ctx, atname->base_name);
1493 } else {
1494 path = talloc_asprintf(mem_ctx, "%s/%s",
1495 dirfsp->fsp_name->base_name,
1496 atname->base_name);
1498 if (path == NULL) {
1499 return NULL;
1502 fname = synthetic_smb_fname(mem_ctx,
1503 path,
1504 atname->stream_name,
1505 &atname->st,
1506 atname->twrp,
1507 atname->flags);
1508 TALLOC_FREE(path);
1509 if (fname == NULL) {
1510 return NULL;
1513 return fname;