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 longer than PATH_MAX?
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
);
821 * Path sanitizing further up has cleaned or rejected
822 * empty path components. Assert this here.
824 SMB_ASSERT(rel_fname
.base_name
[0] != '\0');
826 if (ISDOT(rel_fname
.base_name
) ||
827 ISDOTDOT(rel_fname
.base_name
)) {
828 DBG_DEBUG("%s contains a dot\n", path_in
);
829 status
= NT_STATUS_OBJECT_NAME_INVALID
;
833 /* Check veto files. */
834 if (IS_VETO_PATH(conn
, rel_fname
.base_name
)) {
835 DBG_DEBUG("%s contains veto files path component %s\n",
836 path_in
, rel_fname
.base_name
);
837 status
= NT_STATUS_OBJECT_PATH_NOT_FOUND
;
841 rel_fname
.base_name
= next
;
844 if (conn
->open_how_resolve
& VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS
) {
847 * Try a direct openat2 with RESOLVE_NO_SYMLINKS to
848 * avoid the openat/close loop further down.
851 rel_fname
.base_name
= discard_const_p(char, path_in
);
852 how
.resolve
= VFS_OPEN_HOW_RESOLVE_NO_SYMLINKS
;
854 fd
= SMB_VFS_OPENAT(conn
, dirfsp
, &rel_fname
, fsp
, &how
);
857 TALLOC_FREE(full_fname
.base_name
);
858 full_fname
= rel_fname
;
862 status
= map_nt_error_from_unix(errno
);
863 DBG_DEBUG("SMB_VFS_OPENAT(%s, %s, RESOLVE_NO_SYMLINKS) "
864 "returned %d %s => %s\n",
865 smb_fname_str_dbg(dirfsp
->fsp_name
), path_in
,
866 errno
, strerror(errno
), nt_errstr(status
));
867 SMB_ASSERT(fd
== -1);
871 * We got ENOSYS, so fallback to the old code
872 * if the kernel doesn't support openat2() yet.
879 * For ELOOP we also fallback in order to
880 * return the correct information with
881 * NT_STATUS_STOPPED_ON_SYMLINK.
883 * O_NOFOLLOW|O_DIRECTORY results in
884 * ENOTDIR instead of ELOOP for the final
891 * If we got ENOENT, the filesystem could
892 * be case sensitive. For now we only do
893 * the get_real_filename_at() dance in
894 * the fallback loop below.
903 * Just fallback to the openat loop
909 * Now we loop over all components
910 * opening each one and using it
911 * as dirfd for the next one.
913 * It means we can detect symlinks
916 rel_fname
.base_name
= path
;
918 next
= strv_next(path
, rel_fname
.base_name
);
927 if ((fd
== -1) && (errno
== ENOENT
)) {
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 dirfsp
->fsp_name
= NULL
;
1062 SMB_ASSERT(fsp_get_pathref_fd(dirfsp
) != -1);
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 dirfsp
->fsp_name
= NULL
;
1126 SMB_ASSERT(fsp_get_pathref_fd(dirfsp
) != -1);
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
;
1178 * Create an smb_fname and open smb_fname->fsp pathref
1180 NTSTATUS
synthetic_pathref(TALLOC_CTX
*mem_ctx
,
1181 struct files_struct
*dirfsp
,
1182 const char *base_name
,
1183 const char *stream_name
,
1184 const SMB_STRUCT_STAT
*psbuf
,
1187 struct smb_filename
**_smb_fname
)
1189 struct smb_filename
*smb_fname
= NULL
;
1192 smb_fname
= synthetic_smb_fname(mem_ctx
,
1198 if (smb_fname
== NULL
) {
1199 return NT_STATUS_NO_MEMORY
;
1202 status
= openat_pathref_fsp(dirfsp
, smb_fname
);
1203 if (!NT_STATUS_IS_OK(status
)) {
1204 DBG_ERR("opening [%s] failed\n",
1205 smb_fname_str_dbg(smb_fname
));
1206 TALLOC_FREE(smb_fname
);
1210 *_smb_fname
= smb_fname
;
1211 return NT_STATUS_OK
;
1214 static int atname_destructor(struct smb_filename
*smb_fname
)
1216 destroy_fsp_smb_fname_link(&smb_fname
->fsp_link
);
1221 * Turn a path into a parent pathref and atname
1223 * This returns the parent pathref in _parent and the name relative to it. If
1224 * smb_fname was a pathref (ie smb_fname->fsp != NULL), then _atname will be a
1225 * pathref as well, ie _atname->fsp will point at the same fsp as
1228 NTSTATUS
parent_pathref(TALLOC_CTX
*mem_ctx
,
1229 struct files_struct
*dirfsp
,
1230 const struct smb_filename
*smb_fname
,
1231 struct smb_filename
**_parent
,
1232 struct smb_filename
**_atname
)
1234 struct smb_filename
*parent
= NULL
;
1235 struct smb_filename
*atname
= NULL
;
1238 status
= SMB_VFS_PARENT_PATHNAME(dirfsp
->conn
,
1243 if (!NT_STATUS_IS_OK(status
)) {
1248 * We know that the parent name must
1249 * exist, and the name has been canonicalized
1250 * even if this was a POSIX pathname.
1251 * Ensure that we follow symlinks for
1252 * the parent. See the torture test
1253 * POSIX-SYMLINK-PARENT for details.
1255 parent
->flags
&= ~SMB_FILENAME_POSIX_PATH
;
1257 status
= openat_pathref_fsp(dirfsp
, parent
);
1258 if (!NT_STATUS_IS_OK(status
)) {
1259 TALLOC_FREE(parent
);
1263 if (smb_fname
->fsp
!= NULL
) {
1264 status
= fsp_smb_fname_link(smb_fname
->fsp
,
1267 if (!NT_STATUS_IS_OK(status
)) {
1268 TALLOC_FREE(parent
);
1271 talloc_set_destructor(atname
, atname_destructor
);
1275 return NT_STATUS_OK
;
1278 static bool close_file_in_loop(struct files_struct
*fsp
,
1279 enum file_close_type close_type
)
1281 if (fsp_is_alternate_stream(fsp
)) {
1283 * This is a stream, it can't be a base
1285 SMB_ASSERT(fsp
->stream_fsp
== NULL
);
1286 SMB_ASSERT(fsp
->base_fsp
->stream_fsp
== fsp
);
1289 * Remove the base<->stream link so that
1290 * close_file_free() does not close fsp->base_fsp as
1291 * well. This would destroy walking the linked list of
1294 fsp
->base_fsp
->stream_fsp
= NULL
;
1295 fsp
->base_fsp
= NULL
;
1297 close_file_free(NULL
, &fsp
, close_type
);
1301 if (fsp
->stream_fsp
!= NULL
) {
1303 * This is the base of a stream.
1305 SMB_ASSERT(fsp
->stream_fsp
->base_fsp
== fsp
);
1308 * Remove the base<->stream link. This will make fsp
1309 * look like a normal fsp for the next round.
1311 fsp
->stream_fsp
->base_fsp
= NULL
;
1312 fsp
->stream_fsp
= NULL
;
1315 * Have us called back a second time. In the second
1316 * round, "fsp" now looks like a normal fsp.
1321 close_file_free(NULL
, &fsp
, close_type
);
1325 /****************************************************************************
1326 Close all open files for a connection.
1327 ****************************************************************************/
1329 struct file_close_conn_state
{
1330 struct connection_struct
*conn
;
1331 enum file_close_type close_type
;
1332 bool fsp_left_behind
;
1335 static struct files_struct
*file_close_conn_fn(
1336 struct files_struct
*fsp
,
1339 struct file_close_conn_state
*state
= private_data
;
1342 if (fsp
->conn
!= state
->conn
) {
1346 if (fsp
->op
!= NULL
&& fsp
->op
->global
->durable
) {
1348 * A tree disconnect closes a durable handle
1350 fsp
->op
->global
->durable
= false;
1353 did_close
= close_file_in_loop(fsp
, state
->close_type
);
1355 state
->fsp_left_behind
= true;
1361 void file_close_conn(connection_struct
*conn
, enum file_close_type close_type
)
1363 struct file_close_conn_state state
= { .conn
= conn
,
1364 .close_type
= close_type
};
1366 files_forall(conn
->sconn
, file_close_conn_fn
, &state
);
1368 if (state
.fsp_left_behind
) {
1369 state
.fsp_left_behind
= false;
1370 files_forall(conn
->sconn
, file_close_conn_fn
, &state
);
1371 SMB_ASSERT(!state
.fsp_left_behind
);
1375 /****************************************************************************
1376 Initialise file structures.
1377 ****************************************************************************/
1379 static int files_max_open_fds
;
1381 bool file_init_global(void)
1383 int request_max
= lp_max_open_files();
1387 if (files_max_open_fds
!= 0) {
1392 * Set the max_open files to be the requested
1393 * max plus a fudgefactor to allow for the extra
1394 * fd's we need such as log files etc...
1396 real_lim
= set_maxfiles(request_max
+ MAX_OPEN_FUDGEFACTOR
);
1398 real_max
= real_lim
- MAX_OPEN_FUDGEFACTOR
;
1400 if (real_max
+ FILE_HANDLE_OFFSET
+ MAX_OPEN_PIPES
> 65536) {
1401 real_max
= 65536 - FILE_HANDLE_OFFSET
- MAX_OPEN_PIPES
;
1404 if (real_max
!= request_max
) {
1405 DEBUG(1, ("file_init_global: Information only: requested %d "
1406 "open files, %d are available.\n",
1407 request_max
, real_max
));
1410 SMB_ASSERT(real_max
> 100);
1412 files_max_open_fds
= real_max
;
1416 bool file_init(struct smbd_server_connection
*sconn
)
1420 ok
= file_init_global();
1425 sconn
->real_max_open_files
= files_max_open_fds
;
1430 /****************************************************************************
1431 Close files open by a specified vuid.
1432 ****************************************************************************/
1434 struct file_close_user_state
{
1436 bool fsp_left_behind
;
1439 static struct files_struct
*file_close_user_fn(
1440 struct files_struct
*fsp
,
1443 struct file_close_user_state
*state
= private_data
;
1446 if (fsp
->vuid
!= state
->vuid
) {
1450 did_close
= close_file_in_loop(fsp
, SHUTDOWN_CLOSE
);
1452 state
->fsp_left_behind
= true;
1458 void file_close_user(struct smbd_server_connection
*sconn
, uint64_t vuid
)
1460 struct file_close_user_state state
= { .vuid
= vuid
};
1462 files_forall(sconn
, file_close_user_fn
, &state
);
1464 if (state
.fsp_left_behind
) {
1465 state
.fsp_left_behind
= false;
1466 files_forall(sconn
, file_close_user_fn
, &state
);
1467 SMB_ASSERT(!state
.fsp_left_behind
);
1472 * Walk the files table until "fn" returns non-NULL
1475 struct files_struct
*files_forall(
1476 struct smbd_server_connection
*sconn
,
1477 struct files_struct
*(*fn
)(struct files_struct
*fsp
,
1478 void *private_data
),
1481 struct files_struct
*fsp
, *next
;
1483 for (fsp
= sconn
->files
; fsp
; fsp
= next
) {
1484 struct files_struct
*ret
;
1486 ret
= fn(fsp
, private_data
);
1494 /****************************************************************************
1495 Find a fsp given a file descriptor.
1496 ****************************************************************************/
1498 files_struct
*file_find_fd(struct smbd_server_connection
*sconn
, int fd
)
1503 for (fsp
=sconn
->files
; fsp
; fsp
=fsp
->next
,count
++) {
1504 if (fsp_get_pathref_fd(fsp
) == fd
) {
1506 DLIST_PROMOTE(sconn
->files
, fsp
);
1515 /****************************************************************************
1516 Find a fsp given a device, inode and file_id.
1517 ****************************************************************************/
1519 files_struct
*file_find_dif(struct smbd_server_connection
*sconn
,
1520 struct file_id id
, unsigned long gen_id
)
1529 for (fsp
= sconn
->files
; fsp
; fsp
= fsp
->next
,count
++) {
1531 * We can have a fsp->fh->fd == -1 here as it could be a stat
1534 if (!file_id_equal(&fsp
->file_id
, &id
)) {
1537 if (!fsp
->fsp_flags
.is_fsa
) {
1540 if (fh_get_gen_id(fsp
->fh
) != gen_id
) {
1544 DLIST_PROMOTE(sconn
->files
, fsp
);
1546 /* Paranoia check. */
1547 if ((fsp_get_pathref_fd(fsp
) == -1) &&
1548 (fsp
->oplock_type
!= NO_OPLOCK
&&
1549 fsp
->oplock_type
!= LEASE_OPLOCK
))
1551 struct file_id_buf idbuf
;
1553 DBG_ERR("file %s file_id = "
1554 "%s, gen = %u oplock_type = %u is a "
1555 "stat open with oplock type !\n",
1557 file_id_str_buf(fsp
->file_id
, &idbuf
),
1558 (unsigned int)fh_get_gen_id(fsp
->fh
),
1559 (unsigned int)fsp
->oplock_type
);
1560 smb_panic("file_find_dif");
1568 /****************************************************************************
1569 Find the first fsp given a device and inode.
1570 We use a singleton cache here to speed up searching from getfilepathinfo
1572 ****************************************************************************/
1574 files_struct
*file_find_di_first(struct smbd_server_connection
*sconn
,
1580 if (file_id_equal(&sconn
->fsp_fi_cache
.id
, &id
)) {
1581 /* Positive or negative cache hit. */
1582 return sconn
->fsp_fi_cache
.fsp
;
1585 sconn
->fsp_fi_cache
.id
= id
;
1587 for (fsp
=sconn
->files
;fsp
;fsp
=fsp
->next
) {
1588 if (need_fsa
&& !fsp
->fsp_flags
.is_fsa
) {
1591 if (file_id_equal(&fsp
->file_id
, &id
)) {
1592 /* Setup positive cache. */
1593 sconn
->fsp_fi_cache
.fsp
= fsp
;
1598 /* Setup negative cache. */
1599 sconn
->fsp_fi_cache
.fsp
= NULL
;
1603 /****************************************************************************
1604 Find the next fsp having the same device and inode.
1605 ****************************************************************************/
1607 files_struct
*file_find_di_next(files_struct
*start_fsp
,
1612 for (fsp
= start_fsp
->next
;fsp
;fsp
=fsp
->next
) {
1613 if (need_fsa
&& !fsp
->fsp_flags
.is_fsa
) {
1616 if (file_id_equal(&fsp
->file_id
, &start_fsp
->file_id
)) {
1624 struct files_struct
*file_find_one_fsp_from_lease_key(
1625 struct smbd_server_connection
*sconn
,
1626 const struct smb2_lease_key
*lease_key
)
1628 struct files_struct
*fsp
;
1630 for (fsp
= sconn
->files
; fsp
; fsp
=fsp
->next
) {
1631 if ((fsp
->lease
!= NULL
) &&
1632 (fsp
->lease
->lease
.lease_key
.data
[0] ==
1633 lease_key
->data
[0]) &&
1634 (fsp
->lease
->lease
.lease_key
.data
[1] ==
1635 lease_key
->data
[1])) {
1642 /****************************************************************************
1643 Find any fsp open with a pathname below that of an already open path.
1644 ****************************************************************************/
1646 bool file_find_subpath(files_struct
*dir_fsp
)
1650 char *d_fullname
= NULL
;
1652 d_fullname
= talloc_asprintf(talloc_tos(), "%s/%s",
1653 dir_fsp
->conn
->connectpath
,
1654 dir_fsp
->fsp_name
->base_name
);
1660 dlen
= strlen(d_fullname
);
1662 for (fsp
=dir_fsp
->conn
->sconn
->files
; fsp
; fsp
=fsp
->next
) {
1665 if (fsp
== dir_fsp
) {
1669 d1_fullname
= talloc_asprintf(talloc_tos(),
1671 fsp
->conn
->connectpath
,
1672 fsp
->fsp_name
->base_name
);
1675 * If the open file has a path that is a longer
1676 * component, then it's a subpath.
1678 if (strnequal(d_fullname
, d1_fullname
, dlen
) &&
1679 (d1_fullname
[dlen
] == '/')) {
1680 TALLOC_FREE(d1_fullname
);
1681 TALLOC_FREE(d_fullname
);
1684 TALLOC_FREE(d1_fullname
);
1687 TALLOC_FREE(d_fullname
);
1691 /****************************************************************************
1693 ****************************************************************************/
1695 static void fsp_free(files_struct
*fsp
)
1697 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1699 if (fsp
== sconn
->fsp_fi_cache
.fsp
) {
1700 ZERO_STRUCT(sconn
->fsp_fi_cache
);
1703 DLIST_REMOVE(sconn
->files
, fsp
);
1704 SMB_ASSERT(sconn
->num_files
> 0);
1707 TALLOC_FREE(fsp
->fake_file_handle
);
1709 if (fh_get_refcount(fsp
->fh
) == 1) {
1710 TALLOC_FREE(fsp
->fh
);
1712 size_t new_refcount
= fh_get_refcount(fsp
->fh
) - 1;
1713 fh_set_refcount(fsp
->fh
, new_refcount
);
1716 if (fsp
->lease
!= NULL
) {
1717 if (fsp
->lease
->ref_count
== 1) {
1718 TALLOC_FREE(fsp
->lease
);
1720 fsp
->lease
->ref_count
--;
1724 fsp
->conn
->num_files_open
--;
1726 if (fsp
->fsp_name
!= NULL
&&
1727 fsp
->fsp_name
->fsp_link
!= NULL
)
1730 * Free fsp_link of fsp->fsp_name. To do this in the correct
1731 * talloc destructor order we have to do it here. The
1732 * talloc_free() of the link should set the fsp pointer to NULL.
1734 TALLOC_FREE(fsp
->fsp_name
->fsp_link
);
1735 SMB_ASSERT(fsp
->fsp_name
->fsp
== NULL
);
1738 /* this is paranoia, just in case someone tries to reuse the
1742 /* fsp->fsp_name is a talloc child and is free'd automatically. */
1747 * Rundown of all smb-related sub-structures of an fsp
1749 void fsp_unbind_smb(struct smb_request
*req
, files_struct
*fsp
)
1751 if (fsp
== fsp
->conn
->cwd_fsp
) {
1756 size_t len
= fsp_fullbasepath(fsp
, NULL
, 0);
1757 char fullpath
[len
+1];
1759 fsp_fullbasepath(fsp
, fullpath
, sizeof(fullpath
));
1762 * Avoid /. at the end of the path name. notify can't
1765 if (len
> 1 && fullpath
[len
-1] == '.' &&
1766 fullpath
[len
-2] == '/') {
1767 fullpath
[len
-2] = '\0';
1770 notify_remove(fsp
->conn
->sconn
->notify_ctx
, fsp
, fullpath
);
1771 TALLOC_FREE(fsp
->notify
);
1774 /* Ensure this event will never fire. */
1775 TALLOC_FREE(fsp
->update_write_time_event
);
1777 if (fsp
->op
!= NULL
) {
1778 fsp
->op
->compat
= NULL
;
1780 TALLOC_FREE(fsp
->op
);
1782 if ((req
!= NULL
) && (fsp
== req
->chain_fsp
)) {
1783 req
->chain_fsp
= NULL
;
1787 * Clear all possible chained fsp
1788 * pointers in the SMB2 request queue.
1790 remove_smb2_chained_fsp(fsp
);
1793 void file_free(struct smb_request
*req
, files_struct
*fsp
)
1795 struct smbd_server_connection
*sconn
= fsp
->conn
->sconn
;
1796 uint64_t fnum
= fsp
->fnum
;
1798 fsp_unbind_smb(req
, fsp
);
1800 /* Drop all remaining extensions. */
1801 vfs_remove_all_fsp_extensions(fsp
);
1805 DBG_INFO("freed files structure %"PRIu64
" (%zu used)\n",
1810 /****************************************************************************
1811 Get an fsp from a packet given a 16 bit fnum.
1812 ****************************************************************************/
1814 files_struct
*file_fsp(struct smb_request
*req
, uint16_t fid
)
1816 struct smbXsrv_open
*op
;
1823 * We should never get here. req==NULL could in theory
1824 * only happen from internal opens with a non-zero
1825 * root_dir_fid. Internal opens just don't do that, at
1826 * least they are not supposed to do so. And if they
1827 * start to do so, they better fake up a smb_request
1828 * from which we get the right smbd_server_conn. While
1829 * this should never happen, let's return NULL here.
1834 if (req
->chain_fsp
!= NULL
) {
1835 if (req
->chain_fsp
->fsp_flags
.closing
) {
1838 return req
->chain_fsp
;
1841 if (req
->xconn
== NULL
) {
1845 now
= timeval_to_nttime(&req
->request_time
);
1847 status
= smb1srv_open_lookup(req
->xconn
,
1849 if (!NT_STATUS_IS_OK(status
)) {
1858 if (fsp
->fsp_flags
.closing
) {
1862 req
->chain_fsp
= fsp
;
1863 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
1867 struct files_struct
*file_fsp_get(struct smbd_smb2_request
*smb2req
,
1868 uint64_t persistent_id
,
1869 uint64_t volatile_id
)
1871 struct smbXsrv_open
*op
;
1874 struct files_struct
*fsp
;
1876 now
= timeval_to_nttime(&smb2req
->request_time
);
1878 status
= smb2srv_open_lookup(smb2req
->xconn
,
1879 persistent_id
, volatile_id
,
1881 if (!NT_STATUS_IS_OK(status
)) {
1890 if (smb2req
->tcon
== NULL
) {
1894 if (smb2req
->tcon
->compat
!= fsp
->conn
) {
1898 if (smb2req
->session
== NULL
) {
1902 if (smb2req
->session
->global
->session_wire_id
!= fsp
->vuid
) {
1906 if (fsp
->fsp_flags
.closing
) {
1910 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
1915 struct files_struct
*file_fsp_smb2(struct smbd_smb2_request
*smb2req
,
1916 uint64_t persistent_id
,
1917 uint64_t volatile_id
)
1919 struct files_struct
*fsp
;
1921 if (smb2req
->compat_chain_fsp
!= NULL
) {
1922 if (smb2req
->compat_chain_fsp
->fsp_flags
.closing
) {
1925 smb2req
->compat_chain_fsp
->fsp_name
->st
.cached_dos_attributes
=
1926 FILE_ATTRIBUTES_INVALID
;
1927 return smb2req
->compat_chain_fsp
;
1930 fsp
= file_fsp_get(smb2req
, persistent_id
, volatile_id
);
1935 smb2req
->compat_chain_fsp
= fsp
;
1939 /****************************************************************************
1940 Duplicate the file handle part for a DOS or FCB open.
1941 ****************************************************************************/
1943 NTSTATUS
dup_file_fsp(
1945 uint32_t access_mask
,
1948 size_t new_refcount
;
1950 /* this can never happen for print files */
1951 SMB_ASSERT(from
->print_file
== NULL
);
1953 TALLOC_FREE(to
->fh
);
1956 new_refcount
= fh_get_refcount(to
->fh
) + 1;
1957 fh_set_refcount(to
->fh
, new_refcount
);
1959 to
->file_id
= from
->file_id
;
1960 to
->initial_allocation_size
= from
->initial_allocation_size
;
1961 to
->file_pid
= from
->file_pid
;
1962 to
->vuid
= from
->vuid
;
1963 to
->open_time
= from
->open_time
;
1964 to
->access_mask
= access_mask
;
1965 to
->oplock_type
= from
->oplock_type
;
1966 to
->fsp_flags
.can_lock
= from
->fsp_flags
.can_lock
;
1967 to
->fsp_flags
.can_read
= ((access_mask
& FILE_READ_DATA
) != 0);
1968 to
->fsp_flags
.can_write
=
1969 CAN_WRITE(from
->conn
) &&
1970 ((access_mask
& (FILE_WRITE_DATA
| FILE_APPEND_DATA
)) != 0);
1971 to
->fsp_flags
.modified
= from
->fsp_flags
.modified
;
1972 to
->fsp_flags
.is_directory
= from
->fsp_flags
.is_directory
;
1973 to
->fsp_flags
.aio_write_behind
= from
->fsp_flags
.aio_write_behind
;
1974 to
->fsp_flags
.is_fsa
= from
->fsp_flags
.is_fsa
;
1975 to
->fsp_flags
.is_pathref
= from
->fsp_flags
.is_pathref
;
1976 to
->fsp_flags
.have_proc_fds
= from
->fsp_flags
.have_proc_fds
;
1977 to
->fsp_flags
.is_dirfsp
= from
->fsp_flags
.is_dirfsp
;
1979 return fsp_set_smb_fname(to
, from
->fsp_name
);
1983 * Return a jenkins hash of a pathname on a connection.
1986 NTSTATUS
file_name_hash(connection_struct
*conn
,
1987 const char *name
, uint32_t *p_name_hash
)
1989 char tmpbuf
[PATH_MAX
];
1990 char *fullpath
, *to_free
;
1994 /* Set the hash of the full pathname. */
1996 if (name
[0] == '/') {
1997 strlcpy(tmpbuf
, name
, sizeof(tmpbuf
));
1999 len
= strlen(fullpath
);
2002 len
= full_path_tos(conn
->connectpath
,
2010 return NT_STATUS_NO_MEMORY
;
2012 key
= (TDB_DATA
) { .dptr
= (uint8_t *)fullpath
, .dsize
= len
+1 };
2013 *p_name_hash
= tdb_jenkins_hash(&key
);
2015 DEBUG(10,("file_name_hash: %s hash 0x%x\n",
2017 (unsigned int)*p_name_hash
));
2019 TALLOC_FREE(to_free
);
2020 return NT_STATUS_OK
;
2023 static NTSTATUS
fsp_attach_smb_fname(struct files_struct
*fsp
,
2024 struct smb_filename
**_smb_fname
)
2026 struct smb_filename
*smb_fname_new
= talloc_move(fsp
, _smb_fname
);
2027 const char *name_str
= NULL
;
2028 uint32_t name_hash
= 0;
2031 name_str
= smb_fname_str_dbg(smb_fname_new
);
2032 if (name_str
== NULL
) {
2033 return NT_STATUS_NO_MEMORY
;
2036 status
= file_name_hash(fsp
->conn
,
2039 if (!NT_STATUS_IS_OK(status
)) {
2043 status
= fsp_smb_fname_link(fsp
,
2044 &smb_fname_new
->fsp_link
,
2045 &smb_fname_new
->fsp
);
2046 if (!NT_STATUS_IS_OK(status
)) {
2050 fsp
->name_hash
= name_hash
;
2051 fsp
->fsp_name
= smb_fname_new
;
2052 fsp
->fsp_name
->st
.cached_dos_attributes
= FILE_ATTRIBUTES_INVALID
;
2054 return NT_STATUS_OK
;
2058 * The only way that the fsp->fsp_name field should ever be set.
2060 NTSTATUS
fsp_set_smb_fname(struct files_struct
*fsp
,
2061 const struct smb_filename
*smb_fname_in
)
2063 struct smb_filename
*smb_fname_old
= fsp
->fsp_name
;
2064 struct smb_filename
*smb_fname_new
= NULL
;
2067 smb_fname_new
= cp_smb_filename(fsp
, smb_fname_in
);
2068 if (smb_fname_new
== NULL
) {
2069 return NT_STATUS_NO_MEMORY
;
2072 status
= fsp_attach_smb_fname(fsp
, &smb_fname_new
);
2073 if (!NT_STATUS_IS_OK(status
)) {
2074 TALLOC_FREE(smb_fname_new
);
2078 if (smb_fname_old
!= NULL
) {
2079 smb_fname_fsp_unlink(smb_fname_old
);
2080 TALLOC_FREE(smb_fname_old
);
2083 return NT_STATUS_OK
;
2086 size_t fsp_fullbasepath(struct files_struct
*fsp
, char *buf
, size_t buflen
)
2089 char tmp_buf
[1] = {'\0'};
2092 * Don't pass NULL buffer to snprintf (to satisfy static checker)
2093 * Some callers will call this function with NULL for buf and
2094 * 0 for buflen in order to get length of fullbasepath (without
2095 * needing to allocate or write to buf)
2099 SMB_ASSERT(buflen
==0);
2102 len
= snprintf(buf
, buflen
, "%s/%s", fsp
->conn
->connectpath
,
2103 fsp
->fsp_name
->base_name
);
2109 void fsp_set_base_fsp(struct files_struct
*fsp
, struct files_struct
*base_fsp
)
2111 SMB_ASSERT(fsp
->stream_fsp
== NULL
);
2112 if (base_fsp
!= NULL
) {
2113 SMB_ASSERT(base_fsp
->base_fsp
== NULL
);
2114 SMB_ASSERT(base_fsp
->stream_fsp
== NULL
);
2117 if (fsp
->base_fsp
!= NULL
) {
2118 SMB_ASSERT(fsp
->base_fsp
->stream_fsp
== fsp
);
2119 fsp
->base_fsp
->stream_fsp
= NULL
;
2122 fsp
->base_fsp
= base_fsp
;
2123 if (fsp
->base_fsp
!= NULL
) {
2124 fsp
->base_fsp
->stream_fsp
= fsp
;
2128 bool fsp_is_alternate_stream(const struct files_struct
*fsp
)
2130 return (fsp
->base_fsp
!= NULL
);
2133 struct files_struct
*metadata_fsp(struct files_struct
*fsp
)
2135 if (fsp_is_alternate_stream(fsp
)) {
2136 return fsp
->base_fsp
;