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 "offload_token.h"
34 #include "string_replace.h"
35 #include "hash_inode.h"
36 #include "lib/adouble.h"
37 #include "lib/util_macstreams.h"
40 * Enhanced OS X and Netatalk compatibility
41 * ========================================
43 * This modules takes advantage of vfs_streams_xattr and
44 * vfs_catia. VFS modules vfs_fruit and vfs_streams_xattr must be
45 * loaded in the correct order:
47 * vfs modules = catia fruit streams_xattr
49 * The module intercepts the OS X special streams "AFP_AfpInfo" and
50 * "AFP_Resource" and handles them in a special way. All other named
51 * streams are deferred to vfs_streams_xattr.
53 * The OS X client maps all NTFS illegal characters to the Unicode
54 * private range. This module optionally stores the characters using
55 * their native ASCII encoding using vfs_catia. If you're not enabling
56 * this feature, you can skip catia from vfs modules.
58 * Finally, open modes are optionally checked against Netatalk AFP
61 * The "AFP_AfpInfo" named stream is a binary blob containing OS X
62 * extended metadata for files and directories. This module optionally
63 * reads and stores this metadata in a way compatible with Netatalk 3
64 * which stores the metadata in an EA "org.netatalk.metadata". Cf
65 * source3/include/MacExtensions.h for a description of the binary
68 * The "AFP_Resource" named stream may be arbitrarily large, thus it
69 * can't be stored in an xattr on most filesystem. ZFS on Solaris is
70 * the only available filesystem where xattrs can be of any size and
71 * the OS supports using the file APIs for xattrs.
73 * The AFP_Resource stream is stored in an AppleDouble file prepending
74 * "._" to the filename. On Solaris with ZFS the stream is optionally
75 * stored in an EA "org.netatalk.resource".
81 * The OS X SMB client sends xattrs as ADS too. For xattr interop with
82 * other protocols you may want to adjust the xattr names the VFS
83 * module vfs_streams_xattr uses for storing ADS's. This defaults to
84 * user.DosStream.ADS_NAME:$DATA and can be changed by specifying
85 * these module parameters:
87 * streams_xattr:prefix = user.
88 * streams_xattr:store_stream_type = false
94 * - log diagnostic if any needed VFS module is not loaded
95 * (eg with lp_vfs_objects())
99 static int vfs_fruit_debug_level
= DBGC_VFS
;
101 static struct global_fruit_config
{
102 bool nego_aapl
; /* client negotiated AAPL */
104 } global_fruit_config
;
107 #define DBGC_CLASS vfs_fruit_debug_level
109 #define FRUIT_PARAM_TYPE_NAME "fruit"
111 enum apple_fork
{APPLE_FORK_DATA
, APPLE_FORK_RSRC
};
113 enum fruit_rsrc
{FRUIT_RSRC_STREAM
, FRUIT_RSRC_ADFILE
, FRUIT_RSRC_XATTR
};
114 enum fruit_meta
{FRUIT_META_STREAM
, FRUIT_META_NETATALK
};
115 enum fruit_locking
{FRUIT_LOCKING_NETATALK
, FRUIT_LOCKING_NONE
};
116 enum fruit_encoding
{FRUIT_ENC_NATIVE
, FRUIT_ENC_PRIVATE
};
118 struct fruit_config_data
{
119 enum fruit_rsrc rsrc
;
120 enum fruit_meta meta
;
121 enum fruit_locking locking
;
122 enum fruit_encoding encoding
;
123 bool use_aapl
; /* config from smb.conf */
125 bool readdir_attr_enabled
;
126 bool unix_info_enabled
;
127 bool copyfile_enabled
;
128 bool veto_appledouble
;
130 bool aapl_zero_file_id
;
133 off_t time_machine_max_size
;
134 bool wipe_intentionally_left_blank_rfork
;
135 bool delete_empty_adfiles
;
138 * Additional options, all enabled by default,
139 * possibly useful for analyzing performance. The associated
140 * operations with each of them may be expensive, so having
141 * the chance to disable them individually gives a chance
142 * tweaking the setup for the particular usecase.
144 bool readdir_attr_rsize
;
145 bool readdir_attr_finder_info
;
146 bool readdir_attr_max_access
;
149 static const struct enum_list fruit_rsrc
[] = {
150 {FRUIT_RSRC_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
151 {FRUIT_RSRC_ADFILE
, "file"}, /* ._ AppleDouble file */
152 {FRUIT_RSRC_XATTR
, "xattr"}, /* Netatalk compatible xattr (ZFS only) */
156 static const struct enum_list fruit_meta
[] = {
157 {FRUIT_META_STREAM
, "stream"}, /* pass on to vfs_streams_xattr */
158 {FRUIT_META_NETATALK
, "netatalk"}, /* Netatalk compatible xattr */
162 static const struct enum_list fruit_locking
[] = {
163 {FRUIT_LOCKING_NETATALK
, "netatalk"}, /* synchronize locks with Netatalk */
164 {FRUIT_LOCKING_NONE
, "none"},
168 static const struct enum_list fruit_encoding
[] = {
169 {FRUIT_ENC_NATIVE
, "native"}, /* map unicode private chars to ASCII */
170 {FRUIT_ENC_PRIVATE
, "private"}, /* keep unicode private chars */
175 /* tcon config handle */
176 struct fruit_config_data
*config
;
178 /* Denote stream type, meta or rsrc */
182 * AFP_AfpInfo stream created, but not written yet, thus still a fake
183 * pipe fd. This is set to true in fruit_open_meta if there was no
184 * existing stream but the caller requested O_CREAT. It is later set to
185 * false when we get a write on the stream that then does open and
193 /*****************************************************************************
195 *****************************************************************************/
198 * Initialize config struct from our smb.conf config parameters
200 static int init_fruit_config(vfs_handle_struct
*handle
)
202 struct fruit_config_data
*config
;
204 const char *tm_size_str
= NULL
;
206 config
= talloc_zero(handle
->conn
, struct fruit_config_data
);
208 DEBUG(1, ("talloc_zero() failed\n"));
214 * Versions up to Samba 4.5.x had a spelling bug in the
215 * fruit:resource option calling lp_parm_enum with
216 * "res*s*ource" (ie two s).
218 * In Samba 4.6 we accept both the wrong and the correct
219 * spelling, in Samba 4.7 the bad spelling will be removed.
221 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
222 "ressource", fruit_rsrc
, FRUIT_RSRC_ADFILE
);
224 DEBUG(1, ("value for %s: resource type unknown\n",
225 FRUIT_PARAM_TYPE_NAME
));
228 config
->rsrc
= (enum fruit_rsrc
)enumval
;
230 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
231 "resource", fruit_rsrc
, enumval
);
233 DEBUG(1, ("value for %s: resource type unknown\n",
234 FRUIT_PARAM_TYPE_NAME
));
237 config
->rsrc
= (enum fruit_rsrc
)enumval
;
239 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
240 "metadata", fruit_meta
, FRUIT_META_NETATALK
);
242 DEBUG(1, ("value for %s: metadata type unknown\n",
243 FRUIT_PARAM_TYPE_NAME
));
246 config
->meta
= (enum fruit_meta
)enumval
;
248 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
249 "locking", fruit_locking
, FRUIT_LOCKING_NONE
);
251 DEBUG(1, ("value for %s: locking type unknown\n",
252 FRUIT_PARAM_TYPE_NAME
));
255 config
->locking
= (enum fruit_locking
)enumval
;
257 enumval
= lp_parm_enum(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
258 "encoding", fruit_encoding
, FRUIT_ENC_PRIVATE
);
260 DEBUG(1, ("value for %s: encoding type unknown\n",
261 FRUIT_PARAM_TYPE_NAME
));
264 config
->encoding
= (enum fruit_encoding
)enumval
;
266 if (config
->rsrc
== FRUIT_RSRC_ADFILE
) {
267 config
->veto_appledouble
= lp_parm_bool(SNUM(handle
->conn
),
268 FRUIT_PARAM_TYPE_NAME
,
273 config
->use_aapl
= lp_parm_bool(
274 -1, FRUIT_PARAM_TYPE_NAME
, "aapl", true);
276 config
->time_machine
= lp_parm_bool(
277 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "time machine", false);
279 config
->unix_info_enabled
= lp_parm_bool(
280 -1, FRUIT_PARAM_TYPE_NAME
, "nfs_aces", true);
282 config
->use_copyfile
= lp_parm_bool(-1, FRUIT_PARAM_TYPE_NAME
,
285 config
->posix_rename
= lp_parm_bool(
286 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
, "posix_rename", true);
288 config
->aapl_zero_file_id
=
289 lp_parm_bool(SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
290 "zero_file_id", false);
292 config
->readdir_attr_rsize
= lp_parm_bool(
293 SNUM(handle
->conn
), "readdir_attr", "aapl_rsize", true);
295 config
->readdir_attr_finder_info
= lp_parm_bool(
296 SNUM(handle
->conn
), "readdir_attr", "aapl_finder_info", true);
298 config
->readdir_attr_max_access
= lp_parm_bool(
299 SNUM(handle
->conn
), "readdir_attr", "aapl_max_access", true);
301 config
->model
= lp_parm_const_string(
302 -1, FRUIT_PARAM_TYPE_NAME
, "model", "MacSamba");
304 tm_size_str
= lp_parm_const_string(
305 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
306 "time machine max size", NULL
);
307 if (tm_size_str
!= NULL
) {
308 config
->time_machine_max_size
= conv_str_size(tm_size_str
);
311 config
->wipe_intentionally_left_blank_rfork
= lp_parm_bool(
312 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
313 "wipe_intentionally_left_blank_rfork", false);
315 config
->delete_empty_adfiles
= lp_parm_bool(
316 SNUM(handle
->conn
), FRUIT_PARAM_TYPE_NAME
,
317 "delete_empty_adfiles", false);
319 SMB_VFS_HANDLE_SET_DATA(handle
, config
,
320 NULL
, struct fruit_config_data
,
326 static bool add_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
327 struct stream_struct
**streams
,
328 const char *name
, off_t size
,
331 struct stream_struct
*tmp
;
333 tmp
= talloc_realloc(mem_ctx
, *streams
, struct stream_struct
,
339 tmp
[*num_streams
].name
= talloc_asprintf(tmp
, "%s:$DATA", name
);
340 if (tmp
[*num_streams
].name
== NULL
) {
344 tmp
[*num_streams
].size
= size
;
345 tmp
[*num_streams
].alloc_size
= alloc_size
;
352 static bool filter_empty_rsrc_stream(unsigned int *num_streams
,
353 struct stream_struct
**streams
)
355 struct stream_struct
*tmp
= *streams
;
358 if (*num_streams
== 0) {
362 for (i
= 0; i
< *num_streams
; i
++) {
363 if (strequal_m(tmp
[i
].name
, AFPRESOURCE_STREAM
)) {
368 if (i
== *num_streams
) {
372 if (tmp
[i
].size
> 0) {
376 TALLOC_FREE(tmp
[i
].name
);
377 if (*num_streams
- 1 > i
) {
378 memmove(&tmp
[i
], &tmp
[i
+1],
379 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
386 static bool del_fruit_stream(TALLOC_CTX
*mem_ctx
, unsigned int *num_streams
,
387 struct stream_struct
**streams
,
390 struct stream_struct
*tmp
= *streams
;
393 if (*num_streams
== 0) {
397 for (i
= 0; i
< *num_streams
; i
++) {
398 if (strequal_m(tmp
[i
].name
, name
)) {
403 if (i
== *num_streams
) {
407 TALLOC_FREE(tmp
[i
].name
);
408 if (*num_streams
- 1 > i
) {
409 memmove(&tmp
[i
], &tmp
[i
+1],
410 (*num_streams
- i
- 1) * sizeof(struct stream_struct
));
417 static bool ad_empty_finderinfo(const struct adouble
*ad
)
420 char emptybuf
[ADEDLEN_FINDERI
] = {0};
423 fi
= ad_get_entry(ad
, ADEID_FINDERI
);
425 DBG_ERR("Missing FinderInfo in struct adouble [%p]\n", ad
);
429 cmp
= memcmp(emptybuf
, fi
, ADEDLEN_FINDERI
);
433 static bool ai_empty_finderinfo(const AfpInfo
*ai
)
436 char emptybuf
[ADEDLEN_FINDERI
] = {0};
438 cmp
= memcmp(emptybuf
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
443 * Update btime with btime from Netatalk
445 static void update_btime(vfs_handle_struct
*handle
,
446 struct smb_filename
*smb_fname
)
449 struct timespec creation_time
= {0};
451 struct fruit_config_data
*config
= NULL
;
453 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
456 switch (config
->meta
) {
457 case FRUIT_META_STREAM
:
459 case FRUIT_META_NETATALK
:
463 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
467 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
471 if (ad_getdate(ad
, AD_DATE_UNIX
| AD_DATE_CREATE
, &t
) != 0) {
477 creation_time
.tv_sec
= convert_uint32_t_to_time_t(t
);
478 update_stat_ex_create_time(&smb_fname
->st
, creation_time
);
484 * Map an access mask to a Netatalk single byte byte range lock
486 static off_t
access_to_netatalk_brl(enum apple_fork fork_type
,
487 uint32_t access_mask
)
491 switch (access_mask
) {
493 offset
= AD_FILELOCK_OPEN_RD
;
496 case FILE_WRITE_DATA
:
497 case FILE_APPEND_DATA
:
498 offset
= AD_FILELOCK_OPEN_WR
;
502 offset
= AD_FILELOCK_OPEN_NONE
;
506 if (fork_type
== APPLE_FORK_RSRC
) {
507 if (offset
== AD_FILELOCK_OPEN_NONE
) {
508 offset
= AD_FILELOCK_RSRC_OPEN_NONE
;
518 * Map a deny mode to a Netatalk brl
520 static off_t
denymode_to_netatalk_brl(enum apple_fork fork_type
,
527 offset
= AD_FILELOCK_DENY_RD
;
531 offset
= AD_FILELOCK_DENY_WR
;
535 smb_panic("denymode_to_netatalk_brl: bad deny mode\n");
538 if (fork_type
== APPLE_FORK_RSRC
) {
546 * Call fcntl() with an exclusive F_GETLK request in order to
547 * determine if there's an existing shared lock
549 * @return true if the requested lock was found or any error occurred
550 * false if the lock was not found
552 static bool test_netatalk_lock(files_struct
*fsp
, off_t in_offset
)
555 off_t offset
= in_offset
;
560 result
= SMB_VFS_GETLOCK(fsp
, &offset
, &len
, &type
, &pid
);
561 if (result
== false) {
565 if (type
!= F_UNLCK
) {
572 static NTSTATUS
fruit_check_access(vfs_handle_struct
*handle
,
574 uint32_t access_mask
,
577 NTSTATUS status
= NT_STATUS_OK
;
579 bool share_for_read
= (share_mode
& FILE_SHARE_READ
);
580 bool share_for_write
= (share_mode
& FILE_SHARE_WRITE
);
581 bool netatalk_already_open_for_reading
= false;
582 bool netatalk_already_open_for_writing
= false;
583 bool netatalk_already_open_with_deny_read
= false;
584 bool netatalk_already_open_with_deny_write
= false;
585 struct GUID req_guid
= GUID_random();
587 /* FIXME: hardcoded data fork, add resource fork */
588 enum apple_fork fork_type
= APPLE_FORK_DATA
;
590 DBG_DEBUG("fruit_check_access: %s, am: %s/%s, sm: 0x%x\n",
592 access_mask
& FILE_READ_DATA
? "READ" :"-",
593 access_mask
& FILE_WRITE_DATA
? "WRITE" : "-",
596 if (fsp
->fh
->fd
== -1) {
600 /* Read NetATalk opens and deny modes on the file. */
601 netatalk_already_open_for_reading
= test_netatalk_lock(fsp
,
602 access_to_netatalk_brl(fork_type
,
605 netatalk_already_open_with_deny_read
= test_netatalk_lock(fsp
,
606 denymode_to_netatalk_brl(fork_type
,
609 netatalk_already_open_for_writing
= test_netatalk_lock(fsp
,
610 access_to_netatalk_brl(fork_type
,
613 netatalk_already_open_with_deny_write
= test_netatalk_lock(fsp
,
614 denymode_to_netatalk_brl(fork_type
,
617 /* If there are any conflicts - sharing violation. */
618 if ((access_mask
& FILE_READ_DATA
) &&
619 netatalk_already_open_with_deny_read
) {
620 return NT_STATUS_SHARING_VIOLATION
;
623 if (!share_for_read
&&
624 netatalk_already_open_for_reading
) {
625 return NT_STATUS_SHARING_VIOLATION
;
628 if ((access_mask
& FILE_WRITE_DATA
) &&
629 netatalk_already_open_with_deny_write
) {
630 return NT_STATUS_SHARING_VIOLATION
;
633 if (!share_for_write
&&
634 netatalk_already_open_for_writing
) {
635 return NT_STATUS_SHARING_VIOLATION
;
638 if (!(access_mask
& FILE_READ_DATA
)) {
640 * Nothing we can do here, we need read access
646 /* Set NetAtalk locks matching our access */
647 if (access_mask
& FILE_READ_DATA
) {
648 off
= access_to_netatalk_brl(fork_type
, FILE_READ_DATA
);
649 req_guid
.time_hi_and_version
= __LINE__
;
654 fsp
->op
->global
->open_persistent_id
,
662 if (!NT_STATUS_IS_OK(status
)) {
667 if (!share_for_read
) {
668 off
= denymode_to_netatalk_brl(fork_type
, DENY_READ
);
669 req_guid
.time_hi_and_version
= __LINE__
;
674 fsp
->op
->global
->open_persistent_id
,
682 if (!NT_STATUS_IS_OK(status
)) {
687 if (access_mask
& FILE_WRITE_DATA
) {
688 off
= access_to_netatalk_brl(fork_type
, FILE_WRITE_DATA
);
689 req_guid
.time_hi_and_version
= __LINE__
;
694 fsp
->op
->global
->open_persistent_id
,
702 if (!NT_STATUS_IS_OK(status
)) {
707 if (!share_for_write
) {
708 off
= denymode_to_netatalk_brl(fork_type
, DENY_WRITE
);
709 req_guid
.time_hi_and_version
= __LINE__
;
714 fsp
->op
->global
->open_persistent_id
,
722 if (!NT_STATUS_IS_OK(status
)) {
730 static NTSTATUS
check_aapl(vfs_handle_struct
*handle
,
731 struct smb_request
*req
,
732 const struct smb2_create_blobs
*in_context_blobs
,
733 struct smb2_create_blobs
*out_context_blobs
)
735 struct fruit_config_data
*config
;
737 struct smb2_create_blob
*aapl
= NULL
;
741 DATA_BLOB blob
= data_blob_talloc(req
, NULL
, 0);
742 uint64_t req_bitmap
, client_caps
;
743 uint64_t server_caps
= SMB2_CRTCTX_AAPL_UNIX_BASED
;
747 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
748 return NT_STATUS_UNSUCCESSFUL
);
750 if (!config
->use_aapl
751 || in_context_blobs
== NULL
752 || out_context_blobs
== NULL
) {
756 aapl
= smb2_create_blob_find(in_context_blobs
,
757 SMB2_CREATE_TAG_AAPL
);
762 if (aapl
->data
.length
!= 24) {
763 DEBUG(1, ("unexpected AAPL ctxt length: %ju\n",
764 (uintmax_t)aapl
->data
.length
));
765 return NT_STATUS_INVALID_PARAMETER
;
768 cmd
= IVAL(aapl
->data
.data
, 0);
769 if (cmd
!= SMB2_CRTCTX_AAPL_SERVER_QUERY
) {
770 DEBUG(1, ("unsupported AAPL cmd: %d\n", cmd
));
771 return NT_STATUS_INVALID_PARAMETER
;
774 req_bitmap
= BVAL(aapl
->data
.data
, 8);
775 client_caps
= BVAL(aapl
->data
.data
, 16);
777 SIVAL(p
, 0, SMB2_CRTCTX_AAPL_SERVER_QUERY
);
779 SBVAL(p
, 8, req_bitmap
);
780 ok
= data_blob_append(req
, &blob
, p
, 16);
782 return NT_STATUS_UNSUCCESSFUL
;
785 if (req_bitmap
& SMB2_CRTCTX_AAPL_SERVER_CAPS
) {
786 if ((client_caps
& SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
) &&
787 (handle
->conn
->tcon
->compat
->fs_capabilities
& FILE_NAMED_STREAMS
)) {
788 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_READ_DIR_ATTR
;
789 config
->readdir_attr_enabled
= true;
792 if (config
->use_copyfile
) {
793 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_OSX_COPYFILE
;
794 config
->copyfile_enabled
= true;
798 * The client doesn't set the flag, so we can't check
799 * for it and just set it unconditionally
801 if (config
->unix_info_enabled
) {
802 server_caps
|= SMB2_CRTCTX_AAPL_SUPPORTS_NFS_ACE
;
805 SBVAL(p
, 0, server_caps
);
806 ok
= data_blob_append(req
, &blob
, p
, 8);
808 return NT_STATUS_UNSUCCESSFUL
;
812 if (req_bitmap
& SMB2_CRTCTX_AAPL_VOLUME_CAPS
) {
813 int val
= lp_case_sensitive(SNUM(handle
->conn
->tcon
->compat
));
821 caps
|= SMB2_CRTCTX_AAPL_CASE_SENSITIVE
;
828 if (config
->time_machine
) {
829 caps
|= SMB2_CRTCTX_AAPL_FULL_SYNC
;
834 ok
= data_blob_append(req
, &blob
, p
, 8);
836 return NT_STATUS_UNSUCCESSFUL
;
840 if (req_bitmap
& SMB2_CRTCTX_AAPL_MODEL_INFO
) {
841 ok
= convert_string_talloc(req
,
843 config
->model
, strlen(config
->model
),
846 return NT_STATUS_UNSUCCESSFUL
;
850 SIVAL(p
+ 4, 0, modellen
);
851 ok
= data_blob_append(req
, &blob
, p
, 8);
854 return NT_STATUS_UNSUCCESSFUL
;
857 ok
= data_blob_append(req
, &blob
, model
, modellen
);
860 return NT_STATUS_UNSUCCESSFUL
;
864 status
= smb2_create_blob_add(out_context_blobs
,
866 SMB2_CREATE_TAG_AAPL
,
868 if (NT_STATUS_IS_OK(status
)) {
869 global_fruit_config
.nego_aapl
= true;
875 static bool readdir_attr_meta_finderi_stream(
876 struct vfs_handle_struct
*handle
,
877 const struct smb_filename
*smb_fname
,
880 struct smb_filename
*stream_name
= NULL
;
881 files_struct
*fsp
= NULL
;
886 uint8_t buf
[AFP_INFO_SIZE
];
888 stream_name
= synthetic_smb_fname(talloc_tos(),
889 smb_fname
->base_name
,
891 NULL
, smb_fname
->flags
);
892 if (stream_name
== NULL
) {
896 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
901 status
= SMB_VFS_CREATE_FILE(
902 handle
->conn
, /* conn */
904 0, /* root_dir_fid */
905 stream_name
, /* fname */
906 FILE_READ_DATA
, /* access_mask */
907 (FILE_SHARE_READ
| FILE_SHARE_WRITE
| /* share_access */
909 FILE_OPEN
, /* create_disposition*/
910 0, /* create_options */
911 0, /* file_attributes */
912 INTERNAL_OPEN_ONLY
, /* oplock_request */
914 0, /* allocation_size */
915 0, /* private_flags */
920 NULL
, NULL
); /* create context */
922 TALLOC_FREE(stream_name
);
924 if (!NT_STATUS_IS_OK(status
)) {
928 nread
= SMB_VFS_PREAD(fsp
, &buf
[0], AFP_INFO_SIZE
, 0);
929 if (nread
!= AFP_INFO_SIZE
) {
930 DBG_ERR("short read [%s] [%zd/%d]\n",
931 smb_fname_str_dbg(stream_name
), nread
, AFP_INFO_SIZE
);
936 memcpy(&ai
->afpi_FinderInfo
[0], &buf
[AFP_OFF_FinderInfo
],
943 close_file(NULL
, fsp
, NORMAL_CLOSE
);
949 static bool readdir_attr_meta_finderi_netatalk(
950 struct vfs_handle_struct
*handle
,
951 const struct smb_filename
*smb_fname
,
954 struct adouble
*ad
= NULL
;
957 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
962 p
= ad_get_entry(ad
, ADEID_FINDERI
);
964 DBG_ERR("No ADEID_FINDERI for [%s]\n", smb_fname
->base_name
);
969 memcpy(&ai
->afpi_FinderInfo
[0], p
, AFP_FinderSize
);
974 static bool readdir_attr_meta_finderi(struct vfs_handle_struct
*handle
,
975 const struct smb_filename
*smb_fname
,
976 struct readdir_attr_data
*attr_data
)
978 struct fruit_config_data
*config
= NULL
;
983 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
984 struct fruit_config_data
,
987 switch (config
->meta
) {
988 case FRUIT_META_NETATALK
:
989 ok
= readdir_attr_meta_finderi_netatalk(
990 handle
, smb_fname
, &ai
);
993 case FRUIT_META_STREAM
:
994 ok
= readdir_attr_meta_finderi_stream(
995 handle
, smb_fname
, &ai
);
999 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1004 /* Don't bother with errors, it's likely ENOENT */
1008 if (S_ISREG(smb_fname
->st
.st_ex_mode
)) {
1010 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0],
1011 &ai
.afpi_FinderInfo
[0], 4);
1013 /* finder_creator */
1014 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 4,
1015 &ai
.afpi_FinderInfo
[4], 4);
1019 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 8,
1020 &ai
.afpi_FinderInfo
[8], 2);
1022 /* finder_ext_flags */
1023 memcpy(&attr_data
->attr_data
.aapl
.finder_info
[0] + 10,
1024 &ai
.afpi_FinderInfo
[24], 2);
1027 date_added
= convert_time_t_to_uint32_t(
1028 smb_fname
->st
.st_ex_btime
.tv_sec
- AD_DATE_DELTA
);
1030 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
[0], 12, date_added
);
1035 static uint64_t readdir_attr_rfork_size_adouble(
1036 struct vfs_handle_struct
*handle
,
1037 const struct smb_filename
*smb_fname
)
1039 struct adouble
*ad
= NULL
;
1040 uint64_t rfork_size
;
1042 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
1048 rfork_size
= ad_getentrylen(ad
, ADEID_RFORK
);
1054 static uint64_t readdir_attr_rfork_size_stream(
1055 struct vfs_handle_struct
*handle
,
1056 const struct smb_filename
*smb_fname
)
1058 struct smb_filename
*stream_name
= NULL
;
1060 uint64_t rfork_size
;
1062 stream_name
= synthetic_smb_fname(talloc_tos(),
1063 smb_fname
->base_name
,
1064 AFPRESOURCE_STREAM_NAME
,
1066 if (stream_name
== NULL
) {
1070 ret
= SMB_VFS_STAT(handle
->conn
, stream_name
);
1072 TALLOC_FREE(stream_name
);
1076 rfork_size
= stream_name
->st
.st_ex_size
;
1077 TALLOC_FREE(stream_name
);
1082 static uint64_t readdir_attr_rfork_size(struct vfs_handle_struct
*handle
,
1083 const struct smb_filename
*smb_fname
)
1085 struct fruit_config_data
*config
= NULL
;
1086 uint64_t rfork_size
;
1088 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1089 struct fruit_config_data
,
1092 switch (config
->rsrc
) {
1093 case FRUIT_RSRC_ADFILE
:
1094 rfork_size
= readdir_attr_rfork_size_adouble(handle
,
1098 case FRUIT_RSRC_XATTR
:
1099 case FRUIT_RSRC_STREAM
:
1100 rfork_size
= readdir_attr_rfork_size_stream(handle
,
1105 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1113 static NTSTATUS
readdir_attr_macmeta(struct vfs_handle_struct
*handle
,
1114 const struct smb_filename
*smb_fname
,
1115 struct readdir_attr_data
*attr_data
)
1117 NTSTATUS status
= NT_STATUS_OK
;
1118 struct fruit_config_data
*config
= NULL
;
1121 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1122 struct fruit_config_data
,
1123 return NT_STATUS_UNSUCCESSFUL
);
1126 /* Ensure we return a default value in the creation_date field */
1127 RSIVAL(&attr_data
->attr_data
.aapl
.finder_info
, 12, AD_DATE_START
);
1130 * Resource fork length
1133 if (config
->readdir_attr_rsize
) {
1134 uint64_t rfork_size
;
1136 rfork_size
= readdir_attr_rfork_size(handle
, smb_fname
);
1137 attr_data
->attr_data
.aapl
.rfork_size
= rfork_size
;
1144 if (config
->readdir_attr_finder_info
) {
1145 ok
= readdir_attr_meta_finderi(handle
, smb_fname
, attr_data
);
1147 status
= NT_STATUS_INTERNAL_ERROR
;
1154 static NTSTATUS
remove_virtual_nfs_aces(struct security_descriptor
*psd
)
1159 if (psd
->dacl
== NULL
) {
1160 return NT_STATUS_OK
;
1163 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
1164 /* MS NFS style mode/uid/gid */
1165 int cmp
= dom_sid_compare_domain(
1166 &global_sid_Unix_NFS
,
1167 &psd
->dacl
->aces
[i
].trustee
);
1169 /* Normal ACE entry. */
1174 * security_descriptor_dacl_del()
1175 * *must* return NT_STATUS_OK as we know
1176 * we have something to remove.
1179 status
= security_descriptor_dacl_del(psd
,
1180 &psd
->dacl
->aces
[i
].trustee
);
1181 if (!NT_STATUS_IS_OK(status
)) {
1182 DBG_WARNING("failed to remove MS NFS style ACE: %s\n",
1188 * security_descriptor_dacl_del() may delete more
1189 * then one entry subsequent to this one if the
1190 * SID matches, but we only need to ensure that
1191 * we stay looking at the same element in the array.
1195 return NT_STATUS_OK
;
1198 /* Search MS NFS style ACE with UNIX mode */
1199 static NTSTATUS
check_ms_nfs(vfs_handle_struct
*handle
,
1201 struct security_descriptor
*psd
,
1206 struct fruit_config_data
*config
= NULL
;
1210 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1211 struct fruit_config_data
,
1212 return NT_STATUS_UNSUCCESSFUL
);
1214 if (!global_fruit_config
.nego_aapl
) {
1215 return NT_STATUS_OK
;
1217 if (psd
->dacl
== NULL
|| !config
->unix_info_enabled
) {
1218 return NT_STATUS_OK
;
1221 for (i
= 0; i
< psd
->dacl
->num_aces
; i
++) {
1222 if (dom_sid_compare_domain(
1223 &global_sid_Unix_NFS_Mode
,
1224 &psd
->dacl
->aces
[i
].trustee
) == 0) {
1225 *pmode
= (mode_t
)psd
->dacl
->aces
[i
].trustee
.sub_auths
[2];
1226 *pmode
&= (S_IRWXU
| S_IRWXG
| S_IRWXO
);
1229 DEBUG(10, ("MS NFS chmod request %s, %04o\n",
1230 fsp_str_dbg(fsp
), (unsigned)(*pmode
)));
1236 * Remove any incoming virtual ACE entries generated by
1237 * fruit_fget_nt_acl().
1240 return remove_virtual_nfs_aces(psd
);
1243 /****************************************************************************
1245 ****************************************************************************/
1247 static int fruit_connect(vfs_handle_struct
*handle
,
1248 const char *service
,
1252 char *list
= NULL
, *newlist
= NULL
;
1253 struct fruit_config_data
*config
;
1254 const struct loadparm_substitution
*lp_sub
=
1255 loadparm_s3_global_substitution();
1257 DEBUG(10, ("fruit_connect\n"));
1259 rc
= SMB_VFS_NEXT_CONNECT(handle
, service
, user
);
1264 rc
= init_fruit_config(handle
);
1269 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1270 struct fruit_config_data
, return -1);
1272 if (config
->veto_appledouble
) {
1273 list
= lp_veto_files(talloc_tos(), lp_sub
, SNUM(handle
->conn
));
1276 if (strstr(list
, "/" ADOUBLE_NAME_PREFIX
"*/") == NULL
) {
1277 newlist
= talloc_asprintf(
1279 "%s/" ADOUBLE_NAME_PREFIX
"*/",
1281 lp_do_parameter(SNUM(handle
->conn
),
1286 lp_do_parameter(SNUM(handle
->conn
),
1288 "/" ADOUBLE_NAME_PREFIX
"*/");
1294 if (config
->encoding
== FRUIT_ENC_NATIVE
) {
1295 lp_do_parameter(SNUM(handle
->conn
),
1297 macos_string_replace_map
);
1300 if (config
->time_machine
) {
1301 DBG_NOTICE("Enabling durable handles for Time Machine "
1302 "support on [%s]\n", service
);
1303 lp_do_parameter(SNUM(handle
->conn
), "durable handles", "yes");
1304 lp_do_parameter(SNUM(handle
->conn
), "kernel oplocks", "no");
1305 lp_do_parameter(SNUM(handle
->conn
), "kernel share modes", "no");
1306 if (!lp_strict_sync(SNUM(handle
->conn
))) {
1307 DBG_WARNING("Time Machine without strict sync is not "
1310 lp_do_parameter(SNUM(handle
->conn
), "posix locking", "no");
1316 static int fruit_fake_fd(void)
1323 * Return a valid fd, but ensure any attempt to use it returns
1324 * an error (EPIPE). Once we get a write on the handle, we open
1327 ret
= pipe(pipe_fds
);
1337 static int fruit_open_meta_stream(vfs_handle_struct
*handle
,
1338 struct smb_filename
*smb_fname
,
1343 struct fruit_config_data
*config
= NULL
;
1344 struct fio
*fio
= NULL
;
1345 int open_flags
= flags
& ~O_CREAT
;
1348 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1350 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1351 struct fruit_config_data
, return -1);
1353 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
1354 fio
->type
= ADOUBLE_META
;
1355 fio
->config
= config
;
1357 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, open_flags
, mode
);
1362 if (!(flags
& O_CREAT
)) {
1363 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
1367 fd
= fruit_fake_fd();
1369 VFS_REMOVE_FSP_EXTENSION(handle
, fsp
);
1373 fio
->fake_fd
= true;
1380 static int fruit_open_meta_netatalk(vfs_handle_struct
*handle
,
1381 struct smb_filename
*smb_fname
,
1386 struct fruit_config_data
*config
= NULL
;
1387 struct fio
*fio
= NULL
;
1388 struct adouble
*ad
= NULL
;
1389 bool meta_exists
= false;
1392 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1394 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
1401 if (!meta_exists
&& !(flags
& O_CREAT
)) {
1406 fd
= fruit_fake_fd();
1411 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1412 struct fruit_config_data
, return -1);
1414 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
1415 fio
->type
= ADOUBLE_META
;
1416 fio
->config
= config
;
1417 fio
->fake_fd
= true;
1424 static int fruit_open_meta(vfs_handle_struct
*handle
,
1425 struct smb_filename
*smb_fname
,
1426 files_struct
*fsp
, int flags
, mode_t mode
)
1429 struct fruit_config_data
*config
= NULL
;
1431 DBG_DEBUG("path [%s]\n", smb_fname_str_dbg(smb_fname
));
1433 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1434 struct fruit_config_data
, return -1);
1436 switch (config
->meta
) {
1437 case FRUIT_META_STREAM
:
1438 fd
= fruit_open_meta_stream(handle
, smb_fname
,
1442 case FRUIT_META_NETATALK
:
1443 fd
= fruit_open_meta_netatalk(handle
, smb_fname
,
1448 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1452 DBG_DEBUG("path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1457 static int fruit_open_rsrc_adouble(vfs_handle_struct
*handle
,
1458 struct smb_filename
*smb_fname
,
1464 struct adouble
*ad
= NULL
;
1465 struct smb_filename
*smb_fname_base
= NULL
;
1466 struct fruit_config_data
*config
= NULL
;
1469 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1470 struct fruit_config_data
, return -1);
1472 if ((!(flags
& O_CREAT
)) &&
1473 S_ISDIR(fsp
->base_fsp
->fsp_name
->st
.st_ex_mode
))
1475 /* sorry, but directories don't habe a resource fork */
1480 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_base
);
1485 /* We always need read/write access for the metadata header too */
1486 flags
&= ~(O_RDONLY
| O_WRONLY
);
1489 hostfd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname_base
, fsp
,
1496 if (flags
& (O_CREAT
| O_TRUNC
)) {
1497 ad
= ad_init(fsp
, ADOUBLE_RSRC
);
1503 fsp
->fh
->fd
= hostfd
;
1505 rc
= ad_fset(handle
, ad
, fsp
);
1516 TALLOC_FREE(smb_fname_base
);
1518 DEBUG(10, ("fruit_open resource fork: rc=%d, fd=%d\n", rc
, hostfd
));
1520 int saved_errno
= errno
;
1523 * BUGBUGBUG -- we would need to call
1524 * fd_close_posix here, but we don't have a
1527 fsp
->fh
->fd
= hostfd
;
1531 errno
= saved_errno
;
1536 static int fruit_open_rsrc_xattr(vfs_handle_struct
*handle
,
1537 struct smb_filename
*smb_fname
,
1542 #ifdef HAVE_ATTROPEN
1545 fd
= attropen(smb_fname
->base_name
,
1546 AFPRESOURCE_EA_NETATALK
,
1561 static int fruit_open_rsrc(vfs_handle_struct
*handle
,
1562 struct smb_filename
*smb_fname
,
1563 files_struct
*fsp
, int flags
, mode_t mode
)
1566 struct fruit_config_data
*config
= NULL
;
1567 struct fio
*fio
= NULL
;
1569 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1571 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1572 struct fruit_config_data
, return -1);
1574 switch (config
->rsrc
) {
1575 case FRUIT_RSRC_STREAM
:
1576 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
1579 case FRUIT_RSRC_ADFILE
:
1580 fd
= fruit_open_rsrc_adouble(handle
, smb_fname
,
1584 case FRUIT_RSRC_XATTR
:
1585 fd
= fruit_open_rsrc_xattr(handle
, smb_fname
,
1590 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1594 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1600 fio
= VFS_ADD_FSP_EXTENSION(handle
, fsp
, struct fio
, NULL
);
1601 fio
->type
= ADOUBLE_RSRC
;
1602 fio
->config
= config
;
1607 static int fruit_open(vfs_handle_struct
*handle
,
1608 struct smb_filename
*smb_fname
,
1609 files_struct
*fsp
, int flags
, mode_t mode
)
1613 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
1615 if (!is_named_stream(smb_fname
)) {
1616 return SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
1619 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
1620 fd
= fruit_open_meta(handle
, smb_fname
, fsp
, flags
, mode
);
1621 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
1622 fd
= fruit_open_rsrc(handle
, smb_fname
, fsp
, flags
, mode
);
1624 fd
= SMB_VFS_NEXT_OPEN(handle
, smb_fname
, fsp
, flags
, mode
);
1627 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(smb_fname
), fd
);
1632 static int fruit_close_meta(vfs_handle_struct
*handle
,
1635 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
1637 struct fruit_config_data
*config
= NULL
;
1639 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1640 struct fruit_config_data
, return -1);
1642 switch (config
->meta
) {
1643 case FRUIT_META_STREAM
:
1645 ret
= vfs_fake_fd_close(fsp
->fh
->fd
);
1648 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1652 case FRUIT_META_NETATALK
:
1653 ret
= vfs_fake_fd_close(fsp
->fh
->fd
);
1658 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
1666 static int fruit_close_rsrc(vfs_handle_struct
*handle
,
1670 struct fruit_config_data
*config
= NULL
;
1672 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1673 struct fruit_config_data
, return -1);
1675 switch (config
->rsrc
) {
1676 case FRUIT_RSRC_STREAM
:
1677 case FRUIT_RSRC_ADFILE
:
1678 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1681 case FRUIT_RSRC_XATTR
:
1682 ret
= vfs_fake_fd_close(fsp
->fh
->fd
);
1687 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
1694 static int fruit_close(vfs_handle_struct
*handle
,
1702 DBG_DEBUG("Path [%s] fd [%d]\n", smb_fname_str_dbg(fsp
->fsp_name
), fd
);
1704 if (!is_named_stream(fsp
->fsp_name
)) {
1705 return SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1708 if (is_afpinfo_stream(fsp
->fsp_name
->stream_name
)) {
1709 ret
= fruit_close_meta(handle
, fsp
);
1710 } else if (is_afpresource_stream(fsp
->fsp_name
->stream_name
)) {
1711 ret
= fruit_close_rsrc(handle
, fsp
);
1713 ret
= SMB_VFS_NEXT_CLOSE(handle
, fsp
);
1719 static int fruit_renameat(struct vfs_handle_struct
*handle
,
1720 files_struct
*srcfsp
,
1721 const struct smb_filename
*smb_fname_src
,
1722 files_struct
*dstfsp
,
1723 const struct smb_filename
*smb_fname_dst
)
1726 struct fruit_config_data
*config
= NULL
;
1727 struct smb_filename
*src_adp_smb_fname
= NULL
;
1728 struct smb_filename
*dst_adp_smb_fname
= NULL
;
1730 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1731 struct fruit_config_data
, return -1);
1733 if (!VALID_STAT(smb_fname_src
->st
)) {
1734 DBG_ERR("Need valid stat for [%s]\n",
1735 smb_fname_str_dbg(smb_fname_src
));
1739 rc
= SMB_VFS_NEXT_RENAMEAT(handle
,
1748 if ((config
->rsrc
!= FRUIT_RSRC_ADFILE
) ||
1749 (!S_ISREG(smb_fname_src
->st
.st_ex_mode
)))
1754 rc
= adouble_path(talloc_tos(), smb_fname_src
, &src_adp_smb_fname
);
1759 rc
= adouble_path(talloc_tos(), smb_fname_dst
, &dst_adp_smb_fname
);
1764 DBG_DEBUG("%s -> %s\n",
1765 smb_fname_str_dbg(src_adp_smb_fname
),
1766 smb_fname_str_dbg(dst_adp_smb_fname
));
1768 rc
= SMB_VFS_NEXT_RENAMEAT(handle
,
1773 if (errno
== ENOENT
) {
1778 TALLOC_FREE(src_adp_smb_fname
);
1779 TALLOC_FREE(dst_adp_smb_fname
);
1783 static int fruit_unlink_meta_stream(vfs_handle_struct
*handle
,
1784 struct files_struct
*dirfsp
,
1785 const struct smb_filename
*smb_fname
)
1787 return SMB_VFS_NEXT_UNLINKAT(handle
,
1793 static int fruit_unlink_meta_netatalk(vfs_handle_struct
*handle
,
1794 const struct smb_filename
*smb_fname
)
1796 return SMB_VFS_REMOVEXATTR(handle
->conn
,
1798 AFPINFO_EA_NETATALK
);
1801 static int fruit_unlink_meta(vfs_handle_struct
*handle
,
1802 struct files_struct
*dirfsp
,
1803 const struct smb_filename
*smb_fname
)
1805 struct fruit_config_data
*config
= NULL
;
1808 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1809 struct fruit_config_data
, return -1);
1811 switch (config
->meta
) {
1812 case FRUIT_META_STREAM
:
1813 rc
= fruit_unlink_meta_stream(handle
,
1818 case FRUIT_META_NETATALK
:
1819 rc
= fruit_unlink_meta_netatalk(handle
, smb_fname
);
1823 DBG_ERR("Unsupported meta config [%d]\n", config
->meta
);
1830 static int fruit_unlink_rsrc_stream(vfs_handle_struct
*handle
,
1831 struct files_struct
*dirfsp
,
1832 const struct smb_filename
*smb_fname
,
1837 if (!force_unlink
) {
1838 struct smb_filename
*smb_fname_cp
= NULL
;
1841 smb_fname_cp
= cp_smb_filename(talloc_tos(), smb_fname
);
1842 if (smb_fname_cp
== NULL
) {
1847 * 0 byte resource fork streams are not listed by
1848 * vfs_streaminfo, as a result stream cleanup/deletion of file
1849 * deletion doesn't remove the resourcefork stream.
1852 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname_cp
);
1854 TALLOC_FREE(smb_fname_cp
);
1855 DBG_ERR("stat [%s] failed [%s]\n",
1856 smb_fname_str_dbg(smb_fname_cp
), strerror(errno
));
1860 size
= smb_fname_cp
->st
.st_ex_size
;
1861 TALLOC_FREE(smb_fname_cp
);
1864 /* OS X ignores resource fork stream delete requests */
1869 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
1873 if ((ret
!= 0) && (errno
== ENOENT
) && force_unlink
) {
1880 static int fruit_unlink_rsrc_adouble(vfs_handle_struct
*handle
,
1881 struct files_struct
*dirfsp
,
1882 const struct smb_filename
*smb_fname
,
1886 struct adouble
*ad
= NULL
;
1887 struct smb_filename
*adp_smb_fname
= NULL
;
1889 if (!force_unlink
) {
1890 ad
= ad_get(talloc_tos(), handle
, smb_fname
,
1899 * 0 byte resource fork streams are not listed by
1900 * vfs_streaminfo, as a result stream cleanup/deletion of file
1901 * deletion doesn't remove the resourcefork stream.
1904 if (ad_getentrylen(ad
, ADEID_RFORK
) > 0) {
1905 /* OS X ignores resource fork stream delete requests */
1913 rc
= adouble_path(talloc_tos(), smb_fname
, &adp_smb_fname
);
1918 rc
= SMB_VFS_NEXT_UNLINKAT(handle
,
1922 TALLOC_FREE(adp_smb_fname
);
1923 if ((rc
!= 0) && (errno
== ENOENT
) && force_unlink
) {
1930 static int fruit_unlink_rsrc_xattr(vfs_handle_struct
*handle
,
1931 const struct smb_filename
*smb_fname
,
1935 * OS X ignores resource fork stream delete requests, so nothing to do
1936 * here. Removing the file will remove the xattr anyway, so we don't
1937 * have to take care of removing 0 byte resource forks that could be
1943 static int fruit_unlink_rsrc(vfs_handle_struct
*handle
,
1944 struct files_struct
*dirfsp
,
1945 const struct smb_filename
*smb_fname
,
1948 struct fruit_config_data
*config
= NULL
;
1951 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1952 struct fruit_config_data
, return -1);
1954 switch (config
->rsrc
) {
1955 case FRUIT_RSRC_STREAM
:
1956 rc
= fruit_unlink_rsrc_stream(handle
,
1962 case FRUIT_RSRC_ADFILE
:
1963 rc
= fruit_unlink_rsrc_adouble(handle
,
1969 case FRUIT_RSRC_XATTR
:
1970 rc
= fruit_unlink_rsrc_xattr(handle
, smb_fname
, force_unlink
);
1974 DBG_ERR("Unsupported rsrc config [%d]\n", config
->rsrc
);
1981 static int fruit_unlink_internal(vfs_handle_struct
*handle
,
1982 struct files_struct
*dirfsp
,
1983 const struct smb_filename
*smb_fname
)
1986 struct fruit_config_data
*config
= NULL
;
1987 struct smb_filename
*rsrc_smb_fname
= NULL
;
1989 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
1990 struct fruit_config_data
, return -1);
1992 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
1993 return fruit_unlink_meta(handle
,
1996 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
1997 return fruit_unlink_rsrc(handle
,
2001 } else if (is_named_stream(smb_fname
)) {
2002 return SMB_VFS_NEXT_UNLINKAT(handle
,
2006 } else if (is_adouble_file(smb_fname
->base_name
)) {
2007 return SMB_VFS_NEXT_UNLINKAT(handle
,
2014 * A request to delete the base file. Because 0 byte resource
2015 * fork streams are not listed by fruit_streaminfo,
2016 * delete_all_streams() can't remove 0 byte resource fork
2017 * streams, so we have to cleanup this here.
2019 rsrc_smb_fname
= synthetic_smb_fname(talloc_tos(),
2020 smb_fname
->base_name
,
2021 AFPRESOURCE_STREAM_NAME
,
2024 if (rsrc_smb_fname
== NULL
) {
2028 rc
= fruit_unlink_rsrc(handle
, dirfsp
, rsrc_smb_fname
, true);
2029 if ((rc
!= 0) && (errno
!= ENOENT
)) {
2030 DBG_ERR("Forced unlink of [%s] failed [%s]\n",
2031 smb_fname_str_dbg(rsrc_smb_fname
), strerror(errno
));
2032 TALLOC_FREE(rsrc_smb_fname
);
2035 TALLOC_FREE(rsrc_smb_fname
);
2037 return SMB_VFS_NEXT_UNLINKAT(handle
,
2043 static int fruit_chmod(vfs_handle_struct
*handle
,
2044 const struct smb_filename
*smb_fname
,
2048 struct fruit_config_data
*config
= NULL
;
2049 struct smb_filename
*smb_fname_adp
= NULL
;
2051 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname
, mode
);
2056 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2057 struct fruit_config_data
, return -1);
2059 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
2063 if (!VALID_STAT(smb_fname
->st
)) {
2067 if (!S_ISREG(smb_fname
->st
.st_ex_mode
)) {
2071 rc
= adouble_path(talloc_tos(), smb_fname
, &smb_fname_adp
);
2076 DEBUG(10, ("fruit_chmod: %s\n", smb_fname_adp
->base_name
));
2078 rc
= SMB_VFS_NEXT_CHMOD(handle
, smb_fname_adp
, mode
);
2079 if (errno
== ENOENT
) {
2083 TALLOC_FREE(smb_fname_adp
);
2087 static int fruit_rmdir_internal(struct vfs_handle_struct
*handle
,
2088 struct files_struct
*dirfsp
,
2089 const struct smb_filename
*smb_fname
)
2093 struct fruit_config_data
*config
;
2095 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
2096 struct fruit_config_data
, return -1);
2098 if (config
->rsrc
!= FRUIT_RSRC_ADFILE
) {
2103 * Due to there is no way to change bDeleteVetoFiles variable
2104 * from this module, need to clean up ourselves
2107 dh
= SMB_VFS_OPENDIR(handle
->conn
, smb_fname
, NULL
, 0);
2112 while ((de
= SMB_VFS_READDIR(handle
->conn
, dh
, NULL
)) != NULL
) {
2113 struct adouble
*ad
= NULL
;
2115 struct smb_filename
*ad_smb_fname
= NULL
;
2118 if (!is_adouble_file(de
->d_name
)) {
2122 p
= talloc_asprintf(talloc_tos(), "%s/%s",
2123 smb_fname
->base_name
, de
->d_name
);
2125 DBG_ERR("talloc_asprintf failed\n");
2129 ad_smb_fname
= synthetic_smb_fname(talloc_tos(), p
,
2133 if (ad_smb_fname
== NULL
) {
2134 DBG_ERR("synthetic_smb_fname failed\n");
2139 * Check whether it's a valid AppleDouble file, if
2140 * yes, delete it, ignore it otherwise.
2142 ad
= ad_get(talloc_tos(), handle
, ad_smb_fname
, ADOUBLE_RSRC
);
2144 TALLOC_FREE(ad_smb_fname
);
2150 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
2155 DBG_ERR("Deleting [%s] failed\n",
2156 smb_fname_str_dbg(ad_smb_fname
));
2158 TALLOC_FREE(ad_smb_fname
);
2163 SMB_VFS_CLOSEDIR(handle
->conn
, dh
);
2165 return SMB_VFS_NEXT_UNLINKAT(handle
,
2171 static int fruit_unlinkat(vfs_handle_struct
*handle
,
2172 struct files_struct
*dirfsp
,
2173 const struct smb_filename
*smb_fname
,
2178 SMB_ASSERT(dirfsp
== dirfsp
->conn
->cwd_fsp
);
2179 if (flags
& AT_REMOVEDIR
) {
2180 ret
= fruit_rmdir_internal(handle
,
2184 ret
= fruit_unlink_internal(handle
,
2191 static ssize_t
fruit_pread_meta_stream(vfs_handle_struct
*handle
,
2192 files_struct
*fsp
, void *data
,
2193 size_t n
, off_t offset
)
2195 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2203 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2204 if (nread
== -1 || nread
== n
) {
2208 DBG_ERR("Removing [%s] after short read [%zd]\n",
2209 fsp_str_dbg(fsp
), nread
);
2211 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
2216 DBG_ERR("Removing [%s] failed\n", fsp_str_dbg(fsp
));
2224 static ssize_t
fruit_pread_meta_adouble(vfs_handle_struct
*handle
,
2225 files_struct
*fsp
, void *data
,
2226 size_t n
, off_t offset
)
2229 struct adouble
*ad
= NULL
;
2230 char afpinfo_buf
[AFP_INFO_SIZE
];
2234 ai
= afpinfo_new(talloc_tos());
2239 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
2245 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2247 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
2252 memcpy(&ai
->afpi_FinderInfo
[0], p
, ADEDLEN_FINDERI
);
2254 nread
= afpinfo_pack(ai
, afpinfo_buf
);
2255 if (nread
!= AFP_INFO_SIZE
) {
2260 memcpy(data
, afpinfo_buf
, n
);
2268 static ssize_t
fruit_pread_meta(vfs_handle_struct
*handle
,
2269 files_struct
*fsp
, void *data
,
2270 size_t n
, off_t offset
)
2272 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2277 * OS X has a off-by-1 error in the offset calculation, so we're
2278 * bug compatible here. It won't hurt, as any relevant real
2279 * world read requests from the AFP_AfpInfo stream will be
2280 * offset=0 n=60. offset is ignored anyway, see below.
2282 if ((offset
< 0) || (offset
>= AFP_INFO_SIZE
+ 1)) {
2287 DBG_ERR("Failed to fetch fsp extension");
2291 /* Yes, macOS always reads from offset 0 */
2293 to_return
= MIN(n
, AFP_INFO_SIZE
);
2295 switch (fio
->config
->meta
) {
2296 case FRUIT_META_STREAM
:
2297 nread
= fruit_pread_meta_stream(handle
, fsp
, data
,
2301 case FRUIT_META_NETATALK
:
2302 nread
= fruit_pread_meta_adouble(handle
, fsp
, data
,
2307 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
2311 if (nread
== -1 && fio
->fake_fd
) {
2313 char afpinfo_buf
[AFP_INFO_SIZE
];
2315 ai
= afpinfo_new(talloc_tos());
2320 nread
= afpinfo_pack(ai
, afpinfo_buf
);
2322 if (nread
!= AFP_INFO_SIZE
) {
2326 memcpy(data
, afpinfo_buf
, to_return
);
2333 static ssize_t
fruit_pread_rsrc_stream(vfs_handle_struct
*handle
,
2334 files_struct
*fsp
, void *data
,
2335 size_t n
, off_t offset
)
2337 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2340 static ssize_t
fruit_pread_rsrc_xattr(vfs_handle_struct
*handle
,
2341 files_struct
*fsp
, void *data
,
2342 size_t n
, off_t offset
)
2344 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2347 static ssize_t
fruit_pread_rsrc_adouble(vfs_handle_struct
*handle
,
2348 files_struct
*fsp
, void *data
,
2349 size_t n
, off_t offset
)
2351 struct adouble
*ad
= NULL
;
2354 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
2359 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
,
2360 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2366 static ssize_t
fruit_pread_rsrc(vfs_handle_struct
*handle
,
2367 files_struct
*fsp
, void *data
,
2368 size_t n
, off_t offset
)
2370 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2378 switch (fio
->config
->rsrc
) {
2379 case FRUIT_RSRC_STREAM
:
2380 nread
= fruit_pread_rsrc_stream(handle
, fsp
, data
, n
, offset
);
2383 case FRUIT_RSRC_ADFILE
:
2384 nread
= fruit_pread_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
2387 case FRUIT_RSRC_XATTR
:
2388 nread
= fruit_pread_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
2392 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
2399 static ssize_t
fruit_pread(vfs_handle_struct
*handle
,
2400 files_struct
*fsp
, void *data
,
2401 size_t n
, off_t offset
)
2403 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2406 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
2407 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
2410 return SMB_VFS_NEXT_PREAD(handle
, fsp
, data
, n
, offset
);
2413 if (fio
->type
== ADOUBLE_META
) {
2414 nread
= fruit_pread_meta(handle
, fsp
, data
, n
, offset
);
2416 nread
= fruit_pread_rsrc(handle
, fsp
, data
, n
, offset
);
2419 DBG_DEBUG("Path [%s] nread [%zd]\n", fsp_str_dbg(fsp
), nread
);
2423 static bool fruit_must_handle_aio_stream(struct fio
*fio
)
2429 if (fio
->type
== ADOUBLE_META
) {
2433 if ((fio
->type
== ADOUBLE_RSRC
) &&
2434 (fio
->config
->rsrc
== FRUIT_RSRC_ADFILE
))
2442 struct fruit_pread_state
{
2444 struct vfs_aio_state vfs_aio_state
;
2447 static void fruit_pread_done(struct tevent_req
*subreq
);
2449 static struct tevent_req
*fruit_pread_send(
2450 struct vfs_handle_struct
*handle
,
2451 TALLOC_CTX
*mem_ctx
,
2452 struct tevent_context
*ev
,
2453 struct files_struct
*fsp
,
2455 size_t n
, off_t offset
)
2457 struct tevent_req
*req
= NULL
;
2458 struct tevent_req
*subreq
= NULL
;
2459 struct fruit_pread_state
*state
= NULL
;
2460 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2462 req
= tevent_req_create(mem_ctx
, &state
,
2463 struct fruit_pread_state
);
2468 if (fruit_must_handle_aio_stream(fio
)) {
2469 state
->nread
= SMB_VFS_PREAD(fsp
, data
, n
, offset
);
2470 if (state
->nread
!= n
) {
2471 if (state
->nread
!= -1) {
2474 tevent_req_error(req
, errno
);
2475 return tevent_req_post(req
, ev
);
2477 tevent_req_done(req
);
2478 return tevent_req_post(req
, ev
);
2481 subreq
= SMB_VFS_NEXT_PREAD_SEND(state
, ev
, handle
, fsp
,
2483 if (tevent_req_nomem(req
, subreq
)) {
2484 return tevent_req_post(req
, ev
);
2486 tevent_req_set_callback(subreq
, fruit_pread_done
, req
);
2490 static void fruit_pread_done(struct tevent_req
*subreq
)
2492 struct tevent_req
*req
= tevent_req_callback_data(
2493 subreq
, struct tevent_req
);
2494 struct fruit_pread_state
*state
= tevent_req_data(
2495 req
, struct fruit_pread_state
);
2497 state
->nread
= SMB_VFS_PREAD_RECV(subreq
, &state
->vfs_aio_state
);
2498 TALLOC_FREE(subreq
);
2500 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
2503 tevent_req_done(req
);
2506 static ssize_t
fruit_pread_recv(struct tevent_req
*req
,
2507 struct vfs_aio_state
*vfs_aio_state
)
2509 struct fruit_pread_state
*state
= tevent_req_data(
2510 req
, struct fruit_pread_state
);
2512 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
2516 *vfs_aio_state
= state
->vfs_aio_state
;
2517 return state
->nread
;
2520 static ssize_t
fruit_pwrite_meta_stream(vfs_handle_struct
*handle
,
2521 files_struct
*fsp
, const void *data
,
2522 size_t n
, off_t offset
)
2524 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2530 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
2531 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
2538 int fd
= fsp
->fh
->fd
;
2540 ret
= vfs_fake_fd_close(fd
);
2543 DBG_ERR("Close [%s] failed: %s\n",
2544 fsp_str_dbg(fsp
), strerror(errno
));
2548 fd
= SMB_VFS_NEXT_OPEN(handle
,
2554 DBG_ERR("On-demand create [%s] in write failed: %s\n",
2555 fsp_str_dbg(fsp
), strerror(errno
));
2559 fio
->fake_fd
= false;
2562 ai
= afpinfo_unpack(talloc_tos(), data
);
2567 if (ai_empty_finderinfo(ai
)) {
2569 * Writing an all 0 blob to the metadata stream results in the
2570 * stream being removed on a macOS server. This ensures we
2571 * behave the same and it verified by the "delete AFP_AfpInfo by
2572 * writing all 0" test.
2574 ret
= SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, 0);
2576 DBG_ERR("SMB_VFS_NEXT_FTRUNCATE on [%s] failed\n",
2581 ok
= set_delete_on_close(
2584 handle
->conn
->session_info
->security_token
,
2585 handle
->conn
->session_info
->unix_token
);
2587 DBG_ERR("set_delete_on_close on [%s] failed\n",
2594 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2595 if (nwritten
!= n
) {
2602 static ssize_t
fruit_pwrite_meta_netatalk(vfs_handle_struct
*handle
,
2603 files_struct
*fsp
, const void *data
,
2604 size_t n
, off_t offset
)
2606 struct adouble
*ad
= NULL
;
2612 ai
= afpinfo_unpack(talloc_tos(), data
);
2617 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_META
);
2619 ad
= ad_init(talloc_tos(), ADOUBLE_META
);
2624 p
= ad_get_entry(ad
, ADEID_FINDERI
);
2626 DBG_ERR("No ADEID_FINDERI for [%s]\n", fsp_str_dbg(fsp
));
2631 memcpy(p
, &ai
->afpi_FinderInfo
[0], ADEDLEN_FINDERI
);
2633 ret
= ad_fset(handle
, ad
, fsp
);
2635 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
2642 if (!ai_empty_finderinfo(ai
)) {
2647 * Writing an all 0 blob to the metadata stream results in the stream
2648 * being removed on a macOS server. This ensures we behave the same and
2649 * it verified by the "delete AFP_AfpInfo by writing all 0" test.
2652 ok
= set_delete_on_close(
2655 handle
->conn
->session_info
->security_token
,
2656 handle
->conn
->session_info
->unix_token
);
2658 DBG_ERR("set_delete_on_close on [%s] failed\n",
2666 static ssize_t
fruit_pwrite_meta(vfs_handle_struct
*handle
,
2667 files_struct
*fsp
, const void *data
,
2668 size_t n
, off_t offset
)
2670 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2672 uint8_t buf
[AFP_INFO_SIZE
];
2678 DBG_ERR("Failed to fetch fsp extension");
2687 if (offset
!= 0 && n
< 60) {
2692 cmp
= memcmp(data
, "AFP", 3);
2698 if (n
<= AFP_OFF_FinderInfo
) {
2700 * Nothing to do here really, just return
2708 if (to_copy
> AFP_INFO_SIZE
) {
2709 to_copy
= AFP_INFO_SIZE
;
2711 memcpy(buf
, data
, to_copy
);
2714 if (to_write
!= AFP_INFO_SIZE
) {
2715 to_write
= AFP_INFO_SIZE
;
2718 switch (fio
->config
->meta
) {
2719 case FRUIT_META_STREAM
:
2720 nwritten
= fruit_pwrite_meta_stream(handle
,
2727 case FRUIT_META_NETATALK
:
2728 nwritten
= fruit_pwrite_meta_netatalk(handle
,
2736 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
2740 if (nwritten
!= to_write
) {
2745 * Return the requested amount, verified against macOS SMB server
2750 static ssize_t
fruit_pwrite_rsrc_stream(vfs_handle_struct
*handle
,
2751 files_struct
*fsp
, const void *data
,
2752 size_t n
, off_t offset
)
2754 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2757 static ssize_t
fruit_pwrite_rsrc_xattr(vfs_handle_struct
*handle
,
2758 files_struct
*fsp
, const void *data
,
2759 size_t n
, off_t offset
)
2761 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2764 static ssize_t
fruit_pwrite_rsrc_adouble(vfs_handle_struct
*handle
,
2765 files_struct
*fsp
, const void *data
,
2766 size_t n
, off_t offset
)
2768 struct adouble
*ad
= NULL
;
2772 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
2774 DBG_ERR("ad_get [%s] failed\n", fsp_str_dbg(fsp
));
2778 nwritten
= SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
,
2779 offset
+ ad_getentryoff(ad
, ADEID_RFORK
));
2780 if (nwritten
!= n
) {
2781 DBG_ERR("Short write on [%s] [%zd/%zd]\n",
2782 fsp_str_dbg(fsp
), nwritten
, n
);
2787 if ((n
+ offset
) > ad_getentrylen(ad
, ADEID_RFORK
)) {
2788 ad_setentrylen(ad
, ADEID_RFORK
, n
+ offset
);
2789 ret
= ad_fset(handle
, ad
, fsp
);
2791 DBG_ERR("ad_pwrite [%s] failed\n", fsp_str_dbg(fsp
));
2801 static ssize_t
fruit_pwrite_rsrc(vfs_handle_struct
*handle
,
2802 files_struct
*fsp
, const void *data
,
2803 size_t n
, off_t offset
)
2805 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2809 DBG_ERR("Failed to fetch fsp extension");
2813 switch (fio
->config
->rsrc
) {
2814 case FRUIT_RSRC_STREAM
:
2815 nwritten
= fruit_pwrite_rsrc_stream(handle
, fsp
, data
, n
, offset
);
2818 case FRUIT_RSRC_ADFILE
:
2819 nwritten
= fruit_pwrite_rsrc_adouble(handle
, fsp
, data
, n
, offset
);
2822 case FRUIT_RSRC_XATTR
:
2823 nwritten
= fruit_pwrite_rsrc_xattr(handle
, fsp
, data
, n
, offset
);
2827 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
2834 static ssize_t
fruit_pwrite(vfs_handle_struct
*handle
,
2835 files_struct
*fsp
, const void *data
,
2836 size_t n
, off_t offset
)
2838 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2841 DBG_DEBUG("Path [%s] offset=%"PRIdMAX
", size=%zd\n",
2842 fsp_str_dbg(fsp
), (intmax_t)offset
, n
);
2845 return SMB_VFS_NEXT_PWRITE(handle
, fsp
, data
, n
, offset
);
2848 if (fio
->type
== ADOUBLE_META
) {
2849 nwritten
= fruit_pwrite_meta(handle
, fsp
, data
, n
, offset
);
2851 nwritten
= fruit_pwrite_rsrc(handle
, fsp
, data
, n
, offset
);
2854 DBG_DEBUG("Path [%s] nwritten=%zd\n", fsp_str_dbg(fsp
), nwritten
);
2858 struct fruit_pwrite_state
{
2860 struct vfs_aio_state vfs_aio_state
;
2863 static void fruit_pwrite_done(struct tevent_req
*subreq
);
2865 static struct tevent_req
*fruit_pwrite_send(
2866 struct vfs_handle_struct
*handle
,
2867 TALLOC_CTX
*mem_ctx
,
2868 struct tevent_context
*ev
,
2869 struct files_struct
*fsp
,
2871 size_t n
, off_t offset
)
2873 struct tevent_req
*req
= NULL
;
2874 struct tevent_req
*subreq
= NULL
;
2875 struct fruit_pwrite_state
*state
= NULL
;
2876 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
2878 req
= tevent_req_create(mem_ctx
, &state
,
2879 struct fruit_pwrite_state
);
2884 if (fruit_must_handle_aio_stream(fio
)) {
2885 state
->nwritten
= SMB_VFS_PWRITE(fsp
, data
, n
, offset
);
2886 if (state
->nwritten
!= n
) {
2887 if (state
->nwritten
!= -1) {
2890 tevent_req_error(req
, errno
);
2891 return tevent_req_post(req
, ev
);
2893 tevent_req_done(req
);
2894 return tevent_req_post(req
, ev
);
2897 subreq
= SMB_VFS_NEXT_PWRITE_SEND(state
, ev
, handle
, fsp
,
2899 if (tevent_req_nomem(req
, subreq
)) {
2900 return tevent_req_post(req
, ev
);
2902 tevent_req_set_callback(subreq
, fruit_pwrite_done
, req
);
2906 static void fruit_pwrite_done(struct tevent_req
*subreq
)
2908 struct tevent_req
*req
= tevent_req_callback_data(
2909 subreq
, struct tevent_req
);
2910 struct fruit_pwrite_state
*state
= tevent_req_data(
2911 req
, struct fruit_pwrite_state
);
2913 state
->nwritten
= SMB_VFS_PWRITE_RECV(subreq
, &state
->vfs_aio_state
);
2914 TALLOC_FREE(subreq
);
2916 if (tevent_req_error(req
, state
->vfs_aio_state
.error
)) {
2919 tevent_req_done(req
);
2922 static ssize_t
fruit_pwrite_recv(struct tevent_req
*req
,
2923 struct vfs_aio_state
*vfs_aio_state
)
2925 struct fruit_pwrite_state
*state
= tevent_req_data(
2926 req
, struct fruit_pwrite_state
);
2928 if (tevent_req_is_unix_error(req
, &vfs_aio_state
->error
)) {
2932 *vfs_aio_state
= state
->vfs_aio_state
;
2933 return state
->nwritten
;
2937 * Helper to stat/lstat the base file of an smb_fname.
2939 static int fruit_stat_base(vfs_handle_struct
*handle
,
2940 struct smb_filename
*smb_fname
,
2943 char *tmp_stream_name
;
2946 tmp_stream_name
= smb_fname
->stream_name
;
2947 smb_fname
->stream_name
= NULL
;
2949 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2951 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2953 smb_fname
->stream_name
= tmp_stream_name
;
2955 DBG_DEBUG("fruit_stat_base [%s] dev [%ju] ino [%ju]\n",
2956 smb_fname
->base_name
,
2957 (uintmax_t)smb_fname
->st
.st_ex_dev
,
2958 (uintmax_t)smb_fname
->st
.st_ex_ino
);
2962 static int fruit_stat_meta_stream(vfs_handle_struct
*handle
,
2963 struct smb_filename
*smb_fname
,
2969 ret
= fruit_stat_base(handle
, smb_fname
, false);
2974 ino
= hash_inode(&smb_fname
->st
, smb_fname
->stream_name
);
2977 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
2979 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
2982 smb_fname
->st
.st_ex_ino
= ino
;
2987 static int fruit_stat_meta_netatalk(vfs_handle_struct
*handle
,
2988 struct smb_filename
*smb_fname
,
2991 struct adouble
*ad
= NULL
;
2993 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
2995 DBG_INFO("fruit_stat_meta %s: %s\n",
2996 smb_fname_str_dbg(smb_fname
), strerror(errno
));
3002 /* Populate the stat struct with info from the base file. */
3003 if (fruit_stat_base(handle
, smb_fname
, follow_links
) == -1) {
3006 smb_fname
->st
.st_ex_size
= AFP_INFO_SIZE
;
3007 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3008 smb_fname
->stream_name
);
3012 static int fruit_stat_meta(vfs_handle_struct
*handle
,
3013 struct smb_filename
*smb_fname
,
3016 struct fruit_config_data
*config
= NULL
;
3019 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3020 struct fruit_config_data
, return -1);
3022 switch (config
->meta
) {
3023 case FRUIT_META_STREAM
:
3024 ret
= fruit_stat_meta_stream(handle
, smb_fname
, follow_links
);
3027 case FRUIT_META_NETATALK
:
3028 ret
= fruit_stat_meta_netatalk(handle
, smb_fname
, follow_links
);
3032 DBG_ERR("Unexpected meta config [%d]\n", config
->meta
);
3039 static int fruit_stat_rsrc_netatalk(vfs_handle_struct
*handle
,
3040 struct smb_filename
*smb_fname
,
3043 struct adouble
*ad
= NULL
;
3046 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
3052 /* Populate the stat struct with info from the base file. */
3053 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
3059 smb_fname
->st
.st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
3060 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3061 smb_fname
->stream_name
);
3066 static int fruit_stat_rsrc_stream(vfs_handle_struct
*handle
,
3067 struct smb_filename
*smb_fname
,
3073 ret
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3075 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3081 static int fruit_stat_rsrc_xattr(vfs_handle_struct
*handle
,
3082 struct smb_filename
*smb_fname
,
3085 #ifdef HAVE_ATTROPEN
3089 /* Populate the stat struct with info from the base file. */
3090 ret
= fruit_stat_base(handle
, smb_fname
, follow_links
);
3095 fd
= attropen(smb_fname
->base_name
,
3096 AFPRESOURCE_EA_NETATALK
,
3102 ret
= sys_fstat(fd
, &smb_fname
->st
, false);
3105 DBG_ERR("fstat [%s:%s] failed\n", smb_fname
->base_name
,
3106 AFPRESOURCE_EA_NETATALK
);
3112 smb_fname
->st
.st_ex_ino
= hash_inode(&smb_fname
->st
,
3113 smb_fname
->stream_name
);
3123 static int fruit_stat_rsrc(vfs_handle_struct
*handle
,
3124 struct smb_filename
*smb_fname
,
3127 struct fruit_config_data
*config
= NULL
;
3130 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3132 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
3133 struct fruit_config_data
, return -1);
3135 switch (config
->rsrc
) {
3136 case FRUIT_RSRC_STREAM
:
3137 ret
= fruit_stat_rsrc_stream(handle
, smb_fname
, follow_links
);
3140 case FRUIT_RSRC_XATTR
:
3141 ret
= fruit_stat_rsrc_xattr(handle
, smb_fname
, follow_links
);
3144 case FRUIT_RSRC_ADFILE
:
3145 ret
= fruit_stat_rsrc_netatalk(handle
, smb_fname
, follow_links
);
3149 DBG_ERR("Unexpected rsrc config [%d]\n", config
->rsrc
);
3156 static int fruit_stat(vfs_handle_struct
*handle
,
3157 struct smb_filename
*smb_fname
)
3161 DEBUG(10, ("fruit_stat called for %s\n",
3162 smb_fname_str_dbg(smb_fname
)));
3164 if (!is_named_stream(smb_fname
)) {
3165 rc
= SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3167 update_btime(handle
, smb_fname
);
3173 * Note if lp_posix_paths() is true, we can never
3174 * get here as is_ntfs_stream_smb_fname() is
3175 * always false. So we never need worry about
3176 * not following links here.
3179 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
3180 rc
= fruit_stat_meta(handle
, smb_fname
, true);
3181 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
3182 rc
= fruit_stat_rsrc(handle
, smb_fname
, true);
3184 return SMB_VFS_NEXT_STAT(handle
, smb_fname
);
3188 update_btime(handle
, smb_fname
);
3189 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
3190 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
3191 smb_fname
->st
.st_ex_blocks
=
3192 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3197 static int fruit_lstat(vfs_handle_struct
*handle
,
3198 struct smb_filename
*smb_fname
)
3202 DEBUG(10, ("fruit_lstat called for %s\n",
3203 smb_fname_str_dbg(smb_fname
)));
3205 if (!is_named_stream(smb_fname
)) {
3206 rc
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3208 update_btime(handle
, smb_fname
);
3213 if (is_afpinfo_stream(smb_fname
->stream_name
)) {
3214 rc
= fruit_stat_meta(handle
, smb_fname
, false);
3215 } else if (is_afpresource_stream(smb_fname
->stream_name
)) {
3216 rc
= fruit_stat_rsrc(handle
, smb_fname
, false);
3218 return SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
3222 update_btime(handle
, smb_fname
);
3223 smb_fname
->st
.st_ex_mode
&= ~S_IFMT
;
3224 smb_fname
->st
.st_ex_mode
|= S_IFREG
;
3225 smb_fname
->st
.st_ex_blocks
=
3226 smb_fname
->st
.st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3231 static int fruit_fstat_meta_stream(vfs_handle_struct
*handle
,
3233 SMB_STRUCT_STAT
*sbuf
)
3235 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3236 struct smb_filename smb_fname
;
3245 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3250 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3251 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
3252 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3256 smb_fname
= (struct smb_filename
) {
3257 .base_name
= fsp
->fsp_name
->base_name
,
3260 ret
= fruit_stat_base(handle
, &smb_fname
, false);
3264 *sbuf
= smb_fname
.st
;
3266 ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3268 ret
= SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3273 sbuf
->st_ex_ino
= ino
;
3277 static int fruit_fstat_meta_netatalk(vfs_handle_struct
*handle
,
3279 SMB_STRUCT_STAT
*sbuf
)
3283 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3288 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3289 sbuf
->st_ex_size
= AFP_INFO_SIZE
;
3290 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3295 static int fruit_fstat_meta(vfs_handle_struct
*handle
,
3297 SMB_STRUCT_STAT
*sbuf
,
3302 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
3304 switch (fio
->config
->meta
) {
3305 case FRUIT_META_STREAM
:
3306 ret
= fruit_fstat_meta_stream(handle
, fsp
, sbuf
);
3309 case FRUIT_META_NETATALK
:
3310 ret
= fruit_fstat_meta_netatalk(handle
, fsp
, sbuf
);
3314 DBG_ERR("Unexpected meta config [%d]\n", fio
->config
->meta
);
3318 DBG_DEBUG("Path [%s] ret [%d]\n", fsp_str_dbg(fsp
), ret
);
3322 static int fruit_fstat_rsrc_xattr(vfs_handle_struct
*handle
,
3324 SMB_STRUCT_STAT
*sbuf
)
3326 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3329 static int fruit_fstat_rsrc_stream(vfs_handle_struct
*handle
,
3331 SMB_STRUCT_STAT
*sbuf
)
3333 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3336 static int fruit_fstat_rsrc_adouble(vfs_handle_struct
*handle
,
3338 SMB_STRUCT_STAT
*sbuf
)
3340 struct adouble
*ad
= NULL
;
3343 /* Populate the stat struct with info from the base file. */
3344 ret
= fruit_stat_base(handle
, fsp
->base_fsp
->fsp_name
, false);
3349 ad
= ad_get(talloc_tos(), handle
,
3350 fsp
->base_fsp
->fsp_name
,
3353 DBG_ERR("ad_get [%s] failed [%s]\n",
3354 fsp_str_dbg(fsp
), strerror(errno
));
3358 *sbuf
= fsp
->base_fsp
->fsp_name
->st
;
3359 sbuf
->st_ex_size
= ad_getentrylen(ad
, ADEID_RFORK
);
3360 sbuf
->st_ex_ino
= hash_inode(sbuf
, fsp
->fsp_name
->stream_name
);
3366 static int fruit_fstat_rsrc(vfs_handle_struct
*handle
, files_struct
*fsp
,
3367 SMB_STRUCT_STAT
*sbuf
, struct fio
*fio
)
3371 switch (fio
->config
->rsrc
) {
3372 case FRUIT_RSRC_STREAM
:
3373 ret
= fruit_fstat_rsrc_stream(handle
, fsp
, sbuf
);
3376 case FRUIT_RSRC_ADFILE
:
3377 ret
= fruit_fstat_rsrc_adouble(handle
, fsp
, sbuf
);
3380 case FRUIT_RSRC_XATTR
:
3381 ret
= fruit_fstat_rsrc_xattr(handle
, fsp
, sbuf
);
3385 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
3392 static int fruit_fstat(vfs_handle_struct
*handle
, files_struct
*fsp
,
3393 SMB_STRUCT_STAT
*sbuf
)
3395 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3399 return SMB_VFS_NEXT_FSTAT(handle
, fsp
, sbuf
);
3402 DBG_DEBUG("Path [%s]\n", fsp_str_dbg(fsp
));
3404 if (fio
->type
== ADOUBLE_META
) {
3405 rc
= fruit_fstat_meta(handle
, fsp
, sbuf
, fio
);
3407 rc
= fruit_fstat_rsrc(handle
, fsp
, sbuf
, fio
);
3411 sbuf
->st_ex_mode
&= ~S_IFMT
;
3412 sbuf
->st_ex_mode
|= S_IFREG
;
3413 sbuf
->st_ex_blocks
= sbuf
->st_ex_size
/ STAT_ST_BLOCKSIZE
+ 1;
3416 DBG_DEBUG("Path [%s] rc [%d] size [%"PRIdMAX
"]\n",
3417 fsp_str_dbg(fsp
), rc
, (intmax_t)sbuf
->st_ex_size
);
3421 static NTSTATUS
delete_invalid_meta_stream(
3422 vfs_handle_struct
*handle
,
3423 const struct smb_filename
*smb_fname
,
3424 TALLOC_CTX
*mem_ctx
,
3425 unsigned int *pnum_streams
,
3426 struct stream_struct
**pstreams
,
3429 struct smb_filename
*sname
= NULL
;
3433 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
, AFPINFO_STREAM
);
3435 return NT_STATUS_INTERNAL_ERROR
;
3439 return NT_STATUS_OK
;
3442 sname
= synthetic_smb_fname(talloc_tos(),
3443 smb_fname
->base_name
,
3444 AFPINFO_STREAM_NAME
,
3446 if (sname
== NULL
) {
3447 return NT_STATUS_NO_MEMORY
;
3450 ret
= SMB_VFS_NEXT_UNLINKAT(handle
,
3451 handle
->conn
->cwd_fsp
,
3456 DBG_ERR("Removing [%s] failed\n", smb_fname_str_dbg(sname
));
3457 return map_nt_error_from_unix(errno
);
3460 return NT_STATUS_OK
;
3463 static NTSTATUS
fruit_streaminfo_meta_stream(
3464 vfs_handle_struct
*handle
,
3465 struct files_struct
*fsp
,
3466 const struct smb_filename
*smb_fname
,
3467 TALLOC_CTX
*mem_ctx
,
3468 unsigned int *pnum_streams
,
3469 struct stream_struct
**pstreams
)
3471 struct stream_struct
*stream
= *pstreams
;
3472 unsigned int num_streams
= *pnum_streams
;
3475 for (i
= 0; i
< num_streams
; i
++) {
3476 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
3481 if (i
== num_streams
) {
3482 return NT_STATUS_OK
;
3485 if (stream
[i
].size
!= AFP_INFO_SIZE
) {
3486 DBG_ERR("Removing invalid AFPINFO_STREAM size [%jd] from [%s]\n",
3487 (intmax_t)stream
[i
].size
, smb_fname_str_dbg(smb_fname
));
3489 return delete_invalid_meta_stream(handle
,
3498 return NT_STATUS_OK
;
3501 static NTSTATUS
fruit_streaminfo_meta_netatalk(
3502 vfs_handle_struct
*handle
,
3503 struct files_struct
*fsp
,
3504 const struct smb_filename
*smb_fname
,
3505 TALLOC_CTX
*mem_ctx
,
3506 unsigned int *pnum_streams
,
3507 struct stream_struct
**pstreams
)
3509 struct stream_struct
*stream
= *pstreams
;
3510 unsigned int num_streams
= *pnum_streams
;
3511 struct adouble
*ad
= NULL
;
3516 /* Remove the Netatalk xattr from the list */
3517 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3518 ":" NETATALK_META_XATTR
":$DATA");
3520 return NT_STATUS_NO_MEMORY
;
3524 * Check if there's a AFPINFO_STREAM from the VFS streams
3525 * backend and if yes, remove it from the list
3527 for (i
= 0; i
< num_streams
; i
++) {
3528 if (strequal_m(stream
[i
].name
, AFPINFO_STREAM
)) {
3533 if (i
< num_streams
) {
3534 DBG_WARNING("Unexpected AFPINFO_STREAM on [%s]\n",
3535 smb_fname_str_dbg(smb_fname
));
3537 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3540 return NT_STATUS_INTERNAL_ERROR
;
3544 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
3546 return NT_STATUS_OK
;
3549 is_fi_empty
= ad_empty_finderinfo(ad
);
3553 return NT_STATUS_OK
;
3556 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3557 AFPINFO_STREAM_NAME
, AFP_INFO_SIZE
,
3558 smb_roundup(handle
->conn
, AFP_INFO_SIZE
));
3560 return NT_STATUS_NO_MEMORY
;
3563 return NT_STATUS_OK
;
3566 static NTSTATUS
fruit_streaminfo_meta(vfs_handle_struct
*handle
,
3567 struct files_struct
*fsp
,
3568 const struct smb_filename
*smb_fname
,
3569 TALLOC_CTX
*mem_ctx
,
3570 unsigned int *pnum_streams
,
3571 struct stream_struct
**pstreams
)
3573 struct fruit_config_data
*config
= NULL
;
3576 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3577 return NT_STATUS_INTERNAL_ERROR
);
3579 switch (config
->meta
) {
3580 case FRUIT_META_NETATALK
:
3581 status
= fruit_streaminfo_meta_netatalk(handle
, fsp
, smb_fname
,
3582 mem_ctx
, pnum_streams
,
3586 case FRUIT_META_STREAM
:
3587 status
= fruit_streaminfo_meta_stream(handle
, fsp
, smb_fname
,
3588 mem_ctx
, pnum_streams
,
3593 return NT_STATUS_INTERNAL_ERROR
;
3599 static NTSTATUS
fruit_streaminfo_rsrc_stream(
3600 vfs_handle_struct
*handle
,
3601 struct files_struct
*fsp
,
3602 const struct smb_filename
*smb_fname
,
3603 TALLOC_CTX
*mem_ctx
,
3604 unsigned int *pnum_streams
,
3605 struct stream_struct
**pstreams
)
3609 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
3611 DBG_ERR("Filtering resource stream failed\n");
3612 return NT_STATUS_INTERNAL_ERROR
;
3614 return NT_STATUS_OK
;
3617 static NTSTATUS
fruit_streaminfo_rsrc_xattr(
3618 vfs_handle_struct
*handle
,
3619 struct files_struct
*fsp
,
3620 const struct smb_filename
*smb_fname
,
3621 TALLOC_CTX
*mem_ctx
,
3622 unsigned int *pnum_streams
,
3623 struct stream_struct
**pstreams
)
3627 ok
= filter_empty_rsrc_stream(pnum_streams
, pstreams
);
3629 DBG_ERR("Filtering resource stream failed\n");
3630 return NT_STATUS_INTERNAL_ERROR
;
3632 return NT_STATUS_OK
;
3635 static NTSTATUS
fruit_streaminfo_rsrc_adouble(
3636 vfs_handle_struct
*handle
,
3637 struct files_struct
*fsp
,
3638 const struct smb_filename
*smb_fname
,
3639 TALLOC_CTX
*mem_ctx
,
3640 unsigned int *pnum_streams
,
3641 struct stream_struct
**pstreams
)
3643 struct stream_struct
*stream
= *pstreams
;
3644 unsigned int num_streams
= *pnum_streams
;
3645 struct adouble
*ad
= NULL
;
3651 * Check if there's a AFPRESOURCE_STREAM from the VFS streams backend
3652 * and if yes, remove it from the list
3654 for (i
= 0; i
< num_streams
; i
++) {
3655 if (strequal_m(stream
[i
].name
, AFPRESOURCE_STREAM
)) {
3660 if (i
< num_streams
) {
3661 DBG_WARNING("Unexpected AFPRESOURCE_STREAM on [%s]\n",
3662 smb_fname_str_dbg(smb_fname
));
3664 ok
= del_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3665 AFPRESOURCE_STREAM
);
3667 return NT_STATUS_INTERNAL_ERROR
;
3671 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_RSRC
);
3673 return NT_STATUS_OK
;
3676 rlen
= ad_getentrylen(ad
, ADEID_RFORK
);
3680 return NT_STATUS_OK
;
3683 ok
= add_fruit_stream(mem_ctx
, pnum_streams
, pstreams
,
3684 AFPRESOURCE_STREAM_NAME
, rlen
,
3685 smb_roundup(handle
->conn
, rlen
));
3687 return NT_STATUS_NO_MEMORY
;
3690 return NT_STATUS_OK
;
3693 static NTSTATUS
fruit_streaminfo_rsrc(vfs_handle_struct
*handle
,
3694 struct files_struct
*fsp
,
3695 const struct smb_filename
*smb_fname
,
3696 TALLOC_CTX
*mem_ctx
,
3697 unsigned int *pnum_streams
,
3698 struct stream_struct
**pstreams
)
3700 struct fruit_config_data
*config
= NULL
;
3703 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3704 return NT_STATUS_INTERNAL_ERROR
);
3706 switch (config
->rsrc
) {
3707 case FRUIT_RSRC_STREAM
:
3708 status
= fruit_streaminfo_rsrc_stream(handle
, fsp
, smb_fname
,
3709 mem_ctx
, pnum_streams
,
3713 case FRUIT_RSRC_XATTR
:
3714 status
= fruit_streaminfo_rsrc_xattr(handle
, fsp
, smb_fname
,
3715 mem_ctx
, pnum_streams
,
3719 case FRUIT_RSRC_ADFILE
:
3720 status
= fruit_streaminfo_rsrc_adouble(handle
, fsp
, smb_fname
,
3721 mem_ctx
, pnum_streams
,
3726 return NT_STATUS_INTERNAL_ERROR
;
3732 static void fruit_filter_empty_streams(unsigned int *pnum_streams
,
3733 struct stream_struct
**pstreams
)
3735 unsigned num_streams
= *pnum_streams
;
3736 struct stream_struct
*streams
= *pstreams
;
3739 if (!global_fruit_config
.nego_aapl
) {
3743 while (i
< num_streams
) {
3744 struct smb_filename smb_fname
= (struct smb_filename
) {
3745 .stream_name
= streams
[i
].name
,
3748 if (is_ntfs_default_stream_smb_fname(&smb_fname
)
3749 || streams
[i
].size
> 0)
3755 streams
[i
] = streams
[num_streams
- 1];
3759 *pnum_streams
= num_streams
;
3762 static NTSTATUS
fruit_streaminfo(vfs_handle_struct
*handle
,
3763 struct files_struct
*fsp
,
3764 const struct smb_filename
*smb_fname
,
3765 TALLOC_CTX
*mem_ctx
,
3766 unsigned int *pnum_streams
,
3767 struct stream_struct
**pstreams
)
3769 struct fruit_config_data
*config
= NULL
;
3772 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3773 return NT_STATUS_UNSUCCESSFUL
);
3775 DBG_DEBUG("Path [%s]\n", smb_fname_str_dbg(smb_fname
));
3777 status
= SMB_VFS_NEXT_STREAMINFO(handle
, fsp
, smb_fname
, mem_ctx
,
3778 pnum_streams
, pstreams
);
3779 if (!NT_STATUS_IS_OK(status
)) {
3783 fruit_filter_empty_streams(pnum_streams
, pstreams
);
3785 status
= fruit_streaminfo_meta(handle
, fsp
, smb_fname
,
3786 mem_ctx
, pnum_streams
, pstreams
);
3787 if (!NT_STATUS_IS_OK(status
)) {
3791 status
= fruit_streaminfo_rsrc(handle
, fsp
, smb_fname
,
3792 mem_ctx
, pnum_streams
, pstreams
);
3793 if (!NT_STATUS_IS_OK(status
)) {
3797 return NT_STATUS_OK
;
3800 static int fruit_ntimes(vfs_handle_struct
*handle
,
3801 const struct smb_filename
*smb_fname
,
3802 struct smb_file_time
*ft
)
3805 struct adouble
*ad
= NULL
;
3806 struct fruit_config_data
*config
= NULL
;
3808 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
3811 if ((config
->meta
!= FRUIT_META_NETATALK
) ||
3812 is_omit_timespec(&ft
->create_time
))
3814 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
3817 DEBUG(10,("set btime for %s to %s\n", smb_fname_str_dbg(smb_fname
),
3818 time_to_asc(convert_timespec_to_time_t(ft
->create_time
))));
3820 ad
= ad_get(talloc_tos(), handle
, smb_fname
, ADOUBLE_META
);
3825 ad_setdate(ad
, AD_DATE_CREATE
| AD_DATE_UNIX
,
3826 convert_time_t_to_uint32_t(ft
->create_time
.tv_sec
));
3828 rc
= ad_set(handle
, ad
, smb_fname
);
3834 DEBUG(1, ("fruit_ntimes: %s\n", smb_fname_str_dbg(smb_fname
)));
3837 return SMB_VFS_NEXT_NTIMES(handle
, smb_fname
, ft
);
3840 static int fruit_fallocate(struct vfs_handle_struct
*handle
,
3841 struct files_struct
*fsp
,
3846 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3849 return SMB_VFS_NEXT_FALLOCATE(handle
, fsp
, mode
, offset
, len
);
3852 /* Let the pwrite code path handle it. */
3857 static int fruit_ftruncate_rsrc_xattr(struct vfs_handle_struct
*handle
,
3858 struct files_struct
*fsp
,
3861 #ifdef HAVE_ATTROPEN
3862 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
3867 static int fruit_ftruncate_rsrc_adouble(struct vfs_handle_struct
*handle
,
3868 struct files_struct
*fsp
,
3872 struct adouble
*ad
= NULL
;
3875 ad
= ad_fget(talloc_tos(), handle
, fsp
, ADOUBLE_RSRC
);
3877 DBG_DEBUG("ad_get [%s] failed [%s]\n",
3878 fsp_str_dbg(fsp
), strerror(errno
));
3882 ad_off
= ad_getentryoff(ad
, ADEID_RFORK
);
3884 rc
= ftruncate(fsp
->fh
->fd
, offset
+ ad_off
);
3890 ad_setentrylen(ad
, ADEID_RFORK
, offset
);
3892 rc
= ad_fset(handle
, ad
, fsp
);
3894 DBG_ERR("ad_fset [%s] failed [%s]\n",
3895 fsp_str_dbg(fsp
), strerror(errno
));
3904 static int fruit_ftruncate_rsrc_stream(struct vfs_handle_struct
*handle
,
3905 struct files_struct
*fsp
,
3908 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
3911 static int fruit_ftruncate_rsrc(struct vfs_handle_struct
*handle
,
3912 struct files_struct
*fsp
,
3915 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3919 DBG_ERR("Failed to fetch fsp extension");
3923 switch (fio
->config
->rsrc
) {
3924 case FRUIT_RSRC_XATTR
:
3925 ret
= fruit_ftruncate_rsrc_xattr(handle
, fsp
, offset
);
3928 case FRUIT_RSRC_ADFILE
:
3929 ret
= fruit_ftruncate_rsrc_adouble(handle
, fsp
, offset
);
3932 case FRUIT_RSRC_STREAM
:
3933 ret
= fruit_ftruncate_rsrc_stream(handle
, fsp
, offset
);
3937 DBG_ERR("Unexpected rsrc config [%d]\n", fio
->config
->rsrc
);
3945 static int fruit_ftruncate_meta(struct vfs_handle_struct
*handle
,
3946 struct files_struct
*fsp
,
3950 DBG_WARNING("ftruncate %s to %jd",
3951 fsp_str_dbg(fsp
), (intmax_t)offset
);
3952 /* OS X returns NT_STATUS_ALLOTTED_SPACE_EXCEEDED */
3957 /* OS X returns success but does nothing */
3958 DBG_INFO("ignoring ftruncate %s to %jd\n",
3959 fsp_str_dbg(fsp
), (intmax_t)offset
);
3963 static int fruit_ftruncate(struct vfs_handle_struct
*handle
,
3964 struct files_struct
*fsp
,
3967 struct fio
*fio
= (struct fio
*)VFS_FETCH_FSP_EXTENSION(handle
, fsp
);
3970 DBG_DEBUG("Path [%s] offset [%"PRIdMAX
"]\n", fsp_str_dbg(fsp
),
3974 return SMB_VFS_NEXT_FTRUNCATE(handle
, fsp
, offset
);
3977 if (fio
->type
== ADOUBLE_META
) {
3978 ret
= fruit_ftruncate_meta(handle
, fsp
, offset
);
3980 ret
= fruit_ftruncate_rsrc(handle
, fsp
, offset
);
3983 DBG_DEBUG("Path [%s] result [%d]\n", fsp_str_dbg(fsp
), ret
);
3987 static NTSTATUS
fruit_create_file(vfs_handle_struct
*handle
,
3988 struct smb_request
*req
,
3989 uint16_t root_dir_fid
,
3990 struct smb_filename
*smb_fname
,
3991 uint32_t access_mask
,
3992 uint32_t share_access
,
3993 uint32_t create_disposition
,
3994 uint32_t create_options
,
3995 uint32_t file_attributes
,
3996 uint32_t oplock_request
,
3997 const struct smb2_lease
*lease
,
3998 uint64_t allocation_size
,
3999 uint32_t private_flags
,
4000 struct security_descriptor
*sd
,
4001 struct ea_list
*ea_list
,
4002 files_struct
**result
,
4004 const struct smb2_create_blobs
*in_context_blobs
,
4005 struct smb2_create_blobs
*out_context_blobs
)
4008 struct fruit_config_data
*config
= NULL
;
4009 files_struct
*fsp
= NULL
;
4010 bool internal_open
= (oplock_request
& INTERNAL_OPEN_ONLY
);
4013 status
= check_aapl(handle
, req
, in_context_blobs
, out_context_blobs
);
4014 if (!NT_STATUS_IS_OK(status
)) {
4018 SMB_VFS_HANDLE_GET_DATA(handle
, config
, struct fruit_config_data
,
4019 return NT_STATUS_UNSUCCESSFUL
);
4021 if (is_apple_stream(smb_fname
->stream_name
) && !internal_open
) {
4022 uint32_t conv_flags
= 0;
4024 if (config
->wipe_intentionally_left_blank_rfork
) {
4025 conv_flags
|= AD_CONV_WIPE_BLANK
;
4027 if (config
->delete_empty_adfiles
) {
4028 conv_flags
|= AD_CONV_DELETE
;
4031 ret
= ad_convert(handle
,
4032 handle
->conn
->cwd_fsp
,
4034 macos_string_replace_map
,
4037 DBG_ERR("ad_convert() failed\n");
4038 return NT_STATUS_UNSUCCESSFUL
;
4042 status
= SMB_VFS_NEXT_CREATE_FILE(
4043 handle
, req
, root_dir_fid
, smb_fname
,
4044 access_mask
, share_access
,
4045 create_disposition
, create_options
,
4046 file_attributes
, oplock_request
,
4048 allocation_size
, private_flags
,
4049 sd
, ea_list
, result
,
4050 pinfo
, in_context_blobs
, out_context_blobs
);
4051 if (!NT_STATUS_IS_OK(status
)) {
4057 if (global_fruit_config
.nego_aapl
) {
4058 if (config
->posix_rename
&& fsp
->is_directory
) {
4060 * Enable POSIX directory rename behaviour
4062 fsp
->posix_flags
|= FSP_POSIX_FLAGS_RENAME
;
4067 * If this is a plain open for existing files, opening an 0
4068 * byte size resource fork MUST fail with
4069 * NT_STATUS_OBJECT_NAME_NOT_FOUND.
4071 * Cf the vfs_fruit torture tests in test_rfork_create().
4073 if (global_fruit_config
.nego_aapl
&&
4074 create_disposition
== FILE_OPEN
&&
4075 smb_fname
->st
.st_ex_size
== 0 &&
4076 is_named_stream(smb_fname
))
4078 status
= NT_STATUS_OBJECT_NAME_NOT_FOUND
;
4082 if (is_named_stream(smb_fname
)
4083 || fsp
->is_directory
) {
4087 if ((config
->locking
== FRUIT_LOCKING_NETATALK
) &&
4090 status
= fruit_check_access(
4094 if (!NT_STATUS_IS_OK(status
)) {
4102 DEBUG(10, ("fruit_create_file: %s\n", nt_errstr(status
)));
4105 close_file(req
, fsp
, ERROR_CLOSE
);
4106 *result
= fsp
= NULL
;
4112 static NTSTATUS
fruit_readdir_attr(struct vfs_handle_struct
*handle
,
4113 const struct smb_filename
*fname
,
4114 TALLOC_CTX
*mem_ctx
,
4115 struct readdir_attr_data
**pattr_data
)
4117 struct fruit_config_data
*config
= NULL
;
4118 struct readdir_attr_data
*attr_data
;
4119 uint32_t conv_flags
= 0;
4123 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4124 struct fruit_config_data
,
4125 return NT_STATUS_UNSUCCESSFUL
);
4127 if (!global_fruit_config
.nego_aapl
) {
4128 return SMB_VFS_NEXT_READDIR_ATTR(handle
, fname
, mem_ctx
, pattr_data
);
4131 DEBUG(10, ("fruit_readdir_attr %s\n", fname
->base_name
));
4133 if (config
->wipe_intentionally_left_blank_rfork
) {
4134 conv_flags
|= AD_CONV_WIPE_BLANK
;
4136 if (config
->delete_empty_adfiles
) {
4137 conv_flags
|= AD_CONV_DELETE
;
4140 ret
= ad_convert(handle
,
4141 handle
->conn
->cwd_fsp
,
4143 macos_string_replace_map
,
4146 DBG_ERR("ad_convert() failed\n");
4147 return NT_STATUS_UNSUCCESSFUL
;
4150 *pattr_data
= talloc_zero(mem_ctx
, struct readdir_attr_data
);
4151 if (*pattr_data
== NULL
) {
4152 return NT_STATUS_UNSUCCESSFUL
;
4154 attr_data
= *pattr_data
;
4155 attr_data
->type
= RDATTR_AAPL
;
4158 * Mac metadata: compressed FinderInfo, resource fork length
4161 status
= readdir_attr_macmeta(handle
, fname
, attr_data
);
4162 if (!NT_STATUS_IS_OK(status
)) {
4164 * Error handling is tricky: if we return failure from
4165 * this function, the corresponding directory entry
4166 * will to be passed to the client, so we really just
4167 * want to error out on fatal errors.
4169 if (!NT_STATUS_EQUAL(status
, NT_STATUS_ACCESS_DENIED
)) {
4177 if (config
->unix_info_enabled
) {
4178 attr_data
->attr_data
.aapl
.unix_mode
= fname
->st
.st_ex_mode
;
4184 if (!config
->readdir_attr_max_access
) {
4185 attr_data
->attr_data
.aapl
.max_access
= FILE_GENERIC_ALL
;
4187 status
= smbd_calculate_access_mask(
4191 SEC_FLAG_MAXIMUM_ALLOWED
,
4192 &attr_data
->attr_data
.aapl
.max_access
);
4193 if (!NT_STATUS_IS_OK(status
)) {
4198 return NT_STATUS_OK
;
4201 DEBUG(1, ("fruit_readdir_attr %s, error: %s\n",
4202 fname
->base_name
, nt_errstr(status
)));
4203 TALLOC_FREE(*pattr_data
);
4207 static NTSTATUS
fruit_fget_nt_acl(vfs_handle_struct
*handle
,
4209 uint32_t security_info
,
4210 TALLOC_CTX
*mem_ctx
,
4211 struct security_descriptor
**ppdesc
)
4214 struct security_ace ace
;
4216 struct fruit_config_data
*config
;
4218 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4219 struct fruit_config_data
,
4220 return NT_STATUS_UNSUCCESSFUL
);
4222 status
= SMB_VFS_NEXT_FGET_NT_ACL(handle
, fsp
, security_info
,
4224 if (!NT_STATUS_IS_OK(status
)) {
4229 * Add MS NFS style ACEs with uid, gid and mode
4231 if (!global_fruit_config
.nego_aapl
) {
4232 return NT_STATUS_OK
;
4234 if (!config
->unix_info_enabled
) {
4235 return NT_STATUS_OK
;
4238 /* First remove any existing ACE's with NFS style mode/uid/gid SIDs. */
4239 status
= remove_virtual_nfs_aces(*ppdesc
);
4240 if (!NT_STATUS_IS_OK(status
)) {
4241 DBG_WARNING("failed to remove MS NFS style ACEs\n");
4245 /* MS NFS style mode */
4246 sid_compose(&sid
, &global_sid_Unix_NFS_Mode
, fsp
->fsp_name
->st
.st_ex_mode
);
4247 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4248 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4249 if (!NT_STATUS_IS_OK(status
)) {
4250 DEBUG(1,("failed to add MS NFS style ACE\n"));
4254 /* MS NFS style uid */
4255 sid_compose(&sid
, &global_sid_Unix_NFS_Users
, fsp
->fsp_name
->st
.st_ex_uid
);
4256 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4257 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4258 if (!NT_STATUS_IS_OK(status
)) {
4259 DEBUG(1,("failed to add MS NFS style ACE\n"));
4263 /* MS NFS style gid */
4264 sid_compose(&sid
, &global_sid_Unix_NFS_Groups
, fsp
->fsp_name
->st
.st_ex_gid
);
4265 init_sec_ace(&ace
, &sid
, SEC_ACE_TYPE_ACCESS_DENIED
, 0, 0);
4266 status
= security_descriptor_dacl_add(*ppdesc
, &ace
);
4267 if (!NT_STATUS_IS_OK(status
)) {
4268 DEBUG(1,("failed to add MS NFS style ACE\n"));
4272 return NT_STATUS_OK
;
4275 static NTSTATUS
fruit_fset_nt_acl(vfs_handle_struct
*handle
,
4277 uint32_t security_info_sent
,
4278 const struct security_descriptor
*orig_psd
)
4282 mode_t ms_nfs_mode
= 0;
4284 struct security_descriptor
*psd
= NULL
;
4285 uint32_t orig_num_aces
= 0;
4287 if (orig_psd
->dacl
!= NULL
) {
4288 orig_num_aces
= orig_psd
->dacl
->num_aces
;
4291 psd
= security_descriptor_copy(talloc_tos(), orig_psd
);
4293 return NT_STATUS_NO_MEMORY
;
4296 DBG_DEBUG("fruit_fset_nt_acl: %s\n", fsp_str_dbg(fsp
));
4298 status
= check_ms_nfs(handle
, fsp
, psd
, &ms_nfs_mode
, &do_chmod
);
4299 if (!NT_STATUS_IS_OK(status
)) {
4300 DEBUG(1, ("fruit_fset_nt_acl: check_ms_nfs failed%s\n", fsp_str_dbg(fsp
)));
4306 * If only ms_nfs ACE entries were sent, ensure we set the DACL
4307 * sent/present flags correctly now we've removed them.
4310 if (orig_num_aces
!= 0) {
4312 * Are there any ACE's left ?
4314 if (psd
->dacl
->num_aces
== 0) {
4315 /* No - clear the DACL sent/present flags. */
4316 security_info_sent
&= ~SECINFO_DACL
;
4317 psd
->type
&= ~SEC_DESC_DACL_PRESENT
;
4321 status
= SMB_VFS_NEXT_FSET_NT_ACL(handle
, fsp
, security_info_sent
, psd
);
4322 if (!NT_STATUS_IS_OK(status
)) {
4323 DEBUG(1, ("fruit_fset_nt_acl: SMB_VFS_NEXT_FSET_NT_ACL failed%s\n", fsp_str_dbg(fsp
)));
4329 result
= SMB_VFS_FCHMOD(fsp
, ms_nfs_mode
);
4331 DBG_WARNING("%s, result: %d, %04o error %s\n",
4334 (unsigned)ms_nfs_mode
,
4336 status
= map_nt_error_from_unix(errno
);
4343 return NT_STATUS_OK
;
4346 static struct vfs_offload_ctx
*fruit_offload_ctx
;
4348 struct fruit_offload_read_state
{
4349 struct vfs_handle_struct
*handle
;
4350 struct tevent_context
*ev
;
4356 static void fruit_offload_read_done(struct tevent_req
*subreq
);
4358 static struct tevent_req
*fruit_offload_read_send(
4359 TALLOC_CTX
*mem_ctx
,
4360 struct tevent_context
*ev
,
4361 struct vfs_handle_struct
*handle
,
4368 struct tevent_req
*req
= NULL
;
4369 struct tevent_req
*subreq
= NULL
;
4370 struct fruit_offload_read_state
*state
= NULL
;
4372 req
= tevent_req_create(mem_ctx
, &state
,
4373 struct fruit_offload_read_state
);
4377 *state
= (struct fruit_offload_read_state
) {
4384 subreq
= SMB_VFS_NEXT_OFFLOAD_READ_SEND(mem_ctx
, ev
, handle
, fsp
,
4385 fsctl
, ttl
, offset
, to_copy
);
4386 if (tevent_req_nomem(subreq
, req
)) {
4387 return tevent_req_post(req
, ev
);
4389 tevent_req_set_callback(subreq
, fruit_offload_read_done
, req
);
4393 static void fruit_offload_read_done(struct tevent_req
*subreq
)
4395 struct tevent_req
*req
= tevent_req_callback_data(
4396 subreq
, struct tevent_req
);
4397 struct fruit_offload_read_state
*state
= tevent_req_data(
4398 req
, struct fruit_offload_read_state
);
4401 status
= SMB_VFS_NEXT_OFFLOAD_READ_RECV(subreq
,
4405 TALLOC_FREE(subreq
);
4406 if (tevent_req_nterror(req
, status
)) {
4410 if (state
->fsctl
!= FSCTL_SRV_REQUEST_RESUME_KEY
) {
4411 tevent_req_done(req
);
4415 status
= vfs_offload_token_ctx_init(state
->fsp
->conn
->sconn
->client
,
4416 &fruit_offload_ctx
);
4417 if (tevent_req_nterror(req
, status
)) {
4421 status
= vfs_offload_token_db_store_fsp(fruit_offload_ctx
,
4424 if (tevent_req_nterror(req
, status
)) {
4428 tevent_req_done(req
);
4432 static NTSTATUS
fruit_offload_read_recv(struct tevent_req
*req
,
4433 struct vfs_handle_struct
*handle
,
4434 TALLOC_CTX
*mem_ctx
,
4437 struct fruit_offload_read_state
*state
= tevent_req_data(
4438 req
, struct fruit_offload_read_state
);
4441 if (tevent_req_is_nterror(req
, &status
)) {
4442 tevent_req_received(req
);
4446 token
->length
= state
->token
.length
;
4447 token
->data
= talloc_move(mem_ctx
, &state
->token
.data
);
4449 tevent_req_received(req
);
4450 return NT_STATUS_OK
;
4453 struct fruit_offload_write_state
{
4454 struct vfs_handle_struct
*handle
;
4456 struct files_struct
*src_fsp
;
4457 struct files_struct
*dst_fsp
;
4461 static void fruit_offload_write_done(struct tevent_req
*subreq
);
4462 static struct tevent_req
*fruit_offload_write_send(struct vfs_handle_struct
*handle
,
4463 TALLOC_CTX
*mem_ctx
,
4464 struct tevent_context
*ev
,
4467 off_t transfer_offset
,
4468 struct files_struct
*dest_fsp
,
4472 struct tevent_req
*req
, *subreq
;
4473 struct fruit_offload_write_state
*state
;
4475 struct fruit_config_data
*config
;
4476 off_t src_off
= transfer_offset
;
4477 files_struct
*src_fsp
= NULL
;
4478 off_t to_copy
= num
;
4479 bool copyfile_enabled
= false;
4481 DEBUG(10,("soff: %ju, doff: %ju, len: %ju\n",
4482 (uintmax_t)src_off
, (uintmax_t)dest_off
, (uintmax_t)num
));
4484 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
4485 struct fruit_config_data
,
4488 req
= tevent_req_create(mem_ctx
, &state
,
4489 struct fruit_offload_write_state
);
4493 state
->handle
= handle
;
4494 state
->dst_fsp
= dest_fsp
;
4497 case FSCTL_SRV_COPYCHUNK
:
4498 case FSCTL_SRV_COPYCHUNK_WRITE
:
4499 copyfile_enabled
= config
->copyfile_enabled
;
4506 * Check if this a OS X copyfile style copychunk request with
4507 * a requested chunk count of 0 that was translated to a
4508 * offload_write_send VFS call overloading the parameters src_off
4509 * = dest_off = num = 0.
4511 if (copyfile_enabled
&& num
== 0 && src_off
== 0 && dest_off
== 0) {
4512 status
= vfs_offload_token_db_fetch_fsp(
4513 fruit_offload_ctx
, token
, &src_fsp
);
4514 if (tevent_req_nterror(req
, status
)) {
4515 return tevent_req_post(req
, ev
);
4517 state
->src_fsp
= src_fsp
;
4519 status
= vfs_stat_fsp(src_fsp
);
4520 if (tevent_req_nterror(req
, status
)) {
4521 return tevent_req_post(req
, ev
);
4524 to_copy
= src_fsp
->fsp_name
->st
.st_ex_size
;
4525 state
->is_copyfile
= true;
4528 subreq
= SMB_VFS_NEXT_OFFLOAD_WRITE_SEND(handle
,
4537 if (tevent_req_nomem(subreq
, req
)) {
4538 return tevent_req_post(req
, ev
);
4541 tevent_req_set_callback(subreq
, fruit_offload_write_done
, req
);
4545 static void fruit_offload_write_done(struct tevent_req
*subreq
)
4547 struct tevent_req
*req
= tevent_req_callback_data(
4548 subreq
, struct tevent_req
);
4549 struct fruit_offload_write_state
*state
= tevent_req_data(
4550 req
, struct fruit_offload_write_state
);
4552 unsigned int num_streams
= 0;
4553 struct stream_struct
*streams
= NULL
;
4555 struct smb_filename
*src_fname_tmp
= NULL
;
4556 struct smb_filename
*dst_fname_tmp
= NULL
;
4558 status
= SMB_VFS_NEXT_OFFLOAD_WRITE_RECV(state
->handle
,
4561 TALLOC_FREE(subreq
);
4562 if (tevent_req_nterror(req
, status
)) {
4566 if (!state
->is_copyfile
) {
4567 tevent_req_done(req
);
4572 * Now copy all remaining streams. We know the share supports
4573 * streams, because we're in vfs_fruit. We don't do this async
4574 * because streams are few and small.
4576 status
= vfs_streaminfo(state
->handle
->conn
, state
->src_fsp
,
4577 state
->src_fsp
->fsp_name
,
4578 req
, &num_streams
, &streams
);
4579 if (tevent_req_nterror(req
, status
)) {
4583 if (num_streams
== 1) {
4584 /* There is always one stream, ::$DATA. */
4585 tevent_req_done(req
);
4589 for (i
= 0; i
< num_streams
; i
++) {
4590 DEBUG(10, ("%s: stream: '%s'/%zu\n",
4591 __func__
, streams
[i
].name
, (size_t)streams
[i
].size
));
4593 src_fname_tmp
= synthetic_smb_fname(
4595 state
->src_fsp
->fsp_name
->base_name
,
4598 state
->src_fsp
->fsp_name
->flags
);
4599 if (tevent_req_nomem(src_fname_tmp
, req
)) {
4603 if (is_ntfs_default_stream_smb_fname(src_fname_tmp
)) {
4604 TALLOC_FREE(src_fname_tmp
);
4608 dst_fname_tmp
= synthetic_smb_fname(
4610 state
->dst_fsp
->fsp_name
->base_name
,
4613 state
->dst_fsp
->fsp_name
->flags
);
4614 if (tevent_req_nomem(dst_fname_tmp
, req
)) {
4615 TALLOC_FREE(src_fname_tmp
);
4619 status
= copy_file(req
,
4620 state
->handle
->conn
,
4623 OPENX_FILE_CREATE_IF_NOT_EXIST
,
4625 if (!NT_STATUS_IS_OK(status
)) {
4626 DEBUG(1, ("%s: copy %s to %s failed: %s\n", __func__
,
4627 smb_fname_str_dbg(src_fname_tmp
),
4628 smb_fname_str_dbg(dst_fname_tmp
),
4629 nt_errstr(status
)));
4630 TALLOC_FREE(src_fname_tmp
);
4631 TALLOC_FREE(dst_fname_tmp
);
4632 tevent_req_nterror(req
, status
);
4636 TALLOC_FREE(src_fname_tmp
);
4637 TALLOC_FREE(dst_fname_tmp
);
4640 TALLOC_FREE(streams
);
4641 TALLOC_FREE(src_fname_tmp
);
4642 TALLOC_FREE(dst_fname_tmp
);
4643 tevent_req_done(req
);
4646 static NTSTATUS
fruit_offload_write_recv(struct vfs_handle_struct
*handle
,
4647 struct tevent_req
*req
,
4650 struct fruit_offload_write_state
*state
= tevent_req_data(
4651 req
, struct fruit_offload_write_state
);
4654 if (tevent_req_is_nterror(req
, &status
)) {
4655 DEBUG(1, ("server side copy chunk failed: %s\n",
4656 nt_errstr(status
)));
4658 tevent_req_received(req
);
4662 *copied
= state
->copied
;
4663 tevent_req_received(req
);
4665 return NT_STATUS_OK
;
4668 static char *fruit_get_bandsize_line(char **lines
, int numlines
)
4671 static bool re_initialized
= false;
4675 if (!re_initialized
) {
4676 ret
= regcomp(&re
, "^[[:blank:]]*<key>band-size</key>$", 0);
4680 re_initialized
= true;
4683 for (i
= 0; i
< numlines
; i
++) {
4684 regmatch_t matches
[1];
4686 ret
= regexec(&re
, lines
[i
], 1, matches
, 0);
4689 * Check if the match was on the last line, sa we want
4690 * the subsequent line.
4692 if (i
+ 1 == numlines
) {
4695 return lines
[i
+ 1];
4697 if (ret
!= REG_NOMATCH
) {
4705 static bool fruit_get_bandsize_from_line(char *line
, size_t *_band_size
)
4708 static bool re_initialized
= false;
4709 regmatch_t matches
[2];
4714 if (!re_initialized
) {
4717 "<integer>\\([[:digit:]]*\\)</integer>$",
4722 re_initialized
= true;
4725 ret
= regexec(&re
, line
, 2, matches
, 0);
4727 DBG_ERR("regex failed [%s]\n", line
);
4731 line
[matches
[1].rm_eo
] = '\0';
4733 ok
= conv_str_u64(&line
[matches
[1].rm_so
], &band_size
);
4737 *_band_size
= (size_t)band_size
;
4742 * This reads and parses an Info.plist from a TM sparsebundle looking for the
4743 * "band-size" key and value.
4745 static bool fruit_get_bandsize(vfs_handle_struct
*handle
,
4749 #define INFO_PLIST_MAX_SIZE 64*1024
4751 struct smb_filename
*smb_fname
= NULL
;
4752 files_struct
*fsp
= NULL
;
4753 uint8_t *file_data
= NULL
;
4754 char **lines
= NULL
;
4755 char *band_size_line
= NULL
;
4756 size_t plist_file_size
;
4763 plist
= talloc_asprintf(talloc_tos(),
4765 handle
->conn
->connectpath
,
4767 if (plist
== NULL
) {
4772 smb_fname
= synthetic_smb_fname(talloc_tos(), plist
, NULL
, NULL
, 0);
4773 if (smb_fname
== NULL
) {
4778 ret
= SMB_VFS_NEXT_LSTAT(handle
, smb_fname
);
4780 DBG_INFO("Ignoring Sparsebundle without Info.plist [%s]\n", dir
);
4785 plist_file_size
= smb_fname
->st
.st_ex_size
;
4787 if (plist_file_size
> INFO_PLIST_MAX_SIZE
) {
4788 DBG_INFO("%s is too large, ignoring\n", plist
);
4793 status
= SMB_VFS_NEXT_CREATE_FILE(
4796 0, /* root_dir_fid */
4797 smb_fname
, /* fname */
4798 FILE_GENERIC_READ
, /* access_mask */
4799 FILE_SHARE_READ
| FILE_SHARE_WRITE
, /* share_access */
4800 FILE_OPEN
, /* create_disposition */
4801 0, /* create_options */
4802 0, /* file_attributes */
4803 INTERNAL_OPEN_ONLY
, /* oplock_request */
4805 0, /* allocation_size */
4806 0, /* private_flags */
4811 NULL
, NULL
); /* create context */
4812 if (!NT_STATUS_IS_OK(status
)) {
4813 DBG_INFO("Opening [%s] failed [%s]\n",
4814 smb_fname_str_dbg(smb_fname
), nt_errstr(status
));
4819 file_data
= talloc_array(talloc_tos(), uint8_t, plist_file_size
);
4820 if (file_data
== NULL
) {
4825 nread
= SMB_VFS_NEXT_PREAD(handle
, fsp
, file_data
, plist_file_size
, 0);
4826 if (nread
!= plist_file_size
) {
4827 DBG_ERR("Short read on [%s]: %zu/%zd\n",
4828 fsp_str_dbg(fsp
), nread
, plist_file_size
);
4834 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
4836 if (!NT_STATUS_IS_OK(status
)) {
4837 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
4842 lines
= file_lines_parse((char *)file_data
,
4846 if (lines
== NULL
) {
4851 band_size_line
= fruit_get_bandsize_line(lines
, numlines
);
4852 if (band_size_line
== NULL
) {
4853 DBG_ERR("Didn't find band-size key in [%s]\n",
4854 smb_fname_str_dbg(smb_fname
));
4859 ok
= fruit_get_bandsize_from_line(band_size_line
, band_size
);
4861 DBG_ERR("fruit_get_bandsize_from_line failed\n");
4865 DBG_DEBUG("Parsed band-size [%zu] for [%s]\n", *band_size
, plist
);
4869 status
= close_file(NULL
, fsp
, NORMAL_CLOSE
);
4870 if (!NT_STATUS_IS_OK(status
)) {
4871 DBG_ERR("close_file failed: %s\n", nt_errstr(status
));
4876 TALLOC_FREE(smb_fname
);
4877 TALLOC_FREE(file_data
);
4882 struct fruit_disk_free_state
{
4886 static bool fruit_get_num_bands(vfs_handle_struct
*handle
,
4891 struct smb_filename
*bands_dir
= NULL
;
4893 struct dirent
*e
= NULL
;
4897 path
= talloc_asprintf(talloc_tos(),
4899 handle
->conn
->connectpath
,
4905 bands_dir
= synthetic_smb_fname(talloc_tos(),
4911 if (bands_dir
== NULL
) {
4915 d
= SMB_VFS_NEXT_OPENDIR(handle
, bands_dir
, NULL
, 0);
4917 TALLOC_FREE(bands_dir
);
4923 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
4925 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
))
4927 if (ISDOT(e
->d_name
) || ISDOTDOT(e
->d_name
)) {
4933 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
4935 TALLOC_FREE(bands_dir
);
4939 DBG_DEBUG("%zu bands in [%s]\n", nbands
, smb_fname_str_dbg(bands_dir
));
4941 TALLOC_FREE(bands_dir
);
4947 static bool fruit_tmsize_do_dirent(vfs_handle_struct
*handle
,
4948 struct fruit_disk_free_state
*state
,
4953 size_t sparsebundle_strlen
= strlen("sparsebundle");
4954 size_t bandsize
= 0;
4958 p
= strstr(e
->d_name
, "sparsebundle");
4963 if (p
[sparsebundle_strlen
] != '\0') {
4967 DBG_DEBUG("Processing sparsebundle [%s]\n", e
->d_name
);
4969 ok
= fruit_get_bandsize(handle
, e
->d_name
, &bandsize
);
4972 * Beware of race conditions: this may be an uninitialized
4973 * Info.plist that a client is just creating. We don't want let
4974 * this to trigger complete failure.
4976 DBG_ERR("Processing sparsebundle [%s] failed\n", e
->d_name
);
4980 ok
= fruit_get_num_bands(handle
, e
->d_name
, &nbands
);
4983 * Beware of race conditions: this may be a backup sparsebundle
4984 * in an early stage lacking a bands subdirectory. We don't want
4985 * let this to trigger complete failure.
4987 DBG_ERR("Processing sparsebundle [%s] failed\n", e
->d_name
);
4992 * Arithmetic on 32-bit systems may cause overflow, depending on
4993 * size_t precision. First we check its unlikely, then we
4994 * force the precision into target off_t, then we check that
4995 * the total did not overflow either.
4997 if (bandsize
> SIZE_MAX
/nbands
) {
4998 DBG_ERR("tmsize potential overflow: bandsize [%zu] nbands [%zu]\n",
5002 tm_size
= (off_t
)bandsize
* (off_t
)nbands
;
5004 if (state
->total_size
+ tm_size
< state
->total_size
) {
5005 DBG_ERR("tm total size overflow: bandsize [%zu] nbands [%zu]\n",
5010 state
->total_size
+= tm_size
;
5012 DBG_DEBUG("[%s] tm_size [%jd] total_size [%jd]\n",
5013 e
->d_name
, (intmax_t)tm_size
, (intmax_t)state
->total_size
);
5019 * Calculate used size of a TimeMachine volume
5021 * This assumes that the volume is used only for TimeMachine.
5023 * - readdir(basedir of share), then
5024 * - for every element that matches regex "^\(.*\)\.sparsebundle$" :
5025 * - parse "\1.sparsebundle/Info.plist" and read the band-size XML key
5026 * - count band files in "\1.sparsebundle/bands/"
5027 * - calculate used size of all bands: band_count * band_size
5029 static uint64_t fruit_disk_free(vfs_handle_struct
*handle
,
5030 const struct smb_filename
*smb_fname
,
5035 struct fruit_config_data
*config
= NULL
;
5036 struct fruit_disk_free_state state
= {0};
5038 struct dirent
*e
= NULL
;
5044 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5045 struct fruit_config_data
,
5048 if (!config
->time_machine
||
5049 config
->time_machine_max_size
== 0)
5051 return SMB_VFS_NEXT_DISK_FREE(handle
,
5058 d
= SMB_VFS_NEXT_OPENDIR(handle
, smb_fname
, NULL
, 0);
5063 for (e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
);
5065 e
= SMB_VFS_NEXT_READDIR(handle
, d
, NULL
))
5067 ok
= fruit_tmsize_do_dirent(handle
, &state
, e
);
5069 SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
5074 ret
= SMB_VFS_NEXT_CLOSEDIR(handle
, d
);
5079 dsize
= config
->time_machine_max_size
/ 512;
5080 dfree
= dsize
- (state
.total_size
/ 512);
5081 if (dfree
> dsize
) {
5091 static uint64_t fruit_fs_file_id(struct vfs_handle_struct
*handle
,
5092 const SMB_STRUCT_STAT
*psbuf
)
5094 struct fruit_config_data
*config
= NULL
;
5096 SMB_VFS_HANDLE_GET_DATA(handle
, config
,
5097 struct fruit_config_data
,
5100 if (global_fruit_config
.nego_aapl
&&
5101 config
->aapl_zero_file_id
)
5106 return SMB_VFS_NEXT_FS_FILE_ID(handle
, psbuf
);
5109 static struct vfs_fn_pointers vfs_fruit_fns
= {
5110 .connect_fn
= fruit_connect
,
5111 .disk_free_fn
= fruit_disk_free
,
5113 /* File operations */
5114 .chmod_fn
= fruit_chmod
,
5115 .unlinkat_fn
= fruit_unlinkat
,
5116 .renameat_fn
= fruit_renameat
,
5117 .open_fn
= fruit_open
,
5118 .close_fn
= fruit_close
,
5119 .pread_fn
= fruit_pread
,
5120 .pwrite_fn
= fruit_pwrite
,
5121 .pread_send_fn
= fruit_pread_send
,
5122 .pread_recv_fn
= fruit_pread_recv
,
5123 .pwrite_send_fn
= fruit_pwrite_send
,
5124 .pwrite_recv_fn
= fruit_pwrite_recv
,
5125 .stat_fn
= fruit_stat
,
5126 .lstat_fn
= fruit_lstat
,
5127 .fstat_fn
= fruit_fstat
,
5128 .streaminfo_fn
= fruit_streaminfo
,
5129 .ntimes_fn
= fruit_ntimes
,
5130 .ftruncate_fn
= fruit_ftruncate
,
5131 .fallocate_fn
= fruit_fallocate
,
5132 .create_file_fn
= fruit_create_file
,
5133 .readdir_attr_fn
= fruit_readdir_attr
,
5134 .offload_read_send_fn
= fruit_offload_read_send
,
5135 .offload_read_recv_fn
= fruit_offload_read_recv
,
5136 .offload_write_send_fn
= fruit_offload_write_send
,
5137 .offload_write_recv_fn
= fruit_offload_write_recv
,
5138 .fs_file_id_fn
= fruit_fs_file_id
,
5140 /* NT ACL operations */
5141 .fget_nt_acl_fn
= fruit_fget_nt_acl
,
5142 .fset_nt_acl_fn
= fruit_fset_nt_acl
,
5146 NTSTATUS
vfs_fruit_init(TALLOC_CTX
*ctx
)
5148 NTSTATUS ret
= smb_register_vfs(SMB_VFS_INTERFACE_VERSION
, "fruit",
5150 if (!NT_STATUS_IS_OK(ret
)) {
5154 vfs_fruit_debug_level
= debug_add_class("fruit");
5155 if (vfs_fruit_debug_level
== -1) {
5156 vfs_fruit_debug_level
= DBGC_VFS
;
5157 DEBUG(0, ("%s: Couldn't register custom debugging class!\n",
5160 DEBUG(10, ("%s: Debug class number of '%s': %d\n",
5161 "vfs_fruit_init","fruit",vfs_fruit_debug_level
));