smbd: Make a fake file's stat a valid regular file
[Samba.git] / source3 / smbd / filename.c
bloba37d0709d0f0f903aaef3a2336faa7d6aa59352d
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 static bool get_real_filename_cache_key(
418 TALLOC_CTX *mem_ctx,
419 struct files_struct *dirfsp,
420 const char *name,
421 DATA_BLOB *_key)
423 struct file_id fid = vfs_file_id_from_sbuf(
424 dirfsp->conn, &dirfsp->fsp_name->st);
425 char *upper = NULL;
426 uint8_t *key = NULL;
427 size_t namelen, keylen;
429 upper = talloc_strdup_upper(mem_ctx, name);
430 if (upper == NULL) {
431 return false;
433 namelen = talloc_get_size(upper);
435 keylen = namelen + sizeof(fid);
436 if (keylen < sizeof(fid)) {
437 TALLOC_FREE(upper);
438 return false;
441 key = talloc_size(mem_ctx, keylen);
442 if (key == NULL) {
443 TALLOC_FREE(upper);
444 return false;
447 memcpy(key, &fid, sizeof(fid));
448 memcpy(key + sizeof(fid), upper, namelen);
449 TALLOC_FREE(upper);
451 *_key = (DATA_BLOB) { .data = key, .length = keylen, };
452 return true;
456 * Lightweight function to just get last component
457 * for rename / enumerate directory calls.
460 char *get_original_lcomp(TALLOC_CTX *ctx,
461 connection_struct *conn,
462 const char *filename_in,
463 uint32_t ucf_flags)
465 char *last_slash = NULL;
466 char *orig_lcomp;
467 NTSTATUS status;
469 last_slash = strrchr(filename_in, '/');
470 if (last_slash != NULL) {
471 orig_lcomp = talloc_strdup(ctx, last_slash+1);
472 } else {
473 orig_lcomp = talloc_strdup(ctx, filename_in);
475 if (orig_lcomp == NULL) {
476 return NULL;
478 status = normalize_filename_case(conn, orig_lcomp, ucf_flags);
479 if (!NT_STATUS_IS_OK(status)) {
480 TALLOC_FREE(orig_lcomp);
481 return NULL;
483 return orig_lcomp;
487 * Get the correct capitalized stream name hanging off
488 * base_fsp. Equivalent of get_real_filename(), but for streams.
490 static NTSTATUS get_real_stream_name(
491 TALLOC_CTX *mem_ctx,
492 struct files_struct *base_fsp,
493 const char *stream_name,
494 char **_found)
496 unsigned int i, num_streams = 0;
497 struct stream_struct *streams = NULL;
498 NTSTATUS status;
500 status = vfs_fstreaminfo(
501 base_fsp, talloc_tos(), &num_streams, &streams);
502 if (!NT_STATUS_IS_OK(status)) {
503 return status;
506 for (i=0; i<num_streams; i++) {
507 bool equal = sname_equal(stream_name, streams[i].name, false);
509 DBG_DEBUG("comparing [%s] and [%s]: %sequal\n",
510 stream_name,
511 streams[i].name,
512 equal ? "" : "not ");
514 if (equal) {
515 *_found = talloc_move(mem_ctx, &streams[i].name);
516 TALLOC_FREE(streams);
517 return NT_STATUS_OK;
521 TALLOC_FREE(streams);
522 return NT_STATUS_OBJECT_NAME_NOT_FOUND;
525 static bool filename_split_lcomp(
526 TALLOC_CTX *mem_ctx,
527 const char *name_in,
528 bool posix,
529 char **_dirname,
530 const char **_fname_rel,
531 const char **_streamname)
533 const char *lcomp = NULL;
534 const char *fname_rel = NULL;
535 const char *streamname = NULL;
536 char *dirname = NULL;
538 if (name_in[0] == '\0') {
539 fname_rel = ".";
540 dirname = talloc_strdup(mem_ctx, "");
541 if (dirname == NULL) {
542 return false;
544 goto done;
547 lcomp = strrchr_m(name_in, '/');
548 if (lcomp != NULL) {
549 fname_rel = lcomp+1;
550 dirname = talloc_strndup(mem_ctx, name_in, lcomp - name_in);
551 if (dirname == NULL) {
552 return false;
554 goto find_stream;
558 * No slash, dir is empty
560 dirname = talloc_strdup(mem_ctx, "");
561 if (dirname == NULL) {
562 return false;
565 if (!posix && (name_in[0] == ':')) {
567 * Special case for stream on root directory
569 fname_rel = ".";
570 streamname = name_in;
571 goto done;
574 fname_rel = name_in;
576 find_stream:
577 if (!posix) {
578 streamname = strchr_m(fname_rel, ':');
580 if (streamname != NULL) {
581 fname_rel = talloc_strndup(
582 mem_ctx,
583 fname_rel,
584 streamname - fname_rel);
585 if (fname_rel == NULL) {
586 TALLOC_FREE(dirname);
587 return false;
592 done:
593 *_dirname = dirname;
594 *_fname_rel = fname_rel;
595 *_streamname = streamname;
596 return true;
600 * Create the correct capitalization of a file name to be created.
602 static NTSTATUS filename_convert_normalize_new(
603 TALLOC_CTX *mem_ctx,
604 struct connection_struct *conn,
605 char *name_in,
606 char **_normalized)
608 char *name = name_in;
610 *_normalized = NULL;
612 if (!conn->case_preserve ||
613 (mangle_is_8_3(name, false,
614 conn->params) &&
615 !conn->short_case_preserve)) {
617 char *normalized = talloc_strdup(mem_ctx, name);
618 if (normalized == NULL) {
619 return NT_STATUS_NO_MEMORY;
622 strnorm(normalized, lp_default_case(SNUM(conn)));
623 name = normalized;
626 if (mangle_is_mangled(name, conn->params)) {
627 bool found;
628 char *unmangled = NULL;
630 found = mangle_lookup_name_from_8_3(
631 mem_ctx, name, &unmangled, conn->params);
632 if (found) {
633 name = unmangled;
637 if (name != name_in) {
638 *_normalized = name;
641 return NT_STATUS_OK;
645 * Open smb_fname_rel->fsp as a pathref fsp with a case insensitive
646 * fallback using GETREALFILENAME_CACHE and get_real_filename_at() if
647 * the first attempt based on the filename sent by the client gives
648 * ENOENT.
650 static NTSTATUS openat_pathref_fsp_case_insensitive(
651 struct files_struct *dirfsp,
652 struct smb_filename *smb_fname_rel,
653 uint32_t ucf_flags)
655 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
656 DATA_BLOB cache_key = { .data = NULL, };
657 char *found_name = NULL;
658 NTSTATUS status;
659 bool ok;
661 SET_STAT_INVALID(smb_fname_rel->st);
663 /* Check veto files - only looks at last component. */
664 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
665 DBG_DEBUG("veto files rejecting last component %s\n",
666 smb_fname_str_dbg(smb_fname_rel));
667 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
670 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
672 if (NT_STATUS_IS_OK(status)) {
673 return NT_STATUS_OK;
676 if (VALID_STAT(smb_fname_rel->st)) {
678 * We got an error although the object existed. Might
679 * be a symlink we don't want.
681 return status;
684 if (!NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
686 * Only retry on ENOENT
688 return status;
691 if (posix || dirfsp->conn->case_sensitive) {
693 * Only return case insensitive if required
695 return status;
698 if (lp_stat_cache()) {
699 char *base_name = smb_fname_rel->base_name;
700 char *original_relname = NULL;
701 DATA_BLOB value = { .data = NULL };
703 ok = get_real_filename_cache_key(
704 talloc_tos(), dirfsp, base_name, &cache_key);
705 if (!ok) {
707 * probably ENOMEM, just bail
709 return status;
712 DO_PROFILE_INC(statcache_lookups);
714 ok = memcache_lookup(
715 NULL, GETREALFILENAME_CACHE, cache_key, &value);
716 if (!ok) {
717 DO_PROFILE_INC(statcache_misses);
718 goto lookup;
720 DO_PROFILE_INC(statcache_hits);
723 * For the "new filename" case we need to preserve the
724 * capitalization the client sent us, see
725 * https://bugzilla.samba.org/show_bug.cgi?id=15481
727 original_relname = smb_fname_rel->base_name;
729 smb_fname_rel->base_name = talloc_memdup(
730 smb_fname_rel, value.data, value.length);
731 if (smb_fname_rel->base_name == NULL) {
732 TALLOC_FREE(cache_key.data);
733 return NT_STATUS_NO_MEMORY;
736 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
737 DBG_DEBUG("veto files rejecting last component %s\n",
738 smb_fname_str_dbg(smb_fname_rel));
739 TALLOC_FREE(cache_key.data);
740 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
743 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
744 if (NT_STATUS_IS_OK(status)) {
745 TALLOC_FREE(cache_key.data);
746 TALLOC_FREE(original_relname);
747 return NT_STATUS_OK;
750 memcache_delete(NULL, GETREALFILENAME_CACHE, cache_key);
751 TALLOC_FREE(smb_fname_rel->base_name);
752 smb_fname_rel->base_name = original_relname;
755 lookup:
756 status = get_real_filename_at(
757 dirfsp, smb_fname_rel->base_name, smb_fname_rel, &found_name);
758 if (NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED) &&
759 (ucf_flags & UCF_PREP_CREATEFILE)) {
761 * dropbox
763 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
766 if (NT_STATUS_IS_OK(status)) {
767 TALLOC_FREE(smb_fname_rel->base_name);
768 smb_fname_rel->base_name = found_name;
770 if (IS_VETO_PATH(dirfsp->conn, smb_fname_rel->base_name)) {
771 DBG_DEBUG("veto files rejecting last component %s\n",
772 smb_fname_str_dbg(smb_fname_rel));
773 return NT_STATUS_NETWORK_OPEN_RESTRICTION;
776 status = openat_pathref_fsp(dirfsp, smb_fname_rel);
779 if (NT_STATUS_IS_OK(status) && (cache_key.data != NULL)) {
780 DATA_BLOB value = {
781 .data = (uint8_t *)smb_fname_rel->base_name,
782 .length = strlen(smb_fname_rel->base_name) + 1,
785 memcache_add(NULL, GETREALFILENAME_CACHE, cache_key, value);
788 TALLOC_FREE(cache_key.data);
790 return status;
793 static const char *previous_slash(const char *name_in, const char *slash)
795 const char *prev = NULL;
797 SMB_ASSERT((name_in <= slash) && (slash[0] == '/'));
799 prev = strchr_m(name_in, '/');
801 if (prev == slash) {
802 /* No previous slash */
803 return NULL;
806 while (true) {
807 const char *next = strchr_m(prev + 1, '/');
809 if (next == slash) {
810 return prev;
812 prev = next;
815 return NULL; /* unreachable */
818 static char *symlink_target_path(
819 TALLOC_CTX *mem_ctx,
820 const char *name_in,
821 const char *substitute,
822 size_t unparsed)
824 size_t name_in_len = strlen(name_in);
825 const char *p_unparsed = NULL;
826 const char *parent = NULL;
827 char *ret;
829 SMB_ASSERT(unparsed <= name_in_len);
831 p_unparsed = name_in + (name_in_len - unparsed);
833 if (substitute[0] == '/') {
834 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
835 return ret;
838 if (unparsed == 0) {
839 parent = strrchr_m(name_in, '/');
840 } else {
841 parent = previous_slash(name_in, p_unparsed);
844 if (parent == NULL) {
845 ret = talloc_asprintf(mem_ctx, "%s%s", substitute, p_unparsed);
846 } else {
847 ret = talloc_asprintf(mem_ctx,
848 "%.*s/%s%s",
849 (int)(parent - name_in),
850 name_in,
851 substitute,
852 p_unparsed);
855 return ret;
858 static NTSTATUS safe_symlink_target_path(
859 TALLOC_CTX *mem_ctx,
860 const char *connectpath,
861 const char *name_in,
862 const char *substitute,
863 size_t unparsed,
864 char **_name_out)
866 char *target = NULL;
867 char *abs_target = NULL;
868 char *abs_target_canon = NULL;
869 const char *relative = NULL;
870 char *name_out = NULL;
871 NTSTATUS status = NT_STATUS_NO_MEMORY;
872 bool in_share;
874 target = symlink_target_path(mem_ctx, name_in, substitute, unparsed);
875 if (target == NULL) {
876 goto fail;
879 DBG_DEBUG("name_in: %s, substitute: %s, unparsed: %zu, target=%s\n",
880 name_in,
881 substitute,
882 unparsed,
883 target);
885 if (target[0] == '/') {
886 abs_target = target;
887 } else {
888 abs_target = talloc_asprintf(
889 target, "%s/%s", connectpath, target);
890 if (abs_target == NULL) {
891 goto fail;
895 abs_target_canon = canonicalize_absolute_path(target, abs_target);
896 if (abs_target_canon == NULL) {
897 goto fail;
900 DBG_DEBUG("abs_target_canon=%s\n", abs_target_canon);
902 in_share = subdir_of(
903 connectpath, strlen(connectpath), abs_target_canon, &relative);
904 if (!in_share) {
905 DBG_DEBUG("wide link to %s\n", abs_target_canon);
906 status = (unparsed != 0) ? NT_STATUS_OBJECT_PATH_NOT_FOUND
907 : NT_STATUS_OBJECT_NAME_NOT_FOUND;
908 goto fail;
911 name_out = talloc_strdup(mem_ctx, relative);
912 if (name_out == NULL) {
913 goto fail;
916 status = NT_STATUS_OK;
917 *_name_out = name_out;
918 fail:
919 TALLOC_FREE(target);
920 return status;
924 * Split up name_in as sent by the client into a directory pathref fsp
925 * and a relative smb_filename.
927 static NTSTATUS filename_convert_dirfsp_nosymlink(
928 TALLOC_CTX *mem_ctx,
929 connection_struct *conn,
930 const char *name_in,
931 uint32_t ucf_flags,
932 NTTIME twrp,
933 struct files_struct **_dirfsp,
934 struct smb_filename **_smb_fname,
935 struct open_symlink_err **_symlink_err)
937 struct smb_filename *smb_dirname = NULL;
938 struct smb_filename *smb_fname_rel = NULL;
939 struct smb_filename *smb_fname = NULL;
940 const bool posix = (ucf_flags & UCF_POSIX_PATHNAMES);
941 char *dirname = NULL;
942 const char *fname_rel = NULL;
943 const char *streamname = NULL;
944 char *saved_streamname = NULL;
945 struct files_struct *base_fsp = NULL;
946 bool ok;
947 NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
949 SMB_ASSERT(!(ucf_flags & UCF_DFS_PATHNAME));
951 if (is_fake_file_path(name_in)) {
952 smb_fname = synthetic_smb_fname_split(mem_ctx, name_in, posix);
953 if (smb_fname == NULL) {
954 return NT_STATUS_NO_MEMORY;
956 smb_fname->st = (SMB_STRUCT_STAT){
957 .st_ex_nlink = 1,
958 .st_ex_mode = S_IFREG | 0644,
960 smb_fname->st.st_ex_btime =
961 (struct timespec){0, SAMBA_UTIME_OMIT};
962 smb_fname->st.st_ex_atime =
963 (struct timespec){0, SAMBA_UTIME_OMIT};
964 smb_fname->st.st_ex_mtime =
965 (struct timespec){0, SAMBA_UTIME_OMIT};
966 smb_fname->st.st_ex_ctime =
967 (struct timespec){0, SAMBA_UTIME_OMIT};
969 *_dirfsp = conn->cwd_fsp;
970 *_smb_fname = smb_fname;
971 return NT_STATUS_OK;
975 * Catch an invalid path of "." before we
976 * call filename_split_lcomp(). We need to
977 * do this as filename_split_lcomp() will
978 * use "." for the missing relative component
979 * when an empty name_in path is sent by
980 * the client.
982 if (ISDOT(name_in)) {
983 status = NT_STATUS_OBJECT_NAME_INVALID;
984 goto fail;
987 ok = filename_split_lcomp(
988 talloc_tos(),
989 name_in,
990 posix,
991 &dirname,
992 &fname_rel,
993 &streamname);
994 if (!ok) {
995 status = NT_STATUS_NO_MEMORY;
996 goto fail;
999 if ((streamname != NULL) &&
1000 ((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
1001 status = NT_STATUS_OBJECT_NAME_INVALID;
1002 goto fail;
1005 if (!posix) {
1006 bool name_has_wild = ms_has_wild(dirname);
1007 name_has_wild |= ms_has_wild(fname_rel);
1008 if (name_has_wild) {
1009 status = NT_STATUS_OBJECT_NAME_INVALID;
1010 goto fail;
1014 if (dirname[0] == '\0') {
1015 status = synthetic_pathref(
1016 mem_ctx,
1017 conn->cwd_fsp,
1018 ".",
1019 NULL,
1020 NULL,
1022 posix ? SMB_FILENAME_POSIX_PATH : 0,
1023 &smb_dirname);
1024 } else {
1025 struct open_symlink_err *symlink_err = NULL;
1027 status = normalize_filename_case(conn, dirname, ucf_flags);
1028 if (!NT_STATUS_IS_OK(status)) {
1029 DBG_ERR("normalize_filename_case %s failed: %s\n",
1030 dirname,
1031 nt_errstr(status));
1032 goto fail;
1035 status = openat_pathref_fsp_nosymlink(mem_ctx,
1036 conn,
1037 conn->cwd_fsp,
1038 dirname,
1040 posix,
1041 &smb_dirname,
1042 &symlink_err);
1044 if (NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1045 size_t name_in_len, dirname_len;
1047 if (lp_host_msdfs() && lp_msdfs_root(SNUM(conn)) &&
1048 strnequal(symlink_err->reparse->substitute_name,
1049 "msdfs:",
1050 6)) {
1051 status = NT_STATUS_PATH_NOT_COVERED;
1052 goto fail;
1055 name_in_len = strlen(name_in);
1056 dirname_len = strlen(dirname);
1058 SMB_ASSERT(name_in_len >= dirname_len);
1060 symlink_err->unparsed += (name_in_len - dirname_len);
1061 *_symlink_err = symlink_err;
1063 goto fail;
1067 if (!NT_STATUS_IS_OK(status)) {
1068 DBG_DEBUG("opening directory %s failed: %s\n",
1069 dirname,
1070 nt_errstr(status));
1071 TALLOC_FREE(dirname);
1073 if (!NT_STATUS_EQUAL(status, NT_STATUS_ACCESS_DENIED)) {
1075 * Except ACCESS_DENIED, everything else leads
1076 * to PATH_NOT_FOUND.
1078 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1081 goto fail;
1084 if (!VALID_STAT_OF_DIR(smb_dirname->st)) {
1085 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
1086 goto fail;
1088 smb_dirname->fsp->fsp_flags.is_directory = true;
1091 * Only look at bad last component values
1092 * once we know we have a valid directory. That
1093 * way we won't confuse error messages from
1094 * opening the directory path with error
1095 * messages from a bad last component.
1098 /* Relative filename can't be empty */
1099 if (fname_rel[0] == '\0') {
1100 status = NT_STATUS_OBJECT_NAME_INVALID;
1101 goto fail;
1104 /* Relative filename can't be ".." */
1105 if (ISDOTDOT(fname_rel)) {
1106 status = NT_STATUS_OBJECT_NAME_INVALID;
1107 goto fail;
1109 /* Relative name can only be dot if directory is empty. */
1110 if (ISDOT(fname_rel) && dirname[0] != '\0') {
1111 status = NT_STATUS_OBJECT_NAME_INVALID;
1112 goto fail;
1115 TALLOC_FREE(dirname);
1117 smb_fname_rel = synthetic_smb_fname(
1118 mem_ctx,
1119 fname_rel,
1120 streamname,
1121 NULL,
1122 twrp,
1123 posix ? SMB_FILENAME_POSIX_PATH : 0);
1124 if (smb_fname_rel == NULL) {
1125 status = NT_STATUS_NO_MEMORY;
1126 goto fail;
1129 if ((conn->fs_capabilities & FILE_NAMED_STREAMS) &&
1130 is_named_stream(smb_fname_rel)) {
1132 * Find the base_fsp first without the stream.
1134 saved_streamname = smb_fname_rel->stream_name;
1135 smb_fname_rel->stream_name = NULL;
1138 status = normalize_filename_case(
1139 conn, smb_fname_rel->base_name, ucf_flags);
1140 if (!NT_STATUS_IS_OK(status)) {
1141 DBG_ERR("normalize_filename_case %s failed: %s\n",
1142 smb_fname_rel->base_name,
1143 nt_errstr(status));
1144 goto fail;
1147 status = openat_pathref_fsp_case_insensitive(
1148 smb_dirname->fsp, smb_fname_rel, ucf_flags);
1150 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1151 VALID_STAT(smb_fname_rel->st) &&
1152 S_ISLNK(smb_fname_rel->st.st_ex_mode)) {
1155 * If we're on an MSDFS share, see if this is
1156 * an MSDFS link.
1158 if (lp_host_msdfs() &&
1159 lp_msdfs_root(SNUM(conn)) &&
1160 is_msdfs_link(smb_dirname->fsp, smb_fname_rel))
1162 status = NT_STATUS_PATH_NOT_COVERED;
1163 goto fail;
1166 #if defined(WITH_SMB1SERVER)
1168 * In SMB1 posix mode, if this is a symlink,
1169 * allow access to the name with a NULL smb_fname->fsp.
1171 if (ucf_flags & UCF_LCOMP_LNK_OK) {
1172 SMB_ASSERT(smb_fname_rel->fsp == NULL);
1173 SMB_ASSERT(streamname == NULL);
1175 smb_fname = full_path_from_dirfsp_atname(
1176 mem_ctx,
1177 smb_dirname->fsp,
1178 smb_fname_rel);
1179 if (smb_fname == NULL) {
1180 status = NT_STATUS_NO_MEMORY;
1181 goto fail;
1183 goto done;
1185 #endif
1188 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1189 !VALID_STAT(smb_fname_rel->st)) {
1191 char *normalized = NULL;
1194 * Creating a new file
1197 status = filename_convert_normalize_new(
1198 smb_fname_rel,
1199 conn,
1200 smb_fname_rel->base_name,
1201 &normalized);
1202 if (!NT_STATUS_IS_OK(status)) {
1203 DBG_DEBUG("filename_convert_normalize_new failed: "
1204 "%s\n",
1205 nt_errstr(status));
1206 goto fail;
1208 if (normalized != NULL) {
1209 smb_fname_rel->base_name = normalized;
1212 smb_fname_rel->stream_name = saved_streamname;
1214 smb_fname = full_path_from_dirfsp_atname(
1215 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1216 if (smb_fname == NULL) {
1217 status = NT_STATUS_NO_MEMORY;
1218 goto fail;
1220 goto done;
1223 if (NT_STATUS_EQUAL(status, NT_STATUS_NETWORK_OPEN_RESTRICTION)) {
1224 /* A vetoed file, pretend it's not there */
1225 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
1227 if (!NT_STATUS_IS_OK(status)) {
1228 goto fail;
1231 if (saved_streamname == NULL) {
1232 /* smb_fname must be allocated off mem_ctx. */
1233 smb_fname = cp_smb_filename(mem_ctx,
1234 smb_fname_rel->fsp->fsp_name);
1235 if (smb_fname == NULL) {
1236 goto fail;
1238 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1239 if (!NT_STATUS_IS_OK(status)) {
1240 goto fail;
1242 goto done;
1245 base_fsp = smb_fname_rel->fsp;
1246 smb_fname_fsp_unlink(smb_fname_rel);
1247 SET_STAT_INVALID(smb_fname_rel->st);
1249 smb_fname_rel->stream_name = saved_streamname;
1251 status = open_stream_pathref_fsp(&base_fsp, smb_fname_rel);
1253 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND) &&
1254 !conn->case_sensitive) {
1255 char *found = NULL;
1257 status = get_real_stream_name(
1258 smb_fname_rel,
1259 base_fsp,
1260 smb_fname_rel->stream_name,
1261 &found);
1263 if (NT_STATUS_IS_OK(status)) {
1264 smb_fname_rel->stream_name = found;
1265 found = NULL;
1266 status = open_stream_pathref_fsp(
1267 &base_fsp, smb_fname_rel);
1271 if (NT_STATUS_IS_OK(status)) {
1272 /* smb_fname must be allocated off mem_ctx. */
1273 smb_fname = cp_smb_filename(mem_ctx,
1274 smb_fname_rel->fsp->fsp_name);
1275 if (smb_fname == NULL) {
1276 goto fail;
1278 status = move_smb_fname_fsp_link(smb_fname, smb_fname_rel);
1279 if (!NT_STATUS_IS_OK(status)) {
1280 goto fail;
1282 goto done;
1285 if (NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_NAME_NOT_FOUND)) {
1287 * Creating a new stream
1289 * We should save the already-open base fsp for
1290 * create_file_unixpath() somehow.
1292 smb_fname = full_path_from_dirfsp_atname(
1293 mem_ctx, smb_dirname->fsp, smb_fname_rel);
1294 if (smb_fname == NULL) {
1295 status = NT_STATUS_NO_MEMORY;
1296 goto fail;
1299 * When open_stream_pathref_fsp() returns
1300 * NT_STATUS_OBJECT_NAME_NOT_FOUND, smb_fname_rel->fsp
1301 * has been set to NULL, so we must free base_fsp separately
1302 * to prevent fd-leaks when opening a stream that doesn't
1303 * exist.
1305 fd_close(base_fsp);
1306 file_free(NULL, base_fsp);
1307 base_fsp = NULL;
1308 goto done;
1311 if (!NT_STATUS_IS_OK(status)) {
1312 goto fail;
1315 done:
1316 *_dirfsp = smb_dirname->fsp;
1317 *_smb_fname = smb_fname;
1319 smb_fname_fsp_unlink(smb_fname_rel);
1320 TALLOC_FREE(smb_fname_rel);
1321 return NT_STATUS_OK;
1323 fail:
1325 * If open_stream_pathref_fsp() returns an error, smb_fname_rel->fsp
1326 * has been set to NULL, so we must free base_fsp separately
1327 * to prevent fd-leaks when opening a stream that doesn't
1328 * exist.
1330 if (base_fsp != NULL) {
1331 fd_close(base_fsp);
1332 file_free(NULL, base_fsp);
1333 base_fsp = NULL;
1335 TALLOC_FREE(dirname);
1336 TALLOC_FREE(smb_dirname);
1337 TALLOC_FREE(smb_fname_rel);
1338 return status;
1341 NTSTATUS filename_convert_dirfsp(
1342 TALLOC_CTX *mem_ctx,
1343 connection_struct *conn,
1344 const char *name_in,
1345 uint32_t ucf_flags,
1346 NTTIME twrp,
1347 struct files_struct **_dirfsp,
1348 struct smb_filename **_smb_fname)
1350 struct open_symlink_err *symlink_err = NULL;
1351 NTSTATUS status;
1352 char *substitute = NULL;
1353 char *target = NULL;
1354 size_t symlink_redirects = 0;
1356 next:
1357 if (symlink_redirects > 40) {
1358 return NT_STATUS_OBJECT_PATH_NOT_FOUND;
1361 status = filename_convert_dirfsp_nosymlink(mem_ctx,
1362 conn,
1363 name_in,
1364 ucf_flags,
1365 twrp,
1366 _dirfsp,
1367 _smb_fname,
1368 &symlink_err);
1370 if (!NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK)) {
1371 return status;
1374 if (!lp_follow_symlinks(SNUM(conn))) {
1375 status = (symlink_err->unparsed == 0)
1376 ? NT_STATUS_OBJECT_NAME_NOT_FOUND
1377 : NT_STATUS_OBJECT_PATH_NOT_FOUND;
1378 TALLOC_FREE(symlink_err);
1379 return status;
1383 * Right now, SMB2 and SMB1 always traverse symlinks
1384 * within the share. SMB1+POSIX traverses non-terminal
1385 * symlinks within the share.
1387 * When we add SMB2+POSIX we need to return
1388 * a NT_STATUS_STOPPED_ON_SYMLINK error here, using the
1389 * symlink target data read below if SMB2+POSIX has
1390 * UCF_POSIX_PATHNAMES set to cause the client to
1391 * resolve all symlinks locally.
1394 substitute = symlink_err->reparse->substitute_name;
1396 status = safe_symlink_target_path(mem_ctx,
1397 conn->connectpath,
1398 name_in,
1399 substitute,
1400 symlink_err->unparsed,
1401 &target);
1402 TALLOC_FREE(symlink_err);
1403 if (!NT_STATUS_IS_OK(status)) {
1404 return status;
1406 name_in = target;
1408 symlink_redirects += 1;
1410 goto next;
1413 char *full_path_from_dirfsp_at_basename(TALLOC_CTX *mem_ctx,
1414 const struct files_struct *dirfsp,
1415 const char *at_base_name)
1417 char *path = NULL;
1419 if (dirfsp == dirfsp->conn->cwd_fsp ||
1420 ISDOT(dirfsp->fsp_name->base_name) || at_base_name[0] == '/') {
1421 path = talloc_strdup(mem_ctx, at_base_name);
1422 } else {
1423 path = talloc_asprintf(mem_ctx,
1424 "%s/%s",
1425 dirfsp->fsp_name->base_name,
1426 at_base_name);
1429 return path;
1433 * Build the full path from a dirfsp and dirfsp relative name
1435 struct smb_filename *
1436 full_path_from_dirfsp_atname(TALLOC_CTX *mem_ctx,
1437 const struct files_struct *dirfsp,
1438 const struct smb_filename *atname)
1440 struct smb_filename *fname = NULL;
1441 char *path = NULL;
1443 path = full_path_from_dirfsp_at_basename(mem_ctx,
1444 dirfsp,
1445 atname->base_name);
1446 if (path == NULL) {
1447 return NULL;
1450 fname = synthetic_smb_fname(mem_ctx,
1451 path,
1452 atname->stream_name,
1453 &atname->st,
1454 atname->twrp,
1455 atname->flags);
1456 TALLOC_FREE(path);
1457 if (fname == NULL) {
1458 return NULL;
1461 return fname;