2 Unix SMB/CIFS implementation.
3 Files[] structure handling
4 Copyright (C) Andrew Tridgell 1998
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "smbd/smbXsrv_open.h"
24 #include "libcli/security/security.h"
26 #include "lib/util/bitmap.h"
27 #include "lib/util/strv.h"
29 #define FILE_HANDLE_OFFSET 0x1000
31 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
32 struct smb_filename **_smb_fname);
35 * create new fsp to be used for file_new or a durable handle reconnect
37 NTSTATUS fsp_new(struct connection_struct *conn, TALLOC_CTX *mem_ctx,
38 files_struct **result)
40 NTSTATUS status = NT_STATUS_NO_MEMORY;
41 files_struct *fsp = NULL;
42 struct smbd_server_connection *sconn = conn->sconn;
44 fsp = talloc_zero(mem_ctx, struct files_struct);
50 * This can't be a child of fsp because the file_handle can be ref'd
51 * when doing a dos/fcb open, which will then share the file_handle
52 * across multiple fsps.
54 fsp->fh = fd_handle_create(mem_ctx);
55 if (fsp->fh == NULL) {
59 fsp->fsp_flags.use_ofd_locks = !lp_smbd_force_process_locks(SNUM(conn));
60 #ifndef HAVE_OFD_LOCKS
61 fsp->fsp_flags.use_ofd_locks = false;
64 fh_set_refcount(fsp->fh, 1);
67 fsp->fnum = FNUM_FIELD_INVALID;
69 fsp->close_write_time = make_omit_timespec();
71 DLIST_ADD(sconn->files, fsp);
72 sconn->num_files += 1;
74 conn->num_files_open++;
76 DBG_INFO("allocated files structure (%u used)\n",
77 (unsigned int)sconn->num_files);
91 void fsp_set_gen_id(files_struct *fsp)
93 static uint64_t gen_id = 1;
96 * A billion of 64-bit increments per second gives us
97 * more than 500 years of runtime without wrap.
100 fh_set_gen_id(fsp->fh, gen_id);
103 /****************************************************************************
104 Find first available file slot.
105 ****************************************************************************/
107 NTSTATUS fsp_bind_smb(struct files_struct *fsp, struct smb_request *req)
109 struct smbXsrv_open *op = NULL;
114 DBG_DEBUG("INTERNAL_OPEN_ONLY, skipping smbXsrv_open\n");
118 now = timeval_to_nttime(&fsp->open_time);
120 status = smbXsrv_open_create(req->xconn,
121 fsp->conn->session_info,
124 if (!NT_STATUS_IS_OK(status)) {
129 fsp->fnum = op->local_id;
132 req->chain_fsp = fsp;
134 DBG_DEBUG("fsp [%s] mid [%" PRIu64"]\n",
135 fsp_str_dbg(fsp), fsp->mid);
140 NTSTATUS file_new(struct smb_request *req, connection_struct *conn,
141 files_struct **result)
143 struct smbd_server_connection *sconn = conn->sconn;
147 status = fsp_new(conn, conn, &fsp);
148 if (!NT_STATUS_IS_OK(status)) {
152 GetTimeOfDay(&fsp->open_time);
154 status = fsp_bind_smb(fsp, req);
155 if (!NT_STATUS_IS_OK(status)) {
156 file_free(NULL, fsp);
163 * Create an smb_filename with "" for the base_name. There are very
164 * few NULL checks, so make sure it's initialized with something. to
165 * be safe until an audit can be done.
167 fsp->fsp_name = synthetic_smb_fname(fsp,
173 if (fsp->fsp_name == NULL) {
174 file_free(NULL, fsp);
175 return NT_STATUS_NO_MEMORY;
178 DBG_INFO("new file %s\n", fsp_fnum_dbg(fsp));
180 /* A new fsp invalidates the positive and
181 negative fsp_fi_cache as the new fsp is pushed
182 at the start of the list and we search from
183 a cache hit to the *end* of the list. */
185 ZERO_STRUCT(sconn->fsp_fi_cache);
191 NTSTATUS create_internal_fsp(connection_struct *conn,
192 const struct smb_filename *smb_fname,
193 struct files_struct **_fsp)
195 struct files_struct *fsp = NULL;
198 status = file_new(NULL, conn, &fsp);
199 if (!NT_STATUS_IS_OK(status)) {
203 status = fsp_set_smb_fname(fsp, smb_fname);
204 if (!NT_STATUS_IS_OK(status)) {
205 file_free(NULL, fsp);
214 * Create an internal fsp for an *existing* directory.
216 * This should only be used by callers in the VFS that need to control the
217 * opening of the directory. Otherwise use open_internal_dirfsp_at().
219 NTSTATUS create_internal_dirfsp(connection_struct *conn,
220 const struct smb_filename *smb_dname,
221 struct files_struct **_fsp)
223 struct files_struct *fsp = NULL;
226 status = create_internal_fsp(conn, smb_dname, &fsp);
227 if (!NT_STATUS_IS_OK(status)) {
231 fsp->access_mask = FILE_LIST_DIRECTORY;
232 fsp->fsp_flags.is_directory = true;
233 fsp->fsp_flags.is_dirfsp = true;
240 * Open an internal fsp for an *existing* directory.
242 NTSTATUS open_internal_dirfsp(connection_struct *conn,
243 const struct smb_filename *smb_dname,
245 struct files_struct **_fsp)
247 struct vfs_open_how how = { .flags = _open_flags, };
248 struct files_struct *fsp = NULL;
251 status = create_internal_dirfsp(conn, smb_dname, &fsp);
252 if (!NT_STATUS_IS_OK(status)) {
257 how.flags |= O_DIRECTORY;
259 status = fd_openat(conn->cwd_fsp, fsp->fsp_name, fsp, &how);
260 if (!NT_STATUS_IS_OK(status)) {
261 DBG_INFO("Could not open fd for %s (%s)\n",
262 smb_fname_str_dbg(smb_dname),
264 file_free(NULL, fsp);
268 status = vfs_stat_fsp(fsp);
269 if (!NT_STATUS_IS_OK(status)) {
270 file_free(NULL, fsp);
274 if (!S_ISDIR(fsp->fsp_name->st.st_ex_mode)) {
275 DBG_ERR("%s is not a directory!\n",
276 smb_fname_str_dbg(smb_dname));
277 file_free(NULL, fsp);
278 return NT_STATUS_NOT_A_DIRECTORY;
281 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
288 * Convert a pathref dirfsp into a real fsp. No need to do any cwd
289 * tricks, we just open ".".
291 NTSTATUS openat_internal_dir_from_pathref(
292 struct files_struct *dirfsp,
294 struct files_struct **_fsp)
296 struct connection_struct *conn = dirfsp->conn;
297 struct smb_filename *smb_dname = dirfsp->fsp_name;
298 struct files_struct *fsp = NULL;
300 struct smb_filename smb_dot = {
302 .flags = smb_dname->flags,
303 .twrp = smb_dname->twrp,
305 struct vfs_open_how how = { .flags = _open_flags, };
308 status = create_internal_dirfsp(conn, smb_dname, &fsp);
309 if (!NT_STATUS_IS_OK(status)) {
314 * Pointless for opening ".", but you never know...
316 how.flags |= O_NOFOLLOW;
318 status = fd_openat(dirfsp, &smb_dot, fsp, &how);
319 if (!NT_STATUS_IS_OK(status)) {
320 DBG_INFO("fd_openat(\"%s\", \".\") failed: %s\n",
323 file_free(NULL, fsp);
327 fsp->fsp_name->st = smb_dname->st;
328 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
334 * The "link" in the name doesn't imply link in the filesystem
335 * sense. It's a object that "links" together an fsp and an smb_fname
336 * and the link allocated as talloc child of an fsp.
338 * The link is created for fsps that openat_pathref_fsp() returns in
339 * smb_fname->fsp. When this fsp is freed by file_free() by some caller
340 * somewhere, the destructor fsp_smb_fname_link_destructor() on the link object
341 * will use the link to reset the reference in smb_fname->fsp that is about to
344 * This prevents smb_fname_internal_fsp_destructor() from seeing dangling fsp
348 struct fsp_smb_fname_link {
349 struct fsp_smb_fname_link **smb_fname_link;
350 struct files_struct **smb_fname_fsp;
353 static int fsp_smb_fname_link_destructor(struct fsp_smb_fname_link *link)
355 if (link->smb_fname_link == NULL) {
359 *link->smb_fname_link = NULL;
360 *link->smb_fname_fsp = NULL;
364 static NTSTATUS fsp_smb_fname_link(struct files_struct *fsp,
365 struct fsp_smb_fname_link **smb_fname_link,
366 struct files_struct **smb_fname_fsp)
368 struct fsp_smb_fname_link *link = NULL;
370 SMB_ASSERT(*smb_fname_link == NULL);
371 SMB_ASSERT(*smb_fname_fsp == NULL);
373 link = talloc_zero(fsp, struct fsp_smb_fname_link);
375 return NT_STATUS_NO_MEMORY;
378 link->smb_fname_link = smb_fname_link;
379 link->smb_fname_fsp = smb_fname_fsp;
380 *smb_fname_link = link;
381 *smb_fname_fsp = fsp;
383 talloc_set_destructor(link, fsp_smb_fname_link_destructor);
388 * Free a link, carefully avoiding to trigger the link destructor
390 static void destroy_fsp_smb_fname_link(struct fsp_smb_fname_link **_link)
392 struct fsp_smb_fname_link *link = *_link;
397 talloc_set_destructor(link, NULL);
403 * Talloc destructor set on an smb_fname set by openat_pathref_fsp() used to
404 * close the embedded smb_fname->fsp.
406 static int smb_fname_fsp_destructor(struct smb_filename *smb_fname)
408 struct files_struct *fsp = smb_fname->fsp;
410 int saved_errno = errno;
412 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
419 if (fsp_is_alternate_stream(fsp)) {
420 struct files_struct *tmp_base_fsp = fsp->base_fsp;
422 fsp_set_base_fsp(fsp, NULL);
424 status = fd_close(tmp_base_fsp);
425 if (!NT_STATUS_IS_OK(status)) {
426 DBG_ERR("Closing fd for fsp [%s] failed: %s. "
427 "Please check your filesystem!!!\n",
428 fsp_str_dbg(fsp), nt_errstr(status));
430 file_free(NULL, tmp_base_fsp);
433 status = fd_close(fsp);
434 if (!NT_STATUS_IS_OK(status)) {
435 DBG_ERR("Closing fd for fsp [%s] failed: %s. "
436 "Please check your filesystem!!!\n",
437 fsp_str_dbg(fsp), nt_errstr(status));
439 file_free(NULL, fsp);
440 smb_fname->fsp = NULL;
446 static NTSTATUS openat_pathref_fullname(
447 struct connection_struct *conn,
448 const struct files_struct *dirfsp,
449 struct files_struct *basefsp,
450 struct smb_filename **full_fname,
451 struct smb_filename *smb_fname,
452 const struct vfs_open_how *how)
454 struct files_struct *fsp = NULL;
455 bool have_dirfsp = (dirfsp != NULL);
456 bool have_basefsp = (basefsp != NULL);
459 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
461 SMB_ASSERT(smb_fname->fsp == NULL);
462 SMB_ASSERT(have_dirfsp != have_basefsp);
464 status = fsp_new(conn, conn, &fsp);
465 if (!NT_STATUS_IS_OK(status)) {
469 GetTimeOfDay(&fsp->open_time);
471 ZERO_STRUCT(conn->sconn->fsp_fi_cache);
473 fsp->fsp_flags.is_pathref = true;
475 status = fsp_attach_smb_fname(fsp, full_fname);
476 if (!NT_STATUS_IS_OK(status)) {
479 fsp_set_base_fsp(fsp, basefsp);
481 status = fd_openat(dirfsp, smb_fname, fsp, how);
482 if (!NT_STATUS_IS_OK(status)) {
484 smb_fname->st = fsp->fsp_name->st;
486 if (NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ||
487 NT_STATUS_EQUAL(status, NT_STATUS_OBJECT_PATH_NOT_FOUND) ||
488 NT_STATUS_EQUAL(status, NT_STATUS_STOPPED_ON_SYMLINK))
491 * streams_xattr return NT_STATUS_NOT_FOUND for
492 * opens of not yet existing streams.
494 * ELOOP maps to NT_STATUS_OBJECT_PATH_NOT_FOUND
495 * and this will result from a open request from
496 * a POSIX client on a symlink.
498 * NT_STATUS_OBJECT_NAME_NOT_FOUND is the simple
501 * NT_STATUS_STOPPED_ON_SYMLINK is returned when trying
502 * to open a symlink, our callers are not interested in
505 status = NT_STATUS_OBJECT_NAME_NOT_FOUND;
511 * fd_openat() has done an FSTAT on the handle
512 * so update the smb_fname stat info with "truth".
515 smb_fname->st = fsp->fsp_name->st;
517 fsp->fsp_flags.is_directory = S_ISDIR(fsp->fsp_name->st.st_ex_mode);
519 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
521 status = fsp_smb_fname_link(fsp,
522 &smb_fname->fsp_link,
524 if (!NT_STATUS_IS_OK(status)) {
528 DBG_DEBUG("fsp [%s]: OK\n", fsp_str_dbg(fsp));
530 talloc_set_destructor(smb_fname, smb_fname_fsp_destructor);
534 DBG_DEBUG("Opening pathref for [%s] failed: %s\n",
535 smb_fname_str_dbg(smb_fname),
538 fsp_set_base_fsp(fsp, NULL);
540 file_free(NULL, fsp);
545 * Open an internal O_PATH based fsp for smb_fname. If O_PATH is not
546 * available, open O_RDONLY as root. Both is done in fd_open() ->
547 * non_widelink_open(), triggered by setting fsp->fsp_flags.is_pathref to
550 NTSTATUS openat_pathref_fsp(const struct files_struct *dirfsp,
551 struct smb_filename *smb_fname)
553 connection_struct *conn = dirfsp->conn;
554 struct smb_filename *full_fname = NULL;
555 struct smb_filename *base_fname = NULL;
556 struct vfs_open_how how = { .flags = O_RDONLY|O_NONBLOCK, };
559 DBG_DEBUG("smb_fname [%s]\n", smb_fname_str_dbg(smb_fname));
561 if (smb_fname->fsp != NULL) {
562 /* We already have one for this name. */
563 DBG_DEBUG("smb_fname [%s] already has a pathref fsp.\n",
564 smb_fname_str_dbg(smb_fname));
568 if (is_named_stream(smb_fname) &&
569 ((conn->fs_capabilities & FILE_NAMED_STREAMS) == 0)) {
570 DBG_DEBUG("stream open [%s] on non-stream share\n",
571 smb_fname_str_dbg(smb_fname));
572 return NT_STATUS_OBJECT_NAME_INVALID;
575 if (!is_named_stream(smb_fname)) {
577 * openat_pathref_fullname() will make "full_fname" a
578 * talloc child of the smb_fname->fsp. Don't use
579 * talloc_tos() to allocate it to avoid making the
580 * talloc stackframe pool long-lived.
582 full_fname = full_path_from_dirfsp_atname(
586 if (full_fname == NULL) {
587 status = NT_STATUS_NO_MEMORY;
590 status = openat_pathref_fullname(
591 conn, dirfsp, NULL, &full_fname, smb_fname, &how);
592 TALLOC_FREE(full_fname);
599 base_fname = cp_smb_filename_nostream(conn, smb_fname);
600 if (base_fname == NULL) {
601 return NT_STATUS_NO_MEMORY;
604 full_fname = full_path_from_dirfsp_atname(
605 conn, /* no talloc_tos(), see comment above */
608 if (full_fname == NULL) {
609 status = NT_STATUS_NO_MEMORY;
613 status = openat_pathref_fullname(
614 conn, dirfsp, NULL, &full_fname, base_fname, &how);
615 TALLOC_FREE(full_fname);
616 if (!NT_STATUS_IS_OK(status)) {
617 DBG_DEBUG("openat_pathref_nostream failed: %s\n",
622 status = open_stream_pathref_fsp(&base_fname->fsp, smb_fname);
623 if (!NT_STATUS_IS_OK(status)) {
624 DBG_DEBUG("open_stream_pathref_fsp failed: %s\n",
629 smb_fname_fsp_unlink(base_fname);
631 TALLOC_FREE(base_fname);
636 * Open a stream given an already opened base_fsp. Avoid
637 * non_widelink_open: This is only valid for the case where we have a
638 * valid non-cwd_fsp dirfsp that we can pass to SMB_VFS_OPENAT()
640 NTSTATUS open_stream_pathref_fsp(
641 struct files_struct **_base_fsp,
642 struct smb_filename *smb_fname)
644 struct files_struct *base_fsp = *_base_fsp;
645 connection_struct *conn = base_fsp->conn;
646 struct smb_filename *base_fname = base_fsp->fsp_name;
647 struct smb_filename *full_fname = NULL;
648 struct vfs_open_how how = { .flags = O_RDONLY|O_NONBLOCK, };
651 SMB_ASSERT(smb_fname->fsp == NULL);
652 SMB_ASSERT(is_named_stream(smb_fname));
654 full_fname = synthetic_smb_fname(
655 conn, /* no talloc_tos(), this will be long-lived */
656 base_fname->base_name,
657 smb_fname->stream_name,
661 if (full_fname == NULL) {
662 return NT_STATUS_NO_MEMORY;
665 status = openat_pathref_fullname(
666 conn, NULL, base_fsp, &full_fname, smb_fname, &how);
667 TALLOC_FREE(full_fname);
671 static char *path_to_strv(TALLOC_CTX *mem_ctx, const char *path)
673 char *result = talloc_strdup(mem_ctx, path);
675 if (result == NULL) {
678 string_replace(result, '/', '\0');
682 NTSTATUS readlink_talloc(
684 struct files_struct *dirfsp,
685 struct smb_filename *smb_relname,
693 if (_substitute == NULL) {
697 if (smb_relname == NULL) {
699 * We have a Linux O_PATH handle in dirfsp and want to
700 * read its value, essentially a freadlink
702 smb_relname = synthetic_smb_fname(
703 talloc_tos(), "", NULL, NULL, 0, 0);
704 if (smb_relname == NULL) {
705 DBG_DEBUG("synthetic_smb_fname() failed\n");
706 return NT_STATUS_NO_MEMORY;
710 ret = SMB_VFS_READLINKAT(
711 dirfsp->conn, dirfsp, smb_relname, buf, sizeof(buf));
713 status = map_nt_error_from_unix(errno);
714 DBG_DEBUG("SMB_VFS_READLINKAT() failed: %s\n",
719 if ((size_t)ret == sizeof(buf)) {
721 * Do we need symlink targets >4k?
723 DBG_DEBUG("Got full %zu bytes from readlink, too long\n",
725 return NT_STATUS_BUFFER_OVERFLOW;
728 substitute = talloc_strndup(mem_ctx, buf, ret);
729 if (substitute == NULL) {
730 DBG_DEBUG("talloc_strndup() failed\n");
731 return NT_STATUS_NO_MEMORY;
734 *_substitute = substitute;
738 NTSTATUS openat_pathref_dirfsp_nosymlink(
740 struct connection_struct *conn,
743 struct smb_filename **_smb_fname,
747 struct files_struct *dirfsp = conn->cwd_fsp;
748 struct smb_filename full_fname = {
752 struct smb_filename rel_fname = {
756 struct smb_filename *result = NULL;
757 struct files_struct *fsp = NULL;
758 char *path = NULL, *next = NULL;
761 struct vfs_open_how how = {
762 .flags = O_NOFOLLOW|O_DIRECTORY,
766 DBG_DEBUG("path_in=%s\n", path_in);
768 status = fsp_new(conn, conn, &fsp);
769 if (!NT_STATUS_IS_OK(status)) {
770 DBG_DEBUG("fsp_new() failed: %s\n", nt_errstr(status));
773 fsp->fsp_name = &full_fname;
777 * Add O_PATH manually, doing this by setting
778 * fsp->fsp_flags.is_pathref will make us become_root() in the
779 * non-O_PATH case, which would cause a security problem.
785 * O_SEARCH just checks for the "x" bit. We are traversing
786 * directories, so we don't need the implicit O_RDONLY ("r"
787 * permissions) but only the "x"-permissions requested by
788 * O_SEARCH. We need either O_PATH or O_SEARCH to correctly
789 * function, without either we will incorrectly require also
790 * the "r" bit when traversing the directory hierarchy.
792 how.flags |= O_SEARCH;
796 full_fname.base_name = talloc_strdup(talloc_tos(), "");
797 if (full_fname.base_name == NULL) {
798 DBG_DEBUG("talloc_strdup() failed\n");
803 * First split the path into individual components.
805 path = path_to_strv(talloc_tos(), path_in);
807 DBG_DEBUG("path_to_strv() failed\n");
812 * First we loop over all components
813 * in order to verify, there's no '.' or '..'
815 rel_fname.base_name = path;
816 while (rel_fname.base_name != NULL) {
818 next = strv_next(path, rel_fname.base_name);
820 if (ISDOT(rel_fname.base_name) ||
821 ISDOTDOT(rel_fname.base_name)) {
822 DBG_DEBUG("%s contains a dot\n", path_in);
823 status = NT_STATUS_OBJECT_NAME_INVALID;
827 /* Check veto files. */
828 if (IS_VETO_PATH(conn, rel_fname.base_name)) {
829 DBG_DEBUG("%s contains veto files path component %s\n",
830 path_in, rel_fname.base_name);
831 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
835 rel_fname.base_name = next;
838 if (conn->open_how_resolve & VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS) {
841 * Try a direct openat2 with RESOLVE_NO_SYMLINKS to
842 * avoid the openat/close loop further down.
845 rel_fname.base_name = discard_const_p(char, path_in);
846 how.resolve = VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS;
848 fd = SMB_VFS_OPENAT(conn, dirfsp, &rel_fname, fsp, &how);
851 TALLOC_FREE(full_fname.base_name);
852 full_fname = rel_fname;
856 status = map_nt_error_from_unix(errno);
857 DBG_DEBUG("SMB_VFS_OPENAT(%s, %s, RESOLVE_NO_SYMLINKS) "
858 "returned %d %s => %s\n",
859 smb_fname_str_dbg(dirfsp->fsp_name), path_in,
860 errno, strerror(errno), nt_errstr(status));
861 SMB_ASSERT(fd == -1);
865 * We got ENOSYS, so fallback to the old code
866 * if the kernel doesn't support openat2() yet.
873 * For ELOOP we also fallback in order to
874 * return the correct information with
875 * NT_STATUS_STOPPED_ON_SYMLINK.
877 * O_NOFOLLOW|O_DIRECTORY results in
878 * ENOTDIR instead of ELOOP for the final
885 * If we got ENOENT, the filesystem could
886 * be case sensitive. For now we only do
887 * the get_real_filename_at() dance in
888 * the fallback loop below.
897 * Just fallback to the openat loop
903 * Now we loop over all components
904 * opening each one and using it
905 * as dirfd for the next one.
907 * It means we can detect symlinks
910 rel_fname.base_name = path;
912 next = strv_next(path, rel_fname.base_name);
921 if ((fd == -1) && (errno == ENOENT)) {
922 const char *orig_base_name = rel_fname.base_name;
924 status = get_real_filename_at(
928 &rel_fname.base_name);
930 if (!NT_STATUS_IS_OK(status)) {
931 DBG_DEBUG("get_real_filename_at failed: %s\n",
936 /* Name might have been demangled - check veto files. */
937 if (IS_VETO_PATH(conn, rel_fname.base_name)) {
938 DBG_DEBUG("%s contains veto files path component "
942 rel_fname.base_name);
943 status = NT_STATUS_OBJECT_PATH_NOT_FOUND;
956 * O_NOFOLLOW|O_DIRECTORY results in
957 * ENOTDIR instead of ELOOP.
959 * But we should be prepared to handle ELOOP too.
961 if ((fd == -1) && (errno == ENOTDIR || errno == ELOOP)) {
962 NTSTATUS orig_status = map_nt_error_from_unix(errno);
964 status = readlink_talloc(
965 mem_ctx, dirfsp, &rel_fname, substitute);
967 if (NT_STATUS_IS_OK(status)) {
969 * readlink_talloc() found a symlink
971 status = NT_STATUS_STOPPED_ON_SYMLINK;
973 if (unparsed != NULL) {
977 size_t parsed = next - path;
978 size_t len = talloc_get_size(path);
979 *unparsed = len - parsed;
983 * If we're on an MSDFS share, see if this is
986 if (lp_host_msdfs() &&
987 lp_msdfs_root(SNUM(conn)) &&
988 (substitute != NULL) &&
989 strnequal(*substitute, "msdfs:", 6) &&
990 is_msdfs_link(dirfsp, &rel_fname))
992 status = NT_STATUS_PATH_NOT_COVERED;
996 DBG_DEBUG("readlink_talloc failed: %s\n",
999 * Restore the error status from SMB_VFS_OPENAT()
1001 status = orig_status;
1007 status = map_nt_error_from_unix(errno);
1008 DBG_DEBUG("SMB_VFS_OPENAT() failed: %s\n",
1012 fsp_set_fd(fsp, fd);
1014 fsp->fsp_flags.is_directory = true; /* See O_DIRECTORY above */
1016 full_fname.base_name = talloc_asprintf_append_buffer(
1017 full_fname.base_name,
1019 full_fname.base_name[0] == '\0' ? "" : "/",
1020 rel_fname.base_name);
1022 if (full_fname.base_name == NULL) {
1023 DBG_DEBUG("talloc_asprintf_append_buffer() failed\n");
1028 struct files_struct *tmp = NULL;
1030 if (dirfsp != conn->cwd_fsp) {
1037 if (tmp == conn->cwd_fsp) {
1038 status = fsp_new(conn, conn, &fsp);
1039 if (!NT_STATUS_IS_OK(status)) {
1040 DBG_DEBUG("fsp_new() failed: %s\n",
1044 fsp->fsp_name = &full_fname;
1049 rel_fname.base_name = next;
1054 if (dirfsp != conn->cwd_fsp) {
1055 dirfsp->fsp_name = NULL;
1056 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) != -1);
1058 file_free(NULL, dirfsp);
1063 fsp->fsp_flags.is_pathref = true;
1064 fsp->fsp_name = NULL;
1066 status = fsp_set_smb_fname(fsp, &full_fname);
1067 if (!NT_STATUS_IS_OK(status)) {
1068 DBG_DEBUG("fsp_set_smb_fname() failed: %s\n",
1073 status = vfs_stat_fsp(fsp);
1074 if (!NT_STATUS_IS_OK(status)) {
1075 DBG_DEBUG("vfs_stat_fsp(%s) failed: %s\n",
1081 * We must correctly set fsp->file_id as code inside
1082 * open.c will use this to check if delete_on_close
1083 * has been set on the dirfsp.
1085 fsp->file_id = vfs_file_id_from_sbuf(conn, &fsp->fsp_name->st);
1087 result = cp_smb_filename(mem_ctx, fsp->fsp_name);
1088 if (result == NULL) {
1089 DBG_DEBUG("cp_smb_filename() failed\n");
1093 status = fsp_smb_fname_link(fsp,
1096 if (!NT_STATUS_IS_OK(status)) {
1099 talloc_set_destructor(result, smb_fname_fsp_destructor);
1101 *_smb_fname = result;
1103 DBG_DEBUG("returning %s\n", smb_fname_str_dbg(result));
1105 return NT_STATUS_OK;
1108 status = NT_STATUS_NO_MEMORY;
1111 if (fsp_get_pathref_fd(fsp) != -1) {
1114 file_free(NULL, fsp);
1118 if ((dirfsp != NULL) && (dirfsp != conn->cwd_fsp)) {
1119 dirfsp->fsp_name = NULL;
1120 SMB_ASSERT(fsp_get_pathref_fd(dirfsp) != -1);
1122 file_free(NULL, dirfsp);
1130 void smb_fname_fsp_unlink(struct smb_filename *smb_fname)
1132 talloc_set_destructor(smb_fname, NULL);
1133 smb_fname->fsp = NULL;
1134 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
1138 * Move any existing embedded fsp refs from the src name to the
1139 * destination. It's safe to call this on src smb_fname's that have no embedded
1142 NTSTATUS move_smb_fname_fsp_link(struct smb_filename *smb_fname_dst,
1143 struct smb_filename *smb_fname_src)
1148 * The target should always not be linked yet!
1150 SMB_ASSERT(smb_fname_dst->fsp == NULL);
1151 SMB_ASSERT(smb_fname_dst->fsp_link == NULL);
1153 if (smb_fname_src->fsp == NULL) {
1154 return NT_STATUS_OK;
1157 status = fsp_smb_fname_link(smb_fname_src->fsp,
1158 &smb_fname_dst->fsp_link,
1159 &smb_fname_dst->fsp);
1160 if (!NT_STATUS_IS_OK(status)) {
1164 talloc_set_destructor(smb_fname_dst, smb_fname_fsp_destructor);
1166 smb_fname_fsp_unlink(smb_fname_src);
1168 return NT_STATUS_OK;
1172 * Create an smb_fname and open smb_fname->fsp pathref
1174 NTSTATUS synthetic_pathref(TALLOC_CTX *mem_ctx,
1175 struct files_struct *dirfsp,
1176 const char *base_name,
1177 const char *stream_name,
1178 const SMB_STRUCT_STAT *psbuf,
1181 struct smb_filename **_smb_fname)
1183 struct smb_filename *smb_fname = NULL;
1186 smb_fname = synthetic_smb_fname(mem_ctx,
1192 if (smb_fname == NULL) {
1193 return NT_STATUS_NO_MEMORY;
1196 status = openat_pathref_fsp(dirfsp, smb_fname);
1197 if (!NT_STATUS_IS_OK(status)) {
1198 DBG_ERR("opening [%s] failed\n",
1199 smb_fname_str_dbg(smb_fname));
1200 TALLOC_FREE(smb_fname);
1204 *_smb_fname = smb_fname;
1205 return NT_STATUS_OK;
1208 static int atname_destructor(struct smb_filename *smb_fname)
1210 destroy_fsp_smb_fname_link(&smb_fname->fsp_link);
1215 * Turn a path into a parent pathref and atname
1217 * This returns the parent pathref in _parent and the name relative to it. If
1218 * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
1219 * pathref as well, ie _atname->fsp will point at the same fsp as
1222 NTSTATUS parent_pathref(TALLOC_CTX *mem_ctx,
1223 struct files_struct *dirfsp,
1224 const struct smb_filename *smb_fname,
1225 struct smb_filename **_parent,
1226 struct smb_filename **_atname)
1228 struct smb_filename *parent = NULL;
1229 struct smb_filename *atname = NULL;
1232 status = SMB_VFS_PARENT_PATHNAME(dirfsp->conn,
1237 if (!NT_STATUS_IS_OK(status)) {
1242 * We know that the parent name must
1243 * exist, and the name has been canonicalized
1244 * even if this was a POSIX pathname.
1245 * Ensure that we follow symlinks for
1246 * the parent. See the torture test
1247 * POSIX-SYMLINK-PARENT for details.
1249 parent->flags &= ~SMB_FILENAME_POSIX_PATH;
1251 status = openat_pathref_fsp(dirfsp, parent);
1252 if (!NT_STATUS_IS_OK(status)) {
1253 TALLOC_FREE(parent);
1257 if (smb_fname->fsp != NULL) {
1258 status = fsp_smb_fname_link(smb_fname->fsp,
1261 if (!NT_STATUS_IS_OK(status)) {
1262 TALLOC_FREE(parent);
1265 talloc_set_destructor(atname, atname_destructor);
1269 return NT_STATUS_OK;
1272 static bool close_file_in_loop(struct files_struct *fsp,
1273 enum file_close_type close_type)
1275 if (fsp_is_alternate_stream(fsp)) {
1277 * This is a stream, it can't be a base
1279 SMB_ASSERT(fsp->stream_fsp == NULL);
1280 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
1283 * Remove the base<->stream link so that
1284 * close_file_free() does not close fsp->base_fsp as
1285 * well. This would destroy walking the linked list of
1288 fsp->base_fsp->stream_fsp = NULL;
1289 fsp->base_fsp = NULL;
1291 close_file_free(NULL, &fsp, close_type);
1295 if (fsp->stream_fsp != NULL) {
1297 * This is the base of a stream.
1299 SMB_ASSERT(fsp->stream_fsp->base_fsp == fsp);
1302 * Remove the base<->stream link. This will make fsp
1303 * look like a normal fsp for the next round.
1305 fsp->stream_fsp->base_fsp = NULL;
1306 fsp->stream_fsp = NULL;
1309 * Have us called back a second time. In the second
1310 * round, "fsp" now looks like a normal fsp.
1315 close_file_free(NULL, &fsp, close_type);
1319 /****************************************************************************
1320 Close all open files for a connection.
1321 ****************************************************************************/
1323 struct file_close_conn_state {
1324 struct connection_struct *conn;
1325 enum file_close_type close_type;
1326 bool fsp_left_behind;
1329 static struct files_struct *file_close_conn_fn(
1330 struct files_struct *fsp,
1333 struct file_close_conn_state *state = private_data;
1336 if (fsp->conn != state->conn) {
1340 if (fsp->op != NULL && fsp->op->global->durable) {
1342 * A tree disconnect closes a durable handle
1344 fsp->op->global->durable = false;
1347 did_close = close_file_in_loop(fsp, state->close_type);
1349 state->fsp_left_behind = true;
1355 void file_close_conn(connection_struct *conn, enum file_close_type close_type)
1357 struct file_close_conn_state state = { .conn = conn,
1358 .close_type = close_type };
1360 files_forall(conn->sconn, file_close_conn_fn, &state);
1362 if (state.fsp_left_behind) {
1363 state.fsp_left_behind = false;
1364 files_forall(conn->sconn, file_close_conn_fn, &state);
1365 SMB_ASSERT(!state.fsp_left_behind);
1369 /****************************************************************************
1370 Initialise file structures.
1371 ****************************************************************************/
1373 static int files_max_open_fds;
1375 bool file_init_global(void)
1377 int request_max = lp_max_open_files();
1381 if (files_max_open_fds != 0) {
1386 * Set the max_open files to be the requested
1387 * max plus a fudgefactor to allow for the extra
1388 * fd's we need such as log files etc...
1390 real_lim = set_maxfiles(request_max + MAX_OPEN_FUDGEFACTOR);
1392 real_max = real_lim - MAX_OPEN_FUDGEFACTOR;
1394 if (real_max + FILE_HANDLE_OFFSET + MAX_OPEN_PIPES > 65536) {
1395 real_max = 65536 - FILE_HANDLE_OFFSET - MAX_OPEN_PIPES;
1398 if (real_max != request_max) {
1399 DEBUG(1, ("file_init_global: Information only: requested %d "
1400 "open files, %d are available.\n",
1401 request_max, real_max));
1404 SMB_ASSERT(real_max > 100);
1406 files_max_open_fds = real_max;
1410 bool file_init(struct smbd_server_connection *sconn)
1414 ok = file_init_global();
1419 sconn->real_max_open_files = files_max_open_fds;
1424 /****************************************************************************
1425 Close files open by a specified vuid.
1426 ****************************************************************************/
1428 struct file_close_user_state {
1430 bool fsp_left_behind;
1433 static struct files_struct *file_close_user_fn(
1434 struct files_struct *fsp,
1437 struct file_close_user_state *state = private_data;
1440 if (fsp->vuid != state->vuid) {
1444 did_close = close_file_in_loop(fsp, SHUTDOWN_CLOSE);
1446 state->fsp_left_behind = true;
1452 void file_close_user(struct smbd_server_connection *sconn, uint64_t vuid)
1454 struct file_close_user_state state = { .vuid = vuid };
1456 files_forall(sconn, file_close_user_fn, &state);
1458 if (state.fsp_left_behind) {
1459 state.fsp_left_behind = false;
1460 files_forall(sconn, file_close_user_fn, &state);
1461 SMB_ASSERT(!state.fsp_left_behind);
1466 * Walk the files table until "fn" returns non-NULL
1469 struct files_struct *files_forall(
1470 struct smbd_server_connection *sconn,
1471 struct files_struct *(*fn)(struct files_struct *fsp,
1472 void *private_data),
1475 struct files_struct *fsp, *next;
1477 for (fsp = sconn->files; fsp; fsp = next) {
1478 struct files_struct *ret;
1480 ret = fn(fsp, private_data);
1488 /****************************************************************************
1489 Find a fsp given a file descriptor.
1490 ****************************************************************************/
1492 files_struct *file_find_fd(struct smbd_server_connection *sconn, int fd)
1497 for (fsp=sconn->files; fsp; fsp=fsp->next,count++) {
1498 if (fsp_get_pathref_fd(fsp) == fd) {
1500 DLIST_PROMOTE(sconn->files, fsp);
1509 /****************************************************************************
1510 Find a fsp given a device, inode and file_id.
1511 ****************************************************************************/
1513 files_struct *file_find_dif(struct smbd_server_connection *sconn,
1514 struct file_id id, unsigned long gen_id)
1523 for (fsp = sconn->files; fsp; fsp = fsp->next,count++) {
1525 * We can have a fsp->fh->fd == -1 here as it could be a stat
1528 if (!file_id_equal(&fsp->file_id, &id)) {
1531 if (!fsp->fsp_flags.is_fsa) {
1534 if (fh_get_gen_id(fsp->fh) != gen_id) {
1538 DLIST_PROMOTE(sconn->files, fsp);
1540 /* Paranoia check. */
1541 if ((fsp_get_pathref_fd(fsp) == -1) &&
1542 (fsp->oplock_type != NO_OPLOCK &&
1543 fsp->oplock_type != LEASE_OPLOCK))
1545 struct file_id_buf idbuf;
1547 DBG_ERR("file %s file_id = "
1548 "%s, gen = %u oplock_type = %u is a "
1549 "stat open with oplock type !\n",
1551 file_id_str_buf(fsp->file_id, &idbuf),
1552 (unsigned int)fh_get_gen_id(fsp->fh),
1553 (unsigned int)fsp->oplock_type);
1554 smb_panic("file_find_dif");
1562 /****************************************************************************
1563 Find the first fsp given a device and inode.
1564 We use a singleton cache here to speed up searching from getfilepathinfo
1566 ****************************************************************************/
1568 files_struct *file_find_di_first(struct smbd_server_connection *sconn,
1574 if (file_id_equal(&sconn->fsp_fi_cache.id, &id)) {
1575 /* Positive or negative cache hit. */
1576 return sconn->fsp_fi_cache.fsp;
1579 sconn->fsp_fi_cache.id = id;
1581 for (fsp=sconn->files;fsp;fsp=fsp->next) {
1582 if (need_fsa && !fsp->fsp_flags.is_fsa) {
1585 if (file_id_equal(&fsp->file_id, &id)) {
1586 /* Setup positive cache. */
1587 sconn->fsp_fi_cache.fsp = fsp;
1592 /* Setup negative cache. */
1593 sconn->fsp_fi_cache.fsp = NULL;
1597 /****************************************************************************
1598 Find the next fsp having the same device and inode.
1599 ****************************************************************************/
1601 files_struct *file_find_di_next(files_struct *start_fsp,
1606 for (fsp = start_fsp->next;fsp;fsp=fsp->next) {
1607 if (need_fsa && !fsp->fsp_flags.is_fsa) {
1610 if (file_id_equal(&fsp->file_id, &start_fsp->file_id)) {
1618 struct files_struct *file_find_one_fsp_from_lease_key(
1619 struct smbd_server_connection *sconn,
1620 const struct smb2_lease_key *lease_key)
1622 struct files_struct *fsp;
1624 for (fsp = sconn->files; fsp; fsp=fsp->next) {
1625 if ((fsp->lease != NULL) &&
1626 (fsp->lease->lease.lease_key.data[0] ==
1627 lease_key->data[0]) &&
1628 (fsp->lease->lease.lease_key.data[1] ==
1629 lease_key->data[1])) {
1636 /****************************************************************************
1637 Find any fsp open with a pathname below that of an already open path.
1638 ****************************************************************************/
1640 bool file_find_subpath(files_struct *dir_fsp)
1644 char *d_fullname = NULL;
1646 d_fullname = talloc_asprintf(talloc_tos(), "%s/%s",
1647 dir_fsp->conn->connectpath,
1648 dir_fsp->fsp_name->base_name);
1654 dlen = strlen(d_fullname);
1656 for (fsp=dir_fsp->conn->sconn->files; fsp; fsp=fsp->next) {
1659 if (fsp == dir_fsp) {
1663 d1_fullname = talloc_asprintf(talloc_tos(),
1665 fsp->conn->connectpath,
1666 fsp->fsp_name->base_name);
1669 * If the open file has a path that is a longer
1670 * component, then it's a subpath.
1672 if (strnequal(d_fullname, d1_fullname, dlen) &&
1673 (d1_fullname[dlen] == '/')) {
1674 TALLOC_FREE(d1_fullname);
1675 TALLOC_FREE(d_fullname);
1678 TALLOC_FREE(d1_fullname);
1681 TALLOC_FREE(d_fullname);
1685 /****************************************************************************
1687 ****************************************************************************/
1689 static void fsp_free(files_struct *fsp)
1691 struct smbd_server_connection *sconn = fsp->conn->sconn;
1693 if (fsp == sconn->fsp_fi_cache.fsp) {
1694 ZERO_STRUCT(sconn->fsp_fi_cache);
1697 DLIST_REMOVE(sconn->files, fsp);
1698 SMB_ASSERT(sconn->num_files > 0);
1701 TALLOC_FREE(fsp->fake_file_handle);
1703 if (fh_get_refcount(fsp->fh) == 1) {
1704 TALLOC_FREE(fsp->fh);
1706 size_t new_refcount = fh_get_refcount(fsp->fh) - 1;
1707 fh_set_refcount(fsp->fh, new_refcount);
1710 if (fsp->lease != NULL) {
1711 if (fsp->lease->ref_count == 1) {
1712 TALLOC_FREE(fsp->lease);
1714 fsp->lease->ref_count--;
1718 fsp->conn->num_files_open--;
1720 if (fsp->fsp_name != NULL &&
1721 fsp->fsp_name->fsp_link != NULL)
1724 * Free fsp_link of fsp->fsp_name. To do this in the correct
1725 * talloc destructor order we have to do it here. The
1726 * talloc_free() of the link should set the fsp pointer to NULL.
1728 TALLOC_FREE(fsp->fsp_name->fsp_link);
1729 SMB_ASSERT(fsp->fsp_name->fsp == NULL);
1732 /* this is paranoia, just in case someone tries to reuse the
1736 /* fsp->fsp_name is a talloc child and is free'd automatically. */
1741 * Rundown of all smb-related sub-structures of an fsp
1743 void fsp_unbind_smb(struct smb_request *req, files_struct *fsp)
1745 if (fsp == fsp->conn->cwd_fsp) {
1750 size_t len = fsp_fullbasepath(fsp, NULL, 0);
1751 char fullpath[len+1];
1753 fsp_fullbasepath(fsp, fullpath, sizeof(fullpath));
1756 * Avoid /. at the end of the path name. notify can't
1759 if (len > 1 && fullpath[len-1] == '.' &&
1760 fullpath[len-2] == '/') {
1761 fullpath[len-2] = '\0';
1764 notify_remove(fsp->conn->sconn->notify_ctx, fsp, fullpath);
1765 TALLOC_FREE(fsp->notify);
1768 /* Ensure this event will never fire. */
1769 TALLOC_FREE(fsp->update_write_time_event);
1771 if (fsp->op != NULL) {
1772 fsp->op->compat = NULL;
1774 TALLOC_FREE(fsp->op);
1776 if ((req != NULL) && (fsp == req->chain_fsp)) {
1777 req->chain_fsp = NULL;
1781 * Clear all possible chained fsp
1782 * pointers in the SMB2 request queue.
1784 remove_smb2_chained_fsp(fsp);
1787 void file_free(struct smb_request *req, files_struct *fsp)
1789 struct smbd_server_connection *sconn = fsp->conn->sconn;
1790 uint64_t fnum = fsp->fnum;
1792 fsp_unbind_smb(req, fsp);
1794 /* Drop all remaining extensions. */
1795 vfs_remove_all_fsp_extensions(fsp);
1799 DBG_INFO("freed files structure %"PRIu64" (%zu used)\n",
1804 /****************************************************************************
1805 Get an fsp from a packet given a 16 bit fnum.
1806 ****************************************************************************/
1808 files_struct *file_fsp(struct smb_request *req, uint16_t fid)
1810 struct smbXsrv_open *op;
1817 * We should never get here. req==NULL could in theory
1818 * only happen from internal opens with a non-zero
1819 * root_dir_fid. Internal opens just don't do that, at
1820 * least they are not supposed to do so. And if they
1821 * start to do so, they better fake up a smb_request
1822 * from which we get the right smbd_server_conn. While
1823 * this should never happen, let's return NULL here.
1828 if (req->chain_fsp != NULL) {
1829 if (req->chain_fsp->fsp_flags.closing) {
1832 return req->chain_fsp;
1835 if (req->xconn == NULL) {
1839 now = timeval_to_nttime(&req->request_time);
1841 status = smb1srv_open_lookup(req->xconn,
1843 if (!NT_STATUS_IS_OK(status)) {
1852 if (fsp->fsp_flags.closing) {
1856 req->chain_fsp = fsp;
1857 fsp->fsp_name->st.cached_dos_attributes = FILE_ATTRIBUTES_INVALID;
1861 struct files_struct *file_fsp_get(struct smbd_smb2_request *smb2req,
1862 uint64_t persistent_id,
1863 uint64_t volatile_id)
1865 struct smbXsrv_open *op;
1868 struct files_struct *fsp;
1870 now = timeval_to_nttime(&smb2req->request_time);
1872 status = smb2srv_open_lookup(smb2req->xconn,
1873 persistent_id, volatile_id,
1875 if (!NT_STATUS_IS_OK(status)) {
1884 if (smb2req->tcon == NULL) {
1888 if (smb2req->tcon->compat != fsp->conn) {
1892 if (smb2req->session == NULL) {
1896 if (smb2req->session->global->session_wire_id != fsp->vuid) {
1900 if (fsp->fsp_flags.closing) {
1904 fsp->fsp_name->st.cached_dos_attributes = FILE_ATTRIBUTES_INVALID;
1909 struct files_struct *file_fsp_smb2(struct smbd_smb2_request *smb2req,
1910 uint64_t persistent_id,
1911 uint64_t volatile_id)
1913 struct files_struct *fsp;
1915 if (smb2req->compat_chain_fsp != NULL) {
1916 if (smb2req->compat_chain_fsp->fsp_flags.closing) {
1919 smb2req->compat_chain_fsp->fsp_name->st.cached_dos_attributes =
1920 FILE_ATTRIBUTES_INVALID;
1921 return smb2req->compat_chain_fsp;
1924 fsp = file_fsp_get(smb2req, persistent_id, volatile_id);
1929 smb2req->compat_chain_fsp = fsp;
1933 /****************************************************************************
1934 Duplicate the file handle part for a DOS or FCB open.
1935 ****************************************************************************/
1937 NTSTATUS dup_file_fsp(
1939 uint32_t access_mask,
1942 size_t new_refcount;
1944 /* this can never happen for print files */
1945 SMB_ASSERT(from->print_file == NULL);
1947 TALLOC_FREE(to->fh);
1950 new_refcount = fh_get_refcount(to->fh) + 1;
1951 fh_set_refcount(to->fh, new_refcount);
1953 to->file_id = from->file_id;
1954 to->initial_allocation_size = from->initial_allocation_size;
1955 to->file_pid = from->file_pid;
1956 to->vuid = from->vuid;
1957 to->open_time = from->open_time;
1958 to->access_mask = access_mask;
1959 to->oplock_type = from->oplock_type;
1960 to->fsp_flags.can_lock = from->fsp_flags.can_lock;
1961 to->fsp_flags.can_read = ((access_mask & FILE_READ_DATA) != 0);
1962 to->fsp_flags.can_write =
1963 CAN_WRITE(from->conn) &&
1964 ((access_mask & (FILE_WRITE_DATA | FILE_APPEND_DATA)) != 0);
1965 to->fsp_flags.modified = from->fsp_flags.modified;
1966 to->fsp_flags.is_directory = from->fsp_flags.is_directory;
1967 to->fsp_flags.aio_write_behind = from->fsp_flags.aio_write_behind;
1968 to->fsp_flags.is_fsa = from->fsp_flags.is_fsa;
1969 to->fsp_flags.is_pathref = from->fsp_flags.is_pathref;
1970 to->fsp_flags.have_proc_fds = from->fsp_flags.have_proc_fds;
1971 to->fsp_flags.is_dirfsp = from->fsp_flags.is_dirfsp;
1973 return fsp_set_smb_fname(to, from->fsp_name);
1977 * Return a jenkins hash of a pathname on a connection.
1980 NTSTATUS file_name_hash(connection_struct *conn,
1981 const char *name, uint32_t *p_name_hash)
1983 char tmpbuf[PATH_MAX];
1984 char *fullpath, *to_free;
1988 /* Set the hash of the full pathname. */
1990 if (name[0] == '/') {
1991 strlcpy(tmpbuf, name, sizeof(tmpbuf));
1993 len = strlen(fullpath);
1996 len = full_path_tos(conn->connectpath,
2004 return NT_STATUS_NO_MEMORY;
2006 key = (TDB_DATA) { .dptr = (uint8_t *)fullpath, .dsize = len+1 };
2007 *p_name_hash = tdb_jenkins_hash(&key);
2009 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
2011 (unsigned int)*p_name_hash ));
2013 TALLOC_FREE(to_free);
2014 return NT_STATUS_OK;
2017 static NTSTATUS fsp_attach_smb_fname(struct files_struct *fsp,
2018 struct smb_filename **_smb_fname)
2020 struct smb_filename *smb_fname_new = talloc_move(fsp, _smb_fname);
2021 const char *name_str = NULL;
2022 uint32_t name_hash = 0;
2025 name_str = smb_fname_str_dbg(smb_fname_new);
2026 if (name_str == NULL) {
2027 return NT_STATUS_NO_MEMORY;
2030 status = file_name_hash(fsp->conn,
2033 if (!NT_STATUS_IS_OK(status)) {
2037 status = fsp_smb_fname_link(fsp,
2038 &smb_fname_new->fsp_link,
2039 &smb_fname_new->fsp);
2040 if (!NT_STATUS_IS_OK(status)) {
2044 fsp->name_hash = name_hash;
2045 fsp->fsp_name = smb_fname_new;
2046 fsp->fsp_name->st.cached_dos_attributes = FILE_ATTRIBUTES_INVALID;
2048 return NT_STATUS_OK;
2052 * The only way that the fsp->fsp_name field should ever be set.
2054 NTSTATUS fsp_set_smb_fname(struct files_struct *fsp,
2055 const struct smb_filename *smb_fname_in)
2057 struct smb_filename *smb_fname_old = fsp->fsp_name;
2058 struct smb_filename *smb_fname_new = NULL;
2061 smb_fname_new = cp_smb_filename(fsp, smb_fname_in);
2062 if (smb_fname_new == NULL) {
2063 return NT_STATUS_NO_MEMORY;
2066 status = fsp_attach_smb_fname(fsp, &smb_fname_new);
2067 if (!NT_STATUS_IS_OK(status)) {
2068 TALLOC_FREE(smb_fname_new);
2072 if (smb_fname_old != NULL) {
2073 smb_fname_fsp_unlink(smb_fname_old);
2074 TALLOC_FREE(smb_fname_old);
2077 return NT_STATUS_OK;
2080 size_t fsp_fullbasepath(struct files_struct *fsp, char *buf, size_t buflen)
2083 char tmp_buf[1] = {'\0'};
2086 * Don't pass NULL buffer to snprintf (to satisfy static checker)
2087 * Some callers will call this function with NULL for buf and
2088 * 0 for buflen in order to get length of fullbasepath (without
2089 * needing to allocate or write to buf)
2093 SMB_ASSERT(buflen==0);
2096 len = snprintf(buf, buflen, "%s/%s", fsp->conn->connectpath,
2097 fsp->fsp_name->base_name);
2103 void fsp_set_base_fsp(struct files_struct *fsp, struct files_struct *base_fsp)
2105 SMB_ASSERT(fsp->stream_fsp == NULL);
2106 if (base_fsp != NULL) {
2107 SMB_ASSERT(base_fsp->base_fsp == NULL);
2108 SMB_ASSERT(base_fsp->stream_fsp == NULL);
2111 if (fsp->base_fsp != NULL) {
2112 SMB_ASSERT(fsp->base_fsp->stream_fsp == fsp);
2113 fsp->base_fsp->stream_fsp = NULL;
2116 fsp->base_fsp = base_fsp;
2117 if (fsp->base_fsp != NULL) {
2118 fsp->base_fsp->stream_fsp = fsp;
2122 bool fsp_is_alternate_stream(const struct files_struct *fsp)
2124 return (fsp->base_fsp != NULL);
2127 struct files_struct *metadata_fsp(struct files_struct *fsp)
2129 if (fsp_is_alternate_stream(fsp)) {
2130 return fsp->base_fsp;