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_fullname() 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
,
688 struct smb_filename null_fname
= {
689 .base_name
= discard_const_p(char, ""),
696 if (smb_relname
== NULL
) {
698 * We have a Linux O_PATH handle in dirfsp and want to
699 * read its value, essentially a freadlink
701 smb_relname
= &null_fname
;
704 ret
= SMB_VFS_READLINKAT(
705 dirfsp
->conn
, dirfsp
, smb_relname
, buf
, sizeof(buf
));
707 status
= map_nt_error_from_unix(errno
);
708 DBG_DEBUG("SMB_VFS_READLINKAT() failed: %s\n",
713 if ((size_t)ret
== sizeof(buf
)) {
715 * Do we need symlink targets longer than PATH_MAX?
717 DBG_DEBUG("Got full %zu bytes from readlink, too long\n",
719 return NT_STATUS_BUFFER_OVERFLOW
;
722 substitute
= talloc_strndup(mem_ctx
, buf
, ret
);
723 if (substitute
== NULL
) {
724 DBG_DEBUG("talloc_strndup() failed\n");
725 return NT_STATUS_NO_MEMORY
;
728 *_substitute
= substitute
;
732 NTSTATUS
openat_pathref_dirfsp_nosymlink(
734 struct connection_struct
*conn
,
738 struct smb_filename
**_smb_fname
,
742 struct files_struct
*dirfsp
= conn
->cwd_fsp
;
743 struct smb_filename full_fname
= {
746 .flags
= posix
? SMB_FILENAME_POSIX_PATH
: 0,
748 struct smb_filename rel_fname
= {
751 .flags
= full_fname
.flags
,
753 struct smb_filename
*result
= NULL
;
754 struct files_struct
*fsp
= NULL
;
755 char *path
= NULL
, *next
= NULL
;
759 struct vfs_open_how how
= {
760 .flags
= O_NOFOLLOW
|O_DIRECTORY
,
764 DBG_DEBUG("path_in=%s\n", path_in
);
766 status
= fsp_new(conn
, conn
, &fsp
);
767 if (!NT_STATUS_IS_OK(status
)) {
768 DBG_DEBUG("fsp_new() failed: %s\n", nt_errstr(status
));
771 fsp
->fsp_name
= &full_fname
;
775 * Add O_PATH manually, doing this by setting
776 * fsp->fsp_flags.is_pathref will make us become_root() in the
777 * non-O_PATH case, which would cause a security problem.
783 * O_SEARCH just checks for the "x" bit. We are traversing
784 * directories, so we don't need the implicit O_RDONLY ("r"
785 * permissions) but only the "x"-permissions requested by
786 * O_SEARCH. We need either O_PATH or O_SEARCH to correctly
787 * function, without either we will incorrectly require also
788 * the "r" bit when traversing the directory hierarchy.
790 how
.flags
|= O_SEARCH
;
794 full_fname
.base_name
= talloc_strdup(talloc_tos(), "");
795 if (full_fname
.base_name
== NULL
) {
796 DBG_DEBUG("talloc_strdup() failed\n");
801 * First split the path into individual components.
803 path
= path_to_strv(talloc_tos(), path_in
);
805 DBG_DEBUG("path_to_strv() failed\n");
810 * First we loop over all components
811 * in order to verify, there's no '.' or '..'
813 rel_fname
.base_name
= path
;
814 while (rel_fname
.base_name
!= NULL
) {
816 next
= strv_next(path
, rel_fname
.base_name
);
819 * Path sanitizing further up has cleaned or rejected
820 * empty path components. Assert this here.
822 SMB_ASSERT(rel_fname
.base_name
[0] != '\0');
824 if (ISDOT(rel_fname
.base_name
) ||
825 ISDOTDOT(rel_fname
.base_name
)) {
826 DBG_DEBUG("%s contains a dot\n", path_in
);
827 status
= NT_STATUS_OBJECT_NAME_INVALID
;
831 /* Check veto files. */
832 if (IS_VETO_PATH(conn
, rel_fname
.base_name
)) {
833 DBG_DEBUG("%s contains veto files path component %s\n",
834 path_in
, rel_fname
.base_name
);
835 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
839 rel_fname
.base_name
= next
;
842 if (conn
->open_how_resolve
& VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS
) {
845 * Try a direct openat2 with RESOLVE_NO_SYMLINKS to
846 * avoid the openat/close loop further down.
849 rel_fname
.base_name
= discard_const_p(char, path_in
);
850 how
.resolve
= VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS
;
852 fd
= SMB_VFS_OPENAT(conn
, dirfsp
, &rel_fname
, fsp
, &how
);
855 TALLOC_FREE(full_fname
.base_name
);
856 full_fname
= rel_fname
;
860 status
= map_nt_error_from_unix(errno
);
861 DBG_DEBUG("SMB_VFS_OPENAT(%s, %s, RESOLVE_NO_SYMLINKS) "
862 "returned %d %s => %s\n",
863 smb_fname_str_dbg(dirfsp
->fsp_name
), path_in
,
864 errno
, strerror(errno
), nt_errstr(status
));
865 SMB_ASSERT(fd
== -1);
869 * We got ENOSYS, so fallback to the old code
870 * if the kernel doesn't support openat2() yet.
877 * For ELOOP we also fallback in order to
878 * return the correct information with
879 * NT_STATUS_STOPPED_ON_SYMLINK.
881 * O_NOFOLLOW|O_DIRECTORY results in
882 * ENOTDIR instead of ELOOP for the final
889 * If we got ENOENT, the filesystem could
890 * be case sensitive. For now we only do
891 * the get_real_filename_at() dance in
892 * the fallback loop below.
901 * Just fallback to the openat loop
907 * Now we loop over all components
908 * opening each one and using it
909 * as dirfd for the next one.
911 * It means we can detect symlinks
914 rel_fname
.base_name
= path
;
916 next
= strv_next(path
, rel_fname
.base_name
);
925 case_sensitive
= (posix
|| conn
->case_sensitive
);
927 if ((fd
== -1) && (errno
== ENOENT
) && !case_sensitive
) {
928 const char *orig_base_name
= rel_fname
.base_name
;
930 status
= get_real_filename_at(
934 &rel_fname
.base_name
);
936 if (!NT_STATUS_IS_OK(status
)) {
937 DBG_DEBUG("get_real_filename_at failed: %s\n",
942 /* Name might have been demangled - check veto files. */
943 if (IS_VETO_PATH(conn
, rel_fname
.base_name
)) {
944 DBG_DEBUG("%s contains veto files path component "
948 rel_fname
.base_name
);
949 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
962 * O_NOFOLLOW|O_DIRECTORY results in
963 * ENOTDIR instead of ELOOP.
965 * But we should be prepared to handle ELOOP too.
967 if ((fd
== -1) && (errno
== ENOTDIR
|| errno
== ELOOP
)) {
968 NTSTATUS orig_status
= map_nt_error_from_unix(errno
);
970 status
= readlink_talloc(
971 mem_ctx
, dirfsp
, &rel_fname
, substitute
);
973 if (NT_STATUS_IS_OK(status
)) {
975 * readlink_talloc() found a symlink
977 status
= NT_STATUS_STOPPED_ON_SYMLINK
;
979 if (unparsed
!= NULL
) {
983 size_t parsed
= next
- path
;
984 size_t len
= talloc_get_size(path
);
985 *unparsed
= len
- parsed
;
989 * If we're on an MSDFS share, see if this is
992 if (lp_host_msdfs() &&
993 lp_msdfs_root(SNUM(conn
)) &&
994 (substitute
!= NULL
) &&
995 strnequal(*substitute
, "msdfs:", 6) &&
996 is_msdfs_link(dirfsp
, &rel_fname
))
998 status
= NT_STATUS_PATH_NOT_COVERED
;
1002 DBG_DEBUG("readlink_talloc failed: %s\n",
1005 * Restore the error status from SMB_VFS_OPENAT()
1007 status
= orig_status
;
1013 status
= map_nt_error_from_unix(errno
);
1014 DBG_DEBUG("SMB_VFS_OPENAT() failed: %s\n",
1018 fsp_set_fd(fsp
, fd
);
1020 fsp
->fsp_flags
.is_directory
= true; /* See O_DIRECTORY above */
1022 full_fname
.base_name
= talloc_asprintf_append_buffer(
1023 full_fname
.base_name
,
1025 full_fname
.base_name
[0] == '\0' ? "" : "/",
1026 rel_fname
.base_name
);
1028 if (full_fname
.base_name
== NULL
) {
1029 DBG_DEBUG("talloc_asprintf_append_buffer() failed\n");
1034 struct files_struct
*tmp
= NULL
;
1036 if (dirfsp
!= conn
->cwd_fsp
) {
1043 if (tmp
== conn
->cwd_fsp
) {
1044 status
= fsp_new(conn
, conn
, &fsp
);
1045 if (!NT_STATUS_IS_OK(status
)) {
1046 DBG_DEBUG("fsp_new() failed: %s\n",
1050 fsp
->fsp_name
= &full_fname
;
1055 rel_fname
.base_name
= next
;
1060 if (dirfsp
!= conn
->cwd_fsp
) {
1061 SMB_ASSERT(fsp_get_pathref_fd(dirfsp
) != -1);
1063 dirfsp
->fsp_name
= NULL
;
1064 file_free(NULL
, dirfsp
);
1069 fsp
->fsp_flags
.is_pathref
= true;
1070 fsp
->fsp_name
= NULL
;
1072 status
= fsp_set_smb_fname(fsp
, &full_fname
);
1073 if (!NT_STATUS_IS_OK(status
)) {
1074 DBG_DEBUG("fsp_set_smb_fname() failed: %s\n",
1079 status
= vfs_stat_fsp(fsp
);
1080 if (!NT_STATUS_IS_OK(status
)) {
1081 DBG_DEBUG("vfs_stat_fsp(%s) failed: %s\n",
1087 * We must correctly set fsp->file_id as code inside
1088 * open.c will use this to check if delete_on_close
1089 * has been set on the dirfsp.
1091 fsp
->file_id
= vfs_file_id_from_sbuf(conn
, &fsp
->fsp_name
->st
);
1093 result
= cp_smb_filename(mem_ctx
, fsp
->fsp_name
);
1094 if (result
== NULL
) {
1095 DBG_DEBUG("cp_smb_filename() failed\n");
1099 status
= fsp_smb_fname_link(fsp
,
1102 if (!NT_STATUS_IS_OK(status
)) {
1105 talloc_set_destructor(result
, smb_fname_fsp_destructor
);
1107 *_smb_fname
= result
;
1109 DBG_DEBUG("returning %s\n", smb_fname_str_dbg(result
));
1111 return NT_STATUS_OK
;
1114 status
= NT_STATUS_NO_MEMORY
;
1117 if (fsp_get_pathref_fd(fsp
) != -1) {
1120 file_free(NULL
, fsp
);
1124 if ((dirfsp
!= NULL
) && (dirfsp
!= conn
->cwd_fsp
)) {
1125 SMB_ASSERT(fsp_get_pathref_fd(dirfsp
) != -1);
1127 dirfsp
->fsp_name
= NULL
;
1128 file_free(NULL
, dirfsp
);
1136 void smb_fname_fsp_unlink(struct smb_filename
*smb_fname
)
1138 talloc_set_destructor(smb_fname
, NULL
);
1139 smb_fname
->fsp
= NULL
;
1140 destroy_fsp_smb_fname_link(&smb_fname
->fsp_link
);
1144 * Move any existing embedded fsp refs from the src name to the
1145 * destination. It's safe to call this on src smb_fname's that have no embedded
1148 NTSTATUS
move_smb_fname_fsp_link(struct smb_filename
*smb_fname_dst
,
1149 struct smb_filename
*smb_fname_src
)
1154 * The target should always not be linked yet!
1156 SMB_ASSERT(smb_fname_dst
->fsp
== NULL
);
1157 SMB_ASSERT(smb_fname_dst
->fsp_link
== NULL
);
1159 if (smb_fname_src
->fsp
== NULL
) {
1160 return NT_STATUS_OK
;
1163 status
= fsp_smb_fname_link(smb_fname_src
->fsp
,
1164 &smb_fname_dst
->fsp_link
,
1165 &smb_fname_dst
->fsp
);
1166 if (!NT_STATUS_IS_OK(status
)) {
1170 talloc_set_destructor(smb_fname_dst
, smb_fname_fsp_destructor
);
1172 smb_fname_fsp_unlink(smb_fname_src
);
1174 return NT_STATUS_OK
;
1177 static int fsp_ref_no_close_destructor(struct smb_filename
*smb_fname
)
1179 destroy_fsp_smb_fname_link(&smb_fname
->fsp_link
);
1183 NTSTATUS
reference_smb_fname_fsp_link(struct smb_filename
*smb_fname_dst
,
1184 const struct smb_filename
*smb_fname_src
)
1189 * The target should always not be linked yet!
1191 SMB_ASSERT(smb_fname_dst
->fsp
== NULL
);
1192 SMB_ASSERT(smb_fname_dst
->fsp_link
== NULL
);
1194 if (smb_fname_src
->fsp
== NULL
) {
1195 return NT_STATUS_OK
;
1198 status
= fsp_smb_fname_link(smb_fname_src
->fsp
,
1199 &smb_fname_dst
->fsp_link
,
1200 &smb_fname_dst
->fsp
);
1201 if (!NT_STATUS_IS_OK(status
)) {
1205 talloc_set_destructor(smb_fname_dst
, fsp_ref_no_close_destructor
);
1207 return NT_STATUS_OK
;
1211 * Create an smb_fname and open smb_fname->fsp pathref
1213 NTSTATUS
synthetic_pathref(TALLOC_CTX
*mem_ctx
,
1214 struct files_struct
*dirfsp
,
1215 const char *base_name
,
1216 const char *stream_name
,
1217 const SMB_STRUCT_STAT
*psbuf
,
1220 struct smb_filename
**_smb_fname
)
1222 struct smb_filename
*smb_fname
= NULL
;
1225 smb_fname
= synthetic_smb_fname(mem_ctx
,
1231 if (smb_fname
== NULL
) {
1232 return NT_STATUS_NO_MEMORY
;
1235 status
= openat_pathref_fsp(dirfsp
, smb_fname
);
1236 if (!NT_STATUS_IS_OK(status
)) {
1237 DBG_NOTICE("opening [%s] failed\n",
1238 smb_fname_str_dbg(smb_fname
));
1239 TALLOC_FREE(smb_fname
);
1243 *_smb_fname
= smb_fname
;
1244 return NT_STATUS_OK
;
1248 * Turn a path into a parent pathref and atname
1250 * This returns the parent pathref in _parent and the name relative to it. If
1251 * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
1252 * pathref as well, ie _atname->fsp will point at the same fsp as
1255 NTSTATUS
parent_pathref(TALLOC_CTX
*mem_ctx
,
1256 struct files_struct
*dirfsp
,
1257 const struct smb_filename
*smb_fname
,
1258 struct smb_filename
**_parent
,
1259 struct smb_filename
**_atname
)
1261 struct smb_filename
*parent
= NULL
;
1262 struct smb_filename
*atname
= NULL
;
1265 status
= SMB_VFS_PARENT_PATHNAME(dirfsp
->conn
,
1270 if (!NT_STATUS_IS_OK(status
)) {
1275 * We know that the parent name must
1276 * exist, and the name has been canonicalized
1277 * even if this was a POSIX pathname.
1278 * Ensure that we follow symlinks for
1279 * the parent. See the torture test
1280 * POSIX-SYMLINK-PARENT for details.
1282 parent
->flags
&= ~SMB_FILENAME_POSIX_PATH
;
1284 status
= openat_pathref_fsp(dirfsp
, parent
);
1285 if (!NT_STATUS_IS_OK(status
)) {
1286 TALLOC_FREE(parent
);
1290 status
= reference_smb_fname_fsp_link(atname
, smb_fname
);
1291 if (!NT_STATUS_IS_OK(status
)) {
1292 TALLOC_FREE(parent
);
1298 return NT_STATUS_OK
;
1301 static bool close_file_in_loop(struct files_struct
*fsp
,
1302 enum file_close_type close_type
)
1304 if (fsp_is_alternate_stream(fsp
)) {
1306 * This is a stream, it can't be a base
1308 SMB_ASSERT(fsp
->stream_fsp
== NULL
);
1309 SMB_ASSERT(fsp
->base_fsp
->stream_fsp
== fsp
);
1312 * Remove the base<->stream link so that
1313 * close_file_free() does not close fsp->base_fsp as
1314 * well. This would destroy walking the linked list of
1317 fsp
->base_fsp
->stream_fsp
= NULL
;
1318 fsp
->base_fsp
= NULL
;
1320 close_file_free(NULL
, &fsp
, close_type
);
1324 if (fsp
->stream_fsp
!= NULL
) {
1326 * This is the base of a stream.
1328 SMB_ASSERT(fsp
->stream_fsp
->base_fsp
== fsp
);
1331 * Remove the base<->stream link. This will make fsp
1332 * look like a normal fsp for the next round.
1334 fsp
->stream_fsp
->base_fsp
= NULL
;
1335 fsp
->stream_fsp
= NULL
;
1338 * Have us called back a second time. In the second
1339 * round, "fsp" now looks like a normal fsp.
1344 close_file_free(NULL
, &fsp
, close_type
);
1348 /****************************************************************************
1349 Close all open files for a connection.
1350 ****************************************************************************/
1352 struct file_close_conn_state
{
1353 struct connection_struct
*conn
;
1354 enum file_close_type close_type
;
1355 bool fsp_left_behind
;
1358 static struct files_struct
*file_close_conn_fn(
1359 struct files_struct
*fsp
,
1362 struct file_close_conn_state
*state
= private_data
;
1365 if (fsp
->conn
!= state
->conn
) {
1369 if (fsp
->op
!= NULL
&& fsp
->op
->global
->durable
) {
1371 * A tree disconnect closes a durable handle
1373 fsp
->op
->global
->durable
= false;
1376 did_close
= close_file_in_loop(fsp
, state
->close_type
);
1378 state
->fsp_left_behind
= true;
1384 void file_close_conn(connection_struct
*conn
, enum file_close_type close_type
)
1386 struct file_close_conn_state state
= { .conn
= conn
,
1387 .close_type
= close_type
};
1389 files_forall(conn
->sconn
, file_close_conn_fn
, &state
);
1391 if (state
.fsp_left_behind
) {
1392 state
.fsp_left_behind
= false;
1393 files_forall(conn
->sconn
, file_close_conn_fn
, &state
);
1394 SMB_ASSERT(!state
.fsp_left_behind
);
1398 /****************************************************************************
1399 Initialise file structures.
1400 ****************************************************************************/
1402 static int files_max_open_fds
;
1404 bool file_init_global(void)
1406 int request_max
= lp_max_open_files();
1410 if (files_max_open_fds
!= 0) {
1415 * Set the max_open files to be the requested
1416 * max plus a fudgefactor to allow for the extra
1417 * fd's we need such as log files etc...
1419 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
1421 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
1423 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
1424 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
1427 if (real_max
!= request_max
) {
1428 DEBUG(1, ("file_init_global: Information only: requested %d "
1429 "open files, %d are available.\n",
1430 request_max
, real_max
));
1433 SMB_ASSERT(real_max
> 100);
1435 files_max_open_fds
= real_max
;
1439 bool file_init(struct smbd_server_connection
*sconn
)
1443 ok
= file_init_global();
1448 sconn
->real_max_open_files
= files_max_open_fds
;
1453 /****************************************************************************
1454 Close files open by a specified vuid.
1455 ****************************************************************************/
1457 struct file_close_user_state
{
1459 bool fsp_left_behind
;
1462 static struct files_struct
*file_close_user_fn(
1463 struct files_struct
*fsp
,
1466 struct file_close_user_state
*state
= private_data
;
1469 if (fsp
->vuid
!= state
->vuid
) {
1473 did_close
= close_file_in_loop(fsp
, SHUTDOWN_CLOSE
);
1475 state
->fsp_left_behind
= true;
1481 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
1483 struct file_close_user_state state
= { .vuid
= vuid
};
1485 files_forall(sconn
, file_close_user_fn
, &state
);
1487 if (state
.fsp_left_behind
) {
1488 state
.fsp_left_behind
= false;
1489 files_forall(sconn
, file_close_user_fn
, &state
);
1490 SMB_ASSERT(!state
.fsp_left_behind
);
1495 * Walk the files table until "fn" returns non-NULL
1498 struct files_struct
*files_forall(
1499 struct smbd_server_connection
*sconn
,
1500 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
1501 void *private_data
),
1504 struct files_struct
*fsp
, *next
;
1506 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
1507 struct files_struct
*ret
;
1509 ret
= fn(fsp
, private_data
);
1517 /****************************************************************************
1518 Find a fsp given a file descriptor.
1519 ****************************************************************************/
1521 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
1526 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
1527 if (fsp_get_pathref_fd(fsp
) == fd
) {
1529 DLIST_PROMOTE(sconn
->files
, fsp
);
1538 /****************************************************************************
1539 Find a fsp given a device, inode and file_id.
1540 ****************************************************************************/
1542 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
1543 struct file_id id
, unsigned long gen_id
)
1552 for (fsp
= sconn
->files
; fsp
; fsp
= fsp
->next
,count
++) {
1554 * We can have a fsp->fh->fd == -1 here as it could be a stat
1557 if (!file_id_equal(&fsp
->file_id
, &id
)) {
1560 if (!fsp
->fsp_flags
.is_fsa
) {
1563 if (fh_get_gen_id(fsp
->fh
) != gen_id
) {
1567 DLIST_PROMOTE(sconn
->files
, fsp
);
1575 /****************************************************************************
1576 Find the first fsp given a device and inode.
1577 We use a singleton cache here to speed up searching from getfilepathinfo
1579 ****************************************************************************/
1581 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
1587 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
1588 /* Positive or negative cache hit. */
1589 return sconn
->fsp_fi_cache
.fsp
;
1592 sconn
->fsp_fi_cache
.id
= id
;
1594 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
1595 if (need_fsa
&& !fsp
->fsp_flags
.is_fsa
) {
1598 if (file_id_equal(&fsp
->file_id
, &id
)) {
1599 /* Setup positive cache. */
1600 sconn
->fsp_fi_cache
.fsp
= fsp
;
1605 /* Setup negative cache. */
1606 sconn
->fsp_fi_cache
.fsp
= NULL
;
1610 /****************************************************************************
1611 Find the next fsp having the same device and inode.
1612 ****************************************************************************/
1614 files_struct
*file_find_di_next(files_struct
*start_fsp
,
1619 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
1620 if (need_fsa
&& !fsp
->fsp_flags
.is_fsa
) {
1623 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
1631 struct files_struct
*file_find_one_fsp_from_lease_key(
1632 struct smbd_server_connection
*sconn
,
1633 const struct smb2_lease_key
*lease_key
)
1635 struct files_struct
*fsp
;
1637 for (fsp
= sconn
->files
; fsp
; fsp
=fsp
->next
) {
1638 if ((fsp
->lease
!= NULL
) &&
1639 (fsp
->lease
->lease
.lease_key
.data
[0] ==
1640 lease_key
->data
[0]) &&
1641 (fsp
->lease
->lease
.lease_key
.data
[1] ==
1642 lease_key
->data
[1])) {
1649 /****************************************************************************
1650 Find any fsp open with a pathname below that of an already open path.
1651 ****************************************************************************/
1653 bool file_find_subpath(files_struct
*dir_fsp
)
1657 char *d_fullname
= NULL
;
1659 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
1660 dir_fsp
->conn
->connectpath
,
1661 dir_fsp
->fsp_name
->base_name
);
1667 dlen
= strlen(d_fullname
);
1669 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
1672 if (fsp
== dir_fsp
) {
1676 d1_fullname
= talloc_asprintf(talloc_tos(),
1678 fsp
->conn
->connectpath
,
1679 fsp
->fsp_name
->base_name
);
1682 * If the open file has a path that is a longer
1683 * component, then it's a subpath.
1685 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
1686 (d1_fullname
[dlen
] == '/')) {
1687 TALLOC_FREE(d1_fullname
);
1688 TALLOC_FREE(d_fullname
);
1691 TALLOC_FREE(d1_fullname
);
1694 TALLOC_FREE(d_fullname
);
1698 /****************************************************************************
1700 ****************************************************************************/
1702 static void fsp_free(files_struct
*fsp
)
1704 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1706 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
1707 ZERO_STRUCT(sconn
->fsp_fi_cache
);
1710 DLIST_REMOVE(sconn
->files
, fsp
);
1711 SMB_ASSERT(sconn
->num_files
> 0);
1714 TALLOC_FREE(fsp
->fake_file_handle
);
1716 if (fh_get_refcount(fsp
->fh
) == 1) {
1717 TALLOC_FREE(fsp
->fh
);
1719 size_t new_refcount
= fh_get_refcount(fsp
->fh
) - 1;
1720 fh_set_refcount(fsp
->fh
, new_refcount
);
1723 if (fsp
->lease
!= NULL
) {
1724 if (fsp
->lease
->ref_count
== 1) {
1725 TALLOC_FREE(fsp
->lease
);
1727 fsp
->lease
->ref_count
--;
1731 fsp
->conn
->num_files_open
--;
1733 if (fsp
->fsp_name
!= NULL
&&
1734 fsp
->fsp_name
->fsp_link
!= NULL
)
1737 * Free fsp_link of fsp->fsp_name. To do this in the correct
1738 * talloc destructor order we have to do it here. The
1739 * talloc_free() of the link should set the fsp pointer to NULL.
1741 TALLOC_FREE(fsp
->fsp_name
->fsp_link
);
1742 SMB_ASSERT(fsp
->fsp_name
->fsp
== NULL
);
1745 /* this is paranoia, just in case someone tries to reuse the
1749 /* fsp->fsp_name is a talloc child and is free'd automatically. */
1754 * Rundown of all smb-related sub-structures of an fsp
1756 void fsp_unbind_smb(struct smb_request
*req
, files_struct
*fsp
)
1758 if (fsp
== fsp
->conn
->cwd_fsp
) {
1763 size_t len
= fsp_fullbasepath(fsp
, NULL
, 0);
1764 char fullpath
[len
+1];
1766 fsp_fullbasepath(fsp
, fullpath
, sizeof(fullpath
));
1769 * Avoid /. at the end of the path name. notify can't
1772 if (len
> 1 && fullpath
[len
-1] == '.' &&
1773 fullpath
[len
-2] == '/') {
1774 fullpath
[len
-2] = '\0';
1777 notify_remove(fsp
->conn
->sconn
->notify_ctx
, fsp
, fullpath
);
1778 TALLOC_FREE(fsp
->notify
);
1781 /* Ensure this event will never fire. */
1782 TALLOC_FREE(fsp
->update_write_time_event
);
1784 if (fsp
->op
!= NULL
) {
1785 fsp
->op
->compat
= NULL
;
1787 TALLOC_FREE(fsp
->op
);
1789 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
1790 req
->chain_fsp
= NULL
;
1794 * Clear all possible chained fsp
1795 * pointers in the SMB2 request queue.
1797 remove_smb2_chained_fsp(fsp
);
1800 void file_free(struct smb_request
*req
, files_struct
*fsp
)
1802 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1803 uint64_t fnum
= fsp
->fnum
;
1805 fsp_unbind_smb(req
, fsp
);
1807 /* Drop all remaining extensions. */
1808 vfs_remove_all_fsp_extensions(fsp
);
1812 DBG_INFO("freed files structure %"PRIu64
" (%zu used)\n",
1817 /****************************************************************************
1818 Get an fsp from a packet given a 16 bit fnum.
1819 ****************************************************************************/
1821 files_struct
*file_fsp(struct smb_request
*req
, uint16_t fid
)
1823 struct smbXsrv_open
*op
;
1830 * We should never get here. req==NULL could in theory
1831 * only happen from internal opens with a non-zero
1832 * root_dir_fid. Internal opens just don't do that, at
1833 * least they are not supposed to do so. And if they
1834 * start to do so, they better fake up a smb_request
1835 * from which we get the right smbd_server_conn. While
1836 * this should never happen, let's return NULL here.
1841 if (req
->chain_fsp
!= NULL
) {
1842 if (req
->chain_fsp
->fsp_flags
.closing
) {
1845 return req
->chain_fsp
;
1848 if (req
->xconn
== NULL
) {
1852 now
= timeval_to_nttime(&req
->request_time
);
1854 status
= smb1srv_open_lookup(req
->xconn
,
1856 if (!NT_STATUS_IS_OK(status
)) {
1865 if (fsp
->fsp_flags
.closing
) {
1869 req
->chain_fsp
= fsp
;
1870 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
1874 struct files_struct
*file_fsp_get(struct smbd_smb2_request
*smb2req
,
1875 uint64_t persistent_id
,
1876 uint64_t volatile_id
)
1878 struct smbXsrv_open
*op
;
1881 struct files_struct
*fsp
;
1883 now
= timeval_to_nttime(&smb2req
->request_time
);
1885 status
= smb2srv_open_lookup(smb2req
->xconn
,
1886 persistent_id
, volatile_id
,
1888 if (!NT_STATUS_IS_OK(status
)) {
1897 if (smb2req
->tcon
== NULL
) {
1901 if (smb2req
->tcon
->compat
!= fsp
->conn
) {
1905 if (smb2req
->session
== NULL
) {
1909 if (smb2req
->session
->global
->session_wire_id
!= fsp
->vuid
) {
1913 if (fsp
->fsp_flags
.closing
) {
1917 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
1922 struct files_struct
*file_fsp_smb2(struct smbd_smb2_request
*smb2req
,
1923 uint64_t persistent_id
,
1924 uint64_t volatile_id
)
1926 struct files_struct
*fsp
;
1928 if (smb2req
->compat_chain_fsp
!= NULL
) {
1929 if (smb2req
->compat_chain_fsp
->fsp_flags
.closing
) {
1932 smb2req
->compat_chain_fsp
->fsp_name
->st
.cached_dos_attributes
=
1933 FILE_ATTRIBUTES_INVALID
;
1934 return smb2req
->compat_chain_fsp
;
1937 fsp
= file_fsp_get(smb2req
, persistent_id
, volatile_id
);
1942 smb2req
->compat_chain_fsp
= fsp
;
1946 /****************************************************************************
1947 Duplicate the file handle part for a DOS or FCB open.
1948 ****************************************************************************/
1950 NTSTATUS
dup_file_fsp(
1952 uint32_t access_mask
,
1955 size_t new_refcount
;
1957 /* this can never happen for print files */
1958 SMB_ASSERT(from
->print_file
== NULL
);
1960 TALLOC_FREE(to
->fh
);
1963 new_refcount
= fh_get_refcount(to
->fh
) + 1;
1964 fh_set_refcount(to
->fh
, new_refcount
);
1966 to
->file_id
= from
->file_id
;
1967 to
->initial_allocation_size
= from
->initial_allocation_size
;
1968 to
->file_pid
= from
->file_pid
;
1969 to
->vuid
= from
->vuid
;
1970 to
->open_time
= from
->open_time
;
1971 to
->access_mask
= access_mask
;
1972 to
->oplock_type
= from
->oplock_type
;
1973 to
->fsp_flags
.can_lock
= from
->fsp_flags
.can_lock
;
1974 to
->fsp_flags
.can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
1975 to
->fsp_flags
.can_write
=
1976 CAN_WRITE(from
->conn
) &&
1977 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
1978 to
->fsp_flags
.modified
= from
->fsp_flags
.modified
;
1979 to
->fsp_flags
.is_directory
= from
->fsp_flags
.is_directory
;
1980 to
->fsp_flags
.aio_write_behind
= from
->fsp_flags
.aio_write_behind
;
1981 to
->fsp_flags
.is_fsa
= from
->fsp_flags
.is_fsa
;
1982 to
->fsp_flags
.is_pathref
= from
->fsp_flags
.is_pathref
;
1983 to
->fsp_flags
.have_proc_fds
= from
->fsp_flags
.have_proc_fds
;
1984 to
->fsp_flags
.is_dirfsp
= from
->fsp_flags
.is_dirfsp
;
1986 return fsp_set_smb_fname(to
, from
->fsp_name
);
1990 * Return a jenkins hash of a pathname on a connection.
1993 NTSTATUS
file_name_hash(connection_struct
*conn
,
1994 const char *name
, uint32_t *p_name_hash
)
1996 char tmpbuf
[PATH_MAX
];
1997 char *fullpath
, *to_free
;
2001 /* Set the hash of the full pathname. */
2003 if (name
[0] == '/') {
2004 strlcpy(tmpbuf
, name
, sizeof(tmpbuf
));
2006 len
= strlen(fullpath
);
2009 len
= full_path_tos(conn
->connectpath
,
2017 return NT_STATUS_NO_MEMORY
;
2019 key
= (TDB_DATA
) { .dptr
= (uint8_t *)fullpath
, .dsize
= len
+1 };
2020 *p_name_hash
= tdb_jenkins_hash(&key
);
2022 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
2024 (unsigned int)*p_name_hash
));
2026 TALLOC_FREE(to_free
);
2027 return NT_STATUS_OK
;
2030 static NTSTATUS
fsp_attach_smb_fname(struct files_struct
*fsp
,
2031 struct smb_filename
**_smb_fname
)
2033 struct smb_filename
*smb_fname_new
= talloc_move(fsp
, _smb_fname
);
2034 const char *name_str
= NULL
;
2035 uint32_t name_hash
= 0;
2038 name_str
= smb_fname_str_dbg(smb_fname_new
);
2039 if (name_str
== NULL
) {
2040 return NT_STATUS_NO_MEMORY
;
2043 status
= file_name_hash(fsp
->conn
,
2046 if (!NT_STATUS_IS_OK(status
)) {
2050 status
= fsp_smb_fname_link(fsp
,
2051 &smb_fname_new
->fsp_link
,
2052 &smb_fname_new
->fsp
);
2053 if (!NT_STATUS_IS_OK(status
)) {
2057 fsp
->name_hash
= name_hash
;
2058 fsp
->fsp_name
= smb_fname_new
;
2059 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
2061 return NT_STATUS_OK
;
2065 * The only way that the fsp->fsp_name field should ever be set.
2067 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
2068 const struct smb_filename
*smb_fname_in
)
2070 struct smb_filename
*smb_fname_old
= fsp
->fsp_name
;
2071 struct smb_filename
*smb_fname_new
= NULL
;
2074 smb_fname_new
= cp_smb_filename(fsp
, smb_fname_in
);
2075 if (smb_fname_new
== NULL
) {
2076 return NT_STATUS_NO_MEMORY
;
2079 status
= fsp_attach_smb_fname(fsp
, &smb_fname_new
);
2080 if (!NT_STATUS_IS_OK(status
)) {
2081 TALLOC_FREE(smb_fname_new
);
2085 if (smb_fname_old
!= NULL
) {
2086 smb_fname_fsp_unlink(smb_fname_old
);
2087 TALLOC_FREE(smb_fname_old
);
2090 return NT_STATUS_OK
;
2093 size_t fsp_fullbasepath(struct files_struct
*fsp
, char *buf
, size_t buflen
)
2096 char tmp_buf
[1] = {'\0'};
2099 * Don't pass NULL buffer to snprintf (to satisfy static checker)
2100 * Some callers will call this function with NULL for buf and
2101 * 0 for buflen in order to get length of fullbasepath (without
2102 * needing to allocate or write to buf)
2106 SMB_ASSERT(buflen
==0);
2109 len
= snprintf(buf
, buflen
, "%s/%s", fsp
->conn
->connectpath
,
2110 fsp
->fsp_name
->base_name
);
2116 void fsp_set_base_fsp(struct files_struct
*fsp
, struct files_struct
*base_fsp
)
2118 SMB_ASSERT(fsp
->stream_fsp
== NULL
);
2119 if (base_fsp
!= NULL
) {
2120 SMB_ASSERT(base_fsp
->base_fsp
== NULL
);
2121 SMB_ASSERT(base_fsp
->stream_fsp
== NULL
);
2124 if (fsp
->base_fsp
!= NULL
) {
2125 SMB_ASSERT(fsp
->base_fsp
->stream_fsp
== fsp
);
2126 fsp
->base_fsp
->stream_fsp
= NULL
;
2129 fsp
->base_fsp
= base_fsp
;
2130 if (fsp
->base_fsp
!= NULL
) {
2131 fsp
->base_fsp
->stream_fsp
= fsp
;
2135 bool fsp_is_alternate_stream(const struct files_struct
*fsp
)
2137 return (fsp
->base_fsp
!= NULL
);
2140 struct files_struct
*metadata_fsp(struct files_struct
*fsp
)
2142 if (fsp_is_alternate_stream(fsp
)) {
2143 return fsp
->base_fsp
;
2148 static bool fsp_generic_ask_sharemode(struct files_struct
*fsp
)
2154 if (fsp
->posix_flags
& FSP_POSIX_FLAGS_PATHNAMES
) {
2155 /* Always use filesystem for UNIX mtime query. */
2162 bool fsp_search_ask_sharemode(struct files_struct
*fsp
)
2164 if (!fsp_generic_ask_sharemode(fsp
)) {
2168 return lp_smbd_search_ask_sharemode(SNUM(fsp
->conn
));
2171 bool fsp_getinfo_ask_sharemode(struct files_struct
*fsp
)
2173 if (!fsp_generic_ask_sharemode(fsp
)) {
2177 return lp_smbd_getinfo_ask_sharemode(SNUM(fsp
->conn
));