smbd: Make get_real_filename_cache_key() public
[Samba.git] / source3 / smbd / filename.c
blob552940f60d70355ce65d96f650a0d61045c82f81
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"
33 #include "libcli/smb/reparse.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 * Create the memcache-key for GETREALFILENAME_CACHE: This supplements
410 * the stat cache for the last component to be looked up. Cache
411 * contents is the correctly capitalized translation of the parameter
412 * "name" as it exists on disk. This is indexed by inode of the dirfsp
413 * and name, and contrary to stat_cahce_lookup() it does not
414 * vfs_stat() the last component. This will be taken care of by an
415 * attempt to do a openat_pathref_fsp().
417 bool get_real_filename_cache_key(TALLOC_CTX *mem_ctx,
418 struct files_struct *dirfsp,
419 const char *name,
420 DATA_BLOB *_key)
422 struct file_id fid = vfs_file_id_from_sbuf(
423 dirfsp->conn, &dirfsp->fsp_name->st);
424 char *upper = NULL;
425 uint8_t *key = NULL;
426 size_t namelen, keylen;
428 upper = talloc_strdup_upper(mem_ctx, name);
429 if (upper == NULL) {
430 return false;
432 namelen = talloc_get_size(upper);
434 keylen = namelen + sizeof(fid);
435 if (keylen < sizeof(fid)) {
436 TALLOC_FREE(upper);
437 return false;
440 key = talloc_size(mem_ctx, keylen);
441 if (key == NULL) {
442 TALLOC_FREE(upper);
443 return false;
446 memcpy(key, &fid, sizeof(fid));
447 memcpy(key + sizeof(fid), upper, namelen);
448 TALLOC_FREE(upper);
450 *_key = (DATA_BLOB) { .data = key, .length = keylen, };
451 return true;
455 * Lightweight function to just get last component
456 * for rename / enumerate directory calls.
459 char *get_original_lcomp(TALLOC_CTX *ctx,
460 connection_struct *conn,
461 const char *filename_in,
462 uint32_t ucf_flags)
464 char *last_slash = NULL;
465 char *orig_lcomp;
466 NTSTATUS status;
468 last_slash = strrchr(filename_in, '/');
469 if (last_slash != NULL) {
470 orig_lcomp = talloc_strdup(ctx, last_slash+1);
471 } else {
472 orig_lcomp = talloc_strdup(ctx, filename_in);
474 if (orig_lcomp == NULL) {
475 return NULL;
477 status = normalize_filename_case(conn, orig_lcomp, ucf_flags);
478 if (!NT_STATUS_IS_OK(status)) {
479 TALLOC_FREE(orig_lcomp);
480 return NULL;
482 return orig_lcomp;
486 * Get the correct capitalized stream name hanging off
487 * base_fsp. Equivalent of get_real_filename(), but for streams.
489 static NTSTATUS get_real_stream_name(
490 TALLOC_CTX *mem_ctx,
491 struct files_struct *base_fsp,
492 const char *stream_name,
493 char **_found)
495 unsigned int i, num_streams = 0;
496 struct stream_struct *streams = NULL;
497 NTSTATUS status;
499 status = vfs_fstreaminfo(
500 base_fsp, talloc_tos(), &num_streams, &streams);
501 if (!NT_STATUS_IS_OK(status)) {
502 return status;
505 for (i=0; i<num_streams; i++) {
506 bool equal = sname_equal(stream_name, streams[i].name, false);
508 DBG_DEBUG("comparing [%s] and [%s]: %sequal\n",
509 stream_name,
510 streams[i].name,
511 equal ? "" : "not ");
513 if (equal) {
514 *_found = talloc_move(mem_ctx, &streams[i].name);
515 TALLOC_FREE(streams);
516 return NT_STATUS_OK;
520 TALLOC_FREE(streams);
521 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
524 static bool filename_split_lcomp(
525 TALLOC_CTX *mem_ctx,
526 const char *name_in,
527 bool posix,
528 char **_dirname,
529 const char **_fname_rel,
530 const char **_streamname)
532 const char *lcomp = NULL;
533 const char *fname_rel = NULL;
534 const char *streamname = NULL;
535 char *dirname = NULL;
537 if (name_in[0] == '\0') {
538 fname_rel = ".";
539 dirname = talloc_strdup(mem_ctx, "");
540 if (dirname == NULL) {
541 return false;
543 goto done;
546 lcomp = strrchr_m(name_in, '/');
547 if (lcomp != NULL) {
548 fname_rel = lcomp+1;
549 dirname = talloc_strndup(mem_ctx, name_in, lcomp - name_in);
550 if (dirname == NULL) {
551 return false;
553 goto find_stream;
557 * No slash, dir is empty
559 dirname = talloc_strdup(mem_ctx, "");
560 if (dirname == NULL) {
561 return false;
564 if (!posix && (name_in[0] == ':')) {
566 * Special case for stream on root directory
568 fname_rel = ".";
569 streamname = name_in;
570 goto done;
573 fname_rel = name_in;
575 find_stream:
576 if (!posix) {
577 streamname = strchr_m(fname_rel, ':');
579 if (streamname != NULL) {
580 fname_rel = talloc_strndup(
581 mem_ctx,
582 fname_rel,
583 streamname - fname_rel);
584 if (fname_rel == NULL) {
585 TALLOC_FREE(dirname);
586 return false;
591 done:
592 *_dirname = dirname;
593 *_fname_rel = fname_rel;
594 *_streamname = streamname;
595 return true;
599 * Create the correct capitalization of a file name to be created.
601 static NTSTATUS filename_convert_normalize_new(
602 TALLOC_CTX *mem_ctx,
603 struct connection_struct *conn,
604 char *name_in,
605 char **_normalized)
607 char *name = name_in;
609 *_normalized = NULL;
611 if (!conn->case_preserve ||
612 (mangle_is_8_3(name, false,
613 conn->params) &&
614 !conn->short_case_preserve)) {
616 char *normalized = talloc_strdup(mem_ctx, name);
617 if (normalized == NULL) {
618 return NT_STATUS_NO_MEMORY;
621 strnorm(normalized, lp_default_case(SNUM(conn)));
622 name = normalized;
625 if (mangle_is_mangled(name, conn->params)) {
626 bool found;
627 char *unmangled = NULL;
629 found = mangle_lookup_name_from_8_3(
630 mem_ctx, name, &unmangled, conn->params);
631 if (found) {
632 name = unmangled;
636 if (name != name_in) {
637 *_normalized = name;
640 return NT_STATUS_OK;
644 * Open smb_fname_rel->fsp as a pathref fsp with a case insensitive
645 * fallback using GETREALFILENAME_CACHE and get_real_filename_at() if
646 * the first attempt based on the filename sent by the client gives
647 * ENOENT.
649 static NTSTATUS openat_pathref_fsp_case_insensitive(
650 struct files_struct *dirfsp,
651 struct smb_filename *smb_fname_rel,
652 uint32_t ucf_flags)
654 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
655 DATA_BLOB cache_key = { .data = NULL, };
656 char *found_name = NULL;
657 NTSTATUS status;
658 bool ok;
660 SET_STAT_INVALID(smb_fname_rel->st);
662 /* Check veto files - only looks at last component. */
663 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
664 DBG_DEBUG("veto files rejecting last component %s\n",
665 smb_fname_str_dbg(smb_fname_rel));
666 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
669 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
671 if (NT_STATUS_IS_OK(status)) {
672 return NT_STATUS_OK;
675 if (VALID_STAT(smb_fname_rel->st)) {
677 * We got an error although the object existed. Might
678 * be a symlink we don't want.
680 return status;
683 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
685 * Only retry on ENOENT
687 return status;
690 if (posix || dirfsp->conn->case_sensitive) {
692 * Only return case insensitive if required
694 return status;
697 if (lp_stat_cache()) {
698 char *base_name = smb_fname_rel->base_name;
699 char *original_relname = NULL;
700 DATA_BLOB value = { .data = NULL };
702 ok = get_real_filename_cache_key(
703 talloc_tos(), dirfsp, base_name, &cache_key);
704 if (!ok) {
706 * probably ENOMEM, just bail
708 return status;
711 DO_PROFILE_INC(statcache_lookups);
713 ok = memcache_lookup(
714 NULL, GETREALFILENAME_CACHE, cache_key, &value);
715 if (!ok) {
716 DO_PROFILE_INC(statcache_misses);
717 goto lookup;
719 DO_PROFILE_INC(statcache_hits);
722 * For the "new filename" case we need to preserve the
723 * capitalization the client sent us, see
724 * https://bugzilla.samba.org/show_bug.cgi?id=15481
726 original_relname = smb_fname_rel->base_name;
728 smb_fname_rel->base_name = talloc_memdup(
729 smb_fname_rel, value.data, value.length);
730 if (smb_fname_rel->base_name == NULL) {
731 TALLOC_FREE(cache_key.data);
732 return NT_STATUS_NO_MEMORY;
735 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
736 DBG_DEBUG("veto files rejecting last component %s\n",
737 smb_fname_str_dbg(smb_fname_rel));
738 TALLOC_FREE(cache_key.data);
739 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
742 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
743 if (NT_STATUS_IS_OK(status)) {
744 TALLOC_FREE(cache_key.data);
745 TALLOC_FREE(original_relname);
746 return NT_STATUS_OK;
749 memcache_delete(NULL, GETREALFILENAME_CACHE, cache_key);
750 TALLOC_FREE(smb_fname_rel->base_name);
751 smb_fname_rel->base_name = original_relname;
754 lookup:
755 status = get_real_filename_at(
756 dirfsp, smb_fname_rel->base_name, smb_fname_rel, &found_name);
757 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
758 (ucf_flags & UCF_PREP_CREATEFILE)) {
760 * dropbox
762 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
765 if (NT_STATUS_IS_OK(status)) {
766 TALLOC_FREE(smb_fname_rel->base_name);
767 smb_fname_rel->base_name = found_name;
769 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
770 DBG_DEBUG("veto files rejecting last component %s\n",
771 smb_fname_str_dbg(smb_fname_rel));
772 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
775 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
778 if (NT_STATUS_IS_OK(status) && (cache_key.data != NULL)) {
779 DATA_BLOB value = {
780 .data = (uint8_t *)smb_fname_rel->base_name,
781 .length = strlen(smb_fname_rel->base_name) + 1,
784 memcache_add(NULL, GETREALFILENAME_CACHE, cache_key, value);
787 TALLOC_FREE(cache_key.data);
789 return status;
792 static const char *previous_slash(const char *name_in, const char *slash)
794 const char *prev = NULL;
796 SMB_ASSERT((name_in <= slash) && (slash[0] == '/'));
798 prev = strchr_m(name_in, '/');
800 if (prev == slash) {
801 /* No previous slash */
802 return NULL;
805 while (true) {
806 const char *next = strchr_m(prev + 1, '/');
808 if (next == slash) {
809 return prev;
811 prev = next;
814 return NULL; /* unreachable */
817 static char *symlink_target_path(
818 TALLOC_CTX *mem_ctx,
819 const char *name_in,
820 const char *substitute,
821 size_t unparsed)
823 size_t name_in_len = strlen(name_in);
824 const char *p_unparsed = NULL;
825 const char *parent = NULL;
826 char *ret;
828 SMB_ASSERT(unparsed <= name_in_len);
830 p_unparsed = name_in + (name_in_len - unparsed);
832 if (substitute[0] == '/') {
833 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
834 return ret;
837 if (unparsed == 0) {
838 parent = strrchr_m(name_in, '/');
839 } else {
840 parent = previous_slash(name_in, p_unparsed);
843 if (parent == NULL) {
844 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
845 } else {
846 ret = talloc_asprintf(mem_ctx,
847 "%.*s/%s%s",
848 (int)(parent - name_in),
849 name_in,
850 substitute,
851 p_unparsed);
854 return ret;
857 static NTSTATUS safe_symlink_target_path(
858 TALLOC_CTX *mem_ctx,
859 const char *connectpath,
860 const char *name_in,
861 const char *substitute,
862 size_t unparsed,
863 char **_name_out)
865 char *target = NULL;
866 char *abs_target = NULL;
867 char *abs_target_canon = NULL;
868 const char *relative = NULL;
869 char *name_out = NULL;
870 NTSTATUS status = NT_STATUS_NO_MEMORY;
871 bool in_share;
873 target = symlink_target_path(mem_ctx, name_in, substitute, unparsed);
874 if (target == NULL) {
875 goto fail;
878 DBG_DEBUG("name_in: %s, substitute: %s, unparsed: %zu, target=%s\n",
879 name_in,
880 substitute,
881 unparsed,
882 target);
884 if (target[0] == '/') {
885 abs_target = target;
886 } else {
887 abs_target = talloc_asprintf(
888 target, "%s/%s", connectpath, target);
889 if (abs_target == NULL) {
890 goto fail;
894 abs_target_canon = canonicalize_absolute_path(target, abs_target);
895 if (abs_target_canon == NULL) {
896 goto fail;
899 DBG_DEBUG("abs_target_canon=%s\n", abs_target_canon);
901 in_share = subdir_of(
902 connectpath, strlen(connectpath), abs_target_canon, &relative);
903 if (!in_share) {
904 DBG_DEBUG("wide link to %s\n", abs_target_canon);
905 status = (unparsed != 0) ? NT_STATUS_OBJECT_PATH_NOT_FOUND
906 : NT_STATUS_OBJECT_NAME_NOT_FOUND;
907 goto fail;
910 name_out = talloc_strdup(mem_ctx, relative);
911 if (name_out == NULL) {
912 goto fail;
915 status = NT_STATUS_OK;
916 *_name_out = name_out;
917 fail:
918 TALLOC_FREE(target);
919 return status;
923 * Split up name_in as sent by the client into a directory pathref fsp
924 * and a relative smb_filename.
926 static NTSTATUS filename_convert_dirfsp_nosymlink(
927 TALLOC_CTX *mem_ctx,
928 connection_struct *conn,
929 const char *name_in,
930 uint32_t ucf_flags,
931 NTTIME twrp,
932 struct files_struct **_dirfsp,
933 struct smb_filename **_smb_fname,
934 struct open_symlink_err **_symlink_err)
936 struct smb_filename *smb_dirname = NULL;
937 struct smb_filename *smb_fname_rel = NULL;
938 struct smb_filename *smb_fname = NULL;
939 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
940 char *dirname = NULL;
941 const char *fname_rel = NULL;
942 const char *streamname = NULL;
943 char *saved_streamname = NULL;
944 struct files_struct *base_fsp = NULL;
945 bool ok;
946 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
948 SMB_ASSERT(!(ucf_flags & UCF_DFS_PATHNAME));
950 if (is_fake_file_path(name_in)) {
951 smb_fname = synthetic_smb_fname_split(mem_ctx, name_in, posix);
952 if (smb_fname == NULL) {
953 return NT_STATUS_NO_MEMORY;
955 smb_fname->st = (SMB_STRUCT_STAT){
956 .st_ex_nlink = 1,
957 .st_ex_mode = S_IFREG | 0644,
959 smb_fname->st.st_ex_btime =
960 (struct timespec){0, SAMBA_UTIME_OMIT};
961 smb_fname->st.st_ex_atime =
962 (struct timespec){0, SAMBA_UTIME_OMIT};
963 smb_fname->st.st_ex_mtime =
964 (struct timespec){0, SAMBA_UTIME_OMIT};
965 smb_fname->st.st_ex_ctime =
966 (struct timespec){0, SAMBA_UTIME_OMIT};
968 *_dirfsp = conn->cwd_fsp;
969 *_smb_fname = smb_fname;
970 return NT_STATUS_OK;
974 * Catch an invalid path of "." before we
975 * call filename_split_lcomp(). We need to
976 * do this as filename_split_lcomp() will
977 * use "." for the missing relative component
978 * when an empty name_in path is sent by
979 * the client.
981 if (ISDOT(name_in)) {
982 status = NT_STATUS_OBJECT_NAME_INVALID;
983 goto fail;
986 ok = filename_split_lcomp(
987 talloc_tos(),
988 name_in,
989 posix,
990 &dirname,
991 &fname_rel,
992 &streamname);
993 if (!ok) {
994 status = NT_STATUS_NO_MEMORY;
995 goto fail;
998 if ((streamname != NULL) &&
999 ((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
1000 status = NT_STATUS_OBJECT_NAME_INVALID;
1001 goto fail;
1004 if (!posix) {
1005 bool name_has_wild = ms_has_wild(dirname);
1006 name_has_wild |= ms_has_wild(fname_rel);
1007 if (name_has_wild) {
1008 status = NT_STATUS_OBJECT_NAME_INVALID;
1009 goto fail;
1013 if (dirname[0] == '\0') {
1014 status = synthetic_pathref(
1015 mem_ctx,
1016 conn->cwd_fsp,
1017 ".",
1018 NULL,
1019 NULL,
1021 posix ? SMB_FILENAME_POSIX_PATH : 0,
1022 &smb_dirname);
1023 } else {
1024 struct open_symlink_err *symlink_err = NULL;
1026 status = normalize_filename_case(conn, dirname, ucf_flags);
1027 if (!NT_STATUS_IS_OK(status)) {
1028 DBG_ERR("normalize_filename_case %s failed: %s\n",
1029 dirname,
1030 nt_errstr(status));
1031 goto fail;
1034 status = openat_pathref_fsp_nosymlink(mem_ctx,
1035 conn,
1036 conn->cwd_fsp,
1037 dirname,
1039 posix,
1040 &smb_dirname,
1041 &symlink_err);
1043 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1044 size_t name_in_len, dirname_len;
1046 if (lp_host_msdfs() && lp_msdfs_root(SNUM(conn)) &&
1047 strnequal(symlink_err->reparse->substitute_name,
1048 "msdfs:",
1049 6)) {
1050 status = NT_STATUS_PATH_NOT_COVERED;
1051 goto fail;
1054 name_in_len = strlen(name_in);
1055 dirname_len = strlen(dirname);
1057 SMB_ASSERT(name_in_len >= dirname_len);
1059 symlink_err->unparsed += (name_in_len - dirname_len);
1060 *_symlink_err = symlink_err;
1062 goto fail;
1066 if (!NT_STATUS_IS_OK(status)) {
1067 DBG_DEBUG("opening directory %s failed: %s\n",
1068 dirname,
1069 nt_errstr(status));
1070 TALLOC_FREE(dirname);
1072 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1074 * Except ACCESS_DENIED, everything else leads
1075 * to PATH_NOT_FOUND.
1077 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1080 goto fail;
1083 if (!VALID_STAT_OF_DIR(smb_dirname->st)) {
1084 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1085 goto fail;
1087 smb_dirname->fsp->fsp_flags.is_directory = true;
1090 * Only look at bad last component values
1091 * once we know we have a valid directory. That
1092 * way we won't confuse error messages from
1093 * opening the directory path with error
1094 * messages from a bad last component.
1097 /* Relative filename can't be empty */
1098 if (fname_rel[0] == '\0') {
1099 status = NT_STATUS_OBJECT_NAME_INVALID;
1100 goto fail;
1103 /* Relative filename can't be ".." */
1104 if (ISDOTDOT(fname_rel)) {
1105 status = NT_STATUS_OBJECT_NAME_INVALID;
1106 goto fail;
1108 /* Relative name can only be dot if directory is empty. */
1109 if (ISDOT(fname_rel) && dirname[0] != '\0') {
1110 status = NT_STATUS_OBJECT_NAME_INVALID;
1111 goto fail;
1114 TALLOC_FREE(dirname);
1116 smb_fname_rel = synthetic_smb_fname(
1117 mem_ctx,
1118 fname_rel,
1119 streamname,
1120 NULL,
1121 twrp,
1122 posix ? SMB_FILENAME_POSIX_PATH : 0);
1123 if (smb_fname_rel == NULL) {
1124 status = NT_STATUS_NO_MEMORY;
1125 goto fail;
1128 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
1129 is_named_stream(smb_fname_rel)) {
1131 * Find the base_fsp first without the stream.
1133 saved_streamname = smb_fname_rel->stream_name;
1134 smb_fname_rel->stream_name = NULL;
1137 status = normalize_filename_case(
1138 conn, smb_fname_rel->base_name, ucf_flags);
1139 if (!NT_STATUS_IS_OK(status)) {
1140 DBG_ERR("normalize_filename_case %s failed: %s\n",
1141 smb_fname_rel->base_name,
1142 nt_errstr(status));
1143 goto fail;
1146 status = openat_pathref_fsp_case_insensitive(
1147 smb_dirname->fsp, smb_fname_rel, ucf_flags);
1149 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1150 VALID_STAT(smb_fname_rel->st) &&
1151 S_ISLNK(smb_fname_rel->st.st_ex_mode)) {
1154 * If we're on an MSDFS share, see if this is
1155 * an MSDFS link.
1157 if (lp_host_msdfs() &&
1158 lp_msdfs_root(SNUM(conn)) &&
1159 is_msdfs_link(smb_dirname->fsp, smb_fname_rel))
1161 status = NT_STATUS_PATH_NOT_COVERED;
1162 goto fail;
1165 #if defined(WITH_SMB1SERVER)
1167 * In SMB1 posix mode, if this is a symlink,
1168 * allow access to the name with a NULL smb_fname->fsp.
1170 if (ucf_flags & UCF_LCOMP_LNK_OK) {
1171 SMB_ASSERT(smb_fname_rel->fsp == NULL);
1172 SMB_ASSERT(streamname == NULL);
1174 smb_fname = full_path_from_dirfsp_atname(
1175 mem_ctx,
1176 smb_dirname->fsp,
1177 smb_fname_rel);
1178 if (smb_fname == NULL) {
1179 status = NT_STATUS_NO_MEMORY;
1180 goto fail;
1182 goto done;
1184 #endif
1187 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1188 !VALID_STAT(smb_fname_rel->st)) {
1190 char *normalized = NULL;
1193 * Creating a new file
1196 status = filename_convert_normalize_new(
1197 smb_fname_rel,
1198 conn,
1199 smb_fname_rel->base_name,
1200 &normalized);
1201 if (!NT_STATUS_IS_OK(status)) {
1202 DBG_DEBUG("filename_convert_normalize_new failed: "
1203 "%s\n",
1204 nt_errstr(status));
1205 goto fail;
1207 if (normalized != NULL) {
1208 smb_fname_rel->base_name = normalized;
1211 smb_fname_rel->stream_name = saved_streamname;
1213 smb_fname = full_path_from_dirfsp_atname(
1214 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1215 if (smb_fname == NULL) {
1216 status = NT_STATUS_NO_MEMORY;
1217 goto fail;
1219 goto done;
1222 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_OPEN_RESTRICTION)) {
1223 /* A vetoed file, pretend it's not there */
1224 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1226 if (!NT_STATUS_IS_OK(status)) {
1227 goto fail;
1230 if (saved_streamname == NULL) {
1231 /* smb_fname must be allocated off mem_ctx. */
1232 smb_fname = cp_smb_filename(mem_ctx,
1233 smb_fname_rel->fsp->fsp_name);
1234 if (smb_fname == NULL) {
1235 goto fail;
1237 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1238 if (!NT_STATUS_IS_OK(status)) {
1239 goto fail;
1241 goto done;
1244 base_fsp = smb_fname_rel->fsp;
1245 smb_fname_fsp_unlink(smb_fname_rel);
1246 SET_STAT_INVALID(smb_fname_rel->st);
1248 smb_fname_rel->stream_name = saved_streamname;
1250 status = open_stream_pathref_fsp(&base_fsp, smb_fname_rel);
1252 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1253 !conn->case_sensitive) {
1254 char *found = NULL;
1256 status = get_real_stream_name(
1257 smb_fname_rel,
1258 base_fsp,
1259 smb_fname_rel->stream_name,
1260 &found);
1262 if (NT_STATUS_IS_OK(status)) {
1263 smb_fname_rel->stream_name = found;
1264 found = NULL;
1265 status = open_stream_pathref_fsp(
1266 &base_fsp, smb_fname_rel);
1270 if (NT_STATUS_IS_OK(status)) {
1271 /* smb_fname must be allocated off mem_ctx. */
1272 smb_fname = cp_smb_filename(mem_ctx,
1273 smb_fname_rel->fsp->fsp_name);
1274 if (smb_fname == NULL) {
1275 goto fail;
1277 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1278 if (!NT_STATUS_IS_OK(status)) {
1279 goto fail;
1281 goto done;
1284 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1286 * Creating a new stream
1288 * We should save the already-open base fsp for
1289 * create_file_unixpath() somehow.
1291 smb_fname = full_path_from_dirfsp_atname(
1292 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1293 if (smb_fname == NULL) {
1294 status = NT_STATUS_NO_MEMORY;
1295 goto fail;
1298 * When open_stream_pathref_fsp() returns
1299 * NT_STATUS_OBJECT_NAME_NOT_FOUND, smb_fname_rel->fsp
1300 * has been set to NULL, so we must free base_fsp separately
1301 * to prevent fd-leaks when opening a stream that doesn't
1302 * exist.
1304 fd_close(base_fsp);
1305 file_free(NULL, base_fsp);
1306 base_fsp = NULL;
1307 goto done;
1310 if (!NT_STATUS_IS_OK(status)) {
1311 goto fail;
1314 done:
1315 *_dirfsp = smb_dirname->fsp;
1316 *_smb_fname = smb_fname;
1318 smb_fname_fsp_unlink(smb_fname_rel);
1319 TALLOC_FREE(smb_fname_rel);
1320 return NT_STATUS_OK;
1322 fail:
1324 * If open_stream_pathref_fsp() returns an error, smb_fname_rel->fsp
1325 * has been set to NULL, so we must free base_fsp separately
1326 * to prevent fd-leaks when opening a stream that doesn't
1327 * exist.
1329 if (base_fsp != NULL) {
1330 fd_close(base_fsp);
1331 file_free(NULL, base_fsp);
1332 base_fsp = NULL;
1334 TALLOC_FREE(dirname);
1335 TALLOC_FREE(smb_dirname);
1336 TALLOC_FREE(smb_fname_rel);
1337 return status;
1340 NTSTATUS filename_convert_dirfsp(
1341 TALLOC_CTX *mem_ctx,
1342 connection_struct *conn,
1343 const char *name_in,
1344 uint32_t ucf_flags,
1345 NTTIME twrp,
1346 struct files_struct **_dirfsp,
1347 struct smb_filename **_smb_fname)
1349 struct open_symlink_err *symlink_err = NULL;
1350 NTSTATUS status;
1351 char *substitute = NULL;
1352 char *target = NULL;
1353 size_t symlink_redirects = 0;
1355 next:
1356 if (symlink_redirects > 40) {
1357 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1360 status = filename_convert_dirfsp_nosymlink(mem_ctx,
1361 conn,
1362 name_in,
1363 ucf_flags,
1364 twrp,
1365 _dirfsp,
1366 _smb_fname,
1367 &symlink_err);
1369 if (!NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1370 return status;
1373 if (!lp_follow_symlinks(SNUM(conn))) {
1374 status = (symlink_err->unparsed == 0)
1375 ? NT_STATUS_OBJECT_NAME_NOT_FOUND
1376 : NT_STATUS_OBJECT_PATH_NOT_FOUND;
1377 TALLOC_FREE(symlink_err);
1378 return status;
1382 * Right now, SMB2 and SMB1 always traverse symlinks
1383 * within the share. SMB1+POSIX traverses non-terminal
1384 * symlinks within the share.
1386 * When we add SMB2+POSIX we need to return
1387 * a NT_STATUS_STOPPED_ON_SYMLINK error here, using the
1388 * symlink target data read below if SMB2+POSIX has
1389 * UCF_POSIX_PATHNAMES set to cause the client to
1390 * resolve all symlinks locally.
1393 substitute = symlink_err->reparse->substitute_name;
1395 status = safe_symlink_target_path(mem_ctx,
1396 conn->connectpath,
1397 name_in,
1398 substitute,
1399 symlink_err->unparsed,
1400 &target);
1401 TALLOC_FREE(symlink_err);
1402 if (!NT_STATUS_IS_OK(status)) {
1403 return status;
1405 name_in = target;
1407 symlink_redirects += 1;
1409 goto next;
1412 char *full_path_from_dirfsp_at_basename(TALLOC_CTX *mem_ctx,
1413 const struct files_struct *dirfsp,
1414 const char *at_base_name)
1416 char *path = NULL;
1418 if (dirfsp == dirfsp->conn->cwd_fsp ||
1419 ISDOT(dirfsp->fsp_name->base_name) || at_base_name[0] == '/') {
1420 path = talloc_strdup(mem_ctx, at_base_name);
1421 } else {
1422 path = talloc_asprintf(mem_ctx,
1423 "%s/%s",
1424 dirfsp->fsp_name->base_name,
1425 at_base_name);
1428 return path;
1432 * Build the full path from a dirfsp and dirfsp relative name
1434 struct smb_filename *
1435 full_path_from_dirfsp_atname(TALLOC_CTX *mem_ctx,
1436 const struct files_struct *dirfsp,
1437 const struct smb_filename *atname)
1439 struct smb_filename *fname = NULL;
1440 char *path = NULL;
1442 path = full_path_from_dirfsp_at_basename(mem_ctx,
1443 dirfsp,
1444 atname->base_name);
1445 if (path == NULL) {
1446 return NULL;
1449 fname = synthetic_smb_fname(mem_ctx,
1450 path,
1451 atname->stream_name,
1452 &atname->st,
1453 atname->twrp,
1454 atname->flags);
1455 TALLOC_FREE(path);
1456 if (fname == NULL) {
1457 return NULL;
1460 return fname;