2 * OS X and Netatalk interoperability VFS module for Samba-3.x
4 * Copyright (C) Ralph Boehme, 2013, 2014
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 "MacExtensions.h"
22 #include "smbd/smbd.h"
23 #include "system/filesys.h"
24 #include "lib/util/time.h"
25 #include "system/shmem.h"
26 #include "locking/proto.h"
27 #include "smbd/globals.h"
29 #include "libcli/security/security.h"
30 #include "../libcli/smb/smb2_create_ctx.h"
31 #include "lib/util/tevent_ntstatus.h"
32 #include "lib/util/tevent_unix.h"
33 #include "lib/util/util_file.h"
34 #include "offload_token.h"
35 #include "string_replace.h"
36 #include "hash_inode.h"
37 #include "lib/adouble.h"
38 #include "lib/util_macstreams.h"
39 #include "source3/smbd/dir.h"
42 * Enhanced OS X and Netatalk compatibility
43 * ========================================
45 * This modules takes advantage of vfs_streams_xattr and
46 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
47 * loaded in the correct order:
49 * vfs modules = catia fruit streams_xattr
51 * The module intercepts the OS X special streams "AFP_AfpInfo" and
52 * "AFP_Resource" and handles them in a special way. All other named
53 * streams are deferred to vfs_streams_xattr.
55 * The OS X client maps all NTFS illegal characters to the Unicode
56 * private range. This module optionally stores the characters using
57 * their native ASCII encoding using vfs_catia. If you're not enabling
58 * this feature, you can skip catia from vfs modules.
60 * Finally, open modes are optionally checked against Netatalk AFP
63 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
64 * extended metadata for files and directories. This module optionally
65 * reads and stores this metadata in a way compatible with Netatalk 3
66 * which stores the metadata in an EA "org.netatalk.metadata". Cf
67 * source3/include/MacExtensions.h for a description of the binary
70 * The "AFP_Resource" named stream may be arbitrarily large, thus it
71 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
72 * the only available filesystem where xattrs can be of any size and
73 * the OS supports using the file APIs for xattrs.
75 * The AFP_Resource stream is stored in an AppleDouble file prepending
76 * "._" to the filename. On Solaris with ZFS the stream is optionally
77 * stored in an EA "org.netatalk.resource".
83 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
84 * other protocols you may want to adjust the xattr names the VFS
85 * module vfs_streams_xattr uses for storing ADS's. This defaults to
86 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
87 * these module parameters:
89 * streams_xattr:prefix = user.
90 * streams_xattr:store_stream_type = false
96 * - log diagnostic if any needed VFS module is not loaded
97 * (eg with lp_vfs_objects())
101 static int vfs_fruit_debug_level
= DBGC_VFS
;
103 static struct global_fruit_config
{
104 bool nego_aapl
; /* client negotiated AAPL */
106 } global_fruit_config
;
109 #define DBGC_CLASS vfs_fruit_debug_level
111 #define FRUIT_PARAM_TYPE_NAME "fruit"
113 enum apple_fork
{APPLE_FORK_DATA
, APPLE_FORK_RSRC
};
115 enum fruit_rsrc
{FRUIT_RSRC_STREAM
, FRUIT_RSRC_ADFILE
, FRUIT_RSRC_XATTR
};
116 enum fruit_meta
{FRUIT_META_STREAM
, FRUIT_META_NETATALK
};
117 enum fruit_locking
{FRUIT_LOCKING_NETATALK
, FRUIT_LOCKING_NONE
};
118 enum fruit_encoding
{FRUIT_ENC_NATIVE
, FRUIT_ENC_PRIVATE
};
120 struct fruit_config_data
{
121 enum fruit_rsrc rsrc
;
122 enum fruit_meta meta
;
123 enum fruit_locking locking
;
124 enum fruit_encoding encoding
;
125 bool use_aapl
; /* config from smb.conf */
127 bool readdir_attr_enabled
;
128 bool unix_info_enabled
;
129 bool copyfile_enabled
;
130 bool veto_appledouble
;
132 bool aapl_zero_file_id
;
135 off_t time_machine_max_size
;
136 bool convert_adouble
;
137 bool wipe_intentionally_left_blank_rfork
;
138 bool delete_empty_adfiles
;
139 bool validate_afpinfo
;
142 * Additional options, all enabled by default,
143 * possibly useful for analyzing performance. The associated
144 * operations with each of them may be expensive, so having
145 * the chance to disable them individually gives a chance
146 * tweaking the setup for the particular usecase.
148 bool readdir_attr_rsize
;
149 bool readdir_attr_finder_info
;
150 bool readdir_attr_max_access
;
151 /* Recursion guard. Will go away when we have STATX. */
152 bool in_openat_pathref_fsp
;
155 static const struct enum_list fruit_rsrc
[] = {
156 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
157 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
158 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
162 static const struct enum_list fruit_meta
[] = {
163 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
164 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
168 static const struct enum_list fruit_locking
[] = {
169 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
170 {FRUIT_LOCKING_NONE
, "none"},
174 static const struct enum_list fruit_encoding
[] = {
175 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
176 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
181 vfs_handle_struct
*handle
;
182 files_struct
*fsp
; /* backlink to itself */
184 /* tcon config handle */
185 struct fruit_config_data
*config
;
187 /* Backend fsp for AppleDouble file, can be NULL */
188 files_struct
*ad_fsp
;
189 /* link from adouble_open_from_base_fsp() to fio */
190 struct fio
*real_fio
;
192 /* Denote stream type, meta or rsrc */
196 * AFP_AfpInfo stream created, but not written yet, thus still a fake
197 * pipe fd. This is set to true in fruit_open_meta if there was no
198 * existing stream but the caller requested O_CREAT. It is later set to
199 * false when we get a write on the stream that then does open and
207 /*****************************************************************************
209 *****************************************************************************/
211 static struct adouble
*ad_get_meta_fsp(TALLOC_CTX
*ctx
,
212 vfs_handle_struct
*handle
,
213 const struct smb_filename
*smb_fname
)
216 struct adouble
*ad
= NULL
;
217 struct smb_filename
*smb_fname_cp
= NULL
;
218 struct fruit_config_data
*config
= NULL
;
220 if (smb_fname
->fsp
!= NULL
) {
221 return ad_get(ctx
, handle
, smb_fname
, ADOUBLE_META
);
224 SMB_VFS_HANDLE_GET_DATA(handle
,
226 struct fruit_config_data
,
229 if (config
->in_openat_pathref_fsp
) {
233 smb_fname_cp
= cp_smb_filename(ctx
,
235 if (smb_fname_cp
== NULL
) {
238 TALLOC_FREE(smb_fname_cp
->stream_name
);
239 config
->in_openat_pathref_fsp
= true;
240 status
= openat_pathref_fsp(handle
->conn
->cwd_fsp
,
242 config
->in_openat_pathref_fsp
= false;
243 if (!NT_STATUS_IS_OK(status
)) {
244 TALLOC_FREE(smb_fname_cp
);
248 ad
= ad_get(ctx
, handle
, smb_fname_cp
, ADOUBLE_META
);
249 TALLOC_FREE(smb_fname_cp
);
253 static struct fio
*fruit_get_complete_fio(vfs_handle_struct
*handle
,
256 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
262 if (fio
->real_fio
!= NULL
) {
264 * This is an fsp from adouble_open_from_base_fsp()
265 * we should just pass this to the next
275 * Initialize config struct from our smb.conf config parameters
277 static int init_fruit_config(vfs_handle_struct
*handle
)
279 struct fruit_config_data
*config
;
281 const char *tm_size_str
= NULL
;
283 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
285 DEBUG(1, ("talloc_zero() failed\n"));
290 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
291 "resource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
293 DEBUG(1, ("value for %s: resource type unknown\n",
294 FRUIT_PARAM_TYPE_NAME
));
297 config
->rsrc
= (enum fruit_rsrc
)enumval
;
299 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
300 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
302 DEBUG(1, ("value for %s: metadata type unknown\n",
303 FRUIT_PARAM_TYPE_NAME
));
306 config
->meta
= (enum fruit_meta
)enumval
;
308 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
309 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
311 DEBUG(1, ("value for %s: locking type unknown\n",
312 FRUIT_PARAM_TYPE_NAME
));
315 config
->locking
= (enum fruit_locking
)enumval
;
317 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
318 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
320 DEBUG(1, ("value for %s: encoding type unknown\n",
321 FRUIT_PARAM_TYPE_NAME
));
324 config
->encoding
= (enum fruit_encoding
)enumval
;
326 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
327 config
->veto_appledouble
= lp_parm_bool(SNUM(handle
->conn
),
328 FRUIT_PARAM_TYPE_NAME
,
333 config
->use_aapl
= lp_parm_bool(
334 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
336 config
->time_machine
= lp_parm_bool(
337 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "time machine", false);
339 config
->unix_info_enabled
= lp_parm_bool(
340 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
342 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
345 config
->posix_rename
= lp_parm_bool(
346 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "posix_rename", true);
348 config
->aapl_zero_file_id
=
349 lp_parm_bool(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
350 "zero_file_id", true);
352 config
->readdir_attr_rsize
= lp_parm_bool(
353 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
355 config
->readdir_attr_finder_info
= lp_parm_bool(
356 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
358 config
->readdir_attr_max_access
= lp_parm_bool(
359 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
361 config
->model
= lp_parm_const_string(
362 -1, FRUIT_PARAM_TYPE_NAME
, "model", "MacSamba");
364 tm_size_str
= lp_parm_const_string(
365 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
366 "time machine max size", NULL
);
367 if (tm_size_str
!= NULL
) {
368 config
->time_machine_max_size
= conv_str_size(tm_size_str
);
371 config
->convert_adouble
= lp_parm_bool(
372 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
373 "convert_adouble", true);
375 config
->wipe_intentionally_left_blank_rfork
= lp_parm_bool(
376 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
377 "wipe_intentionally_left_blank_rfork", false);
379 config
->delete_empty_adfiles
= lp_parm_bool(
380 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
381 "delete_empty_adfiles", false);
383 config
->validate_afpinfo
= lp_parm_bool(
384 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
385 "validate_afpinfo", true);
387 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
388 NULL
, struct fruit_config_data
,
394 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
395 struct stream_struct
**streams
,
396 const char *name
, off_t size
,
399 struct stream_struct
*tmp
;
401 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
407 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
408 if (tmp
[*num_streams
].name
== NULL
) {
412 tmp
[*num_streams
].size
= size
;
413 tmp
[*num_streams
].alloc_size
= alloc_size
;
420 static bool filter_empty_rsrc_stream(unsigned int *num_streams
,
421 struct stream_struct
**streams
)
423 struct stream_struct
*tmp
= *streams
;
426 if (*num_streams
== 0) {
430 for (i
= 0; i
< *num_streams
; i
++) {
431 if (strequal_m(tmp
[i
].name
, AFPRESOURCE_STREAM
)) {
436 if (i
== *num_streams
) {
440 if (tmp
[i
].size
> 0) {
444 TALLOC_FREE(tmp
[i
].name
);
445 ARRAY_DEL_ELEMENT(tmp
, i
, *num_streams
);
450 static bool del_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
451 struct stream_struct
**streams
,
454 struct stream_struct
*tmp
= *streams
;
457 if (*num_streams
== 0) {
461 for (i
= 0; i
< *num_streams
; i
++) {
462 if (strequal_m(tmp
[i
].name
, name
)) {
467 if (i
== *num_streams
) {
471 TALLOC_FREE(tmp
[i
].name
);
472 ARRAY_DEL_ELEMENT(tmp
, i
, *num_streams
);
477 static bool ad_empty_finderinfo(const struct adouble
*ad
)
480 char emptybuf
[ADEDLEN_FINDERI
] = {0};
483 fi
= ad_get_entry(ad
, ADEID_FINDERI
);
485 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad
);
489 cmp
= memcmp(emptybuf
, fi
, ADEDLEN_FINDERI
);
493 static bool ai_empty_finderinfo(const AfpInfo
*ai
)
496 char emptybuf
[ADEDLEN_FINDERI
] = {0};
498 cmp
= memcmp(emptybuf
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
503 * Update btime with btime from Netatalk
505 static void update_btime(vfs_handle_struct
*handle
,
506 struct smb_filename
*smb_fname
)
509 struct timespec creation_time
= {0};
511 struct fruit_config_data
*config
= NULL
;
513 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
516 switch (config
->meta
) {
517 case FRUIT_META_STREAM
:
519 case FRUIT_META_NETATALK
:
523 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
527 ad
= ad_get_meta_fsp(talloc_tos(), handle
, smb_fname
);
531 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
537 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
538 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
544 * Map an access mask to a Netatalk single byte byte range lock
546 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
547 uint32_t access_mask
)
551 switch (access_mask
) {
553 offset
= AD_FILELOCK_OPEN_RD
;
556 case FILE_WRITE_DATA
:
557 case FILE_APPEND_DATA
:
558 offset
= AD_FILELOCK_OPEN_WR
;
562 offset
= AD_FILELOCK_OPEN_NONE
;
566 if (fork_type
== APPLE_FORK_RSRC
) {
567 if (offset
== AD_FILELOCK_OPEN_NONE
) {
568 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
578 * Map a deny mode to a Netatalk brl
580 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
587 offset
= AD_FILELOCK_DENY_RD
;
591 offset
= AD_FILELOCK_DENY_WR
;
595 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
598 if (fork_type
== APPLE_FORK_RSRC
) {
606 * Call fcntl() with an exclusive F_GETLK request in order to
607 * determine if there's an existing shared lock
609 * @return true if the requested lock was found or any error occurred
610 * false if the lock was not found
612 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
615 off_t offset
= in_offset
;
620 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
621 if (result
== false) {
625 if (type
!= F_UNLCK
) {
632 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
634 uint32_t access_mask
,
637 NTSTATUS status
= NT_STATUS_OK
;
639 bool share_for_read
= (share_mode
& FILE_SHARE_READ
);
640 bool share_for_write
= (share_mode
& FILE_SHARE_WRITE
);
641 bool netatalk_already_open_for_reading
= false;
642 bool netatalk_already_open_for_writing
= false;
643 bool netatalk_already_open_with_deny_read
= false;
644 bool netatalk_already_open_with_deny_write
= false;
645 struct GUID req_guid
= GUID_random();
647 /* FIXME: hardcoded data fork, add resource fork */
648 enum apple_fork fork_type
= APPLE_FORK_DATA
;
650 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
652 access_mask
& FILE_READ_DATA
? "READ" :"-",
653 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
656 if (fsp_get_io_fd(fsp
) == -1) {
660 /* Read NetATalk opens and deny modes on the file. */
661 netatalk_already_open_for_reading
= test_netatalk_lock(fsp
,
662 access_to_netatalk_brl(fork_type
,
665 netatalk_already_open_with_deny_read
= test_netatalk_lock(fsp
,
666 denymode_to_netatalk_brl(fork_type
,
669 netatalk_already_open_for_writing
= test_netatalk_lock(fsp
,
670 access_to_netatalk_brl(fork_type
,
673 netatalk_already_open_with_deny_write
= test_netatalk_lock(fsp
,
674 denymode_to_netatalk_brl(fork_type
,
677 /* If there are any conflicts - sharing violation. */
678 if ((access_mask
& FILE_READ_DATA
) &&
679 netatalk_already_open_with_deny_read
) {
680 return NT_STATUS_SHARING_VIOLATION
;
683 if (!share_for_read
&&
684 netatalk_already_open_for_reading
) {
685 return NT_STATUS_SHARING_VIOLATION
;
688 if ((access_mask
& FILE_WRITE_DATA
) &&
689 netatalk_already_open_with_deny_write
) {
690 return NT_STATUS_SHARING_VIOLATION
;
693 if (!share_for_write
&&
694 netatalk_already_open_for_writing
) {
695 return NT_STATUS_SHARING_VIOLATION
;
698 if (!(access_mask
& FILE_READ_DATA
)) {
700 * Nothing we can do here, we need read access
706 /* Set NetAtalk locks matching our access */
707 if (access_mask
& FILE_READ_DATA
) {
708 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
709 req_guid
.time_hi_and_version
= __LINE__
;
714 fsp
->op
->global
->open_persistent_id
,
722 if (!NT_STATUS_IS_OK(status
)) {
727 if (!share_for_read
) {
728 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
729 req_guid
.time_hi_and_version
= __LINE__
;
734 fsp
->op
->global
->open_persistent_id
,
742 if (!NT_STATUS_IS_OK(status
)) {
747 if (access_mask
& FILE_WRITE_DATA
) {
748 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
749 req_guid
.time_hi_and_version
= __LINE__
;
754 fsp
->op
->global
->open_persistent_id
,
762 if (!NT_STATUS_IS_OK(status
)) {
767 if (!share_for_write
) {
768 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
769 req_guid
.time_hi_and_version
= __LINE__
;
774 fsp
->op
->global
->open_persistent_id
,
782 if (!NT_STATUS_IS_OK(status
)) {
790 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
791 struct smb_request
*req
,
792 const struct smb2_create_blobs
*in_context_blobs
,
793 struct smb2_create_blobs
*out_context_blobs
)
795 struct fruit_config_data
*config
;
797 struct smb2_create_blob
*aapl
= NULL
;
801 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
802 uint64_t req_bitmap
, client_caps
;
803 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
807 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
808 return NT_STATUS_UNSUCCESSFUL
);
810 if (!config
->use_aapl
811 || in_context_blobs
== NULL
812 || out_context_blobs
== NULL
) {
816 aapl
= smb2_create_blob_find(in_context_blobs
,
817 SMB2_CREATE_TAG_AAPL
);
822 if (aapl
->data
.length
!= 24) {
823 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
824 (uintmax_t)aapl
->data
.length
));
825 return NT_STATUS_INVALID_PARAMETER
;
828 cmd
= IVAL(aapl
->data
.data
, 0);
829 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
830 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
831 return NT_STATUS_INVALID_PARAMETER
;
834 req_bitmap
= BVAL(aapl
->data
.data
, 8);
835 client_caps
= BVAL(aapl
->data
.data
, 16);
837 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
839 SBVAL(p
, 8, req_bitmap
);
840 ok
= data_blob_append(req
, &blob
, p
, 16);
842 return NT_STATUS_UNSUCCESSFUL
;
845 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
846 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
847 (handle
->conn
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
848 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
849 config
->readdir_attr_enabled
= true;
852 if (config
->use_copyfile
) {
853 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
854 config
->copyfile_enabled
= true;
858 * The client doesn't set the flag, so we can't check
859 * for it and just set it unconditionally
861 if (config
->unix_info_enabled
) {
862 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
865 SBVAL(p
, 0, server_caps
);
866 ok
= data_blob_append(req
, &blob
, p
, 8);
868 return NT_STATUS_UNSUCCESSFUL
;
872 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
873 int val
= lp_case_sensitive(SNUM(handle
->conn
));
881 caps
|= SMB2_CRTCTX_AAPL_CASE_SENSITIVE
;
888 if (config
->time_machine
) {
889 caps
|= SMB2_CRTCTX_AAPL_FULL_SYNC
;
894 ok
= data_blob_append(req
, &blob
, p
, 8);
896 return NT_STATUS_UNSUCCESSFUL
;
900 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
901 ok
= convert_string_talloc(req
,
903 config
->model
, strlen(config
->model
),
906 return NT_STATUS_UNSUCCESSFUL
;
910 SIVAL(p
+ 4, 0, modellen
);
911 ok
= data_blob_append(req
, &blob
, p
, 8);
914 return NT_STATUS_UNSUCCESSFUL
;
917 ok
= data_blob_append(req
, &blob
, model
, modellen
);
920 return NT_STATUS_UNSUCCESSFUL
;
924 status
= smb2_create_blob_add(out_context_blobs
,
926 SMB2_CREATE_TAG_AAPL
,
928 if (NT_STATUS_IS_OK(status
)) {
929 global_fruit_config
.nego_aapl
= true;
935 static bool readdir_attr_meta_finderi_stream(
936 struct vfs_handle_struct
*handle
,
937 const struct smb_filename
*smb_fname
,
940 struct smb_filename
*stream_name
= NULL
;
941 files_struct
*fsp
= NULL
;
945 uint8_t buf
[AFP_INFO_SIZE
];
947 status
= synthetic_pathref(talloc_tos(),
948 handle
->conn
->cwd_fsp
,
949 smb_fname
->base_name
,
955 if (!NT_STATUS_IS_OK(status
)) {
959 status
= SMB_VFS_CREATE_FILE(
960 handle
->conn
, /* conn */
963 stream_name
, /* fname */
964 FILE_READ_DATA
, /* access_mask */
965 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
967 FILE_OPEN
, /* create_disposition*/
968 0, /* create_options */
969 0, /* file_attributes */
970 INTERNAL_OPEN_ONLY
, /* oplock_request */
972 0, /* allocation_size */
973 0, /* private_flags */
978 NULL
, NULL
); /* create context */
980 TALLOC_FREE(stream_name
);
982 if (!NT_STATUS_IS_OK(status
)) {
986 nread
= SMB_VFS_PREAD(fsp
, &buf
[0], AFP_INFO_SIZE
, 0);
987 if (nread
!= AFP_INFO_SIZE
) {
988 DBG_ERR("short read [%s] [%zd/%d]\n",
989 smb_fname_str_dbg(stream_name
), nread
, AFP_INFO_SIZE
);
994 memcpy(&ai
->afpi_FinderInfo
[0], &buf
[AFP_OFF_FinderInfo
],
1001 close_file_free(NULL
, &fsp
, NORMAL_CLOSE
);
1007 static bool readdir_attr_meta_finderi_netatalk(
1008 struct vfs_handle_struct
*handle
,
1009 const struct smb_filename
*smb_fname
,
1012 struct adouble
*ad
= NULL
;
1015 ad
= ad_get_meta_fsp(talloc_tos(), handle
, smb_fname
);
1020 p
= ad_get_entry(ad
, ADEID_FINDERI
);
1022 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname
->base_name
);
1027 memcpy(&ai
->afpi_FinderInfo
[0], p
, AFP_FinderSize
);
1032 static bool readdir_attr_meta_finderi(struct vfs_handle_struct
*handle
,
1033 const struct smb_filename
*smb_fname
,
1034 struct readdir_attr_data
*attr_data
)
1036 struct fruit_config_data
*config
= NULL
;
1037 uint32_t date_added
;
1041 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1042 struct fruit_config_data
,
1045 switch (config
->meta
) {
1046 case FRUIT_META_NETATALK
:
1047 ok
= readdir_attr_meta_finderi_netatalk(
1048 handle
, smb_fname
, &ai
);
1051 case FRUIT_META_STREAM
:
1052 ok
= readdir_attr_meta_finderi_stream(
1053 handle
, smb_fname
, &ai
);
1057 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1062 /* Don't bother with errors, it's likely ENOENT */
1066 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
1068 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
1069 &ai
.afpi_FinderInfo
[0], 4);
1071 /* finder_creator */
1072 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
1073 &ai
.afpi_FinderInfo
[4], 4);
1077 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
1078 &ai
.afpi_FinderInfo
[8], 2);
1080 /* finder_ext_flags */
1081 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
1082 &ai
.afpi_FinderInfo
[24], 2);
1085 date_added
= convert_time_t_to_uint32_t(
1086 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
1088 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
1093 static uint64_t readdir_attr_rfork_size_adouble(
1094 struct vfs_handle_struct
*handle
,
1095 const struct smb_filename
*smb_fname
)
1097 struct adouble
*ad
= NULL
;
1098 uint64_t rfork_size
;
1100 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
1106 rfork_size
= ad_getentrylen(ad
, ADEID_RFORK
);
1112 static uint64_t readdir_attr_rfork_size_stream(
1113 struct vfs_handle_struct
*handle
,
1114 const struct smb_filename
*smb_fname
)
1116 struct smb_filename
*stream_name
= NULL
;
1118 uint64_t rfork_size
;
1120 stream_name
= synthetic_smb_fname(talloc_tos(),
1121 smb_fname
->base_name
,
1122 AFPRESOURCE_STREAM_NAME
,
1126 if (stream_name
== NULL
) {
1130 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
1132 TALLOC_FREE(stream_name
);
1136 rfork_size
= stream_name
->st
.st_ex_size
;
1137 TALLOC_FREE(stream_name
);
1142 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct
*handle
,
1143 const struct smb_filename
*smb_fname
)
1145 struct fruit_config_data
*config
= NULL
;
1146 uint64_t rfork_size
;
1148 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1149 struct fruit_config_data
,
1152 switch (config
->rsrc
) {
1153 case FRUIT_RSRC_ADFILE
:
1154 rfork_size
= readdir_attr_rfork_size_adouble(handle
,
1158 case FRUIT_RSRC_XATTR
:
1159 case FRUIT_RSRC_STREAM
:
1160 rfork_size
= readdir_attr_rfork_size_stream(handle
,
1165 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1173 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
1174 const struct smb_filename
*smb_fname
,
1175 struct readdir_attr_data
*attr_data
)
1177 NTSTATUS status
= NT_STATUS_OK
;
1178 struct fruit_config_data
*config
= NULL
;
1181 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1182 struct fruit_config_data
,
1183 return NT_STATUS_UNSUCCESSFUL
);
1186 /* Ensure we return a default value in the creation_date field */
1187 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
1190 * Resource fork length
1193 if (config
->readdir_attr_rsize
) {
1194 uint64_t rfork_size
;
1196 rfork_size
= readdir_attr_rfork_size(handle
, smb_fname
);
1197 attr_data
->attr_data
.aapl
.rfork_size
= rfork_size
;
1204 if (config
->readdir_attr_finder_info
) {
1205 ok
= readdir_attr_meta_finderi(handle
, smb_fname
, attr_data
);
1207 status
= NT_STATUS_INTERNAL_ERROR
;
1214 static NTSTATUS
remove_virtual_nfs_aces(struct security_descriptor
*psd
)
1219 if (psd
->dacl
== NULL
) {
1220 return NT_STATUS_OK
;
1223 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
1224 /* MS NFS style mode/uid/gid */
1225 int cmp
= dom_sid_compare_domain(
1226 &global_sid_Unix_NFS
,
1227 &psd
->dacl
->aces
[i
].trustee
);
1229 /* Normal ACE entry. */
1234 * security_descriptor_dacl_del()
1235 * *must* return NT_STATUS_OK as we know
1236 * we have something to remove.
1239 status
= security_descriptor_dacl_del(psd
,
1240 &psd
->dacl
->aces
[i
].trustee
);
1241 if (!NT_STATUS_IS_OK(status
)) {
1242 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1248 * security_descriptor_dacl_del() may delete more
1249 * then one entry subsequent to this one if the
1250 * SID matches, but we only need to ensure that
1251 * we stay looking at the same element in the array.
1255 return NT_STATUS_OK
;
1258 /* Search MS NFS style ACE with UNIX mode */
1259 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
1261 struct security_descriptor
*psd
,
1266 struct fruit_config_data
*config
= NULL
;
1270 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1271 struct fruit_config_data
,
1272 return NT_STATUS_UNSUCCESSFUL
);
1274 if (!global_fruit_config
.nego_aapl
) {
1275 return NT_STATUS_OK
;
1277 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
1278 return NT_STATUS_OK
;
1281 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
1282 if (dom_sid_compare_domain(
1283 &global_sid_Unix_NFS_Mode
,
1284 &psd
->dacl
->aces
[i
].trustee
) == 0) {
1285 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
1286 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1289 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1290 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
1296 * Remove any incoming virtual ACE entries generated by
1297 * fruit_fget_nt_acl().
1300 return remove_virtual_nfs_aces(psd
);
1303 /****************************************************************************
1305 ****************************************************************************/
1307 static int fruit_connect(vfs_handle_struct
*handle
,
1308 const char *service
,
1312 char *list
= NULL
, *newlist
= NULL
;
1313 struct fruit_config_data
*config
;
1314 const struct loadparm_substitution
*lp_sub
=
1315 loadparm_s3_global_substitution();
1317 DEBUG(10, ("fruit_connect\n"));
1319 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
1324 rc
= init_fruit_config(handle
);
1329 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1330 struct fruit_config_data
, return -1);
1332 if (config
->veto_appledouble
) {
1333 list
= lp_veto_files(talloc_tos(), lp_sub
, SNUM(handle
->conn
));
1336 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
1337 newlist
= talloc_asprintf(
1339 "%s/" ADOUBLE_NAME_PREFIX
"*/",
1341 lp_do_parameter(SNUM(handle
->conn
),
1346 lp_do_parameter(SNUM(handle
->conn
),
1348 "/" ADOUBLE_NAME_PREFIX
"*/");
1354 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
1355 lp_do_parameter(SNUM(handle
->conn
),
1357 macos_string_replace_map
);
1360 if (config
->time_machine
) {
1361 DBG_NOTICE("Enabling durable handles for Time Machine "
1362 "support on [%s]\n", service
);
1363 lp_do_parameter(SNUM(handle
->conn
), "durable handles", "yes");
1364 lp_do_parameter(SNUM(handle
->conn
), "kernel oplocks", "no");
1365 lp_do_parameter(SNUM(handle
->conn
), "kernel share modes", "no");
1366 if (!lp_strict_sync(SNUM(handle
->conn
))) {
1367 DBG_WARNING("Time Machine without strict sync is not "
1370 lp_do_parameter(SNUM(handle
->conn
), "posix locking", "no");
1376 static void fio_ref_destroy_fn(void *p_data
)
1378 struct fio
*ref_fio
= (struct fio
*)p_data
;
1379 if (ref_fio
->real_fio
!= NULL
) {
1380 SMB_ASSERT(ref_fio
->real_fio
->ad_fsp
== ref_fio
->fsp
);
1381 ref_fio
->real_fio
->ad_fsp
= NULL
;
1382 ref_fio
->real_fio
= NULL
;
1386 static void fio_close_ad_fsp(struct fio
*fio
)
1388 if (fio
->ad_fsp
!= NULL
) {
1389 fd_close(fio
->ad_fsp
);
1390 file_free(NULL
, fio
->ad_fsp
);
1391 /* fio_ref_destroy_fn() should have cleared this */
1392 SMB_ASSERT(fio
->ad_fsp
== NULL
);
1396 static void fio_destroy_fn(void *p_data
)
1398 struct fio
*fio
= (struct fio
*)p_data
;
1399 fio_close_ad_fsp(fio
);
1402 static int fruit_open_meta_stream(vfs_handle_struct
*handle
,
1403 const struct files_struct
*dirfsp
,
1404 const struct smb_filename
*smb_fname
,
1409 struct fruit_config_data
*config
= NULL
;
1410 struct fio
*fio
= NULL
;
1411 struct vfs_open_how how
= {
1412 .flags
= flags
& ~O_CREAT
,
1417 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1419 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1420 struct fruit_config_data
, return -1);
1422 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, fio_destroy_fn
);
1423 fio
->handle
= handle
;
1425 fio
->type
= ADOUBLE_META
;
1426 fio
->config
= config
;
1428 fd
= SMB_VFS_NEXT_OPENAT(handle
,
1437 if (!(flags
& O_CREAT
)) {
1438 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
1444 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
1448 fio
->fake_fd
= true;
1455 static int fruit_open_meta_netatalk(vfs_handle_struct
*handle
,
1456 const struct files_struct
*dirfsp
,
1457 const struct smb_filename
*smb_fname
,
1462 struct fruit_config_data
*config
= NULL
;
1463 struct fio
*fio
= NULL
;
1464 struct adouble
*ad
= NULL
;
1465 bool meta_exists
= false;
1468 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1471 * We know this is a stream open, so fsp->base_fsp must
1474 SMB_ASSERT(fsp_is_alternate_stream(fsp
));
1475 SMB_ASSERT(fsp
->base_fsp
->fsp_name
->fsp
== fsp
->base_fsp
);
1477 ad
= ad_get(talloc_tos(), handle
, fsp
->base_fsp
->fsp_name
, ADOUBLE_META
);
1484 if (!meta_exists
&& !(flags
& O_CREAT
)) {
1494 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1495 struct fruit_config_data
, return -1);
1497 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, fio_destroy_fn
);
1498 fio
->handle
= handle
;
1500 fio
->type
= ADOUBLE_META
;
1501 fio
->config
= config
;
1502 fio
->fake_fd
= true;
1509 static int fruit_open_meta(vfs_handle_struct
*handle
,
1510 const struct files_struct
*dirfsp
,
1511 const struct smb_filename
*smb_fname
,
1512 files_struct
*fsp
, int flags
, mode_t mode
)
1515 struct fruit_config_data
*config
= NULL
;
1517 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname
));
1519 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1520 struct fruit_config_data
, return -1);
1522 switch (config
->meta
) {
1523 case FRUIT_META_STREAM
:
1524 fd
= fruit_open_meta_stream(handle
, dirfsp
, smb_fname
,
1528 case FRUIT_META_NETATALK
:
1529 fd
= fruit_open_meta_netatalk(handle
, dirfsp
, smb_fname
,
1534 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1538 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1543 static int fruit_open_rsrc_adouble(vfs_handle_struct
*handle
,
1544 const struct files_struct
*dirfsp
,
1545 const struct smb_filename
*smb_fname
,
1551 struct fruit_config_data
*config
= NULL
;
1552 struct files_struct
*ad_fsp
= NULL
;
1553 struct fio
*fio
= NULL
;
1554 struct fio
*ref_fio
= NULL
;
1558 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1559 struct fruit_config_data
, return -1);
1561 if ((!(flags
& O_CREAT
)) &&
1562 S_ISDIR(fsp
->base_fsp
->fsp_name
->st
.st_ex_mode
))
1564 /* sorry, but directories don't have a resource fork */
1571 * We return a fake_fd to the vfs modules above,
1572 * while we open an internal backend fsp for the
1573 * '._' file for the next vfs modules.
1575 * Note that adouble_open_from_base_fsp() recurses
1576 * into fruit_openat(), but it'll just pass to
1577 * the next module as just opens a flat file on
1587 status
= adouble_open_from_base_fsp(fsp
->conn
->cwd_fsp
,
1593 if (!NT_STATUS_IS_OK(status
)) {
1594 errno
= map_errno_from_nt_status(status
);
1600 * Now we need to glue both handles together,
1601 * so that they automatically detach each other
1604 fio
= fruit_get_complete_fio(handle
, fsp
);
1606 DBG_ERR("fio=NULL for [%s]\n", fsp_str_dbg(fsp
));
1612 ref_fio
= VFS_ADD_FSP_EXTENSION(handle
, ad_fsp
,
1614 fio_ref_destroy_fn
);
1615 if (ref_fio
== NULL
) {
1616 int saved_errno
= errno
;
1618 file_free(NULL
, ad_fsp
);
1620 errno
= saved_errno
;
1625 SMB_ASSERT(ref_fio
->fsp
== NULL
);
1626 ref_fio
->handle
= handle
;
1627 ref_fio
->fsp
= ad_fsp
;
1628 ref_fio
->type
= ADOUBLE_RSRC
;
1629 ref_fio
->config
= config
;
1630 ref_fio
->real_fio
= fio
;
1631 SMB_ASSERT(fio
->ad_fsp
== NULL
);
1632 fio
->ad_fsp
= ad_fsp
;
1633 fio
->fake_fd
= true;
1637 DEBUG(10, ("fruit_open resource fork: rc=%d\n", rc
));
1639 int saved_errno
= errno
;
1641 vfs_fake_fd_close(fd
);
1643 errno
= saved_errno
;
1649 static int fruit_open_rsrc_xattr(vfs_handle_struct
*handle
,
1650 const struct files_struct
*dirfsp
,
1651 const struct smb_filename
*smb_fname
,
1656 #ifdef HAVE_ATTROPEN
1660 * As there's no attropenat() this is only going to work with AT_FDCWD.
1662 SMB_ASSERT(fsp_get_pathref_fd(dirfsp
) == AT_FDCWD
);
1664 fd
= attropen(smb_fname
->base_name
,
1665 AFPRESOURCE_EA_NETATALK
,
1680 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
1681 const struct files_struct
*dirfsp
,
1682 const struct smb_filename
*smb_fname
,
1683 files_struct
*fsp
, int flags
, mode_t mode
)
1686 struct fruit_config_data
*config
= NULL
;
1687 struct fio
*fio
= NULL
;
1689 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1691 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1692 struct fruit_config_data
, return -1);
1694 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, fio_destroy_fn
);
1695 fio
->handle
= handle
;
1697 fio
->type
= ADOUBLE_RSRC
;
1698 fio
->config
= config
;
1700 switch (config
->rsrc
) {
1701 case FRUIT_RSRC_STREAM
: {
1702 struct vfs_open_how how
= {
1703 .flags
= flags
, .mode
= mode
,
1705 fd
= SMB_VFS_NEXT_OPENAT(handle
,
1713 case FRUIT_RSRC_ADFILE
:
1714 fd
= fruit_open_rsrc_adouble(handle
, dirfsp
, smb_fname
,
1718 case FRUIT_RSRC_XATTR
:
1719 fd
= fruit_open_rsrc_xattr(handle
, dirfsp
, smb_fname
,
1724 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1729 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1738 static int fruit_openat(vfs_handle_struct
*handle
,
1739 const struct files_struct
*dirfsp
,
1740 const struct smb_filename
*smb_fname
,
1742 const struct vfs_open_how
*how
)
1746 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1748 if (!is_named_stream(smb_fname
)) {
1749 return SMB_VFS_NEXT_OPENAT(handle
,
1756 if (how
->resolve
!= 0) {
1761 SMB_ASSERT(fsp_is_alternate_stream(fsp
));
1763 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
1764 fd
= fruit_open_meta(handle
,
1770 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
1771 fd
= fruit_open_rsrc(handle
,
1778 fd
= SMB_VFS_NEXT_OPENAT(handle
,
1785 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1787 /* Prevent reopen optimisation */
1788 fsp
->fsp_flags
.have_proc_fds
= false;
1792 static int fruit_close_meta(vfs_handle_struct
*handle
,
1796 struct fruit_config_data
*config
= NULL
;
1798 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1799 struct fruit_config_data
, return -1);
1801 switch (config
->meta
) {
1802 case FRUIT_META_STREAM
:
1804 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
1809 ret
= vfs_fake_fd_close(fsp_get_pathref_fd(fsp
));
1810 fsp_set_fd(fsp
, -1);
1812 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1816 case FRUIT_META_NETATALK
:
1817 ret
= vfs_fake_fd_close(fsp_get_pathref_fd(fsp
));
1818 fsp_set_fd(fsp
, -1);
1822 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1830 static int fruit_close_rsrc(vfs_handle_struct
*handle
,
1834 struct fruit_config_data
*config
= NULL
;
1836 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1837 struct fruit_config_data
, return -1);
1839 switch (config
->rsrc
) {
1840 case FRUIT_RSRC_STREAM
:
1841 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1844 case FRUIT_RSRC_ADFILE
:
1846 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
1850 fio_close_ad_fsp(fio
);
1851 ret
= vfs_fake_fd_close(fsp_get_pathref_fd(fsp
));
1852 fsp_set_fd(fsp
, -1);
1856 case FRUIT_RSRC_XATTR
:
1857 ret
= vfs_fake_fd_close(fsp_get_pathref_fd(fsp
));
1858 fsp_set_fd(fsp
, -1);
1862 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1869 static int fruit_close(vfs_handle_struct
*handle
,
1875 fd
= fsp_get_pathref_fd(fsp
);
1877 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp
->fsp_name
), fd
);
1879 if (!fsp_is_alternate_stream(fsp
)) {
1880 return SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1883 if (is_afpinfo_stream(fsp
->fsp_name
->stream_name
)) {
1884 ret
= fruit_close_meta(handle
, fsp
);
1885 } else if (is_afpresource_stream(fsp
->fsp_name
->stream_name
)) {
1886 ret
= fruit_close_rsrc(handle
, fsp
);
1888 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1894 static int fruit_renameat(struct vfs_handle_struct
*handle
,
1895 files_struct
*srcfsp
,
1896 const struct smb_filename
*smb_fname_src
,
1897 files_struct
*dstfsp
,
1898 const struct smb_filename
*smb_fname_dst
,
1899 const struct vfs_rename_how
*how
)
1902 struct fruit_config_data
*config
= NULL
;
1903 struct smb_filename
*src_adp_smb_fname
= NULL
;
1904 struct smb_filename
*dst_adp_smb_fname
= NULL
;
1906 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1907 struct fruit_config_data
, return -1);
1909 if (!VALID_STAT(smb_fname_src
->st
)) {
1910 DBG_ERR("Need valid stat for [%s]\n",
1911 smb_fname_str_dbg(smb_fname_src
));
1915 rc
= SMB_VFS_NEXT_RENAMEAT(handle
,
1925 if ((config
->rsrc
!= FRUIT_RSRC_ADFILE
) ||
1926 (!S_ISREG(smb_fname_src
->st
.st_ex_mode
)))
1931 rc
= adouble_path(talloc_tos(), smb_fname_src
, &src_adp_smb_fname
);
1936 rc
= adouble_path(talloc_tos(), smb_fname_dst
, &dst_adp_smb_fname
);
1941 DBG_DEBUG("%s -> %s\n",
1942 smb_fname_str_dbg(src_adp_smb_fname
),
1943 smb_fname_str_dbg(dst_adp_smb_fname
));
1945 rc
= SMB_VFS_NEXT_RENAMEAT(handle
,
1951 if (errno
== ENOENT
) {
1956 TALLOC_FREE(src_adp_smb_fname
);
1957 TALLOC_FREE(dst_adp_smb_fname
);
1961 static int fruit_unlink_meta_stream(vfs_handle_struct
*handle
,
1962 struct files_struct
*dirfsp
,
1963 const struct smb_filename
*smb_fname
)
1965 return SMB_VFS_NEXT_UNLINKAT(handle
,
1971 static int fruit_unlink_meta_netatalk(vfs_handle_struct
*handle
,
1972 const struct smb_filename
*smb_fname
)
1974 SMB_ASSERT(smb_fname
->fsp
!= NULL
);
1975 SMB_ASSERT(fsp_is_alternate_stream(smb_fname
->fsp
));
1976 return SMB_VFS_FREMOVEXATTR(smb_fname
->fsp
->base_fsp
,
1977 AFPINFO_EA_NETATALK
);
1980 static int fruit_unlink_meta(vfs_handle_struct
*handle
,
1981 struct files_struct
*dirfsp
,
1982 const struct smb_filename
*smb_fname
)
1984 struct fruit_config_data
*config
= NULL
;
1987 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1988 struct fruit_config_data
, return -1);
1990 switch (config
->meta
) {
1991 case FRUIT_META_STREAM
:
1992 rc
= fruit_unlink_meta_stream(handle
,
1997 case FRUIT_META_NETATALK
:
1998 rc
= fruit_unlink_meta_netatalk(handle
, smb_fname
);
2002 DBG_ERR("Unsupported meta config [%d]\n", config
->meta
);
2009 static int fruit_unlink_rsrc_stream(vfs_handle_struct
*handle
,
2010 struct files_struct
*dirfsp
,
2011 const struct smb_filename
*smb_fname
,
2016 if (!force_unlink
) {
2017 struct smb_filename
*full_fname
= NULL
;
2021 * TODO: use SMB_VFS_STATX() once we have it.
2024 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
2027 if (full_fname
== NULL
) {
2032 * 0 byte resource fork streams are not listed by
2033 * vfs_streaminfo, as a result stream cleanup/deletion of file
2034 * deletion doesn't remove the resourcefork stream.
2037 ret
= SMB_VFS_NEXT_STAT(handle
, full_fname
);
2039 TALLOC_FREE(full_fname
);
2040 DBG_ERR("stat [%s] failed [%s]\n",
2041 smb_fname_str_dbg(full_fname
), strerror(errno
));
2045 size
= full_fname
->st
.st_ex_size
;
2046 TALLOC_FREE(full_fname
);
2049 /* OS X ignores resource fork stream delete requests */
2054 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
2058 if ((ret
!= 0) && (errno
== ENOENT
) && force_unlink
) {
2065 static int fruit_unlink_rsrc_adouble(vfs_handle_struct
*handle
,
2066 struct files_struct
*dirfsp
,
2067 const struct smb_filename
*smb_fname
,
2071 struct adouble
*ad
= NULL
;
2072 struct smb_filename
*adp_smb_fname
= NULL
;
2074 if (!force_unlink
) {
2075 struct smb_filename
*full_fname
= NULL
;
2077 full_fname
= full_path_from_dirfsp_atname(talloc_tos(),
2080 if (full_fname
== NULL
) {
2084 ad
= ad_get(talloc_tos(), handle
, full_fname
,
2086 TALLOC_FREE(full_fname
);
2094 * 0 byte resource fork streams are not listed by
2095 * vfs_streaminfo, as a result stream cleanup/deletion of file
2096 * deletion doesn't remove the resourcefork stream.
2099 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
2100 /* OS X ignores resource fork stream delete requests */
2108 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
2113 rc
= SMB_VFS_NEXT_UNLINKAT(handle
,
2117 TALLOC_FREE(adp_smb_fname
);
2118 if ((rc
!= 0) && (errno
== ENOENT
|| errno
== ENAMETOOLONG
) && force_unlink
) {
2125 static int fruit_unlink_rsrc_xattr(vfs_handle_struct
*handle
,
2126 const struct smb_filename
*smb_fname
,
2130 * OS X ignores resource fork stream delete requests, so nothing to do
2131 * here. Removing the file will remove the xattr anyway, so we don't
2132 * have to take care of removing 0 byte resource forks that could be
2138 static int fruit_unlink_rsrc(vfs_handle_struct
*handle
,
2139 struct files_struct
*dirfsp
,
2140 const struct smb_filename
*smb_fname
,
2143 struct fruit_config_data
*config
= NULL
;
2146 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2147 struct fruit_config_data
, return -1);
2149 switch (config
->rsrc
) {
2150 case FRUIT_RSRC_STREAM
:
2151 rc
= fruit_unlink_rsrc_stream(handle
,
2157 case FRUIT_RSRC_ADFILE
:
2158 rc
= fruit_unlink_rsrc_adouble(handle
,
2164 case FRUIT_RSRC_XATTR
:
2165 rc
= fruit_unlink_rsrc_xattr(handle
, smb_fname
, force_unlink
);
2169 DBG_ERR("Unsupported rsrc config [%d]\n", config
->rsrc
);
2176 static int fruit_fchmod(vfs_handle_struct
*handle
,
2177 struct files_struct
*fsp
,
2181 struct fruit_config_data
*config
= NULL
;
2182 struct smb_filename
*smb_fname_adp
= NULL
;
2183 const struct smb_filename
*smb_fname
= NULL
;
2186 rc
= SMB_VFS_NEXT_FCHMOD(handle
, fsp
, mode
);
2191 smb_fname
= fsp
->fsp_name
;
2192 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2193 struct fruit_config_data
, return -1);
2195 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
2199 if (!VALID_STAT(smb_fname
->st
)) {
2203 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
2207 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_adp
);
2212 status
= openat_pathref_fsp(handle
->conn
->cwd_fsp
,
2214 if (!NT_STATUS_IS_OK(status
)) {
2215 /* detect ENOENT (mapped to OBJECT_NAME_NOT_FOUND) */
2216 if (NT_STATUS_EQUAL(status
,
2217 NT_STATUS_OBJECT_NAME_NOT_FOUND
)){
2225 DBG_DEBUG("%s\n", smb_fname_adp
->base_name
);
2227 rc
= SMB_VFS_NEXT_FCHMOD(handle
, smb_fname_adp
->fsp
, mode
);
2228 if (errno
== ENOENT
) {
2232 TALLOC_FREE(smb_fname_adp
);
2236 static int fruit_unlinkat(vfs_handle_struct
*handle
,
2237 struct files_struct
*dirfsp
,
2238 const struct smb_filename
*smb_fname
,
2241 struct fruit_config_data
*config
= NULL
;
2242 struct smb_filename
*rsrc_smb_fname
= NULL
;
2245 if (flags
& AT_REMOVEDIR
) {
2246 return SMB_VFS_NEXT_UNLINKAT(handle
,
2252 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2253 struct fruit_config_data
, return -1);
2255 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
2256 return fruit_unlink_meta(handle
,
2259 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
2260 return fruit_unlink_rsrc(handle
,
2264 } else if (is_named_stream(smb_fname
)) {
2265 return SMB_VFS_NEXT_UNLINKAT(handle
,
2269 } else if (is_adouble_file(smb_fname
->base_name
)) {
2270 return SMB_VFS_NEXT_UNLINKAT(handle
,
2277 * A request to delete the base file. Because 0 byte resource
2278 * fork streams are not listed by fruit_streaminfo,
2279 * delete_all_streams() can't remove 0 byte resource fork
2280 * streams, so we have to cleanup this here.
2282 rsrc_smb_fname
= synthetic_smb_fname(talloc_tos(),
2283 smb_fname
->base_name
,
2284 AFPRESOURCE_STREAM_NAME
,
2288 if (rsrc_smb_fname
== NULL
) {
2292 ret
= fruit_unlink_rsrc(handle
, dirfsp
, rsrc_smb_fname
, true);
2293 if ((ret
!= 0) && (errno
!= ENOENT
)) {
2294 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2295 smb_fname_str_dbg(rsrc_smb_fname
), strerror(errno
));
2296 TALLOC_FREE(rsrc_smb_fname
);
2299 TALLOC_FREE(rsrc_smb_fname
);
2301 return SMB_VFS_NEXT_UNLINKAT(handle
,
2307 static ssize_t
fruit_pread_meta_stream(vfs_handle_struct
*handle
,
2308 files_struct
*fsp
, void *data
,
2309 size_t n
, off_t offset
)
2311 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2315 if ((fio
== NULL
) || fio
->fake_fd
) {
2319 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2320 if (nread
== -1 || nread
== n
) {
2324 DBG_ERR("Removing [%s] after short read [%zd]\n",
2325 fsp_str_dbg(fsp
), nread
);
2327 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
2332 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp
));
2340 static ssize_t
fruit_pread_meta_adouble(vfs_handle_struct
*handle
,
2341 files_struct
*fsp
, void *data
,
2342 size_t n
, off_t offset
)
2345 struct adouble
*ad
= NULL
;
2346 char afpinfo_buf
[AFP_INFO_SIZE
];
2350 ai
= afpinfo_new(talloc_tos());
2355 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
2361 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2363 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
2368 memcpy(&ai
->afpi_FinderInfo
[0], p
, ADEDLEN_FINDERI
);
2370 nread
= afpinfo_pack(ai
, afpinfo_buf
);
2371 if (nread
!= AFP_INFO_SIZE
) {
2376 memcpy(data
, afpinfo_buf
, n
);
2384 static ssize_t
fruit_pread_meta(vfs_handle_struct
*handle
,
2385 files_struct
*fsp
, void *data
,
2386 size_t n
, off_t offset
)
2388 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2393 * OS X has a off-by-1 error in the offset calculation, so we're
2394 * bug compatible here. It won't hurt, as any relevant real
2395 * world read requests from the AFP_AfpInfo stream will be
2396 * offset=0 n=60. offset is ignored anyway, see below.
2398 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
+ 1)) {
2403 DBG_ERR("Failed to fetch fsp extension\n");
2407 /* Yes, macOS always reads from offset 0 */
2409 to_return
= MIN(n
, AFP_INFO_SIZE
);
2411 switch (fio
->config
->meta
) {
2412 case FRUIT_META_STREAM
:
2413 nread
= fruit_pread_meta_stream(handle
, fsp
, data
,
2417 case FRUIT_META_NETATALK
:
2418 nread
= fruit_pread_meta_adouble(handle
, fsp
, data
,
2423 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
2427 if (nread
== -1 && fio
->fake_fd
) {
2429 char afpinfo_buf
[AFP_INFO_SIZE
];
2431 ai
= afpinfo_new(talloc_tos());
2436 nread
= afpinfo_pack(ai
, afpinfo_buf
);
2438 if (nread
!= AFP_INFO_SIZE
) {
2442 memcpy(data
, afpinfo_buf
, to_return
);
2449 static ssize_t
fruit_pread_rsrc_stream(vfs_handle_struct
*handle
,
2450 files_struct
*fsp
, void *data
,
2451 size_t n
, off_t offset
)
2453 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2456 static ssize_t
fruit_pread_rsrc_xattr(vfs_handle_struct
*handle
,
2457 files_struct
*fsp
, void *data
,
2458 size_t n
, off_t offset
)
2460 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2463 static ssize_t
fruit_pread_rsrc_adouble(vfs_handle_struct
*handle
,
2464 files_struct
*fsp
, void *data
,
2465 size_t n
, off_t offset
)
2467 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2468 struct adouble
*ad
= NULL
;
2471 if (fio
== NULL
|| fio
->ad_fsp
== NULL
) {
2472 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp
));
2477 ad
= ad_fget(talloc_tos(), handle
, fio
->ad_fsp
, ADOUBLE_RSRC
);
2479 DBG_ERR("ad_fget [%s] failed [%s]\n",
2480 fsp_str_dbg(fio
->ad_fsp
), strerror(errno
));
2484 nread
= SMB_VFS_NEXT_PREAD(handle
, fio
->ad_fsp
, data
, n
,
2485 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2491 static ssize_t
fruit_pread_rsrc(vfs_handle_struct
*handle
,
2492 files_struct
*fsp
, void *data
,
2493 size_t n
, off_t offset
)
2495 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2503 switch (fio
->config
->rsrc
) {
2504 case FRUIT_RSRC_STREAM
:
2505 nread
= fruit_pread_rsrc_stream(handle
, fsp
, data
, n
, offset
);
2508 case FRUIT_RSRC_ADFILE
:
2509 nread
= fruit_pread_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
2512 case FRUIT_RSRC_XATTR
:
2513 nread
= fruit_pread_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
2517 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
2524 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
2525 files_struct
*fsp
, void *data
,
2526 size_t n
, off_t offset
)
2528 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2531 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
2532 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
2535 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2538 if (fio
->type
== ADOUBLE_META
) {
2539 nread
= fruit_pread_meta(handle
, fsp
, data
, n
, offset
);
2541 nread
= fruit_pread_rsrc(handle
, fsp
, data
, n
, offset
);
2544 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp
), nread
);
2548 static bool fruit_must_handle_aio_stream(struct fio
*fio
)
2554 if (fio
->type
== ADOUBLE_META
) {
2558 if ((fio
->type
== ADOUBLE_RSRC
) &&
2559 (fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
))
2567 struct fruit_pread_state
{
2569 struct vfs_aio_state vfs_aio_state
;
2572 static void fruit_pread_done(struct tevent_req
*subreq
);
2574 static struct tevent_req
*fruit_pread_send(
2575 struct vfs_handle_struct
*handle
,
2576 TALLOC_CTX
*mem_ctx
,
2577 struct tevent_context
*ev
,
2578 struct files_struct
*fsp
,
2580 size_t n
, off_t offset
)
2582 struct tevent_req
*req
= NULL
;
2583 struct tevent_req
*subreq
= NULL
;
2584 struct fruit_pread_state
*state
= NULL
;
2585 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2587 req
= tevent_req_create(mem_ctx
, &state
,
2588 struct fruit_pread_state
);
2593 if (fruit_must_handle_aio_stream(fio
)) {
2594 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
2595 if (state
->nread
!= n
) {
2596 if (state
->nread
!= -1) {
2599 tevent_req_error(req
, errno
);
2600 return tevent_req_post(req
, ev
);
2602 tevent_req_done(req
);
2603 return tevent_req_post(req
, ev
);
2606 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
2608 if (tevent_req_nomem(req
, subreq
)) {
2609 return tevent_req_post(req
, ev
);
2611 tevent_req_set_callback(subreq
, fruit_pread_done
, req
);
2615 static void fruit_pread_done(struct tevent_req
*subreq
)
2617 struct tevent_req
*req
= tevent_req_callback_data(
2618 subreq
, struct tevent_req
);
2619 struct fruit_pread_state
*state
= tevent_req_data(
2620 req
, struct fruit_pread_state
);
2622 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
2623 TALLOC_FREE(subreq
);
2625 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
2628 tevent_req_done(req
);
2631 static ssize_t
fruit_pread_recv(struct tevent_req
*req
,
2632 struct vfs_aio_state
*vfs_aio_state
)
2634 struct fruit_pread_state
*state
= tevent_req_data(
2635 req
, struct fruit_pread_state
);
2636 ssize_t retval
= -1;
2638 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
2639 tevent_req_received(req
);
2643 *vfs_aio_state
= state
->vfs_aio_state
;
2644 retval
= state
->nread
;
2645 tevent_req_received(req
);
2649 static ssize_t
fruit_pwrite_meta_stream(vfs_handle_struct
*handle
,
2650 files_struct
*fsp
, const void *indata
,
2651 size_t n
, off_t offset
)
2653 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2654 const void *data
= indata
;
2655 char afpinfo_buf
[AFP_INFO_SIZE
];
2661 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
2662 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
2669 struct vfs_open_how how
= {
2670 .flags
= fio
->flags
, .mode
= fio
->mode
,
2672 int fd
= fsp_get_pathref_fd(fsp
);
2674 ret
= vfs_fake_fd_close(fd
);
2675 fsp_set_fd(fsp
, -1);
2677 DBG_ERR("Close [%s] failed: %s\n",
2678 fsp_str_dbg(fsp
), strerror(errno
));
2682 fd
= SMB_VFS_NEXT_OPENAT(handle
,
2683 NULL
, /* opening a stream */
2688 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2689 fsp_str_dbg(fsp
), strerror(errno
));
2692 fsp_set_fd(fsp
, fd
);
2693 fio
->fake_fd
= false;
2696 ai
= afpinfo_unpack(talloc_tos(), data
, fio
->config
->validate_afpinfo
);
2701 if (ai_empty_finderinfo(ai
)) {
2703 * Writing an all 0 blob to the metadata stream results in the
2704 * stream being removed on a macOS server. This ensures we
2705 * behave the same and it verified by the "delete AFP_AfpInfo by
2706 * writing all 0" test.
2708 ret
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, 0);
2710 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2715 ok
= set_delete_on_close(
2718 handle
->conn
->session_info
->security_token
,
2719 handle
->conn
->session_info
->unix_token
);
2721 DBG_ERR("set_delete_on_close on [%s] failed\n",
2728 if (!fio
->config
->validate_afpinfo
) {
2730 * Ensure the buffer contains a valid header, so marshall
2731 * the data from the afpinfo struck back into a buffer
2732 * and write that instead of the possibly malformed data
2733 * we got from the client.
2735 nwritten
= afpinfo_pack(ai
, afpinfo_buf
);
2736 if (nwritten
!= AFP_INFO_SIZE
) {
2743 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2744 if (nwritten
!= n
) {
2751 static ssize_t
fruit_pwrite_meta_netatalk(vfs_handle_struct
*handle
,
2752 files_struct
*fsp
, const void *data
,
2753 size_t n
, off_t offset
)
2755 struct fruit_config_data
*config
= NULL
;
2756 struct adouble
*ad
= NULL
;
2762 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2763 struct fruit_config_data
, return -1);
2765 ai
= afpinfo_unpack(talloc_tos(), data
, config
->validate_afpinfo
);
2770 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
2772 ad
= ad_init(talloc_tos(), ADOUBLE_META
);
2777 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2779 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
2784 memcpy(p
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2786 ret
= ad_fset(handle
, ad
, fsp
);
2788 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
2795 if (!ai_empty_finderinfo(ai
)) {
2800 * Writing an all 0 blob to the metadata stream results in the stream
2801 * being removed on a macOS server. This ensures we behave the same and
2802 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2805 ok
= set_delete_on_close(
2808 handle
->conn
->session_info
->security_token
,
2809 handle
->conn
->session_info
->unix_token
);
2811 DBG_ERR("set_delete_on_close on [%s] failed\n",
2819 static ssize_t
fruit_pwrite_meta(vfs_handle_struct
*handle
,
2820 files_struct
*fsp
, const void *data
,
2821 size_t n
, off_t offset
)
2823 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2825 uint8_t buf
[AFP_INFO_SIZE
];
2831 DBG_ERR("Failed to fetch fsp extension\n");
2840 if (offset
!= 0 && n
< 60) {
2845 if (fio
->config
->validate_afpinfo
) {
2846 cmp
= memcmp(data
, "AFP", 3);
2853 if (n
<= AFP_OFF_FinderInfo
) {
2855 * Nothing to do here really, just return
2863 if (to_copy
> AFP_INFO_SIZE
) {
2864 to_copy
= AFP_INFO_SIZE
;
2866 memcpy(buf
, data
, to_copy
);
2869 if (to_write
!= AFP_INFO_SIZE
) {
2870 to_write
= AFP_INFO_SIZE
;
2873 switch (fio
->config
->meta
) {
2874 case FRUIT_META_STREAM
:
2875 nwritten
= fruit_pwrite_meta_stream(handle
,
2882 case FRUIT_META_NETATALK
:
2883 nwritten
= fruit_pwrite_meta_netatalk(handle
,
2891 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
2895 if (nwritten
!= to_write
) {
2900 * Return the requested amount, verified against macOS SMB server
2905 static ssize_t
fruit_pwrite_rsrc_stream(vfs_handle_struct
*handle
,
2906 files_struct
*fsp
, const void *data
,
2907 size_t n
, off_t offset
)
2909 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2912 static ssize_t
fruit_pwrite_rsrc_xattr(vfs_handle_struct
*handle
,
2913 files_struct
*fsp
, const void *data
,
2914 size_t n
, off_t offset
)
2916 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2919 static ssize_t
fruit_pwrite_rsrc_adouble(vfs_handle_struct
*handle
,
2920 files_struct
*fsp
, const void *data
,
2921 size_t n
, off_t offset
)
2923 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2924 struct adouble
*ad
= NULL
;
2928 if (fio
== NULL
|| fio
->ad_fsp
== NULL
) {
2929 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp
));
2934 ad
= ad_fget(talloc_tos(), handle
, fio
->ad_fsp
, ADOUBLE_RSRC
);
2936 DBG_ERR("ad_fget [%s] failed [%s]\n",
2937 fsp_str_dbg(fio
->ad_fsp
), strerror(errno
));
2941 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fio
->ad_fsp
, data
, n
,
2942 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2943 if (nwritten
!= n
) {
2944 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2945 fsp_str_dbg(fio
->ad_fsp
), nwritten
, n
);
2950 if ((n
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
2951 ad_setentrylen(ad
, ADEID_RFORK
, n
+ offset
);
2952 ret
= ad_fset(handle
, ad
, fio
->ad_fsp
);
2954 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fio
->ad_fsp
));
2964 static ssize_t
fruit_pwrite_rsrc(vfs_handle_struct
*handle
,
2965 files_struct
*fsp
, const void *data
,
2966 size_t n
, off_t offset
)
2968 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
2972 DBG_ERR("Failed to fetch fsp extension\n");
2976 switch (fio
->config
->rsrc
) {
2977 case FRUIT_RSRC_STREAM
:
2978 nwritten
= fruit_pwrite_rsrc_stream(handle
, fsp
, data
, n
, offset
);
2981 case FRUIT_RSRC_ADFILE
:
2982 nwritten
= fruit_pwrite_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
2985 case FRUIT_RSRC_XATTR
:
2986 nwritten
= fruit_pwrite_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
2990 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
2997 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
2998 files_struct
*fsp
, const void *data
,
2999 size_t n
, off_t offset
)
3001 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3004 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
3005 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
3008 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
3011 if (fio
->type
== ADOUBLE_META
) {
3012 nwritten
= fruit_pwrite_meta(handle
, fsp
, data
, n
, offset
);
3014 nwritten
= fruit_pwrite_rsrc(handle
, fsp
, data
, n
, offset
);
3017 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp
), nwritten
);
3021 struct fruit_pwrite_state
{
3023 struct vfs_aio_state vfs_aio_state
;
3026 static void fruit_pwrite_done(struct tevent_req
*subreq
);
3028 static struct tevent_req
*fruit_pwrite_send(
3029 struct vfs_handle_struct
*handle
,
3030 TALLOC_CTX
*mem_ctx
,
3031 struct tevent_context
*ev
,
3032 struct files_struct
*fsp
,
3034 size_t n
, off_t offset
)
3036 struct tevent_req
*req
= NULL
;
3037 struct tevent_req
*subreq
= NULL
;
3038 struct fruit_pwrite_state
*state
= NULL
;
3039 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3041 req
= tevent_req_create(mem_ctx
, &state
,
3042 struct fruit_pwrite_state
);
3047 if (fruit_must_handle_aio_stream(fio
)) {
3048 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
3049 if (state
->nwritten
!= n
) {
3050 if (state
->nwritten
!= -1) {
3053 tevent_req_error(req
, errno
);
3054 return tevent_req_post(req
, ev
);
3056 tevent_req_done(req
);
3057 return tevent_req_post(req
, ev
);
3060 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
3062 if (tevent_req_nomem(req
, subreq
)) {
3063 return tevent_req_post(req
, ev
);
3065 tevent_req_set_callback(subreq
, fruit_pwrite_done
, req
);
3069 static void fruit_pwrite_done(struct tevent_req
*subreq
)
3071 struct tevent_req
*req
= tevent_req_callback_data(
3072 subreq
, struct tevent_req
);
3073 struct fruit_pwrite_state
*state
= tevent_req_data(
3074 req
, struct fruit_pwrite_state
);
3076 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
3077 TALLOC_FREE(subreq
);
3079 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
3082 tevent_req_done(req
);
3085 static ssize_t
fruit_pwrite_recv(struct tevent_req
*req
,
3086 struct vfs_aio_state
*vfs_aio_state
)
3088 struct fruit_pwrite_state
*state
= tevent_req_data(
3089 req
, struct fruit_pwrite_state
);
3090 ssize_t retval
= -1;
3092 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
3093 tevent_req_received(req
);
3097 *vfs_aio_state
= state
->vfs_aio_state
;
3098 retval
= state
->nwritten
;
3099 tevent_req_received(req
);
3103 struct fruit_fsync_state
{
3105 struct vfs_aio_state vfs_aio_state
;
3108 static void fruit_fsync_done(struct tevent_req
*subreq
);
3110 static struct tevent_req
*fruit_fsync_send(
3111 struct vfs_handle_struct
*handle
,
3112 TALLOC_CTX
*mem_ctx
,
3113 struct tevent_context
*ev
,
3114 struct files_struct
*fsp
)
3116 struct tevent_req
*req
= NULL
;
3117 struct tevent_req
*subreq
= NULL
;
3118 struct fruit_fsync_state
*state
= NULL
;
3119 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3121 req
= tevent_req_create(mem_ctx
, &state
,
3122 struct fruit_fsync_state
);
3127 if (fruit_must_handle_aio_stream(fio
)) {
3128 struct adouble
*ad
= NULL
;
3130 if (fio
->type
== ADOUBLE_META
) {
3132 * We must never pass a fake_fd
3133 * to lower level fsync calls.
3134 * Everything is already done
3135 * synchronously, so just return
3138 SMB_ASSERT(fio
->fake_fd
);
3139 tevent_req_done(req
);
3140 return tevent_req_post(req
, ev
);
3144 * We know the following must be true,
3145 * as it's the condition for fruit_must_handle_aio_stream()
3146 * to return true if fio->type == ADOUBLE_RSRC.
3148 SMB_ASSERT(fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
);
3149 if (fio
->ad_fsp
== NULL
) {
3150 tevent_req_error(req
, EBADF
);
3151 return tevent_req_post(req
, ev
);
3153 ad
= ad_fget(talloc_tos(), handle
, fio
->ad_fsp
, ADOUBLE_RSRC
);
3155 tevent_req_error(req
, ENOMEM
);
3156 return tevent_req_post(req
, ev
);
3161 subreq
= SMB_VFS_NEXT_FSYNC_SEND(state
, ev
, handle
, fsp
);
3162 if (tevent_req_nomem(req
, subreq
)) {
3163 return tevent_req_post(req
, ev
);
3165 tevent_req_set_callback(subreq
, fruit_fsync_done
, req
);
3169 static void fruit_fsync_done(struct tevent_req
*subreq
)
3171 struct tevent_req
*req
= tevent_req_callback_data(
3172 subreq
, struct tevent_req
);
3173 struct fruit_fsync_state
*state
= tevent_req_data(
3174 req
, struct fruit_fsync_state
);
3176 state
->ret
= SMB_VFS_FSYNC_RECV(subreq
, &state
->vfs_aio_state
);
3177 TALLOC_FREE(subreq
);
3178 if (state
->ret
!= 0) {
3179 tevent_req_error(req
, errno
);
3182 tevent_req_done(req
);
3185 static int fruit_fsync_recv(struct tevent_req
*req
,
3186 struct vfs_aio_state
*vfs_aio_state
)
3188 struct fruit_fsync_state
*state
= tevent_req_data(
3189 req
, struct fruit_fsync_state
);
3192 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
3193 tevent_req_received(req
);
3197 *vfs_aio_state
= state
->vfs_aio_state
;
3198 retval
= state
->ret
;
3199 tevent_req_received(req
);
3204 * Helper to stat/lstat the base file of an smb_fname.
3206 static int fruit_stat_base(vfs_handle_struct
*handle
,
3207 struct smb_filename
*smb_fname
,
3210 char *tmp_stream_name
;
3213 tmp_stream_name
= smb_fname
->stream_name
;
3214 smb_fname
->stream_name
= NULL
;
3216 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3218 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3220 smb_fname
->stream_name
= tmp_stream_name
;
3222 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
3223 smb_fname
->base_name
,
3224 (uintmax_t)smb_fname
->st
.st_ex_dev
,
3225 (uintmax_t)smb_fname
->st
.st_ex_ino
);
3229 static int fruit_stat_meta_stream(vfs_handle_struct
*handle
,
3230 struct smb_filename
*smb_fname
,
3236 ret
= fruit_stat_base(handle
, smb_fname
, false);
3241 ino
= hash_inode(&smb_fname
->st
, smb_fname
->stream_name
);
3244 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3246 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3249 smb_fname
->st
.st_ex_ino
= ino
;
3254 static int fruit_stat_meta_netatalk(vfs_handle_struct
*handle
,
3255 struct smb_filename
*smb_fname
,
3258 struct adouble
*ad
= NULL
;
3260 /* Populate the stat struct with info from the base file. */
3261 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
3265 ad
= ad_get_meta_fsp(talloc_tos(), handle
, smb_fname
);
3267 DBG_INFO("fruit_stat_meta %s: %s\n",
3268 smb_fname_str_dbg(smb_fname
), strerror(errno
));
3274 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
3275 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3276 smb_fname
->stream_name
);
3280 static int fruit_stat_meta(vfs_handle_struct
*handle
,
3281 struct smb_filename
*smb_fname
,
3284 struct fruit_config_data
*config
= NULL
;
3287 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3288 struct fruit_config_data
, return -1);
3290 switch (config
->meta
) {
3291 case FRUIT_META_STREAM
:
3292 ret
= fruit_stat_meta_stream(handle
, smb_fname
, follow_links
);
3295 case FRUIT_META_NETATALK
:
3296 ret
= fruit_stat_meta_netatalk(handle
, smb_fname
, follow_links
);
3300 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
3307 static int fruit_stat_rsrc_netatalk(vfs_handle_struct
*handle
,
3308 struct smb_filename
*smb_fname
,
3311 struct adouble
*ad
= NULL
;
3314 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
3320 /* Populate the stat struct with info from the base file. */
3321 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
3327 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
3328 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3329 smb_fname
->stream_name
);
3334 static int fruit_stat_rsrc_stream(vfs_handle_struct
*handle
,
3335 struct smb_filename
*smb_fname
,
3341 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3343 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3349 static int fruit_stat_rsrc_xattr(vfs_handle_struct
*handle
,
3350 struct smb_filename
*smb_fname
,
3353 #ifdef HAVE_ATTROPEN
3357 /* Populate the stat struct with info from the base file. */
3358 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
3363 fd
= attropen(smb_fname
->base_name
,
3364 AFPRESOURCE_EA_NETATALK
,
3370 ret
= sys_fstat(fd
, &smb_fname
->st
, false);
3373 DBG_ERR("fstat [%s:%s] failed\n", smb_fname
->base_name
,
3374 AFPRESOURCE_EA_NETATALK
);
3380 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3381 smb_fname
->stream_name
);
3391 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
3392 struct smb_filename
*smb_fname
,
3395 struct fruit_config_data
*config
= NULL
;
3398 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3400 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3401 struct fruit_config_data
, return -1);
3403 switch (config
->rsrc
) {
3404 case FRUIT_RSRC_STREAM
:
3405 ret
= fruit_stat_rsrc_stream(handle
, smb_fname
, follow_links
);
3408 case FRUIT_RSRC_XATTR
:
3409 ret
= fruit_stat_rsrc_xattr(handle
, smb_fname
, follow_links
);
3412 case FRUIT_RSRC_ADFILE
:
3413 ret
= fruit_stat_rsrc_netatalk(handle
, smb_fname
, follow_links
);
3417 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3424 static int fruit_stat(vfs_handle_struct
*handle
,
3425 struct smb_filename
*smb_fname
)
3429 DEBUG(10, ("fruit_stat called for %s\n",
3430 smb_fname_str_dbg(smb_fname
)));
3432 if (!is_named_stream(smb_fname
)) {
3433 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3435 update_btime(handle
, smb_fname
);
3441 * Note if lp_posix_paths() is true, we can never
3442 * get here as is_ntfs_stream_smb_fname() is
3443 * always false. So we never need worry about
3444 * not following links here.
3447 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
3448 rc
= fruit_stat_meta(handle
, smb_fname
, true);
3449 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
3450 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
3452 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3456 update_btime(handle
, smb_fname
);
3457 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
3458 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
3459 smb_fname
->st
.st_ex_blocks
=
3460 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3465 static int fruit_lstat(vfs_handle_struct
*handle
,
3466 struct smb_filename
*smb_fname
)
3470 DEBUG(10, ("fruit_lstat called for %s\n",
3471 smb_fname_str_dbg(smb_fname
)));
3473 if (!is_named_stream(smb_fname
)) {
3474 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3476 update_btime(handle
, smb_fname
);
3481 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
3482 rc
= fruit_stat_meta(handle
, smb_fname
, false);
3483 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
3484 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
3486 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3490 update_btime(handle
, smb_fname
);
3491 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
3492 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
3493 smb_fname
->st
.st_ex_blocks
=
3494 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3499 static int fruit_fstat_meta_stream(vfs_handle_struct
*handle
,
3501 SMB_STRUCT_STAT
*sbuf
)
3503 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3504 struct smb_filename smb_fname
;
3513 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3518 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3519 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
3520 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3524 smb_fname
= (struct smb_filename
) {
3525 .base_name
= fsp
->fsp_name
->base_name
,
3526 .twrp
= fsp
->fsp_name
->twrp
,
3529 ret
= fruit_stat_base(handle
, &smb_fname
, false);
3533 *sbuf
= smb_fname
.st
;
3535 ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3537 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3542 sbuf
->st_ex_ino
= ino
;
3546 static int fruit_fstat_meta_netatalk(vfs_handle_struct
*handle
,
3548 SMB_STRUCT_STAT
*sbuf
)
3552 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3557 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3558 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
3559 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3564 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
3566 SMB_STRUCT_STAT
*sbuf
,
3571 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
3573 switch (fio
->config
->meta
) {
3574 case FRUIT_META_STREAM
:
3575 ret
= fruit_fstat_meta_stream(handle
, fsp
, sbuf
);
3578 case FRUIT_META_NETATALK
:
3579 ret
= fruit_fstat_meta_netatalk(handle
, fsp
, sbuf
);
3583 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
3587 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp
), ret
);
3591 static int fruit_fstat_rsrc_xattr(vfs_handle_struct
*handle
,
3593 SMB_STRUCT_STAT
*sbuf
)
3595 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3598 static int fruit_fstat_rsrc_stream(vfs_handle_struct
*handle
,
3600 SMB_STRUCT_STAT
*sbuf
)
3602 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3605 static int fruit_fstat_rsrc_adouble(vfs_handle_struct
*handle
,
3607 SMB_STRUCT_STAT
*sbuf
)
3609 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3610 struct adouble
*ad
= NULL
;
3613 if (fio
== NULL
|| fio
->ad_fsp
== NULL
) {
3614 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp
));
3619 /* Populate the stat struct with info from the base file. */
3620 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3625 ad
= ad_fget(talloc_tos(), handle
, fio
->ad_fsp
, ADOUBLE_RSRC
);
3627 DBG_ERR("ad_fget [%s] failed [%s]\n",
3628 fsp_str_dbg(fio
->ad_fsp
), strerror(errno
));
3632 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3633 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
3634 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3640 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
3641 SMB_STRUCT_STAT
*sbuf
, struct fio
*fio
)
3645 switch (fio
->config
->rsrc
) {
3646 case FRUIT_RSRC_STREAM
:
3647 ret
= fruit_fstat_rsrc_stream(handle
, fsp
, sbuf
);
3650 case FRUIT_RSRC_ADFILE
:
3651 ret
= fruit_fstat_rsrc_adouble(handle
, fsp
, sbuf
);
3654 case FRUIT_RSRC_XATTR
:
3655 ret
= fruit_fstat_rsrc_xattr(handle
, fsp
, sbuf
);
3659 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
3666 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
3667 SMB_STRUCT_STAT
*sbuf
)
3669 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
3673 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3676 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
3678 if (fio
->type
== ADOUBLE_META
) {
3679 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
, fio
);
3681 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
, fio
);
3685 sbuf
->st_ex_mode
&= ~S_IFMT
;
3686 sbuf
->st_ex_mode
|= S_IFREG
;
3687 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3690 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX
"]\n",
3691 fsp_str_dbg(fsp
), rc
, (intmax_t)sbuf
->st_ex_size
);
3695 static NTSTATUS
delete_invalid_meta_stream(
3696 vfs_handle_struct
*handle
,
3697 const struct smb_filename
*smb_fname
,
3698 TALLOC_CTX
*mem_ctx
,
3699 unsigned int *pnum_streams
,
3700 struct stream_struct
**pstreams
,
3703 struct smb_filename
*sname
= NULL
;
3708 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
, AFPINFO_STREAM
);
3710 return NT_STATUS_INTERNAL_ERROR
;
3714 return NT_STATUS_OK
;
3717 status
= synthetic_pathref(talloc_tos(),
3718 handle
->conn
->cwd_fsp
,
3719 smb_fname
->base_name
,
3720 AFPINFO_STREAM_NAME
,
3725 if (!NT_STATUS_IS_OK(status
)) {
3726 return NT_STATUS_NO_MEMORY
;
3729 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
3730 handle
->conn
->cwd_fsp
,
3734 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname
));
3736 return map_nt_error_from_unix(errno
);
3740 return NT_STATUS_OK
;
3743 static NTSTATUS
fruit_streaminfo_meta_stream(
3744 vfs_handle_struct
*handle
,
3745 struct files_struct
*fsp
,
3746 const struct smb_filename
*smb_fname
,
3747 TALLOC_CTX
*mem_ctx
,
3748 unsigned int *pnum_streams
,
3749 struct stream_struct
**pstreams
)
3751 struct stream_struct
*stream
= *pstreams
;
3752 unsigned int num_streams
= *pnum_streams
;
3755 for (i
= 0; i
< num_streams
; i
++) {
3756 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
3761 if (i
== num_streams
) {
3762 return NT_STATUS_OK
;
3765 if (stream
[i
].size
!= AFP_INFO_SIZE
) {
3766 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3767 (intmax_t)stream
[i
].size
, smb_fname_str_dbg(smb_fname
));
3769 return delete_invalid_meta_stream(handle
,
3778 return NT_STATUS_OK
;
3781 static NTSTATUS
fruit_streaminfo_meta_netatalk(
3782 vfs_handle_struct
*handle
,
3783 struct files_struct
*fsp
,
3784 const struct smb_filename
*smb_fname
,
3785 TALLOC_CTX
*mem_ctx
,
3786 unsigned int *pnum_streams
,
3787 struct stream_struct
**pstreams
)
3789 struct stream_struct
*stream
= *pstreams
;
3790 unsigned int num_streams
= *pnum_streams
;
3791 struct adouble
*ad
= NULL
;
3796 /* Remove the Netatalk xattr from the list */
3797 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3798 ":" NETATALK_META_XATTR
":$DATA");
3800 return NT_STATUS_NO_MEMORY
;
3804 * Check if there's a AFPINFO_STREAM from the VFS streams
3805 * backend and if yes, remove it from the list
3807 for (i
= 0; i
< num_streams
; i
++) {
3808 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
3813 if (i
< num_streams
) {
3814 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3815 smb_fname_str_dbg(smb_fname
));
3817 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3820 return NT_STATUS_INTERNAL_ERROR
;
3824 ad
= ad_get_meta_fsp(talloc_tos(), handle
, smb_fname
);
3826 return NT_STATUS_OK
;
3829 is_fi_empty
= ad_empty_finderinfo(ad
);
3833 return NT_STATUS_OK
;
3836 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3837 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
3838 smb_roundup(handle
->conn
, AFP_INFO_SIZE
));
3840 return NT_STATUS_NO_MEMORY
;
3843 return NT_STATUS_OK
;
3846 static NTSTATUS
fruit_streaminfo_meta(vfs_handle_struct
*handle
,
3847 struct files_struct
*fsp
,
3848 const struct smb_filename
*smb_fname
,
3849 TALLOC_CTX
*mem_ctx
,
3850 unsigned int *pnum_streams
,
3851 struct stream_struct
**pstreams
)
3853 struct fruit_config_data
*config
= NULL
;
3856 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3857 return NT_STATUS_INTERNAL_ERROR
);
3859 switch (config
->meta
) {
3860 case FRUIT_META_NETATALK
:
3861 status
= fruit_streaminfo_meta_netatalk(handle
, fsp
, smb_fname
,
3862 mem_ctx
, pnum_streams
,
3866 case FRUIT_META_STREAM
:
3867 status
= fruit_streaminfo_meta_stream(handle
, fsp
, smb_fname
,
3868 mem_ctx
, pnum_streams
,
3873 return NT_STATUS_INTERNAL_ERROR
;
3879 static NTSTATUS
fruit_streaminfo_rsrc_stream(
3880 vfs_handle_struct
*handle
,
3881 struct files_struct
*fsp
,
3882 const struct smb_filename
*smb_fname
,
3883 TALLOC_CTX
*mem_ctx
,
3884 unsigned int *pnum_streams
,
3885 struct stream_struct
**pstreams
)
3889 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
3891 DBG_ERR("Filtering resource stream failed\n");
3892 return NT_STATUS_INTERNAL_ERROR
;
3894 return NT_STATUS_OK
;
3897 static NTSTATUS
fruit_streaminfo_rsrc_xattr(
3898 vfs_handle_struct
*handle
,
3899 struct files_struct
*fsp
,
3900 const struct smb_filename
*smb_fname
,
3901 TALLOC_CTX
*mem_ctx
,
3902 unsigned int *pnum_streams
,
3903 struct stream_struct
**pstreams
)
3907 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
3909 DBG_ERR("Filtering resource stream failed\n");
3910 return NT_STATUS_INTERNAL_ERROR
;
3912 return NT_STATUS_OK
;
3915 static NTSTATUS
fruit_streaminfo_rsrc_adouble(
3916 vfs_handle_struct
*handle
,
3917 struct files_struct
*fsp
,
3918 const struct smb_filename
*smb_fname
,
3919 TALLOC_CTX
*mem_ctx
,
3920 unsigned int *pnum_streams
,
3921 struct stream_struct
**pstreams
)
3923 struct stream_struct
*stream
= *pstreams
;
3924 unsigned int num_streams
= *pnum_streams
;
3925 struct adouble
*ad
= NULL
;
3931 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3932 * and if yes, remove it from the list
3934 for (i
= 0; i
< num_streams
; i
++) {
3935 if (strequal_m(stream
[i
].name
, AFPRESOURCE_STREAM
)) {
3940 if (i
< num_streams
) {
3941 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3942 smb_fname_str_dbg(smb_fname
));
3944 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3945 AFPRESOURCE_STREAM
);
3947 return NT_STATUS_INTERNAL_ERROR
;
3951 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
3953 return NT_STATUS_OK
;
3956 rlen
= ad_getentrylen(ad
, ADEID_RFORK
);
3960 return NT_STATUS_OK
;
3963 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3964 AFPRESOURCE_STREAM_NAME
, rlen
,
3965 smb_roundup(handle
->conn
, rlen
));
3967 return NT_STATUS_NO_MEMORY
;
3970 return NT_STATUS_OK
;
3973 static NTSTATUS
fruit_streaminfo_rsrc(vfs_handle_struct
*handle
,
3974 struct files_struct
*fsp
,
3975 const struct smb_filename
*smb_fname
,
3976 TALLOC_CTX
*mem_ctx
,
3977 unsigned int *pnum_streams
,
3978 struct stream_struct
**pstreams
)
3980 struct fruit_config_data
*config
= NULL
;
3983 if (S_ISDIR(smb_fname
->st
.st_ex_mode
)) {
3984 return NT_STATUS_OK
;
3987 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3988 return NT_STATUS_INTERNAL_ERROR
);
3990 switch (config
->rsrc
) {
3991 case FRUIT_RSRC_STREAM
:
3992 status
= fruit_streaminfo_rsrc_stream(handle
, fsp
, smb_fname
,
3993 mem_ctx
, pnum_streams
,
3997 case FRUIT_RSRC_XATTR
:
3998 status
= fruit_streaminfo_rsrc_xattr(handle
, fsp
, smb_fname
,
3999 mem_ctx
, pnum_streams
,
4003 case FRUIT_RSRC_ADFILE
:
4004 status
= fruit_streaminfo_rsrc_adouble(handle
, fsp
, smb_fname
,
4005 mem_ctx
, pnum_streams
,
4010 return NT_STATUS_INTERNAL_ERROR
;
4016 static void fruit_filter_empty_streams(unsigned int *pnum_streams
,
4017 struct stream_struct
**pstreams
)
4019 unsigned num_streams
= *pnum_streams
;
4020 struct stream_struct
*streams
= *pstreams
;
4023 if (!global_fruit_config
.nego_aapl
) {
4027 while (i
< num_streams
) {
4028 struct smb_filename smb_fname
= (struct smb_filename
) {
4029 .stream_name
= streams
[i
].name
,
4032 if (is_ntfs_default_stream_smb_fname(&smb_fname
)
4033 || streams
[i
].size
> 0)
4039 streams
[i
] = streams
[num_streams
- 1];
4043 *pnum_streams
= num_streams
;
4046 static NTSTATUS
fruit_fstreaminfo(vfs_handle_struct
*handle
,
4047 struct files_struct
*fsp
,
4048 TALLOC_CTX
*mem_ctx
,
4049 unsigned int *pnum_streams
,
4050 struct stream_struct
**pstreams
)
4052 struct fruit_config_data
*config
= NULL
;
4053 const struct smb_filename
*smb_fname
= NULL
;
4056 smb_fname
= fsp
->fsp_name
;
4058 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
4059 return NT_STATUS_UNSUCCESSFUL
);
4061 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
4063 status
= SMB_VFS_NEXT_FSTREAMINFO(handle
, fsp
, mem_ctx
,
4064 pnum_streams
, pstreams
);
4065 if (!NT_STATUS_IS_OK(status
)) {
4069 fruit_filter_empty_streams(pnum_streams
, pstreams
);
4071 status
= fruit_streaminfo_meta(handle
, fsp
, smb_fname
,
4072 mem_ctx
, pnum_streams
, pstreams
);
4073 if (!NT_STATUS_IS_OK(status
)) {
4077 status
= fruit_streaminfo_rsrc(handle
, fsp
, smb_fname
,
4078 mem_ctx
, pnum_streams
, pstreams
);
4079 if (!NT_STATUS_IS_OK(status
)) {
4083 return NT_STATUS_OK
;
4086 static int fruit_fntimes(vfs_handle_struct
*handle
,
4088 struct smb_file_time
*ft
)
4091 struct adouble
*ad
= NULL
;
4092 struct fruit_config_data
*config
= NULL
;
4094 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
4097 if ((config
->meta
!= FRUIT_META_NETATALK
) ||
4098 is_omit_timespec(&ft
->create_time
))
4100 return SMB_VFS_NEXT_FNTIMES(handle
, fsp
, ft
);
4103 DBG_DEBUG("set btime for %s to %s", fsp_str_dbg(fsp
),
4104 time_to_asc(convert_timespec_to_time_t(ft
->create_time
)));
4106 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
4111 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
4112 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
4114 rc
= ad_fset(handle
, ad
, fsp
);
4120 DBG_WARNING("%s\n", fsp_str_dbg(fsp
));
4123 return SMB_VFS_NEXT_FNTIMES(handle
, fsp
, ft
);
4126 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
4127 struct files_struct
*fsp
,
4132 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
4135 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
4138 /* Let the pwrite code path handle it. */
4143 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct
*handle
,
4144 struct files_struct
*fsp
,
4147 #ifdef HAVE_ATTROPEN
4148 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
4153 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct
*handle
,
4154 struct files_struct
*fsp
,
4157 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
4159 struct adouble
*ad
= NULL
;
4162 if (fio
== NULL
|| fio
->ad_fsp
== NULL
) {
4163 DBG_ERR("fio/ad_fsp=NULL for [%s]\n", fsp_str_dbg(fsp
));
4168 ad
= ad_fget(talloc_tos(), handle
, fio
->ad_fsp
, ADOUBLE_RSRC
);
4170 DBG_ERR("ad_fget [%s] failed [%s]\n",
4171 fsp_str_dbg(fio
->ad_fsp
), strerror(errno
));
4175 ad_off
= ad_getentryoff(ad
, ADEID_RFORK
);
4177 rc
= SMB_VFS_NEXT_FTRUNCATE(handle
, fio
->ad_fsp
, offset
+ ad_off
);
4183 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
4185 rc
= ad_fset(handle
, ad
, fio
->ad_fsp
);
4187 DBG_ERR("ad_fset [%s] failed [%s]\n",
4188 fsp_str_dbg(fio
->ad_fsp
), strerror(errno
));
4197 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct
*handle
,
4198 struct files_struct
*fsp
,
4201 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
4204 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
4205 struct files_struct
*fsp
,
4208 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
4212 DBG_ERR("Failed to fetch fsp extension\n");
4216 switch (fio
->config
->rsrc
) {
4217 case FRUIT_RSRC_XATTR
:
4218 ret
= fruit_ftruncate_rsrc_xattr(handle
, fsp
, offset
);
4221 case FRUIT_RSRC_ADFILE
:
4222 ret
= fruit_ftruncate_rsrc_adouble(handle
, fsp
, offset
);
4225 case FRUIT_RSRC_STREAM
:
4226 ret
= fruit_ftruncate_rsrc_stream(handle
, fsp
, offset
);
4230 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
4238 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
4239 struct files_struct
*fsp
,
4243 DBG_WARNING("ftruncate %s to %jd\n",
4244 fsp_str_dbg(fsp
), (intmax_t)offset
);
4245 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
4250 /* OS X returns success but does nothing */
4251 DBG_INFO("ignoring ftruncate %s to %jd\n",
4252 fsp_str_dbg(fsp
), (intmax_t)offset
);
4256 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
4257 struct files_struct
*fsp
,
4260 struct fio
*fio
= fruit_get_complete_fio(handle
, fsp
);
4263 DBG_DEBUG("Path [%s] offset [%"PRIdMAX
"]\n", fsp_str_dbg(fsp
),
4267 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
4270 if (fio
->type
== ADOUBLE_META
) {
4271 ret
= fruit_ftruncate_meta(handle
, fsp
, offset
);
4273 ret
= fruit_ftruncate_rsrc(handle
, fsp
, offset
);
4276 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp
), ret
);
4280 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
4281 struct smb_request
*req
,
4282 struct files_struct
*dirfsp
,
4283 struct smb_filename
*smb_fname
,
4284 uint32_t access_mask
,
4285 uint32_t share_access
,
4286 uint32_t create_disposition
,
4287 uint32_t create_options
,
4288 uint32_t file_attributes
,
4289 uint32_t oplock_request
,
4290 const struct smb2_lease
*lease
,
4291 uint64_t allocation_size
,
4292 uint32_t private_flags
,
4293 struct security_descriptor
*sd
,
4294 struct ea_list
*ea_list
,
4295 files_struct
**result
,
4297 const struct smb2_create_blobs
*in_context_blobs
,
4298 struct smb2_create_blobs
*out_context_blobs
)
4301 struct fruit_config_data
*config
= NULL
;
4302 files_struct
*fsp
= NULL
;
4303 bool internal_open
= (oplock_request
& INTERNAL_OPEN_ONLY
);
4306 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
4307 if (!NT_STATUS_IS_OK(status
)) {
4311 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
4312 return NT_STATUS_UNSUCCESSFUL
);
4314 if (is_apple_stream(smb_fname
->stream_name
) &&
4316 config
->convert_adouble
)
4318 uint32_t conv_flags
= 0;
4320 if (config
->wipe_intentionally_left_blank_rfork
) {
4321 conv_flags
|= AD_CONV_WIPE_BLANK
;
4323 if (config
->delete_empty_adfiles
) {
4324 conv_flags
|= AD_CONV_DELETE
;
4327 ret
= ad_convert(handle
,
4329 macos_string_replace_map
,
4332 DBG_ERR("ad_convert(\"%s\") failed\n",
4333 smb_fname_str_dbg(smb_fname
));
4337 status
= SMB_VFS_NEXT_CREATE_FILE(
4338 handle
, req
, dirfsp
, smb_fname
,
4339 access_mask
, share_access
,
4340 create_disposition
, create_options
,
4341 file_attributes
, oplock_request
,
4343 allocation_size
, private_flags
,
4344 sd
, ea_list
, result
,
4345 pinfo
, in_context_blobs
, out_context_blobs
);
4346 if (!NT_STATUS_IS_OK(status
)) {
4352 if (global_fruit_config
.nego_aapl
) {
4353 if (config
->posix_rename
&& fsp
->fsp_flags
.is_directory
) {
4355 * Enable POSIX directory rename behaviour
4357 fsp
->posix_flags
|= FSP_POSIX_FLAGS_RENAME
;
4362 * If this is a plain open for existing files, opening an 0
4363 * byte size resource fork MUST fail with
4364 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4366 * Cf the vfs_fruit torture tests in test_rfork_create().
4368 if (global_fruit_config
.nego_aapl
&&
4369 create_disposition
== FILE_OPEN
&&
4370 smb_fname
->st
.st_ex_size
== 0 &&
4371 is_named_stream(smb_fname
))
4373 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4377 if (is_named_stream(smb_fname
) || fsp
->fsp_flags
.is_directory
) {
4381 if ((config
->locking
== FRUIT_LOCKING_NETATALK
) &&
4382 (fsp
->op
!= NULL
) &&
4383 !fsp
->fsp_flags
.is_pathref
)
4385 status
= fruit_check_access(
4389 if (!NT_STATUS_IS_OK(status
)) {
4397 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
4400 close_file_free(req
, &fsp
, ERROR_CLOSE
);
4407 static NTSTATUS
fruit_freaddir_attr(struct vfs_handle_struct
*handle
,
4408 struct files_struct
*fsp
,
4409 TALLOC_CTX
*mem_ctx
,
4410 struct readdir_attr_data
**pattr_data
)
4412 struct fruit_config_data
*config
= NULL
;
4413 struct readdir_attr_data
*attr_data
;
4414 uint32_t conv_flags
= 0;
4418 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4419 struct fruit_config_data
,
4420 return NT_STATUS_UNSUCCESSFUL
);
4422 if (!global_fruit_config
.nego_aapl
) {
4423 return SMB_VFS_NEXT_FREADDIR_ATTR(handle
,
4429 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
4431 if (config
->convert_adouble
) {
4432 if (config
->wipe_intentionally_left_blank_rfork
) {
4433 conv_flags
|= AD_CONV_WIPE_BLANK
;
4435 if (config
->delete_empty_adfiles
) {
4436 conv_flags
|= AD_CONV_DELETE
;
4439 ret
= ad_convert(handle
,
4441 macos_string_replace_map
,
4444 DBG_ERR("ad_convert(\"%s\") failed\n",
4449 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
4450 if (*pattr_data
== NULL
) {
4451 return NT_STATUS_NO_MEMORY
;
4453 attr_data
= *pattr_data
;
4454 attr_data
->type
= RDATTR_AAPL
;
4457 * Mac metadata: compressed FinderInfo, resource fork length
4460 status
= readdir_attr_macmeta(handle
, fsp
->fsp_name
, attr_data
);
4461 if (!NT_STATUS_IS_OK(status
)) {
4463 * Error handling is tricky: if we return failure from
4464 * this function, the corresponding directory entry
4465 * will to be passed to the client, so we really just
4466 * want to error out on fatal errors.
4468 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
4476 if (config
->unix_info_enabled
) {
4477 attr_data
->attr_data
.aapl
.unix_mode
=
4478 fsp
->fsp_name
->st
.st_ex_mode
;
4484 if (!config
->readdir_attr_max_access
) {
4485 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
4487 status
= smbd_calculate_access_mask_fsp(fsp
->conn
->cwd_fsp
,
4490 SEC_FLAG_MAXIMUM_ALLOWED
,
4491 &attr_data
->attr_data
.aapl
.max_access
);
4492 if (!NT_STATUS_IS_OK(status
)) {
4497 return NT_STATUS_OK
;
4500 DBG_WARNING("Path [%s], error: %s\n", fsp_str_dbg(fsp
),
4502 TALLOC_FREE(*pattr_data
);
4506 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
4508 uint32_t security_info
,
4509 TALLOC_CTX
*mem_ctx
,
4510 struct security_descriptor
**ppdesc
)
4513 struct security_ace ace
;
4515 struct fruit_config_data
*config
;
4517 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4518 struct fruit_config_data
,
4519 return NT_STATUS_UNSUCCESSFUL
);
4521 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
4523 if (!NT_STATUS_IS_OK(status
)) {
4528 * Add MS NFS style ACEs with uid, gid and mode
4530 if (!global_fruit_config
.nego_aapl
) {
4531 return NT_STATUS_OK
;
4533 if (!config
->unix_info_enabled
) {
4534 return NT_STATUS_OK
;
4537 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4538 status
= remove_virtual_nfs_aces(*ppdesc
);
4539 if (!NT_STATUS_IS_OK(status
)) {
4540 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4544 /* MS NFS style mode */
4545 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
4546 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4547 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4548 if (!NT_STATUS_IS_OK(status
)) {
4549 DEBUG(1,("failed to add MS NFS style ACE\n"));
4553 /* MS NFS style uid */
4554 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
4555 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4556 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4557 if (!NT_STATUS_IS_OK(status
)) {
4558 DEBUG(1,("failed to add MS NFS style ACE\n"));
4562 /* MS NFS style gid */
4563 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
4564 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4565 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4566 if (!NT_STATUS_IS_OK(status
)) {
4567 DEBUG(1,("failed to add MS NFS style ACE\n"));
4571 return NT_STATUS_OK
;
4574 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
4576 uint32_t security_info_sent
,
4577 const struct security_descriptor
*orig_psd
)
4581 mode_t ms_nfs_mode
= 0;
4583 struct security_descriptor
*psd
= NULL
;
4584 uint32_t orig_num_aces
= 0;
4586 if (orig_psd
->dacl
!= NULL
) {
4587 orig_num_aces
= orig_psd
->dacl
->num_aces
;
4590 psd
= security_descriptor_copy(talloc_tos(), orig_psd
);
4592 return NT_STATUS_NO_MEMORY
;
4595 DBG_DEBUG("%s\n", fsp_str_dbg(fsp
));
4597 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
4598 if (!NT_STATUS_IS_OK(status
)) {
4599 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
4605 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4606 * sent/present flags correctly now we've removed them.
4609 if (orig_num_aces
!= 0) {
4611 * Are there any ACE's left ?
4613 if (psd
->dacl
->num_aces
== 0) {
4614 /* No - clear the DACL sent/present flags. */
4615 security_info_sent
&= ~SECINFO_DACL
;
4616 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
4620 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
4621 if (!NT_STATUS_IS_OK(status
)) {
4622 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
4628 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
4630 DBG_WARNING("%s, result: %d, %04o error %s\n",
4633 (unsigned)ms_nfs_mode
,
4635 status
= map_nt_error_from_unix(errno
);
4642 return NT_STATUS_OK
;
4645 static struct vfs_offload_ctx
*fruit_offload_ctx
;
4647 struct fruit_offload_read_state
{
4648 struct vfs_handle_struct
*handle
;
4649 struct tevent_context
*ev
;
4657 static void fruit_offload_read_done(struct tevent_req
*subreq
);
4659 static struct tevent_req
*fruit_offload_read_send(
4660 TALLOC_CTX
*mem_ctx
,
4661 struct tevent_context
*ev
,
4662 struct vfs_handle_struct
*handle
,
4669 struct tevent_req
*req
= NULL
;
4670 struct tevent_req
*subreq
= NULL
;
4671 struct fruit_offload_read_state
*state
= NULL
;
4673 req
= tevent_req_create(mem_ctx
, &state
,
4674 struct fruit_offload_read_state
);
4678 *state
= (struct fruit_offload_read_state
) {
4685 subreq
= SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx
, ev
, handle
, fsp
,
4686 fsctl
, ttl
, offset
, to_copy
);
4687 if (tevent_req_nomem(subreq
, req
)) {
4688 return tevent_req_post(req
, ev
);
4690 tevent_req_set_callback(subreq
, fruit_offload_read_done
, req
);
4694 static void fruit_offload_read_done(struct tevent_req
*subreq
)
4696 struct tevent_req
*req
= tevent_req_callback_data(
4697 subreq
, struct tevent_req
);
4698 struct fruit_offload_read_state
*state
= tevent_req_data(
4699 req
, struct fruit_offload_read_state
);
4702 status
= SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq
,
4708 TALLOC_FREE(subreq
);
4709 if (tevent_req_nterror(req
, status
)) {
4713 if (state
->fsctl
!= FSCTL_SRV_REQUEST_RESUME_KEY
) {
4714 tevent_req_done(req
);
4718 status
= vfs_offload_token_ctx_init(state
->fsp
->conn
->sconn
->client
,
4719 &fruit_offload_ctx
);
4720 if (tevent_req_nterror(req
, status
)) {
4724 status
= vfs_offload_token_db_store_fsp(fruit_offload_ctx
,
4727 if (tevent_req_nterror(req
, status
)) {
4731 tevent_req_done(req
);
4735 static NTSTATUS
fruit_offload_read_recv(struct tevent_req
*req
,
4736 struct vfs_handle_struct
*handle
,
4737 TALLOC_CTX
*mem_ctx
,
4742 struct fruit_offload_read_state
*state
= tevent_req_data(
4743 req
, struct fruit_offload_read_state
);
4746 if (tevent_req_is_nterror(req
, &status
)) {
4747 tevent_req_received(req
);
4751 *flags
= state
->flags
;
4752 *xferlen
= state
->xferlen
;
4753 token
->length
= state
->token
.length
;
4754 token
->data
= talloc_move(mem_ctx
, &state
->token
.data
);
4756 tevent_req_received(req
);
4757 return NT_STATUS_OK
;
4760 struct fruit_offload_write_state
{
4761 struct vfs_handle_struct
*handle
;
4763 struct files_struct
*src_fsp
;
4764 struct files_struct
*dst_fsp
;
4768 static void fruit_offload_write_done(struct tevent_req
*subreq
);
4769 static struct tevent_req
*fruit_offload_write_send(struct vfs_handle_struct
*handle
,
4770 TALLOC_CTX
*mem_ctx
,
4771 struct tevent_context
*ev
,
4774 off_t transfer_offset
,
4775 struct files_struct
*dest_fsp
,
4779 struct tevent_req
*req
, *subreq
;
4780 struct fruit_offload_write_state
*state
;
4782 struct fruit_config_data
*config
;
4783 off_t src_off
= transfer_offset
;
4784 files_struct
*src_fsp
= NULL
;
4785 off_t to_copy
= num
;
4786 bool copyfile_enabled
= false;
4788 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4789 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
4791 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4792 struct fruit_config_data
,
4795 req
= tevent_req_create(mem_ctx
, &state
,
4796 struct fruit_offload_write_state
);
4800 state
->handle
= handle
;
4801 state
->dst_fsp
= dest_fsp
;
4804 case FSCTL_SRV_COPYCHUNK
:
4805 case FSCTL_SRV_COPYCHUNK_WRITE
:
4806 copyfile_enabled
= config
->copyfile_enabled
;
4813 * Check if this a OS X copyfile style copychunk request with
4814 * a requested chunk count of 0 that was translated to a
4815 * offload_write_send VFS call overloading the parameters src_off
4816 * = dest_off = num = 0.
4818 if (copyfile_enabled
&& num
== 0 && src_off
== 0 && dest_off
== 0) {
4819 status
= vfs_offload_token_db_fetch_fsp(
4820 fruit_offload_ctx
, token
, &src_fsp
);
4821 if (tevent_req_nterror(req
, status
)) {
4822 return tevent_req_post(req
, ev
);
4824 state
->src_fsp
= src_fsp
;
4826 status
= vfs_stat_fsp(src_fsp
);
4827 if (tevent_req_nterror(req
, status
)) {
4828 return tevent_req_post(req
, ev
);
4831 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
4832 state
->is_copyfile
= true;
4835 subreq
= SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle
,
4844 if (tevent_req_nomem(subreq
, req
)) {
4845 return tevent_req_post(req
, ev
);
4848 tevent_req_set_callback(subreq
, fruit_offload_write_done
, req
);
4852 static void fruit_offload_write_done(struct tevent_req
*subreq
)
4854 struct tevent_req
*req
= tevent_req_callback_data(
4855 subreq
, struct tevent_req
);
4856 struct fruit_offload_write_state
*state
= tevent_req_data(
4857 req
, struct fruit_offload_write_state
);
4859 unsigned int num_streams
= 0;
4860 struct stream_struct
*streams
= NULL
;
4862 struct smb_filename
*src_fname_tmp
= NULL
;
4863 struct smb_filename
*dst_fname_tmp
= NULL
;
4865 status
= SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state
->handle
,
4868 TALLOC_FREE(subreq
);
4869 if (tevent_req_nterror(req
, status
)) {
4873 if (!state
->is_copyfile
) {
4874 tevent_req_done(req
);
4879 * Now copy all remaining streams. We know the share supports
4880 * streams, because we're in vfs_fruit. We don't do this async
4881 * because streams are few and small.
4883 status
= vfs_fstreaminfo(state
->src_fsp
,
4884 req
, &num_streams
, &streams
);
4885 if (tevent_req_nterror(req
, status
)) {
4889 if (num_streams
== 1) {
4890 /* There is always one stream, ::$DATA. */
4891 tevent_req_done(req
);
4895 for (i
= 0; i
< num_streams
; i
++) {
4896 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4897 __func__
, streams
[i
].name
, (size_t)streams
[i
].size
));
4899 src_fname_tmp
= synthetic_smb_fname(
4901 state
->src_fsp
->fsp_name
->base_name
,
4904 state
->src_fsp
->fsp_name
->twrp
,
4905 state
->src_fsp
->fsp_name
->flags
);
4906 if (tevent_req_nomem(src_fname_tmp
, req
)) {
4910 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
4911 TALLOC_FREE(src_fname_tmp
);
4915 dst_fname_tmp
= synthetic_smb_fname(
4917 state
->dst_fsp
->fsp_name
->base_name
,
4920 state
->dst_fsp
->fsp_name
->twrp
,
4921 state
->dst_fsp
->fsp_name
->flags
);
4922 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
4923 TALLOC_FREE(src_fname_tmp
);
4927 status
= copy_file(req
,
4928 state
->handle
->conn
,
4932 if (!NT_STATUS_IS_OK(status
)) {
4933 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
4934 smb_fname_str_dbg(src_fname_tmp
),
4935 smb_fname_str_dbg(dst_fname_tmp
),
4936 nt_errstr(status
)));
4937 TALLOC_FREE(src_fname_tmp
);
4938 TALLOC_FREE(dst_fname_tmp
);
4939 tevent_req_nterror(req
, status
);
4943 TALLOC_FREE(src_fname_tmp
);
4944 TALLOC_FREE(dst_fname_tmp
);
4947 TALLOC_FREE(streams
);
4948 TALLOC_FREE(src_fname_tmp
);
4949 TALLOC_FREE(dst_fname_tmp
);
4950 tevent_req_done(req
);
4953 static NTSTATUS
fruit_offload_write_recv(struct vfs_handle_struct
*handle
,
4954 struct tevent_req
*req
,
4957 struct fruit_offload_write_state
*state
= tevent_req_data(
4958 req
, struct fruit_offload_write_state
);
4961 if (tevent_req_is_nterror(req
, &status
)) {
4962 DEBUG(1, ("server side copy chunk failed: %s\n",
4963 nt_errstr(status
)));
4965 tevent_req_received(req
);
4969 *copied
= state
->copied
;
4970 tevent_req_received(req
);
4972 return NT_STATUS_OK
;
4975 static char *fruit_get_bandsize_line(char **lines
, int numlines
)
4978 static bool re_initialized
= false;
4982 if (!re_initialized
) {
4983 ret
= regcomp(&re
, "^[[:blank:]]*<key>band-size</key>$", 0);
4987 re_initialized
= true;
4990 for (i
= 0; i
< numlines
; i
++) {
4991 regmatch_t matches
[1];
4993 ret
= regexec(&re
, lines
[i
], 1, matches
, 0);
4996 * Check if the match was on the last line, sa we want
4997 * the subsequent line.
4999 if (i
+ 1 == numlines
) {
5002 return lines
[i
+ 1];
5004 if (ret
!= REG_NOMATCH
) {
5012 static bool fruit_get_bandsize_from_line(char *line
, size_t *_band_size
)
5015 static bool re_initialized
= false;
5016 regmatch_t matches
[2];
5021 if (!re_initialized
) {
5024 "<integer>\\([[:digit:]]*\\)</integer>$",
5029 re_initialized
= true;
5032 ret
= regexec(&re
, line
, 2, matches
, 0);
5034 DBG_ERR("regex failed [%s]\n", line
);
5038 line
[matches
[1].rm_eo
] = '\0';
5040 ok
= conv_str_u64(&line
[matches
[1].rm_so
], &band_size
);
5044 *_band_size
= (size_t)band_size
;
5049 * This reads and parses an Info.plist from a TM sparsebundle looking for the
5050 * "band-size" key and value.
5052 static bool fruit_get_bandsize(vfs_handle_struct
*handle
,
5056 #define INFO_PLIST_MAX_SIZE 64*1024
5058 struct smb_filename
*smb_fname
= NULL
;
5059 files_struct
*fsp
= NULL
;
5060 uint8_t *file_data
= NULL
;
5061 char **lines
= NULL
;
5062 char *band_size_line
= NULL
;
5063 size_t plist_file_size
;
5070 plist
= talloc_asprintf(talloc_tos(),
5072 handle
->conn
->connectpath
,
5074 if (plist
== NULL
) {
5079 smb_fname
= synthetic_smb_fname(talloc_tos(),
5085 if (smb_fname
== NULL
) {
5090 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
5092 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir
);
5097 plist_file_size
= smb_fname
->st
.st_ex_size
;
5099 if (plist_file_size
> INFO_PLIST_MAX_SIZE
) {
5100 DBG_INFO("%s is too large, ignoring\n", plist
);
5105 status
= SMB_VFS_NEXT_CREATE_FILE(
5109 smb_fname
, /* fname */
5110 FILE_GENERIC_READ
, /* access_mask */
5111 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
5112 FILE_OPEN
, /* create_disposition */
5113 0, /* create_options */
5114 0, /* file_attributes */
5115 INTERNAL_OPEN_ONLY
, /* oplock_request */
5117 0, /* allocation_size */
5118 0, /* private_flags */
5123 NULL
, NULL
); /* create context */
5124 if (!NT_STATUS_IS_OK(status
)) {
5125 DBG_INFO("Opening [%s] failed [%s]\n",
5126 smb_fname_str_dbg(smb_fname
), nt_errstr(status
));
5131 file_data
= talloc_zero_array(talloc_tos(),
5133 plist_file_size
+ 1);
5134 if (file_data
== NULL
) {
5139 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, file_data
, plist_file_size
, 0);
5140 if (nread
!= plist_file_size
) {
5141 DBG_ERR("Short read on [%s]: %zu/%zd\n",
5142 fsp_str_dbg(fsp
), nread
, plist_file_size
);
5148 status
= close_file_free(NULL
, &fsp
, NORMAL_CLOSE
);
5149 if (!NT_STATUS_IS_OK(status
)) {
5150 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
5155 lines
= file_lines_parse((char *)file_data
,
5159 if (lines
== NULL
) {
5164 band_size_line
= fruit_get_bandsize_line(lines
, numlines
);
5165 if (band_size_line
== NULL
) {
5166 DBG_ERR("Didn't find band-size key in [%s]\n",
5167 smb_fname_str_dbg(smb_fname
));
5172 ok
= fruit_get_bandsize_from_line(band_size_line
, band_size
);
5174 DBG_ERR("fruit_get_bandsize_from_line failed\n");
5178 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size
, plist
);
5182 status
= close_file_free(NULL
, &fsp
, NORMAL_CLOSE
);
5183 if (!NT_STATUS_IS_OK(status
)) {
5184 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
5188 TALLOC_FREE(smb_fname
);
5189 TALLOC_FREE(file_data
);
5194 struct fruit_disk_free_state
{
5198 static bool fruit_get_num_bands(vfs_handle_struct
*handle
,
5203 struct smb_filename
*bands_dir
= NULL
;
5204 struct smb_Dir
*dir_hnd
= NULL
;
5205 const char *dname
= NULL
;
5206 char *talloced
= NULL
;
5210 path
= talloc_asprintf(talloc_tos(),
5212 handle
->conn
->connectpath
,
5218 bands_dir
= synthetic_smb_fname(talloc_tos(),
5225 if (bands_dir
== NULL
) {
5229 status
= OpenDir(talloc_tos(),
5235 if (!NT_STATUS_IS_OK(status
)) {
5236 TALLOC_FREE(bands_dir
);
5237 errno
= map_errno_from_nt_status(status
);
5243 while ((dname
= ReadDirName(dir_hnd
, &talloced
)) != NULL
) {
5244 if (ISDOT(dname
) || ISDOTDOT(dname
)) {
5249 TALLOC_FREE(dir_hnd
);
5251 DBG_DEBUG("%zu bands in [%s]\n", nbands
, smb_fname_str_dbg(bands_dir
));
5253 TALLOC_FREE(bands_dir
);
5259 static bool fruit_tmsize_do_dirent(vfs_handle_struct
*handle
,
5260 struct fruit_disk_free_state
*state
,
5265 size_t sparsebundle_strlen
= strlen("sparsebundle");
5266 size_t bandsize
= 0;
5270 p
= strstr(name
, "sparsebundle");
5275 if (p
[sparsebundle_strlen
] != '\0') {
5279 DBG_DEBUG("Processing sparsebundle [%s]\n", name
);
5281 ok
= fruit_get_bandsize(handle
, name
, &bandsize
);
5284 * Beware of race conditions: this may be an uninitialized
5285 * Info.plist that a client is just creating. We don't want let
5286 * this to trigger complete failure.
5288 DBG_ERR("Processing sparsebundle [%s] failed\n", name
);
5292 ok
= fruit_get_num_bands(handle
, name
, &nbands
);
5295 * Beware of race conditions: this may be a backup sparsebundle
5296 * in an early stage lacking a bands subdirectory. We don't want
5297 * let this to trigger complete failure.
5299 DBG_ERR("Processing sparsebundle [%s] failed\n", name
);
5304 * Arithmetic on 32-bit systems may cause overflow, depending on
5305 * size_t precision. First we check its unlikely, then we
5306 * force the precision into target off_t, then we check that
5307 * the total did not overflow either.
5309 if (bandsize
> SIZE_MAX
/nbands
) {
5310 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5314 tm_size
= (off_t
)bandsize
* (off_t
)nbands
;
5316 if (state
->total_size
+ tm_size
< state
->total_size
) {
5317 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5322 state
->total_size
+= tm_size
;
5324 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5325 name
, (intmax_t)tm_size
, (intmax_t)state
->total_size
);
5331 * Calculate used size of a TimeMachine volume
5333 * This assumes that the volume is used only for TimeMachine.
5335 * - readdir(basedir of share), then
5336 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5337 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5338 * - count band files in "\1.sparsebundle/bands/"
5339 * - calculate used size of all bands: band_count * band_size
5341 static uint64_t fruit_disk_free(vfs_handle_struct
*handle
,
5342 const struct smb_filename
*smb_fname
,
5347 struct fruit_config_data
*config
= NULL
;
5348 struct fruit_disk_free_state state
= {0};
5349 struct smb_Dir
*dir_hnd
= NULL
;
5350 const char *dname
= NULL
;
5351 char *talloced
= NULL
;
5357 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5358 struct fruit_config_data
,
5361 if (!config
->time_machine
||
5362 config
->time_machine_max_size
== 0)
5364 return SMB_VFS_NEXT_DISK_FREE(handle
,
5371 status
= OpenDir(talloc_tos(),
5377 if (!NT_STATUS_IS_OK(status
)) {
5378 errno
= map_errno_from_nt_status(status
);
5382 while ((dname
= ReadDirName(dir_hnd
, &talloced
)) != NULL
) {
5383 ok
= fruit_tmsize_do_dirent(handle
, &state
, dname
);
5385 TALLOC_FREE(talloced
);
5386 TALLOC_FREE(dir_hnd
);
5389 TALLOC_FREE(talloced
);
5392 TALLOC_FREE(dir_hnd
);
5394 dsize
= config
->time_machine_max_size
/ 512;
5395 dfree
= dsize
- (state
.total_size
/ 512);
5396 if (dfree
> dsize
) {
5406 static uint64_t fruit_fs_file_id(struct vfs_handle_struct
*handle
,
5407 const SMB_STRUCT_STAT
*psbuf
)
5409 struct fruit_config_data
*config
= NULL
;
5411 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5412 struct fruit_config_data
,
5415 if (global_fruit_config
.nego_aapl
&&
5416 config
->aapl_zero_file_id
)
5421 return SMB_VFS_NEXT_FS_FILE_ID(handle
, psbuf
);
5424 static struct vfs_fn_pointers vfs_fruit_fns
= {
5425 .connect_fn
= fruit_connect
,
5426 .disk_free_fn
= fruit_disk_free
,
5428 /* File operations */
5429 .fchmod_fn
= fruit_fchmod
,
5430 .unlinkat_fn
= fruit_unlinkat
,
5431 .renameat_fn
= fruit_renameat
,
5432 .openat_fn
= fruit_openat
,
5433 .close_fn
= fruit_close
,
5434 .pread_fn
= fruit_pread
,
5435 .pwrite_fn
= fruit_pwrite
,
5436 .pread_send_fn
= fruit_pread_send
,
5437 .pread_recv_fn
= fruit_pread_recv
,
5438 .pwrite_send_fn
= fruit_pwrite_send
,
5439 .pwrite_recv_fn
= fruit_pwrite_recv
,
5440 .fsync_send_fn
= fruit_fsync_send
,
5441 .fsync_recv_fn
= fruit_fsync_recv
,
5442 .stat_fn
= fruit_stat
,
5443 .lstat_fn
= fruit_lstat
,
5444 .fstat_fn
= fruit_fstat
,
5445 .fstreaminfo_fn
= fruit_fstreaminfo
,
5446 .fntimes_fn
= fruit_fntimes
,
5447 .ftruncate_fn
= fruit_ftruncate
,
5448 .fallocate_fn
= fruit_fallocate
,
5449 .create_file_fn
= fruit_create_file
,
5450 .freaddir_attr_fn
= fruit_freaddir_attr
,
5451 .offload_read_send_fn
= fruit_offload_read_send
,
5452 .offload_read_recv_fn
= fruit_offload_read_recv
,
5453 .offload_write_send_fn
= fruit_offload_write_send
,
5454 .offload_write_recv_fn
= fruit_offload_write_recv
,
5455 .fs_file_id_fn
= fruit_fs_file_id
,
5457 /* NT ACL operations */
5458 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
5459 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
5463 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*ctx
)
5465 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
5467 if (!NT_STATUS_IS_OK(ret
)) {
5471 vfs_fruit_debug_level
= debug_add_class("fruit");
5472 if (vfs_fruit_debug_level
== -1) {
5473 vfs_fruit_debug_level
= DBGC_VFS
;
5474 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5477 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5478 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));